IR optimization

IR optimization currently very basic, in fact it probably doesn't even
improve performance measurably.
This commit is contained in:
r4 2021-12-25 23:01:18 +01:00
parent 850dafbbc9
commit cf93109f1e
3 changed files with 19 additions and 1 deletions

17
ir.c
View File

@ -124,9 +124,24 @@ void print_ir(IRToks *v, const BuiltinFunc *builtin_funcs) {
} }
break; break;
} }
default: ASSERT_UNREACHED(); break; default: ASSERT_UNREACHED();
} }
printf(" ; %zu:%zu", v->toks[i].ln, v->toks[i].col); printf(" ; %zu:%zu", v->toks[i].ln, v->toks[i].col);
printf("\n"); 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;
}
}
}

2
ir.h
View File

@ -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 print_ir(IRToks *v, const BuiltinFunc *builtin_funcs);
void optimize_ir(IRToks *v);
#endif /* IR_H */ #endif /* IR_H */

1
main.c
View File

@ -167,6 +167,7 @@ int main(int argc, const char **argv) {
return 1; return 1;
} }
toklist_term(&tokens); toklist_term(&tokens);
optimize_ir(&ir);
if (opt_emit_ir) if (opt_emit_ir)
print_ir(&ir, funcs); print_ir(&ir, funcs);
/* run the IR */ /* run the IR */