Skip to content

Commit b367fe3

Browse files
committed
transpiler cleanup
1 parent 2e63479 commit b367fe3

1 file changed

Lines changed: 17 additions & 39 deletions

File tree

avm-transpiler/src/transpile.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
3636
BinaryFieldOp::Sub => AvmOpcode::SUB,
3737
BinaryFieldOp::Mul => AvmOpcode::MUL,
3838
BinaryFieldOp::Div => AvmOpcode::FDIV,
39+
BinaryFieldOp::IntegerDiv => AvmOpcode::DIV,
3940
BinaryFieldOp::Equals => AvmOpcode::EQ,
4041
BinaryFieldOp::LessThan => AvmOpcode::LT,
4142
BinaryFieldOp::LessThanEquals => AvmOpcode::LTE,
42-
BinaryFieldOp::IntegerDiv => AvmOpcode::DIV,
4343
};
4444
avm_instrs.push(AvmInstruction {
4545
opcode: avm_opcode,
@@ -73,42 +73,28 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
7373
lhs,
7474
rhs,
7575
} => {
76-
let is_integral = is_integral_bit_size(*bit_size);
76+
assert!(is_integral_bit_size(*bit_size), "BinaryIntOp bit size should be integral: {:?}", brillig_instr);
7777
let avm_opcode = match op {
78-
BinaryIntOp::Add if is_integral => AvmOpcode::ADD,
79-
BinaryIntOp::Sub if is_integral => AvmOpcode::SUB,
80-
BinaryIntOp::Mul if is_integral => AvmOpcode::MUL,
81-
BinaryIntOp::UnsignedDiv if is_integral => AvmOpcode::DIV,
82-
BinaryIntOp::UnsignedDiv if is_field_bit_size(*bit_size) => AvmOpcode::FDIV,
83-
BinaryIntOp::Equals if is_integral => AvmOpcode::EQ,
84-
BinaryIntOp::LessThan if is_integral => AvmOpcode::LT,
85-
BinaryIntOp::LessThanEquals if is_integral => AvmOpcode::LTE,
86-
BinaryIntOp::And if is_integral => AvmOpcode::AND,
87-
BinaryIntOp::Or if is_integral => AvmOpcode::OR,
88-
BinaryIntOp::Xor if is_integral => AvmOpcode::XOR,
89-
BinaryIntOp::Shl if is_integral => AvmOpcode::SHL,
90-
BinaryIntOp::Shr if is_integral => AvmOpcode::SHR,
91-
// https://github.com/noir-lang/noir/issues/4543
92-
// Using Field for now, until the bug is fixed.
93-
BinaryIntOp::Mul if is_field_bit_size(*bit_size) => AvmOpcode::MUL,
94-
BinaryIntOp::Sub if is_field_bit_size(*bit_size) => AvmOpcode::SUB,
95-
// https://github.com/noir-lang/noir/issues/4544
96-
// These are implemented on our side, but Brillig does not have LT(E) in BinaryFieldOp
97-
// So they use BinaryIntOp.
98-
BinaryIntOp::LessThan if is_field_bit_size(*bit_size) => AvmOpcode::LT,
99-
BinaryIntOp::LessThanEquals if is_field_bit_size(*bit_size) => AvmOpcode::LTE,
78+
BinaryIntOp::Add => AvmOpcode::ADD,
79+
BinaryIntOp::Sub => AvmOpcode::SUB,
80+
BinaryIntOp::Mul => AvmOpcode::MUL,
81+
BinaryIntOp::UnsignedDiv => AvmOpcode::DIV,
82+
BinaryIntOp::Equals => AvmOpcode::EQ,
83+
BinaryIntOp::LessThan => AvmOpcode::LT,
84+
BinaryIntOp::LessThanEquals => AvmOpcode::LTE,
85+
BinaryIntOp::And => AvmOpcode::AND,
86+
BinaryIntOp::Or => AvmOpcode::OR,
87+
BinaryIntOp::Xor => AvmOpcode::XOR,
88+
BinaryIntOp::Shl => AvmOpcode::SHL,
89+
BinaryIntOp::Shr => AvmOpcode::SHR,
10090
_ => panic!(
10191
"Transpiler doesn't know how to process {:?}", brillig_instr
10292
),
10393
};
10494
avm_instrs.push(AvmInstruction {
10595
opcode: avm_opcode,
10696
indirect: Some(ALL_DIRECT),
107-
tag: if is_integral {
108-
Some(tag_from_bit_size(*bit_size))
109-
} else {
110-
None
111-
},
97+
tag: Some(tag_from_bit_size(*bit_size)),
11298
operands: vec![
11399
AvmOperand::U32 {
114100
value: lhs.to_usize() as u32,
@@ -811,14 +797,10 @@ fn handle_const(
811797
if !matches!(tag, AvmTypeTag::FIELD) {
812798
avm_instrs.push(generate_set_instruction(tag, dest, value.to_u128()));
813799
} else {
814-
// Handling fields is a bit more complex since we cannot fit a field in a single instruction.
815-
// We need to split the field into 128-bit chunks and set them individually.
800+
// We can't fit a field in an instruction. This should've been handled in Brillig.
816801
let field = value.to_field();
817802
if !field.fits_in_u128() {
818-
// If the field doesn't fit in 128 bits, we need scratch space. That's not trivial.
819-
// Will this ever happen? ACIR supports up to 126 bit fields.
820-
// However, it might be needed _inside_ the unconstrained function.
821-
panic!("SET: Field value doesn't fit in 128 bits, that's not supported yet!");
803+
panic!("SET: Field value doesn't fit in 128 bits, that's not supported!");
822804
}
823805
avm_instrs.extend([
824806
generate_set_instruction(AvmTypeTag::UINT128, dest, field.to_u128()),
@@ -1034,10 +1016,6 @@ fn map_brillig_pcs_to_avm_pcs(initial_offset: usize, brillig: &Brillig) -> Vec<u
10341016
pc_map
10351017
}
10361018

1037-
fn is_field_bit_size(bit_size: u32) -> bool {
1038-
bit_size == 254
1039-
}
1040-
10411019
fn is_integral_bit_size(bit_size: u32) -> bool {
10421020
match bit_size {
10431021
1 | 8 | 16 | 32 | 64 | 128 => true,

0 commit comments

Comments
 (0)