#ifndef AMAL_PROGRAM_H #define AMAL_PROGRAM_H #include "std/buffer.h" #include "std/hash_map.h" #include "std/arena_allocator.h" #include "bytecode/bytecode.h" #include "../executor/executor.h" /* TODO: Remove all these errors and move program decoding and execution to another process (crash sandbox) */ #define AMAL_PROGRAM_OK 0 #define AMAL_PROGRAM_ERR -1 #define AMAL_PROGRAM_INVALID_HEADER -16 #define AMAL_PROGRAM_INVALID_MAGIC_NUMBER -2 #define AMAL_PROGRAM_INCOMPATIBLE -3 #define AMAL_PROGRAM_INVALID_INTERMEDIATES -4 #define AMAL_PROGRAM_INVALID_INTERMEDIATES_SIZE -5 #define AMAL_PROGRAM_INVALID_STRINGS -6 #define AMAL_PROGRAM_INVALID_STRINGS_SIZE -7 #define AMAL_PROGRAM_ALLOC_FAILURE -8 #define AMAL_PROGRAM_INVALID_INSTRUCTIONS_SIZE -9 #define AMAL_PROGRAM_INVALID_INSTRUCTION -10 #define AMAL_PROGRAM_INSTRUCTION_INVALID_INTERMEDIATE_INDEX -11 #define AMAL_PROGRAM_INSTRUCTION_INVALID_DATA_INDEX -12 #define AMAL_PROGRAM_INSTRUCTION_STACK_OVERFLOW -13 #define AMAL_PROGRAM_INSTRUCTION_STACK_OOM -14 #define AMAL_PROGRAM_INSTRUCTION_ILLEGAL_JUMP_TARGET -15 #define AMAL_PROGRAM_INVALID_FUNCTIONS -16 #define AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS -17 #define AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS_SIZE -18 #define AMAL_PROGRAM_INSTRUCTION_INVALID_EXTERN_FUNC_INDEX -19 #define AMAL_PROGRAM_NO_SUCH_EXTERNAL_FUNCTION -20 #define AMAL_PROGRAM_EXTERN_FUNC_ALREADY_EXISTS -21 #define AMAL_PROGRAM_INVALID_EXPORTED_FUNCTIONS -22 #define AMAL_PROGRAM_INVALID_EXPORTED_FUNCTIONS_SIZE -23 #define AMAL_PROGRAM_INSTRUCTION_INVALID_EXPORTED_FUNC_INDEX -24 #define AMAL_PROGRAM_NO_MAIN_FUNC -25 #define AMAL_PROGRAM_INVALID_IMPORTS -26 #define AMAL_PROGRAM_SECTION_ERROR -27 #define AMAL_PROGRAM_MAX_RETURN_VALUES 128 typedef struct { void *func; u32 args_byte_size; } ProgramExternFunc; typedef struct { Buffer/*<...>*/ data; u32 *string_indices; u8 *intermediates_start; /* Reference inside @data */ u8 *strings_start; /* Reference inside @data */ u8 *funcs_start; /* Reference inside @data */ u8 *extern_funcs_start; /* Reference inside @data */ u8 *exported_funcs; /* Reference inside @data */ u8 *exported_funcs_end; /* Reference inside @data */ u8 *imports_start; /* Reference inside @data */ usize read_index; u32 main_func_instruction_offset; u16 num_intermediates; u16 num_strings; u16 num_functions; u16 num_extern_functions; u16 num_exported_functions; u8 num_imports; ArenaAllocator allocator; /* Owned. Used by @extern_funcs_map */ HashMapType(BufferView, ProgramExternFunc) extern_funcs_map; /* key=((func_index<<32)|funcs_start), value=Buffer */ HashMapType(u64, Buffer) deferred_func_calls; i8 return_values_stack[AMAL_PROGRAM_MAX_RETURN_VALUES]; int return_value_index; } amal_program; CHECK_RESULT int amal_program_init(amal_program *self); void amal_program_deinit(amal_program *self); /* returns @AMAL_PROGRAM_EXTERN_FUNC_ALREADY_EXISTS if a function with the name @name already exists in the program. If the function has a vararg parameter, then @args_byte_size should be the minimum parameters size excluding the varargs. */ CHECK_RESULT int amal_program_register_extern_func(amal_program *self, BufferView name, void *func_ptr, u32 args_byte_size); CHECK_RESULT int amal_program_append_bytecode(amal_program *self, Bytecode *bytecode); CHECK_RESULT int amal_program_run(amal_program *self); CHECK_RESULT int amal_program_save(amal_program *self, const char *filepath); #endif