Skip to content
Open
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
86 changes: 39 additions & 47 deletions crates/lean_compiler/src/c_compile_final.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@
(Label::function("main"), STARTING_PC, entrypoint),
];

for (label, instructions) in &intermediate_bytecode.bytecode {
let remaining_bytecode = std::mem::take(&mut intermediate_bytecode.bytecode);
for (label, instructions) in remaining_bytecode {
label_to_pc.insert(label.clone(), pc);
if let Label::Function(function_name) = label {
if let Label::Function(ref function_name) = label {
hints.entry(pc).or_insert_with(Vec::new).push(Hint::StackFrame {
label: label.clone(),
size: *intermediate_bytecode
.memory_size_per_function
.get(function_name)
.get(function_name.as_str())
.unwrap(),
});
}
code_blocks.push((label.clone(), pc, instructions.clone()));
pc += count_real_instructions(instructions);
let count = count_real_instructions(&instructions);
code_blocks.push((label, pc, instructions));
pc += count;
}

let mut match_block_sizes = Vec::new();
Expand Down Expand Up @@ -215,10 +217,10 @@

let codegen_jump = |hints: &BTreeMap<CodeAddress, Vec<Hint>>,
low_level_bytecode: &mut Vec<Instruction>,
condition: IntermediateValue,
dest: IntermediateValue,
updated_fp: Option<IntermediateValue>| {
let dest = try_as_mem_or_constant(&dest).expect("Fatal: Could not materialize jump destination");
condition: &IntermediateValue,
dest: &IntermediateValue,
updated_fp: Option<&IntermediateValue>| {
let dest = try_as_mem_or_constant(dest).expect("Fatal: Could not materialize jump destination");
let label = match dest {
MemOrConstant::Constant(dest) => hints
.get(&usize::try_from(dest.as_canonical_u32()).unwrap())
Expand All @@ -236,7 +238,7 @@
.map(|fp| fp.try_into_mem_or_fp_or_constant(compiler).unwrap())
.unwrap_or(MemOrFpOrConstant::FpRelative { offset: 0 });
low_level_bytecode.push(Instruction::Jump {
condition: try_as_mem_or_constant(&condition).unwrap(),
condition: try_as_mem_or_constant(condition).unwrap(),
label,
dest,
updated_fp,
Expand All @@ -245,15 +247,15 @@

let mut pc = pc_start;
for instruction in block {
match instruction.clone() {
match instruction {
IntermediateInstruction::Computation {
operation,
mut arg_a,
mut arg_b,
arg_a,
arg_b,
res,
} => {
if let Some(arg_a_cst) = try_as_constant(&arg_a, compiler)
&& let Some(arg_b_cst) = try_as_constant(&arg_b, compiler)
if let Some(arg_a_cst) = try_as_constant(arg_a, compiler)
&& let Some(arg_b_cst) = try_as_constant(arg_b, compiler)
{
// res = constant +/x constant

Expand All @@ -266,18 +268,16 @@
res: MemOrConstant::Constant(op_res),
});
pc += 1;
continue;

Check warning on line 271 in crates/lean_compiler/src/c_compile_final.rs

View workflow job for this annotation

GitHub Actions / Rustfmt Check

Diff in /home/runner/work/leanMultisig/leanMultisig/crates/lean_compiler/src/c_compile_final.rs
}

if arg_b.is_constant() {
std::mem::swap(&mut arg_a, &mut arg_b);
}
let (arg_a, arg_b) = if arg_b.is_constant() { (arg_b, arg_a) } else { (arg_a, arg_b) };

low_level_bytecode.push(Instruction::Computation {
operation,
arg_a: try_as_mem_or_constant(&arg_a).unwrap(),
operation: operation.clone(),
arg_a: try_as_mem_or_constant(arg_a).unwrap(),
arg_c: arg_b.try_into_mem_or_fp_or_constant(compiler).unwrap(),
res: try_as_mem_or_constant(&res).unwrap(),
res: try_as_mem_or_constant(res).unwrap(),
});
}
IntermediateInstruction::Panic => {
Expand All @@ -291,19 +291,19 @@
}
IntermediateInstruction::Deref { shift_0, shift_1, res } => {
low_level_bytecode.push(Instruction::Deref {
shift_0: eval_const_expression(&shift_0, compiler).to_usize(),
shift_1: eval_const_expression(&shift_1, compiler).to_usize(),
shift_0: eval_const_expression(shift_0, compiler).to_usize(),
shift_1: eval_const_expression(shift_1, compiler).to_usize(),
res: res.try_into_mem_or_fp_or_constant(compiler).unwrap(),
});
}
IntermediateInstruction::JumpIfNotZero {
condition,
dest,
updated_fp,
} => codegen_jump(hints, low_level_bytecode, condition, dest, updated_fp),
} => codegen_jump(hints, low_level_bytecode, condition, dest, updated_fp.as_ref()),
IntermediateInstruction::Jump { dest, updated_fp } => {
let one = ConstExpression::one().into();
codegen_jump(hints, low_level_bytecode, one, dest, updated_fp)
codegen_jump(hints, low_level_bytecode, &one, dest, updated_fp.as_ref())
}
IntermediateInstruction::Precompile {
table,
Expand All @@ -314,51 +314,43 @@
aux_2,
} => {
low_level_bytecode.push(Instruction::Precompile {
table,
table: *table,
arg_a: arg_a.try_into_mem_or_fp_or_constant(compiler).unwrap(),
arg_b: arg_b.try_into_mem_or_fp_or_constant(compiler).unwrap(),
arg_c: arg_c.try_into_mem_or_fp_or_constant(compiler).unwrap(),
aux_1: eval_const_expression_usize(&aux_1, compiler),
aux_2: eval_const_expression_usize(&aux_2, compiler),
aux_1: eval_const_expression_usize(aux_1, compiler),
aux_2: eval_const_expression_usize(aux_2, compiler),
});

Check warning on line 323 in crates/lean_compiler/src/c_compile_final.rs

View workflow job for this annotation

GitHub Actions / Rustfmt Check

Diff in /home/runner/work/leanMultisig/leanMultisig/crates/lean_compiler/src/c_compile_final.rs
}
IntermediateInstruction::CustomHint(hint, args) => {
let hint = Hint::Custom(
hint,
args.into_iter()
.map(|expr| try_as_mem_or_constant(&expr).unwrap())
.collect(),
);
let hint = Hint::Custom(*hint, args.iter().map(|expr| try_as_mem_or_constant(expr).unwrap()).collect());
hints.entry(pc).or_default().push(hint);
}
IntermediateInstruction::Inverse { arg, res_offset } => {
let hint = Hint::Inverse {
arg: try_as_mem_or_constant(&arg).unwrap(),
res_offset,
arg: try_as_mem_or_constant(arg).unwrap(),
res_offset: *res_offset,
};
hints.entry(pc).or_default().push(hint);
}
IntermediateInstruction::RequestMemory { offset, size } => {
let size = try_as_mem_or_constant(&size).unwrap();
let size = try_as_mem_or_constant(size).unwrap();
let hint = Hint::RequestMemory {
function_name: function_name.clone(),
offset: eval_const_expression_usize(&offset, compiler),
offset: eval_const_expression_usize(offset, compiler),
size,
};
hints.entry(pc).or_default().push(hint);
}
IntermediateInstruction::Print { line_info, content } => {
let hint = Hint::Print {
line_info: line_info.clone(),
content: content
.into_iter()
.map(|c| try_as_mem_or_constant(&c).unwrap())
.collect(),
content: content.iter().map(|c| try_as_mem_or_constant(c).unwrap()).collect(),
};
hints.entry(pc).or_default().push(hint);
}
IntermediateInstruction::LocationReport { location } => {
let hint = Hint::LocationReport { location };
let hint = Hint::LocationReport { location: *location };
hints.entry(pc).or_default().push(hint);
}
IntermediateInstruction::DebugAssert(boolean, line_number) => {
Expand All @@ -368,7 +360,7 @@
right: try_as_mem_or_constant(&boolean.right).unwrap(),
kind: boolean.kind,
},
line_number,
*line_number,
);
hints.entry(pc).or_default().push(hint);
}
Expand All @@ -377,13 +369,13 @@
offset_target,
} => {
let hint = Hint::DerefHint {
offset_src: eval_const_expression_usize(&offset_src, compiler),
offset_target: eval_const_expression_usize(&offset_target, compiler),
offset_src: eval_const_expression_usize(offset_src, compiler),
offset_target: eval_const_expression_usize(offset_target, compiler),
};
hints.entry(pc).or_default().push(hint);

Check warning on line 375 in crates/lean_compiler/src/c_compile_final.rs

View workflow job for this annotation

GitHub Actions / Rustfmt Check

Diff in /home/runner/work/leanMultisig/leanMultisig/crates/lean_compiler/src/c_compile_final.rs
}
IntermediateInstruction::PanicHint { message } => {
let hint = Hint::Panic { message };
let hint = Hint::Panic { message: message.clone() };
hints.entry(pc).or_default().push(hint);
}
}
Expand Down
Loading