moved collections and string code to tlibc submodule

This commit is contained in:
2025-07-24 17:49:50 +03:00
parent cf5ed7b601
commit 72f8e196a7
35 changed files with 278 additions and 896 deletions

View File

@@ -1,5 +1,5 @@
#include "instructions.h"
#include "../collections/HashMap.h"
#include "tlibc/collections/HashMap.h"
i32 NOP_impl(VM* vm);
i32 EXIT_impl(VM* vm);
@@ -31,8 +31,7 @@ i32 JNZ_impl(VM* vm);
i32 JZ_impl(VM* vm);
Array_declare(Instruction);
static const Array_Instruction instructions_array = ARRAY(Instruction, {
static const Array(Instruction) instructions_array = ARRAY(Instruction, {
Instruction_construct(NOP),
Instruction_construct(EXIT),
Instruction_construct(SYS),
@@ -64,35 +63,39 @@ static const Array_Instruction instructions_array = ARRAY(Instruction, {
});
const Instruction* Instruction_getByOpcode(Opcode opcode){
if(opcode >= instructions_array.len)
if(opcode >= Array_len(&instructions_array, Instruction))
return NULL;
return instructions_array.data + opcode;
return (Instruction*)instructions_array.data + opcode;
}
HashMap_declare(Instruction);
HashMap_define(Instruction, HashMap_DESTROY_VALUE_FUNC_NULL);
static HashMap_Instruction* instructions_map = NULL;
static HashMap(Opcode)* opcode_map = NULL;
static void _opcode_map_init(){
opcode_map = malloc(sizeof(*opcode_map));
HashMap_create(opcode_map, Opcode, NULL);
for(u32 i = 0; i < Array_len(&instructions_array, Instruction); i++){
Instruction* instr_ptr = (Instruction*)instructions_array.data + i;
HashMap_tryPush(opcode_map, instr_ptr->name, &instr_ptr->opcode);
}
}
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]);
}
}
if(opcode_map == NULL)
_opcode_map_init();
str name_upper = str_toUpper(name);
Instruction* iptr = HashMap_Instruction_tryGetPtr(instructions_map, name_upper);
Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper);
free(name_upper.data);
return iptr;
if(op_ptr == NULL)
return NULL;
return Instruction_getByOpcode(*op_ptr);
}
void Instruction_freeSearchStructs(){
if(instructions_map != NULL){
HashMap_Instruction_free(instructions_map);
free(instructions_map);
if(opcode_map != NULL){
HashMap_destroy(opcode_map);
free(opcode_map);
}
}
}

View File

@@ -60,15 +60,15 @@ str RegisterCode_toString(RegisterCode code){
break;
case 4:
buf_str.data += 1;
buf_str.len -= 1;
buf_str.size -= 1;
break;
case 7:
buf_str.data[0] = 'l';
buf_str.len -= 1;
buf_str.size -= 1;
break;
case 8:
buf_str.data[0] = 'h';
buf_str.len -= 1;
buf_str.size -= 1;
break;
}

View File

@@ -1,6 +1,6 @@
#pragma once
#include "../std.h"
#include "../string/str.h"
#include "tlibc/std.h"
#include "tlibc/string/str.h"
typedef enum RegisterCode {
RegisterCode_Unset = 0,