Compare commits
15 Commits
2831474f79
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 715a2cd82e | |||
| 3fd45311c5 | |||
| b1b20d336d | |||
| 53ca7e1b49 | |||
| fe6d690251 | |||
| 51ef24bb53 | |||
| 422d967165 | |||
| 5d275c8dd1 | |||
| 2faff91981 | |||
| b443367f46 | |||
| fd9e5dda78 | |||
| 823223ffa7 | |||
| 8a38813ba5 | |||
| 7c2809aae2 | |||
| 9e9b43afb4 |
@@ -77,7 +77,9 @@ i32 VM_boot(VM* vm){
|
|||||||
|
|
||||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||||
if(pos + size >= vm->data_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);
|
size, (u32)pos, vm->data_size - size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
|
|
||||||
typedef union Register {
|
typedef union Register {
|
||||||
u32 u32v;
|
u32 u32v;
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#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)\
|
#define Array_declare(T)\
|
||||||
typedef struct Array_##T {\
|
typedef struct Array_##T {\
|
||||||
T* data;\
|
T* data;\
|
||||||
u32 len;\
|
u32 len;\
|
||||||
} Array_##T;\
|
} Array_##T;\
|
||||||
\
|
\
|
||||||
static inline Array_##T Array_##T##_construct(T* data_ptr, u32 len) {\
|
static inline Array_##T Array_##T##_alloc(u32 len){\
|
||||||
return (Array_##T){ .data = data_ptr, .len = len };\
|
return Array_construct(T, (T*)malloc(len * sizeof(T)), len);\
|
||||||
}\
|
}\
|
||||||
\
|
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
|
||||||
static inline Array_##T Array_##T##_alloc(u32 len){\
|
ptr->data = (T*)realloc(ptr->data, new_len * sizeof(T));\
|
||||||
return Array_##T##_construct((T*)malloc(len * sizeof(T)), len);\
|
ptr->len = new_len;\
|
||||||
}\
|
}
|
||||||
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
|
|
||||||
ptr->data = (T*)realloc(ptr->data, new_len * sizeof(T));\
|
Array_declare(u8)
|
||||||
ptr->len = new_len;\
|
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;\
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "List.h"
|
#include "List.h"
|
||||||
|
|
||||||
List_define(cstr);
|
|
||||||
List_define(u32);
|
List_define(u32);
|
||||||
List_define(u8);
|
List_define(u8);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
T* List_##T##_expand(List_##T* ptr, u32 count);\
|
T* List_##T##_expand(List_##T* ptr, u32 count);\
|
||||||
void List_##T##_push(List_##T* ptr, T value);\
|
void List_##T##_push(List_##T* ptr, T value);\
|
||||||
void List_##T##_pushMany(List_##T* ptr, T* values, u32 count);\
|
void List_##T##_pushMany(List_##T* ptr, T* values, u32 count);\
|
||||||
|
bool List_##T##_tryRemoveAt(List_##T* ptr, u32 i);\
|
||||||
|
|
||||||
|
|
||||||
#define List_define(T)\
|
#define List_define(T)\
|
||||||
@@ -29,17 +30,22 @@
|
|||||||
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
|
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
|
||||||
/* branchless version of max(max_len, __List_min_size) */\
|
/* branchless version of max(max_len, __List_min_size) */\
|
||||||
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
|
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){\
|
T* List_##T##_expand(List_##T* ptr, u32 count){\
|
||||||
u32 occupied_len = ptr->len;\
|
u32 occupied_len = ptr->len;\
|
||||||
u32 expanded_max_len = ptr->max_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;\
|
ptr->len += count;\
|
||||||
while(ptr->len > ptr->max_len){\
|
while(ptr->len > expanded_max_len){\
|
||||||
expanded_max_len *= 2;\
|
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;\
|
return ptr->data + occupied_len;\
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
@@ -52,8 +58,18 @@
|
|||||||
T* empty_cell_ptr = List_##T##_expand(ptr, count);\
|
T* empty_cell_ptr = List_##T##_expand(ptr, count);\
|
||||||
memcpy(empty_cell_ptr, values, count * sizeof(T));\
|
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(cstr);
|
|
||||||
List_declare(u32);
|
List_declare(u32);
|
||||||
List_declare(u8);
|
List_declare(u8);
|
||||||
|
|||||||
@@ -5,31 +5,37 @@ List_define(Operation);
|
|||||||
List_define(DataDefinition);
|
List_define(DataDefinition);
|
||||||
List_define(Section);
|
List_define(Section);
|
||||||
|
|
||||||
static cstr _ArgumentType_str[] = {
|
static str _ArgumentType_str[] = {
|
||||||
"Unset",
|
STR("Unset"),
|
||||||
"Register",
|
STR("Register"),
|
||||||
"ConstValue",
|
STR("ConstValue"),
|
||||||
"DataName",
|
STR("VarDataName"),
|
||||||
"NamedDataPointer",
|
STR("ConstDataPointer"),
|
||||||
"NamedDataSize",
|
STR("ConstDataSize"),
|
||||||
};
|
};
|
||||||
|
|
||||||
cstr ArgumentType_toString(ArgumentType t){
|
str ArgumentType_toString(ArgumentType t){
|
||||||
if(t >= ARRAY_SIZE(_ArgumentType_str))
|
if(t >= ARRAY_SIZE(_ArgumentType_str))
|
||||||
return "!!INDEX_ERROR!!";
|
return STR("!!ArgumentType INDEX_ERROR!!");
|
||||||
return _ArgumentType_str[t];
|
return _ArgumentType_str[t];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Section_init(Section* sec, char* name){
|
void Section_init(Section* sec, str name){
|
||||||
sec->name = name;
|
sec->name = name;
|
||||||
sec->data = List_DataDefinition_alloc(256);
|
sec->data = List_DataDefinition_alloc(256);
|
||||||
sec->code = List_Operation_alloc(1024);
|
sec->code = List_Operation_alloc(1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Section_free(Section* sec){
|
void Section_free(Section* sec){
|
||||||
free(sec->name);
|
for(u32 i = 0; i < sec->data.len; i++){
|
||||||
|
free(sec->data.data[i].data.data);
|
||||||
|
}
|
||||||
free(sec->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);
|
free(sec->code.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,29 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#include "../std.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 {
|
||||||
ArgumentType_Unset,
|
ArgumentType_Unset,
|
||||||
ArgumentType_Register,
|
ArgumentType_Register,
|
||||||
ArgumentType_ConstValue,
|
ArgumentType_ConstValue,
|
||||||
ArgumentType_DataName,
|
ArgumentType_VarDataName,
|
||||||
ArgumentType_NamedDataPointer,
|
ArgumentType_ConstDataPointer,
|
||||||
ArgumentType_NamedDataSize,
|
ArgumentType_ConstDataSize,
|
||||||
} ArgumentType;
|
} ArgumentType;
|
||||||
|
|
||||||
cstr ArgumentType_toString(ArgumentType t);
|
str ArgumentType_toString(ArgumentType t);
|
||||||
|
|
||||||
|
|
||||||
typedef struct Argument {
|
typedef struct Argument {
|
||||||
ArgumentType type;
|
ArgumentType type;
|
||||||
u32 value;
|
union {
|
||||||
|
i64 i;
|
||||||
|
f64 f;
|
||||||
|
str data_name;
|
||||||
|
RegisterCode register_code;
|
||||||
|
} value;
|
||||||
} Argument;
|
} Argument;
|
||||||
|
|
||||||
List_declare(Argument);
|
List_declare(Argument);
|
||||||
@@ -25,14 +31,14 @@ List_declare(Argument);
|
|||||||
|
|
||||||
typedef struct Operation {
|
typedef struct Operation {
|
||||||
List_Argument args;
|
List_Argument args;
|
||||||
Opcode op;
|
Opcode opcode;
|
||||||
} Operation;
|
} Operation;
|
||||||
|
|
||||||
List_declare(Operation);
|
List_declare(Operation);
|
||||||
|
|
||||||
|
|
||||||
typedef struct DataDefinition {
|
typedef struct DataDefinition {
|
||||||
cstr name;
|
str name;
|
||||||
List_u8 data;
|
List_u8 data;
|
||||||
u32 element_size;
|
u32 element_size;
|
||||||
} DataDefinition;
|
} DataDefinition;
|
||||||
@@ -41,14 +47,14 @@ List_declare(DataDefinition);
|
|||||||
|
|
||||||
|
|
||||||
typedef struct Section {
|
typedef struct Section {
|
||||||
char* name;
|
str name;
|
||||||
List_DataDefinition data;
|
List_DataDefinition data;
|
||||||
List_Operation code;
|
List_Operation code;
|
||||||
} Section;
|
} Section;
|
||||||
|
|
||||||
List_declare(Section);
|
List_declare(Section);
|
||||||
|
|
||||||
void Section_init(Section* Section, char* name);
|
void Section_init(Section* Section, str name);
|
||||||
void Section_free(Section* Section);
|
void Section_free(Section* Section);
|
||||||
|
|
||||||
typedef struct AST {
|
typedef struct AST {
|
||||||
|
|||||||
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,5 +1,6 @@
|
|||||||
#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));
|
||||||
@@ -7,18 +8,20 @@ void Compiler_init(Compiler* cmp){
|
|||||||
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){
|
||||||
free(cmp->code);
|
free(cmp->code.data);
|
||||||
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){
|
||||||
u32 prev_lines_len = 0;
|
u32 prev_lines_len = 0;
|
||||||
if(pos >= cmp->code_len)
|
if(pos >= cmp->code.len)
|
||||||
return CodePos_create(0, 0);
|
return CodePos_create(0, 0);
|
||||||
|
|
||||||
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||||
@@ -33,8 +36,8 @@ CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
|||||||
|
|
||||||
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
|
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
|
||||||
// happens at the end of file
|
// happens at the end of file
|
||||||
if(cmp->pos >= cmp->code_len)
|
if(cmp->pos >= cmp->code.len)
|
||||||
cmp->pos = cmp->code_len - 1;
|
cmp->pos = cmp->code.len - 1;
|
||||||
char position_str[32];
|
char position_str[32];
|
||||||
CodePos code_pos = Compiler_getLineAndColumn(cmp, cmp->pos);
|
CodePos code_pos = Compiler_getLineAndColumn(cmp, cmp->pos);
|
||||||
sprintf(position_str, "[at %u:%u][", code_pos.line, code_pos.column);
|
sprintf(position_str, "[at %u:%u][", code_pos.line, code_pos.column);
|
||||||
@@ -56,17 +59,161 @@ void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
|
|||||||
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
|
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
|
||||||
}
|
}
|
||||||
|
|
||||||
char* Compiler_extractTokenStr(Compiler* cmp, Token t){
|
str Compiler_constructTokenStr(Compiler* cmp, Token t){
|
||||||
char* s = malloc(t.length + 1);
|
str s = str_construct((char*)(cmp->code.data + t.begin), t.length, false);
|
||||||
memcpy(s, cmp->code, t.length);
|
|
||||||
s[t.length] = 0;
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,71 +222,140 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
if(f == NULL)
|
if(f == NULL)
|
||||||
returnError("ERROR: can't open file '%s'", source_file_name);
|
returnError("ERROR: can't open file '%s'", source_file_name);
|
||||||
|
|
||||||
List_u8 buf = List_u8_alloc(64 * 1024);
|
StringBuilder sb = StringBuilder_alloc(64 * 1024);
|
||||||
int ret;
|
int ret;
|
||||||
while((ret = fgetc(f)) != EOF) {
|
while((ret = fgetc(f)) != EOF) {
|
||||||
List_u8_push(&buf, ret);
|
StringBuilder_append_char(&sb, ret);
|
||||||
}
|
}
|
||||||
if(ferror(f)){
|
if(ferror(f)){
|
||||||
free(buf.data);
|
StringBuilder_free(&sb);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
returnError("can't read file '%s'", source_file_name);
|
returnError("can't read file '%s'", source_file_name);
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if(buf.len == 0){
|
if(sb.buffer.len == 0){
|
||||||
free(buf.data);
|
StringBuilder_free(&sb);
|
||||||
fclose(f);
|
|
||||||
returnError("soucre file is empty");
|
returnError("soucre file is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp->code = (char*)buf.data;
|
cmp->code = str_copy(StringBuilder_getStr(&sb));
|
||||||
cmp->code_len = buf.len;
|
StringBuilder_free(&sb);
|
||||||
List_u8_push(&buf, 0);
|
|
||||||
|
|
||||||
f = fopen(out_file_name, "wb");
|
f = fopen(out_file_name, "wb");
|
||||||
if(f == NULL){
|
if(f == NULL){
|
||||||
free(buf.data);
|
|
||||||
returnError("ERROR: can't open file '%s'", out_file_name);
|
returnError("ERROR: can't open file '%s'", out_file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug_log){
|
if(debug_log){
|
||||||
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
|
printf("===========================[%s]===========================\n", source_file_name);
|
||||||
fputs(cmp->code, stdout);
|
fputs(cmp->code.data, stdout);
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(debug_log)
|
||||||
|
printf("===================================[lexing]===================================\n");
|
||||||
bool success = Compiler_lex(cmp);
|
bool success = Compiler_lex(cmp);
|
||||||
|
|
||||||
if(debug_log){
|
if(debug_log){
|
||||||
printf("------------------------------------[lines]-----------------------------------\n");
|
printf("------------------------------------[lines]------------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||||
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("------------------------------------[tokens]-----------------------------------\n");
|
printf("------------------------------------[tokens]-----------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->tokens.len; i++){
|
for(u32 i = 0; i < cmp->tokens.len; i++){
|
||||||
Token t = cmp->tokens.data[i];
|
Token t = cmp->tokens.data[i];
|
||||||
CodePos pos = Compiler_getLineAndColumn(cmp, t.begin);
|
CodePos pos = Compiler_getLineAndColumn(cmp, t.begin);
|
||||||
char* tokstr = malloc(4096);
|
char* tokstr = malloc(4096);
|
||||||
strncpy(tokstr, cmp->code + t.begin, t.length);
|
strncpy(tokstr, cmp->code.data + t.begin, t.length);
|
||||||
tokstr[t.length] = 0;
|
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",
|
printf("[l:%3u, c:%3u] %s '%s'\n",
|
||||||
pos.line, pos.column,
|
pos.line, pos.column,
|
||||||
TokenType_toString(t.type), tokstr);
|
TokenType_toString(t.type).data, tokstr_stripped);
|
||||||
free(tokstr);
|
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){
|
if(!success){
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
success = Compiler_parse(cmp);
|
if(debug_log)
|
||||||
if(!success){
|
printf("==================================[compiling]==================================\n");
|
||||||
fclose(f);
|
success = writeBinaryFile(cmp, f);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
success = compileFile(cmp, f);
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if(success){
|
if(success){
|
||||||
cmp->state = CompilerState_Success;
|
cmp->state = CompilerState_Success;
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#include "../std.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,
|
||||||
@@ -13,17 +15,24 @@ typedef enum CompilerState {
|
|||||||
CompilerState_Success
|
CompilerState_Success
|
||||||
} CompilerState;
|
} CompilerState;
|
||||||
|
|
||||||
|
typedef Section* SectionPtr;
|
||||||
|
HashMap_declare(SectionPtr);
|
||||||
|
|
||||||
typedef struct Compiler {
|
typedef struct Compiler {
|
||||||
char* code;
|
/* general fields */
|
||||||
u32 code_len;
|
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);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
#include "../string/StringBuilder.h"
|
||||||
|
|
||||||
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ typedef struct CodePos {
|
|||||||
/// @param pos index in code buffer
|
/// @param pos index in code buffer
|
||||||
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos);
|
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos);
|
||||||
|
|
||||||
char* Compiler_extractTokenStr(Compiler* cmp, Token t);
|
str Compiler_constructTokenStr(Compiler* cmp, Token t);
|
||||||
|
|
||||||
bool Compiler_lex(Compiler* cmp);
|
bool Compiler_lex(Compiler* cmp);
|
||||||
bool Compiler_parse(Compiler* cmp);
|
bool Compiler_parse(Compiler* cmp);
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ static void readCommentSingleLine(Compiler* cmp){
|
|||||||
cmp->column++;
|
cmp->column++;
|
||||||
cmp->pos++;
|
cmp->pos++;
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
c = cmp->code[cmp->pos];
|
c = cmp->code.data[cmp->pos];
|
||||||
// end of line
|
// end of line
|
||||||
if(c == '\r' || c == '\n'){
|
if(c == '\r' || c == '\n'){
|
||||||
tok.length = cmp->pos - tok.begin;
|
tok.length = cmp->pos - tok.begin;
|
||||||
@@ -44,10 +44,10 @@ static void readCommentMultiLine(Compiler* cmp){
|
|||||||
cmp->column++;
|
cmp->column++;
|
||||||
cmp->pos++;
|
cmp->pos++;
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
c = cmp->code[cmp->pos];
|
c = cmp->code.data[cmp->pos];
|
||||||
// closing comment
|
// closing comment
|
||||||
if(cmp->pos > tok.begin + 3 && c == '/' && cmp->code[cmp->pos - 1] == '*') {
|
if(cmp->pos > tok.begin + 3 && c == '/' && cmp->code.data[cmp->pos - 1] == '*') {
|
||||||
tok.length = cmp->pos - tok.begin + 1;
|
tok.length = cmp->pos - tok.begin + 1;
|
||||||
List_Token_push(&cmp->tokens, tok);
|
List_Token_push(&cmp->tokens, tok);
|
||||||
return;
|
return;
|
||||||
@@ -65,14 +65,14 @@ static void readCommentMultiLine(Compiler* cmp){
|
|||||||
|
|
||||||
static void readComment(Compiler* cmp){
|
static void readComment(Compiler* cmp){
|
||||||
char c; // '/'
|
char c; // '/'
|
||||||
if(cmp->pos + 1 == cmp->code_len){
|
if(cmp->pos + 1 == cmp->code.len){
|
||||||
setError(Error_endOfFile);
|
setError(Error_endOfFile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = cmp->code[cmp->pos + 1];
|
c = cmp->code.data[cmp->pos + 1];
|
||||||
if(c == '\r' || c == '\n'){
|
if(c == '\r' || c == '\n'){
|
||||||
setError(Error_unexpectedCharacter(cmp->code[--cmp->pos]));
|
setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,14 +91,14 @@ static void readLabel(Compiler* cmp){
|
|||||||
cmp->column++;
|
cmp->column++;
|
||||||
Token tok = Token_construct(TokenType_Label, cmp->pos, 0);
|
Token tok = Token_construct(TokenType_Label, cmp->pos, 0);
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
c = cmp->code[cmp->pos];
|
c = cmp->code.data[cmp->pos];
|
||||||
// end of line
|
// end of line
|
||||||
if(c == ':' || c == '\r' || c == '\n'){
|
if(c == ':' || c == '\r' || c == '\n'){
|
||||||
tok.length = cmp->pos - tok.begin;
|
tok.length = cmp->pos - tok.begin;
|
||||||
if(tok.length > 0)
|
if(tok.length > 0)
|
||||||
List_Token_push(&cmp->tokens, tok);
|
List_Token_push(&cmp->tokens, tok);
|
||||||
else setError(Error_unexpectedCharacter(cmp->code[--cmp->pos]));
|
else setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
|
||||||
// cmp->line will be increased in lex()
|
// cmp->line will be increased in lex()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -125,12 +125,12 @@ static void readArguments(Compiler* cmp){
|
|||||||
Token tok = Token_construct(TokenType_Unset, cmp->pos, 0);
|
Token tok = Token_construct(TokenType_Unset, cmp->pos, 0);
|
||||||
char quot = '\0'; // quotation character of a string value
|
char quot = '\0'; // quotation character of a string value
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
c = cmp->code[cmp->pos];
|
c = cmp->code.data[cmp->pos];
|
||||||
|
|
||||||
// string argument reading
|
// string argument reading
|
||||||
if(quot != '\0'){
|
if(quot != '\0'){
|
||||||
if(c == quot && cmp->code[cmp->pos - 1] != '\\'){
|
if(c == quot && cmp->code.data[cmp->pos - 1] != '\\'){
|
||||||
quot = '\0';
|
quot = '\0';
|
||||||
}
|
}
|
||||||
else if(c == '\r' || c == '\n'){
|
else if(c == '\r' || c == '\n'){
|
||||||
@@ -189,8 +189,8 @@ static void readInstruction(Compiler* cmp){
|
|||||||
cmp->pos++;
|
cmp->pos++;
|
||||||
cmp->column++;
|
cmp->column++;
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
char c = cmp->code[cmp->pos];
|
char c = cmp->code.data[cmp->pos];
|
||||||
// end of line
|
// end of line
|
||||||
if(c == '\r' || c == '\n' || c == ';'){
|
if(c == '\r' || c == '\n' || c == ';'){
|
||||||
tok.length = cmp->pos - tok.begin;
|
tok.length = cmp->pos - tok.begin;
|
||||||
@@ -232,8 +232,8 @@ bool Compiler_lex(Compiler* cmp){
|
|||||||
cmp->state = CompilerState_Lexing;
|
cmp->state = CompilerState_Lexing;
|
||||||
cmp->column = 1;
|
cmp->column = 1;
|
||||||
|
|
||||||
while(cmp->pos < cmp->code_len){
|
while(cmp->pos < cmp->code.len){
|
||||||
char c = cmp->code[cmp->pos];
|
char c = cmp->code.data[cmp->pos];
|
||||||
switch(c){
|
switch(c){
|
||||||
// skip blank characters
|
// skip blank characters
|
||||||
case ' ': case '\t': case '\r': case '\n':
|
case ' ': case '\t': case '\r': case '\n':
|
||||||
@@ -257,7 +257,7 @@ bool Compiler_lex(Compiler* cmp){
|
|||||||
if(cmp->state == CompilerState_Error)
|
if(cmp->state == CompilerState_Error)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
c = cmp->code[cmp->pos];
|
c = cmp->code.data[cmp->pos];
|
||||||
if(c == '\n')
|
if(c == '\n')
|
||||||
completeLine(cmp);
|
completeLine(cmp);
|
||||||
cmp->column++;
|
cmp->column++;
|
||||||
|
|||||||
@@ -6,17 +6,23 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define setError_unexpectedToken(T) {\
|
#define setError_unexpectedToken(T) {\
|
||||||
char* tok_str = Compiler_extractTokenStr(cmp, T);\
|
str tok_str = str_copy(Compiler_constructTokenStr(cmp, T));\
|
||||||
cmp->pos = T.begin;\
|
cmp->pos = T.begin;\
|
||||||
Compiler_setError(cmp, "unexpected token '%s'", tok_str);\
|
Compiler_setError(cmp, "unexpected token '%s'", tok_str.data);\
|
||||||
free(tok_str);\
|
free(tok_str.data);\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define setError_unexpectedTokenChar(T, I) {\
|
#define setError_unexpectedTokenChar(T, I) {\
|
||||||
cmp->pos = T.begin + I;\
|
cmp->pos = T.begin + I;\
|
||||||
Compiler_setError(cmp, "unexpected token '%c'", cmp->code[cmp->pos]);\
|
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_TokenUnset "token of undefined type"
|
||||||
#define Error_BitSize "invalid size in bits"
|
#define Error_BitSize "invalid size in bits"
|
||||||
@@ -28,120 +34,202 @@ 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(u8*) resolveEscapeSequences(Compiler* cmp, cstr src){
|
static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
||||||
u32 len = strlen(src);
|
StringBuilder sb = StringBuilder_alloc(src.len);
|
||||||
List_u8 resolved = List_u8_alloc(len);
|
|
||||||
char c;
|
char c;
|
||||||
bool escaped = false;
|
bool escaped = false;
|
||||||
for(u32 i = 0; i < len; i++){
|
for(u32 i = 0; i < src.len; i++){
|
||||||
c = src[i];
|
c = src.data[i];
|
||||||
if(c == '\\'){
|
if(c == '\\'){
|
||||||
escaped = !escaped;
|
escaped = !escaped;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!escaped){
|
if(!escaped){
|
||||||
List_u8_push(&resolved, c);
|
StringBuilder_append_char(&sb, c);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// escape codes
|
// escape codes
|
||||||
switch(c){
|
switch(c){
|
||||||
case '0':
|
case '0':
|
||||||
List_u8_push(&resolved, '\0');
|
StringBuilder_append_char(&sb, '\0');
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
List_u8_push(&resolved, '\n');
|
StringBuilder_append_char(&sb, '\n');
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
List_u8_push(&resolved, '\r');
|
StringBuilder_append_char(&sb, '\r');
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
List_u8_push(&resolved, '\t');
|
StringBuilder_append_char(&sb, '\t');
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
List_u8_push(&resolved, '\e');
|
StringBuilder_append_char(&sb, '\e');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
||||||
free(resolved.data);
|
StringBuilder_free(&sb);
|
||||||
return NULL;
|
return str_null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolved.data;
|
return StringBuilder_getStr(&sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseDataDefinition(Compiler* cmp, char* instr_name, DataDefinition* ddf){
|
static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* ddf){
|
||||||
i32 _element_size_bits;
|
i32 _element_size_bits;
|
||||||
if(sscanf(instr_name, "const%i", &_element_size_bits) != 1 || !isVarSizeBits(_element_size_bits)){
|
str _instr_name_zero_terminated = str_copy(instr_name);
|
||||||
|
if(sscanf(_instr_name_zero_terminated.data, "const%i", &_element_size_bits) != 1 || !isVarSizeBits(_element_size_bits)){
|
||||||
|
free(_instr_name_zero_terminated.data);
|
||||||
setError(Error_BitSize);
|
setError(Error_BitSize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
free(_instr_name_zero_terminated.data);
|
||||||
ddf->element_size = _element_size_bits / 8;
|
ddf->element_size = _element_size_bits / 8;
|
||||||
|
ddf->data = List_u8_alloc(32);
|
||||||
|
|
||||||
Token tok = cmp->tokens.data[++cmp->tok_i];
|
Token tok = cmp->tokens.data[++cmp->tok_i];
|
||||||
char* tok_str = Compiler_extractTokenStr(cmp, tok);
|
if(tok.type != TokenType_Name){
|
||||||
u8* processed_str = NULL;
|
setError_unexpectedToken(tok);
|
||||||
u32 len = 0;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
str tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||||
|
str processed_str = str_null;
|
||||||
ddf->name = tok_str;
|
ddf->name = tok_str;
|
||||||
|
|
||||||
while(++cmp->tok_i < cmp->tokens.len){
|
while(++cmp->tok_i < cmp->tokens.len){
|
||||||
|
tok = cmp->tokens.data[cmp->tok_i];
|
||||||
switch(tok.type){
|
switch(tok.type){
|
||||||
case TokenType_Unset:
|
|
||||||
setError(Error_TokenUnset);
|
|
||||||
return;
|
|
||||||
case TokenType_SingleLineComment:
|
case TokenType_SingleLineComment:
|
||||||
case TokenType_MultiLineComment:
|
case TokenType_MultiLineComment:
|
||||||
// skip comments
|
// skip comments
|
||||||
break;
|
break;
|
||||||
case TokenType_Number:
|
|
||||||
tok_str = Compiler_extractTokenStr(cmp, tok);
|
|
||||||
if(cstr_seekChar(tok_str, '.', 0, -1) != -1){
|
|
||||||
f64 f = atof(tok_str);
|
|
||||||
List_u8_pushBytes(&ddf->data, &f, 8 - ddf->element_size, ddf->element_size);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
i64 i = atoll(tok_str);
|
|
||||||
List_u8_pushBytes(&ddf->data, &i, 8 - ddf->element_size, ddf->element_size);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TokenType_Char:
|
|
||||||
tok.begin += 1;
|
|
||||||
tok.length -= 2;
|
|
||||||
tok_str = Compiler_extractTokenStr(cmp, tok);
|
|
||||||
processed_str = resolveEscapeSequences(cmp, tok_str);
|
|
||||||
free(tok_str);
|
|
||||||
len = strlen((char*)processed_str);
|
|
||||||
if(len != ddf->element_size){
|
|
||||||
setError("can't fit char of size %i in %u bit variable", len, _element_size_bits);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List_u8_pushBytes(&ddf->data, processed_str, 0, len);
|
|
||||||
break;
|
|
||||||
case TokenType_String:
|
|
||||||
tok.begin += 1;
|
|
||||||
tok.length -= 2;
|
|
||||||
tok_str = Compiler_extractTokenStr(cmp, tok);
|
|
||||||
processed_str = resolveEscapeSequences(cmp, tok_str);
|
|
||||||
free(tok_str);
|
|
||||||
len = strlen((char*)processed_str);
|
|
||||||
List_u8_pushBytes(&ddf->data, processed_str, 0, len);
|
|
||||||
break;
|
|
||||||
case TokenType_OperationEnd:
|
case TokenType_OperationEnd:
|
||||||
return;
|
return;
|
||||||
|
case TokenType_Unset:
|
||||||
|
setError(Error_TokenUnset);
|
||||||
|
return;
|
||||||
default:
|
default:
|
||||||
setError_unexpectedToken(tok);
|
setError_unexpectedToken(tok);
|
||||||
return;
|
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(processed_str.data);
|
||||||
|
List_u8_pushBytes(&ddf->data, &f, 8 - ddf->element_size, ddf->element_size);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
i64 i = atoll(processed_str.data);
|
||||||
|
List_u8_pushBytes(&ddf->data, &i, 8 - ddf->element_size, ddf->element_size);
|
||||||
|
}
|
||||||
|
free(processed_str.data);
|
||||||
|
break;
|
||||||
|
case TokenType_Char:
|
||||||
|
tok.begin += 1;
|
||||||
|
tok.length -= 2;
|
||||||
|
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||||
|
processed_str = resolveEscapeSequences(cmp, tok_str);
|
||||||
|
|
||||||
|
if(processed_str.len != ddf->element_size){
|
||||||
|
setError("can't fit char of size %i in %u bit variable", processed_str.len, _element_size_bits);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List_u8_pushBytes(&ddf->data, processed_str.data, 0, processed_str.len);
|
||||||
|
free(processed_str.data);
|
||||||
|
break;
|
||||||
|
case TokenType_String:
|
||||||
|
tok.begin += 1;
|
||||||
|
tok.length -= 2;
|
||||||
|
tok_str = Compiler_constructTokenStr(cmp, tok);
|
||||||
|
processed_str = resolveEscapeSequences(cmp, tok_str);
|
||||||
|
List_u8_pushBytes(&ddf->data, processed_str.data, 0, processed_str.len);
|
||||||
|
free(processed_str.data);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void parseOperation(Compiler* cmp, char* instr_name, Operation* operPtr){
|
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){
|
bool Compiler_parse(Compiler* cmp){
|
||||||
@@ -162,14 +250,14 @@ bool Compiler_parse(Compiler* cmp){
|
|||||||
case TokenType_Label:
|
case TokenType_Label:
|
||||||
// create new section
|
// create new section
|
||||||
sec = List_Section_expand(&cmp->ast.sections, 1);
|
sec = List_Section_expand(&cmp->ast.sections, 1);
|
||||||
Section_init(sec, Compiler_extractTokenStr(cmp, tok));
|
Section_init(sec, Compiler_constructTokenStr(cmp, tok));
|
||||||
break;
|
break;
|
||||||
case TokenType_Instruction:
|
case TokenType_Instruction:
|
||||||
if(sec == NULL)
|
if(sec == NULL)
|
||||||
returnError("no section");
|
returnError("no section");
|
||||||
char* instr_name = Compiler_extractTokenStr(cmp, tok);
|
str instr_name = Compiler_constructTokenStr(cmp, tok);
|
||||||
// data definition starts with const
|
// data definition starts with const
|
||||||
if(cstr_seek(instr_name, "const", 0, 1)){
|
if(str_startsWith(instr_name, STR("const"))){
|
||||||
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
||||||
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,23 @@
|
|||||||
|
|
||||||
List_define(Token);
|
List_define(Token);
|
||||||
|
|
||||||
static cstr _TokenType_str[] = {
|
static str _TokenType_str[] = {
|
||||||
"Unset",
|
STR("Unset"),
|
||||||
"SingleLineComment",
|
STR("SingleLineComment"),
|
||||||
"MultiLineComment",
|
STR("MultiLineComment"),
|
||||||
"Instruction",
|
STR("Instruction"),
|
||||||
"Label",
|
STR("Label"),
|
||||||
"Number",
|
STR("Number"),
|
||||||
"Char",
|
STR("Char"),
|
||||||
"String",
|
STR("String"),
|
||||||
"Name",
|
STR("Name"),
|
||||||
"NamedDataPointer",
|
STR("NamedDataPointer"),
|
||||||
"NamedDataSize"
|
STR("NamedDataSize"),
|
||||||
|
STR("OperationEnd"),
|
||||||
};
|
};
|
||||||
|
|
||||||
cstr TokenType_toString(TokenType t){
|
str TokenType_toString(TokenType t){
|
||||||
if(t >= ARRAY_SIZE(_TokenType_str))
|
if(t >= ARRAY_SIZE(_TokenType_str))
|
||||||
return "!!INDEX_ERROR!!";
|
return STR("!!TokenType INDEX_ERROR!!");
|
||||||
return _TokenType_str[t];
|
return _TokenType_str[t];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
#include "../collections/List.h"
|
#include "../collections/List.h"
|
||||||
|
|
||||||
typedef enum TokenType {
|
typedef enum TokenType {
|
||||||
@@ -17,7 +18,7 @@ typedef enum TokenType {
|
|||||||
TokenType_OperationEnd, // EOL or EOF or ;
|
TokenType_OperationEnd, // EOL or EOF or ;
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
cstr TokenType_toString(TokenType t);
|
str TokenType_toString(TokenType t);
|
||||||
|
|
||||||
typedef struct Token {
|
typedef struct Token {
|
||||||
u32 begin; // some index in Compiler->code
|
u32 begin; // some index in Compiler->code
|
||||||
|
|||||||
70
src/cstr.c
70
src/cstr.c
@@ -43,78 +43,10 @@ char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
|||||||
|
|
||||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||||
char* buf = malloc(buffer_size);
|
char* buf = malloc(buffer_size);
|
||||||
int r = vsprintf_s(buf, buffer_size, format, argv);
|
int r = vsprintf(buf, format, argv);
|
||||||
if(r < 0){
|
if(r < 0){
|
||||||
free(buf);
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
|
|
||||||
char sc = *src, fc = *fragment;
|
|
||||||
if(sc == 0 || fc == 0)
|
|
||||||
return -1;
|
|
||||||
u32 fr_start = startIndex;
|
|
||||||
for(u32 si = startIndex; si-startIndex < seekLength && sc != 0; si++){
|
|
||||||
sc = src[si];
|
|
||||||
fc = fragment[si-fr_start];
|
|
||||||
if(fc == 0)
|
|
||||||
return fr_start;
|
|
||||||
if(sc != fc)
|
|
||||||
fr_start++;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
|
|
||||||
char sc = *src, fc = *fragment;
|
|
||||||
if(sc == 0 || fc == 0)
|
|
||||||
return -1;
|
|
||||||
i32 len = strlen(src);
|
|
||||||
if(startIndex == (u32)-1)
|
|
||||||
startIndex = len-1;
|
|
||||||
u32 fr_len = strlen(fragment);
|
|
||||||
for(u32 si = startIndex; si < (u32)-1 && si != (len - seekLength - 1); si--){
|
|
||||||
if(si + 1 < fr_len)
|
|
||||||
return -1;
|
|
||||||
sc = src[si];
|
|
||||||
fc = fragment[0];
|
|
||||||
u32 fr_start = si;
|
|
||||||
for(u32 fi = 0; fc == sc ; fi++){
|
|
||||||
if(fi == fr_len)
|
|
||||||
return fr_start;
|
|
||||||
fc = fragment[fi];
|
|
||||||
sc = src[si--];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
i32 cstr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength){
|
|
||||||
char sc=*src;
|
|
||||||
if(sc==0 || fragment==0)
|
|
||||||
return -1;
|
|
||||||
for(u32 si=startIndex; si-startIndex<seekLength && sc!=0; si++){
|
|
||||||
sc=src[si];
|
|
||||||
if(sc==fragment)
|
|
||||||
return si;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 cstr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength){
|
|
||||||
char sc=*src;
|
|
||||||
if(sc==0 || fragment==0)
|
|
||||||
return -1;
|
|
||||||
i32 len=strlen(src);
|
|
||||||
if(startIndex==(u32)-1)
|
|
||||||
startIndex=len-1;
|
|
||||||
for(u32 si=startIndex; si<(u32)-1 && si!=len-1-seekLength; si--){
|
|
||||||
sc=src[si];
|
|
||||||
if(sc==fragment)
|
|
||||||
return si;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
i32 MOV_impl(VM* vm){
|
i32 MOV_impl(VM* vm){
|
||||||
u8 dst_register_i = 0;
|
u8 dst_register_i = 0;
|
||||||
readRegisterVar(dst_register_i);
|
readRegisterVar(dst_register_i);
|
||||||
u8 src_register_i = 0;
|
u8 src_register_i = 0;
|
||||||
readRegisterVar(src_register_i);
|
readRegisterVar(src_register_i);
|
||||||
if(dst_register_i == src_register_i){
|
if(dst_register_i == src_register_i){
|
||||||
VM_setError(vm, "dst_register_i == src_register_i (%x) ", 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){
|
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))) \
|
||||||
@@ -8,7 +9,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define validateRegisterIndex(VAR) {\
|
#define validateRegisterIndex(VAR) {\
|
||||||
if(VAR > sizeof(vm->registers)){\
|
if(VAR> sizeof(vm->registers)){\
|
||||||
VM_setError(vm, "invalid register index (%x)", VAR);\
|
VM_setError(vm, "invalid register index (%x)", VAR);\
|
||||||
return -1;\
|
return -1;\
|
||||||
}\
|
}\
|
||||||
@@ -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: \
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "instructions.h"
|
#include "instructions.h"
|
||||||
|
#include "../collections/HashMap.h"
|
||||||
|
|
||||||
i32 NOP_impl(VM* vm);
|
i32 NOP_impl(VM* vm);
|
||||||
i32 PUSH_impl(VM* vm);
|
i32 PUSH_impl(VM* vm);
|
||||||
@@ -13,8 +14,8 @@ i32 EXIT_impl(VM* vm);
|
|||||||
i32 JMP_impl(VM* vm);
|
i32 JMP_impl(VM* vm);
|
||||||
i32 CALL_impl(VM* vm);
|
i32 CALL_impl(VM* vm);
|
||||||
|
|
||||||
|
Array_declare(Instruction);
|
||||||
const Instruction instructions[] = {
|
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
||||||
Instruction_construct(NOP),
|
Instruction_construct(NOP),
|
||||||
Instruction_construct(PUSH),
|
Instruction_construct(PUSH),
|
||||||
Instruction_construct(MOV),
|
Instruction_construct(MOV),
|
||||||
@@ -27,11 +28,38 @@ const Instruction instructions[] = {
|
|||||||
Instruction_construct(EXIT),
|
Instruction_construct(EXIT),
|
||||||
// Instruction_construct(JMP),
|
// Instruction_construct(JMP),
|
||||||
// Instruction_construct(CALL),
|
// Instruction_construct(CALL),
|
||||||
};
|
});
|
||||||
|
|
||||||
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
||||||
if(opcode >= ARRAY_SIZE(instructions))
|
if(opcode >= instructions_array.len)
|
||||||
return NULL;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,13 +19,13 @@ typedef enum __attribute__((__packed__)) Opcode {
|
|||||||
} Opcode;
|
} Opcode;
|
||||||
|
|
||||||
typedef struct Instruction {
|
typedef struct Instruction {
|
||||||
cstr name;
|
str name;
|
||||||
InstructionImplFunc_t implementation;
|
InstructionImplFunc_t implementation;
|
||||||
Opcode opcode;
|
Opcode opcode;
|
||||||
} Instruction;
|
} Instruction;
|
||||||
|
|
||||||
#define Instruction_construct(NAME) {\
|
#define Instruction_construct(NAME) {\
|
||||||
.name = #NAME, \
|
.name = STR(#NAME), \
|
||||||
.implementation = NAME##_impl, \
|
.implementation = NAME##_impl, \
|
||||||
.opcode = Opcode_##NAME\
|
.opcode = Opcode_##NAME\
|
||||||
}
|
}
|
||||||
@@ -34,3 +34,5 @@ typedef struct Instruction {
|
|||||||
/// @param opcode any byte
|
/// @param opcode any byte
|
||||||
/// @return ptr to struct or NULL
|
/// @return ptr to struct or NULL
|
||||||
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
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);
|
||||||
@@ -38,7 +38,7 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
for(u8 opcode = 0; opcode < 255; opcode++){
|
for(u8 opcode = 0; opcode < 255; opcode++){
|
||||||
const Instruction* instr = Instruction_getByOpcode(opcode);
|
const Instruction* instr = Instruction_getByOpcode(opcode);
|
||||||
if(instr != NULL){
|
if(instr != NULL){
|
||||||
printf("%02X %s\n", opcode, instr->name);
|
printf("%02X %s\n", opcode, instr->name.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -92,6 +92,8 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
exit_code = bootFromImage(image_file);
|
exit_code = bootFromImage(image_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// frees global variables to supress valgrind memory leak errors
|
||||||
|
Instruction_freeSearchStructs();
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
28
src/std.h
28
src/std.h
@@ -26,7 +26,13 @@ typedef u8 bool;
|
|||||||
|
|
||||||
typedef const char* cstr;
|
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 ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||||
|
|
||||||
#define __count_args( \
|
#define __count_args( \
|
||||||
@@ -58,23 +64,3 @@ char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);
|
|||||||
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
|
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
|
||||||
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
|
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
|
||||||
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
|
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
|
||||||
|
|
||||||
/// @param startIndex 0 ... src length
|
|
||||||
/// @param seekLength 0 ... -1
|
|
||||||
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
|
|
||||||
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
|
|
||||||
|
|
||||||
/// @param startIndex -1 ... src length
|
|
||||||
/// @param seekLength 0 ... -1
|
|
||||||
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
|
|
||||||
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
|
|
||||||
|
|
||||||
/// @param startIndex 0 ... src length
|
|
||||||
/// @param seekLength 0 ... -1
|
|
||||||
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
|
|
||||||
i32 cstr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength);
|
|
||||||
|
|
||||||
/// @param startIndex -1 ... src length
|
|
||||||
/// @param seekLength 0 ... -1
|
|
||||||
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
|
|
||||||
i32 cstr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength);
|
|
||||||
|
|||||||
@@ -2,14 +2,24 @@
|
|||||||
|
|
||||||
void StringBuilder_free(StringBuilder* b){
|
void StringBuilder_free(StringBuilder* b){
|
||||||
free(b->buffer.data);
|
free(b->buffer.data);
|
||||||
|
b->buffer = List_u8_construct(NULL, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
str StringBuilder_build(StringBuilder* b){
|
str StringBuilder_getStr(StringBuilder* b){
|
||||||
List_u8_push(&b->buffer, '\0');
|
List_u8_push(&b->buffer, '\0');
|
||||||
str result = str_construct((char*)b->buffer.data, b->buffer.len - 1, true);
|
str result = str_construct((char*)b->buffer.data, b->buffer.len - 1, true);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count){
|
||||||
|
if(count < b->buffer.len){
|
||||||
|
b->buffer.len -= count;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
b->buffer.len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void StringBuilder_append_char(StringBuilder* b, char c){
|
void StringBuilder_append_char(StringBuilder* b, char c){
|
||||||
List_u8_push(&b->buffer, c);
|
List_u8_push(&b->buffer, c);
|
||||||
@@ -26,18 +36,18 @@ void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
|||||||
|
|
||||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf_s(buf, sizeof(buf), "%llu", n);
|
sprintf(buf, IFWIN("%lli", "%li"), n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf_s(buf, sizeof(buf), "%llu", n);
|
sprintf(buf, IFWIN("%llu", "%lu"), n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf_s(buf, sizeof(buf), "%lf", n);
|
sprintf(buf, "%lf", n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "../collections/List.h"
|
#include "../collections/List.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
@@ -11,11 +7,12 @@ typedef struct StringBuilder {
|
|||||||
List_u8 buffer;
|
List_u8 buffer;
|
||||||
} StringBuilder;
|
} StringBuilder;
|
||||||
|
|
||||||
static inline StringBuilder StringBuilder_construct(u32 initial_size) {
|
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
|
||||||
return (StringBuilder){ .buffer = List_u8_alloc(initial_size) };
|
return (StringBuilder){ .buffer = List_u8_alloc(initial_size) };
|
||||||
}
|
}
|
||||||
void StringBuilder_free(StringBuilder* b);
|
void StringBuilder_free(StringBuilder* b);
|
||||||
|
|
||||||
|
/// @param count set to -1 to clear StringBuilder
|
||||||
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
||||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||||
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
||||||
@@ -26,7 +23,3 @@ void StringBuilder_append_f64(StringBuilder* b, f64 a);
|
|||||||
|
|
||||||
// adds '\0' to the buffer and returns pointer to buffer content
|
// adds '\0' to the buffer and returns pointer to buffer content
|
||||||
str StringBuilder_getStr(StringBuilder* b);
|
str StringBuilder_getStr(StringBuilder* b);
|
||||||
|
|
||||||
#if __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
108
src/string/str.c
108
src/string/str.c
@@ -3,27 +3,123 @@
|
|||||||
str str_copy(str src){
|
str str_copy(str src){
|
||||||
if(src.data == NULL || src.len == 0)
|
if(src.data == NULL || src.len == 0)
|
||||||
return src;
|
return src;
|
||||||
|
|
||||||
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
|
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
|
||||||
memcpy(nstr.data, src.data, src.len);
|
memcpy(nstr.data, src.data, src.len);
|
||||||
nstr.data[nstr.len] = '\0';
|
nstr.data[nstr.len] = '\0';
|
||||||
return nstr;
|
return nstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool str_compare(str str0, str str1){
|
bool str_equals(str s0, str s1){
|
||||||
if(str0.len!= str1.len) return false;
|
if(s0.len != s1.len)
|
||||||
if(!str0.data) return str1.data ? false : true;
|
return false;
|
||||||
else if(!str1.data) return false;
|
|
||||||
while(str0.len-- > 0)
|
for(u32 i = 0; i < s0.len; i++)
|
||||||
if(*str0.data++ != *str1.data++ )
|
if(s0.data[i] != s1.data[i])
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
str str_reverse(str s){
|
str str_reverse(str s){
|
||||||
if(s.data == NULL || s.len == 0)
|
if(s.data == NULL || s.len == 0)
|
||||||
return s;
|
return s;
|
||||||
|
|
||||||
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
|
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
|
||||||
for(u32 i = 0; i < s.len; i++ )
|
for(u32 i = 0; i < s.len; i++ )
|
||||||
r.data[i] = s.data[s.len - i - 1];
|
r.data[i] = s.data[s.len - i - 1];
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i32 str_seek(str src, str fragment, u32 startIndex){
|
||||||
|
if(src.len == 0 || fragment.len == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for(u32 i = startIndex; i < src.len - fragment.len + 1; i++){
|
||||||
|
for(u32 j = 0;; j++){
|
||||||
|
if(j == fragment.len)
|
||||||
|
return i;
|
||||||
|
if(src.data[i + j] != fragment.data[j])
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 str_seekReverse(str src, str fragment, u32 startIndex){
|
||||||
|
if(src.len == 0 || fragment.len == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if(startIndex > src.len - 1)
|
||||||
|
startIndex = src.len - 1;
|
||||||
|
for(u32 i = startIndex; i >= fragment.len - 1; i--){
|
||||||
|
for(u32 j = 0;; j++){
|
||||||
|
if(j == fragment.len)
|
||||||
|
return i - j + 1;
|
||||||
|
if(src.data[i - j] != fragment.data[fragment.len - 1 - j])
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 str_seekChar(str src, char c, u32 startIndex){
|
||||||
|
for(u32 i = startIndex; i < src.len; i++){
|
||||||
|
if(src.data[i] == c)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 str_seekCharReverse(str src, char c, u32 startIndex){
|
||||||
|
if(startIndex > src.len - 1)
|
||||||
|
startIndex = src.len - 1;
|
||||||
|
for(u32 i = startIndex; i != (u32)-1; i--){
|
||||||
|
if(src.data[i] == c)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool str_startsWith(str src, str fragment){
|
||||||
|
if(src.len < fragment.len)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
src.len = fragment.len;
|
||||||
|
return str_equals(src, fragment);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool str_endsWith(str src, str fragment){
|
||||||
|
if(src.len < fragment.len)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
src.data = (char*)(src.data + src.len - fragment.len);
|
||||||
|
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,23 +8,34 @@ typedef struct str {
|
|||||||
bool isZeroTerminated;
|
bool isZeroTerminated;
|
||||||
} str;
|
} str;
|
||||||
|
|
||||||
|
/// 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 })
|
#define str_construct(DATA, LEN, ZERO_TERMINATED) ((str){ .data = DATA, .len = LEN, .isZeroTerminated = ZERO_TERMINATED })
|
||||||
|
|
||||||
static const str str_null = str_construct(NULL, 0, 0);
|
static const str str_null = str_construct(NULL, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
/// copies str content to new char pointer value (adding '\0' at the end)
|
|
||||||
char* str_extractcstr(str str);
|
|
||||||
|
|
||||||
/// copies src content to new string and adds \0 at the end
|
/// copies src content to new string and adds \0 at the end
|
||||||
str str_copy(str src);
|
str str_copy(str src);
|
||||||
|
|
||||||
/// compares two strings, NullPtr-friendly
|
/// compares two strings, NullPtr-friendly
|
||||||
bool str_compare(str str0, str str1);
|
bool str_equals(str str0, str str1);
|
||||||
|
|
||||||
/// allocates new string which is reversed variant of <s>
|
/// allocates new string which is reversed variant of <s>
|
||||||
str str_reverse(str s);
|
str str_reverse(str s);
|
||||||
|
|
||||||
#if __cplusplus
|
i32 str_seek(str src, str fragment, u32 startIndex);
|
||||||
}
|
i32 str_seekReverse(str src, str fragment, u32 startIndex);
|
||||||
#endif
|
|
||||||
|
i32 str_seekChar(str src, char c, u32 startIndex);
|
||||||
|
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