Skip to content

Define VM Structures #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
75 changes: 75 additions & 0 deletions src/compiler/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use num_bigint::BigUint;

num Register {
AP,
FP
}

pub struct Instruction {
off0: BigUint,
off1: BigUint,
off2: BigUint,
imm: Option<BigUint>,
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
}
}
}
3 changes: 3 additions & 0 deletions src/vm/builtin_runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct BuiltinRunner {

}
9 changes: 9 additions & 0 deletions src/vm/memory_dict.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod relocatable;

use num_bigint::BigUint;
use relocatable::RelocatableValue
struct MemoryDict {
data: HashMap,
frozen: bool,
relocation_rules: HashMap<BigUint, RelocatableValue>
}
11 changes: 11 additions & 0 deletions src/vm/relocatable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use num_bigint::BigUint;

struct RelocatableValue {
segment_index: BigUint,
offset: BigUint
}

enum MaybeRelocatable {
RelocatableValue(RelocatableValue),
Int(BigUint)
}
7 changes: 7 additions & 0 deletions src/vm/trace_entry.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
pc: T,
ap: T,
fp: T
}
14 changes: 14 additions & 0 deletions src/vm/validated_memory_dict.rs
Original file line number Diff line number Diff line change
@@ -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<BigUint, Vec<(ValidationRule, ())>>,
validated_addresses: Vec<RelocatableValue>
}

pub struct ValidationRule(fn(MemoryDict, MaybeRelocatable, ()) -> RelocatableValue)
52 changes: 52 additions & 0 deletions src/vm/vm_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use num_bigint::BigUint;
use std::collections::HashMap;
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<MaybeRelocatable>,
op0: MaybeRelocatable,
op1: MaybeRelocatable
}

struct RunContext {
memory: MemoryDict,
pc: MaybeRelocatable,
ap: MaybeRelocatable,
fp: MaybeRelocatable,
prime: BigUint
}

pub struct VirtualMachine {
run_context: RunContext,
prime: BigUint,
builtin_runners: Option<HashMap<String, BuiltinRunner>>,
exec_scopes: Vec<HashMap<..., ...>>,
enter_scope: ,
hints: HashMap<MaybeRelocatable, Vec<CompiledHint>>,
hint_locals: HashMap<..., ...>,
hint_pc_and_index: HashMap<i32, (MaybeRelocatable, i32)>,
static_locals: Option<HashMap<..., ...>>,
intruction_debug_info: HashMap<MaybeRelocatable, InstructionLocation>,
debug_file_contents: HashMap<String, String>,
error_message_attributes: Vec<VmAttributeScope>,
program: ProgramBase,
program_base: Option<MaybeRelocatable>,
validated_memory: ValidatedMemoryDict,
auto_deduction: HashMap<i32, Vec<(Rule, ())>>,
accessesed_addresses: Vec<MaybeRelocatable>,
trace: Vec<TraceEntry>,
current_step: BigUint,
skip_instruction_execution: bool
}