Skip to content

Commit

Permalink
Merge branch 'ExecutionImplementation' into cairo-run-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-calvin committed Jul 28, 2023
2 parents 0c55154 + 3c6f073 commit 3bc6117
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
23 changes: 18 additions & 5 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
#include "relocatable.h"
#include <stdint.h>

typedef struct felt_to_u64_result {
bool is_ok;
uint64_t val;
} felt_to_u64_result;

// TODO: check this function
uint64_t to_u64(felt_t f) { return f[3]; }
felt_to_u64_result to_u64(felt_t f) {
if (f[0] == 0 && f[1] == 0 && f[2] == 0) {
felt_to_u64_result res = {.is_ok = true, .val = f[3]};
return res;
} else {
felt_to_u64_result res = {.is_ok = false, .val = 0};
return res;
};
}

maybe_relocatable add_maybe_relocatable(maybe_relocatable a, maybe_relocatable b) {
if (a.is_felt && b.is_felt) {
Expand All @@ -20,8 +33,8 @@ maybe_relocatable add_maybe_relocatable(maybe_relocatable a, maybe_relocatable b
felt_t f1 = {a.value.felt[0], a.value.felt[1], a.value.felt[2], a.value.felt[3]};
relocatable rel = b.value.relocatable;
uint64_t offset = (uint64_t)rel.offset;
uint64_t other = to_u64(f1);
uint64_t new_offset = offset + other;
felt_to_u64_result other = to_u64(f1);
uint64_t new_offset = offset + other.val;
maybe_relocatable res = {
.is_felt = false,
.value = {.relocatable = {.offset = new_offset, .segment_index = rel.segment_index}}};
Expand All @@ -31,8 +44,8 @@ maybe_relocatable add_maybe_relocatable(maybe_relocatable a, maybe_relocatable b
felt_t f1 = {a.value.felt[0], a.value.felt[1], a.value.felt[2], a.value.felt[3]};
relocatable rel = b.value.relocatable;
uint64_t offset = (uint64_t)rel.offset;
uint64_t other = to_u64(f1);
uint64_t new_offset = offset + other;
felt_to_u64_result other = to_u64(f1);
uint64_t new_offset = offset + other.val;
maybe_relocatable res = {
.is_felt = false,
.value = {.relocatable = {.offset = new_offset, .segment_index = rel.segment_index}}};
Expand Down
51 changes: 47 additions & 4 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,50 @@ maybe_relocatable compute_res(Instruction instr, maybe_relocatable op0, maybe_re
return res;
}

bool was_op0_deducted(uint8_t ded_ops) { return (ded_ops & 1 << 1) != 0; }
bool was_op1_deducted(uint8_t ded_ops) { return (ded_ops & 1 << 2) != 0; }
bool was_dst_deducted(uint8_t ded_ops) { return (ded_ops & 1) != 0; }

vm_result insert_deduced_operands(memory mem, uint8_t ded_ops, operands opers, operands_addresses oper_addr) {
vm_result result;
if (was_op0_deducted(ded_ops)) {
ResultMemory res = memory_insert(&mem, oper_addr.op0_addr, opers.op0);
if (res.type == Err) {
result = (vm_result){
.is_ok = false,
.error = MemoryError,
};
}
}
if (was_op1_deducted(ded_ops)) {
ResultMemory res = memory_insert(&mem, oper_addr.op1_addr, opers.op1);
if (res.type == Err) {
result = (vm_result){
.is_ok = false,
.error = MemoryError,
};
}
}
if (was_dst_deducted(ded_ops)) {
ResultMemory res = memory_insert(&mem, oper_addr.dst_addr, opers.dst);
if (res.type == Err) {
result = (vm_result){
.is_ok = false,
.error = MemoryError,
};
}
}

else {
result = (vm_result){
.is_ok = true,
.error = None,
};
}

return result;
}

computed_operands_res compute_operands(virtual_machine vm, Instruction instr) {
relocatable dst_addr = compute_dst_addr(vm.run_context, instr);
ResultMemory dst_op = memory_get(&vm.memory, dst_addr);
Expand All @@ -44,10 +88,6 @@ computed_operands_res compute_operands(virtual_machine vm, Instruction instr) {

relocatable op0_addr = compute_op0_addr(vm.run_context, instr);
ResultMemory op0_op = memory_get(&vm.memory, op0_addr);
if (op0_op.type == Err) {
computed_operands_res res = {.value = {.error = MemoryError}, .is_error = true};
return res;
}

relocatable op1_addr = compute_op1_addr(vm.run_context, instr, op0_op.value.memory_value);
ResultMemory op1_op = memory_get(&vm.memory, op1_addr);
Expand All @@ -65,6 +105,9 @@ computed_operands_res compute_operands(virtual_machine vm, Instruction instr) {

// for now this is always pre computed. we should handle the case when it is not
maybe_relocatable op0 = op0_op.value.memory_value;
if (op0_op.type == Err) {

}
maybe_relocatable op1 = op1_op.value.memory_value;

// compute res
Expand Down

0 comments on commit 3bc6117

Please sign in to comment.