Compare commits
34 Commits
9da8732d93
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 715a2cd82e | |||
| 3fd45311c5 | |||
| b1b20d336d | |||
| 53ca7e1b49 | |||
| fe6d690251 | |||
| 51ef24bb53 | |||
| 422d967165 | |||
| 5d275c8dd1 | |||
| 2faff91981 | |||
| b443367f46 | |||
| fd9e5dda78 | |||
| 823223ffa7 | |||
| 8a38813ba5 | |||
| 7c2809aae2 | |||
| 9e9b43afb4 | |||
| 2831474f79 | |||
| 2d3e66dd38 | |||
| 7f606dfaff | |||
| 46e5eb1887 | |||
| e43a987e1e | |||
| 83e28c9022 | |||
| 5c9197436f | |||
| f710aa4199 | |||
| facacc90f8 | |||
| dbe8569a3b | |||
| bd8215fd73 | |||
| ad232f187a | |||
| 1729070b80 | |||
| 1cae800a66 | |||
| a073d0ebd9 | |||
| bc54f34a4d | |||
| 9d2a26fae3 | |||
| 83172df776 | |||
| b8296a45a7 |
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -7,7 +7,7 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/bin/tcpu",
|
"program": "${workspaceFolder}/bin/tcpu",
|
||||||
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
||||||
"args": [ "--image", "image.bin" ],
|
"args": [ "-c", "../examples/s.tasm", "o.bin", "--debug" ],
|
||||||
"cwd": "${workspaceFolder}/bin",
|
"cwd": "${workspaceFolder}/bin",
|
||||||
"preLaunchTask": "build_exec_dbg",
|
"preLaunchTask": "build_exec_dbg",
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
|
|||||||
16
examples/s.tasm
Normal file
16
examples/s.tasm
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
"hello world" program in my assembly language
|
||||||
|
*/
|
||||||
|
|
||||||
|
.data:
|
||||||
|
// named array of 8-bit values
|
||||||
|
const8 msg "Hello, World :3\0"
|
||||||
|
|
||||||
|
.main:
|
||||||
|
push ax 1; // sys_write
|
||||||
|
push bx 1; // stdout
|
||||||
|
push cx @msg; // address of msg data
|
||||||
|
push dx #msg; // size of msg data
|
||||||
|
sys
|
||||||
|
push ax 0
|
||||||
|
exit
|
||||||
54
src/VM/VM.c
54
src/VM/VM.c
@@ -6,13 +6,30 @@ void VM_init(VM* vm){
|
|||||||
vm->state = VMState_Initialized;
|
vm->state = VMState_Initialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
void _VM_setError(VM* vm, cstr context, cstr format, ...){
|
||||||
|
va_list argv;
|
||||||
|
char position_str[32];
|
||||||
|
sprintf(position_str, "[at 0x%x][", (u32)vm->current_pos);
|
||||||
|
char* real_format = strcat_malloc(position_str, context, "] ", format);
|
||||||
|
va_start(argv, format);
|
||||||
|
char* NULLABLE(buf) = vsprintf_malloc(256, real_format, argv);
|
||||||
|
va_end(argv);
|
||||||
|
free(real_format);
|
||||||
|
if(buf == NULL){
|
||||||
|
buf = malloc(16);
|
||||||
|
strcpy(buf, "SPRINTF FAILED");
|
||||||
|
}
|
||||||
|
vm->error_message = buf;
|
||||||
|
vm->state = VMState_InternalError;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VM_setMemory(VM* vm, u8* data, size_t size){
|
||||||
if(data == NULL){
|
if(data == NULL){
|
||||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
|
VM_setError(vm, "data == NULL");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(size == 0){
|
if(size == 0){
|
||||||
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because size == 0");
|
VM_setError(vm, "size == 0");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,32 +38,36 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 VM_executeProgram(VM* vm){
|
i32 VM_boot(VM* vm){
|
||||||
if(vm->data == NULL){
|
if(vm->data == NULL){
|
||||||
VM_setErrorMessage(vm, "[VM_executeProgram] data is null");
|
VM_setError(vm, "data == null");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t pos = 0;
|
vm->state = VMState_Executing;
|
||||||
while (pos < vm->data_size){
|
vm->current_pos = 0;
|
||||||
u8 opcode = vm->data[pos];
|
while (vm->current_pos < vm->data_size){
|
||||||
|
u8 opcode = vm->data[vm->current_pos];
|
||||||
|
|
||||||
const Instruction* instr = Instruction_getFromOpcode(opcode);
|
const Instruction* instr = Instruction_getByOpcode(opcode);
|
||||||
|
// printfe("[at 0x%x] %02X %s\n", (u32)vm->current_pos, opcode, instr->name);
|
||||||
if(instr == NULL){
|
if(instr == NULL){
|
||||||
|
VM_setError(vm, "unknown opcode %02X", opcode);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
vm->current_pos++;
|
||||||
|
|
||||||
pos++;
|
i32 bytes_read = instr->implementation(vm);
|
||||||
i32 bytes_read = instr->implementation(vm, pos);
|
// internal error occured
|
||||||
if(bytes_read < 0)
|
if(bytes_read < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
pos += bytes_read;
|
if(vm->state == VMState_Exited)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(vm->state != VMState_Exited){
|
if(vm->state != VMState_Exited){
|
||||||
VM_setErrorMessage(vm, "[%p] unexpected end of program", (void*)pos);
|
VM_setError(vm, "unexpected end of the program");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +77,10 @@ i32 VM_executeProgram(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_setErrorMessage(vm, "[%p] unexpected end of data", (void*)vm->data_size);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
src/VM/VM.h
20
src/VM/VM.h
@@ -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;
|
||||||
@@ -31,6 +32,7 @@ typedef enum VMState {
|
|||||||
VMState_Initialized,
|
VMState_Initialized,
|
||||||
VMState_Executing,
|
VMState_Executing,
|
||||||
VMState_Exited,
|
VMState_Exited,
|
||||||
|
VMState_InternalError
|
||||||
} VMState;
|
} VMState;
|
||||||
|
|
||||||
typedef struct VM {
|
typedef struct VM {
|
||||||
@@ -45,27 +47,25 @@ typedef struct VM {
|
|||||||
};
|
};
|
||||||
|
|
||||||
VMState state;
|
VMState state;
|
||||||
char* error_message;
|
char* NULLABLE(error_message); // not null on if state == VMState_InternalError
|
||||||
|
|
||||||
u8* data;
|
u8* data;
|
||||||
size_t data_size;
|
size_t data_size;
|
||||||
|
size_t current_pos;
|
||||||
} VM;
|
} VM;
|
||||||
|
|
||||||
void VM_init(VM* vm);
|
void VM_init(VM* vm);
|
||||||
|
|
||||||
/// @brief Loads a program from the buffer.
|
/// @brief Loads a program from the buffer.
|
||||||
/// @param data buffer with full program code
|
/// @param data buffer starting with machine code
|
||||||
/// @param size size of the program in bytes
|
/// @param size size of the program in bytes
|
||||||
bool VM_loadProgram(VM* vm, u8* data, size_t size);
|
bool VM_setMemory(VM* vm, u8* data, size_t size);
|
||||||
|
|
||||||
/// @brief Executes the program loaded into VM.
|
/// @brief Executes the program loaded into VM.
|
||||||
/// @return program exit code or -1 on error (check vm.error_message)
|
/// @return program exit code or -1 on error (check vm.error_message)
|
||||||
i32 VM_executeProgram(VM* vm);
|
i32 VM_boot(VM* vm);
|
||||||
|
|
||||||
#define VM_setErrorMessage(V, FORMAT, ...) {\
|
|
||||||
char* buf = malloc(256);\
|
|
||||||
sprintf(buf, FORMAT, ##__VA_ARGS__);\
|
|
||||||
vm->error_message = buf;\
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
#define VM_setError(vm, format, ...) _VM_setError(vm, __func__, format ,##__VA_ARGS__)
|
||||||
|
void _VM_setError(VM* vm, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||||
|
|||||||
24
src/collections/Array.h
Normal file
24
src/collections/Array.h
Normal file
@@ -0,0 +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##_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;\
|
||||||
|
}
|
||||||
4
src/collections/List.c
Normal file
4
src/collections/List.c
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include "List.h"
|
||||||
|
|
||||||
|
List_define(u32);
|
||||||
|
List_define(u8);
|
||||||
75
src/collections/List.h
Normal file
75
src/collections/List.h
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../std.h"
|
||||||
|
|
||||||
|
// minimal max_len after initial (0)
|
||||||
|
#define __List_min_size 16
|
||||||
|
|
||||||
|
#define List_declare(T)\
|
||||||
|
typedef struct List_##T {\
|
||||||
|
T* data;\
|
||||||
|
u32 len;\
|
||||||
|
u32 max_len;\
|
||||||
|
} List_##T;\
|
||||||
|
\
|
||||||
|
static inline List_##T List_##T##_construct(T* data_ptr, u32 len, u32 max_len) {\
|
||||||
|
return (List_##T){ .data = data_ptr, .len = len, .max_len = max_len };\
|
||||||
|
}\
|
||||||
|
\
|
||||||
|
List_##T List_##T##_alloc(u32 initial_len);\
|
||||||
|
\
|
||||||
|
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)\
|
||||||
|
List_##T List_##T##_alloc(u32 initial_len){\
|
||||||
|
if(initial_len == 0)\
|
||||||
|
return List_##T##_construct((T*)NULL, 0, 0);\
|
||||||
|
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(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 > expanded_max_len){\
|
||||||
|
expanded_max_len *= 2;\
|
||||||
|
}\
|
||||||
|
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;\
|
||||||
|
}\
|
||||||
|
\
|
||||||
|
void List_##T##_push(List_##T* ptr, T value){\
|
||||||
|
T* empty_cell_ptr = List_##T##_expand(ptr, 1);\
|
||||||
|
*empty_cell_ptr = value;\
|
||||||
|
}\
|
||||||
|
\
|
||||||
|
void List_##T##_pushMany(List_##T* ptr, T* values, u32 count){\
|
||||||
|
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);
|
||||||
|
List_declare(u8);
|
||||||
52
src/compiler/AST.c
Normal file
52
src/compiler/AST.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "AST.h"
|
||||||
|
|
||||||
|
List_define(Argument);
|
||||||
|
List_define(Operation);
|
||||||
|
List_define(DataDefinition);
|
||||||
|
List_define(Section);
|
||||||
|
|
||||||
|
static str _ArgumentType_str[] = {
|
||||||
|
STR("Unset"),
|
||||||
|
STR("Register"),
|
||||||
|
STR("ConstValue"),
|
||||||
|
STR("VarDataName"),
|
||||||
|
STR("ConstDataPointer"),
|
||||||
|
STR("ConstDataSize"),
|
||||||
|
};
|
||||||
|
|
||||||
|
str ArgumentType_toString(ArgumentType t){
|
||||||
|
if(t >= ARRAY_SIZE(_ArgumentType_str))
|
||||||
|
return STR("!!ArgumentType INDEX_ERROR!!");
|
||||||
|
return _ArgumentType_str[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Section_init(Section* sec, str name){
|
||||||
|
sec->name = name;
|
||||||
|
sec->data = List_DataDefinition_alloc(256);
|
||||||
|
sec->code = List_Operation_alloc(1024);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Section_free(Section* sec){
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AST_init(AST* ast){
|
||||||
|
ast->sections = List_Section_alloc(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AST_free(AST* ast){
|
||||||
|
for(u32 i = 0; i != ast->sections.len; i++){
|
||||||
|
Section_free(&ast->sections.data[i]);
|
||||||
|
}
|
||||||
|
free(ast->sections.data);
|
||||||
|
}
|
||||||
65
src/compiler/AST.h
Normal file
65
src/compiler/AST.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"
|
||||||
|
|
||||||
|
typedef enum ArgumentType {
|
||||||
|
ArgumentType_Unset,
|
||||||
|
ArgumentType_Register,
|
||||||
|
ArgumentType_ConstValue,
|
||||||
|
ArgumentType_VarDataName,
|
||||||
|
ArgumentType_ConstDataPointer,
|
||||||
|
ArgumentType_ConstDataSize,
|
||||||
|
} ArgumentType;
|
||||||
|
|
||||||
|
str ArgumentType_toString(ArgumentType t);
|
||||||
|
|
||||||
|
typedef struct Argument {
|
||||||
|
ArgumentType type;
|
||||||
|
union {
|
||||||
|
i64 i;
|
||||||
|
f64 f;
|
||||||
|
str data_name;
|
||||||
|
RegisterCode register_code;
|
||||||
|
} value;
|
||||||
|
} Argument;
|
||||||
|
|
||||||
|
List_declare(Argument);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Operation {
|
||||||
|
List_Argument args;
|
||||||
|
Opcode opcode;
|
||||||
|
} Operation;
|
||||||
|
|
||||||
|
List_declare(Operation);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct DataDefinition {
|
||||||
|
str name;
|
||||||
|
List_u8 data;
|
||||||
|
u32 element_size;
|
||||||
|
} DataDefinition;
|
||||||
|
|
||||||
|
List_declare(DataDefinition);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Section {
|
||||||
|
str name;
|
||||||
|
List_DataDefinition data;
|
||||||
|
List_Operation code;
|
||||||
|
} Section;
|
||||||
|
|
||||||
|
List_declare(Section);
|
||||||
|
|
||||||
|
void Section_init(Section* Section, str name);
|
||||||
|
void Section_free(Section* Section);
|
||||||
|
|
||||||
|
typedef struct AST {
|
||||||
|
List_Section sections;
|
||||||
|
} AST;
|
||||||
|
|
||||||
|
void AST_init(AST* ast);
|
||||||
|
void AST_free(AST* 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);
|
||||||
365
src/compiler/Compiler.c
Normal file
365
src/compiler/Compiler.c
Normal file
@@ -0,0 +1,365 @@
|
|||||||
|
#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){
|
||||||
|
free(cmp->code.data);
|
||||||
|
free(cmp->tokens.data);
|
||||||
|
free(cmp->line_lengths.data);
|
||||||
|
AST_free(&cmp->ast);
|
||||||
|
BinaryObject_free(&cmp->binary);
|
||||||
|
}
|
||||||
|
|
||||||
|
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
||||||
|
u32 prev_lines_len = 0;
|
||||||
|
if(pos >= cmp->code.len)
|
||||||
|
return CodePos_create(0, 0);
|
||||||
|
|
||||||
|
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||||
|
u32 line_len = cmp->line_lengths.data[i];
|
||||||
|
if(prev_lines_len + line_len > pos)
|
||||||
|
return CodePos_create(i + 1, pos + 1 - prev_lines_len);
|
||||||
|
prev_lines_len += line_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CodePos_create(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
|
||||||
|
// happens at the end of file
|
||||||
|
if(cmp->pos >= cmp->code.len)
|
||||||
|
cmp->pos = cmp->code.len - 1;
|
||||||
|
char position_str[32];
|
||||||
|
CodePos code_pos = Compiler_getLineAndColumn(cmp, cmp->pos);
|
||||||
|
sprintf(position_str, "[at %u:%u][", code_pos.line, code_pos.column);
|
||||||
|
char* real_format = strcat_malloc(position_str, context, "] ", format);
|
||||||
|
va_list argv;
|
||||||
|
va_start(argv, format);
|
||||||
|
char* NULLABLE(buf) = vsprintf_malloc(512, real_format, argv);
|
||||||
|
va_end(argv);
|
||||||
|
free(real_format);
|
||||||
|
if(buf == NULL){
|
||||||
|
buf = malloc(16);
|
||||||
|
strcpy(buf, "SPRINTF FAILED");
|
||||||
|
}
|
||||||
|
cmp->state = CompilerState_Error;
|
||||||
|
cmp->error_message = buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define setError(FORMAT, ...) {\
|
||||||
|
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
|
||||||
|
}
|
||||||
|
|
||||||
|
str Compiler_constructTokenStr(Compiler* cmp, Token t){
|
||||||
|
str s = str_construct((char*)(cmp->code.data + t.begin), t.length, false);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, bool debug_log){
|
||||||
|
FILE* f = fopen(source_file_name, "rb");
|
||||||
|
if(f == NULL)
|
||||||
|
returnError("ERROR: can't open file '%s'", source_file_name);
|
||||||
|
|
||||||
|
StringBuilder sb = StringBuilder_alloc(64 * 1024);
|
||||||
|
int ret;
|
||||||
|
while((ret = fgetc(f)) != EOF) {
|
||||||
|
StringBuilder_append_char(&sb, ret);
|
||||||
|
}
|
||||||
|
if(ferror(f)){
|
||||||
|
StringBuilder_free(&sb);
|
||||||
|
fclose(f);
|
||||||
|
returnError("can't read file '%s'", source_file_name);
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
if(sb.buffer.len == 0){
|
||||||
|
StringBuilder_free(&sb);
|
||||||
|
returnError("soucre file is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->code = str_copy(StringBuilder_getStr(&sb));
|
||||||
|
StringBuilder_free(&sb);
|
||||||
|
|
||||||
|
f = fopen(out_file_name, "wb");
|
||||||
|
if(f == NULL){
|
||||||
|
returnError("ERROR: can't open file '%s'", out_file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(debug_log){
|
||||||
|
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");
|
||||||
|
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];
|
||||||
|
CodePos pos = Compiler_getLineAndColumn(cmp, t.begin);
|
||||||
|
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_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(debug_log)
|
||||||
|
printf("==================================[compiling]==================================\n");
|
||||||
|
success = writeBinaryFile(cmp, f);
|
||||||
|
fclose(f);
|
||||||
|
if(success){
|
||||||
|
cmp->state = CompilerState_Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
43
src/compiler/Compiler.h
Normal file
43
src/compiler/Compiler.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
|
#include "../collections/List.h"
|
||||||
|
#include "../collections/HashMap.h"
|
||||||
|
#include "Token.h"
|
||||||
|
#include "Binary.h"
|
||||||
|
|
||||||
|
typedef enum CompilerState {
|
||||||
|
CompilerState_Initial,
|
||||||
|
CompilerState_Lexing,
|
||||||
|
CompilerState_Parsing,
|
||||||
|
CompilerState_Compiling,
|
||||||
|
CompilerState_Error,
|
||||||
|
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);
|
||||||
|
void Compiler_free(Compiler* cmp);
|
||||||
|
|
||||||
|
/// @brief compile assembly language code to machine code
|
||||||
|
/// @return true if no errors, false if any error occured (check cmp->error_message)
|
||||||
|
bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, bool debug);
|
||||||
30
src/compiler/Compiler_internal.h
Normal file
30
src/compiler/Compiler_internal.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include "Compiler.h"
|
||||||
|
#include "../string/StringBuilder.h"
|
||||||
|
|
||||||
|
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||||
|
|
||||||
|
#define Compiler_setError(cmp, format, ...) _Compiler_setError(cmp, __func__, format ,##__VA_ARGS__)
|
||||||
|
|
||||||
|
#define returnError(FORMAT, ...) {\
|
||||||
|
setError(FORMAT, ##__VA_ARGS__);\
|
||||||
|
return false;\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define returnErrorIf(STATEMENT, FORMAT, ...) if(STATEMENT) returnError(FORMAT, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
#define returnErrorIf_auto(STATEMENT) returnErrorIf(STATEMENT, #STATEMENT)
|
||||||
|
|
||||||
|
typedef struct CodePos {
|
||||||
|
u32 line; // 0 on error
|
||||||
|
u32 column; // 0 on error
|
||||||
|
} CodePos;
|
||||||
|
|
||||||
|
#define CodePos_create(L, C) ((CodePos){ .line = L, .column = C })
|
||||||
|
|
||||||
|
/// @param pos index in code buffer
|
||||||
|
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos);
|
||||||
|
|
||||||
|
str Compiler_constructTokenStr(Compiler* cmp, Token t);
|
||||||
|
|
||||||
|
bool Compiler_lex(Compiler* cmp);
|
||||||
|
bool Compiler_parse(Compiler* cmp);
|
||||||
269
src/compiler/Lexer.c
Normal file
269
src/compiler/Lexer.c
Normal file
@@ -0,0 +1,269 @@
|
|||||||
|
#include "Compiler_internal.h"
|
||||||
|
|
||||||
|
#define setError(FORMAT, ...) {\
|
||||||
|
completeLine(cmp);\
|
||||||
|
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define Error_unexpectedCharacter(C) "unexpected character '%c'", C
|
||||||
|
#define Error_endOfFile "unexpected end of file"
|
||||||
|
|
||||||
|
static void completeLine(Compiler* cmp){
|
||||||
|
List_u32_push(&cmp->line_lengths, cmp->column);
|
||||||
|
cmp->column = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readCommentSingleLine(Compiler* cmp){
|
||||||
|
char c; // '/'
|
||||||
|
Token tok = Token_construct(TokenType_SingleLineComment, cmp->pos - 1, 0);
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
c = cmp->code.data[cmp->pos];
|
||||||
|
// end of line
|
||||||
|
if(c == '\r' || c == '\n'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
// cmp->line will be increased in lex()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of file
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readCommentMultiLine(Compiler* cmp){
|
||||||
|
char c; // '*'
|
||||||
|
Token tok = Token_construct(TokenType_MultiLineComment, cmp->pos - 1, 0);
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
c = cmp->code.data[cmp->pos];
|
||||||
|
// closing comment
|
||||||
|
if(cmp->pos > tok.begin + 3 && c == '/' && cmp->code.data[cmp->pos - 1] == '*') {
|
||||||
|
tok.length = cmp->pos - tok.begin + 1;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c == '\n')
|
||||||
|
completeLine(cmp);
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of file
|
||||||
|
setError(Error_endOfFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readComment(Compiler* cmp){
|
||||||
|
char c; // '/'
|
||||||
|
if(cmp->pos + 1 == cmp->code.len){
|
||||||
|
setError(Error_endOfFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = cmp->code.data[cmp->pos + 1];
|
||||||
|
if(c == '\r' || c == '\n'){
|
||||||
|
setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->pos++;
|
||||||
|
cmp->column++;
|
||||||
|
if(c == '/')
|
||||||
|
readCommentSingleLine(cmp);
|
||||||
|
else if(c == '*')
|
||||||
|
readCommentMultiLine(cmp);
|
||||||
|
else setError(Error_unexpectedCharacter(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readLabel(Compiler* cmp){
|
||||||
|
char c; // '.'
|
||||||
|
cmp->pos++;
|
||||||
|
cmp->column++;
|
||||||
|
Token tok = Token_construct(TokenType_Label, cmp->pos, 0);
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
c = cmp->code.data[cmp->pos];
|
||||||
|
// end of line
|
||||||
|
if(c == ':' || c == '\r' || c == '\n'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
if(tok.length > 0)
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
else setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
|
||||||
|
// cmp->line will be increased in lex()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c) &&
|
||||||
|
c != '_' && c != '.'){
|
||||||
|
setError(Error_unexpectedCharacter(c));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of file
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
if(tok.length > 0)
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
else setError(Error_endOfFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readArguments(Compiler* cmp){
|
||||||
|
char c; // space
|
||||||
|
Token tok = Token_construct(TokenType_Unset, cmp->pos, 0);
|
||||||
|
char quot = '\0'; // quotation character of a string value
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
c = cmp->code.data[cmp->pos];
|
||||||
|
|
||||||
|
// string argument reading
|
||||||
|
if(quot != '\0'){
|
||||||
|
if(c == quot && cmp->code.data[cmp->pos - 1] != '\\'){
|
||||||
|
quot = '\0';
|
||||||
|
}
|
||||||
|
else if(c == '\r' || c == '\n'){
|
||||||
|
setError("line end reached but string hasn't been closed yet");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of operation
|
||||||
|
else if(c == '\r' || c == '\n' || c == ';'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
if(tok.length > 0)
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
// cmp->line will be increased in lex()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// new argument begins
|
||||||
|
else if(c == ' ' || c == '\t'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
if(tok.length > 0)
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
tok = Token_construct(TokenType_Unset, cmp->pos + 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(tok.type == TokenType_Unset){
|
||||||
|
if(c == '\''){
|
||||||
|
tok.type = TokenType_Char;
|
||||||
|
quot = c;
|
||||||
|
}
|
||||||
|
else if(c == '"'){
|
||||||
|
tok.type = TokenType_String;
|
||||||
|
quot = c;
|
||||||
|
}
|
||||||
|
else if(c == '@')
|
||||||
|
tok.type = TokenType_NamedDataPointer;
|
||||||
|
else if(c == '#')
|
||||||
|
tok.type = TokenType_NamedDataSize;
|
||||||
|
else if(isDigit(c))
|
||||||
|
tok.type = TokenType_Number;
|
||||||
|
else tok.type = TokenType_Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of file
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
if(tok.length > 0)
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readInstruction(Compiler* cmp){
|
||||||
|
Token tok = Token_construct(TokenType_Instruction, cmp->pos, 0);
|
||||||
|
cmp->pos++;
|
||||||
|
cmp->column++;
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
char c = cmp->code.data[cmp->pos];
|
||||||
|
// end of line
|
||||||
|
if(c == '\r' || c == '\n' || c == ';'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
// cmp->line will be increased in lex()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// arguments begin
|
||||||
|
if(c == ' ' || c == '\t'){
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
readArguments(cmp);
|
||||||
|
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c)){
|
||||||
|
setError(Error_unexpectedCharacter(c));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of file
|
||||||
|
tok.length = cmp->pos - tok.begin;
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
|
||||||
|
List_Token_push(&cmp->tokens, tok);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Compiler_lex(Compiler* cmp){
|
||||||
|
returnErrorIf_auto(cmp->state != CompilerState_Initial);
|
||||||
|
cmp->state = CompilerState_Lexing;
|
||||||
|
cmp->column = 1;
|
||||||
|
|
||||||
|
while(cmp->pos < cmp->code.len){
|
||||||
|
char c = cmp->code.data[cmp->pos];
|
||||||
|
switch(c){
|
||||||
|
// skip blank characters
|
||||||
|
case ' ': case '\t': case '\r': case '\n':
|
||||||
|
break;
|
||||||
|
// try read comment
|
||||||
|
case '/':
|
||||||
|
readComment(cmp);
|
||||||
|
break;
|
||||||
|
// try read label
|
||||||
|
case '.':
|
||||||
|
readLabel(cmp);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// try read instruction
|
||||||
|
if(isAlphabeticalLower(c) || isAlphabeticalUpper(c))
|
||||||
|
readInstruction(cmp);
|
||||||
|
else returnError(Error_unexpectedCharacter(c));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmp->state == CompilerState_Error)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
c = cmp->code.data[cmp->pos];
|
||||||
|
if(c == '\n')
|
||||||
|
completeLine(cmp);
|
||||||
|
cmp->column++;
|
||||||
|
cmp->pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
completeLine(cmp);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
281
src/compiler/Parser.c
Normal file
281
src/compiler/Parser.c
Normal file
@@ -0,0 +1,281 @@
|
|||||||
|
#include "Compiler_internal.h"
|
||||||
|
|
||||||
|
#define setError(FORMAT, ...) {\
|
||||||
|
cmp->pos = cmp->tokens.data[cmp->tok_i].begin;\
|
||||||
|
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define setError_unexpectedToken(T) {\
|
||||||
|
str tok_str = str_copy(Compiler_constructTokenStr(cmp, T));\
|
||||||
|
cmp->pos = T.begin;\
|
||||||
|
Compiler_setError(cmp, "unexpected token '%s'", tok_str.data);\
|
||||||
|
free(tok_str.data);\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define setError_unexpectedTokenChar(T, I) {\
|
||||||
|
cmp->pos = T.begin + I;\
|
||||||
|
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"
|
||||||
|
|
||||||
|
static void List_u8_pushBytes(List_u8* l, void* value, u32 startIndex, u32 count){
|
||||||
|
u8* v = value;
|
||||||
|
for(u32 byte_i = startIndex; byte_i < startIndex + count; byte_i++){
|
||||||
|
List_u8_push(l, v[byte_i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
char c;
|
||||||
|
bool escaped = false;
|
||||||
|
for(u32 i = 0; i < src.len; i++){
|
||||||
|
c = src.data[i];
|
||||||
|
if(c == '\\'){
|
||||||
|
escaped = !escaped;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!escaped){
|
||||||
|
StringBuilder_append_char(&sb, c);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// escape codes
|
||||||
|
switch(c){
|
||||||
|
case '0':
|
||||||
|
StringBuilder_append_char(&sb, '\0');
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
StringBuilder_append_char(&sb, '\n');
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
StringBuilder_append_char(&sb, '\r');
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
StringBuilder_append_char(&sb, '\t');
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
StringBuilder_append_char(&sb, '\e');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
||||||
|
StringBuilder_free(&sb);
|
||||||
|
return str_null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return StringBuilder_getStr(&sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* ddf){
|
||||||
|
i32 _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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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_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(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, 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){
|
||||||
|
returnErrorIf_auto(cmp->state != CompilerState_Lexing);
|
||||||
|
cmp->state = CompilerState_Parsing;
|
||||||
|
Token tok;
|
||||||
|
Section* sec = NULL;
|
||||||
|
|
||||||
|
while(cmp->tok_i < cmp->tokens.len){
|
||||||
|
tok = cmp->tokens.data[cmp->tok_i];
|
||||||
|
switch(tok.type){
|
||||||
|
case TokenType_Unset:
|
||||||
|
returnError(Error_TokenUnset);
|
||||||
|
case TokenType_SingleLineComment:
|
||||||
|
case TokenType_MultiLineComment:
|
||||||
|
// skip comments
|
||||||
|
break;
|
||||||
|
case TokenType_Label:
|
||||||
|
// create new section
|
||||||
|
sec = List_Section_expand(&cmp->ast.sections, 1);
|
||||||
|
Section_init(sec, Compiler_constructTokenStr(cmp, tok));
|
||||||
|
break;
|
||||||
|
case TokenType_Instruction:
|
||||||
|
if(sec == NULL)
|
||||||
|
returnError("no section");
|
||||||
|
str instr_name = Compiler_constructTokenStr(cmp, tok);
|
||||||
|
// data definition starts with const
|
||||||
|
if(str_startsWith(instr_name, STR("const"))){
|
||||||
|
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
||||||
|
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Operation* operPtr = List_Operation_expand(&sec->code, 1);
|
||||||
|
parseOperation(cmp, instr_name, operPtr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
setError_unexpectedToken(tok);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmp->state == CompilerState_Error)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
cmp->tok_i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
24
src/compiler/Token.c
Normal file
24
src/compiler/Token.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include "Token.h"
|
||||||
|
|
||||||
|
List_define(Token);
|
||||||
|
|
||||||
|
static str _TokenType_str[] = {
|
||||||
|
STR("Unset"),
|
||||||
|
STR("SingleLineComment"),
|
||||||
|
STR("MultiLineComment"),
|
||||||
|
STR("Instruction"),
|
||||||
|
STR("Label"),
|
||||||
|
STR("Number"),
|
||||||
|
STR("Char"),
|
||||||
|
STR("String"),
|
||||||
|
STR("Name"),
|
||||||
|
STR("NamedDataPointer"),
|
||||||
|
STR("NamedDataSize"),
|
||||||
|
STR("OperationEnd"),
|
||||||
|
};
|
||||||
|
|
||||||
|
str TokenType_toString(TokenType t){
|
||||||
|
if(t >= ARRAY_SIZE(_TokenType_str))
|
||||||
|
return STR("!!TokenType INDEX_ERROR!!");
|
||||||
|
return _TokenType_str[t];
|
||||||
|
}
|
||||||
31
src/compiler/Token.h
Normal file
31
src/compiler/Token.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../std.h"
|
||||||
|
#include "../string/str.h"
|
||||||
|
#include "../collections/List.h"
|
||||||
|
|
||||||
|
typedef enum TokenType {
|
||||||
|
TokenType_Unset, // initial value
|
||||||
|
TokenType_SingleLineComment, // //comment
|
||||||
|
TokenType_MultiLineComment, // /* comment */
|
||||||
|
TokenType_Instruction, // abc
|
||||||
|
TokenType_Label, // .abc:
|
||||||
|
TokenType_Number, // 0123
|
||||||
|
TokenType_Char, // 'A'
|
||||||
|
TokenType_String, // "aaaa"
|
||||||
|
TokenType_Name, // xyz
|
||||||
|
TokenType_NamedDataPointer, // @xyz
|
||||||
|
TokenType_NamedDataSize, // #xyz
|
||||||
|
TokenType_OperationEnd, // EOL or EOF or ;
|
||||||
|
} TokenType;
|
||||||
|
|
||||||
|
str TokenType_toString(TokenType t);
|
||||||
|
|
||||||
|
typedef struct Token {
|
||||||
|
u32 begin; // some index in Compiler->code
|
||||||
|
u32 length : 24; // length in characters (24 bits)
|
||||||
|
TokenType type : 8; // type of token (8 bits)
|
||||||
|
} Token;
|
||||||
|
|
||||||
|
List_declare(Token);
|
||||||
|
|
||||||
|
#define Token_construct(TYPE, BEGIN, LEN) ((Token){ .type = TYPE, .begin = BEGIN, .length = LEN })
|
||||||
52
src/cstr.c
Normal file
52
src/cstr.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "std.h"
|
||||||
|
|
||||||
|
char* _strcat_malloc(size_t n, cstr str0, ...){
|
||||||
|
va_list argv;
|
||||||
|
va_start(argv, str0);
|
||||||
|
char* heap_ptr = _vstrcat_malloc(n, str0, argv);
|
||||||
|
va_end(argv);
|
||||||
|
return heap_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv){
|
||||||
|
size_t str0_len = strlen(str0);
|
||||||
|
size_t total_len = str0_len;
|
||||||
|
cstr* const parts = malloc(sizeof(cstr) * n);
|
||||||
|
size_t* const part_lengths = malloc(sizeof(size_t) * n);
|
||||||
|
for(size_t i = 0; i < n; i++){
|
||||||
|
cstr part = va_arg(argv, cstr);
|
||||||
|
size_t length = strlen(part);
|
||||||
|
parts[i] = part;
|
||||||
|
part_lengths[i] = length;
|
||||||
|
total_len += length;
|
||||||
|
}
|
||||||
|
char* const buf = malloc(total_len + 1);
|
||||||
|
memcpy(buf, str0, str0_len);
|
||||||
|
char* walking_ptr = buf + str0_len;
|
||||||
|
for(size_t i = 0; i < n; i++){
|
||||||
|
memcpy(walking_ptr, parts[i], part_lengths[i]);
|
||||||
|
walking_ptr += part_lengths[i];
|
||||||
|
}
|
||||||
|
buf[total_len] = '\0';
|
||||||
|
free(parts);
|
||||||
|
free(part_lengths);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
||||||
|
va_list argv;
|
||||||
|
va_start(argv, format);
|
||||||
|
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
|
||||||
|
va_end(argv);
|
||||||
|
return heap_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||||
|
char* buf = malloc(buffer_size);
|
||||||
|
int r = vsprintf(buf, format, argv);
|
||||||
|
if(r < 0){
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
/// EXIT
|
/// EXIT
|
||||||
/// ax - exit code
|
/// ax - exit code
|
||||||
i32 EXIT_impl(VM* vm, size_t pos){
|
i32 EXIT_impl(VM* vm){
|
||||||
vm->state = VMState_Exited;
|
vm->state = VMState_Exited;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
/// MOV [dst_register] [src_register]
|
/// MOV [dst_register] [src_register]
|
||||||
i32 MOV_impl(VM* vm, size_t pos){
|
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_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, src_register_i);
|
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
/// NOP
|
/// NOP
|
||||||
i32 NOP_impl(VM* vm, size_t pos){
|
i32 NOP_impl(VM* vm){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
/// PUSH [dst_register] [value_size] [value]
|
/// PUSH [dst_register] [value_size] [value]
|
||||||
i32 PUSH_impl(VM* vm, size_t pos){
|
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, pos, value_size))
|
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
||||||
return -1;
|
return -1;
|
||||||
|
vm->current_pos += value_size;
|
||||||
|
|
||||||
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,66 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
FILE* fileFromN(VM* vm, size_t pos, u32 file_n){
|
FILE* NULLABLE(fileFromN)(VM* vm, u32 file_n){
|
||||||
FILE* f = NULL;
|
FILE* f = NULL;
|
||||||
switch(file_n){
|
switch(file_n){
|
||||||
case 0: f = stdin; break;
|
case 0: f = stdin; break;
|
||||||
case 1: f = stdout; break;
|
case 1: f = stdout; break;
|
||||||
case 2: f = stderr; break;
|
case 2: f = stderr; break;
|
||||||
default:
|
default:
|
||||||
VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n);
|
VM_setError(vm, "invalid file_n (%x) ", file_n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SYS
|
|
||||||
/// ax - func code
|
|
||||||
i32 SYS_impl(VM* vm, size_t pos){
|
|
||||||
u8 func_code = vm->ax.u8v0;
|
|
||||||
size_t result_code = 0;
|
|
||||||
switch(func_code){
|
|
||||||
// sys_read
|
// sys_read
|
||||||
// bx - file n
|
// bx - file n
|
||||||
// cx - buffer ptr
|
// cx - buffer ptr
|
||||||
// dx - buffer size
|
// dx - buffer size
|
||||||
case 0:;
|
i32 SYS_read(VM* vm){
|
||||||
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
const u32 file_n = vm->bx.u32v;
|
||||||
break;
|
u8* const buf = vm->data + vm->cx.u32v;
|
||||||
|
const u32 size = vm->dx.u32v;
|
||||||
|
|
||||||
|
if(buf + size > vm->data + vm->data_size)
|
||||||
|
return 40;
|
||||||
|
|
||||||
|
FILE* f = fileFromN(vm, file_n);
|
||||||
|
return fread(buf, 1, size, f);
|
||||||
|
}
|
||||||
|
|
||||||
// sys_write
|
// sys_write
|
||||||
// bx - file n
|
// bx - file n
|
||||||
// cx - buffer ptr
|
// cx - buffer ptr
|
||||||
// dx - buffer size
|
// dx - buffer size
|
||||||
|
i32 SYS_write(VM* vm){
|
||||||
|
const u32 file_n = vm->bx.u32v;
|
||||||
|
u8* const buf = vm->data + vm->cx.u32v;
|
||||||
|
const u32 size = vm->dx.u32v;
|
||||||
|
|
||||||
|
if(buf + size > vm->data + vm->data_size)
|
||||||
|
return 41;
|
||||||
|
|
||||||
|
FILE* f = fileFromN(vm, file_n);
|
||||||
|
return fwrite(buf, 1, size, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SYS
|
||||||
|
/// before call: ax - func code
|
||||||
|
/// after call: ax - result code
|
||||||
|
i32 SYS_impl(VM* vm){
|
||||||
|
u8 func_code = vm->ax.u8v0;
|
||||||
|
u32 result_code = 0;
|
||||||
|
switch(func_code){
|
||||||
|
case 0:
|
||||||
|
result_code = SYS_read(vm);
|
||||||
|
break;
|
||||||
case 1:;
|
case 1:;
|
||||||
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
result_code = SYS_write(vm);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code);
|
VM_setError(vm, "invalid system call (%x) ", func_code);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../instructions.h"
|
#include "../instructions.h"
|
||||||
|
#include "../registers.h"
|
||||||
|
|
||||||
#define readVar(VAR) {\
|
#define readVar(VAR) {\
|
||||||
if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \
|
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
||||||
return -1;\
|
return -1;\
|
||||||
pos += sizeof(VAR);\
|
vm->current_pos += sizeof(VAR);\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define validateRegisterIndex(VAR) {\
|
#define validateRegisterIndex(VAR) {\
|
||||||
if(VAR> sizeof(vm->registers)){\
|
if(VAR> sizeof(vm->registers)){\
|
||||||
VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\
|
VM_setError(vm, "invalid register index (%x)", VAR);\
|
||||||
return -1;\
|
return -1;\
|
||||||
}\
|
}\
|
||||||
}
|
}
|
||||||
|
|
||||||
#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_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\
|
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
||||||
return -1;\
|
return -1;\
|
||||||
}\
|
}\
|
||||||
}
|
}
|
||||||
@@ -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: \
|
||||||
@@ -22,26 +23,26 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// ADD [dst_register] [src_register]
|
/// ADD [dst_register] [src_register]
|
||||||
i32 ADD_impl(VM* vm, size_t pos){
|
i32 ADD_impl(VM* vm){
|
||||||
mathOperatorImpl(+);
|
mathOperatorImpl(+);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SUB [dst_register] [src_register]
|
/// SUB [dst_register] [src_register]
|
||||||
i32 SUB_impl(VM* vm, size_t pos){
|
i32 SUB_impl(VM* vm){
|
||||||
mathOperatorImpl(-);
|
mathOperatorImpl(-);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// MUL [dst_register] [src_register]
|
/// MUL [dst_register] [src_register]
|
||||||
i32 MUL_impl(VM* vm, size_t pos){
|
i32 MUL_impl(VM* vm){
|
||||||
mathOperatorImpl(*)
|
mathOperatorImpl(*)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// DIV [dst_register] [src_register]
|
/// DIV [dst_register] [src_register]
|
||||||
i32 DIV_impl(VM* vm, size_t pos){
|
i32 DIV_impl(VM* vm){
|
||||||
mathOperatorImpl(/)
|
mathOperatorImpl(/)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// MOD [dst_register] [src_register]
|
/// MOD [dst_register] [src_register]
|
||||||
i32 MOD_impl(VM* vm, size_t pos){
|
i32 MOD_impl(VM* vm){
|
||||||
mathOperatorImpl(%)
|
mathOperatorImpl(%)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,21 @@
|
|||||||
#include "instructions.h"
|
#include "instructions.h"
|
||||||
|
#include "../collections/HashMap.h"
|
||||||
|
|
||||||
const Instruction instructions[] = {
|
i32 NOP_impl(VM* vm);
|
||||||
|
i32 PUSH_impl(VM* vm);
|
||||||
|
i32 MOV_impl(VM* vm);
|
||||||
|
i32 ADD_impl(VM* vm);
|
||||||
|
i32 SUB_impl(VM* vm);
|
||||||
|
i32 MUL_impl(VM* vm);
|
||||||
|
i32 DIV_impl(VM* vm);
|
||||||
|
i32 MOD_impl(VM* vm);
|
||||||
|
i32 SYS_impl(VM* vm);
|
||||||
|
i32 EXIT_impl(VM* vm);
|
||||||
|
i32 JMP_impl(VM* vm);
|
||||||
|
i32 CALL_impl(VM* vm);
|
||||||
|
|
||||||
|
Array_declare(Instruction);
|
||||||
|
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),
|
||||||
@@ -13,12 +28,38 @@ const Instruction instructions[] = {
|
|||||||
Instruction_construct(EXIT),
|
Instruction_construct(EXIT),
|
||||||
// Instruction_construct(JMP),
|
// Instruction_construct(JMP),
|
||||||
// Instruction_construct(CALL),
|
// Instruction_construct(CALL),
|
||||||
};
|
});
|
||||||
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
|
||||||
|
|
||||||
const Instruction* Instruction_getFromOpcode(u8 opcode){
|
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
||||||
if(opcode >= instructions_count)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,32 +3,36 @@
|
|||||||
|
|
||||||
///@param program_pos position in vm->program next afrer opcode
|
///@param program_pos position in vm->program next afrer opcode
|
||||||
///@returns number of bytes read
|
///@returns number of bytes read
|
||||||
typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos);
|
typedef i32 (*InstructionImplFunc_t)(VM* vm);
|
||||||
|
|
||||||
|
typedef enum __attribute__((__packed__)) Opcode {
|
||||||
|
Opcode_NOP,
|
||||||
|
Opcode_PUSH,
|
||||||
|
Opcode_MOV,
|
||||||
|
Opcode_ADD,
|
||||||
|
Opcode_SUB,
|
||||||
|
Opcode_MUL,
|
||||||
|
Opcode_DIV,
|
||||||
|
Opcode_MOD,
|
||||||
|
Opcode_SYS,
|
||||||
|
Opcode_EXIT,
|
||||||
|
} Opcode;
|
||||||
|
|
||||||
typedef struct Instruction {
|
typedef struct Instruction {
|
||||||
const char* name;
|
str name;
|
||||||
InstructionImplFunc_t implementation;
|
InstructionImplFunc_t implementation;
|
||||||
|
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\
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief get instruction info from table
|
/// @brief get instruction info from table
|
||||||
/// @param opcode any byte
|
/// @param opcode any byte
|
||||||
/// @return ptr to struct or NULL
|
/// @return ptr to struct or NULL
|
||||||
const Instruction* Instruction_getFromOpcode(u8 opcode);
|
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
||||||
|
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
||||||
i32 NOP_impl(VM* vm, size_t pos);
|
void Instruction_freeSearchStructs();
|
||||||
i32 PUSH_impl(VM* vm, size_t pos);
|
|
||||||
i32 MOV_impl(VM* vm, size_t pos);
|
|
||||||
i32 ADD_impl(VM* vm, size_t pos);
|
|
||||||
i32 SUB_impl(VM* vm, size_t pos);
|
|
||||||
i32 MUL_impl(VM* vm, size_t pos);
|
|
||||||
i32 DIV_impl(VM* vm, size_t pos);
|
|
||||||
i32 MOD_impl(VM* vm, size_t pos);
|
|
||||||
i32 SYS_impl(VM* vm, size_t pos);
|
|
||||||
i32 EXIT_impl(VM* vm, size_t pos);
|
|
||||||
i32 JMP_impl(VM* vm, size_t pos);
|
|
||||||
i32 CALL_impl(VM* vm, size_t pos);
|
|
||||||
|
|||||||
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);
|
||||||
133
src/main.c
133
src/main.c
@@ -1,62 +1,118 @@
|
|||||||
#include "VM/VM.h"
|
#include "VM/VM.h"
|
||||||
#include "instructions/instructions.h"
|
#include "instructions/instructions.h"
|
||||||
|
#include "collections/List.h"
|
||||||
|
#include "compiler/Compiler.h"
|
||||||
|
|
||||||
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
||||||
|
|
||||||
i32 main(const i32 argc, const char** argv){
|
i32 compileSources(cstr source_file, cstr out_file, bool debug_log);
|
||||||
const char* filename = NULL;
|
i32 bootFromImage(cstr image_file);
|
||||||
|
|
||||||
|
i32 main(const i32 argc, cstr* argv){
|
||||||
|
if(argc < 2){
|
||||||
|
printfe("ERROR: no arguments provided. Use --help to know more.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool boot = false;
|
||||||
|
cstr NULLABLE(image_file) = NULL;
|
||||||
|
|
||||||
|
bool compile = false;
|
||||||
|
cstr NULLABLE(out_file) = NULL;
|
||||||
|
cstr NULLABLE(source_file) = NULL;
|
||||||
|
|
||||||
|
bool debug_log = false;
|
||||||
|
|
||||||
for(i32 argi = 1; argi < argc; argi++){
|
for(i32 argi = 1; argi < argc; argi++){
|
||||||
if(arg_is("-h") || arg_is("--help")){
|
if(arg_is("-h") || arg_is("--help")){
|
||||||
printf(
|
printf(
|
||||||
"-h, --help Show this message\n"
|
"-h, --help Show this message.\n"
|
||||||
"-op, --opcodes Shows list of all instructions.\n"
|
"-op, --opcodes Show list of all instructions.\n"
|
||||||
"-i, --image [FILE] Boot VM using image file\n"
|
"-i, --image [FILE] Boot VM using image file.\n"
|
||||||
|
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
|
||||||
|
"-d, --debug Enable debug log.\n"
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if(arg_is("-op") || arg_is("--opcodes")){
|
else if(arg_is("-op") || arg_is("--opcodes")){
|
||||||
for(u8 opcode = 0; opcode < 255; opcode++){
|
for(u8 opcode = 0; opcode < 255; opcode++){
|
||||||
const Instruction* instr = Instruction_getFromOpcode(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;
|
||||||
}
|
}
|
||||||
else if(arg_is("-i") || arg_is("--image")){
|
else if(arg_is("-i") || arg_is("--image")){
|
||||||
if(++argi >= argc){
|
if(boot){
|
||||||
printfe("ERROR: no image file specified");
|
printfe("--image flag is set already\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
filename = argv[argi];
|
|
||||||
|
boot = true;
|
||||||
|
if(++argi >= argc){
|
||||||
|
printfe("ERROR: no image file specified\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
image_file = argv[argi];
|
||||||
|
}
|
||||||
|
else if(arg_is("-c") || arg_is("--compile")){
|
||||||
|
if(compile){
|
||||||
|
printfe("--compile flag is set already\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
compile = true;
|
||||||
|
if(++argi >= argc){
|
||||||
|
printfe("ERROR: no source file file specified\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
source_file = argv[argi];
|
||||||
|
|
||||||
|
if(++argi >= argc){
|
||||||
|
printfe("ERROR: no output file file specified\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
out_file = argv[argi];
|
||||||
|
}
|
||||||
|
else if(arg_is("-d") || arg_is("--debug")){
|
||||||
|
debug_log = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printfe("ERROR: unknown argument '%s'", argv[argi]);
|
printfe("ERROR: unknown argument '%s'\n", argv[argi]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(filename == NULL){
|
i32 exit_code = 0;
|
||||||
printfe("ERROR: no arguments provided. Use --help to know more.");
|
if(compile){
|
||||||
return 1;
|
exit_code = compileSources(source_file, out_file, debug_log);
|
||||||
|
}
|
||||||
|
if(exit_code == 0 && boot){
|
||||||
|
exit_code = bootFromImage(image_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* file = fopen(filename, "rb");
|
// frees global variables to supress valgrind memory leak errors
|
||||||
|
Instruction_freeSearchStructs();
|
||||||
|
return exit_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 bootFromImage(cstr image_file){
|
||||||
|
FILE* file = fopen(image_file, "rb");
|
||||||
if(file == NULL){
|
if(file == NULL){
|
||||||
printfe("ERROR: can't open file '%s'", filename);
|
printfe("ERROR: can't open file '%s'\n", image_file);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t buffer_size = 1024*1024;
|
const size_t buffer_size = 1024*1024;
|
||||||
u8* buffer = malloc(buffer_size);
|
u8* vm_memory = malloc(buffer_size);
|
||||||
memset(buffer, 0, buffer_size);
|
memset(vm_memory, 0, buffer_size);
|
||||||
|
|
||||||
size_t bytes_read = fread(buffer, 1, buffer_size, file);
|
size_t bytes_read = fread(vm_memory, 1, buffer_size, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if(bytes_read == (size_t)EOF){
|
if(bytes_read == (size_t)EOF){
|
||||||
printfe("ERROR: can't read file '%s'", filename);
|
printfe("ERROR: can't read file '%s'\n", image_file);
|
||||||
free(buffer);
|
free(vm_memory);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,14 +120,39 @@ i32 main(const i32 argc, const char** argv){
|
|||||||
VM_init(&vm);
|
VM_init(&vm);
|
||||||
|
|
||||||
i32 exit_code = 1;
|
i32 exit_code = 1;
|
||||||
if(VM_loadProgram(&vm, buffer, bytes_read)){
|
if(VM_setMemory(&vm, vm_memory, bytes_read)){
|
||||||
exit_code = VM_executeProgram(&vm);
|
exit_code = VM_boot(&vm);
|
||||||
}
|
}
|
||||||
if(vm.error_message != NULL){
|
if(vm.state == VMState_InternalError){
|
||||||
printfe("VM ERROR: %s", vm.error_message);
|
if(vm.error_message){
|
||||||
|
printfe("VM ERROR: %s\n", vm.error_message);
|
||||||
free(vm.error_message);
|
free(vm.error_message);
|
||||||
}
|
}
|
||||||
|
else printfe("VM ERROR: unknown (error_message is null)\n");
|
||||||
|
}
|
||||||
|
|
||||||
free(buffer);
|
if(exit_code != 0){
|
||||||
|
printfe("program exited with code %i\n", exit_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(vm_memory);
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i32 compileSources(cstr source_file, cstr out_file, bool debug_log){
|
||||||
|
Compiler cmp;
|
||||||
|
Compiler_init(&cmp);
|
||||||
|
bool success = Compiler_compile(&cmp, source_file, out_file, debug_log);
|
||||||
|
if(!success){
|
||||||
|
if(cmp.error_message){
|
||||||
|
printfe("COMPILER ERROR: %s\n", cmp.error_message);
|
||||||
|
free(cmp.error_message);
|
||||||
|
}
|
||||||
|
else printfe("COMPILER ERROR: unknown (error_message is null)\n");
|
||||||
|
Compiler_free(&cmp);
|
||||||
|
return 111;
|
||||||
|
}
|
||||||
|
|
||||||
|
Compiler_free(&cmp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
39
src/std.h
39
src/std.h
@@ -24,4 +24,43 @@ typedef u8 bool;
|
|||||||
#define true 1
|
#define true 1
|
||||||
#define false 0
|
#define false 0
|
||||||
|
|
||||||
|
typedef const char* cstr;
|
||||||
|
|
||||||
|
#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( \
|
||||||
|
a0, a1, a2, a3, a4, a5, a6, a7 , a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||||
|
a16,a17,a18,a19,a20,a21,a22,a23, a24,a25,a26,a27,a28,a29,a30,a31, \
|
||||||
|
a32,a33,a34,a35,a36,a37,a38,a39, a40,a41,a42,a43,a44,a45,a46,a47, \
|
||||||
|
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||||
|
a64,...) a64
|
||||||
|
// Macro for counting variadic arguments (max 64)
|
||||||
|
// (see usage in kprint.h)
|
||||||
|
#define count_args(ARGS...) __count_args(ARGS, \
|
||||||
|
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
|
||||||
|
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
|
||||||
|
32,31,30,29,28,27,26,25, 24,23,22,21,20,19,18,17, \
|
||||||
|
16,15,14,13,12,11,10,9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||||
|
|
||||||
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
|
||||||
|
|
||||||
|
/// @warning pointer can be null
|
||||||
|
#define NULLABLE(NAME) NAME
|
||||||
|
|
||||||
|
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
|
||||||
|
char* _strcat_malloc(size_t n, cstr str0, ...);
|
||||||
|
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv);
|
||||||
|
|
||||||
|
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) __attribute__((__format__(__printf__, 2, 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 isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
|
||||||
|
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
|
||||||
|
|||||||
53
src/string/StringBuilder.c
Normal file
53
src/string/StringBuilder.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#include "StringBuilder.h"
|
||||||
|
|
||||||
|
void StringBuilder_free(StringBuilder* b){
|
||||||
|
free(b->buffer.data);
|
||||||
|
b->buffer = List_u8_construct(NULL, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
str StringBuilder_getStr(StringBuilder* b){
|
||||||
|
List_u8_push(&b->buffer, '\0');
|
||||||
|
str result = str_construct((char*)b->buffer.data, b->buffer.len - 1, true);
|
||||||
|
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){
|
||||||
|
List_u8_push(&b->buffer, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void StringBuilder_append_string(StringBuilder* b, str s){
|
||||||
|
List_u8_pushMany(&b->buffer, (u8*)s.data, s.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
||||||
|
StringBuilder_append_string(b, str_construct(s, strlen(s), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, IFWIN("%lli", "%li"), n);
|
||||||
|
StringBuilder_append_cstr(b, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, IFWIN("%llu", "%lu"), n);
|
||||||
|
StringBuilder_append_cstr(b, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||||
|
char buf[32];
|
||||||
|
sprintf(buf, "%lf", n);
|
||||||
|
StringBuilder_append_cstr(b, buf);
|
||||||
|
}
|
||||||
25
src/string/StringBuilder.h
Normal file
25
src/string/StringBuilder.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../collections/List.h"
|
||||||
|
#include "str.h"
|
||||||
|
|
||||||
|
typedef struct StringBuilder {
|
||||||
|
List_u8 buffer;
|
||||||
|
} StringBuilder;
|
||||||
|
|
||||||
|
static inline StringBuilder StringBuilder_alloc(u32 initial_size) {
|
||||||
|
return (StringBuilder){ .buffer = List_u8_alloc(initial_size) };
|
||||||
|
}
|
||||||
|
void StringBuilder_free(StringBuilder* b);
|
||||||
|
|
||||||
|
/// @param count set to -1 to clear StringBuilder
|
||||||
|
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
||||||
|
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||||
|
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
||||||
|
void StringBuilder_append_string(StringBuilder* b, str s);
|
||||||
|
void StringBuilder_append_i64(StringBuilder* b, i64 a);
|
||||||
|
void StringBuilder_append_u64(StringBuilder* b, u64 a);
|
||||||
|
void StringBuilder_append_f64(StringBuilder* b, f64 a);
|
||||||
|
|
||||||
|
// adds '\0' to the buffer and returns pointer to buffer content
|
||||||
|
str StringBuilder_getStr(StringBuilder* b);
|
||||||
125
src/string/str.c
Normal file
125
src/string/str.c
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
#include "str.h"
|
||||||
|
|
||||||
|
str str_copy(str src){
|
||||||
|
if(src.data == NULL || src.len == 0)
|
||||||
|
return src;
|
||||||
|
|
||||||
|
str nstr = str_construct((char*)malloc(src.len + 1), src.len, true);
|
||||||
|
memcpy(nstr.data, src.data, src.len);
|
||||||
|
nstr.data[nstr.len] = '\0';
|
||||||
|
return nstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool str_equals(str s0, str s1){
|
||||||
|
if(s0.len != s1.len)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for(u32 i = 0; i < s0.len; i++)
|
||||||
|
if(s0.data[i] != s1.data[i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
str str_reverse(str s){
|
||||||
|
if(s.data == NULL || s.len == 0)
|
||||||
|
return s;
|
||||||
|
|
||||||
|
str r = str_construct(malloc(s.len), s.len, s.isZeroTerminated);
|
||||||
|
for(u32 i = 0; i < s.len; i++ )
|
||||||
|
r.data[i] = s.data[s.len - i - 1];
|
||||||
|
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;
|
||||||
|
}
|
||||||
41
src/string/str.h
Normal file
41
src/string/str.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../std.h"
|
||||||
|
|
||||||
|
typedef struct str {
|
||||||
|
char* data;
|
||||||
|
u32 len;
|
||||||
|
bool isZeroTerminated;
|
||||||
|
} 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 })
|
||||||
|
|
||||||
|
static const str str_null = str_construct(NULL, 0, 0);
|
||||||
|
|
||||||
|
/// copies src content to new string and adds \0 at the end
|
||||||
|
str str_copy(str src);
|
||||||
|
|
||||||
|
/// compares two strings, NullPtr-friendly
|
||||||
|
bool str_equals(str str0, str str1);
|
||||||
|
|
||||||
|
/// allocates new string which is reversed variant of <s>
|
||||||
|
str str_reverse(str s);
|
||||||
|
|
||||||
|
i32 str_seek(str src, str fragment, u32 startIndex);
|
||||||
|
i32 str_seekReverse(str src, str fragment, u32 startIndex);
|
||||||
|
|
||||||
|
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