aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-08-12 09:48:55 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commitea97370f973374f863e4296c2bb872be8b5235a3 (patch)
treebcf74846c250dd5b1f84049622ed2766605365e7 /include
parent4ca3b74621c3608de42a91730a71892d9d7c27b5 (diff)
Before interpreter. Cleanup build script. Begin writing code analyzer tool to find common mistakes
Diffstat (limited to 'include')
-rw-r--r--include/asm/x86_64.h136
-rw-r--r--include/ast.h24
-rw-r--r--include/compiler.h2
-rw-r--r--include/nullable.h2
-rw-r--r--include/program.h2
-rw-r--r--include/ssa/ssa.h4
-rw-r--r--include/std/hash_map.h2
7 files changed, 25 insertions, 147 deletions
diff --git a/include/asm/x86_64.h b/include/asm/x86_64.h
deleted file mode 100644
index 92de96b..0000000
--- a/include/asm/x86_64.h
+++ /dev/null
@@ -1,136 +0,0 @@
-#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 9f01b1b..d89d099 100644
--- a/include/ast.h
+++ b/include/ast.h
@@ -90,7 +90,7 @@ struct Ast {
struct Scope {
Buffer/*<Ast*>*/ ast_objects;
- HashMap/*(key=BufferView, value=Ast<LhsExpr>*)*/ named_objects;
+ HashMapType(BufferView, Ast*) named_objects; /* Value is always an Ast* with type LhsExpr */
Scope *parent;
/* Is null unless the scope is a file scope, in which case this is the parser that owns the scope */
Parser *parser;
@@ -144,6 +144,14 @@ typedef struct {
} value;
} VariableType;
+typedef enum {
+ DECL_FLAG_NONE = 0,
+ DECL_FLAG_EXTERN = 1 << 0,
+ DECL_FLAG_EXPORT = 1 << 1,
+ DECL_FLAG_PUB = 1 << 2,
+ DECL_FLAG_CONST = 1 << 3
+} DeclFlag;
+
/*
Note: When resolving AST, multiple threads can end up resolving the same expressions at the same time.
This is intentional. Instead of using mutex for every expression and locking/unlocking everytime
@@ -153,14 +161,17 @@ typedef struct {
leading to @ast_resolve running again for the same expression.
*/
struct LhsExpr {
- bool is_extern;
- bool is_pub;
- bool is_const;
+ u8 decl_flags;
BufferView var_name;
VariableType type;
- Ast *rhs_expr;
+ nullable Ast *rhs_expr;
};
+#define LHS_EXPR_IS_EXTERN(expr) ((expr)->decl_flags & DECL_FLAG_EXTERN)
+#define LHS_EXPR_IS_EXPORT(expr) ((expr)->decl_flags & DECL_FLAG_EXPORT)
+#define LHS_EXPR_IS_PUB(expr) ((expr)->decl_flags & DECL_FLAG_PUB)
+#define LHS_EXPR_IS_CONST(expr) ((expr)->decl_flags & DECL_FLAG_CONST)
+
struct AssignmentExpr {
Ast *lhs_expr;
Ast *rhs_expr;
@@ -228,8 +239,9 @@ 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);
+LhsExpr* structdecl_get_field_by_name(StructDecl *self, BufferView field_name);
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 lhsexpr_init(LhsExpr *self, DeclFlag decl_flag, BufferView var_name);
void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr);
void import_init(Import *self, BufferView path);
CHECK_RESULT int string_init(String *self, BufferView str);
diff --git a/include/compiler.h b/include/compiler.h
index a0ac17a..83dde63 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -49,7 +49,7 @@ struct amal_compiler {
Scope root_scope;
Buffer/*<Parser*>*/ parsers;
Buffer/*<FileScopeReference*>*/ queued_files;
- HashMap/*<BufferView, FileScopeReference*>*/ file_scopes;
+ HashMapType(BufferView, FileScopeReference*) file_scopes;
ParserThreadData *threads;
int usable_thread_count;
bool started;
diff --git a/include/nullable.h b/include/nullable.h
index f84100b..f38fb37 100644
--- a/include/nullable.h
+++ b/include/nullable.h
@@ -4,6 +4,6 @@
struct __nullable_type_dummy{ int _; };
/* Used by static analysis tool to find null-pointer dereference errors */
-#define nullable
+#define nullable __attribute__((annotate("nullable")))
#endif
diff --git a/include/program.h b/include/program.h
index 3fc69fa..a0ed4ed 100644
--- a/include/program.h
+++ b/include/program.h
@@ -3,7 +3,7 @@
#include "std/buffer.h"
#include "bytecode/bytecode.h"
-#include "asm/x86_64.h"
+#include "../executor/x86_64/asm.h"
#define AMAL_PROGRAM_OK 0
#define AMAL_PROGRAM_INVALID_HEADER -1
diff --git a/include/ssa/ssa.h b/include/ssa/ssa.h
index 1d4c612..016acc8 100644
--- a/include/ssa/ssa.h
+++ b/include/ssa/ssa.h
@@ -48,9 +48,9 @@ typedef u16 SsaFuncIndex;
typedef struct {
Buffer/*instruction data*/ instructions;
- HashMap/*<SsaNumber, SsaIntermediateIndex>*/ intermediates_map;
+ HashMapType(SsaNumber, SsaIntermediateIndex) intermediates_map;
Buffer/*SsaNumber*/ intermediates;
- HashMap/*<BufferView, SsaStringIndex>*/ strings_map;
+ HashMapType(BufferView, SsaStringIndex) strings_map;
Buffer/*BufferView*/ strings;
SsaIntermediateIndex intermediate_counter;
SsaStringIndex string_counter;
diff --git a/include/std/hash_map.h b/include/std/hash_map.h
index b9b90c6..020748b 100644
--- a/include/std/hash_map.h
+++ b/include/std/hash_map.h
@@ -21,6 +21,8 @@ struct HashMap {
HashMapHash hash_func;
};
+#define HashMapType(key_type, value_type) __attribute__((annotate(#key_type", "#value_type))) HashMap
+
CHECK_RESULT int hash_map_init(HashMap *self, ArenaAllocator *allocator, usize value_type_size, HashMapCompare compare_func, HashMapHash hash_func);
/*
Not thread-safe.