Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions src/b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ pub unsafe fn scope_pop(vars: *mut Array<Array<Var>>) {
}

pub unsafe fn find_var_near(vars: *const Array<Var>, name: *const c_char) -> *const Var {
for i in 0..(*vars).count {
let var = (*vars).items.add(i);
for var in (*vars).iter_mut() {
if strcmp((*var).name, name) == 0 {
return var
}
Expand All @@ -173,7 +172,7 @@ pub unsafe fn find_var_near(vars: *const Array<Var>, name: *const c_char) -> *co
pub unsafe fn find_var_deep(vars: *const Array<Array<Var>>, name: *const c_char) -> *const Var {
let mut i = (*vars).count;
while i > 0 {
let var = find_var_near((*vars).items.add(i-1), name);
let var = find_var_near((*vars).at(i-1), name);
if !var.is_null() {
return var;
}
Expand Down Expand Up @@ -214,8 +213,7 @@ pub struct Goto {
}

pub unsafe fn find_goto_label(labels: *const Array<GotoLabel>, name: *const c_char) -> *const GotoLabel {
for i in 0..(*labels).count {
let label = (*labels).items.add(i);
for label in (*labels).iter_mut() {
if strcmp((*label).name, name) == 0 {
return label
}
Expand Down Expand Up @@ -656,8 +654,8 @@ pub unsafe fn compile_block(l: *mut Lexer, c: *mut Compiler) -> Option<()> {
}

pub unsafe fn name_declare_if_not_exists(names: *mut Array<*const c_char>, name: *const c_char) {
for i in 0..(*names).count {
if strcmp(*(*names).items.add(i), name) == 0 {
for existing_name in (*names).iter() {
if strcmp(existing_name, name) == 0 {
return;
}
}
Expand Down Expand Up @@ -970,7 +968,7 @@ pub unsafe fn compile_program(l: *mut Lexer, c: *mut Compiler) -> Option<()> {
get_and_expect_token_but_continue(l, c, Token::ID)?;
let func = arena::strdup(&mut (*c).arena, (*l).string);
let func_loc = (*l).loc;
if let Some(existing_variadic) = assoc_lookup_cstr(da_slice((*c).program.variadics), func) {
if let Some(existing_variadic) = assoc_lookup_cstr((*c).program.variadics, func) {
// TODO: report all the duplicate variadics maybe?
diagf!(func_loc, c!("ERROR: duplicate variadic declaration `%s`\n"), func);
diagf!((*existing_variadic).loc, c!("NOTE: the first declaration is located here\n"));
Expand Down Expand Up @@ -1033,15 +1031,14 @@ pub unsafe fn compile_program(l: *mut Lexer, c: *mut Compiler) -> Option<()> {
compile_statement(l, c)?;
scope_pop(&mut (*c).vars); // end function scope

for i in 0..(*c).func_gotos.count {
let used_label = *(*c).func_gotos.items.add(i);
for used_label in (*c).func_gotos.iter() {
let existing_label = find_goto_label(&(*c).func_goto_labels, used_label.name);
if existing_label.is_null() {
diagf!(used_label.loc, c!("ERROR: label `%s` used but not defined\n"), used_label.name);
bump_error_count(c)?;
continue;
}
(*(*c).func_body.items.add(used_label.addr)).opcode = Op::JmpLabel {label: (*existing_label).label};
(*(*c).func_body.at(used_label.addr)).opcode = Op::JmpLabel {label: (*existing_label).label};
}

da_append(&mut (*c).program.funcs, Func {
Expand Down Expand Up @@ -1193,10 +1190,10 @@ pub unsafe fn get_garbage_base(path: *const c_char, target: Target) -> Option<*m
Some(temp_sprintf(c!("%s/%s.%s"), garbage_dir, filename, target.api.name()))
}

pub unsafe fn print_available_targets(targets: *const [Target]) {
pub unsafe fn print_available_targets(targets: Array<Target>) {
fprintf(stderr(), c!("Compilation targets:\n"));
for i in 0..targets.len() {
fprintf(stderr(), c!(" %s\n"), (*targets)[i].api.name());
for target in targets.iter() {
fprintf(stderr(), c!(" %s\n"), target.api.name());
}
}

Expand All @@ -1206,13 +1203,13 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
let default_target;
// TODO: maybe instead of gas_ the prefix should be gnu_, 'cause that makes more sense.
if cfg!(target_arch = "aarch64") && (cfg!(target_os = "linux") || cfg!(target_os = "android")) {
default_target = Some(Target::by_name(da_slice(targets), c!("gas-aarch64-linux")).expect("Default target for Linux on AArch64"));
default_target = Some(Target::by_name(targets, c!("gas-aarch64-linux")).expect("Default target for Linux on AArch64"));
} else if cfg!(target_arch = "aarch64") && cfg!(target_os = "macos") {
default_target = Some(Target::by_name(da_slice(targets), c!("gas-aarch64-darwin")).expect("Default target for Darwin on AArch64"));
default_target = Some(Target::by_name(targets, c!("gas-aarch64-darwin")).expect("Default target for Darwin on AArch64"));
} else if cfg!(target_arch = "x86_64") && cfg!(target_os = "linux") {
default_target = Some(Target::by_name(da_slice(targets), c!("gas-x86_64-linux")).expect("Default target for Linux on x86_64"));
default_target = Some(Target::by_name(targets, c!("gas-x86_64-linux")).expect("Default target for Linux on x86_64"));
} else if cfg!(target_arch = "x86_64") && cfg!(target_os = "windows") {
default_target = Some(Target::by_name(da_slice(targets), c!("gas-x86_64-windows")).expect("Default target for Windows on x86_64"));
default_target = Some(Target::by_name(targets, c!("gas-x86_64-windows")).expect("Default target for Windows on x86_64"));
} else {
default_target = None;
}
Expand Down Expand Up @@ -1276,13 +1273,13 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
}

if strcmp(*target_name, c!("list")) == 0 || *tlist {
print_available_targets(da_slice(targets));
print_available_targets(targets);
return Some(());
}

let Some(target) = Target::by_name(da_slice(targets), *target_name) else {
let Some(target) = Target::by_name(targets, *target_name) else {
usage();
print_available_targets(da_slice(targets));
print_available_targets(targets);
log(Log_Level::ERROR, c!("Unknown target `%s`"), *target_name);
return None;
};
Expand All @@ -1293,8 +1290,8 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {

if (*linker).count > 0 {
let mut s: Shlex = zeroed();
for i in 0..(*linker).count {
shlex_append_quoted(&mut s, *(*linker).items.add(i));
for arg in (*linker).iter() {
shlex_append_quoted(&mut s, arg);
}
let codegen_arg = temp_sprintf(c!("link-args=%s"), shlex_join(&mut s));
da_append(codegen_args, codegen_arg);
Expand Down Expand Up @@ -1324,8 +1321,7 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {
}

let mut sb: String_Builder = zeroed();
for i in 0..input_paths.count {
let input_path = *input_paths.items.add(i);
for (i, input_path) in input_paths.iter().enumerate() {
if i > 0 { sb_appendf(&mut sb, c!(", ")); }
sb_appendf(&mut sb, c!("%s"), input_path);
}
Expand All @@ -1338,20 +1334,16 @@ pub unsafe fn main(mut argc: i32, mut argv: *mut*mut c_char) -> Option<()> {

scope_push(&mut c.vars); // begin global scope

for i in 0..input_paths.count {
let input_path = *input_paths.items.add(i);

for input_path in input_paths.iter() {
input.count = 0;
read_entire_file(input_path, &mut input)?;

let mut l: Lexer = lexer::new(input_path, input.items, input.items.add(input.count), *historical);
let mut l: Lexer = lexer::new(input_path, input.items, input.at(input.count), *historical);

compile_program(&mut l, &mut c)?;
}

for i in 0..c.used_funcs.count {
let used_global = *c.used_funcs.items.add(i);

for used_global in c.used_funcs.iter() {
if find_var_deep(&mut c.vars, used_global.name).is_null() {
diagf!(used_global.loc, c!("ERROR: could not find name `%s`\n"), used_global.name);
bump_error_count(&mut c)?;
Expand Down
9 changes: 3 additions & 6 deletions src/bgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ pub unsafe fn aggregate_to_libb(folder_path: *const c_char) -> Option<()> {

if !read_entire_dir(folder_path, &mut children) { return None; }

for i in 0..children.count {
let child = *children.items.add(i);
for child in children.iter() {
if *child == '.' as c_char { continue; }
let child_path = temp_sprintf(c!("%s/%s"), folder_path, child);
if temp_strip_suffix(child, c!(".b")).is_none() {
Expand Down Expand Up @@ -56,8 +55,7 @@ pub unsafe fn reset_libb() -> Option<()> {
let folder_path = BUILD_LIBB_PATH;
if !read_entire_dir(folder_path, &mut children) { return None; }

for i in 0..children.count {
let child = *children.items.add(i);
for child in children.iter() {
if strcmp(child, c!(".")) == 0 { continue; }
if strcmp(child, c!("..")) == 0 { continue; }
let child_path = temp_sprintf(c!("%s/%s"), folder_path, child);
Expand All @@ -82,8 +80,7 @@ pub unsafe fn main(mut _argc: i32, mut _argv: *mut*mut c_char) -> Option<()> {
qsort(children.items as *mut c_void, children.count, size_of::<*const c_char>(), compar_cstr);
let mut sb: String_Builder = zeroed();
sb_appendf(&mut sb, c!("codegens! {\n"));
for i in 0..children.count {
let child = *children.items.add(i);
for child in children.iter() {
if *child == '.' as c_char { continue; }
if strcmp(child, c!("mod.rs")) == 0 { continue; }
// TODO: skip the modules that have invalid Rust names.
Expand Down
Loading
Loading