Skip to content

Commit

Permalink
Partial operator functions for bytecode were developed. Not completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed May 4, 2024
1 parent fdd8023 commit 2f3c0ad
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 79 deletions.
207 changes: 176 additions & 31 deletions aqvm/interpreter/bytecode/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h>

#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() {}
124 changes: 76 additions & 48 deletions aqvm/interpreter/bytecode/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,94 @@
#ifndef AQ_AQVM_INTERPRETER_BYTECODE_BYTECODE_H_
#define AQ_AQVM_INTERPRETER_BYTECODE_BYTECODE_H_

#include <stddef.h>

#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

0 comments on commit 2f3c0ad

Please sign in to comment.