Skip to content

Commit

Permalink
feat(example): fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Aug 3, 2023
1 parent 87b80fa commit dcde1bb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
20 changes: 13 additions & 7 deletions codegen/src/masm/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ use crate::{MacroAssembler, Result};
use opcodes::ShangHai as OpCode;

impl MacroAssembler {
// /// OVERRIDE: Greater than comparison.
// ///
// /// TODO:
// pub fn _sgt(&mut self) -> Result<()> {
// self._swap1()?;
// self.asm._sgt()
// }
/// Greater than or equal comparison.
///
/// TODO: refactor this.
pub fn _ge(&mut self) -> Result<()> {
self._sgt()
}

/// Greater than comparison.
///
/// Using lt due to order of stack.
pub fn _gt(&mut self) -> Result<()> {
self.asm._lt()
}

/// Sign-agnostic compare unequal.
pub fn _ne(&mut self) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'a> VisitOperator<'a> for CodeGen {
],
signed_and_float: [add, sub, mul, eq, ne],
map: {
all: [ge => sgt, le => slt],
all: [ge => ge, le => slt],
integer: [rem => mod],
},
mem: {
Expand Down
6 changes: 6 additions & 0 deletions compiler/tests/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ fn as_if() -> Result<()> {
let info = EVM::run(&bytecode, &0.to_bytes32());
assert_eq!(info.ret, 0.to_bytes32());

let info = EVM::run(&bytecode, &1.to_bytes32());
assert_eq!(info.ret, 1.to_bytes32());

let info = EVM::run(&bytecode, &2.to_bytes32());
assert_eq!(info.ret, 41.to_bytes32());

let info = EVM::run(&bytecode, &3.to_bytes32());
assert_eq!(info.ret, 42.to_bytes32());
Ok(())
Expand Down
14 changes: 11 additions & 3 deletions compiler/tests/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ fn fibonacci() -> Result<()> {
let info = EVM::run(&bytecode, &2.to_bytes32());
assert_eq!(1.to_bytes32().to_vec(), info.ret);

// // x = 3
// let info = EVM::run(&bytecode, &3.to_bytes32());
// assert_eq!(3.to_bytes32().to_vec(), info.ret);
// x = 3
let info = EVM::run(&bytecode, &3.to_bytes32());
assert_eq!(2.to_bytes32().to_vec(), info.ret);

// x = 4
let info = EVM::run(&bytecode, &4.to_bytes32());
assert_eq!(3.to_bytes32().to_vec(), info.ret);

// x = 5
let info = EVM::run(&bytecode, &5.to_bytes32());
assert_eq!(5.to_bytes32().to_vec(), info.ret);

Ok(())
}
2 changes: 1 addition & 1 deletion zint/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use revm_interpreter::instruction_result::InstructionResult;
use revm_interpreter::{Contract, DummyHost, Interpreter};
use revm_primitives::{bytecode::Bytecode, specification::ShanghaiSpec, U256};

const INITIAL_GAS: u64 = 1_000_000_000;
const INITIAL_GAS: u64 = 1_000_000_000_000_000;

/// EVM execution result info.
#[derive(Debug)]
Expand Down

0 comments on commit dcde1bb

Please sign in to comment.