🎨 add remove disabled node pass

This commit is contained in:
ValKmjolnir 2024-11-14 00:19:58 +08:00
parent 1cc5202f8b
commit 13fd1ed7ed
8 changed files with 145 additions and 28 deletions

View File

@ -92,4 +92,18 @@ jobs:
zip colgm-linux-x86_64-nightly.zip colgm.ll
zip -r colgm-linux-x86_64-nightly.zip bootstrap
zip -r colgm-linux-x86_64-nightly.zip doc
zip -r colgm-linux-x86_64-nightly.zip src
zip -r colgm-linux-x86_64-nightly.zip src
windows-x86_64-build-bootstrap:
runs-on: windows-latest
steps:
- name: checkout
uses: actions/checkout@v4
- name: fetch llvm-dev
run: |
choco install -y llvm
clang --version
- name: build
run: |
mkdir build
cd build

View File

@ -71,4 +71,10 @@ target_include_directories(colgm-object PRIVATE ${CMAKE_SOURCE_DIR})
# build colgm
add_executable(colgm ${CMAKE_SOURCE_DIR}/main.cpp)
target_link_libraries(colgm colgm-object colgm-ast colgm-sir colgm-mir colgm-package colgm-sema)
target_link_libraries(colgm
colgm-object
colgm-ast
colgm-sir
colgm-mir
colgm-package
colgm-sema)

View File

@ -282,6 +282,15 @@ pub struct ptr_vec {
}
impl ptr_vec {
pub func instance() -> ptr_vec {
var res = ptr_vec {
data: nil => ast**,
size: 0 => u64,
capacity: 0 => u64
};
res.init();
return res;
}
pub func init(self) {
var ptr_size = 8 => u64; // now we only support 64bit
self->size = 0 => u64;

View File

@ -1,5 +1,6 @@
use std::io::{ io };
use std::libc::{ exit };
use misc::{ get_platform, get_arch };
pub struct cli_option {
OPTION_VIEW_TOKEN: bool,
@ -101,28 +102,3 @@ pub func report_invalid_argument(arg: i8*) {
.out(" for help.\n\n");
exit(-1 => i32);
}
#[enable_if(target_os = "macos")]
pub func get_platform() -> i8* {
return "macos";
}
#[enable_if(target_os = "linux")]
pub func get_platform() -> i8* {
return "linux";
}
#[enable_if(target_os = "windows")]
pub func get_platform() -> i8* {
return "windows";
}
#[enable_if(arch = "x86_64")]
pub func get_arch() -> i8* {
return "x86_64";
}
#[enable_if(arch = "aarch64")]
pub func get_arch() -> i8* {
return "aarch64";
}

View File

@ -1,6 +1,6 @@
use std::libc::{ free };
use lexer::{ lexer };
use parser::{ parser };
use parse::parser::{ parser };
use package::{ package };
use sema::sema::{ sema };
use err::report::{ report };

25
src/misc.colgm Normal file
View File

@ -0,0 +1,25 @@
#[enable_if(target_os = "macos")]
pub func get_platform() -> i8* {
return "macos";
}
#[enable_if(target_os = "linux")]
pub func get_platform() -> i8* {
return "linux";
}
#[enable_if(target_os = "windows")]
pub func get_platform() -> i8* {
return "windows";
}
#[enable_if(arch = "x86_64")]
pub func get_arch() -> i8* {
return "x86_64";
}
#[enable_if(arch = "aarch64")]
pub func get_arch() -> i8* {
return "aarch64";
}

View File

@ -6,6 +6,7 @@ use lexer::{ lexer, vec_token, tok_kind };
use std::libc::{ free, streq };
use std::io::{ io };
use std::str::{ str };
use parse::remove_disabled_node::{ remove_disabled_node };
pub struct parser {
err: report*,
@ -1255,6 +1256,8 @@ impl parser {
self->next();
}
self->update_location(self->root => ast*);
remove_disabled_node(self->root);
}
}

View File

@ -0,0 +1,84 @@
use ast::ast::{ ast_kind, ptr_vec, ast, root, ast_cond_compile };
use std::str::{ str };
use std::io::{ io };
use misc::{ get_platform, get_arch };
pub func check_enable_if(acc: ast_cond_compile*) -> bool {
if (!acc->cond_name.eq_const("enable_if")) {
return false;
}
var key_target_os = str::instance();
var key_arch = str::instance();
key_target_os.append_i8_vec("target_os");
key_arch.append_i8_vec("arch");
for (var i = acc->conds.iter(); !i.is_end(); i = i.next()) {
if (!i.key()->eq(key_target_os.__ptr__()) &&
!i.key()->eq(key_arch.__ptr__())) {
return false;
}
}
if (acc->conds.has(key_target_os.__ptr__()) &&
acc->conds.has(key_arch.__ptr__())) {
var target_os = acc->conds.get(key_target_os.__ptr__());
var arch = acc->conds.get(key_arch.__ptr__());
key_target_os.delete();
key_arch.delete();
return target_os->eq_const(get_platform()) &&
arch->eq_const(get_arch());
}
if (acc->conds.has(key_target_os.__ptr__())) {
var target_os = acc->conds.get(key_target_os.__ptr__());
key_target_os.delete();
key_arch.delete();
return target_os->eq_const(get_platform());
}
if (acc->conds.has(key_arch.__ptr__())) {
var arch = acc->conds.get(key_arch.__ptr__());
key_target_os.delete();
key_arch.delete();
return arch->eq_const(get_arch());
}
return false;
}
pub func remove_disabled_node(node: root*) {
var remove_count = 0;
var new_vec = ptr_vec::instance();
var size = node->decls.size;
for (var i = 0 => u64; i < size; i += 1 => u64) {
var d = node->decls.get(i);
if (d->kind != ast_kind::ast_cond_compile) {
new_vec.push(d);
continue;
}
if (check_enable_if(d => ast_cond_compile*)) {
var acc = d => ast_cond_compile*;
new_vec.push(acc->enabled_decl);
acc->enabled_decl = nil => ast*;
acc->delete();
} else {
remove_count += 1;
d->delete();
}
}
var old_vec = node->decls;
for (var i = 0 => u64; i < size; i += 1 => u64) {
old_vec.data[i] = nil => ast*;
}
old_vec.delete();
node->decls = new_vec;
if (remove_count > 0) {
io::stdout().out("[").green().out("ast").reset().out("]");
io::stdout().out(" run remove disabled node pass: ");
io::stdout().out_i64(remove_count).out(" node(s) removed\n");
}
return;
}