aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-12 01:27:54 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commitb35b3e1bf70bf764940498247b1db5bb02761160 (patch)
tree07ad41028fd3d162e9e681a03b75df1cfc740606 /include
parent79bf40f909cefdc611bfa13f70ae55b52ac41d23 (diff)
Starting on ssa
Diffstat (limited to 'include')
-rw-r--r--include/ast.h3
-rw-r--r--include/compiler.h1
-rw-r--r--include/ssa/ssa.h56
-rw-r--r--include/std/buffer.h2
-rw-r--r--include/std/hash_map.h8
5 files changed, 65 insertions, 5 deletions
diff --git a/include/ast.h b/include/ast.h
index 3eb7bb6..497488f 100644
--- a/include/ast.h
+++ b/include/ast.h
@@ -131,6 +131,9 @@ void binop_init(Binop *self);
CHECK_RESULT int scope_init(Scope *self, ScopedAllocator *allocator);
CHECK_RESULT int scope_add_child(Scope *self, Ast *child);
+/* longjump to compiler env on failure */
void scope_resolve(Scope *self, AstCompilerContext *context);
+/* longjump to compiler env on failure */
+void scope_generate_ssa(Scope *self, AstCompilerContext *context);
#endif
diff --git a/include/compiler.h b/include/compiler.h
index d7314cc..4e24f11 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -22,6 +22,7 @@ struct amal_compiler {
bool started;
amal_mutex mutex;
int resolve_ast_index;
+ int generate_ssa_index;
};
CHECK_RESULT int amal_compiler_init(amal_compiler *self);
diff --git a/include/ssa/ssa.h b/include/ssa/ssa.h
new file mode 100644
index 0000000..e5bd86b
--- /dev/null
+++ b/include/ssa/ssa.h
@@ -0,0 +1,56 @@
+#ifndef AMALGAM_SSA_H
+#define AMALGAM_SSA_H
+
+#include "../std/buffer.h"
+#include "../std/hash_map.h"
+#include "../std/defs.h"
+
+typedef enum {
+ SSA_ASSIGN_INTER,
+ SSA_ASSIGN_REG,
+ SSA_ADD,
+ SSA_SUB,
+ SSA_MUL,
+ SSA_DIV,
+ SSA_FUNC_START,
+ SSA_FUNC_END,
+ SSA_PUSH,
+ SSA_CALL
+} SsaInstructionType;
+
+typedef enum {
+ SSA_NUMBER_TYPE_INTEGER,
+ SSA_NUMBER_TYPE_FLOAT
+} SsaNumberType;
+
+typedef struct {
+ i64 value;
+ SsaNumberType type;
+} SsaNumber;
+
+typedef u16 SsaRegister;
+typedef u16 SsaIntermediateIndex;
+typedef u32 SsaFuncIndex;
+
+typedef struct {
+ Buffer/*instruction data*/ instructions;
+ HashMap/*<SsaNumberType, IntermediateIndex>*/ intermediates;
+ SsaIntermediateIndex intermediate_counter;
+ SsaRegister reg_counter;
+ SsaFuncIndex func_counter;
+} Ssa;
+
+/* None of these functions are thread-safe */
+SsaNumber create_ssa_number(i64 value, SsaNumberType type);
+
+CHECK_RESULT int ssa_init(Ssa *self, ScopedAllocator *allocator);
+CHECK_RESULT int ssa_get_unique_reg(Ssa *self, SsaRegister *result);
+CHECK_RESULT int ssa_ins_assign_inter(Ssa *self, SsaRegister dest, SsaNumber number);
+CHECK_RESULT int ssa_ins_assign_reg(Ssa *self, SsaRegister dest, SsaRegister src);
+CHECK_RESULT int ssa_ins_binop(Ssa *self, SsaInstructionType binop_type, SsaRegister lhs, SsaRegister rhs, SsaRegister *result);
+CHECK_RESULT int ssa_ins_func_start(Ssa *self, u8 num_args, SsaFuncIndex *result);
+CHECK_RESULT int ssa_ins_func_end(Ssa *self);
+CHECK_RESULT int ssa_ins_push(Ssa *self, SsaRegister reg);
+CHECK_RESULT int ssa_ins_call(Ssa *self, SsaFuncIndex func, SsaRegister *result);
+
+#endif
diff --git a/include/std/buffer.h b/include/std/buffer.h
index 688f18a..c961b6e 100644
--- a/include/std/buffer.h
+++ b/include/std/buffer.h
@@ -3,6 +3,7 @@
#include "types.h"
#include "misc.h"
+#include "defs.h"
#define BUFFER_OK 0
#define BUFFER_ALLOC_FAIL -1
@@ -13,7 +14,6 @@ typedef struct {
usize capacity;
} Buffer;
-struct ScopedAllocator;
CHECK_RESULT int buffer_init(Buffer *self, struct ScopedAllocator *allocator);
/* @data can be NULL */
diff --git a/include/std/hash_map.h b/include/std/hash_map.h
index c36ecc4..97b0745 100644
--- a/include/std/hash_map.h
+++ b/include/std/hash_map.h
@@ -15,21 +15,21 @@ typedef usize(*HashMapHash)(const u8 *data, usize size);
struct HashMap {
ScopedAllocator *allocator; /* borrowed */
Buffer/*HashMapBucket*/ buckets;
- usize type_size;
+ usize value_type_size;
usize num_elements;
HashMapCompare compare_func;
HashMapHash hash_func;
};
-CHECK_RESULT int hash_map_init(HashMap *self, ScopedAllocator *allocator, usize type_size, HashMapCompare compare_func, HashMapHash hash_func);
+CHECK_RESULT int hash_map_init(HashMap *self, ScopedAllocator *allocator, usize value_type_size, HashMapCompare compare_func, HashMapHash hash_func);
/*
Not thread-safe.
-Expected @value size to be @self->type_size.
+Expected @value size to be @self->value_type_size.
*/
CHECK_RESULT int hash_map_insert(HashMap *self, BufferView key, void *value);
/*
Thread-safe unless @hash_map_insert is used in another thread at the same time.
-Expected @value size to be @self->type_size.
+Expected @value size to be @self->value_type_size.
*/
CHECK_RESULT bool hash_map_get(HashMap *self, BufferView key, void *value);