parser+codegen: Handle function epilogue

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-02-15 23:11:52 -05:00
parent 87f0cfcdb0
commit 07db3ee834
3 changed files with 28 additions and 5 deletions

View File

@@ -13,6 +13,10 @@ resolve_func(struct rifle_state *state, struct ast_node *root)
return -1; return -1;
} }
if (root->epilogue) {
return mu_gen_ret(state);
}
if ((symbol = root->symbol) == NULL) { if ((symbol = root->symbol) == NULL) {
trace_error(state, "internal: no symbol associated with func node\n"); trace_error(state, "internal: no symbol associated with func node\n");
return -1; return -1;

View File

@@ -495,6 +495,9 @@ parse_func(struct rifle_state *state, struct token *tok, struct ast_node **res)
static int static int
parse_rbrace(struct rifle_state *state, struct token *tok, struct ast_node **res) parse_rbrace(struct rifle_state *state, struct token *tok, struct ast_node **res)
{ {
struct ast_node *root = NULL;
tt_t scope;
if (state == NULL || tok == NULL) { if (state == NULL || tok == NULL) {
return -1; return -1;
} }
@@ -507,7 +510,21 @@ parse_rbrace(struct rifle_state *state, struct token *tok, struct ast_node **res
return -1; return -1;
} }
scope_pop(state); scope = scope_pop(state);
switch (scope) {
case TT_F:
if (ast_alloc_node(state, AST_FUNC, &root) < 0) {
trace_error(state, "failed to allocate AST_FUNC\n");
return -1;
}
root->epilogue = 1;
break;
default:
break;
}
*res = root;
return 0; return 0;
} }
@@ -537,12 +554,12 @@ parse_begin(struct rifle_state *state)
utok1(state, &tok); utok1(state, &tok);
return -1; return -1;
} }
}
if (root != NULL) { if (root != NULL) {
if (cg_resolve_node(state, root) < 0) if (cg_resolve_node(state, root) < 0)
return -1; return -1;
} }
}
return 0; return 0;
} }

View File

@@ -25,6 +25,7 @@ typedef enum {
* @left: Left leaf * @left: Left leaf
* @right: Right leaf * @right: Right leaf
* @symbol: Symbol associated with node * @symbol: Symbol associated with node
* @epilogue: If true, represents block epilogue
*/ */
struct ast_node { struct ast_node {
ast_type_t type; ast_type_t type;
@@ -32,6 +33,7 @@ struct ast_node {
struct ast_node *left; struct ast_node *left;
struct ast_node *right; struct ast_node *right;
struct symbol *symbol; struct symbol *symbol;
uint8_t epilogue : 1;
}; };
/* /*