From ce4d74460972aa2b3b668280550888feed5684f3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 20 Apr 2020 22:15:09 +0900 Subject: [PATCH] Emit .file and .loc assembler directives With these directives, gdb can print out an error location when a compiled program crashes. --- codegen.c | 4 ++++ main.c | 1 + 2 files changed, 5 insertions(+) diff --git a/codegen.c b/codegen.c index a276240..055f8c7 100644 --- a/codegen.c +++ b/codegen.c @@ -88,6 +88,8 @@ static void store(Type *ty) { // Generate code for a given node. static void gen_expr(Node *node) { + println(" .loc 1 %d", node->tok->line_no); + switch (node->kind) { case ND_NUM: println(" mov $%d, %%rax", node->val); @@ -172,6 +174,8 @@ static void gen_expr(Node *node) { } static void gen_stmt(Node *node) { + println(" .loc 1 %d", node->tok->line_no); + switch (node->kind) { case ND_IF: { int c = count(); diff --git a/main.c b/main.c index 59e08ec..914b08f 100644 --- a/main.c +++ b/main.c @@ -55,6 +55,7 @@ int main(int argc, char **argv) { // Traverse the AST to emit assembly. FILE *out = open_file(opt_o); + fprintf(out, ".file 1 \"%s\"\n", input_path); codegen(prog, out); return 0; }