start working on runtime oop
This commit is contained in:
0
src/runtime/call/call.c
Normal file
0
src/runtime/call/call.c
Normal file
0
src/runtime/call/call.h
Normal file
0
src/runtime/call/call.h
Normal file
@@ -19,6 +19,7 @@ ArErr runtime_declaration(Translated *translated, RuntimeState *state,
|
||||
SourceLocation *source_location = darray_get(&translated->source_locations, source_location_index);
|
||||
return create_err(source_location->line, source_location->column, source_location->length, state->path, "Runtime Error", "Identifier '%.*s' has already been declared in the current scope", length, arena_get(&translated->constants, offset));
|
||||
}
|
||||
hashmap_insert_GC(stack->scope, hash, arena_get(&translated->constants, offset), state->registers[from_register], 0);
|
||||
ArgonObject * key = init_string_object(arena_get(&translated->constants, offset), length);
|
||||
hashmap_insert_GC(stack->scope, hash, key, state->registers[from_register], 0);
|
||||
return no_err;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#ifndef runtime_declaration_H
|
||||
#define runtime_declaration_H
|
||||
#include "../runtime.h"
|
||||
#include "../objects/string/string.h"
|
||||
|
||||
ArErr runtime_declaration(Translated *translated, RuntimeState *state,
|
||||
struct Stack *stack);
|
||||
|
||||
@@ -12,11 +12,17 @@
|
||||
#include <string.h>
|
||||
|
||||
void darray_armem_init(darray_armem *arr, size_t element_size) {
|
||||
if (element_size > CHUNK_SIZE) {
|
||||
fprintf(stderr, "darray_armem_init: element size larger than chunk size\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
arr->element_size = element_size;
|
||||
arr->size = 0;
|
||||
arr->capacity = CHUNK_SIZE;
|
||||
arr->data = ar_alloc(CHUNK_SIZE * element_size);
|
||||
arr->capacity = CHUNK_SIZE / element_size;
|
||||
arr->data = ar_alloc(CHUNK_SIZE); // fixed-size byte allocation
|
||||
arr->resizable = true;
|
||||
|
||||
if (!arr->data) {
|
||||
fprintf(stderr, "darray_armem_init: allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -28,15 +34,23 @@ void darray_armem_resize(darray_armem *arr, size_t new_size) {
|
||||
fprintf(stderr, "darray_armem_resize: unresizable darray_armem\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
|
||||
|
||||
size_t required_bytes = new_size * arr->element_size;
|
||||
size_t new_capacity_bytes =
|
||||
((required_bytes + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
|
||||
size_t new_capacity = new_capacity_bytes / arr->element_size;
|
||||
|
||||
if (!new_capacity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (new_capacity != arr->capacity) {
|
||||
void *new_data = ar_alloc(new_capacity * arr->element_size);
|
||||
memccpy(new_data,arr->data, arr->element_size, arr->capacity);
|
||||
if (!new_data) {
|
||||
printf("%zu\n", new_capacity_bytes);
|
||||
arr->data = ar_realloc(arr->data, new_capacity_bytes);
|
||||
if (!arr->data) {
|
||||
fprintf(stderr, "darray_armem_resize: reallocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
arr->data = new_data;
|
||||
arr->capacity = new_capacity;
|
||||
}
|
||||
|
||||
@@ -67,11 +81,11 @@ void darray_armem_pop(darray_armem *arr, void (*free_data)(void *)) {
|
||||
return;
|
||||
|
||||
if (free_data) {
|
||||
void *target = (char *)arr->data + (arr->size-1) * arr->element_size;
|
||||
void *target = (char *)arr->data + (arr->size - 1) * arr->element_size;
|
||||
free_data(target);
|
||||
}
|
||||
|
||||
darray_armem_resize(arr, arr->size-1);
|
||||
|
||||
darray_armem_resize(arr, arr->size - 1);
|
||||
}
|
||||
|
||||
void *darray_armem_get(darray_armem *arr, size_t index) {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h> // for size_t
|
||||
|
||||
#define CHUNK_SIZE 1048576
|
||||
#define CHUNK_SIZE 4096
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
|
||||
@@ -29,8 +29,9 @@ ArgonObject *init_argon_class(char *name) {
|
||||
ArgonObject *object = ar_alloc(sizeof(ArgonObject));
|
||||
object->name = name;
|
||||
object->type = TYPE_OBJECT;
|
||||
object->self = NULL;
|
||||
object->baseObject = ARGON_TYPE;
|
||||
object->self = object;
|
||||
object->classObject = ;
|
||||
object->baseObject = BASE_CLASS;
|
||||
object->fields = createHashmap_GC();
|
||||
memset(&object->value, 0, sizeof(object->value));
|
||||
return object;
|
||||
|
||||
@@ -23,14 +23,6 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
uint64_t bytes_to_uint64(const uint8_t bytes[8]) {
|
||||
uint64_t value = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
value |= ((uint64_t)bytes[i]) << (i * 8);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void init_types() {
|
||||
BASE_CLASS = init_argon_class("BASE_CLASS");
|
||||
|
||||
@@ -40,6 +32,13 @@ void init_types() {
|
||||
init_string_type();
|
||||
|
||||
init_base_field();
|
||||
BASE_CLASS->baseObject = ARGON_NULL;
|
||||
}
|
||||
|
||||
int compare_by_order(const void *a, const void *b) {
|
||||
const struct node_GC *itemA = (const struct node_GC *)a;
|
||||
const struct node_GC *itemB = (const struct node_GC *)b;
|
||||
return itemA->order - itemB->order;
|
||||
}
|
||||
|
||||
uint8_t pop_byte(Translated *translated, RuntimeState *state) {
|
||||
@@ -47,11 +46,11 @@ uint8_t pop_byte(Translated *translated, RuntimeState *state) {
|
||||
}
|
||||
|
||||
uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
|
||||
uint8_t bytes[8];
|
||||
for (size_t i = 0; i < sizeof(bytes); i++) {
|
||||
bytes[i] = *((uint8_t *)darray_get(&translated->bytecode, state->head++));
|
||||
uint64_t value = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
value |= ((uint64_t)pop_byte(translated, state)) << (i * 8);
|
||||
}
|
||||
return bytes_to_uint64(bytes);
|
||||
return value;
|
||||
}
|
||||
|
||||
void load_const(Translated *translated, RuntimeState *state) {
|
||||
@@ -147,7 +146,28 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
case OP_NEW_SCOPE:
|
||||
*stack = create_scope(*stack);
|
||||
break;
|
||||
case OP_POP_SCOPE:
|
||||
case OP_POP_SCOPE:;
|
||||
// struct node_GC *array =
|
||||
// checked_malloc(sizeof(struct node_GC) * (*stack)->scope->count);
|
||||
// size_t j = 0;
|
||||
// for (size_t i = 0; i < (*stack)->scope->size; i++) {
|
||||
// struct node_GC *temp = (*stack)->scope->list[i];
|
||||
// while (temp) {
|
||||
// array[j++] = *temp;
|
||||
// temp = temp->next;
|
||||
// }
|
||||
// }
|
||||
// qsort(array, (*stack)->scope->count, sizeof(struct node_GC),
|
||||
// compare_by_order);
|
||||
// for (size_t i = 0; i < (*stack)->scope->count; i++) {
|
||||
// struct node_GC temp = array[i];
|
||||
// printf("%.*s = %.*s\n",
|
||||
// (int)((ArgonObject *)temp.key)->value.as_str.length,
|
||||
// ((ArgonObject *)temp.key)->value.as_str.data,
|
||||
// (int)((ArgonObject *)temp.val)->value.as_str.length,
|
||||
// ((ArgonObject *)temp.val)->value.as_str.data);
|
||||
// }
|
||||
// free(array);
|
||||
*stack = (*stack)->prev;
|
||||
break;
|
||||
default:
|
||||
@@ -160,7 +180,7 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
|
||||
RuntimeState init_runtime_state(Translated translated, char *path) {
|
||||
return (RuntimeState){
|
||||
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0, path,
|
||||
ARGON_NULL};
|
||||
ARGON_NULL, NULL, NULL};
|
||||
}
|
||||
|
||||
Stack *create_scope(Stack *prev) {
|
||||
@@ -172,11 +192,21 @@ Stack *create_scope(Stack *prev) {
|
||||
|
||||
ArErr runtime(Translated translated, RuntimeState state, Stack *stack) {
|
||||
state.head = 0;
|
||||
while (state.head < translated.bytecode.size) {
|
||||
ArErr err = run_instruction(&translated, &state, &stack);
|
||||
if (err.exists) {
|
||||
return err;
|
||||
StackFrame *currentStackFrame = checked_malloc(sizeof(StackFrame));
|
||||
*currentStackFrame = (StackFrame){translated, state, stack, NULL};
|
||||
state.currentStackFramePointer = ¤tStackFrame;
|
||||
ArErr err = no_err;
|
||||
while (currentStackFrame) {
|
||||
while (currentStackFrame->state.head <
|
||||
currentStackFrame->translated.bytecode.size &&
|
||||
!err.exists) {
|
||||
err =
|
||||
run_instruction(¤tStackFrame->translated,
|
||||
¤tStackFrame->state, ¤tStackFrame->stack);
|
||||
}
|
||||
StackFrame *tempStackFrame = currentStackFrame;
|
||||
currentStackFrame = currentStackFrame->previousStackFrame;
|
||||
free(tempStackFrame);
|
||||
}
|
||||
return no_err;
|
||||
return err;
|
||||
}
|
||||
@@ -6,20 +6,35 @@
|
||||
|
||||
#ifndef RUNTIME_H
|
||||
#define RUNTIME_H
|
||||
#include "../returnTypes.h"
|
||||
#include "../translator/translator.h"
|
||||
#include "internals/hashmap/hashmap.h"
|
||||
#include "../returnTypes.h"
|
||||
|
||||
typedef struct {
|
||||
typedef struct StackFrame StackFrame;
|
||||
typedef struct RuntimeState RuntimeState;
|
||||
|
||||
typedef ArErr (*error_result)(ArErr, Translated *translated,
|
||||
RuntimeState *state, struct Stack **stack);
|
||||
|
||||
typedef struct RuntimeState {
|
||||
ArgonObject **registers;
|
||||
size_t head;
|
||||
char*path;
|
||||
ArgonObject * return_value;
|
||||
char *path;
|
||||
ArgonObject *return_value;
|
||||
StackFrame **currentStackFramePointer;
|
||||
error_result result;
|
||||
} RuntimeState;
|
||||
|
||||
typedef struct StackFrame {
|
||||
Translated translated;
|
||||
RuntimeState state;
|
||||
Stack *stack;
|
||||
StackFrame *previousStackFrame;
|
||||
} StackFrame;
|
||||
|
||||
void init_types();
|
||||
|
||||
extern struct hashmap * runtime_hash_table;
|
||||
extern struct hashmap *runtime_hash_table;
|
||||
|
||||
uint64_t runtime_hash(const void *data, size_t len, uint64_t prehash);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user