From f996183c8d83bfd0124bc8a86edecd1ac6defc31 Mon Sep 17 00:00:00 2001 From: Juan-M-V Date: Wed, 27 Apr 2022 12:36:08 -0300 Subject: [PATCH 1/3] Crate initial layout for the vm --- Cargo.toml | 11 +++++++++++ src/vm/vm_core.rs | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/vm/vm_core.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000..dc4916dfd2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "giza" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[dev-dependencies.rusty-hook] +version = "0.11" diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs new file mode 100644 index 0000000000..b42cd16f7e --- /dev/null +++ b/src/vm/vm_core.rs @@ -0,0 +1,23 @@ +use num_bigint::BigUint; +use std::collections::HashMap; + +struct RunContext { + memory: , + pc: , + ap: , + fp: , +} + +struct VirtualMachine { + prime: BigUint, + builtin_runners: , + exec_scopes: Vec>, + enter_scope: , + hints: HashMap<..., Vec<...>>, + hint_pc_and_index: HashMap, + intruction_debug_info: HashMap<..., ...>, + debug_file_contents: HashMap, + error_message_attributes: Vec<...>, + program: ..., + validated_memory: HashMap<...> +} From c814686b0da3c45d08fef843bfba5272e717b130 Mon Sep 17 00:00:00 2001 From: Juan-M-V Date: Wed, 27 Apr 2022 12:54:05 -0300 Subject: [PATCH 2/3] Add more fields to structs --- src/vm/vm_core.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index b42cd16f7e..2123193a11 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -1,23 +1,35 @@ use num_bigint::BigUint; use std::collections::HashMap; +mod maybe_relocatable; +mod memory_dict; struct RunContext { - memory: , - pc: , - ap: , - fp: , + memory: MemoryDict, + pc: MaybeRelocatable, + ap: MaybeRelocatable, + fp: MaybeRelocatable, + prime: BigUint, } struct VirtualMachine { + run_context: RunContext, prime: BigUint, - builtin_runners: , + builtin_runners: Option>, exec_scopes: Vec>, enter_scope: , - hints: HashMap<..., Vec<...>>, - hint_pc_and_index: HashMap, - intruction_debug_info: HashMap<..., ...>, + hints: HashMap>, + hint_locals: HashMap<..., ...>, + hint_pc_and_index: HashMap, + static_locals: Option>, + intruction_debug_info: HashMap, debug_file_contents: HashMap, - error_message_attributes: Vec<...>, - program: ..., - validated_memory: HashMap<...> + error_message_attributes: Vec, + program: ProgramBase, + program_base: Option, + validated_memory: ValidatedMemoryDict, + auto_deduction: HashMap>, + accessesed_addresses: Vec, + trace: Vec, + current_step: BigUint, + skip_instruction_execution: bool, } From 92a41349c6f7df4098ad40178a422872d6847407 Mon Sep 17 00:00:00 2001 From: Federica Moletta Date: Wed, 27 Apr 2022 15:44:47 -0300 Subject: [PATCH 3/3] Add more struct definitions --- src/compiler/instruction.rs | 75 +++++++++++++++++++++++++++++++++ src/vm/builtin_runner.rs | 3 ++ src/vm/memory_dict.rs | 9 ++++ src/vm/relocatable.rs | 11 +++++ src/vm/trace_entry.rs | 7 +++ src/vm/validated_memory_dict.rs | 14 ++++++ src/vm/vm_core.rs | 27 +++++++++--- 7 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 src/compiler/instruction.rs create mode 100644 src/vm/builtin_runner.rs create mode 100644 src/vm/memory_dict.rs create mode 100644 src/vm/relocatable.rs create mode 100644 src/vm/trace_entry.rs create mode 100644 src/vm/validated_memory_dict.rs diff --git a/src/compiler/instruction.rs b/src/compiler/instruction.rs new file mode 100644 index 0000000000..a6e8a9855d --- /dev/null +++ b/src/compiler/instruction.rs @@ -0,0 +1,75 @@ +use num_bigint::BigUint; + +num Register { + AP, + FP +} + +pub struct Instruction { + off0: BigUint, + off1: BigUint, + off2: BigUint, + imm: Option, + dst_register: Register, + op0_register: Register, + op1_addr: Op1Addr, + res: Res, + pc_update: PcUpdate, + ap_update: ApUpdate + fp_update: FpUpdate, + opcode: Opcode + +} + +pub enum Op1Addr { + IMM, + AP, + FP, + OP0 +} + +pub enum Res { + OP1, + ADD, + MUL, + UNCONSTRAINED +} + +pub enum PcUpdate { + REGULAR, + JUMP, + JUMP_REL, + JNZ +} + +pub enum ApUpdate { + REGULAR, + ADD, + ADD1, + ADD2 +} + +pub enum FpUpdate { + REGULAR, + AP_PLUS2, + DST +} + +pub enum Opcode { + NOP, + ASSERT_EQ, + CALL, + RET +} + +trait Size { + fn size(&self) -> i32; +} +impl size for Instruction { + fn size(&self) -> i32 { + match self.imm { + Some(imm) => 2 + None => 1 + } + } +} diff --git a/src/vm/builtin_runner.rs b/src/vm/builtin_runner.rs new file mode 100644 index 0000000000..22ed27d16f --- /dev/null +++ b/src/vm/builtin_runner.rs @@ -0,0 +1,3 @@ +pub struct BuiltinRunner { + +} diff --git a/src/vm/memory_dict.rs b/src/vm/memory_dict.rs new file mode 100644 index 0000000000..49a4951d64 --- /dev/null +++ b/src/vm/memory_dict.rs @@ -0,0 +1,9 @@ +mod relocatable; + +use num_bigint::BigUint; +use relocatable::RelocatableValue +struct MemoryDict { + data: HashMap, + frozen: bool, + relocation_rules: HashMap +} diff --git a/src/vm/relocatable.rs b/src/vm/relocatable.rs new file mode 100644 index 0000000000..eee39501c7 --- /dev/null +++ b/src/vm/relocatable.rs @@ -0,0 +1,11 @@ +use num_bigint::BigUint; + +struct RelocatableValue { + segment_index: BigUint, + offset: BigUint +} + +enum MaybeRelocatable { + RelocatableValue(RelocatableValue), + Int(BigUint) +} diff --git a/src/vm/trace_entry.rs b/src/vm/trace_entry.rs new file mode 100644 index 0000000000..4188f5b90f --- /dev/null +++ b/src/vm/trace_entry.rs @@ -0,0 +1,7 @@ +///A trace entry for every instruction that was executed. +///Holds the register values before the instruction was executed. +pub struct TraceEntry { + pc: T, + ap: T, + fp: T +} diff --git a/src/vm/validated_memory_dict.rs b/src/vm/validated_memory_dict.rs new file mode 100644 index 0000000000..5658e27884 --- /dev/null +++ b/src/vm/validated_memory_dict.rs @@ -0,0 +1,14 @@ +mod memory_dict; +mod relocatable; + +use num_bigint::BigUint; +use memory_dict::MemoryDict; +use relocatable::RelocatableValue + +pub struct ValidatedMemoryDict { + memory : MemoryDict, + validation_rules : HashMap>, + validated_addresses: Vec +} + +pub struct ValidationRule(fn(MemoryDict, MaybeRelocatable, ()) -> RelocatableValue) diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index 2123193a11..34eb606227 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -1,20 +1,37 @@ use num_bigint::BigUint; use std::collections::HashMap; -mod maybe_relocatable; +mod relocatable; mod memory_dict; +mod validated_memory_dict; +mod trace_entry; +mod builtin_runner; + +use::maybe_relocatable::MaybeRelocatable; +use::memory_dict::MemoryDict; +use::validated_memory_dict::ValidatedMemoryDict; +use::relocatable::MaybeRelocatable; +use::trace_entry::TraceEntry; +use::builtin_runner::BuitinRunner; + +struct Operands { + dst: MaybeRelocatable, + res: Option, + op0: MaybeRelocatable, + op1: MaybeRelocatable +} struct RunContext { memory: MemoryDict, pc: MaybeRelocatable, ap: MaybeRelocatable, fp: MaybeRelocatable, - prime: BigUint, + prime: BigUint } -struct VirtualMachine { +pub struct VirtualMachine { run_context: RunContext, prime: BigUint, - builtin_runners: Option>, + builtin_runners: Option>, exec_scopes: Vec>, enter_scope: , hints: HashMap>, @@ -31,5 +48,5 @@ struct VirtualMachine { accessesed_addresses: Vec, trace: Vec, current_step: BigUint, - skip_instruction_execution: bool, + skip_instruction_execution: bool }