Compare commits
11 Commits
823223ffa7
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 715a2cd82e | |||
| 3fd45311c5 | |||
| b1b20d336d | |||
| 53ca7e1b49 | |||
| fe6d690251 | |||
| 51ef24bb53 | |||
| 422d967165 | |||
| 5d275c8dd1 | |||
| 2faff91981 | |||
| b443367f46 | |||
| fd9e5dda78 |
@@ -77,7 +77,9 @@ i32 VM_boot(VM* vm){
|
||||
|
||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||
if(pos + size >= vm->data_size){
|
||||
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable",
|
||||
VM_setError(vm,
|
||||
"can't read " IFWIN("%lli", "%li") " bytes from 0x%x, because only "
|
||||
IFWIN("%lli", "%li") " are avaliable",
|
||||
size, (u32)pos, vm->data_size - size);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
|
||||
#define Array_construct(T, DATA, LEN) ((Array_##T){ .data = DATA, .len = LEN })
|
||||
|
||||
/// creates Array_##T from a const array
|
||||
#define ARRAY(T, A...) Array_construct(T, ((T[])A), ARRAY_SIZE(((T[])A)))
|
||||
|
||||
#define Array_declare(T)\
|
||||
typedef struct Array_##T {\
|
||||
T* data;\
|
||||
u32 len;\
|
||||
} Array_##T;\
|
||||
\
|
||||
static inline Array_##T Array_##T##_construct(T* data_ptr, u32 len) {\
|
||||
return (Array_##T){ .data = data_ptr, .len = len };\
|
||||
}\
|
||||
\
|
||||
static inline Array_##T Array_##T##_alloc(u32 len){\
|
||||
return Array_##T##_construct((T*)malloc(len * sizeof(T)), len);\
|
||||
}\
|
||||
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
|
||||
ptr->data = (T*)realloc(ptr->data, new_len * sizeof(T));\
|
||||
ptr->len = new_len;\
|
||||
}
|
||||
typedef struct Array_##T {\
|
||||
T* data;\
|
||||
u32 len;\
|
||||
} Array_##T;\
|
||||
\
|
||||
static inline Array_##T Array_##T##_alloc(u32 len){\
|
||||
return Array_construct(T, (T*)malloc(len * sizeof(T)), len);\
|
||||
}\
|
||||
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
|
||||
ptr->data = (T*)realloc(ptr->data, new_len * sizeof(T));\
|
||||
ptr->len = new_len;\
|
||||
}
|
||||
|
||||
Array_declare(u8)
|
||||
Array_declare(u32)
|
||||
|
||||
140
src/collections/HashMap.h
Normal file
140
src/collections/HashMap.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#pragma once
|
||||
#include "../std.h"
|
||||
#include "../string/str.h"
|
||||
#include "Array.h"
|
||||
#include "List.h"
|
||||
|
||||
//TODO: sorting of bucket and binary search
|
||||
//TODO: delayed deletion
|
||||
|
||||
#define __HashMap_HASH_FUNC str_hash32
|
||||
#define __HashMapBucket_MAX_LEN 16
|
||||
|
||||
#define HashMap_DESTROY_VALUE_FUNC_NULL ((void (*)(void*))NULL)
|
||||
|
||||
/// call this in a header file
|
||||
///@param T Value type
|
||||
#define HashMap_declare(T)\
|
||||
typedef struct KeyValue_##T {\
|
||||
str key;\
|
||||
T value;\
|
||||
u32 hash;\
|
||||
} KeyValue_##T;\
|
||||
\
|
||||
List_declare(KeyValue_##T);\
|
||||
\
|
||||
typedef struct HashMapBucket_##T {\
|
||||
List_KeyValue_##T kvs;\
|
||||
} HashMapBucket_##T;\
|
||||
\
|
||||
typedef struct HashMap_##T {\
|
||||
HashMapBucket_##T* table;\
|
||||
u32 height;\
|
||||
u16 height_n;\
|
||||
} HashMap_##T;\
|
||||
\
|
||||
void HashMap_##T##_alloc(HashMap_##T* ptr);\
|
||||
void HashMap_##T##_free(HashMap_##T* ptr);\
|
||||
T* NULLABLE(HashMap_##T##_tryGetPtr)(HashMap_##T* ptr, str key);\
|
||||
bool HashMap_##T##_tryPush(HashMap_##T* ptr, str key, T value);\
|
||||
bool HashMap_##T##_tryDelete(HashMap_##T* ptr, str key);\
|
||||
|
||||
|
||||
|
||||
/// call this in a source code file
|
||||
///@param T Value type
|
||||
///@param DESTROY_VALUE_FUNC `void foo (T*)` or HashMap_DESTROY_VALUE_FUNC_NULL
|
||||
#define HashMap_define(T, DESTROY_VALUE_FUNC)\
|
||||
List_define(KeyValue_##T);\
|
||||
\
|
||||
static const Array_u32 __HashMap_##T##_heights = ARRAY(u32, {\
|
||||
17, 31, 61, 127, 257, 521, 1021, 2053, 4099, 8191, 16381, 32771,\
|
||||
65521, 131071, 262147, 524287, 1048583, 2097169, 4194319,\
|
||||
8388617, 16777213, 33554467, 67108859, 134217757, 268435493\
|
||||
});\
|
||||
\
|
||||
void HashMap_##T##_alloc(HashMap_##T* ptr){\
|
||||
ptr->height_n = 0;\
|
||||
ptr->height = __HashMap_##T##_heights.data[0];\
|
||||
ptr->table = (HashMapBucket_##T*)malloc(ptr->height * sizeof(HashMapBucket_##T));\
|
||||
memset(ptr->table, 0, ptr->height * sizeof(HashMapBucket_##T));\
|
||||
}\
|
||||
\
|
||||
void HashMap_##T##_free(HashMap_##T* ptr){\
|
||||
for(u32 i = 0; i < ptr->height; i++){\
|
||||
for(u32 j = 0; j < ptr->table[i].kvs.len; j++){\
|
||||
KeyValue_##T* kv_ptr = &ptr->table[i].kvs.data[j];\
|
||||
if(DESTROY_VALUE_FUNC){\
|
||||
DESTROY_VALUE_FUNC(&kv_ptr->value);\
|
||||
}\
|
||||
free(kv_ptr->key.data);\
|
||||
}\
|
||||
\
|
||||
free(ptr->table[i].kvs.data);\
|
||||
}\
|
||||
\
|
||||
free(ptr->table);\
|
||||
}\
|
||||
\
|
||||
T* NULLABLE(HashMap_##T##_tryGetPtr)(HashMap_##T* ptr, str key){\
|
||||
u32 hash = __HashMap_HASH_FUNC(key);\
|
||||
HashMapBucket_##T* bu = &ptr->table[hash % ptr->height];\
|
||||
for(u32 i = 0; i < bu->kvs.len; i++){\
|
||||
if(bu->kvs.data[i].hash == hash && str_equals(bu->kvs.data[i].key, key)){\
|
||||
return &bu->kvs.data[i].value;\
|
||||
}\
|
||||
}\
|
||||
\
|
||||
return NULL;\
|
||||
}\
|
||||
\
|
||||
bool HashMap_##T##_tryPush(HashMap_##T* ptr, str key, T value){\
|
||||
u32 hash = __HashMap_HASH_FUNC(key);\
|
||||
HashMapBucket_##T* bu = &ptr->table[hash % ptr->height];\
|
||||
for(u32 i = 0; i < bu->kvs.len; i++){\
|
||||
if(bu->kvs.data[i].hash == hash && str_equals(bu->kvs.data[i].key, key)){\
|
||||
return false;\
|
||||
}\
|
||||
}\
|
||||
\
|
||||
if(bu->kvs.len >= __HashMapBucket_MAX_LEN){\
|
||||
u32 height_expanded_n = ptr->height_n + 1;\
|
||||
if(height_expanded_n >= __HashMap_##T##_heights.len){\
|
||||
printf("ERROR: HashMap_" #T " IS FULL\n");\
|
||||
return false;\
|
||||
}\
|
||||
\
|
||||
u32 height_expanded = __HashMap_##T##_heights.data[height_expanded_n];\
|
||||
HashMapBucket_##T* table_expanded = (HashMapBucket_##T*)malloc(height_expanded * sizeof(HashMapBucket_##T));\
|
||||
memset(table_expanded, 0, height_expanded * sizeof(HashMapBucket_##T));\
|
||||
for(u32 i = 0; i < height_expanded; i++){\
|
||||
for(u32 j = 0; j < ptr->table[i].kvs.len; j++){\
|
||||
KeyValue_##T kv = ptr->table[i].kvs.data[j];\
|
||||
List_KeyValue_##T##_push(&table_expanded[kv.hash % height_expanded].kvs, kv);\
|
||||
}\
|
||||
\
|
||||
free(ptr->table[i].kvs.data);\
|
||||
}\
|
||||
free(ptr->table);\
|
||||
ptr->table = table_expanded;\
|
||||
ptr->height = height_expanded;\
|
||||
ptr->height_n = height_expanded_n;\
|
||||
bu = &ptr->table[hash % ptr->height];\
|
||||
}\
|
||||
\
|
||||
KeyValue_##T kv = { .key = str_copy(key), .value = value, .hash = hash };\
|
||||
List_KeyValue_##T##_push(&bu->kvs, kv);\
|
||||
return true;\
|
||||
}\
|
||||
\
|
||||
bool HashMap_##T##_tryDelete(HashMap_##T* ptr, str key){\
|
||||
u32 hash = __HashMap_HASH_FUNC(key);\
|
||||
HashMapBucket_##T* bu = &ptr->table[hash % ptr->height];\
|
||||
for(u32 i = 0; i < bu->kvs.len; i++){\
|
||||
if(bu->kvs.data[i].hash == hash && str_equals(bu->kvs.data[i].key, key)){\
|
||||
return List_KeyValue_##T##_tryRemoveAt(&bu->kvs, i);\
|
||||
}\
|
||||
}\
|
||||
\
|
||||
return false;\
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
T* List_##T##_expand(List_##T* ptr, u32 count);\
|
||||
void List_##T##_push(List_##T* ptr, T value);\
|
||||
void List_##T##_pushMany(List_##T* ptr, T* values, u32 count);\
|
||||
bool List_##T##_tryRemoveAt(List_##T* ptr, u32 i);\
|
||||
|
||||
|
||||
#define List_define(T)\
|
||||
@@ -29,17 +30,22 @@
|
||||
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
|
||||
/* branchless version of max(max_len, __List_min_size) */\
|
||||
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
|
||||
return List_##T##_construct((T*)malloc(initial_len * sizeof(T)), 0, max_len);\
|
||||
return List_##T##_construct((T*)malloc(max_len * sizeof(T)), 0, max_len);\
|
||||
}\
|
||||
\
|
||||
T* List_##T##_expand(List_##T* ptr, u32 count){\
|
||||
u32 occupied_len = ptr->len;\
|
||||
u32 expanded_max_len = ptr->max_len;\
|
||||
expanded_max_len += (expanded_max_len < __List_min_size) * (__List_min_size - expanded_max_len);\
|
||||
ptr->len += count;\
|
||||
while(ptr->len > ptr->max_len){\
|
||||
while(ptr->len > expanded_max_len){\
|
||||
expanded_max_len *= 2;\
|
||||
}\
|
||||
ptr->data = (T*)realloc(ptr->data, expanded_max_len * sizeof(T));\
|
||||
u32 alloc_size = expanded_max_len * sizeof(T);\
|
||||
if(ptr->data == NULL)\
|
||||
ptr->data = (T*)malloc(alloc_size);\
|
||||
else ptr->data = (T*)realloc(ptr->data, alloc_size);\
|
||||
ptr->max_len = expanded_max_len;\
|
||||
return ptr->data + occupied_len;\
|
||||
}\
|
||||
\
|
||||
@@ -52,6 +58,17 @@
|
||||
T* empty_cell_ptr = List_##T##_expand(ptr, count);\
|
||||
memcpy(empty_cell_ptr, values, count * sizeof(T));\
|
||||
}\
|
||||
\
|
||||
bool List_##T##_tryRemoveAt(List_##T* ptr, u32 i){\
|
||||
if(ptr->len == 0 || i >= ptr->len)\
|
||||
return false;\
|
||||
\
|
||||
ptr->len--;\
|
||||
for(; i < ptr->len; i++){\
|
||||
ptr->data[i] = ptr->data[i + 1];\
|
||||
}\
|
||||
return true;\
|
||||
}\
|
||||
|
||||
|
||||
List_declare(u32);
|
||||
|
||||
@@ -9,14 +9,14 @@ static str _ArgumentType_str[] = {
|
||||
STR("Unset"),
|
||||
STR("Register"),
|
||||
STR("ConstValue"),
|
||||
STR("DataName"),
|
||||
STR("NamedDataPointer"),
|
||||
STR("NamedDataSize"),
|
||||
STR("VarDataName"),
|
||||
STR("ConstDataPointer"),
|
||||
STR("ConstDataSize"),
|
||||
};
|
||||
|
||||
str ArgumentType_toString(ArgumentType t){
|
||||
if(t >= ARRAY_SIZE(_ArgumentType_str))
|
||||
return STR("!!INDEX_ERROR!!");
|
||||
return STR("!!ArgumentType INDEX_ERROR!!");
|
||||
return _ArgumentType_str[t];
|
||||
}
|
||||
|
||||
@@ -28,8 +28,14 @@ void Section_init(Section* sec, str name){
|
||||
}
|
||||
|
||||
void Section_free(Section* sec){
|
||||
free(sec->name.data);
|
||||
for(u32 i = 0; i < sec->data.len; i++){
|
||||
free(sec->data.data[i].data.data);
|
||||
}
|
||||
free(sec->data.data);
|
||||
|
||||
for(u32 i = 0; i < sec->code.len; i++){
|
||||
free(sec->code.data[i].args.data);
|
||||
}
|
||||
free(sec->code.data);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,23 +2,28 @@
|
||||
#include "../std.h"
|
||||
#include "../string/str.h"
|
||||
#include "../instructions/instructions.h"
|
||||
#include "../instructions/registers.h"
|
||||
#include "../collections/List.h"
|
||||
|
||||
typedef enum ArgumentType {
|
||||
ArgumentType_Unset,
|
||||
ArgumentType_Register,
|
||||
ArgumentType_ConstValue,
|
||||
ArgumentType_DataName,
|
||||
ArgumentType_NamedDataPointer,
|
||||
ArgumentType_NamedDataSize,
|
||||
ArgumentType_VarDataName,
|
||||
ArgumentType_ConstDataPointer,
|
||||
ArgumentType_ConstDataSize,
|
||||
} ArgumentType;
|
||||
|
||||
str ArgumentType_toString(ArgumentType t);
|
||||
|
||||
|
||||
typedef struct Argument {
|
||||
ArgumentType type;
|
||||
u32 value;
|
||||
union {
|
||||
i64 i;
|
||||
f64 f;
|
||||
str data_name;
|
||||
RegisterCode register_code;
|
||||
} value;
|
||||
} Argument;
|
||||
|
||||
List_declare(Argument);
|
||||
@@ -26,7 +31,7 @@ List_declare(Argument);
|
||||
|
||||
typedef struct Operation {
|
||||
List_Argument args;
|
||||
Opcode op;
|
||||
Opcode opcode;
|
||||
} Operation;
|
||||
|
||||
List_declare(Operation);
|
||||
|
||||
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"
|
||||
|
||||
HashMap_define(SectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
|
||||
|
||||
void Compiler_init(Compiler* cmp){
|
||||
memset(cmp, 0, sizeof(Compiler));
|
||||
cmp->state = CompilerState_Initial;
|
||||
cmp->tokens = List_Token_alloc(4096);
|
||||
cmp->line_lengths = List_u32_alloc(1024);
|
||||
AST_init(&cmp->ast);
|
||||
BinaryObject_construct(&cmp->binary);
|
||||
}
|
||||
|
||||
void Compiler_free(Compiler* cmp){
|
||||
@@ -13,6 +16,7 @@ void Compiler_free(Compiler* cmp){
|
||||
free(cmp->tokens.data);
|
||||
free(cmp->line_lengths.data);
|
||||
AST_free(&cmp->ast);
|
||||
BinaryObject_free(&cmp->binary);
|
||||
}
|
||||
|
||||
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
||||
@@ -60,10 +64,156 @@ str Compiler_constructTokenStr(Compiler* cmp, Token t){
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -98,17 +248,21 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
}
|
||||
|
||||
if(debug_log){
|
||||
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
|
||||
printf("===========================[%s]===========================\n", source_file_name);
|
||||
fputs(cmp->code.data, stdout);
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
|
||||
if(debug_log)
|
||||
printf("===================================[lexing]===================================\n");
|
||||
bool success = Compiler_lex(cmp);
|
||||
|
||||
if(debug_log){
|
||||
printf("------------------------------------[lines]-----------------------------------\n");
|
||||
printf("------------------------------------[lines]------------------------------------\n");
|
||||
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
||||
}
|
||||
|
||||
printf("------------------------------------[tokens]-----------------------------------\n");
|
||||
for(u32 i = 0; i < cmp->tokens.len; i++){
|
||||
Token t = cmp->tokens.data[i];
|
||||
@@ -116,24 +270,92 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
char* tokstr = malloc(4096);
|
||||
strncpy(tokstr, cmp->code.data + t.begin, t.length);
|
||||
tokstr[t.length] = 0;
|
||||
char* tokstr_stripped = tokstr;
|
||||
while(*tokstr_stripped == '\r' || *tokstr_stripped == '\n'){
|
||||
tokstr_stripped++;
|
||||
}
|
||||
printf("[l:%3u, c:%3u] %s '%s'\n",
|
||||
pos.line, pos.column,
|
||||
TokenType_toString(t.type).data, tokstr);
|
||||
TokenType_toString(t.type).data, tokstr_stripped);
|
||||
free(tokstr);
|
||||
}
|
||||
}
|
||||
|
||||
if(!success){
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(debug_log)
|
||||
printf("===================================[parsing]===================================\n");
|
||||
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){
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
success = Compiler_parse(cmp);
|
||||
if(!success){
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
success = compileFile(cmp, f);
|
||||
if(debug_log)
|
||||
printf("==================================[compiling]==================================\n");
|
||||
success = writeBinaryFile(cmp, f);
|
||||
fclose(f);
|
||||
if(success){
|
||||
cmp->state = CompilerState_Success;
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
#include "../std.h"
|
||||
#include "../string/str.h"
|
||||
#include "../collections/List.h"
|
||||
#include "../collections/HashMap.h"
|
||||
#include "Token.h"
|
||||
#include "AST.h"
|
||||
#include "Binary.h"
|
||||
|
||||
typedef enum CompilerState {
|
||||
CompilerState_Initial,
|
||||
@@ -14,16 +15,24 @@ typedef enum CompilerState {
|
||||
CompilerState_Success
|
||||
} CompilerState;
|
||||
|
||||
typedef Section* SectionPtr;
|
||||
HashMap_declare(SectionPtr);
|
||||
|
||||
typedef struct Compiler {
|
||||
/* general fields */
|
||||
str code;
|
||||
u32 column; // > 0 if code parsing started
|
||||
u32 pos;
|
||||
CompilerState state;
|
||||
NULLABLE(char* error_message);
|
||||
/* lexer fields */
|
||||
List_Token tokens;
|
||||
List_u32 line_lengths;
|
||||
/* parser fields */
|
||||
AST ast;
|
||||
u32 tok_i;
|
||||
/* compiler fields */
|
||||
BinaryObject binary;
|
||||
} Compiler;
|
||||
|
||||
void Compiler_init(Compiler* cmp);
|
||||
|
||||
@@ -17,6 +17,12 @@
|
||||
Compiler_setError(cmp, "unexpected token '%c'", cmp->code.data[cmp->pos]);\
|
||||
}
|
||||
|
||||
#define setError_unexpectedInstruction(T) {\
|
||||
str tok_str = str_copy(Compiler_constructTokenStr(cmp, T));\
|
||||
cmp->pos = T.begin;\
|
||||
Compiler_setError(cmp, "unexpected instruction '%s'", tok_str.data);\
|
||||
free(tok_str.data);\
|
||||
}
|
||||
|
||||
#define Error_TokenUnset "token of undefined type"
|
||||
#define Error_BitSize "invalid size in bits"
|
||||
@@ -28,7 +34,7 @@ static void List_u8_pushBytes(List_u8* l, void* value, u32 startIndex, u32 count
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool isVarSizeBits(u32 B) { return (B == 8 && B == 16 && B == 32 && B == 64); }
|
||||
static inline bool isVarSizeBits(u32 B) { return (B == 8 || B == 16 || B == 32 || B == 64); }
|
||||
|
||||
static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
||||
StringBuilder sb = StringBuilder_alloc(src.len);
|
||||
@@ -83,30 +89,44 @@ static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* d
|
||||
}
|
||||
free(_instr_name_zero_terminated.data);
|
||||
ddf->element_size = _element_size_bits / 8;
|
||||
ddf->data = List_u8_alloc(32);
|
||||
|
||||
Token tok = cmp->tokens.data[++cmp->tok_i];
|
||||
if(tok.type != TokenType_Name){
|
||||
setError_unexpectedToken(tok);
|
||||
return;
|
||||
}
|
||||
|
||||
str tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
str processed_str = str_null;
|
||||
ddf->name = tok_str;
|
||||
|
||||
while(++cmp->tok_i < cmp->tokens.len){
|
||||
tok = cmp->tokens.data[cmp->tok_i];
|
||||
switch(tok.type){
|
||||
case TokenType_Unset:
|
||||
setError(Error_TokenUnset);
|
||||
return;
|
||||
case TokenType_SingleLineComment:
|
||||
case TokenType_MultiLineComment:
|
||||
// skip comments
|
||||
break;
|
||||
|
||||
case TokenType_OperationEnd:
|
||||
return;
|
||||
case TokenType_Unset:
|
||||
setError(Error_TokenUnset);
|
||||
return;
|
||||
default:
|
||||
setError_unexpectedToken(tok);
|
||||
return;
|
||||
|
||||
case TokenType_Number:
|
||||
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
processed_str = str_copy(tok_str);
|
||||
if(str_seekChar(tok_str, '.', 0) != -1){
|
||||
f64 f = atof(tok_str.data);
|
||||
f64 f = atof(processed_str.data);
|
||||
List_u8_pushBytes(&ddf->data, &f, 8 - ddf->element_size, ddf->element_size);
|
||||
}
|
||||
else {
|
||||
i64 i = atoll(tok_str.data);
|
||||
i64 i = atoll(processed_str.data);
|
||||
List_u8_pushBytes(&ddf->data, &i, 8 - ddf->element_size, ddf->element_size);
|
||||
}
|
||||
free(processed_str.data);
|
||||
@@ -132,18 +152,84 @@ static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* d
|
||||
List_u8_pushBytes(&ddf->data, processed_str.data, 0, processed_str.len);
|
||||
free(processed_str.data);
|
||||
break;
|
||||
case TokenType_OperationEnd:
|
||||
return;
|
||||
default:
|
||||
setError_unexpectedToken(tok);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void parseOperation(Compiler* cmp, str instr_name, Operation* operPtr){
|
||||
Token tok = cmp->tokens.data[cmp->tok_i];
|
||||
const Instruction* instr = Instruction_getByName(instr_name);
|
||||
if(instr == NULL){
|
||||
setError_unexpectedInstruction(tok);
|
||||
return;
|
||||
}
|
||||
|
||||
operPtr->opcode = instr->opcode;
|
||||
operPtr->args = List_Argument_alloc(8);
|
||||
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
|
||||
str tok_str = str_null;
|
||||
str processed_str = str_null;
|
||||
while(++cmp->tok_i < cmp->tokens.len){
|
||||
tok = cmp->tokens.data[cmp->tok_i];
|
||||
switch(tok.type){
|
||||
case TokenType_SingleLineComment:
|
||||
case TokenType_MultiLineComment:
|
||||
// skip comments
|
||||
break;
|
||||
|
||||
case TokenType_OperationEnd:
|
||||
return;
|
||||
case TokenType_Unset:
|
||||
setError(Error_TokenUnset);
|
||||
return;
|
||||
default:
|
||||
setError_unexpectedToken(tok);
|
||||
return;
|
||||
|
||||
case TokenType_Number:
|
||||
arg.type = ArgumentType_ConstValue;
|
||||
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
processed_str = str_copy(tok_str);
|
||||
if(str_seekChar(tok_str, '.', 0) != -1){
|
||||
arg.value.f = atof(processed_str.data);
|
||||
}
|
||||
else {
|
||||
arg.value.i = atoll(processed_str.data);
|
||||
}
|
||||
free(processed_str.data);
|
||||
List_Argument_push(&operPtr->args, arg);
|
||||
break;
|
||||
case TokenType_Name:
|
||||
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
arg.value.register_code = RegisterCode_parse(tok_str);
|
||||
if(arg.value.register_code != RegisterCode_Unset){
|
||||
arg.type = ArgumentType_Register;
|
||||
}
|
||||
else {
|
||||
arg.type = ArgumentType_VarDataName;
|
||||
arg.value.data_name = tok_str;
|
||||
}
|
||||
List_Argument_push(&operPtr->args, arg);
|
||||
break;
|
||||
case TokenType_NamedDataPointer:
|
||||
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
tok_str.data++;
|
||||
tok_str.len--;
|
||||
arg.type = ArgumentType_ConstDataPointer;
|
||||
arg.value.data_name = tok_str;
|
||||
List_Argument_push(&operPtr->args, arg);
|
||||
break;
|
||||
case TokenType_NamedDataSize:
|
||||
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||
tok_str.data++;
|
||||
tok_str.len--;
|
||||
arg.type = ArgumentType_ConstDataSize;
|
||||
arg.value.data_name = tok_str;
|
||||
List_Argument_push(&operPtr->args, arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Compiler_parse(Compiler* cmp){
|
||||
|
||||
@@ -13,11 +13,12 @@ static str _TokenType_str[] = {
|
||||
STR("String"),
|
||||
STR("Name"),
|
||||
STR("NamedDataPointer"),
|
||||
STR("NamedDataSize")
|
||||
STR("NamedDataSize"),
|
||||
STR("OperationEnd"),
|
||||
};
|
||||
|
||||
str TokenType_toString(TokenType t){
|
||||
if(t >= ARRAY_SIZE(_TokenType_str))
|
||||
return STR("!!INDEX_ERROR!!");
|
||||
return STR("!!TokenType INDEX_ERROR!!");
|
||||
return _TokenType_str[t];
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
||||
|
||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||
char* buf = malloc(buffer_size);
|
||||
int r = vsprintf_s(buf, buffer_size, format, argv);
|
||||
int r = vsprintf(buf, format, argv);
|
||||
if(r < 0){
|
||||
free(buf);
|
||||
return NULL;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
i32 MOV_impl(VM* vm){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 src_register_i = 0;
|
||||
u8 src_register_i = 0;
|
||||
readRegisterVar(src_register_i);
|
||||
if(dst_register_i == src_register_i){
|
||||
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
i32 PUSH_impl(VM* vm){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 value_size = 0;
|
||||
readValueSizeVar(value_size);
|
||||
/*u8 value_size = 0;
|
||||
readValueSizeVar(value_size);*/
|
||||
u8 value_size = 4;\
|
||||
|
||||
vm->registers[dst_register_i].u32v = 0;
|
||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "../instructions.h"
|
||||
#include "../registers.h"
|
||||
|
||||
#define readVar(VAR) {\
|
||||
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
||||
@@ -8,7 +9,7 @@
|
||||
}
|
||||
|
||||
#define validateRegisterIndex(VAR) {\
|
||||
if(VAR > sizeof(vm->registers)){\
|
||||
if(VAR> sizeof(vm->registers)){\
|
||||
VM_setError(vm, "invalid register index (%x)", VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
@@ -16,9 +17,11 @@
|
||||
|
||||
#define readRegisterVar(VAR) {\
|
||||
readVar(VAR);\
|
||||
VAR -= 1;\
|
||||
validateRegisterIndex(VAR);\
|
||||
}
|
||||
|
||||
/*
|
||||
#define validateValueSize(VAR) {\
|
||||
if(VAR < 1 || VAR > 4){\
|
||||
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
||||
@@ -30,3 +33,4 @@
|
||||
readVar(VAR);\
|
||||
validateValueSize(VAR);\
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
u8 dst_register_i = 0, src_register_i = 0;\
|
||||
readRegisterVar(dst_register_i);\
|
||||
readRegisterVar(src_register_i);\
|
||||
u8 value_size = 0;\
|
||||
readValueSizeVar(value_size);\
|
||||
/*u8 value_size = 0;\
|
||||
readValueSizeVar(value_size);*/\
|
||||
u8 value_size = 4;\
|
||||
\
|
||||
switch(value_size){\
|
||||
case 1: \
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "instructions.h"
|
||||
#include "../collections/HashMap.h"
|
||||
|
||||
i32 NOP_impl(VM* vm);
|
||||
i32 PUSH_impl(VM* vm);
|
||||
@@ -13,8 +14,8 @@ i32 EXIT_impl(VM* vm);
|
||||
i32 JMP_impl(VM* vm);
|
||||
i32 CALL_impl(VM* vm);
|
||||
|
||||
|
||||
const Instruction instructions[] = {
|
||||
Array_declare(Instruction);
|
||||
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
||||
Instruction_construct(NOP),
|
||||
Instruction_construct(PUSH),
|
||||
Instruction_construct(MOV),
|
||||
@@ -27,11 +28,38 @@ const Instruction instructions[] = {
|
||||
Instruction_construct(EXIT),
|
||||
// Instruction_construct(JMP),
|
||||
// Instruction_construct(CALL),
|
||||
};
|
||||
});
|
||||
|
||||
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
||||
if(opcode >= ARRAY_SIZE(instructions))
|
||||
if(opcode >= instructions_array.len)
|
||||
return NULL;
|
||||
|
||||
return instructions + opcode;
|
||||
return instructions_array.data + opcode;
|
||||
}
|
||||
|
||||
HashMap_declare(Instruction);
|
||||
HashMap_define(Instruction, HashMap_DESTROY_VALUE_FUNC_NULL);
|
||||
|
||||
static HashMap_Instruction* instructions_map = NULL;
|
||||
|
||||
const Instruction* Instruction_getByName(str name){
|
||||
if(instructions_map == NULL){
|
||||
instructions_map = malloc(sizeof(HashMap_Instruction));
|
||||
HashMap_Instruction_alloc(instructions_map);
|
||||
for(u32 i = 0; i < instructions_array.len; i++){
|
||||
HashMap_Instruction_tryPush(instructions_map, instructions_array.data[i].name, instructions_array.data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
str name_upper = str_toUpper(name);
|
||||
Instruction* iptr = HashMap_Instruction_tryGetPtr(instructions_map, name_upper);
|
||||
free(name_upper.data);
|
||||
return iptr;
|
||||
}
|
||||
|
||||
void Instruction_freeSearchStructs(){
|
||||
if(instructions_map != NULL){
|
||||
HashMap_Instruction_free(instructions_map);
|
||||
free(instructions_map);
|
||||
}
|
||||
}
|
||||
@@ -34,3 +34,5 @@ typedef struct Instruction {
|
||||
/// @param opcode any byte
|
||||
/// @return ptr to struct or NULL
|
||||
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
||||
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
||||
void Instruction_freeSearchStructs();
|
||||
|
||||
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);
|
||||
@@ -92,6 +92,8 @@ i32 main(const i32 argc, cstr* argv){
|
||||
exit_code = bootFromImage(image_file);
|
||||
}
|
||||
|
||||
// frees global variables to supress valgrind memory leak errors
|
||||
Instruction_freeSearchStructs();
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,13 @@ typedef u8 bool;
|
||||
|
||||
typedef const char* cstr;
|
||||
|
||||
#define ARRAY_SIZE(A) sizeof(A)/sizeof(A[0])
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define IFWIN(YES, NO) YES
|
||||
#else
|
||||
#define IFWIN(YES, NO) NO
|
||||
#endif
|
||||
|
||||
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
|
||||
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||
|
||||
#define __count_args( \
|
||||
|
||||
@@ -36,18 +36,18 @@ void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
||||
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||
char buf[32];
|
||||
sprintf_s(buf, sizeof(buf), "%llu", n);
|
||||
sprintf(buf, IFWIN("%lli", "%li"), n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
||||
char buf[32];
|
||||
sprintf_s(buf, sizeof(buf), "%llu", n);
|
||||
sprintf(buf, IFWIN("%llu", "%lu"), n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||
char buf[32];
|
||||
sprintf_s(buf, sizeof(buf), "%lf", n);
|
||||
sprintf(buf, "%lf", n);
|
||||
StringBuilder_append_cstr(b, buf);
|
||||
}
|
||||
|
||||
@@ -97,3 +97,29 @@ bool str_endsWith(str src, str fragment){
|
||||
src.len = fragment.len;
|
||||
return str_equals(src, fragment);
|
||||
}
|
||||
|
||||
u32 str_hash32(str s){
|
||||
u8* ubuf = (u8*)s.data;
|
||||
u32 hash=0;
|
||||
for (u32 i = 0; i < s.len; i++)
|
||||
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
|
||||
return hash;
|
||||
}
|
||||
|
||||
str str_toUpper(str src){
|
||||
str r = str_copy(src);
|
||||
for (u32 i = 0; i < r.len; i++){
|
||||
if(isAlphabeticalLower(r.data[i]))
|
||||
r.data[i] = r.data[i] - 'a' + 'A';
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
str str_toLower(str src){
|
||||
str r = str_copy(src);
|
||||
for (u32 i = 0; i < r.len; i++){
|
||||
if(isAlphabeticalUpper(r.data[i]))
|
||||
r.data[i] = r.data[i] - 'A' + 'a';
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ typedef struct str {
|
||||
bool isZeroTerminated;
|
||||
} str;
|
||||
|
||||
// creates str from a string literal
|
||||
/// creates str from a string literal
|
||||
#define STR(LITERAL) str_construct(LITERAL, ARRAY_SIZE(LITERAL) - 1, true)
|
||||
|
||||
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
||||
@@ -32,3 +32,10 @@ i32 str_seekCharReverse(str src, char c, u32 startIndex);
|
||||
|
||||
bool str_startsWith(str src, str fragment);
|
||||
bool str_endsWith(str src, str fragment);
|
||||
|
||||
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
||||
/// @return non-cryptografic hash of the string
|
||||
u32 str_hash32(str s);
|
||||
|
||||
str str_toUpper(str src);
|
||||
str str_toLower(str src);
|
||||
|
||||
Reference in New Issue
Block a user