work on method wrapper and native function support

This commit is contained in:
William Bell
2025-08-09 06:40:16 +01:00
parent 38e2cad507
commit 0635ba8517
23 changed files with 404 additions and 324 deletions

View File

@@ -24,13 +24,13 @@
#include <time.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h> // for _mkdir
#include <sys/stat.h> // for _stat
#include <direct.h> // for _mkdir
#include <sys/stat.h> // for _stat
#include <windows.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include "../external/cwalk/include/cwalk.h"
#include <string.h>
@@ -48,30 +48,22 @@
// Windows / MinGW usually uses little-endian, so these can be no-ops
// But define them explicitly to avoid implicit declaration warnings
static inline uint32_t le32toh(uint32_t x) {
return x;
}
static inline uint64_t le64toh(uint64_t x) {
return x;
}
static inline uint32_t htole32(uint32_t x) {
return x;
}
static inline uint64_t htole64(uint64_t x) {
return x;
}
static inline uint32_t le32toh(uint32_t x) { return x; }
static inline uint64_t le64toh(uint64_t x) { return x; }
static inline uint32_t htole32(uint32_t x) { return x; }
static inline uint64_t htole64(uint64_t x) { return x; }
#elif defined(__linux__)
#include <endian.h>
#include <endian.h>
#elif defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define htole32(x) OSSwapHostToLittleInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
// Add others as needed
#include <libkern/OSByteOrder.h>
#define htole32(x) OSSwapHostToLittleInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
// Add others as needed
#else
#error "Unsupported platform"
#error "Unsupported platform"
#endif
char *get_current_directory() {
@@ -104,31 +96,53 @@ char *get_current_directory() {
int ensure_dir_exists(const char *path) {
#ifdef _WIN32
struct _stat st;
if (_stat(path, &st) != 0) {
// Directory does not exist, create it
if (_mkdir(path) != 0) {
perror("_mkdir failed");
return -1;
}
} else if (!(st.st_mode & _S_IFDIR)) {
fprintf(stderr, "Path exists but is not a directory\n");
return -1;
struct _stat st;
if (_stat(path, &st) != 0) {
// Directory does not exist, create it
if (_mkdir(path) != 0) {
perror("_mkdir failed");
return -1;
}
} else if (!(st.st_mode & _S_IFDIR)) {
fprintf(stderr, "Path exists but is not a directory\n");
return -1;
}
#else
struct stat st;
if (stat(path, &st) != 0) {
// Directory does not exist, create it
if (mkdir(path, 0755) != 0) {
perror("mkdir failed");
return -1;
}
} else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "Path exists but is not a directory\n");
return -1;
struct stat st;
if (stat(path, &st) != 0) {
// Directory does not exist, create it
if (mkdir(path, 0755) != 0) {
perror("mkdir failed");
return -1;
}
} else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "Path exists but is not a directory\n");
return -1;
}
#endif
return 0;
return 0;
}
static inline void write_and_hash(FILE *file, XXH64_state_t *state,
const void *ptr, size_t size, size_t count) {
fwrite(ptr, size, count, file);
XXH64_update(state, ptr, size * count);
}
static inline void update_hash_from_file(FILE *file, XXH64_state_t *state,
size_t size) {
char buffer[4096];
size_t bytes_read;
size_t remaining = size;
while (remaining > 0 &&
(bytes_read =
fread(buffer, 1,
remaining > sizeof(buffer) ? sizeof(buffer) : remaining,
file)) > 0) {
XXH64_update(state, buffer, bytes_read);
remaining -= bytes_read;
}
}
const char CACHE_FOLDER[] = "__arcache__";
@@ -136,13 +150,52 @@ const char FILE_IDENTIFIER[5] = "ARBI";
const char BYTECODE_EXTENTION[] = "arbin";
const uint32_t version_number = 0;
int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash) {
bool translated_inited = false;
int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash, char*source_path) {
FILE *bytecode_file = fopen(joined_paths, "rb");
if (!bytecode_file) {
printf("cache doesnt exist... compiling from source.\n");
return 1;
}
// Find file size
fseek(bytecode_file, 0, SEEK_END);
long file_size = ftell(bytecode_file);
if (file_size < (long)sizeof(uint64_t)) {
goto FAILED;
}
fseek(bytecode_file, 0, SEEK_SET);
// Footer is the last 8 bytes
long data_size = file_size - sizeof(uint64_t);
// Set up hash state
XXH64_state_t *state = XXH64_createState();
XXH64_reset(state, 0);
// Hash everything except last 8 bytes
update_hash_from_file(bytecode_file, state, data_size);
// Read stored footer hash
uint64_t stored_hash_le;
if (fread(&stored_hash_le, 1, sizeof(stored_hash_le), bytecode_file) !=
sizeof(stored_hash_le)) {
XXH64_freeState(state);
goto FAILED;
}
uint64_t stored_hash = le64toh(stored_hash_le);
// Compare
uint64_t calc_hash = XXH64_digest(state);
XXH64_freeState(state);
if (calc_hash != stored_hash) {
printf("cache hash mismatch (corrupted?)\n");
goto FAILED;
}
// Now actually parse the file contents
fseek(bytecode_file, 0, SEEK_SET); // rewind to start
char file_identifier_from_cache[sizeof(FILE_IDENTIFIER)] = {0};
if (fread(&file_identifier_from_cache, 1,
sizeof(file_identifier_from_cache) - 1,
@@ -194,15 +247,7 @@ int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash) {
}
bytecodeSize = le64toh(bytecodeSize);
uint64_t sourceLocationSize;
if (fread(&sourceLocationSize, 1, sizeof(sourceLocationSize),
bytecode_file) != sizeof(sourceLocationSize)) {
goto FAILED;
}
sourceLocationSize = le64toh(sourceLocationSize);
*translated_dest = init_translator("");
translated_inited = true;
*translated_dest = init_translator(source_path);
arena_resize(&translated_dest->constants, constantsSize);
@@ -211,29 +256,18 @@ int load_cache(Translated *translated_dest, char *joined_paths, uint64_t hash) {
goto FAILED;
}
darray_resize(&translated_dest->bytecode, bytecodeSize);
darray_armem_resize(&translated_dest->bytecode, bytecodeSize);
if (fread(translated_dest->bytecode.data, 1, bytecodeSize, bytecode_file) !=
bytecodeSize) {
goto FAILED;
}
darray_resize(&translated_dest->source_locations, sourceLocationSize);
if (fread(translated_dest->source_locations.data, sizeof(SourceLocation),
sourceLocationSize, bytecode_file) != sourceLocationSize) {
goto FAILED;
}
translated_dest->registerCount = register_count;
printf("cache exists and is valid, so will be used.\n");
fclose(bytecode_file);
return 0;
FAILED:
printf("cache is invalid... compiling from source.\n");
if (translated_inited)
free_translator(translated_dest);
fclose(bytecode_file);
return 1;
}
@@ -292,7 +326,7 @@ Execution execute(char *path, Stack *stack) {
Translated translated;
if (load_cache(&translated, cache_file_path, hash) != 0) {
if (load_cache(&translated, cache_file_path, hash, path) != 0) {
translated = init_translator(path);
DArray tokens;
@@ -302,7 +336,6 @@ Execution execute(char *path, Stack *stack) {
start = clock();
ArErr err = lexer(state);
if (err.exists) {
free_translator(&translated);
darray_free(&tokens, free_token);
return (Execution){err, (Stack){NULL, NULL}};
}
@@ -319,7 +352,6 @@ Execution execute(char *path, Stack *stack) {
start = clock();
err = parser(path, &ast, &tokens, false);
if (err.exists) {
free_translator(&translated);
darray_free(&tokens, free_token);
darray_free(&ast, free_parsed);
return (Execution){err, (Stack){NULL, NULL}};
@@ -333,7 +365,6 @@ Execution execute(char *path, Stack *stack) {
start = clock();
err = translate(&translated, &ast);
if (err.exists) {
free_translator(&translated);
darray_free(&ast, free_parsed);
return (Execution){err, (Stack){NULL, NULL}};
}
@@ -349,27 +380,36 @@ Execution execute(char *path, Stack *stack) {
uint64_t constantsSize = translated.constants.size;
uint64_t bytecodeSize = translated.bytecode.size;
uint64_t sourceLocationSize = translated.source_locations.size;
uint32_t version_number_htole32ed = htole32(version_number);
uint64_t net_hash = htole64(hash);
constantsSize = htole64(constantsSize);
bytecodeSize = htole64(bytecodeSize);
sourceLocationSize = htole64(sourceLocationSize);
fwrite(&FILE_IDENTIFIER, sizeof(char), strlen(FILE_IDENTIFIER), file);
fwrite(&version_number_htole32ed, sizeof(uint32_t), 1, file);
fwrite(&net_hash, sizeof(net_hash), 1, file);
fwrite(&translated.registerCount, sizeof(uint8_t), 1, file);
fwrite(&constantsSize, sizeof(uint64_t), 1, file);
fwrite(&bytecodeSize, sizeof(uint64_t), 1, file);
fwrite(&sourceLocationSize, sizeof(uint64_t), 1, file);
fwrite(translated.constants.data, 1, translated.constants.size, file);
fwrite(translated.bytecode.data, translated.bytecode.element_size,
translated.bytecode.size, file);
fwrite(translated.source_locations.data,
translated.source_locations.element_size,
translated.source_locations.size, file);
XXH64_state_t *hash_state = XXH64_createState();
XXH64_reset(hash_state, 0);
write_and_hash(file, hash_state, &FILE_IDENTIFIER, sizeof(char),
strlen(FILE_IDENTIFIER));
write_and_hash(file, hash_state, &version_number_htole32ed,
sizeof(uint32_t), 1);
write_and_hash(file, hash_state, &net_hash, sizeof(net_hash), 1);
write_and_hash(file, hash_state, &translated.registerCount, sizeof(uint8_t),
1);
write_and_hash(file, hash_state, &constantsSize, sizeof(uint64_t), 1);
write_and_hash(file, hash_state, &bytecodeSize, sizeof(uint64_t), 1);
write_and_hash(file, hash_state, translated.constants.data, 1,
translated.constants.size);
write_and_hash(file, hash_state, translated.bytecode.data,
translated.bytecode.element_size, translated.bytecode.size);
// Finalize the hash
uint64_t file_hash = XXH64_digest(hash_state);
XXH64_freeState(hash_state);
// Convert to little-endian before writing if needed
uint64_t file_hash_le = htole64(file_hash);
fwrite(&file_hash_le, sizeof(file_hash_le), 1, file);
fclose(file);
}
@@ -385,7 +425,6 @@ Execution execute(char *path, Stack *stack) {
printf("Execution time taken: %f seconds\n", time_spent);
printf("total time taken: %f seconds\n", total_time_spent);
free_translator(&translated);
return (Execution){err, *main_scope};
}
@@ -394,6 +433,7 @@ int main(int argc, char *argv[]) {
ar_memory_init();
generate_siphash_key(siphash_key);
bootstrap_types();
bootstrap_globals();
char *CWD = get_current_directory();
if (argc <= 1)
return -1;
@@ -401,7 +441,7 @@ int main(int argc, char *argv[]) {
char path[FILENAME_MAX];
cwk_path_get_absolute(CWD, path_non_absolute, path, sizeof(path));
free(CWD);
Execution resp = execute(path, NULL);
Execution resp = execute(path, Global_Scope);
if (runtime_hash_table)
hashmap_free(runtime_hash_table, NULL);
if (resp.err.exists) {