Skip to content

Commit

Permalink
Merge e0047f6 into f4754f7
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Oct 10, 2024
2 parents f4754f7 + e0047f6 commit 4cdd281
Show file tree
Hide file tree
Showing 83 changed files with 2,255 additions and 1,340 deletions.
11 changes: 10 additions & 1 deletion avm-transpiler/src/bit_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use acvm::{AcirField, FieldElement};
use acvm::{acir::brillig::MemoryAddress, AcirField, FieldElement};

fn get_msb(n: u128) -> usize {
let mut n = n;
Expand Down Expand Up @@ -56,6 +56,15 @@ impl BitsQueryable for usize {
}
}

impl BitsQueryable for MemoryAddress {
fn num_bits(&self) -> usize {
match self {
MemoryAddress::Direct(address) => get_msb(*address as u128),
MemoryAddress::Relative(offset) => get_msb(*offset as u128),
}
}
}

pub fn bits_needed_for<T: BitsQueryable>(val: &T) -> usize {
let num_bits = val.num_bits();
if num_bits <= 8 {
Expand Down
52 changes: 46 additions & 6 deletions avm-transpiler/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::fmt::{self, Display};
use std::fmt::{Debug, Formatter};

use acvm::acir::brillig::MemoryAddress;
use acvm::{AcirField, FieldElement};

use crate::opcodes::AvmOpcode;

/// Common values of the indirect instruction flag
pub const ALL_DIRECT: u8 = 0b00000000;
pub const ZEROTH_OPERAND_INDIRECT: u8 = 0b00000001;
pub const FIRST_OPERAND_INDIRECT: u8 = 0b00000010;
pub const SECOND_OPERAND_INDIRECT: u8 = 0b00000100;

/// A simple representation of an AVM instruction for the purpose
/// of generating an AVM bytecode from Brillig.
/// Note: this does structure not impose rules like "ADD instruction must have 3 operands"
Expand Down Expand Up @@ -141,3 +136,48 @@ impl AvmOperand {
}
}
}

#[derive(Debug, Default)]
pub(crate) struct AddressingModeBuilder {
indirect: Vec<bool>,
relative: Vec<bool>,
}

impl AddressingModeBuilder {
pub(crate) fn direct_operand(mut self, address: &MemoryAddress) -> Self {
self.relative.push(address.is_relative());
self.indirect.push(false);

self
}

pub(crate) fn indirect_operand(mut self, address: &MemoryAddress) -> Self {
self.relative.push(address.is_relative());
self.indirect.push(true);

self
}

pub(crate) fn build(self) -> AvmOperand {
let num_operands = self.indirect.len();
assert!(num_operands <= 8, "Too many operands for building addressing mode bytes");

let mut result = 0;
for (i, (indirect, relative)) in
self.indirect.into_iter().zip(self.relative.into_iter()).enumerate()
{
if indirect {
result |= 1 << i;
}
if relative {
result |= 1 << (num_operands + i);
}
}

if num_operands <= 4 {
AvmOperand::U8 { value: result as u8 }
} else {
AvmOperand::U16 { value: result as u16 }
}
}
}
Loading

0 comments on commit 4cdd281

Please sign in to comment.