Skip to content

Commit

Permalink
Fix a bug with auto vector allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
elimirks committed Jan 1, 2022
1 parent 182d286 commit efbbd43
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
set -e

for f in test/*; do
echo "Testing $f"
cargo -q run --release -- -r $f
done
10 changes: 8 additions & 2 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,14 +1056,20 @@ fn gen_auto(

match var {
Var::Vec(_, size, initial) => {
let offset = if let Loc::Stack(offset) = dest_loc {
offset
} else {
panic!("Auto loc should always be on the stack!");
};

// The first value in the stack for a vector is a data pointer
instructions.push(format!(
"leaq {}(%rbp),%rax", - (1 + size) * 8));
"leaq {}(%rbp),%rax", (offset - size) * 8));
instructions.push(format!("movq %rax,{}", dest_loc));

for i in 0..initial.len() {
let value = initial[i];
let val_dest_loc = Loc::Stack(-size - 1 + i as i64);
let val_dest_loc = Loc::Stack(offset - size + i as i64);

let (val_loc, _) = gen_int(instructions, value, Reg::Rax);
instructions.push(format!(
Expand Down
5 changes: 3 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ unsafe fn get_inside_quotes(
// These aren't ever necessary in code compiled with b64
'{' => '{',
'}' => '}',
other => return CompErr::err(&c.pos(), format!(
"Unknown escape char: {}", other)),
other => return CompErr::err(
&Pos::new(i, c.file_id),
format!("Unknown char/str escape char: {}", other)),
}
},
chr => {
Expand Down
2 changes: 1 addition & 1 deletion test/fib.b
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fib_rec_switch(n) {
}

assert_fib_eq(expected, fib_index) {
printf("Testing fib(%d) = %d*n", fib_index, expected);
printf("Checking if fib(%d) = %d*n", fib_index, expected);
assert_eq_int(expected, fib_rec_switch(fib_index));
assert_eq_int(expected, fib_rec_condexpr(fib_index));
assert_eq_int(expected, fib_loop(fib_index));
Expand Down
67 changes: 66 additions & 1 deletion test/vec.b
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
#import "../assets/best.b";

test_multi_auto_vec() {
auto as[4];
auto bs[4];

/* To check they don't get overridden */
auto a_ptr, b_ptr;
a_ptr = as;
b_ptr = bs;

as[0] = 0;
as[1] = 1;
as[2] = 2;
as[3] = 3;
as[4] = 4;

bs[0] = 0;
bs[1] = 1;
bs[2] = 2;
bs[3] = 3;
bs[4] = 4;

/* Making sure vec pointers didn't get overwritten */
assert_eq_int(a_ptr, as);
assert_eq_int(b_ptr, bs);

assert_eq_int(0, as[0]);
assert_eq_int(1, as[1]);
assert_eq_int(2, as[2]);
assert_eq_int(3, as[3]);
assert_eq_int(4, as[4]);

assert_eq_int(0, bs[0]);
assert_eq_int(1, bs[1]);
assert_eq_int(2, bs[2]);
assert_eq_int(3, bs[3]);
assert_eq_int(4, bs[4]);
}

test_mono_auto_vec() {
auto as[4];

/* To check they don't get overridden */
auto a_ptr;
a_ptr = as;

as[0] = 0;
as[1] = 1;
as[2] = 2;
as[3] = 3;
as[4] = 4;

/* Making sure vec pointers didn't get overwritten */
assert_eq_int(a_ptr, as);

assert_eq_int(0, *as);
assert_eq_int(1, as[1]);
assert_eq_int(2, as[2]);
assert_eq_int(3, as[3]);
assert_eq_int(4, as[4]);
}

main() {
auto nums[2] 2;
auto nums[5] 2;
*(nums + 8) = 3;
nums[2] = 5;

assert_eq_int(2, *nums);
assert_eq_int(3, *(nums + 8));
assert_eq_int(5, nums[2]);

test_mono_auto_vec();
test_multi_auto_vec();
return(0);
}

0 comments on commit efbbd43

Please sign in to comment.