diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-07-31 01:25:05 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 1f28c3c733ea3ae4234bff91e1c55e61b1ee3e96 (patch) | |
tree | 0ab52e362da03fde741ce8159ef8a4110cd1fb6a /include | |
parent | ec1a48e7b86fcd00127dd5a88d56c42083af1d78 (diff) |
Starting on asm, implementing extern function call so progress is visible
Diffstat (limited to 'include')
-rw-r--r-- | include/asm/x86_64.h | 136 | ||||
-rw-r--r-- | include/ast.h | 22 | ||||
-rw-r--r-- | include/bytecode/bytecode.h | 58 | ||||
-rw-r--r-- | include/compiler.h | 41 | ||||
-rw-r--r-- | include/parser.h | 8 | ||||
-rw-r--r-- | include/program.h | 25 | ||||
-rw-r--r-- | include/ssa/ssa.h | 6 | ||||
-rw-r--r-- | include/std/arena_allocator.h | 29 | ||||
-rw-r--r-- | include/std/buffer.h | 4 | ||||
-rw-r--r-- | include/std/defs.h | 4 | ||||
-rw-r--r-- | include/std/hash_map.h | 4 | ||||
-rw-r--r-- | include/std/mem.h | 2 | ||||
-rw-r--r-- | include/std/misc.h | 2 | ||||
-rw-r--r-- | include/std/scoped_allocator.h | 29 | ||||
-rw-r--r-- | include/tokenizer.h | 4 |
15 files changed, 275 insertions, 99 deletions
diff --git a/include/asm/x86_64.h b/include/asm/x86_64.h new file mode 100644 index 0000000..92de96b --- /dev/null +++ b/include/asm/x86_64.h @@ -0,0 +1,136 @@ +#ifndef AMAL_ASM_X86_64_H +#define AMAL_ASM_X86_64_H + +#include "../std/misc.h" +#include "../std/types.h" + +typedef struct { + void *code; + u8 *code_it; + usize size; +} Asm; + +typedef enum { + EAX, + ECX, + EDX, + EBX, + ESP, + EBP, + ESI, + EDI +} Reg32; + +typedef enum { + RAX, + RCX, + RDX, + RBX, + RSP, + RBP, + RSI, + RDI +} Reg64; + +typedef struct { + Reg64 base; + Reg64 index; + i32 disp; + u8 scale; +} AsmPtr; + +void asm_ptr_init(AsmPtr *self, Reg64 base); +void asm_ptr_init_index(AsmPtr *self, Reg64 base, Reg64 index); +void asm_ptr_init_disp(AsmPtr *self, Reg64 base, i32 disp); +void asm_ptr_init_index_disp(AsmPtr *self, Reg64 base, Reg64 index, i32 disp); + +CHECK_RESULT int asm_init(Asm *self); +void asm_deinit(Asm *self); + +CHECK_RESULT int asm_execute(Asm *self); + +CHECK_RESULT int asm_nop(Asm *self); + + + + + + + + + +CHECK_RESULT int asm_mov_mi(Asm *self, AsmPtr *dst, i32 immediate); +CHECK_RESULT int asm_mov_mr(Asm *self, AsmPtr *dst, Reg64 src); +CHECK_RESULT int asm_mov_rm(Asm *self, Reg64 dst, AsmPtr *src); +CHECK_RESULT int asm_mov_ri(Asm *self, Reg64 dst, i64 immediate); +CHECK_RESULT int asm_mov_rr(Asm *self, Reg64 dst, Reg64 src); + +CHECK_RESULT int asm_add_rr(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_sub_rr(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_imul_rr(Asm *self, Reg64 dst, Reg64 src); + +CHECK_RESULT int asm_pushr(Asm *self, Reg64 reg); +CHECK_RESULT int asm_popr(Asm *self, Reg64 reg); + + + + + + + + + + + + + + +CHECK_RESULT int asm_mov_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_add_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_sub_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_and_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_or_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_xor_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_cmp_rm32(Asm *self, Reg32 dst, Reg32 src); +CHECK_RESULT int asm_add_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_or_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_adc_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_sbb_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_and_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_sub_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_xor_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_cmp_rm32_imm(Asm *self, Reg32 reg, i32 immediate); +CHECK_RESULT int asm_rol_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_ror_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_rcl_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_rcr_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_shl_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_shr_rm32_imm(Asm *self, Reg32 reg, i8 immediate); +CHECK_RESULT int asm_sar_rm32_imm(Asm *self, Reg32 reg, i8 immediate); + +CHECK_RESULT int asm_mov_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_add_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_sub_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_and_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_or_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_xor_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_cmp_rm64(Asm *self, Reg64 dst, Reg64 src); +CHECK_RESULT int asm_add_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_or_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_adc_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_sbb_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_and_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_sub_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_xor_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_cmp_rm64_imm(Asm *self, Reg64 reg, i32 immediate); +CHECK_RESULT int asm_rol_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_ror_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_rcl_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_rcr_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_shl_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_shr_rm64_imm(Asm *self, Reg64 reg, i8 immediate); +CHECK_RESULT int asm_sar_rm64_imm(Asm *self, Reg64 reg, i8 immediate); + +CHECK_RESULT int asm_ret(Asm *self, u16 bytes); + +#endif diff --git a/include/ast.h b/include/ast.h index f056db7..4c54766 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ #include "std/buffer_view.h" #include "std/buffer.h" #include "std/misc.h" -#include "std/scoped_allocator.h" +#include "std/arena_allocator.h" #include "std/hash_map.h" #include "binop_type.h" #include "ssa/ssa.h" @@ -106,6 +106,7 @@ struct Variable { struct FunctionSignature { int params; /* TODO: Implement signatures */ + bool resolved; }; struct FunctionDecl { @@ -218,12 +219,13 @@ typedef struct { Scope *scope; } AstCompilerContext; -CHECK_RESULT int ast_create(ScopedAllocator *allocator, void *value, AstType type, Ast **result); +CHECK_RESULT int ast_create(ArenaAllocator *allocator, void *value, AstType type, Ast **result); BufferView ast_get_name(Ast *self); -CHECK_RESULT int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ScopedAllocator *allocator); -CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator); -CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ScopedAllocator *allocator); +void function_signature_init(FunctionSignature *self); +CHECK_RESULT int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ArenaAllocator *allocator); +CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ArenaAllocator *allocator); +CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ArenaAllocator *allocator); void structfield_init(StructField *self, BufferView name, BufferView type_name); void lhsexpr_init(LhsExpr *self, bool is_extern, bool is_pub, bool is_const, BufferView var_name); void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr); @@ -232,12 +234,12 @@ CHECK_RESULT int string_init(String *self, BufferView str); void number_init(Number *self, i64 value, bool is_integer, BufferView code_ref); void variable_init(Variable *self, BufferView name); void binop_init(Binop *self); -CHECK_RESULT int if_statement_init(IfStatement *self, Scope *parent, ScopedAllocator *allocator); -CHECK_RESULT int else_if_statement_init(ElseIfStatement *self, Scope *parent, ScopedAllocator *allocator); -CHECK_RESULT int while_statement_init(WhileStatement *self, Scope *parent, ScopedAllocator *allocator); +CHECK_RESULT int if_statement_init(IfStatement *self, Scope *parent, ArenaAllocator *allocator); +CHECK_RESULT int else_if_statement_init(ElseIfStatement *self, Scope *parent, ArenaAllocator *allocator); +CHECK_RESULT int while_statement_init(WhileStatement *self, Scope *parent, ArenaAllocator *allocator); -CHECK_RESULT int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator); -CHECK_RESULT int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ScopedAllocator *allocator); +CHECK_RESULT int scope_init(Scope *self, Scope *parent, ArenaAllocator *allocator); +CHECK_RESULT int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ArenaAllocator *allocator); CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); diff --git a/include/bytecode/bytecode.h b/include/bytecode/bytecode.h index b571383..32488cc 100644 --- a/include/bytecode/bytecode.h +++ b/include/bytecode/bytecode.h @@ -12,41 +12,45 @@ Variable length opcodes. Sizes range from 1 to 4 bytes. # Instruction formats Instructions can be in 6 different formats: - 1. 1 byte: Opcode - 2. 2 bytes: Opcode + register - 3. 3 bytes: Opcode + register + register + 1. 1 byte: Opcode(u8) + 2. 2 bytes: Opcode(u8) + register(u8) + 3. 3 bytes: Opcode(u8) + register(u8) + register(u8) 4. 3 bytes:\ - 4.1 Opcode + intermediate\ - 4.2 Opcode + data\ - 4.3 Opcode + index\ - 4.4 Opcode + offset - 5. 4 bytes: Opcode + register + register + register + 4.1 Opcode(u8) + intermediate(u16)\ + 4.2 Opcode(u8) + data(u16)\ + 4.3 Opcode(u8) + offset(i16)\ + 4.4 Opcode(u8) + num_reg(u16)\ + 4.5 Opcode(u8) + register(u8) + num_args(u8) + 5. 4 bytes: Opcode(u8) + register(u8) + register(u8) + register(u8) 6. 4 bytes:\ - 6.1 Opcode + register + offset\ - 6.2 Opcode + register + intermediate\ - 6.3 Opcode + register + data + 6.1 Opcode(u8) + register(u8) + offset(i16)\ + 6.2 Opcode(u8) + register(u8) + intermediate(u16)\ + 6.3 Opcode(u8) + register(u8) + data(u16) + 7. 4 bytes: Opcode(u8) + index(u16) + num_args(u8) */ typedef enum { - AMAL_OP_NOP, /* No operation. This can be used for patching */ + AMAL_OP_NOP, /* No operation (do nothing). This can be used for patching code */ AMAL_OP_SETZ, /* setz reg - Set register value to 0 */ - AMAL_OP_MOV, /* mov dst, src - move src register to dst register */ - AMAL_OP_MOVI, /* movi dst, src - move src intermediate to dst register */ - AMAL_OP_MOVD, /* movd dst, src - move src data to dst register */ - AMAL_OP_ADD, /* add dst, reg1, reg2 */ - AMAL_OP_SUB, /* sub dst, reg1, reg2 */ - AMAL_OP_MUL, /* mul dst, reg1, reg2 */ - AMAL_OP_DIV, /* div dst, reg1, reg2 */ + AMAL_OP_MOV, /* mov dst, src - Move src register to dst register */ + AMAL_OP_MOVI, /* movi dst, src - Move src intermediate to dst register */ + AMAL_OP_MOVD, /* movd dst, src - Move src data to dst register */ + AMAL_OP_ADD, /* add dst, reg1, reg2 - Add reg1 and reg2 and put the result in dst */ + AMAL_OP_SUB, /* sub dst, reg1, reg2 - Substract reg2 from reg1 and put the result in dst */ + AMAL_OP_IMUL, /* imul dst, reg1, reg2 - Signed multiplication */ + AMAL_OP_MUL, /* mul dst, reg1, reg2 - Unsigned multiplication */ + AMAL_OP_IDIV, /* idiv dst, reg1, reg2 - Signed division */ + AMAL_OP_DIV, /* div dst, reg1, reg2 - Unsigned division */ AMAL_OP_PUSH, /* push reg - Push register onto stack */ AMAL_OP_PUSHI, /* pushi int - Push intermediate onto stack */ AMAL_OP_PUSHD, /* pushd data - Push data onto stack */ - AMAL_OP_CALL, /* call fi - Call a function using function index (fi). fi is u16 */ - AMAL_OP_CALLR, /* callr reg - Call a function using a register. Used for function pointers */ + AMAL_OP_CALL, /* call fi, num_args - Call a function using function index (fi) and num_args arguments. fi is u16 and num_args is u8 */ + AMAL_OP_CALLR, /* callr reg, num_args - Call a function using a register. Used for function pointers. num_args is u8 */ AMAL_OP_CMP, /* cmp dst, reg1, reg2 - Set dst to 1 if reg1 equals reg2, otherwise set it to 0 */ - AMAL_OP_JZ, /* jz reg, offset - jump to offset if reg is zero. offset is i16 */ - AMAL_OP_JMP, /* jmp offset - unconditional jump to offset. offset is i16 */ - AMAL_OP_RET, /* ret */ - AMAL_OP_FUNC_START, /* func_start */ - AMAL_OP_FUNC_END /* func_end */ + AMAL_OP_JZ, /* jz reg, offset - Jump to offset if reg is zero. offset is i16 */ + AMAL_OP_JMP, /* jmp offset - Unconditional jump to offset. offset is i16 */ + AMAL_OP_RET, /* ret - Return from the function */ + AMAL_OP_FUNC_START, /* func_start num_reg - Start of a function which has num_reg registers allocated. num_reg is a u16 */ + AMAL_OP_FUNC_END /* func_end - End of a function. Implementation should do a ret here */ } AmalOpcode; typedef u8 AmalOpcodeType; @@ -61,7 +65,7 @@ typedef struct { Parser *parser; /* borrowed */ } BytecodeCompilerContext; -CHECK_RESULT int bytecode_init(Bytecode *self, ScopedAllocator *allocator); +CHECK_RESULT int bytecode_init(Bytecode *self, ArenaAllocator *allocator); /* longjump to self->env on failure */ void generate_bytecode_from_ssa(BytecodeCompilerContext *self); diff --git a/include/compiler.h b/include/compiler.h index 0ae8532..a0ac17a 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -4,7 +4,7 @@ #include "std/misc.h" #include "std/buffer.h" #include "std/buffer_view.h" -#include "std/scoped_allocator.h" +#include "std/arena_allocator.h" #include "std/thread.h" #include "compiler_options.h" #include "ast.h" @@ -14,27 +14,38 @@ /* General error */ #define AMAL_COMPILER_ERR -1 +#define NUM_ARITHMETIC_TYPES 10 + +typedef struct { + LhsExpr lhs_expr; + bool is_signed; +} amal_default_type; + typedef struct { - LhsExpr *i8; - LhsExpr *i16; - LhsExpr *i32; - LhsExpr *i64; - LhsExpr *u8; - LhsExpr *u16; - LhsExpr *u32; - LhsExpr *u64; - LhsExpr *isize; - LhsExpr *usize; - LhsExpr *f32; - LhsExpr *f64; - LhsExpr *str; + amal_default_type *i8; + amal_default_type *i16; + amal_default_type *i32; + amal_default_type *i64; + amal_default_type *u8; + amal_default_type *u16; + amal_default_type *u32; + amal_default_type *u64; + amal_default_type *isize; + amal_default_type *usize; + amal_default_type *f32; + amal_default_type *f64; + amal_default_type *str; + + amal_default_type *arithmetic_types[NUM_ARITHMETIC_TYPES]; } amal_default_types; +bool is_arithmetic_type(LhsExpr *expr, amal_compiler *compiler); + struct amal_compiler { amal_default_types default_types; amal_compiler_options options; amal_program *program; - ScopedAllocator allocator; + ArenaAllocator allocator; Scope root_scope; Buffer/*<Parser*>*/ parsers; Buffer/*<FileScopeReference*>*/ queued_files; diff --git a/include/parser.h b/include/parser.h index bc205f0..4427cc8 100644 --- a/include/parser.h +++ b/include/parser.h @@ -2,7 +2,7 @@ #define AMALGAM_PARSER_H #include "std/buffer_view.h" -#include "std/scoped_allocator.h" +#include "std/arena_allocator.h" #include "std/thread.h" #include "bytecode/bytecode.h" #include "tokenizer.h" @@ -25,7 +25,7 @@ typedef enum { struct ParserThreadData { amal_thread thread; ParserThreadStatus status; - ScopedAllocator allocator; + ArenaAllocator allocator; }; typedef enum { @@ -41,7 +41,7 @@ struct Parser { Scope *current_scope; bool has_func_parent; /* Borrowed from @compiler for faster access to allocator. The allocator is thread-specific */ - ScopedAllocator *allocator; + ArenaAllocator *allocator; amal_compiler *compiler; Ssa *ssa; bool started; @@ -56,7 +56,7 @@ void parser_thread_data_deinit(ParserThreadData *self); CHECK_RESULT int parser_thread_data_start(ParserThreadData *self, AmalThreadCallbackFunc callback_func, void *userdata); CHECK_RESULT int parser_thread_data_join(ParserThreadData *self, void **result); -CHECK_RESULT int parser_init(Parser *self, amal_compiler *compiler, ScopedAllocator *allocator); +CHECK_RESULT int parser_init(Parser *self, amal_compiler *compiler, ArenaAllocator *allocator); /* @buffer_name will be the path to the file when using parser_parse_file and when parsing a buffer diff --git a/include/program.h b/include/program.h index f251fbc..3fc69fa 100644 --- a/include/program.h +++ b/include/program.h @@ -3,6 +3,7 @@ #include "std/buffer.h" #include "bytecode/bytecode.h" +#include "asm/x86_64.h" #define AMAL_PROGRAM_OK 0 #define AMAL_PROGRAM_INVALID_HEADER -1 @@ -14,18 +15,36 @@ #define AMAL_PROGRAM_INVALID_STRINGS_SIZE -7 #define AMAL_PROGRAM_STRING_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_MAGIC_NUMBER (u32)0xdec05eba #define AMAL_PROGRAM_MAJOR_VERSION 1 #define AMAL_PROGRAM_MINOR_VERSION 0 #define AMAL_PROGRAM_PATCH_VERSION 0 +#define AMAL_PROGRAM_NUM_REGISTERS 256 + typedef struct { Buffer/*<...>*/ data; - Buffer/*<u32>*/ string_indices; - char *intermediates_start; - char *strings_start; + u32 *string_indices; + char *intermediates_start; /* Reference inside @data */ + char *strings_start; /* Reference inside @data */ usize read_index; + + u16 num_strings; + u16 num_intermediates; + + u64 reg[AMAL_PROGRAM_NUM_REGISTERS]; + u64 *stack; + usize stack_size; + usize stack_index; + + Asm asm; } amal_program; CHECK_RESULT int amal_program_init(amal_program *self); diff --git a/include/ssa/ssa.h b/include/ssa/ssa.h index 15a9f78..1d4c612 100644 --- a/include/ssa/ssa.h +++ b/include/ssa/ssa.h @@ -14,7 +14,9 @@ typedef enum { SSA_ASSIGN_REG, SSA_ADD, SSA_SUB, + SSA_IMUL, SSA_MUL, + SSA_IDIV, SSA_DIV, SSA_EQUALS, SSA_FUNC_START, @@ -69,7 +71,7 @@ typedef struct { typedef struct { SsaFuncIndex func_index; - u8 num_args; + u16 num_registers; } SsaInsFuncStart; typedef struct { @@ -93,7 +95,7 @@ SsaNumber create_ssa_float(f64 value); SsaNumber ssa_get_intermediate(Ssa *self, SsaIntermediateIndex index); BufferView ssa_get_string(Ssa *self, SsaStringIndex index); -CHECK_RESULT int ssa_init(Ssa *self, ScopedAllocator *allocator); +CHECK_RESULT int ssa_init(Ssa *self, ArenaAllocator *allocator); typedef struct { jmp_buf env; diff --git a/include/std/arena_allocator.h b/include/std/arena_allocator.h new file mode 100644 index 0000000..30147c8 --- /dev/null +++ b/include/std/arena_allocator.h @@ -0,0 +1,29 @@ +#ifndef AMALGAM_ARENA_ALLOCATOR_H +#define AMALGAM_ARENA_ALLOCATOR_H + +#include "defs.h" +#include "misc.h" +#include "types.h" +#include "buffer.h" + +struct ArenaAllocatorNode { + char *data; + usize size; + ArenaAllocatorNode *next; +}; + +struct ArenaAllocator { + ArenaAllocatorNode head; + ArenaAllocatorNode *current; + Buffer/*<void*>*/ mems; +}; + +CHECK_RESULT int arena_allocator_node_init(ArenaAllocatorNode *self); +void arena_allocator_node_deinit(ArenaAllocatorNode *self); + +CHECK_RESULT int arena_allocator_init(ArenaAllocator *self); +void arena_allocator_deinit(ArenaAllocator *self); +CHECK_RESULT int arena_allocator_alloc(ArenaAllocator *self, usize size, void **mem); +CHECK_RESULT int arena_allocator_add_mem(ArenaAllocator *self, usize *result_index); + +#endif diff --git a/include/std/buffer.h b/include/std/buffer.h index 7a74d33..ac722b1 100644 --- a/include/std/buffer.h +++ b/include/std/buffer.h @@ -13,11 +13,11 @@ typedef struct { usize size; usize capacity; - ScopedAllocator *allocator; + ArenaAllocator *allocator; usize allocator_index; } Buffer; -CHECK_RESULT int buffer_init(Buffer *self, struct ScopedAllocator *allocator); +CHECK_RESULT int buffer_init(Buffer *self, struct ArenaAllocator *allocator); void buffer_deinit(Buffer *self); /* diff --git a/include/std/defs.h b/include/std/defs.h index ee049d4..8a63a69 100644 --- a/include/std/defs.h +++ b/include/std/defs.h @@ -4,7 +4,7 @@ typedef struct amal_thread amal_thread; typedef struct amal_mutex amal_mutex; -typedef struct ScopedAllocatorNode ScopedAllocatorNode; -typedef struct ScopedAllocator ScopedAllocator; +typedef struct ArenaAllocatorNode ArenaAllocatorNode; +typedef struct ArenaAllocator ArenaAllocator; #endif diff --git a/include/std/hash_map.h b/include/std/hash_map.h index 9b31213..b9b90c6 100644 --- a/include/std/hash_map.h +++ b/include/std/hash_map.h @@ -13,7 +13,7 @@ typedef int(*HashMapCompare)(const void *a, const void *b); typedef usize(*HashMapHash)(const u8 *data, usize size); struct HashMap { - ScopedAllocator *allocator; /* borrowed */ + ArenaAllocator *allocator; /* borrowed */ Buffer/*HashMapBucket*/ buckets; usize value_type_size; usize num_elements; @@ -21,7 +21,7 @@ struct HashMap { HashMapHash hash_func; }; -CHECK_RESULT int hash_map_init(HashMap *self, ScopedAllocator *allocator, usize value_type_size, HashMapCompare compare_func, HashMapHash hash_func); +CHECK_RESULT int hash_map_init(HashMap *self, ArenaAllocator *allocator, usize value_type_size, HashMapCompare compare_func, HashMapHash hash_func); /* Not thread-safe. Expected @value size to be @self->value_type_size. diff --git a/include/std/mem.h b/include/std/mem.h index ddd2b31..2afce0d 100644 --- a/include/std/mem.h +++ b/include/std/mem.h @@ -9,4 +9,6 @@ int am_memcmp(const void *lhs, const void *rhs, usize size); bool am_memeql(const void *lhs, const void *rhs, usize size); void am_memset(void *dest, int value, usize size); +long am_pagesize(); + #endif diff --git a/include/std/misc.h b/include/std/misc.h index b1932d5..2fbe92c 100644 --- a/include/std/misc.h +++ b/include/std/misc.h @@ -23,7 +23,7 @@ do { \ int return_if_result; \ return_if_result = (result); \ - if((return_if_result) != 0) { \ + if(return_if_result != 0) { \ return_if_debug_msg; \ return return_if_result; \ } \ diff --git a/include/std/scoped_allocator.h b/include/std/scoped_allocator.h deleted file mode 100644 index f037ea5..0000000 --- a/include/std/scoped_allocator.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef AMALGAM_SCOPED_ALLOCATOR_H -#define AMALGAM_SCOPED_ALLOCATOR_H - -#include "defs.h" -#include "misc.h" -#include "types.h" -#include "buffer.h" - -struct ScopedAllocatorNode { - char *data; - usize size; - ScopedAllocatorNode *next; -}; - -struct ScopedAllocator { - ScopedAllocatorNode head; - ScopedAllocatorNode *current; - Buffer/*<void*>*/ mems; -}; - -CHECK_RESULT int scoped_allocator_node_init(ScopedAllocatorNode *self); -void scoped_allocator_node_deinit(ScopedAllocatorNode *self); - -CHECK_RESULT int scoped_allocator_init(ScopedAllocator *self); -void scoped_allocator_deinit(ScopedAllocator *self); -CHECK_RESULT int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem); -CHECK_RESULT int scoped_allocator_add_mem(ScopedAllocator *self, usize *result_index); - -#endif diff --git a/include/tokenizer.h b/include/tokenizer.h index 4018e3a..f624816 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -60,7 +60,7 @@ typedef struct { BinopType binop_type; } value; bool number_is_integer; - ScopedAllocator *allocator; /* borrowed */ + ArenaAllocator *allocator; /* borrowed */ const amal_compiler_options *compiler_options; /* borrowed */ } Tokenizer; @@ -69,7 +69,7 @@ typedef struct { char* str; } TokenizerError; -CHECK_RESULT int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name, const amal_compiler_options *compiler_options); +CHECK_RESULT int tokenizer_init(Tokenizer *self, ArenaAllocator *allocator, BufferView code, BufferView code_name, const amal_compiler_options *compiler_options); CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token); /* @result is set to 0 if the next token is equal to @expected_token, |