aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-08-24 23:31:14 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commitd9f652919961a2947452ad3c4af4659f3d2fb330 (patch)
tree2db541db311a9b5a83d3f2c9b199f6d5c3341555 /include
parent40652d7dbf701eda83fa8323b42a6b5bf0ca6bdd (diff)
Add if/else/elseif/while, including the final assembly
Diffstat (limited to 'include')
-rw-r--r--include/bytecode/bytecode.h15
-rw-r--r--include/ssa/ssa.h9
2 files changed, 14 insertions, 10 deletions
diff --git a/include/bytecode/bytecode.h b/include/bytecode/bytecode.h
index 64b54b4..b1b5dda 100644
--- a/include/bytecode/bytecode.h
+++ b/include/bytecode/bytecode.h
@@ -18,18 +18,18 @@
4. 3 bytes:\
4.1 Opcode(u8) + intermediate(u16)\
4.2 Opcode(u8) + data(u16)\
- 4.3 Opcode(u8) + offset(i16)\
+ 4.3 Opcode(u8) + label(i16)\
4.4 Opcode(u8) + register(i8) + num_args(u8)
5. 4 bytes: Opcode(u8) + register(i8) + register(i8) + register(i8)
6. 4 bytes:\
- 6.1 Opcode(u8) + register(i8) + offset(i16)\
+ 6.1 Opcode(u8) + register(i8) + label(i16)\
6.2 Opcode(u8) + register(i8) + intermediate(u16)\
6.3 Opcode(u8) + register(i8) + data(u16)\
6.4 Opcode(u8) + flags(u8) + num_local_var_reg(u16)
7. 5 bytes: Opcode(u8) + index(u16) + num_args(u8) + register(i8)
# Registers
Registers have a range of 128. Local variables start from register 0 and increment while parameters start from -1
- and decrement.
+ and decrement. Registers have the scope of functions and reset after instructions reach a new function (AMAL_OP_FUNC_START).
*/
typedef enum {
AMAL_OP_NOP, /* No operation (do nothing). This can be used for patching code */
@@ -50,11 +50,12 @@ typedef enum {
AMAL_OP_CALLR, /* callr reg, num_args - Call a function using a register. Used for function pointers. num_args is u8 */
AMAL_OP_CALLE, /* calle efi, num_args, dst - Call an extern function using extern function index (efi) and num_args arguments. The result is stored in register dst. efi is u16 and 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_JZ, /* jz reg, label - Jump to label in the current function if reg is zero. label is u16 */
+ AMAL_OP_JMP, /* jmp label - Unconditional jump to label in the current function. label is u16 */
AMAL_OP_RET, /* ret reg - Return from the function with reg result */
AMAL_OP_FUNC_START, /* func_start flags, num_local_var_reg - Start of a function which has @num_local_var_reg local variable registers allocated and has the flag @flag. @flag is u8 and @num_local_var_reg is u16 */
- AMAL_OP_FUNC_END /* func_end - End of a function. Implementation should do a ret here */
+ AMAL_OP_FUNC_END, /* func_end - End of a function. Implementation should do a ret here */
+ AMAL_OP_LABEL, /* label - Label. This is the target of a jump instruction. Jump instructions only jump to labels in the same function scope */
} AmalOpcode;
#define AMAL_BYTECODE_MAGIC_NUMBER (u32)0xdec05eba
@@ -66,7 +67,7 @@ typedef enum {
typedef enum {
FUNC_FLAG_NONE = 0,
- FUNC_FLAG_EXPORTED = (1 << 1)
+ FUNC_FLAG_EXPORTED = 1 << 0
} amal_func_flag;
typedef u8 AmalOpcodeType;
diff --git a/include/ssa/ssa.h b/include/ssa/ssa.h
index ed147bf..9d6949d 100644
--- a/include/ssa/ssa.h
+++ b/include/ssa/ssa.h
@@ -28,7 +28,8 @@ typedef enum {
SSA_CALL_EXTERN,
SSA_JUMP_ZERO,
SSA_JUMP,
- SSA_RET
+ SSA_RET,
+ SSA_LABEL
} SsaInstruction;
typedef enum {
@@ -61,6 +62,7 @@ typedef u16 SsaStringIndex;
typedef u16 SsaExternFuncIndex;
typedef u16 SsaExportFuncIndex;
typedef u16 SsaFuncIndex;
+typedef u16 SsaLabelIndex;
typedef struct {
Buffer/*instruction data*/ instructions;
@@ -78,6 +80,7 @@ typedef struct {
SsaRegister reg_counter;
SsaRegister param_counter;
SsaFuncIndex func_counter;
+ SsaLabelIndex label_counter;
Parser *parser; /* Borrowed */
} Ssa;
@@ -111,11 +114,11 @@ typedef struct {
typedef struct {
SsaRegister condition_reg;
- JumpOffset jump_offset;
+ SsaLabelIndex target_label;
} SsaInsJumpZero;
typedef struct {
- JumpOffset jump_offset;
+ SsaLabelIndex target_label;
} SsaInsJump;
/* None of these functions are thread-safe */