Compare commits
5 Commits
51ef24bb53
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 715a2cd82e | |||
| 3fd45311c5 | |||
| b1b20d336d | |||
| 53ca7e1b49 | |||
| fe6d690251 |
@@ -20,18 +20,6 @@ str ArgumentType_toString(ArgumentType t){
|
|||||||
return _ArgumentType_str[t];
|
return _ArgumentType_str[t];
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterCode RegisterCode_parse(str r){
|
|
||||||
if(str_equals(r, STR("ax")))
|
|
||||||
return RegisterCode_ax;
|
|
||||||
if(str_equals(r, STR("bx")))
|
|
||||||
return RegisterCode_bx;
|
|
||||||
if(str_equals(r, STR("cx")))
|
|
||||||
return RegisterCode_cx;
|
|
||||||
if(str_equals(r, STR("dx")))
|
|
||||||
return RegisterCode_dx;
|
|
||||||
return RegisterCode_Unset;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void Section_init(Section* sec, str name){
|
void Section_init(Section* sec, str name){
|
||||||
sec->name = name;
|
sec->name = name;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
#include "../string/str.h"
|
#include "../string/str.h"
|
||||||
#include "../instructions/instructions.h"
|
#include "../instructions/instructions.h"
|
||||||
|
#include "../instructions/registers.h"
|
||||||
#include "../collections/List.h"
|
#include "../collections/List.h"
|
||||||
|
|
||||||
typedef enum ArgumentType {
|
typedef enum ArgumentType {
|
||||||
@@ -15,16 +16,6 @@ typedef enum ArgumentType {
|
|||||||
|
|
||||||
str ArgumentType_toString(ArgumentType t);
|
str ArgumentType_toString(ArgumentType t);
|
||||||
|
|
||||||
typedef enum RegisterCode {
|
|
||||||
RegisterCode_Unset,
|
|
||||||
RegisterCode_ax,
|
|
||||||
RegisterCode_bx,
|
|
||||||
RegisterCode_cx,
|
|
||||||
RegisterCode_dx
|
|
||||||
} RegisterCode;
|
|
||||||
|
|
||||||
RegisterCode RegisterCode_parse(str register_name);
|
|
||||||
|
|
||||||
typedef struct Argument {
|
typedef struct Argument {
|
||||||
ArgumentType type;
|
ArgumentType type;
|
||||||
union {
|
union {
|
||||||
|
|||||||
40
src/compiler/Binary.c
Normal file
40
src/compiler/Binary.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "Binary.h"
|
||||||
|
|
||||||
|
List_define(ConstDataProps);
|
||||||
|
HashMap_define(ConstDataProps, HashMap_DESTROY_VALUE_FUNC_NULL);
|
||||||
|
List_define(NamedRef);
|
||||||
|
List_define(CompiledSection);
|
||||||
|
HashMap_define(CompiledSectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
|
||||||
|
|
||||||
|
|
||||||
|
void CompiledSection_construct(CompiledSection* ptr, str name){
|
||||||
|
ptr->name = name;
|
||||||
|
ptr->next = NULL;
|
||||||
|
ptr->offset = 0;
|
||||||
|
ptr->const_data_props_list = List_ConstDataProps_construct(NULL, 0, 0);
|
||||||
|
ptr->named_refs = List_NamedRef_construct(NULL, 0, 0);
|
||||||
|
ptr->bytes = List_u8_alloc(64);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CompiledSection_free(CompiledSection* ptr){
|
||||||
|
free(ptr->const_data_props_list.data);
|
||||||
|
free(ptr->named_refs.data);
|
||||||
|
free(ptr->bytes.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BinaryObject_construct(BinaryObject* ptr){
|
||||||
|
ptr->section_list = List_CompiledSection_alloc(64);
|
||||||
|
HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
|
||||||
|
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BinaryObject_free(BinaryObject* ptr){
|
||||||
|
for(u32 i = 0; i < ptr->section_list.len; i++){
|
||||||
|
CompiledSection_free(&ptr->section_list.data[i]);
|
||||||
|
}
|
||||||
|
free(ptr->section_list.data);
|
||||||
|
|
||||||
|
HashMap_CompiledSectionPtr_free(&ptr->section_map);
|
||||||
|
HashMap_ConstDataProps_free(&ptr->const_data_map);
|
||||||
|
}
|
||||||
65
src/compiler/Binary.h
Normal file
65
src/compiler/Binary.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
|
#include "../instructions/instructions.h"
|
||||||
|
#include "../instructions/registers.h"
|
||||||
|
#include "../collections/List.h"
|
||||||
|
#include "../collections/HashMap.h"
|
||||||
|
#include "AST.h"
|
||||||
|
|
||||||
|
typedef struct CompiledSection CompiledSection;
|
||||||
|
typedef struct ConstDataProps {
|
||||||
|
str name;
|
||||||
|
u32 size; // size in bytes
|
||||||
|
u32 offset; // offset in bytes from section start
|
||||||
|
} ConstDataProps;
|
||||||
|
|
||||||
|
#define ConstDataProps_construct(NAME, SIZE, OFFSET) ((ConstDataProps){ .name = NAME, .size = SIZE, .offset = OFFSET})
|
||||||
|
|
||||||
|
List_declare(ConstDataProps);
|
||||||
|
HashMap_declare(ConstDataProps);
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum NamedRefType {
|
||||||
|
NamedRefType_Unset,
|
||||||
|
NamedRefType_Ptr,
|
||||||
|
NamedRefType_Size,
|
||||||
|
} NamedRefType;
|
||||||
|
|
||||||
|
typedef struct NamedRef {
|
||||||
|
str name;
|
||||||
|
NamedRefType type;
|
||||||
|
u32 offset; // offset in bytes from section start
|
||||||
|
} NamedRef;
|
||||||
|
|
||||||
|
#define NamedRef_construct(NAME, TYPE, OFFSET) ((NamedRef){ .name = NAME, .type = TYPE, .offset = OFFSET})
|
||||||
|
|
||||||
|
List_declare(NamedRef);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct CompiledSection {
|
||||||
|
str name;
|
||||||
|
CompiledSection* next;
|
||||||
|
u32 offset;
|
||||||
|
List_ConstDataProps const_data_props_list;
|
||||||
|
List_NamedRef named_refs;
|
||||||
|
List_u8 bytes;
|
||||||
|
} CompiledSection;
|
||||||
|
|
||||||
|
void CompiledSection_construct(CompiledSection* ptr, str name);
|
||||||
|
void CompiledSection_free(CompiledSection* ptr);
|
||||||
|
|
||||||
|
List_declare(CompiledSection);
|
||||||
|
typedef CompiledSection* CompiledSectionPtr;
|
||||||
|
HashMap_declare(CompiledSectionPtr);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct BinaryObject {
|
||||||
|
List_CompiledSection section_list;
|
||||||
|
HashMap_CompiledSectionPtr section_map;
|
||||||
|
HashMap_ConstDataProps const_data_map;
|
||||||
|
u32 total_size;
|
||||||
|
} BinaryObject;
|
||||||
|
|
||||||
|
void BinaryObject_construct(BinaryObject* ptr);
|
||||||
|
void BinaryObject_free(BinaryObject* ptr);
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
#include "Compiler_internal.h"
|
#include "Compiler_internal.h"
|
||||||
|
|
||||||
|
HashMap_define(SectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
|
||||||
|
|
||||||
void Compiler_init(Compiler* cmp){
|
void Compiler_init(Compiler* cmp){
|
||||||
memset(cmp, 0, sizeof(Compiler));
|
memset(cmp, 0, sizeof(Compiler));
|
||||||
cmp->state = CompilerState_Initial;
|
cmp->state = CompilerState_Initial;
|
||||||
cmp->tokens = List_Token_alloc(4096);
|
cmp->tokens = List_Token_alloc(4096);
|
||||||
cmp->line_lengths = List_u32_alloc(1024);
|
cmp->line_lengths = List_u32_alloc(1024);
|
||||||
AST_init(&cmp->ast);
|
AST_init(&cmp->ast);
|
||||||
|
BinaryObject_construct(&cmp->binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Compiler_free(Compiler* cmp){
|
void Compiler_free(Compiler* cmp){
|
||||||
@@ -13,6 +16,7 @@ void Compiler_free(Compiler* cmp){
|
|||||||
free(cmp->tokens.data);
|
free(cmp->tokens.data);
|
||||||
free(cmp->line_lengths.data);
|
free(cmp->line_lengths.data);
|
||||||
AST_free(&cmp->ast);
|
AST_free(&cmp->ast);
|
||||||
|
BinaryObject_free(&cmp->binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
||||||
@@ -60,10 +64,156 @@ str Compiler_constructTokenStr(Compiler* cmp, Token t){
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool compileFile(Compiler* cmp, FILE* f){
|
static bool compileSection(Compiler* cmp, Section* sec){
|
||||||
|
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
|
||||||
|
CompiledSection_construct(cs, sec->name);
|
||||||
|
if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){
|
||||||
|
returnError("duplicate section '%s'", str_copy(sec->name));
|
||||||
|
}
|
||||||
|
|
||||||
|
// compile code
|
||||||
|
u8 zeroes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
|
for(u32 i = 0; i < sec->code.len; i++){
|
||||||
|
Operation* op = &sec->code.data[i];
|
||||||
|
List_u8_pushMany(&cs->bytes, (void*)&op->opcode, sizeof(op->opcode));
|
||||||
|
for(u32 j = 0; j < op->args.len; j++){
|
||||||
|
Argument* arg = &op->args.data[j];
|
||||||
|
switch(arg->type){
|
||||||
|
case ArgumentType_VarDataName:
|
||||||
|
returnError("argument type 'VarDataName' is not supported yet");
|
||||||
|
case ArgumentType_Unset:
|
||||||
|
returnError("ArgumentType is not set");
|
||||||
|
default:
|
||||||
|
returnError("invalid ArgumentType %i", arg->type);
|
||||||
|
|
||||||
|
case ArgumentType_Register:
|
||||||
|
List_u8_push(&cs->bytes, arg->value.register_code);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstValue:
|
||||||
|
//TODO: add const value size parsing
|
||||||
|
List_u8_pushMany(&cs->bytes, (void*)&arg->value.i, 4);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstDataPointer:
|
||||||
|
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
||||||
|
arg->value.data_name,
|
||||||
|
NamedRefType_Ptr,
|
||||||
|
cs->bytes.len));
|
||||||
|
List_u8_pushMany(&cs->bytes, zeroes, 4);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstDataSize:
|
||||||
|
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
||||||
|
arg->value.data_name,
|
||||||
|
NamedRefType_Size,
|
||||||
|
cs->bytes.len));
|
||||||
|
List_u8_pushMany(&cs->bytes, zeroes, 4);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// compile data
|
||||||
|
for(u32 i = 0; i < sec->data.len; i++){
|
||||||
|
DataDefinition* dd = &sec->data.data[i];
|
||||||
|
List_ConstDataProps_push(&cs->const_data_props_list, ConstDataProps_construct(dd->name, dd->data.len, cs->bytes.len));
|
||||||
|
List_u8_pushMany(&cs->bytes, dd->data.data, dd->data.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: push padding
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool compileBinary(Compiler* cmp){
|
||||||
|
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||||
|
SectionPtr sec = &cmp->ast.sections.data[i];
|
||||||
|
if(!compileSection(cmp, sec)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find main section
|
||||||
|
str main_sec_name = STR("main");
|
||||||
|
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, main_sec_name);
|
||||||
|
if(main_sec_ptrptr == NULL){
|
||||||
|
returnError("no 'main' section was defined");
|
||||||
|
}
|
||||||
|
|
||||||
|
// create linked list of CompiledSection where main is the first
|
||||||
|
CompiledSection* prev_sec = *main_sec_ptrptr;
|
||||||
|
u32 total_size = 0;
|
||||||
|
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
||||||
|
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
||||||
|
total_size += sec->bytes.len;
|
||||||
|
if(str_equals(sec->name, main_sec_name))
|
||||||
|
continue;
|
||||||
|
prev_sec->next = sec;
|
||||||
|
sec->offset = prev_sec->offset + prev_sec->bytes.len;
|
||||||
|
|
||||||
|
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
|
||||||
|
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
|
||||||
|
returnError("duplicate named data '%s'", str_copy(cd.name).data);
|
||||||
|
}
|
||||||
|
for(u32 j = 0; j < sec->const_data_props_list.len; j++){
|
||||||
|
cd = sec->const_data_props_list.data[j];
|
||||||
|
cd.offset += sec->offset;
|
||||||
|
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
|
||||||
|
returnError("duplicate named data '%s'", str_copy(cd.name).data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert calculated offsets into sections
|
||||||
|
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
||||||
|
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
||||||
|
for(u32 j = 0; j < sec->named_refs.len; j++){
|
||||||
|
NamedRef* ref = &sec->named_refs.data[j];
|
||||||
|
|
||||||
|
ConstDataProps* target_data = HashMap_ConstDataProps_tryGetPtr(
|
||||||
|
&cmp->binary.const_data_map, ref->name);
|
||||||
|
if(target_data == NULL){
|
||||||
|
returnError("can't find named data '%s'", str_copy(ref->name).data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* ref_value_ptr = sec->bytes.data + ref->offset;
|
||||||
|
|
||||||
|
switch(ref->type){
|
||||||
|
default:
|
||||||
|
returnError("invalid NamedRefType %i", ref->type);
|
||||||
|
case NamedRefType_Size:
|
||||||
|
*((u32*)ref_value_ptr) = target_data->size;
|
||||||
|
break;
|
||||||
|
case NamedRefType_Ptr:
|
||||||
|
*((u32*)ref_value_ptr) = target_data->offset;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->binary.total_size = total_size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool writeBinaryFile(Compiler* cmp, FILE* f){
|
||||||
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
||||||
cmp->state = CompilerState_Compiling;
|
cmp->state = CompilerState_Compiling;
|
||||||
|
|
||||||
|
if(!compileBinary(cmp)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, STR("main"));
|
||||||
|
if(main_sec_ptrptr == NULL){
|
||||||
|
returnError("no 'main' section was defined");
|
||||||
|
}
|
||||||
|
CompiledSection* sec = *main_sec_ptrptr;
|
||||||
|
while(sec){
|
||||||
|
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
|
||||||
|
sec = sec->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: print warnings for unused sections
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +289,65 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
if(debug_log)
|
if(debug_log)
|
||||||
printf("===================================[parsing]===================================\n");
|
printf("===================================[parsing]===================================\n");
|
||||||
success = Compiler_parse(cmp);
|
success = Compiler_parse(cmp);
|
||||||
|
if (debug_log){
|
||||||
|
printf("-------------------------------------[AST]-------------------------------------\n");
|
||||||
|
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||||
|
Section* sec = &cmp->ast.sections.data[i];
|
||||||
|
str tmpstr = str_copy(sec->name);
|
||||||
|
printf("section '%s'\n", tmpstr.data);
|
||||||
|
free(tmpstr.data);
|
||||||
|
|
||||||
|
for(u32 j = 0; j < sec->data.len; j++){
|
||||||
|
DataDefinition* dd = &sec->data.data[j];
|
||||||
|
tmpstr = str_copy(dd->name);
|
||||||
|
printf(" const%u %s (len %u)\n", dd->element_size * 8, tmpstr.data, dd->data.len/dd->element_size);
|
||||||
|
free(tmpstr.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for(u32 j = 0; j < sec->code.len; j++){
|
||||||
|
Operation* op = &sec->code.data[j];
|
||||||
|
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
||||||
|
printf(" %s", instr->name.data);
|
||||||
|
for(u32 k = 0; k < op->args.len; k++){
|
||||||
|
Argument* arg = &op->args.data[k];
|
||||||
|
printf(" %s(", ArgumentType_toString(arg->type).data);
|
||||||
|
|
||||||
|
switch(arg->type){
|
||||||
|
default:
|
||||||
|
fclose(f);
|
||||||
|
returnError("invalid argument type %i", arg->type);
|
||||||
|
case ArgumentType_Register:
|
||||||
|
const char* register_names[] = {"null", "ax", "bx", "cx", "dx"};
|
||||||
|
printf("%s", register_names[arg->value.register_code]);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstValue:
|
||||||
|
printf(IFWIN("%lli", "%li"), arg->value.i);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstDataPointer:
|
||||||
|
tmpstr = str_copy(arg->value.data_name);
|
||||||
|
printf("@%s", tmpstr.data);
|
||||||
|
free(tmpstr.data);
|
||||||
|
break;
|
||||||
|
case ArgumentType_ConstDataSize:
|
||||||
|
tmpstr = str_copy(arg->value.data_name);
|
||||||
|
printf("#%s", tmpstr.data);
|
||||||
|
free(tmpstr.data);
|
||||||
|
break;
|
||||||
|
case ArgumentType_VarDataName:
|
||||||
|
tmpstr = str_copy(arg->value.data_name);
|
||||||
|
printf("%s", tmpstr.data);
|
||||||
|
free(tmpstr.data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if(!success){
|
if(!success){
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return false;
|
return false;
|
||||||
@@ -146,7 +355,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
|
|
||||||
if(debug_log)
|
if(debug_log)
|
||||||
printf("==================================[compiling]==================================\n");
|
printf("==================================[compiling]==================================\n");
|
||||||
success = compileFile(cmp, f);
|
success = writeBinaryFile(cmp, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if(success){
|
if(success){
|
||||||
cmp->state = CompilerState_Success;
|
cmp->state = CompilerState_Success;
|
||||||
|
|||||||
@@ -2,8 +2,9 @@
|
|||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
#include "../string/str.h"
|
#include "../string/str.h"
|
||||||
#include "../collections/List.h"
|
#include "../collections/List.h"
|
||||||
|
#include "../collections/HashMap.h"
|
||||||
#include "Token.h"
|
#include "Token.h"
|
||||||
#include "AST.h"
|
#include "Binary.h"
|
||||||
|
|
||||||
typedef enum CompilerState {
|
typedef enum CompilerState {
|
||||||
CompilerState_Initial,
|
CompilerState_Initial,
|
||||||
@@ -14,16 +15,24 @@ typedef enum CompilerState {
|
|||||||
CompilerState_Success
|
CompilerState_Success
|
||||||
} CompilerState;
|
} CompilerState;
|
||||||
|
|
||||||
|
typedef Section* SectionPtr;
|
||||||
|
HashMap_declare(SectionPtr);
|
||||||
|
|
||||||
typedef struct Compiler {
|
typedef struct Compiler {
|
||||||
|
/* general fields */
|
||||||
str code;
|
str code;
|
||||||
u32 column; // > 0 if code parsing started
|
u32 column; // > 0 if code parsing started
|
||||||
u32 pos;
|
u32 pos;
|
||||||
CompilerState state;
|
CompilerState state;
|
||||||
NULLABLE(char* error_message);
|
NULLABLE(char* error_message);
|
||||||
|
/* lexer fields */
|
||||||
List_Token tokens;
|
List_Token tokens;
|
||||||
List_u32 line_lengths;
|
List_u32 line_lengths;
|
||||||
|
/* parser fields */
|
||||||
AST ast;
|
AST ast;
|
||||||
u32 tok_i;
|
u32 tok_i;
|
||||||
|
/* compiler fields */
|
||||||
|
BinaryObject binary;
|
||||||
} Compiler;
|
} Compiler;
|
||||||
|
|
||||||
void Compiler_init(Compiler* cmp);
|
void Compiler_init(Compiler* cmp);
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
i32 PUSH_impl(VM* vm){
|
i32 PUSH_impl(VM* vm){
|
||||||
u8 dst_register_i = 0;
|
u8 dst_register_i = 0;
|
||||||
readRegisterVar(dst_register_i);
|
readRegisterVar(dst_register_i);
|
||||||
u8 value_size = 0;
|
/*u8 value_size = 0;
|
||||||
readValueSizeVar(value_size);
|
readValueSizeVar(value_size);*/
|
||||||
|
u8 value_size = 4;\
|
||||||
|
|
||||||
vm->registers[dst_register_i].u32v = 0;
|
vm->registers[dst_register_i].u32v = 0;
|
||||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../instructions.h"
|
#include "../instructions.h"
|
||||||
|
#include "../registers.h"
|
||||||
|
|
||||||
#define readVar(VAR) {\
|
#define readVar(VAR) {\
|
||||||
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
||||||
@@ -16,9 +17,11 @@
|
|||||||
|
|
||||||
#define readRegisterVar(VAR) {\
|
#define readRegisterVar(VAR) {\
|
||||||
readVar(VAR);\
|
readVar(VAR);\
|
||||||
|
VAR -= 1;\
|
||||||
validateRegisterIndex(VAR);\
|
validateRegisterIndex(VAR);\
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#define validateValueSize(VAR) {\
|
#define validateValueSize(VAR) {\
|
||||||
if(VAR < 1 || VAR > 4){\
|
if(VAR < 1 || VAR > 4){\
|
||||||
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
||||||
@@ -30,3 +33,4 @@
|
|||||||
readVar(VAR);\
|
readVar(VAR);\
|
||||||
validateValueSize(VAR);\
|
validateValueSize(VAR);\
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
u8 dst_register_i = 0, src_register_i = 0;\
|
u8 dst_register_i = 0, src_register_i = 0;\
|
||||||
readRegisterVar(dst_register_i);\
|
readRegisterVar(dst_register_i);\
|
||||||
readRegisterVar(src_register_i);\
|
readRegisterVar(src_register_i);\
|
||||||
u8 value_size = 0;\
|
/*u8 value_size = 0;\
|
||||||
readValueSizeVar(value_size);\
|
readValueSizeVar(value_size);*/\
|
||||||
|
u8 value_size = 4;\
|
||||||
\
|
\
|
||||||
switch(value_size){\
|
switch(value_size){\
|
||||||
case 1: \
|
case 1: \
|
||||||
|
|||||||
13
src/instructions/registers.c
Normal file
13
src/instructions/registers.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "registers.h"
|
||||||
|
|
||||||
|
RegisterCode RegisterCode_parse(str r){
|
||||||
|
if(str_equals(r, STR("ax")))
|
||||||
|
return RegisterCode_ax;
|
||||||
|
if(str_equals(r, STR("bx")))
|
||||||
|
return RegisterCode_bx;
|
||||||
|
if(str_equals(r, STR("cx")))
|
||||||
|
return RegisterCode_cx;
|
||||||
|
if(str_equals(r, STR("dx")))
|
||||||
|
return RegisterCode_dx;
|
||||||
|
return RegisterCode_Unset;
|
||||||
|
}
|
||||||
13
src/instructions/registers.h
Normal file
13
src/instructions/registers.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
|
|
||||||
|
typedef enum RegisterCode {
|
||||||
|
RegisterCode_Unset,
|
||||||
|
RegisterCode_ax,
|
||||||
|
RegisterCode_bx,
|
||||||
|
RegisterCode_cx,
|
||||||
|
RegisterCode_dx
|
||||||
|
} RegisterCode;
|
||||||
|
|
||||||
|
RegisterCode RegisterCode_parse(str register_name);
|
||||||
Reference in New Issue
Block a user