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