Compare commits
No commits in common. "51ef24bb53dc0b410a4b3a3a4828d36c8400f3c0" and "2faff9198105a0c0f46b0c6dabfef34c627bad05" have entirely different histories.
51ef24bb53
...
2faff91981
@ -77,9 +77,7 @@ i32 VM_boot(VM* vm){
|
|||||||
|
|
||||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||||
if(pos + size >= vm->data_size){
|
if(pos + size >= vm->data_size){
|
||||||
VM_setError(vm,
|
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable",
|
||||||
"can't read " IFWIN("%lli", "%li") " bytes from 0x%x, because only "
|
|
||||||
IFWIN("%lli", "%li") " are avaliable",
|
|
||||||
size, (u32)pos, vm->data_size - size);
|
size, (u32)pos, vm->data_size - size);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,22 +30,17 @@
|
|||||||
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
|
u32 max_len = ALIGN_TO(initial_len, sizeof(void*)/sizeof(T));\
|
||||||
/* branchless version of max(max_len, __List_min_size) */\
|
/* branchless version of max(max_len, __List_min_size) */\
|
||||||
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
|
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
|
||||||
return List_##T##_construct((T*)malloc(max_len * sizeof(T)), 0, max_len);\
|
return List_##T##_construct((T*)malloc(initial_len * sizeof(T)), 0, max_len);\
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
T* List_##T##_expand(List_##T* ptr, u32 count){\
|
T* List_##T##_expand(List_##T* ptr, u32 count){\
|
||||||
u32 occupied_len = ptr->len;\
|
u32 occupied_len = ptr->len;\
|
||||||
u32 expanded_max_len = ptr->max_len;\
|
u32 expanded_max_len = ptr->max_len;\
|
||||||
expanded_max_len += (expanded_max_len < __List_min_size) * (__List_min_size - expanded_max_len);\
|
|
||||||
ptr->len += count;\
|
ptr->len += count;\
|
||||||
while(ptr->len > expanded_max_len){\
|
while(ptr->len > ptr->max_len){\
|
||||||
expanded_max_len *= 2;\
|
expanded_max_len *= 2;\
|
||||||
}\
|
}\
|
||||||
u32 alloc_size = expanded_max_len * sizeof(T);\
|
ptr->data = (T*)realloc(ptr->data, expanded_max_len * sizeof(T));\
|
||||||
if(ptr->data == NULL)\
|
|
||||||
ptr->data = (T*)malloc(alloc_size);\
|
|
||||||
else ptr->data = (T*)realloc(ptr->data, alloc_size);\
|
|
||||||
ptr->max_len = expanded_max_len;\
|
|
||||||
return ptr->data + occupied_len;\
|
return ptr->data + occupied_len;\
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
|
|||||||
@ -40,14 +40,7 @@ void Section_init(Section* sec, str name){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Section_free(Section* sec){
|
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);
|
free(sec->data.data);
|
||||||
|
|
||||||
for(u32 i = 0; i < sec->code.len; i++){
|
|
||||||
free(sec->code.data[i].args.data);
|
|
||||||
}
|
|
||||||
free(sec->code.data);
|
free(sec->code.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -98,21 +98,17 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(debug_log){
|
if(debug_log){
|
||||||
printf("===========================[%s]===========================\n", source_file_name);
|
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
|
||||||
fputs(cmp->code.data, stdout);
|
fputs(cmp->code.data, stdout);
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug_log)
|
|
||||||
printf("===================================[lexing]===================================\n");
|
|
||||||
bool success = Compiler_lex(cmp);
|
bool success = Compiler_lex(cmp);
|
||||||
|
|
||||||
if(debug_log){
|
if(debug_log){
|
||||||
printf("------------------------------------[lines]------------------------------------\n");
|
printf("------------------------------------[lines]-----------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||||
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("------------------------------------[tokens]-----------------------------------\n");
|
printf("------------------------------------[tokens]-----------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->tokens.len; i++){
|
for(u32 i = 0; i < cmp->tokens.len; i++){
|
||||||
Token t = cmp->tokens.data[i];
|
Token t = cmp->tokens.data[i];
|
||||||
@ -130,22 +126,17 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
free(tokstr);
|
free(tokstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!success){
|
if(!success){
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug_log)
|
|
||||||
printf("===================================[parsing]===================================\n");
|
|
||||||
success = Compiler_parse(cmp);
|
success = Compiler_parse(cmp);
|
||||||
if(!success){
|
if(!success){
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug_log)
|
|
||||||
printf("==================================[compiling]==================================\n");
|
|
||||||
success = compileFile(cmp, f);
|
success = compileFile(cmp, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if(success){
|
if(success){
|
||||||
|
|||||||
@ -89,7 +89,6 @@ static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* d
|
|||||||
}
|
}
|
||||||
free(_instr_name_zero_terminated.data);
|
free(_instr_name_zero_terminated.data);
|
||||||
ddf->element_size = _element_size_bits / 8;
|
ddf->element_size = _element_size_bits / 8;
|
||||||
ddf->data = List_u8_alloc(32);
|
|
||||||
|
|
||||||
Token tok = cmp->tokens.data[++cmp->tok_i];
|
Token tok = cmp->tokens.data[++cmp->tok_i];
|
||||||
if(tok.type != TokenType_Name){
|
if(tok.type != TokenType_Name){
|
||||||
@ -166,7 +165,6 @@ static void parseOperation(Compiler* cmp, str instr_name, Operation* operPtr){
|
|||||||
}
|
}
|
||||||
|
|
||||||
operPtr->opcode = instr->opcode;
|
operPtr->opcode = instr->opcode;
|
||||||
operPtr->args = List_Argument_alloc(8);
|
|
||||||
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
|
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
|
||||||
str tok_str = str_null;
|
str tok_str = str_null;
|
||||||
str processed_str = str_null;
|
str processed_str = str_null;
|
||||||
|
|||||||
@ -43,7 +43,7 @@ char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
|
|||||||
|
|
||||||
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
|
||||||
char* buf = malloc(buffer_size);
|
char* buf = malloc(buffer_size);
|
||||||
int r = vsprintf(buf, format, argv);
|
int r = vsprintf_s(buf, buffer_size, format, argv);
|
||||||
if(r < 0){
|
if(r < 0){
|
||||||
free(buf);
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@ -51,15 +51,5 @@ const Instruction* Instruction_getByName(str name){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
str name_upper = str_toUpper(name);
|
return HashMap_Instruction_tryGetPtr(instructions_map, 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -35,4 +35,3 @@ typedef struct Instruction {
|
|||||||
/// @return ptr to struct or NULL
|
/// @return ptr to struct or NULL
|
||||||
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
||||||
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
||||||
void Instruction_freeSearchStructs();
|
|
||||||
|
|||||||
@ -92,8 +92,6 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
exit_code = bootFromImage(image_file);
|
exit_code = bootFromImage(image_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// frees global variables to supress valgrind memory leak errors
|
|
||||||
Instruction_freeSearchStructs();
|
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,12 +26,6 @@ typedef u8 bool;
|
|||||||
|
|
||||||
typedef const char* cstr;
|
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 ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
|
||||||
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||||
|
|
||||||
|
|||||||
@ -36,18 +36,18 @@ void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
|||||||
|
|
||||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf(buf, IFWIN("%lli", "%li"), n);
|
sprintf_s(buf, sizeof(buf), "%llu", n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
void StringBuilder_append_u64(StringBuilder* b, u64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf(buf, IFWIN("%llu", "%lu"), n);
|
sprintf_s(buf, sizeof(buf), "%llu", n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
void StringBuilder_append_f64(StringBuilder* b, f64 n){
|
||||||
char buf[32];
|
char buf[32];
|
||||||
sprintf(buf, "%lf", n);
|
sprintf_s(buf, sizeof(buf), "%lf", n);
|
||||||
StringBuilder_append_cstr(b, buf);
|
StringBuilder_append_cstr(b, buf);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,26 +100,9 @@ bool str_endsWith(str src, str fragment){
|
|||||||
|
|
||||||
u32 str_hash32(str s){
|
u32 str_hash32(str s){
|
||||||
u8* ubuf = (u8*)s.data;
|
u8* ubuf = (u8*)s.data;
|
||||||
|
u32 len = s.len;
|
||||||
u32 hash=0;
|
u32 hash=0;
|
||||||
for (u32 i = 0; i < s.len; i++)
|
for (u32 i = 0; i < len; i++)
|
||||||
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
|
hash = (hash<<6) + (hash<<16) - hash + ubuf[i];
|
||||||
return hash;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -36,6 +36,3 @@ bool str_endsWith(str src, str fragment);
|
|||||||
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
/// @brief calculates string hash using sdbm32 algorythm (something like lightweight crc32)
|
||||||
/// @return non-cryptografic hash of the string
|
/// @return non-cryptografic hash of the string
|
||||||
u32 str_hash32(str s);
|
u32 str_hash32(str s);
|
||||||
|
|
||||||
str str_toUpper(str src);
|
|
||||||
str str_toLower(str src);
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user