shitty boehm is dereferencing 0x20 for some stupid reason

This commit is contained in:
William Bell
2025-09-02 05:06:48 +01:00
parent f5ee0f6fc8
commit 67569bffc2
9 changed files with 33 additions and 34 deletions

View File

@@ -1 +1,3 @@
term.log(1+2+3-4+5-100+10) let i = 10000
while (i) do
i=i-1

View File

@@ -265,6 +265,8 @@ int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash,
goto FAILED; goto FAILED;
} }
translated_dest->constants.size = constantsSize;
darray_resize(&translated_dest->bytecode, bytecodeSize); darray_resize(&translated_dest->bytecode, bytecodeSize);
if (fread(translated_dest->bytecode.data, 1, bytecodeSize, bytecode_file) != if (fread(translated_dest->bytecode.data, 1, bytecodeSize, bytecode_file) !=
@@ -272,6 +274,8 @@ int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash,
goto FAILED; goto FAILED;
} }
translated_dest->bytecode.size = bytecodeSize;
fprintf(stderr, "cache exists and is valid, so will be used.\n"); fprintf(stderr, "cache exists and is valid, so will be used.\n");
fclose(bytecode_file); fclose(bytecode_file);
return 0; return 0;
@@ -430,19 +434,19 @@ Translated load_argon_file(char *path, ArErr *err) {
Translated gc_translated = { Translated gc_translated = {
translated.registerCount, translated.registerAssignment, NULL, {}, {}, translated.registerCount, translated.registerAssignment, NULL, {}, {},
translated.path}; translated.path};
gc_translated.bytecode.data = ar_alloc_atomic(translated.bytecode.size); gc_translated.bytecode.data = ar_alloc_atomic(translated.bytecode.capacity);
memcpy(gc_translated.bytecode.data, translated.bytecode.data, memcpy(gc_translated.bytecode.data, translated.bytecode.data,
translated.bytecode.size); translated.bytecode.capacity);
gc_translated.bytecode.element_size = translated.bytecode.element_size; gc_translated.bytecode.element_size = translated.bytecode.element_size;
gc_translated.bytecode.size = translated.bytecode.size; gc_translated.bytecode.size = translated.bytecode.size;
gc_translated.bytecode.resizable = false; gc_translated.bytecode.resizable = false;
gc_translated.bytecode.capacity = gc_translated.bytecode.capacity =
translated.bytecode.size * translated.bytecode.element_size; translated.bytecode.size * translated.bytecode.element_size;
gc_translated.constants.data = ar_alloc_atomic(translated.constants.size); gc_translated.constants.data = ar_alloc_atomic(translated.constants.capacity);
memcpy(gc_translated.constants.data, translated.constants.data, memcpy(gc_translated.constants.data, translated.constants.data,
translated.constants.size); translated.constants.capacity);
gc_translated.constants.size = translated.constants.size; gc_translated.constants.size = translated.constants.size;
gc_translated.constants.capacity = translated.constants.size * translated.bytecode.element_size; gc_translated.constants.capacity =translated.constants.capacity;
free(translated.bytecode.data); free(translated.bytecode.data);
free(translated.constants.data); free(translated.constants.data);
total_time_spent = (double)(clock() - beginning) / CLOCKS_PER_SEC; total_time_spent = (double)(clock() - beginning) / CLOCKS_PER_SEC;

View File

@@ -6,6 +6,7 @@
#include "memory.h" #include "memory.h"
#include <gc.h> #include <gc.h>
#include <gc/gc.h>
#include <gmp.h> #include <gmp.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> // for malloc/free (temp arena fallback) #include <stdlib.h> // for malloc/free (temp arena fallback)

View File

@@ -157,13 +157,8 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
runtime(new_stackFrame.translated, new_stackFrame.state, runtime(new_stackFrame.translated, new_stackFrame.state,
new_stackFrame.stack, err); new_stackFrame.stack, err);
} else { } else {
if (((*state->currentStackFramePointer)->depth + 1) % STACKFRAME_CHUNKS ==
0) {
*state->currentStackFramePointer = *state->currentStackFramePointer =
ar_alloc(sizeof(StackFrame) * STACKFRAME_CHUNKS); ar_alloc(sizeof(StackFrame));
} else {
*state->currentStackFramePointer = *state->currentStackFramePointer + 1;
}
**state->currentStackFramePointer = new_stackFrame; **state->currentStackFramePointer = new_stackFrame;
if ((*state->currentStackFramePointer)->depth >= 10000) { if ((*state->currentStackFramePointer)->depth >= 10000) {
double logval = double logval =

View File

@@ -9,6 +9,7 @@
#include "../../../memory.h" #include "../../../memory.h"
#include <gc/gc.h> #include <gc/gc.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@@ -21,8 +21,6 @@ ArgonObject *new_string_object(char *data, size_t length, uint64_t prehash,
new_number_object_from_num_and_den(length, 1)); new_number_object_from_num_and_den(length, 1));
object->type = TYPE_STRING; object->type = TYPE_STRING;
object->value.as_str.data = ar_alloc_atomic(length); object->value.as_str.data = ar_alloc_atomic(length);
printf("%zu, %p\n", length,data);
printf("%.*s\n", (int)length,data);
memcpy(object->value.as_str.data, data, length); memcpy(object->value.as_str.data, data, length);
object->value.as_str.prehash = prehash; object->value.as_str.prehash = prehash;
object->value.as_str.hash_computed = hash; object->value.as_str.hash_computed = hash;

View File

@@ -626,7 +626,6 @@ static inline void load_const(Translated *translated, RuntimeState *state) {
uint64_t to_register = pop_byte(translated, state); uint64_t to_register = pop_byte(translated, state);
size_t length = pop_bytecode(translated, state); size_t length = pop_bytecode(translated, state);
uint64_t offset = pop_bytecode(translated, state); uint64_t offset = pop_bytecode(translated, state);
printf("offset %zu\n", offset);
ArgonObject *object = new_string_object(arena_get(&translated->constants, offset), length, 0, 0); ArgonObject *object = new_string_object(arena_get(&translated->constants, offset), length, 0, 0);
state->registers[to_register] = object; state->registers[to_register] = object;
} }
@@ -689,7 +688,7 @@ Stack *create_scope(Stack *prev) {
return stack; return stack;
} }
void runtime(Translated translated, RuntimeState state, Stack *stack, void runtime(Translated _translated, RuntimeState _state, Stack *stack,
ArErr *err) { ArErr *err) {
static void *dispatch_table[] = { static void *dispatch_table[] = {
[OP_LOAD_STRING] = &&DO_LOAD_STRING, [OP_LOAD_STRING] = &&DO_LOAD_STRING,
@@ -714,11 +713,11 @@ void runtime(Translated translated, RuntimeState state, Stack *stack,
[OP_ADDITION] = &&DO_ADDITION, [OP_ADDITION] = &&DO_ADDITION,
[OP_SUBTRACTION] = &&DO_SUBTRACTION, [OP_SUBTRACTION] = &&DO_SUBTRACTION,
[OP_LOAD_ACCESS_FUNCTION] = &&DO_LOAD_ACCESS_FUNCTION}; [OP_LOAD_ACCESS_FUNCTION] = &&DO_LOAD_ACCESS_FUNCTION};
state.head = 0; _state.head = 0;
StackFrame *currentStackFrame = StackFrame *currentStackFrame =
ar_alloc(sizeof(StackFrame) * STACKFRAME_CHUNKS); ar_alloc(sizeof(StackFrame));
*currentStackFrame = (StackFrame){translated, state, stack, NULL, 0}; *currentStackFrame = (StackFrame){_translated, _state, stack, NULL, 0};
currentStackFrame->state.currentStackFramePointer = &currentStackFrame; currentStackFrame->state.currentStackFramePointer = &currentStackFrame;
while (currentStackFrame) { while (currentStackFrame) {
while (likely(currentStackFrame->state.head < while (likely(currentStackFrame->state.head <
@@ -727,7 +726,6 @@ void runtime(Translated translated, RuntimeState state, Stack *stack,
Translated *translated = &currentStackFrame->translated; Translated *translated = &currentStackFrame->translated;
RuntimeState *state = &currentStackFrame->state; RuntimeState *state = &currentStackFrame->state;
uint8_t instruction = pop_byte(translated, state); uint8_t instruction = pop_byte(translated, state);
printf("opcode: %d\n",instruction);
goto *dispatch_table[instruction]; goto *dispatch_table[instruction];
DO_LOAD_NULL: DO_LOAD_NULL:
state->registers[pop_byte(translated, state)] = ARGON_NULL; state->registers[pop_byte(translated, state)] = ARGON_NULL;

View File

@@ -10,6 +10,7 @@
#include "../translator/translator.h" #include "../translator/translator.h"
#include "internals/dynamic_array_armem/darray_armem.h" #include "internals/dynamic_array_armem/darray_armem.h"
#include "internals/hashmap/hashmap.h" #include "internals/hashmap/hashmap.h"
#include <stdio.h>
#define likely(x) __builtin_expect(!!(x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0) #define unlikely(x) __builtin_expect(!!(x), 0)
@@ -57,8 +58,6 @@ typedef struct StackFrame {
uint64_t depth; uint64_t depth;
} StackFrame; } StackFrame;
#define STACKFRAME_CHUNKS 64
void bootstrap_types(); void bootstrap_types();
extern struct hashmap *runtime_hash_table; extern struct hashmap *runtime_hash_table;
@@ -68,22 +67,23 @@ uint64_t runtime_hash(const void *data, size_t len, uint64_t prehash);
void bootstrap_globals(); void bootstrap_globals();
static inline void *arena_get(ConstantArena *arena, size_t offset) { static inline void *arena_get(ConstantArena *arena, size_t offset) {
return (char*)arena->data + offset; return (char *)arena->data + offset;
} }
static inline uint8_t pop_byte(Translated *translated, RuntimeState *state) { static inline uint8_t pop_byte(Translated *translated, RuntimeState *state) {
return ((uint8_t *)(translated->bytecode.data))[state->head++]; return *(((uint8_t *)(translated->bytecode.data)) + state->head++);
} }
static inline uint64_t pop_bytecode(Translated *translated, RuntimeState *state) { static inline uint64_t pop_bytecode(Translated *translated,
uint64_t *ptr = (uint64_t *)((uint8_t*)translated->bytecode.data + state->head); RuntimeState *state) {
uint64_t value = *ptr; uint64_t *ptr =
(uint64_t *)((uint8_t *)translated->bytecode.data + state->head);
state->head += 8; state->head += 8;
return value; return *ptr;
} }
static inline void run_instruction(Translated *translated, RuntimeState *state, static inline void run_instruction(Translated *translated, RuntimeState *state,
struct Stack **stack, ArErr*err); struct Stack **stack, ArErr *err);
RuntimeState init_runtime_state(Translated translated, char *path); RuntimeState init_runtime_state(Translated translated, char *path);

View File

@@ -42,7 +42,7 @@ size_t translate_parsed_assignment(Translated *translated,
push_instruction_byte(translated, 0); push_instruction_byte(translated, 0);
break; break;
default: default:
fprintf(stderr, "panic: freeing NULL pointer\n"); fprintf(stderr, "panic: unsupported assignment\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
translated->return_jumps = old_return_jumps; translated->return_jumps = old_return_jumps;