Emit .file and .loc assembler directives

With these directives, gdb can print out an error location when
a compiled program crashes.
This commit is contained in:
Rui Ueyama 2020-04-20 22:15:09 +09:00
parent 6647ad9b84
commit 1c91d1943a
2 changed files with 5 additions and 0 deletions

View File

@ -89,6 +89,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);
@ -177,6 +179,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();

1
main.c
View File

@ -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;
}