From 2f3c0adbd0659814b4dbdb567c3472c5b16d180f Mon Sep 17 00:00:00 2001 From: ax-6 Date: Sat, 4 May 2024 23:09:29 +0800 Subject: [PATCH] Partial operator functions for bytecode were developed. Not completed. --- aqvm/interpreter/bytecode/bytecode.c | 207 +++++++++++++++++++++++---- aqvm/interpreter/bytecode/bytecode.h | 124 +++++++++------- 2 files changed, 252 insertions(+), 79 deletions(-) diff --git a/aqvm/interpreter/bytecode/bytecode.c b/aqvm/interpreter/bytecode/bytecode.c index 7195c4a..2658919 100644 --- a/aqvm/interpreter/bytecode/bytecode.c +++ b/aqvm/interpreter/bytecode/bytecode.c @@ -4,34 +4,179 @@ #include "aqvm/interpreter/bytecode/bytecode.h" -// TODO(Bytecode): Finish these functions after completing bytecode development. -/* -int AqvmInterpreterBytecode_NOP(int *registers){} -int AqvmInterpreterBytecode_LDC(int *registers, int const value){} -int AqvmInterpreterBytecode_LOAD(int *registers, int const index){} -int AqvmInterpreterBytecode_STORE(int *registers, int const index, int const value){} -int AqvmInterpreterBytecode_NEW(int *registers, int const num_elements){} -int AqvmInterpreterBytecode_FREE(int *registers, int const index){} -int AqvmInterpreterBytecode_SIZE(int *registers, int const index){} -int AqvmInterpreterBytecode_ADD(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_SUB(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_MUL(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_DIV(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_MOD(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_NEG(int *registers, int const value){} -int AqvmInterpreterBytecode_SHL(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_SHR(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_SAR(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_IF(int *registers, int const condition, int const offset){} -int AqvmInterpreterBytecode_WHILE(int *registers, int const condition, int const offset){} -int AqvmInterpreterBytecode_NOT(int *registers, int const value){} -int AqvmInterpreterBytecode_AND(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_OR(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_XOR(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_CMP(int *registers, int const left, int const right){} -int AqvmInterpreterBytecode_INVOKE(int *registers, int (*function)(int *), int const num_args){} -int AqvmInterpreterBytecode_RETURN(int *registers, int const value){} -int AqvmInterpreterBytecode_GOTO(int *registers, int const offset){} -int AqvmInterpreterBytecode_THROW(int *registers, int const value){} -int AqvmInterpreterBytecode_WIDE(int *registers){} -*/ +#include + +#include "aqvm/interpreter/register/register.h" + +int AqvmInterpreterBytecode_NOP() { return 0; } + +int AqvmInterpreterBytecode_LDC(void* value, + AqvmInterpreterRegister_Register* register) { + switch (register->type) { + case AqvmInterpreterRegisterValueType_INT: + register->value.int_value = *(int*)value; + break; + case AqvmInterpreterRegisterValueType_FLOAT: + register->value.float_value = *(float*)value; + break; + case AqvmInterpreterRegisterValueType_DOUBLE: + register->value.double_value = *(double*)value; + break; + case AqvmInterpreterRegisterValueType_LONG: + register->value.long_value = *(long*)value; + break; + case AqvmInterpreterRegisterValueType_CHARACTER: + register->value.character_value = *(char*)value; + break; + case AqvmInterpreterRegisterValueType_BOOLEAN: + register->value.boolean_value = *(bool*)value; + break; + + default: + // TODO(ERROR): Handle the error of the register value type. + return -1; + } + + return 0; +} + +int AqvmInterpreterBytecode_LOAD(AqvmInterpreterRegister_Register* ptr, + AqvmInterpreterRegister_Register* register) { + *register = *ptr; + return 0; +} + +int AqvmInterpreterBytecode_STORE(AqvmInterpreterRegister_Register** ptr, + AqvmInterpreterRegister_Register* register) { + *ptr = register; + return 0; +} + +int AqvmInterpreterBytecode_NEW(void** ptr, size_t size) { + *ptr = malloc(size); + if (*ptr == NULL) { + // TODO(WARNING): Handle the warning of memory allocation. + return -1; + } + return 0; +} + +int AqvmInterpreterBytecode_FREE(void* ptr, size_t size) { + free(ptr); + if (ptr == NULL) { + // TODO(WARNING): Handle the warning of memory deallocation. + return -1; + } + return 0; +} + +// TODO(SIZE): Wait to develop this function until the form of this operator has +// been decided. +int AqvmInterpreterBytecode_SIZE() {} + +int AqvmInterpreterBytecode_ADD(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) { + if (opcode1->type != opcode2->type) { + // TODO(ERROR): Handle the error of the register value type. + return -1; + } + result->type = opcode1->type; + switch (result->type) { + case AqvmInterpreterRegisterValueType_INT: + result->value.int_value = + opcode1->value.int_value + opcode2->value.int_value; + break; + case AqvmInterpreterRegisterValueType_FLOAT: + result->value.float_value = + opcode1->value.float_value + opcode2->value.float_value; + break; + case AqvmInterpreterRegisterValueType_DOUBLE: + result->value.double_value = + opcode1->value.double_value + opcode2->value.double_value; + break; + case AqvmInterpreterRegisterValueType_LONG: + result->value.long_value = + opcode1->value.long_value + opcode2->value.long_value; + break; + + default: + // TODO(ERROR): Handle the error of the register value type. + return -1; + } + return 0; +} +int AqvmInterpreterBytecode_SUB(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) { + if (opcode1->type != opcode2->type) { + // TODO(ERROR): Handle the error of the register value type. + return -1; + } + result->type = opcode1->type; + switch (result->type) { + case AqvmInterpreterRegisterValueType_INT: + result->value.int_value = + opcode1->value.int_value - opcode2->value.int_value; + break; + case AqvmInterpreterRegisterValueType_FLOAT: + result->value.float_value = + opcode1->value.float_value - opcode2->value.float_value; + break; + case AqvmInterpreterRegisterValueType_DOUBLE: + result->value.double_value = + opcode1->value.double_value - opcode2->value.double_value; + break; + case AqvmInterpreterRegisterValueType_LONG: + result->value.long_value = + opcode1->value.long_value - opcode2->value.long_value; + break; + + default: + // TODO(ERROR): Handle the error of the register value type. + return -1; + } + return 0; +} + + + +// TODO(Bytecode): Waiting for development. +int AqvmInterpreterBytecode_MUL(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_DIV(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_MOD(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_NEG(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_SHL(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_SHR(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_SAR(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_IF() {} +int AqvmInterpreterBytecode_WHILE() {} +int AqvmInterpreterBytecode_NOT(bool result, bool opcode) {} +int AqvmInterpreterBytecode_AND(bool result, bool opcode1, bool opcode2) {} +int AqvmInterpreterBytecode_OR(bool result, bool opcode1, bool opcode2) {} +int AqvmInterpreterBytecode_XOR(bool result, bool opcode1, bool opcode2) {} +int AqvmInterpreterBytecode_CMP(bool result, int operator, + AqvmInterpreterRegister_Register * opcode1, + AqvmInterpreterRegister_Register* opcode2) {} +int AqvmInterpreterBytecode_INVOKE() {} +int AqvmInterpreterBytecode_RETURN() {} +int AqvmInterpreterBytecode_GOTO() {} +int AqvmInterpreterBytecode_THROW() {} + +// TODO(WIDE): Update this function if an extended instruction set is +// required. +int AqvmInterpreterBytecode_WIDE() {} \ No newline at end of file diff --git a/aqvm/interpreter/bytecode/bytecode.h b/aqvm/interpreter/bytecode/bytecode.h index 86f9cc8..c78fbea 100644 --- a/aqvm/interpreter/bytecode/bytecode.h +++ b/aqvm/interpreter/bytecode/bytecode.h @@ -5,66 +5,94 @@ #ifndef AQ_AQVM_INTERPRETER_BYTECODE_BYTECODE_H_ #define AQ_AQVM_INTERPRETER_BYTECODE_BYTECODE_H_ +#include + +#include "aqvm/interpreter/register/register.h" + enum AqvmInterpreterBytecode_Type { - NOP = 0x00, - LDC, - LOAD, - STORE, - NEW, - FREE, - SIZE, - ADD, - SUB, - MUL, - DIV, - MOD, - NEG, - SHL, - SHR, - SAR, - IF, - WHILE, - NOT, - AND, - OR, - XOR, - CMP, - INVOKE, - RETURN, - GOTO, - THROW, - WIDE = 0xFF + AqvmInterpreterBytecodeType_NOP = 0x00, + AqvmInterpreterBytecodeType_LDC, + AqvmInterpreterBytecodeType_LOAD, + AqvmInterpreterBytecodeType_STORE, + AqvmInterpreterBytecodeType_NEW, + AqvmInterpreterBytecodeType_FREE, + AqvmInterpreterBytecodeType_SIZE, + AqvmInterpreterBytecodeType_ADD, + AqvmInterpreterBytecodeType_SUB, + AqvmInterpreterBytecodeType_MUL, + AqvmInterpreterBytecodeType_DIV, + AqvmInterpreterBytecodeType_MOD, + AqvmInterpreterBytecodeType_NEG, + AqvmInterpreterBytecodeType_SHL, + AqvmInterpreterBytecodeType_SHR, + AqvmInterpreterBytecodeType_SAR, + AqvmInterpreterBytecodeType_IF, + AqvmInterpreterBytecodeType_WHILE, + AqvmInterpreterBytecodeType_NOT, + AqvmInterpreterBytecodeType_AND, + AqvmInterpreterBytecodeType_OR, + AqvmInterpreterBytecodeType_XOR, + AqvmInterpreterBytecodeType_CMP, + AqvmInterpreterBytecodeType_INVOKE, + AqvmInterpreterBytecodeType_RETURN, + AqvmInterpreterBytecodeType_GOTO, + AqvmInterpreterBytecodeType_THROW, + AqvmInterpreterBytecodeType_WIDE = 0xFF }; // TODO(Bytecode): Change these functions after completing bytecode development. int AqvmInterpreterBytecode_NOP(); -int AqvmInterpreterBytecode_LDC(); -int AqvmInterpreterBytecode_LOAD(); -int AqvmInterpreterBytecode_STORE(); -int AqvmInterpreterBytecode_NEW(); -int AqvmInterpreterBytecode_FREE(); +int AqvmInterpreterBytecode_LDC(void* value, + AqvmInterpreterRegister_Register* register); +int AqvmInterpreterBytecode_LOAD(AqvmInterpreterRegister_Register* ptr, + AqvmInterpreterRegister_Register* register); +int AqvmInterpreterBytecode_STORE(AqvmInterpreterRegister_Register** ptr, + AqvmInterpreterRegister_Register* register); +int AqvmInterpreterBytecode_NEW(void** ptr, size_t size); +int AqvmInterpreterBytecode_FREE(void* ptr, size_t size); int AqvmInterpreterBytecode_SIZE(); -int AqvmInterpreterBytecode_ADD(); -int AqvmInterpreterBytecode_SUB(); -int AqvmInterpreterBytecode_MUL(); -int AqvmInterpreterBytecode_DIV(); -int AqvmInterpreterBytecode_MOD(); -int AqvmInterpreterBytecode_NEG(); -int AqvmInterpreterBytecode_SHL(); -int AqvmInterpreterBytecode_SHR(); -int AqvmInterpreterBytecode_SAR(); +int AqvmInterpreterBytecode_ADD(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_SUB(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_MUL(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_DIV(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_MOD(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_NEG(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_SHL(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_SHR(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); +int AqvmInterpreterBytecode_SAR(AqvmInterpreterRegister_Register* result, + AqvmInterpreterRegister_Register* opcode1, + AqvmInterpreterRegister_Register* opcode2); int AqvmInterpreterBytecode_IF(); int AqvmInterpreterBytecode_WHILE(); -int AqvmInterpreterBytecode_NOT(); -int AqvmInterpreterBytecode_AND(); -int AqvmInterpreterBytecode_OR(); -int AqvmInterpreterBytecode_XOR(); -int AqvmInterpreterBytecode_CMP(); +int AqvmInterpreterBytecode_NOT(bool result, bool opcode); +int AqvmInterpreterBytecode_AND(bool result, bool opcode1, bool opcode2); +int AqvmInterpreterBytecode_OR(bool result, bool opcode1, bool opcode2); +int AqvmInterpreterBytecode_XOR(bool result, bool opcode1, bool opcode2); +int AqvmInterpreterBytecode_CMP(bool result, int operator, + AqvmInterpreterRegister_Register * opcode1, + AqvmInterpreterRegister_Register* opcode2); int AqvmInterpreterBytecode_INVOKE(); int AqvmInterpreterBytecode_RETURN(); int AqvmInterpreterBytecode_GOTO(); int AqvmInterpreterBytecode_THROW(); -int AqvmInterpreterBytecode_WIDE(); +// TODO(WIDE): Update this function if an extended instruction set is required. +int AqvmInterpreterBytecode_WIDE(); #endif