diff --git a/ir.c b/ir.c index 5158c14..be80a42 100644 --- a/ir.c +++ b/ir.c @@ -124,9 +124,24 @@ void print_ir(IRToks *v, const BuiltinFunc *builtin_funcs) { } break; } - default: ASSERT_UNREACHED(); break; + default: ASSERT_UNREACHED(); } printf(" ; %zu:%zu", v->toks[i].ln, v->toks[i].col); printf("\n"); } } + +void optimize_ir(IRToks *v) { + for (size_t i = 0; i < v->len; i++) { + switch (v->toks[i].instr) { + case IRJmp: { + /* resolve jump chains (esp. produced by if-else-if... statements) */ + size_t ja = i; + while (v->toks[ja].instr == IRJmp) + ja = v->toks[ja].Jmp.iaddr; + v->toks[i].Jmp.iaddr = ja; + } + default: break; + } + } +} diff --git a/ir.h b/ir.h index 01f6814..a229afd 100644 --- a/ir.h +++ b/ir.h @@ -97,4 +97,6 @@ void irtoks_eat_irtoks(IRToks *v, IRToks *other, size_t jmp_offset); void print_ir(IRToks *v, const BuiltinFunc *builtin_funcs); +void optimize_ir(IRToks *v); + #endif /* IR_H */ diff --git a/main.c b/main.c index bde91a6..8302cc7 100644 --- a/main.c +++ b/main.c @@ -167,6 +167,7 @@ int main(int argc, const char **argv) { return 1; } toklist_term(&tokens); + optimize_ir(&ir); if (opt_emit_ir) print_ir(&ir, funcs); /* run the IR */