Skip to content

Commit

Permalink
Start work on foreign function
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jul 23, 2024
1 parent 88c0a40 commit c12bd39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
5 changes: 3 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use super::errors::{IResult, InterpreterError};
use super::value::{unwrap_rc, Value};

mod builtin;
mod foreign;
mod unquote;

#[allow(unused)]
Expand Down Expand Up @@ -144,8 +145,8 @@ impl<'local, 'interner> Interpreter<'local, 'interner> {
let builtin = builtin.clone();
builtin::call_builtin(self.elaborator.interner, &builtin, arguments, location)
} else if let Some(foreign) = func_attrs.foreign() {
let item = format!("Comptime evaluation for foreign functions like {foreign}");
Err(InterpreterError::Unimplemented { item, location })
let foreign = foreign.clone();
foreign::call_foreign(self.elaborator.interner, &foreign, arguments, location)
} else if let Some(oracle) = func_attrs.oracle() {
if oracle == "print" {
self.print_oracle(arguments)
Expand Down
19 changes: 17 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(super) fn call_builtin(
}
}

fn check_argument_count(
pub(super) fn check_argument_count(
expected: usize,
arguments: &[(Value, Location)],
location: Location,
Expand All @@ -73,6 +73,21 @@ fn failing_constraint<T>(message: impl Into<String>, location: Location) -> IRes
Err(InterpreterError::FailingConstraint { message, location })
}

pub(super) fn get_array(
interner: &NodeInterner,
value: Value,
location: Location,
) -> IResult<(im::Vector<Value>, Type)> {
match value {
Value::Array(values, typ) => Ok((values, typ)),
value => {
let type_var = Box::new(interner.next_type_variable());
let expected = Type::Array(type_var.clone(), type_var);
Err(InterpreterError::TypeMismatch { expected, value, location })
}
}
}

fn get_slice(
interner: &NodeInterner,
value: Value,
Expand All @@ -88,7 +103,7 @@ fn get_slice(
}
}

fn get_u32(value: Value, location: Location) -> IResult<u32> {
pub(super) fn get_u32(value: Value, location: Location) -> IResult<u32> {
match value {
Value::U32(value) => Ok(value),
value => {
Expand Down
39 changes: 39 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/foreign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

use acvm::{AcirField, FieldElement, BlackBoxFunctionSolver};
use noirc_errors::Location;

use crate::{
hir::comptime::{errors::IResult, InterpreterError, Value},
macros_api::NodeInterner,
};

use super::builtin::{check_argument_count, get_u32, get_array};

pub(super) fn call_foreign(
interner: &mut NodeInterner,
name: &str,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
match name {
"poseidon2_permutation" => poseidon2_permutation(interner, arguments, location),
_ => {
let item = format!("Comptime evaluation for builtin function {name}");
Err(InterpreterError::Unimplemented { item, location })
}
}
}

// poseidon2_permutation<let N: u32>(_input: [Field; N], _state_length: u32) -> [Field; N]
fn posdon2_permutation(
interner: &mut NodeInterner,
mut arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
check_argument_count(2, &arguments, location)?;

let state_length = get_u32(arguments.pop().unwrap().0, location);
let input = get_array(interner, arguments.pop().unwrap().0, location);

let result = Bn254BlackBoxSolver.poseidon2_permutation(inputs, state_length);
}

0 comments on commit c12bd39

Please sign in to comment.