2021-12-21 01:18:22 +01:00
|
|
|
#include "ir.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
const char *irinstr_str[IRInstrEnumSize] = {
|
2021-12-29 21:42:43 +01:00
|
|
|
[IRSet] = "set",
|
|
|
|
[IRNeg] = "neg",
|
|
|
|
[IRAdd] = "add",
|
|
|
|
[IRSub] = "sub",
|
|
|
|
[IRMul] = "mul",
|
|
|
|
[IRDiv] = "div",
|
|
|
|
[IREq] = "eq",
|
|
|
|
[IRNeq] = "neq",
|
|
|
|
[IRLt] = "lt",
|
|
|
|
[IRLe] = "le",
|
|
|
|
[IRNot] = "not",
|
|
|
|
[IRAnd] = "and",
|
|
|
|
[IROr] = "or",
|
|
|
|
[IRJmp] = "jmp",
|
|
|
|
[IRJnz] = "jnz",
|
2021-12-22 16:09:52 +01:00
|
|
|
[IRCallInternal] = "calli",
|
2021-12-29 21:42:43 +01:00
|
|
|
[IRAddrOf] = "addrof",
|
2021-12-30 17:59:28 +01:00
|
|
|
[IRArrMake] = "mkarr",
|
2021-12-21 01:18:22 +01:00
|
|
|
};
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
#define IRLIST_INIT_CAP_LONG 4096
|
|
|
|
#define IRLIST_INIT_CAP_SHORT 16
|
2021-12-21 01:18:22 +01:00
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
static void irlist_init_with_cap(IRList *v, size_t cap);
|
|
|
|
static IRItem *irlist_new_item(IRList *v);
|
2021-12-22 12:57:14 +01:00
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
static void irlist_init_with_cap(IRList *v, size_t cap) {
|
|
|
|
v->begin = NULL;
|
|
|
|
v->end = NULL;
|
|
|
|
v->p = pool_new(sizeof(IRItem) * cap);
|
|
|
|
v->index = NULL;
|
2021-12-21 01:18:22 +01:00
|
|
|
v->len = 0;
|
2021-12-22 12:52:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
static IRItem *irlist_new_item(IRList *v) {
|
|
|
|
IRItem *ret = pool_alloc(v->p, sizeof(IRItem));
|
|
|
|
ret->next = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
2021-12-22 12:52:16 +01:00
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void irlist_init_long(IRList *v) {
|
|
|
|
irlist_init_with_cap(v, IRLIST_INIT_CAP_LONG);
|
2021-12-22 12:52:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void irlist_init_short(IRList *v) {
|
|
|
|
irlist_init_with_cap(v, IRLIST_INIT_CAP_SHORT);
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 21:42:43 +01:00
|
|
|
static void free_irparam(IRParam *v, bool purge);
|
|
|
|
|
|
|
|
/* if purge is set, even statically allocated literals are freed */
|
|
|
|
static void free_irparam(IRParam *v, bool purge) {
|
|
|
|
if (v->kind == IRParamLiteral)
|
|
|
|
free_value(&v->Literal, purge);
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void irlist_term(IRList *v) {
|
|
|
|
for (IRItem *i = v->begin; i; i = i->next) {
|
2021-12-29 21:42:43 +01:00
|
|
|
switch (i->tok.instr) {
|
|
|
|
case IRSet:
|
|
|
|
case IRNeg:
|
|
|
|
case IRNot:
|
|
|
|
case IRAddrOf:
|
|
|
|
free_irparam(&i->tok.Unary.val, true);
|
|
|
|
break;
|
|
|
|
case IRAdd:
|
|
|
|
case IRSub:
|
|
|
|
case IRDiv:
|
|
|
|
case IRMul:
|
|
|
|
case IREq:
|
|
|
|
case IRNeq:
|
|
|
|
case IRLt:
|
|
|
|
case IRLe:
|
|
|
|
case IRAnd:
|
|
|
|
case IROr:
|
|
|
|
free_irparam(&i->tok.Binary.lhs, true);
|
|
|
|
free_irparam(&i->tok.Binary.rhs, true);
|
|
|
|
break;
|
|
|
|
case IRJmp:
|
|
|
|
break;
|
|
|
|
case IRJnz:
|
|
|
|
free_irparam(&i->tok.CJmp.condition, true);
|
|
|
|
break;
|
2021-12-30 17:59:28 +01:00
|
|
|
case IRCallInternal:
|
|
|
|
for (size_t j = 0; j < i->tok.CallI.n_args; j++)
|
2021-12-29 21:42:43 +01:00
|
|
|
free_irparam(&i->tok.CallI.args[j], true);
|
|
|
|
free(i->tok.CallI.args);
|
|
|
|
break;
|
2021-12-30 17:59:28 +01:00
|
|
|
case IRArrMake:
|
|
|
|
for (size_t j = 0; j < i->tok.ArrMake.len; j++)
|
|
|
|
free_irparam(&i->tok.ArrMake.vals[j], true);
|
|
|
|
free(i->tok.ArrMake.vals);
|
|
|
|
break;
|
2021-12-29 21:42:43 +01:00
|
|
|
default:
|
|
|
|
ASSERT_UNREACHED();
|
|
|
|
}
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
2021-12-29 13:27:58 +01:00
|
|
|
pool_term(v->p);
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void irlist_app(IRList *v, IRTok t) {
|
|
|
|
v->index = NULL; /* invalidate index */
|
|
|
|
IRItem *itm = irlist_new_item(v);
|
|
|
|
itm->tok = t;
|
|
|
|
|
|
|
|
if (!v->begin && !v->end)
|
|
|
|
v->begin = v->end = itm;
|
|
|
|
else {
|
|
|
|
v->end->next = itm;
|
|
|
|
v->end = itm;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->len++;
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void irlist_eat_irlist(IRList *v, IRList *other) {
|
|
|
|
v->index = NULL; /* invalidate index */
|
|
|
|
size_t jmp_offset = v->len-1;
|
|
|
|
for (IRItem *i = other->begin; i; i = i->next) {
|
2021-12-23 19:58:00 +01:00
|
|
|
/* correct for changed jump addresses */
|
2021-12-29 13:27:58 +01:00
|
|
|
if (i->tok.instr == IRJmp)
|
|
|
|
i->tok.Jmp.iaddr += jmp_offset;
|
|
|
|
else if (i->tok.instr == IRJnz)
|
|
|
|
i->tok.CJmp.iaddr += jmp_offset;
|
2021-12-23 19:58:00 +01:00
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
irlist_app(v, i->tok);
|
2021-12-23 19:58:00 +01:00
|
|
|
}
|
2021-12-29 13:27:58 +01:00
|
|
|
/* We're not calling irlist_term() because we don't want associated items
|
2021-12-23 19:58:00 +01:00
|
|
|
* (for example function arguments) to get deallocated as well. */
|
2021-12-29 13:27:58 +01:00
|
|
|
pool_term(other->p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void irlist_update_index(IRList *v) {
|
|
|
|
if (v->index)
|
|
|
|
return;
|
2021-12-29 21:42:43 +01:00
|
|
|
v->index = pool_alloc(v->p, sizeof(size_t) * v->len);
|
2021-12-29 13:27:58 +01:00
|
|
|
size_t num_idx = 0;
|
|
|
|
for (IRItem *i = v->begin; i; i = i->next, num_idx++)
|
|
|
|
v->index[num_idx] = i;
|
2021-12-22 12:52:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 01:18:22 +01:00
|
|
|
static void print_irparam(const IRParam *p);
|
|
|
|
|
|
|
|
static void print_irparam(const IRParam *p) {
|
|
|
|
if (p->kind == IRParamLiteral) {
|
2021-12-25 12:16:06 +01:00
|
|
|
print_value(&p->Literal, false);
|
2021-12-21 01:18:22 +01:00
|
|
|
} else if (p->kind == IRParamAddr) {
|
|
|
|
printf("%%%zd", p->Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void print_ir(IRList *v, const BuiltinFunc *builtin_funcs) {
|
|
|
|
size_t iaddr = 0;
|
|
|
|
for (IRItem *i = v->begin; i; i = i->next, iaddr++) {
|
|
|
|
printf("%04zx ", iaddr);
|
|
|
|
printf("%s", irinstr_str[i->tok.instr]);
|
|
|
|
switch (i->tok.instr) {
|
2021-12-21 01:18:22 +01:00
|
|
|
case IRSet:
|
|
|
|
case IRNeg:
|
2021-12-23 21:08:01 +01:00
|
|
|
case IRNot:
|
2021-12-28 13:39:12 +01:00
|
|
|
case IRAddrOf:
|
2021-12-29 13:27:58 +01:00
|
|
|
printf(" %%%zx ", i->tok.Unary.addr);
|
|
|
|
print_irparam(&i->tok.Unary.val);
|
2021-12-21 01:18:22 +01:00
|
|
|
break;
|
|
|
|
case IRAdd:
|
|
|
|
case IRSub:
|
|
|
|
case IRDiv:
|
|
|
|
case IRMul:
|
2021-12-23 21:06:49 +01:00
|
|
|
case IREq:
|
2021-12-26 12:19:54 +01:00
|
|
|
case IRNeq:
|
2021-12-23 21:06:49 +01:00
|
|
|
case IRLt:
|
|
|
|
case IRLe:
|
2021-12-23 21:42:09 +01:00
|
|
|
case IRAnd:
|
|
|
|
case IROr:
|
2021-12-29 13:27:58 +01:00
|
|
|
printf(" %%%zx ", i->tok.Binary.addr);
|
|
|
|
print_irparam(&i->tok.Binary.lhs);
|
2021-12-21 01:18:22 +01:00
|
|
|
printf(" ");
|
2021-12-29 13:27:58 +01:00
|
|
|
print_irparam(&i->tok.Binary.rhs);
|
2021-12-21 01:18:22 +01:00
|
|
|
break;
|
2021-12-21 13:59:08 +01:00
|
|
|
case IRJmp:
|
2021-12-29 13:27:58 +01:00
|
|
|
printf(" %zx", i->tok.Jmp.iaddr);
|
2021-12-21 13:59:08 +01:00
|
|
|
break;
|
2021-12-21 01:18:22 +01:00
|
|
|
case IRJnz:
|
|
|
|
printf(" ");
|
2021-12-29 13:27:58 +01:00
|
|
|
print_irparam(&i->tok.CJmp.condition);
|
|
|
|
printf(" %zx", i->tok.CJmp.iaddr);
|
2021-12-21 01:18:22 +01:00
|
|
|
break;
|
2021-12-22 16:09:52 +01:00
|
|
|
case IRCallInternal: {
|
2021-12-29 13:27:58 +01:00
|
|
|
const BuiltinFunc *f = &builtin_funcs[i->tok.CallI.fid];
|
2021-12-28 13:39:12 +01:00
|
|
|
if (f->returns)
|
2021-12-29 13:27:58 +01:00
|
|
|
printf(" %%%zx", i->tok.CallI.ret_addr);
|
2021-12-28 13:39:12 +01:00
|
|
|
printf(" %s", f->name);
|
2021-12-29 13:27:58 +01:00
|
|
|
for (size_t j = 0; j < i->tok.CallI.n_args; j++) {
|
2021-12-22 16:09:52 +01:00
|
|
|
printf(" ");
|
2021-12-29 13:27:58 +01:00
|
|
|
print_irparam(&i->tok.CallI.args[j]);
|
2021-12-22 16:09:52 +01:00
|
|
|
}
|
2021-12-21 01:18:22 +01:00
|
|
|
break;
|
2021-12-22 16:09:52 +01:00
|
|
|
}
|
2021-12-30 17:59:28 +01:00
|
|
|
case IRArrMake: {
|
|
|
|
printf(" %%%zx", i->tok.ArrMake.arr_addr);
|
|
|
|
for (size_t j = 0; j < i->tok.ArrMake.len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
print_irparam(&i->tok.ArrMake.vals[j]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-12-25 23:01:18 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
2021-12-29 13:27:58 +01:00
|
|
|
printf(" ; %zu:%zu", i->tok.ln, i->tok.col);
|
2021-12-21 01:18:22 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 23:01:18 +01:00
|
|
|
|
2021-12-29 13:27:58 +01:00
|
|
|
void optimize_ir(IRList *v) {
|
|
|
|
irlist_update_index(v);
|
|
|
|
for (IRItem *i = v->begin; i; i = i->next) {
|
|
|
|
switch (i->tok.instr) {
|
2021-12-25 23:01:18 +01:00
|
|
|
case IRJmp: {
|
|
|
|
/* resolve jump chains (esp. produced by if-else-if... statements) */
|
2021-12-29 13:27:58 +01:00
|
|
|
size_t ja = i->tok.Jmp.iaddr;
|
|
|
|
while (ja < v->len && v->index[ja]->tok.instr == IRJmp)
|
|
|
|
ja = v->index[ja]->tok.Jmp.iaddr;
|
|
|
|
i->tok.Jmp.iaddr = ja;
|
2021-12-25 23:01:18 +01:00
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|