diff --git a/src/cairo_runner.c b/src/cairo_runner.c index 44a80ce..8264c4f 100644 --- a/src/cairo_runner.c +++ b/src/cairo_runner.c @@ -1,11 +1,13 @@ #include "cairo_runner.h" +#include "clist.h" #include "program.h" #include "relocatable.h" #include "vm.h" cairo_runner runner_new(struct program program) { virtual_machine vm = vm_new(); - cairo_runner runner = {program, vm, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; + CList *relocated_memory = CList_init(sizeof(felt_t)); + cairo_runner runner = {program, vm, relocated_memory, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}; return runner; } @@ -81,3 +83,5 @@ relocatable runner_initialize(cairo_runner *runner) { runner_initialize_vm(runner); return end; } + +void runner_relocate_memory(cairo_runner *runner) { (void)runner; } diff --git a/src/cairo_runner.h b/src/cairo_runner.h index cd4580f..28d1094 100644 --- a/src/cairo_runner.h +++ b/src/cairo_runner.h @@ -6,9 +6,20 @@ #include "relocatable.h" #include "vm.h" +typedef union relocated_memory_value { + felt_t value; + int none; +} relocated_memory_value; + +typedef struct relocated_memory_cell { + relocated_memory_value memory_value; + bool is_some; +} relocated_memory_cell; + typedef struct cairo_runner { struct program program; virtual_machine vm; + CList *relocated_memory; relocatable program_base; relocatable execution_base; relocatable initial_pc; @@ -26,4 +37,6 @@ void runner_free(cairo_runner *runner); // Performs the intialization step, leaving the runner ready to run a loaded cairo program from a main entrypoint relocatable runner_initialize(cairo_runner *runner); +void runner_relocate_memory(cairo_runner *runner); + #endif diff --git a/test/runner_tests.c b/test/runner_tests.c index 3a26b8b..58308e7 100644 --- a/test/runner_tests.c +++ b/test/runner_tests.c @@ -1,7 +1,9 @@ #include "runner_tests.h" +#include "cairo_runner.h" #include "clist.h" #include "memory.h" #include "program.h" +#include "relocatable.h" #include "run_context.h" #include "utils.h" #include "vm.h" @@ -98,6 +100,83 @@ void initialize_runner_no_builtins_no_proof_mode_empty_program(void) { printf("OK!\n"); } +void relocate_runner_memory(void) { + struct CList *program_data = CList_init(sizeof(maybe_relocatable)); + struct program program = {0, program_data}; + cairo_runner runner = runner_new(program); + runner_initialize(&runner); + + relocatable ptr_0_0 = {0, 0}; + relocatable ptr_0_1 = {0, 1}; + relocatable ptr_0_2 = {0, 2}; + relocatable ptr_1_0 = {1, 0}; + relocatable ptr_1_1 = {1, 1}; + relocatable ptr_1_5 = {1, 5}; + felt_t value_0_0; + felt_t value_0_1; + felt_t value_0_2; + relocatable value_1_0 = {2, 0}; + relocatable value_1_1 = {3, 0}; + felt_t value_1_5; + from(value_0_0, 4613515612218425347); + from(value_0_1, 5); + from(value_0_2, 2345108766317314046); + from(value_1_5, 5); + maybe_relocatable elem_0_0 = maybe_relocatable_from_felt_limbs(value_0_0); + maybe_relocatable elem_0_1 = maybe_relocatable_from_felt_limbs(value_0_1); + maybe_relocatable elem_0_2 = maybe_relocatable_from_felt_limbs(value_0_2); + maybe_relocatable elem_1_0 = {.value = {.relocatable = value_1_0}, .is_felt = false}; + maybe_relocatable elem_1_1 = {.value = {.relocatable = value_1_1}, .is_felt = false}; + maybe_relocatable elem_1_5 = maybe_relocatable_from_felt_limbs(value_1_5); + memory_insert(&runner.vm.memory, ptr_0_0, elem_0_0); + memory_insert(&runner.vm.memory, ptr_0_1, elem_0_1); + memory_insert(&runner.vm.memory, ptr_0_2, elem_0_2); + memory_insert(&runner.vm.memory, ptr_1_0, elem_1_0); + memory_insert(&runner.vm.memory, ptr_1_1, elem_1_1); + memory_insert(&runner.vm.memory, ptr_1_5, elem_1_5); + + runner_relocate_memory(&runner); + + relocated_memory_cell *relocated_memory_0 = runner.relocated_memory->at(runner.relocated_memory, 0); + assert(!relocated_memory_0->is_some); + relocated_memory_cell *relocated_memory_1 = runner.relocated_memory->at(runner.relocated_memory, 1); + felt_t expected_1; + from(expected_1, 4613515612218425347); + assert(relocated_memory_1->is_some); + assert(felt_equal(relocated_memory_1->memory_value.value, expected_1)); + relocated_memory_cell *relocated_memory_2 = runner.relocated_memory->at(runner.relocated_memory, 2); + felt_t expected_2; + from(expected_2, 5); + assert(relocated_memory_2->is_some); + assert(felt_equal(relocated_memory_2->memory_value.value, expected_2)); + relocated_memory_cell *relocated_memory_3 = runner.relocated_memory->at(runner.relocated_memory, 3); + felt_t expected_3; + from(expected_3, 2345108766317314046); + assert(relocated_memory_3->is_some); + assert(felt_equal(relocated_memory_3->memory_value.value, expected_3)); + relocated_memory_cell *relocated_memory_4 = runner.relocated_memory->at(runner.relocated_memory, 4); + felt_t expected_4; + from(expected_4, 10); + assert(relocated_memory_4->is_some); + assert(felt_equal(relocated_memory_4->memory_value.value, expected_4)); + relocated_memory_cell *relocated_memory_5 = runner.relocated_memory->at(runner.relocated_memory, 5); + felt_t expected_5; + from(expected_5, 10); + assert(relocated_memory_5->is_some); + assert(felt_equal(relocated_memory_5->memory_value.value, expected_5)); + relocated_memory_cell *relocated_memory_6 = runner.relocated_memory->at(runner.relocated_memory, 6); + assert(!relocated_memory_6->is_some); + relocated_memory_cell *relocated_memory_7 = runner.relocated_memory->at(runner.relocated_memory, 7); + assert(!relocated_memory_7->is_some); + relocated_memory_cell *relocated_memory_8 = runner.relocated_memory->at(runner.relocated_memory, 8); + assert(!relocated_memory_8->is_some); + relocated_memory_cell *relocated_memory_9 = runner.relocated_memory->at(runner.relocated_memory, 9); + felt_t expected_9; + from(expected_9, 5); + assert(relocated_memory_9->is_some); + assert(felt_equal(relocated_memory_9->memory_value.value, expected_9)); +} + void runner_tests(void) { printf("--------------------------------- \n"); printf("Test: initialize_runner_no_builtins_no_proof_mode_empty_program \n"); @@ -105,4 +184,7 @@ void runner_tests(void) { printf("--------------------------------- \n"); printf("Test: initialize_runner_no_builtins_no_proof_mode_non_empty_program \n"); initialize_runner_no_builtins_no_proof_mode_empty_program(); + printf("--------------------------------- \n"); + printf("Test: relocate_runner_memory \n"); + relocate_runner_memory(); }