change to uint8_t for bytecode to reduce wasted bytes

This commit is contained in:
2025-06-27 06:07:57 +01:00
parent 358127a145
commit aa65393e2c
11 changed files with 100 additions and 53 deletions

View File

@@ -1,22 +1,24 @@
#include "function.h"
#include "../translator.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
size_t translate_parsed_function(Translated *translated,
ParsedFunction *parsedFunction) {
DArray temp_bytecode;
darray_init(&temp_bytecode, sizeof(uint64_t));
DArray main_bytecode = translated->bytecode;
translated->bytecode = temp_bytecode;
DArray _temp_bytecode;
darray_init(&_temp_bytecode, sizeof(uint8_t));
translated->bytecode = _temp_bytecode;
translate_parsed(translated, parsedFunction->body);
size_t function_bytecode_offset =
arena_push(&translated->constants, translated->bytecode.data,
translated->bytecode.size*translated->bytecode.element_size);
size_t function_bytecode_length = translated->bytecode.size;
darray_free(&translated->bytecode, NULL);
translated->bytecode = main_bytecode;
darray_free(&temp_bytecode, NULL);
size_t start = push_instruction_code(translated, OP_LOAD_FUNCTION);
size_t start = push_instruction_byte(translated, OP_LOAD_FUNCTION);
size_t offset = arena_push(&translated->constants, parsedFunction->name,
strlen(parsedFunction->name));
push_instruction_code(translated, offset);