commit 1ff9d1788f4cdba0bbf4ed5c91dc025166da7c1b
parent c3f3291221fa9ce4314b6ee0067326e166d8efe8
Author: Brian Swetland <swetland@frotz.net>
Date: Mon, 6 Dec 2021 03:38:29 -0800
compiler2: interpolate source in listing, fix if/else codegen
- tag AST nodes with source location (just linenumber for now)
- tag PC locations with source locations from AST nodes during codegen
- turn back on the source interpolation in listing generation
- fix the branch-to-exits in if/else codegen to be unconditional (oops!)
Diffstat:
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/codegen-risc5-simple.c b/src/codegen-risc5-simple.c
@@ -220,6 +220,10 @@ void gen_trace(str msg) {
// fprintf(stderr, "%p %p %s\n", err_last_func, err_ast, msg);
}
+void gen_src_xref(Ast node) {
+ ctx.xref[ctx.pc/4] = node->srcloc;
+}
+
void dump_error_ctxt() {
fprintf(stderr, "\n");
if (err_last_func) {
@@ -286,6 +290,7 @@ void reg_restore(u32 base, u32 mask) {
}
u32 gen_call(Ast node) {
+ gen_src_xref(node);
gen_trace("gen_call()");
Symbol sym = node->c0->sym;
Ast arg = node->c2;
@@ -356,6 +361,7 @@ u32 gen_relop(Ast node, u32 cc) {
u32 gen_expr(Ast node) {
err_ast = node;
+ gen_src_xref(node);
gen_trace("gen_expr()");
if (node->kind == AST_U32) {
u32 r = get_reg_tmp();
@@ -463,7 +469,7 @@ void gen_if_else(Ast node) {
node = node->c2;
while (node != nil) {
// jump from end of 'then' block to end
- gen_branch_fwd(EQ, &if_exit);
+ gen_branch_fwd(AL, &if_exit);
// patch false jump (over 'then' block) to here
fixup_branch_fwd(l0_br_false);
@@ -498,6 +504,7 @@ void gen_block(Ast node);
void gen_stmt(Ast node) {
err_ast = node;
+ gen_src_xref(node);
gen_trace("gen_stmt()\n");
u32 kind = node->kind;
if (kind == AST_EXPR) {
@@ -532,6 +539,7 @@ void gen_stmt(Ast node) {
void gen_block(Ast node) {
gen_trace("gen_block()\n");
+ gen_src_xref(node);
node = node->c2;
while (node != nil) {
gen_stmt(node);
@@ -563,6 +571,8 @@ void gen_block(Ast node) {
void gen_func(Ast node) {
err_last_func = node;
err_ast = node;
+
+ gen_src_xref(node);
gen_trace("gen_func()\n");
// local space plus saved lr and fp
diff --git a/src/compiler2.c b/src/compiler2.c
@@ -110,6 +110,8 @@ struct AstRec {
String name;
Symbol sym;
Type type;
+
+ u32 srcloc; // linenumber for now
};
enum {
@@ -256,6 +258,7 @@ struct CtxRec {
u32 code[8192];
u32 data[8192];
+ u32 xref[8192];
};
CtxRec ctx;
@@ -294,6 +297,7 @@ Ast ast_make(ast_t kind, u32 ival, String name, Symbol sym, Type type) {
a->name = name;
a->sym = sym;
a->type = type;
+ a->srcloc = ctx.linenumber;
return a;
}
@@ -1541,6 +1545,8 @@ Ast parse_global_var() {
}
Ast parse_expr_statement() {
+ u32 srcloc = ctx.linenumber;
+
Ast node = nil;
Ast left = parse_expr();
Ast right = nil;
@@ -1577,6 +1583,7 @@ Ast parse_expr_statement() {
// wrap in a statement node
Ast stmt = ast_make_simple(AST_EXPR, 0);
stmt->c0 = node;
+ stmt->srcloc = srcloc;
return stmt;
}
@@ -1983,7 +1990,7 @@ void listing_write(const char* listfn, const char* srcfn) {
char buf[1024];
while (n < ctx.pc) {
u32 ins = ctx.code[n/4];
-#if 0
+#if 1
if ((line < ctx.xref[n/4]) && fin) {
fprintf(fout, "\n");
while (line < ctx.xref[n/4]) {