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 4c4d1e6d92
commit ce4d744609
2 changed files with 5 additions and 0 deletions

View File

@ -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();

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