blob: 9104898b8386eb1ca72ce5d8f968a95b336f0ef2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#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<u32=current_code_index> */
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
|