upgrade the register allocator & prepare for else

This commit is contained in:
afonya2 2025-05-30 22:27:11 +02:00
parent e9d1f9bbd6
commit a01b9876f1
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 32 additions and 1 deletions

View file

@ -84,11 +84,23 @@ fn allocate_register(registers: &Vec<RegisterState>) -> AllocateResult {
//Find the one that wasn't used for the longest time
let mut oldest: usize = 0;
let mut oldest_register: u8 = 0;
let mut oldest_temp: usize = 0;
let mut oldest_temp_register: u8 = 0;
for register in registers {
if ((register.last_used < oldest) || (oldest == 0)) && register.id != 0 {
oldest = register.last_used;
oldest_register = register.id;
}
if ((register.last_used < oldest_temp) || (oldest_temp == 0)) && register.id != 0 && register.variable == 0 {
oldest_temp = register.last_used;
oldest_temp_register = register.id;
}
}
if oldest_temp_register != 0 {
return AllocateResult {
register: oldest_temp_register,
unbind_before: false,
};
}
return AllocateResult {
register: oldest_register,
@ -186,7 +198,13 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
},
ASTPart::If(if_part) => {
let condition_reg = do_ast_op(*if_part.condition, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers);
ops.push(Operation { opcode: 30, arg1: Some(condition_reg), arg2: Some(2), arg3: None });
ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None });
//Update the lastif variable
ops.push(Operation { opcode: 7, arg1: Some(condition_reg), arg2: Some(var_ids["__LASTIF"] as i64), arg3: None });
set_register(registers, RegisterState { id: condition_reg, used: true, variable: var_ids["__LASTIF"], last_used: *op_count });
let op_placeholder = ops.len();
ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None });
for if_op in if_part.body {
@ -198,6 +216,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
arg2: Some(ops.len() as i64),
arg3: None,
};
//Unbind the lastif variable
if get_register_by_id(registers, condition_reg).variable == var_ids["__LASTIF"] {
ops.push(Operation { opcode: 8, arg1: Some(condition_reg), arg2: None, arg3: None });
set_register(registers, RegisterState { id: condition_reg, used: false, variable: 0, last_used: 0 });
}
garbage_collect_registers(registers);
},
ASTPart::Operation(op) => {
@ -273,6 +297,9 @@ fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mu
let mut functions: HashMap<u32, Compiled> = HashMap::new();
let mut next_function_id: u32 = 1;
var_ids.insert(String::from("__LASTIF"), next_var_id);
next_var_id += 1;
match args {
Some(arg_list) => {
for arg in arg_list {

View file

@ -1 +1,5 @@
gethelj a = !piszv
gethelj a = 1
ha geny (1==1) {
gethelj b = 2
}
gethelj c = 3