From d4d887e6a1d0a72f8df41e6b66b59f322197680e Mon Sep 17 00:00:00 2001 From: zongor Date: Sat, 14 Jun 2025 00:10:24 -0400 Subject: [PATCH] add float and int >,<,>=,<= --- src/main.c | 11 ++++---- src/vm.c | 79 +++++++++++++++++++++++++++++++++++++++++++++--------- src/vm.h | 45 ++++++++++++++++++------------- 3 files changed, 99 insertions(+), 36 deletions(-) diff --git a/src/main.c b/src/main.c index 3c1416e..751c876 100644 --- a/src/main.c +++ b/src/main.c @@ -5,10 +5,10 @@ #define MEMORY_SIZE 1024 int main() { - Data memory[MEMORY_SIZE]; /* Memory array */ + Data memory[MEMORY_SIZE] = {0}; /* Memory array */ int i = 0; - memory[i++].u = OP_ADD_F32; + memory[i++].u = OP_ADD_REAL; memory[i++].u = 102; memory[i++].u = 103; memory[i++].u = 103; @@ -16,11 +16,11 @@ int main() { memory[i++].u = 100; memory[i++].u = 101; memory[i++].u = 100; - memory[i++].u = OP_JGZ; + memory[i++].u = OP_JGT_INT; memory[i++].u = 100; + memory[i++].u = 99; memory[i++].u = 0; - memory[i++].u = 0; - memory[i++].u = OP_F32_TO_INT; + memory[i++].u = OP_REAL_TO_INT; memory[i++].u = 103; memory[i++].u = 0; memory[i++].u = 103; @@ -44,6 +44,7 @@ int main() { memory[i++].u = 0; memory[i++].u = 0; memory[i++].u = 0; + memory[99].u = 0; memory[100].u = 5; memory[101].u = 1; memory[102].f = 5.f; diff --git a/src/vm.c b/src/vm.c index 1e60beb..59f2ebc 100644 --- a/src/vm.c +++ b/src/vm.c @@ -56,30 +56,30 @@ void run_vm(Data *memory, uint32_t memory_size) { memory[dest_addr].u = memory[src1_addr].u / memory[src2_addr].u; break; - case OP_ADD_F32: + case OP_ADD_REAL: memory[dest_addr].f = memory[src1_addr].f + memory[src2_addr].f; break; - case OP_SUB_F32: + case OP_SUB_REAL: memory[dest_addr].f = memory[src1_addr].f - memory[src2_addr].f; break; - case OP_MUL_F32: + case OP_MUL_REAL: memory[dest_addr].f = memory[src1_addr].f * memory[src2_addr].f; break; - case OP_DIV_F32: + case OP_DIV_REAL: if (memory[src2_addr].f == 0.0f) { printf("Division by zero error at address %d\n", pc - 4); exit(1); } memory[dest_addr].f = memory[src1_addr].f / memory[src2_addr].f; break; - case OP_F32_TO_INT: { + case OP_REAL_TO_INT: { memory[dest_addr].u = (uint32_t)memory[src1_addr].f; break; } - case OP_INT_TO_F32: { + case OP_INT_TO_REAL: { memory[dest_addr].f = (float)memory[src1_addr].u; break; } @@ -89,13 +89,68 @@ void run_vm(Data *memory, uint32_t memory_size) { case OP_JMP: pc = src1_addr; /* Jump to address */ break; - case OP_JGZ: { + case OP_JGT_INT: { uint32_t value = memory[src1_addr].u; - uint32_t jump_target = src2_addr; + uint32_t value2 = memory[src2_addr].u; + uint32_t jump_target = dest_addr; - /* Branchless greater-than-zero check */ - int32_t mask = -((uint32_t)(value > 0)); - pc = (jump_target & mask) | (pc & ~mask); + pc = (value > value2) ? jump_target : pc; + break; + } + case OP_JLT_INT: { + uint32_t value = memory[src1_addr].u; + uint32_t value2 = memory[src2_addr].u; + uint32_t jump_target = dest_addr; + + pc = (value < value2) ? jump_target : pc; + break; + } + case OP_JGT_REAL: { + float value = memory[src1_addr].f; + float value2 = memory[src2_addr].f; + uint32_t jump_target = dest_addr; + + pc = (value > value2) ? jump_target : pc; + break; + } + case OP_JLT_REAL: { + float value = memory[src1_addr].f; + float value2 = memory[src2_addr].f; + uint32_t jump_target = dest_addr; + + pc = (value < value2) ? jump_target : pc; + break; + } + case OP_JGE_INT: { + uint32_t value = memory[src1_addr].u; + uint32_t value2 = memory[src2_addr].u; + uint32_t jump_target = dest_addr; + + pc = (value >= value2) ? jump_target : pc; + break; + } + case OP_JLE_INT: { + uint32_t value = memory[src1_addr].u; + uint32_t value2 = memory[src2_addr].u; + uint32_t jump_target = dest_addr; + + pc = (value <= value2) ? jump_target : pc; + break; + } + case OP_JGE_REAL: { + float value = memory[src1_addr].f; + float value2 = memory[src2_addr].f; + uint32_t jump_target = dest_addr; + + pc = (value >= value2) ? jump_target : pc; + break; + } + case OP_JLE_REAL: { + float value = memory[src1_addr].f; + float value2 = memory[src2_addr].f; + uint32_t jump_target = dest_addr; + + pc = (value <= value2) ? jump_target : pc; break; } case OP_INT_TO_STRING: { @@ -105,7 +160,7 @@ void run_vm(Data *memory, uint32_t memory_size) { mem_strcpy(memory, buffer, strlen(buffer), dest_addr); break; } - case OP_F32_TO_STRING: { + case OP_REAL_TO_STRING: { float a = memory[src1_addr].f; char buffer[32]; sprintf(buffer, "%f", a); diff --git a/src/vm.h b/src/vm.h index 5aaf1e5..36027fa 100644 --- a/src/vm.h +++ b/src/vm.h @@ -10,25 +10,32 @@ typedef union { } Data; typedef enum { - OP_HALT, /* terminate execution */ - OP_ADD, /* dest = src1 + src2 */ - OP_SUB, /* dest = src1 - src2 */ - OP_MUL, /* dest = src1 * src2 */ - OP_DIV, /* dest = src1 / src2 */ - OP_ADD_F32, /* dest = src1 + src2 */ - OP_SUB_F32, /* dest = src1 - src2 */ - OP_MUL_F32, /* dest = src1 * src2 */ - OP_DIV_F32, /* dest = src1 / src2 */ - OP_F32_TO_INT, /* dest = src1 as int */ - OP_INT_TO_F32, /* dest = src1 as f32 */ - OP_MOV, /* dest = src1 */ - OP_JMP, /* jump to address src1 unconditionally */ - OP_JGZ, /* jump to address dest if src1 > 0 */ - OP_INT_TO_STRING, /* dest = src1 as str */ - OP_F32_TO_STRING, /* dest = src2 as str */ - OP_READ_STRING, /* dest = src1 */ - OP_PRINT_STRING, /* dest = src1 */ - OP_CMP_STRING, /* dest = src1 */ + OP_HALT, /* terminate execution */ + OP_ADD, /* dest = src1 + src2 */ + OP_SUB, /* dest = src1 - src2 */ + OP_MUL, /* dest = src1 * src2 */ + OP_DIV, /* dest = src1 / src2 */ + OP_JGT_INT, /* jump to address dest if src1 as int > src2 as int*/ + OP_JLT_INT, /* jump to address dest if src1 as int < src2 as int */ + OP_JLE_INT, /* jump to address dest if src1 as int <= src2 as int */ + OP_JGE_INT, /* jump to address dest if src1 as int >= src2 as int*/ + OP_INT_TO_REAL, /* dest = src1 as f32 */ + OP_ADD_REAL, /* dest = src1 + src2 */ + OP_SUB_REAL, /* dest = src1 - src2 */ + OP_MUL_REAL, /* dest = src1 * src2 */ + OP_DIV_REAL, /* dest = src1 / src2 */ + OP_JGE_REAL, /* jump to address dest if src1 as real >= src2 as real */ + OP_JGT_REAL, /* jump to address dest if src1 as real > src2 as real */ + OP_JLT_REAL, /* jump to address dest if src1 as real < src2 as real */ + OP_JLE_REAL, /* jump to address dest if src1 as real <= src2 as real */ + OP_REAL_TO_INT, /* dest = src1 as int */ + OP_MOV, /* dest = src1 */ + OP_JMP, /* jump to address src1 unconditionally */ + OP_INT_TO_STRING, /* dest = src1 as str */ + OP_REAL_TO_STRING, /* dest = src2 as str */ + OP_READ_STRING, /* dest = src1 */ + OP_PRINT_STRING, /* dest = src1 */ + OP_CMP_STRING, /* dest = src1 */ } Opcode; void run_vm(Data *memory, uint32_t memory_size);