Skip to content
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

[WIP ]Execution implementation #22

Draft
wants to merge 71 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
3cbb748
Add basic structs and runner initialization
fmoletta Jul 17, 2023
c32247d
Start implementing tests
fmoletta Jul 17, 2023
93ff635
Add initializers + move vm into runner + minor fixes
fmoletta Jul 17, 2023
871638a
Minor fixes
fmoletta Jul 17, 2023
768049f
Improve runner test
fmoletta Jul 17, 2023
683eca8
Fix memory new size
fmoletta Jul 18, 2023
6982d8b
Add memory tests
fmoletta Jul 18, 2023
330c509
Merge branch 'main' of github.com:lambdaclass/cairo-vm_in_C into cair…
fmoletta Jul 18, 2023
74d742c
Implement memory_get + run new formatter
fmoletta Jul 18, 2023
2c5b7d6
Implement memory_insert
fmoletta Jul 18, 2023
d62a338
fixes'
fmoletta Jul 18, 2023
58a601e
Merge main
mmsc2 Jul 19, 2023
8637465
fix format
mmsc2 Jul 19, 2023
1ae32e5
Add file
fmoletta Jul 19, 2023
44f91eb
Finish load_data tests
fmoletta Jul 19, 2023
588b178
Merge branch 'cairo-runner-and-initialization' into ExecutionImplemen…
mmsc2 Jul 19, 2023
9b83d6d
Fix bug
fmoletta Jul 19, 2023
0bdf1e1
Fix bug
fmoletta Jul 19, 2023
194d570
change to pointer
mmsc2 Jul 19, 2023
d193b3f
Add boolean is_felt to maybe_relocatable type
fmoletta Jul 20, 2023
1f43986
Fixes
fmoletta Jul 20, 2023
49805ee
Add fix + test
fmoletta Jul 20, 2023
4353f64
Add more tests
fmoletta Jul 20, 2023
1b22061
Add test
fmoletta Jul 20, 2023
c2ee7ae
Fix converion functions
mmsc2 Jul 20, 2023
c232507
Merge new implmentation of maybe relocatable
mmsc2 Jul 21, 2023
67956db
Fix typos
mmsc2 Jul 21, 2023
2f11a7e
Handle memory gaps in memory functions
fmoletta Jul 21, 2023
65cb901
Add more tests
fmoletta Jul 21, 2023
8093541
Fix bug
fmoletta Jul 21, 2023
d026137
Add tests
fmoletta Jul 21, 2023
3ab79b3
Merge branch 'main' of github.com:lambdaclass/cairo-vm_in_C into cair…
fmoletta Jul 21, 2023
d1b7492
Use felt_t in maybe_relocatable + adapt tests
fmoletta Jul 24, 2023
27ad5c5
Rename function & change parameter type
fmoletta Jul 24, 2023
c3da197
Remove unused import
fmoletta Jul 24, 2023
f147768
Remove unused import
fmoletta Jul 24, 2023
1726141
Add memory_free function
fmoletta Jul 24, 2023
e778bb9
Free memory in memory_tests
fmoletta Jul 24, 2023
31ffde5
fmt
fmoletta Jul 24, 2023
8d16b3f
Use clear in stead of free
fmoletta Jul 24, 2023
2dc7b1e
Free runner
fmoletta Jul 24, 2023
b9cbaf5
Add function comments
fmoletta Jul 24, 2023
d3c7165
Add missing execution segment data loading
fmoletta Jul 24, 2023
073b9c2
Add error handling to runner init & memory load_data
fmoletta Jul 24, 2023
5ea00bc
Revert "Add error handling to runner init & memory load_data"
fmoletta Jul 24, 2023
b366829
add error handling to memory_load_data
fmoletta Jul 24, 2023
59aa6cd
Add err case result
fmoletta Jul 24, 2023
1641508
Add warning, move comment to header
fmoletta Jul 24, 2023
4a0fce9
Move comments to header
fmoletta Jul 24, 2023
364bb88
Free program
fmoletta Jul 24, 2023
fd379ad
Free data used in load_data tests
fmoletta Jul 24, 2023
34d73ec
Swap clear for free
fmoletta Jul 24, 2023
436c3b7
Merge cairo-runner branch
mmsc2 Jul 24, 2023
6ba7cdf
Execution functions
mmsc2 Jul 24, 2023
34fb629
Fix error with relocatables
mmsc2 Jul 25, 2023
540b21b
Add compute operands functions
mmsc2 Jul 25, 2023
8dee43a
Decude op1
mmsc2 Jul 25, 2023
c866258
Fix error on declaration and add types to enum
mmsc2 Jul 25, 2023
cbeb378
maybe reloc
mmsc2 Jul 26, 2023
8d1aba6
merge main
mmsc2 Jul 26, 2023
55db074
add value
mmsc2 Jul 26, 2023
443d5ae
Add maybe relocatable
mmsc2 Jul 26, 2023
f3ebdab
Merge branch 'main' into ExecutionImplementation
mmsc2 Jul 27, 2023
70486a5
Fix err typo
mmsc2 Jul 27, 2023
e5227fb
Fix test
mmsc2 Jul 27, 2023
3a0bb83
Merge branch 'main' into ExecutionImplementation
mmsc2 Jul 27, 2023
3a50829
Merge branch 'main' into ExecutionImplementation
mmsc2 Jul 27, 2023
e404a16
Merge branch 'ExecutionImplementation' of github.com:lambdaclass/cair…
mmsc2 Jul 27, 2023
c77aecc
Fix error on mac
mmsc2 Jul 27, 2023
3c6f073
insert_deduced operatnds
mmsc2 Jul 28, 2023
0181ffe
fix tests
mmsc2 Jul 28, 2023
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
Empty file added computed_operands_res
Empty file.
2 changes: 2 additions & 0 deletions src/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ enum VirtualMachineError {
InvalidRes,
InvalidOpcode,
InvalidApUpdate,
MemoryError,
None
};

union ResultInstructionValue {
Expand Down
3 changes: 1 addition & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdio.h>

int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
printf("Hello World!\n");

printf("hello word");
return 0;
}
23 changes: 12 additions & 11 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ memory memory_new(void) {
ResultMemory memory_get(memory *mem, relocatable ptr) {
maybe_relocatable *value = NULL;
if (cc_hashtable_get(mem->data, &ptr, (void *)&value) == CC_OK) {
ResultMemory ok = {.is_error = false, .value = {.memory_value = *value}};
ResultMemory ok = {.type = MaybeRelocatable, .value = {.memory_value = *value}};
return ok;
}
ResultMemory error = {.is_error = true, .value = {.error = Get}};
ResultMemory error = {.type = Err, .value = {.error = Get}};
return error;
}

ResultMemory memory_insert(memory *mem, relocatable ptr, maybe_relocatable value) {
// Guard out of bounds writes
if (ptr.segment_index >= mem->num_segments) {
ResultMemory error = {.is_error = true, .value = {.error = Insert}};
ResultMemory error = {.type = Err, .value = {.error = Insert}};
return error;
}
// Guard overwrites
maybe_relocatable *prev_value = NULL;
if (cc_hashtable_get(mem->data, &ptr, (void *)&prev_value) == CC_OK) {
if (maybe_relocatable_equal(prev_value, &value)) {
ResultMemory ok = {.is_error = false, .value = {.none = 0}};
ResultMemory ok = {.type = Int, .value = {.none = 0}};
return ok;
} else {
ResultMemory error = {.is_error = true, .value = {.error = Insert}};
ResultMemory error = {.type = Err, .value = {.error = Insert}};
return error;
}
}
Expand All @@ -59,10 +59,10 @@ ResultMemory memory_insert(memory *mem, relocatable ptr, maybe_relocatable value
maybe_relocatable *value_alloc = malloc(sizeof(maybe_relocatable));
*value_alloc = value;
if (cc_hashtable_add(mem->data, ptr_alloc, value_alloc) == CC_OK) {
ResultMemory ok = {.is_error = false, .value = {.none = 0}};
ResultMemory ok = {.type = Int, .value = {.none = 0}};
return ok;
}
ResultMemory error = {.is_error = true, .value = {.error = Insert}};
ResultMemory error = {.type = Err, .value = {.error = Insert}};
return error;
}

Expand All @@ -72,21 +72,22 @@ relocatable memory_add_segment(memory *memory) {
return rel;
}


ResultMemory memory_load_data(memory *mem, relocatable ptr, CC_Array *data) {
// Load each value sequentially
CC_ArrayIter data_iter;
cc_array_iter_init(&data_iter, data);
maybe_relocatable *value = NULL;
while (cc_array_iter_next(&data_iter, (void *)&value) != CC_ITER_END) {
// Insert Value
if (memory_insert(mem, ptr, *value).is_error) {
ResultMemory error = {.is_error = true, .value = {.error = LoadData}};
if (memory_insert(mem, ptr, *value).type == Err) {
ResultMemory error = {.type = Err, .value = {.error = LoadData}};
return error;
}
// Advance ptr
ptr.offset += 1;
}
ResultMemory ok = {.is_error = false, .value = {.ptr = ptr}};
ResultMemory ok = {.type = Err, .value = {.ptr = ptr}};
return ok;
}

Expand All @@ -96,7 +97,7 @@ void print_memory(memory *mem) {
relocatable ptr = {i, 0};
while (true) {
ResultMemory result = memory_get(mem, ptr);
if (result.is_error) {
if (result.type == Err) {
break;
}
maybe_relocatable v = result.value.memory_value;
Expand Down
4 changes: 3 additions & 1 deletion src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum MemoryError {
LoadData,
};

enum ResultMemoryType { Felt, Relocatable, MaybeRelocatable, Int, Err };

union ResultMemoryValue {
enum MemoryError error;
maybe_relocatable memory_value;
Expand All @@ -29,7 +31,7 @@ union ResultMemoryValue {
};

typedef struct ResultMemory {
bool is_error;
enum ResultMemoryType type;
union ResultMemoryValue value;
} ResultMemory;

Expand Down
33 changes: 33 additions & 0 deletions src/relocatable.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
#include "relocatable.h"
#include "utils.h"

// --------------------------------
// Sign operations for relocatable
// --------------------------------

// TODO: fix this to thrown an error if off < other
relocatable sub_relocatable(relocatable a, uint64_t other) {
unsigned int offset = a.offset;
unsigned int new_offset = offset - other;
relocatable result = {.segment_index = a.segment_index, .offset = new_offset};

return result;
}

uint64_t checked_add(uint64_t a, uint64_t b) {
uint64_t res = a + b;
if (res <= a || res <= b)
return 0;
else
return res;
}

// TODO: fix this to thrown an error. if add overflows
relocatable add_relocatable(relocatable a, uint64_t other) {
unsigned int off = a.offset;
uint64_t check_sum = checked_add(off, other);
relocatable res = {.segment_index = a.segment_index, .offset = check_sum};
return res;
}

// -----------------------------------
// operations for maybe_relocatable
// -----------------------------------

bool maybe_relocatable_equal(maybe_relocatable *a, maybe_relocatable *b) {
if (a->is_felt && b->is_felt) {
return felt_equal(a->value.felt, b->value.felt);
Expand Down
5 changes: 4 additions & 1 deletion src/relocatable.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef RELOCATABLE_H
#define RELOCATABLE_H

#include "../lambdaworks/lib/lambdaworks.h"
#include <stdbool.h>
#include <stdio.h>

typedef struct relocatable {
unsigned int segment_index;
Expand All @@ -18,8 +20,9 @@ typedef struct maybe_relocatable {
bool is_felt;
} maybe_relocatable;

relocatable sub_relocatable(relocatable a, uint64_t other);
relocatable add_relocatable(relocatable a, uint64_t other);
bool maybe_relocatable_equal(maybe_relocatable *a, maybe_relocatable *b);

maybe_relocatable maybe_relocatable_from_felt_limbs(limb_t *felt);

#endif
75 changes: 75 additions & 0 deletions src/run_context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "run_context.h"
#include "instruction.h"

relocatable compute_dst_addr(run_context run_ctx, Instruction instr) {
struct relocatable base_addr;
switch (instr.dst_register) {
case REG_AP: {
base_addr = run_ctx.ap;
} break;
default: {
base_addr = run_ctx.fp;
} break;
}

relocatable res_base_addr;
if (instr.off0 < 0) {
res_base_addr = sub_relocatable(base_addr, (uint64_t)absolute(instr.off0));
} else {
res_base_addr = add_relocatable(base_addr, (uint64_t)instr.off0);
}

return res_base_addr;
}

relocatable compute_op0_addr(run_context run_ctx, Instruction instr) {
struct relocatable base_addr;
switch (instr.op0_register) {
case REG_AP: {
base_addr = run_ctx.ap;
} break;
default: {
base_addr = run_ctx.fp;
} break;
}

relocatable res_base_addr;
if (instr.off0 < 0) {
res_base_addr = sub_relocatable(base_addr, (uint64_t)absolute(instr.off1));
} else {
res_base_addr = add_relocatable(base_addr, (uint64_t)instr.off1);
}
return res_base_addr;
}

relocatable compute_op1_addr(run_context run_ctx, Instruction instr, maybe_relocatable op0) {
relocatable base_addr;
switch (instr.op1_addr) {
case OP_FP:
base_addr = run_ctx.fp;
break;
case OP_AP:
base_addr = run_ctx.ap;
break;
case Imm:
if (instr.off2 == 1)
base_addr = run_ctx.pc;
break;
case Op0: {
if (!op0.is_felt) {
base_addr = op0.value.relocatable;
}
} break;
default: {
base_addr = (relocatable){.segment_index = 0, .offset = 0};
} break;
}

relocatable res_base_addr;
if (instr.off2 < 0) {
res_base_addr = sub_relocatable(base_addr, (uint64_t)absolute(instr.off2));
} else {
res_base_addr = add_relocatable(base_addr, (uint64_t)instr.off2);
}
return res_base_addr;
}
6 changes: 6 additions & 0 deletions src/run_context.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#ifndef RUN_CONTEXT_H
#define RUN_CONTEXT_H

#include "instruction.h"
#include "relocatable.h"
#include "utils.h"

typedef struct run_context {
relocatable pc;
relocatable ap;
relocatable fp;
} run_context;

relocatable compute_dst_addr(run_context run_ctx, Instruction instr);
relocatable compute_op0_addr(run_context run_ctx, Instruction instr);
relocatable compute_op1_addr(run_context run_ctx, Instruction instr, maybe_relocatable op0);

#endif
Loading
Loading