aboutsummaryrefslogtreecommitdiff
path: root/include/ssa/ssa.h
blob: b2c8065caa2fc459bfd7fac8bd1abf4e634e6510 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef AMALGAM_SSA_H
#define AMALGAM_SSA_H

#include "../std/buffer.h"
#include "../std/hash_map.h"
#include "../std/defs.h"
#include "../defs.h"

#include <setjmp.h>

#define SSA_ERR_EXTERN_FUNC_SIG_MISMATCH -20

typedef enum {
    SSA_ASSIGN_INTER,
    SSA_ASSIGN_STRING,
    SSA_ASSIGN_REG,
    SSA_ADD,
    SSA_SUB,
    SSA_IMUL,
    SSA_MUL,
    SSA_IDIV,
    SSA_DIV,
    SSA_EQUALS,
    SSA_FUNC_START,
    SSA_FUNC_END,
    SSA_PUSH,
    SSA_CALL,
    SSA_CALL_EXTERN,
    SSA_JUMP_ZERO,
    SSA_JUMP,
    SSA_RET
} SsaInstruction;

typedef enum {
    SSA_NUMBER_TYPE_INTEGER,
    SSA_NUMBER_TYPE_FLOAT
} SsaNumberType;

typedef struct {
    union {
        i64 integer;
        f64 floating;
    } value;
    SsaNumberType type;
} SsaNumber;

typedef struct {
    FunctionSignature *func_sig;
    BufferView name;
} SsaExternFunc;

typedef i16 JumpOffset;
typedef i16 SsaRegister;
typedef u16 SsaIntermediateIndex;
typedef u16 SsaStringIndex;
typedef u16 SsaExternFuncIndex;
typedef u16 SsaFuncIndex;

typedef struct {
    Buffer/*instruction data*/ instructions;
    HashMapType(SsaNumber, SsaIntermediateIndex) intermediates_map;
    Buffer/*SsaNumber*/ intermediates;
    HashMapType(BufferView, SsaStringIndex) strings_map;
    Buffer/*BufferView*/ strings;
    HashMapType(BufferView, SsaExternFuncIndex) extern_funcs_map;
    Buffer/*SsaExternFunc*/ extern_funcs;
    SsaIntermediateIndex intermediate_counter;
    SsaStringIndex string_counter;
    SsaExternFuncIndex extern_func_counter;
    SsaRegister reg_counter;
    SsaRegister param_counter;
    SsaFuncIndex func_counter;
    Parser *parser; /* Borrowed */
} Ssa;

typedef struct {
    SsaRegister lhs;
    u16 rhs;
} SsaInsForm1;

typedef struct {
    SsaRegister result;
    SsaRegister lhs;
    SsaRegister rhs;
} SsaInsForm2;

typedef struct {
    SsaFuncIndex func_index;
    u16 num_params_regs;
    u16 num_local_vars_regs;
} SsaInsFuncStart;

typedef struct {
    u8 num_args;
    SsaRegister result;
    FunctionDecl *func_decl;
} SsaInsFuncCall;

typedef struct {
    u8 num_args;
    SsaRegister result;
    SsaExternFuncIndex extern_func_index;
} SsaInsFuncCallExtern;

typedef struct {
    SsaRegister condition_reg;
    JumpOffset jump_offset;
} SsaInsJumpZero;

typedef struct {
    JumpOffset jump_offset;
} SsaInsJump;

/* None of these functions are thread-safe */
SsaNumber create_ssa_integer(i64 value);
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, Parser *parser);

typedef struct {
    jmp_buf env;
    Ssa *ssa;
    amal_compiler *compiler;
} SsaCompilerContext;

/* longjump to context->env on failure */
void scope_generate_ssa(Scope *self, SsaCompilerContext *context);

#endif