From 2f738a73790258514f86614ba9a9591578a5bdf0 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 25 Apr 2022 12:20:30 -0300 Subject: Add a return op --- src/ir.c | 4 +++- src/ir.h | 3 +++ src/viz.c | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ir.c b/src/ir.c index 86a1a9a..7f841be 100644 --- a/src/ir.c +++ b/src/ir.c @@ -156,7 +156,9 @@ generate_basm(ParseTree *parse_tree) { array_init(program->lines, 0); for (size_t i = 0; i < array_size(parse_tree->roots); ++i) { Node *root = parse_tree->roots[i]; - emit_basm(program, root); + Operand ret = emit_basm(program, root); + LineInfo line = (LineInfo){.line = root->line, .col = root->col}; + EMIT_0(program, line, OP_RETURN, ret); } return program; } diff --git a/src/ir.h b/src/ir.h index c0ce22a..6df1e0d 100644 --- a/src/ir.h +++ b/src/ir.h @@ -44,6 +44,9 @@ typedef enum Operator { OP_JMP_LT, OP_JMP_GE, OP_JMP_LE, + + // Return value from register. + OP_RETURN, } Operator; typedef enum OperandType { diff --git a/src/viz.c b/src/viz.c index 8e31387..ebe1a4c 100644 --- a/src/viz.c +++ b/src/viz.c @@ -272,6 +272,7 @@ static const char* basm_op_str[] = { [OP_JMP_LT] = "jlt ", [OP_JMP_GE] = "jge ", [OP_JMP_LE] = "jle ", + [OP_RETURN] = "ret ", }; void @@ -311,11 +312,16 @@ viz_instruction(Instruction *inst, LineInfo *line) { viz_operand(inst->src_b); } } break; + case OP_RETURN: case OP_JMP: case OP_LABEL: { printf(" "); viz_operand(inst->dst); } break; + case OP_CP8: + case OP_CP16: + case OP_CP32: + case OP_CP64: case OP_JMP_TRUE: case OP_JMP_FALSE: { printf(" "); -- cgit v1.2.1