Skip to content

Commit 9b39560

Browse files
committed
fixed codegen for slices & tuples because slots weren't contiguous
1 parent e42781e commit 9b39560

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

src/codegen.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,16 +933,44 @@ impl BytecodeGenerator {
933933
registers.push(self.visit(annotation_context.clone(), item));
934934
}
935935

936+
let new_arg_registers = find_contiguous_slots!(self, &registers);
937+
for i in 0..registers.len() {
938+
let current_arg_reg = registers[i];
939+
let new_reg = new_arg_registers[i];
940+
941+
if current_arg_reg != new_reg {
942+
// allocate the slot and do a MOV
943+
self.push_instruction(
944+
Instruction {
945+
op_instruction: OpInstruction::MOV,
946+
arg_0: current_arg_reg,
947+
arg_1: new_reg,
948+
arg_2: 0,
949+
},
950+
position.line as usize,
951+
);
952+
}
953+
}
954+
936955
let dest = alloc_slot!(self);
937956
self.push_instruction(
938957
Instruction {
939958
op_instruction: OpInstruction::BUILD_TUPLE,
940-
arg_0: registers[0],
941-
arg_1: registers.len() as u8,
959+
arg_0: new_arg_registers[0],
960+
arg_1: new_arg_registers.len() as u8,
942961
arg_2: dest,
943962
},
944963
position.line as usize,
945964
);
965+
966+
for reg in registers {
967+
free_slot!(self, reg);
968+
}
969+
970+
for reg in new_arg_registers {
971+
free_slot!(self, reg);
972+
}
973+
946974
dest
947975
}
948976

@@ -2117,20 +2145,48 @@ impl BytecodeGenerator {
21172145
items: &Vec<ASTNode>,
21182146
) -> u8 {
21192147
let mut registers: Vec<u8> = vec![];
2148+
// todo contiguous registers
21202149
for item in items {
21212150
registers.push(self.visit(annotation_context.clone(), item));
21222151
}
21232152

2153+
let new_arg_registers = find_contiguous_slots!(self, &registers);
2154+
for i in 0..registers.len() {
2155+
let current_arg_reg = registers[i];
2156+
let new_reg = new_arg_registers[i];
2157+
2158+
if current_arg_reg != new_reg {
2159+
// allocate the slot and do a MOV
2160+
self.push_instruction(
2161+
Instruction {
2162+
op_instruction: OpInstruction::MOV,
2163+
arg_0: current_arg_reg,
2164+
arg_1: new_reg,
2165+
arg_2: 0,
2166+
},
2167+
pos.line as usize,
2168+
);
2169+
}
2170+
}
2171+
21242172
let dest = alloc_slot!(self);
21252173
self.push_instruction(
21262174
Instruction {
21272175
op_instruction: OpInstruction::BUILD_SLICE,
2128-
arg_0: registers[0],
2129-
arg_1: registers.len() as u8,
2176+
arg_0: new_arg_registers[0],
2177+
arg_1: new_arg_registers.len() as u8,
21302178
arg_2: dest,
21312179
},
21322180
pos.line as usize,
21332181
);
2182+
2183+
for reg in registers {
2184+
free_slot!(self, reg);
2185+
}
2186+
for reg in new_arg_registers {
2187+
free_slot!(self, reg);
2188+
}
2189+
21342190
dest
21352191
}
21362192

src/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ impl Compiler {
117117
if compiler_flags.dump_bytecode {
118118
let mut file = OpenOptions::new()
119119
.write(true) // Open for writing
120-
.create(true) // Create the file if it doesn't exist
121-
.append(false) // Append to the file if it exists
120+
.create(true)
121+
.truncate(true) // Append to the file if it exists
122122
// todo extract the filename from here
123123
.open(format!("./gila-build/{}.gilab", "main"))
124124
.unwrap();

0 commit comments

Comments
 (0)