work on method wrapper and native function support
This commit is contained in:
@@ -29,7 +29,6 @@ this operation takes 3 operands.
|
||||
2. the offset in the constant buffer of the variable name.
|
||||
3. the fixed hash of the variable name.
|
||||
4. the register of the given value (*)
|
||||
5. the index of the source location.
|
||||
|
||||
## OP_LOAD_NULL
|
||||
|
||||
@@ -59,7 +58,6 @@ initilises a function to a given register.
|
||||
1. the length of the identifer.
|
||||
2. the offset of the identifier.
|
||||
3. the fixed hash of the variable name.
|
||||
4. the index of the source location.
|
||||
|
||||
## OP_BOOL
|
||||
|
||||
@@ -89,27 +87,27 @@ creates a new stack
|
||||
|
||||
pops the top scope off the current
|
||||
|
||||
## OP_INIT_ARGS
|
||||
## OP_INIT_CALL
|
||||
|
||||
initialises the arguments buffer with a fixed number of objects on the current state
|
||||
initialises a call instance struct and arguments buffer.
|
||||
|
||||
1. the number of objects for the arguments buffer
|
||||
|
||||
## OP_INSERT_ARG
|
||||
|
||||
1. the register to take the object from. (*)
|
||||
1. index of the argument in the arguments buffer to write the object from the register into.
|
||||
|
||||
## OP_RESET_ARGS
|
||||
|
||||
resets the arguments buffer to NULL
|
||||
|
||||
## OP_CALL
|
||||
|
||||
call a function with args
|
||||
|
||||
1. the register containing the function to call. (*)
|
||||
4. the index of the source location.
|
||||
## OP_SOURCE_LOCATION
|
||||
|
||||
sets the source location onto the runtime
|
||||
|
||||
3. the line
|
||||
4. the column
|
||||
5. the length
|
||||
|
||||
## OP_SWAP_REGISTERS
|
||||
|
||||
|
||||
@@ -5,29 +5,27 @@
|
||||
*/
|
||||
#include "call.h"
|
||||
|
||||
size_t translate_parsed_call(Translated *translated, ParsedCall* call, ArErr *err) {
|
||||
size_t translate_parsed_call(Translated *translated, ParsedCall *call,
|
||||
ArErr *err) {
|
||||
set_registers(translated, 1);
|
||||
size_t first = push_instruction_byte(translated, OP_INIT_ARGS);
|
||||
translate_parsed(translated, call->to_call, err);
|
||||
size_t first = push_instruction_byte(translated, OP_INIT_CALL);
|
||||
push_instruction_code(translated, call->args.size);
|
||||
for (size_t i = 0; i < call->args.size; i++) {
|
||||
translate_parsed(translated, darray_get(&call->args, i), err);
|
||||
if (err->exists)
|
||||
return first;
|
||||
push_instruction_byte(translated, OP_INSERT_ARG);
|
||||
push_instruction_byte(translated, 0);
|
||||
push_instruction_code(translated, i);
|
||||
}
|
||||
translate_parsed(translated, call->to_call, err);
|
||||
if (err->exists)
|
||||
return first;
|
||||
|
||||
push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
||||
push_instruction_code(translated, call->line);
|
||||
push_instruction_code(translated, call->column);
|
||||
push_instruction_code(translated, 1);
|
||||
|
||||
push_instruction_byte(translated, OP_CALL);
|
||||
push_instruction_byte(translated, 0);
|
||||
SourceLocation source_locations = {
|
||||
call->line,
|
||||
call->column,
|
||||
1
|
||||
};
|
||||
push_instruction_code(translated, translated->source_locations.size);
|
||||
darray_push(&translated->source_locations, &source_locations);
|
||||
return first;
|
||||
}
|
||||
@@ -27,16 +27,17 @@ size_t translate_parsed_declaration(Translated *translated,
|
||||
size_t length = strlen(singleDeclaration->name);
|
||||
size_t offset =
|
||||
arena_push(&translated->constants, singleDeclaration->name, length);
|
||||
|
||||
|
||||
push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
||||
push_instruction_code(translated, singleDeclaration->line);
|
||||
push_instruction_code(translated, singleDeclaration->column);
|
||||
push_instruction_code(translated, length);
|
||||
|
||||
push_instruction_byte(translated, OP_DECLARE);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, offset);
|
||||
push_instruction_code(translated, siphash64_bytes(singleDeclaration->name, length, siphash_key_fixed_for_translator));
|
||||
push_instruction_byte(translated, 0);
|
||||
SourceLocation source_locations = {singleDeclaration->line,
|
||||
singleDeclaration->column, length};
|
||||
push_instruction_code(translated, translated->source_locations.size);
|
||||
darray_push(&translated->source_locations, &source_locations);
|
||||
translated->return_jumps = old_return_jumps;
|
||||
}
|
||||
return first;
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
#include <string.h>
|
||||
size_t translate_parsed_function(Translated *translated,
|
||||
ParsedFunction *parsedFunction, ArErr *err) {
|
||||
DArray main_bytecode = translated->bytecode;
|
||||
DArray _temp_bytecode;
|
||||
darray_init(&_temp_bytecode, sizeof(uint8_t));
|
||||
darray_armem main_bytecode = translated->bytecode;
|
||||
darray_armem _temp_bytecode;
|
||||
darray_armem_init(&_temp_bytecode, sizeof(uint8_t));
|
||||
set_registers(translated, 1);
|
||||
translated->bytecode = _temp_bytecode;
|
||||
translate_parsed(translated, parsedFunction->body, err);
|
||||
@@ -23,7 +23,6 @@ size_t translate_parsed_function(Translated *translated,
|
||||
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;
|
||||
size_t start = push_instruction_byte(translated, OP_LOAD_FUNCTION);
|
||||
size_t offset = arena_push(&translated->constants, parsedFunction->name,
|
||||
|
||||
@@ -4,27 +4,30 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include "../translator.h"
|
||||
#include "identifier.h"
|
||||
#include "../../hash_data/hash_data.h"
|
||||
#include "../translator.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../../hash_data/hash_data.h"
|
||||
|
||||
size_t translate_parsed_identifier(Translated *translated, ParsedIdentifier *parsedIdentifier) {
|
||||
size_t translate_parsed_identifier(Translated *translated,
|
||||
ParsedIdentifier *parsedIdentifier) {
|
||||
size_t length = strlen(parsedIdentifier->name);
|
||||
size_t identifier_pos = arena_push(&translated->constants, parsedIdentifier->name, length);
|
||||
size_t identifier_pos =
|
||||
arena_push(&translated->constants, parsedIdentifier->name, length);
|
||||
set_registers(translated, 1);
|
||||
size_t start = push_instruction_byte(translated, OP_IDENTIFIER);
|
||||
push_instruction_code(translated,length);
|
||||
|
||||
size_t start = push_instruction_byte(translated, OP_SOURCE_LOCATION);
|
||||
push_instruction_code(translated, parsedIdentifier->line);
|
||||
push_instruction_code(translated, parsedIdentifier->column);
|
||||
push_instruction_code(translated, length);
|
||||
|
||||
push_instruction_byte(translated, OP_IDENTIFIER);
|
||||
push_instruction_code(translated, length);
|
||||
push_instruction_code(translated, identifier_pos);
|
||||
push_instruction_code(translated, siphash64_bytes(parsedIdentifier->name, length, siphash_key_fixed_for_translator));
|
||||
SourceLocation source_locations = {
|
||||
parsedIdentifier->line,
|
||||
parsedIdentifier->column,
|
||||
length
|
||||
};
|
||||
push_instruction_code(translated, translated->source_locations.size);
|
||||
darray_push(&translated->source_locations, &source_locations);
|
||||
push_instruction_code(translated,
|
||||
siphash64_bytes(parsedIdentifier->name, length,
|
||||
siphash_key_fixed_for_translator));
|
||||
return start;
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
#include "translator.h"
|
||||
#include "../hash_data/hash_data.h"
|
||||
#include "../hashmap/hashmap.h"
|
||||
#include "declaration/declaration.h"
|
||||
#include "call/call.h"
|
||||
#include "dowrap/dowrap.h"
|
||||
@@ -29,10 +28,10 @@ void uint64_to_bytes(uint64_t value, uint8_t bytes[8]) {
|
||||
}
|
||||
|
||||
void arena_init(ConstantArena *arena) {
|
||||
arena->data = checked_malloc(CHUNK_SIZE);
|
||||
arena->data = ar_alloc(CHUNK_SIZE);
|
||||
arena->capacity = CHUNK_SIZE;
|
||||
arena->size = 0;
|
||||
arena->hashmap = createHashmap();
|
||||
arena->hashmap = createHashmap_GC();
|
||||
}
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size) {
|
||||
@@ -48,13 +47,6 @@ void arena_resize(ConstantArena *arena, size_t new_size) {
|
||||
arena->capacity = new_capacity;
|
||||
}
|
||||
|
||||
void arena_free(ConstantArena *arena) {
|
||||
free(arena->data);
|
||||
arena->capacity = 0;
|
||||
arena->size = 0;
|
||||
hashmap_free(arena->hashmap, NULL);
|
||||
}
|
||||
|
||||
void *arena_get(ConstantArena *arena, size_t offset) {
|
||||
return arena->data + offset;
|
||||
}
|
||||
@@ -63,7 +55,7 @@ size_t arena_push(ConstantArena *arena, const void *data, size_t length) {
|
||||
uint64_t hash = siphash64_bytes(data, length, siphash_key);
|
||||
|
||||
// Look up offset in hashmap
|
||||
void *val = hashmap_lookup(arena->hashmap, hash);
|
||||
void *val = hashmap_lookup_GC(arena->hashmap, hash);
|
||||
if (val != NULL) {
|
||||
size_t offset =
|
||||
(size_t)(uintptr_t)val - 1; // stored as pointer but really offset
|
||||
@@ -80,7 +72,7 @@ size_t arena_push(ConstantArena *arena, const void *data, size_t length) {
|
||||
arena->size += length;
|
||||
|
||||
// Insert into hashmap: store offset as pointer-sized integer
|
||||
hashmap_insert(arena->hashmap, hash, (void *)data,
|
||||
hashmap_insert_GC(arena->hashmap, hash, (void *)data,
|
||||
(void *)(uintptr_t)offset + 1, 0);
|
||||
|
||||
return offset;
|
||||
@@ -91,15 +83,14 @@ Translated init_translator(char* path) {
|
||||
translated.path = path;
|
||||
translated.registerCount = 1;
|
||||
translated.return_jumps=NULL;
|
||||
darray_init(&translated.bytecode, sizeof(uint8_t));
|
||||
darray_init(&translated.source_locations, sizeof(SourceLocation));
|
||||
darray_armem_init(&translated.bytecode, sizeof(uint8_t));
|
||||
arena_init(&translated.constants);
|
||||
return translated;
|
||||
}
|
||||
|
||||
size_t push_instruction_byte(Translated *translator, uint8_t byte) {
|
||||
size_t offset = translator->bytecode.size;
|
||||
darray_push(&translator->bytecode, &byte);
|
||||
darray_armem_push(&translator->bytecode, &byte);
|
||||
return offset;
|
||||
}
|
||||
|
||||
@@ -112,7 +103,7 @@ size_t push_instruction_code(Translated *translator, uint64_t code) {
|
||||
uint8_t bytes[8];
|
||||
uint64_to_bytes(code, bytes);
|
||||
for (size_t i = 0; i < sizeof(bytes); i++) {
|
||||
darray_push(&translator->bytecode, &(bytes[i]));
|
||||
darray_armem_push(&translator->bytecode, &(bytes[i]));
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
@@ -169,10 +160,4 @@ ArErr translate(Translated *translated, DArray *ast) {
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void free_translator(Translated *translated) {
|
||||
darray_free(&translated->bytecode, NULL);
|
||||
darray_free(&translated->source_locations, NULL);
|
||||
arena_free(&translated->constants);
|
||||
}
|
||||
@@ -14,31 +14,9 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE, OP_INIT_ARGS, OP_INSERT_ARG, OP_RESET_ARGS, OP_CALL } OperationType;
|
||||
typedef enum { OP_LOAD_CONST, OP_DECLARE, OP_LOAD_NULL, OP_LOAD_FUNCTION, OP_IDENTIFIER, OP_BOOL, OP_JUMP_IF_FALSE, OP_JUMP, OP_NEW_SCOPE, OP_POP_SCOPE, OP_INIT_CALL, OP_INSERT_ARG, OP_CALL, OP_SOURCE_LOCATION } OperationType;
|
||||
typedef enum { TYPE_OP_STRING, TYPE_OP_NUMBER } types;
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
struct hashmap * hashmap;
|
||||
} ConstantArena;
|
||||
|
||||
typedef struct {
|
||||
uint8_t registerCount;
|
||||
DArray *return_jumps;
|
||||
DArray bytecode;
|
||||
DArray source_locations;
|
||||
ConstantArena constants;
|
||||
char* path;
|
||||
} Translated;
|
||||
|
||||
typedef struct {
|
||||
uint64_t line;
|
||||
uint64_t column;
|
||||
uint64_t length;
|
||||
} SourceLocation;
|
||||
|
||||
void arena_resize(ConstantArena *arena, size_t new_size);
|
||||
|
||||
void *arena_get(ConstantArena *arena, size_t offset);
|
||||
@@ -61,6 +39,4 @@ size_t translate_parsed(Translated *translated, ParsedValue *parsedValue, ArErr*
|
||||
|
||||
ArErr translate(Translated *translated, DArray *ast);
|
||||
|
||||
void free_translator(Translated *translated);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user