Skip to content

Commit

Permalink
fix codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed Nov 29, 2024
1 parent 3642403 commit 53a9873
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 47 deletions.
4 changes: 2 additions & 2 deletions ir/lifetimes.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <assert.h>
#include "ir.h"
#include "passes.h"

static void extendLifetimeSegment(bool * bools, vx_IrOp * op)
{
Expand Down Expand Up @@ -27,7 +27,7 @@ static void extendLifetimeSegment(bool * bools, vx_IrOp * op)
}

/** only for root blocks */
void vx_IrBlock_lifetimes(vx_IrBlock *block) {
void vx_IrBlock_lifetimes(vx_CU* cu, vx_IrBlock *block) {
assert(block->is_root);
size_t numOps = vx_IrBlock_countOps(block);

Expand Down
10 changes: 8 additions & 2 deletions ir/passes.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void vx_opt_ll(vx_CU* cu, vx_IrBlock *block) {
void vx_llir_prep_lower(vx_CU* cu, vx_IrBlock *block) {
assert(block->is_root);
vx_IrBlock_llir_fix_decl(cu, block);
assert(block->is_root);
//vx_IrBlock_llir_compact(block); /TODO?
vx_IrBlock_lifetimes(cu, block);
vx_IrBlock_ll_share_slots(cu, block);
Expand Down Expand Up @@ -133,8 +134,13 @@ void vx_IrBlock_llir_fix_decl(vx_CU* cu, vx_IrBlock *root) {

// TODO: WHY THE FUCK IS THIS EVEN REQUIRED????

memset(root->as_root.labels, 0, sizeof(*root->as_root.labels) * root->as_root.labels_len);
memset(root->as_root.vars, 0, sizeof(*root->as_root.vars) * root->as_root.vars_len);
memset(root->as_root.labels, 0, sizeof(*root->as_root.labels) * root->as_root.labels_len);

for (size_t i = 0; i < root->as_root.vars_len; i ++) {
root->as_root.vars[i].decl = NULL;
root->as_root.vars[i].ll_type = NULL;
root->as_root.vars[i].ever_placed = false;
}

for (size_t i = 0; i < root->ins_len; i ++) {
vx_IrVar v = root->ins[i].var;
Expand Down
64 changes: 21 additions & 43 deletions targets/x86/cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,47 +1439,37 @@ void vx_cg_x86stupid_gen(vx_CU* _cu, vx_IrBlock* _block, FILE* out) {
}

varData = block->as_root.vars_len == 0 ? NULL : calloc(block->as_root.vars_len, sizeof(VarData));
for (size_t i = 0; i < block->ins_len; i ++) {
assert(varData);
varData[block->ins[i].var].type = block->ins[i].type;
assert(varData);

for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
varData[var].type = block->as_root.vars[var].ll_type;
}

size_t stackOff = 0;

/* ======================== VAR ALLOC ===================== */
signed long long highestHeat = 0;
for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
assert(varData);

vx_IrVar* varsHotFirst = calloc(block->as_root.vars_len, sizeof(vx_IrVar));
bool* varsSorted = calloc(block->as_root.vars_len, sizeof(bool));
size_t highestHeat = 0;
for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
size_t heat = block->as_root.vars[var].heat;
if (heat == 0)
fprintf(stderr, "warning: variable %%%zu has no heat\n", var);
if (heat > highestHeat) {
highestHeat = heat;
}
}

vx_IrVar* varsHotFirst = calloc(block->as_root.vars_len, sizeof(vx_IrVar));
size_t varsHotFirstLen = 0;

char* varsSorted = calloc(block->as_root.vars_len, sizeof(char));
if (use_rax) {
varsSorted[optLastRetFirstArg.var] = true;
}
for (size_t i = anyCalledIntArgsCount; i < block->ins_len; i ++) {
vx_IrVar var = block->ins[i].var;
varsSorted[var] = true;
}
for (; highestHeat >= 0; highestHeat --) {
for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
if (varsSorted[var]) continue;

if (block->as_root.vars[var].heat == highestHeat) {
varsHotFirst[varsHotFirstLen ++] = var;
varsSorted[var] = true;
}
}
}
free(varsSorted);
size_t varsHotFirstLen = 0;
for (; varsHotFirstLen != block->as_root.vars_len; highestHeat --) {
for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
if (varsSorted[var]) continue;
size_t heat = block->as_root.vars[var].heat;
if (heat == highestHeat) {
varsHotFirst[varsHotFirstLen++] = var;
varsSorted[var] = true;
}
}
}
free(varsSorted);

size_t varId = 0;
for (size_t i = 0; i < availableRegistersCount; i ++) {
Expand All @@ -1490,12 +1480,6 @@ void vx_cg_x86stupid_gen(vx_CU* _cu, vx_IrBlock* _block, FILE* out) {
for (; varId < varsHotFirstLen; varId ++) {
vx_IrVar var = varsHotFirst[varId];

assert(varData);
if (block->as_root.vars[var].heat == 0) {
varData[var].location = NULL;
continue;
}

vx_IrType* type = varData[var].type;
if (type == NULL) continue;

Expand Down Expand Up @@ -1532,7 +1516,6 @@ void vx_cg_x86stupid_gen(vx_CU* _cu, vx_IrBlock* _block, FILE* out) {
assert(var.type->kind == VX_IR_TYPE_KIND_BASE);
size_t size = vx_IrType_size(cu, block, var.type);
assert(size != 0);
assert(varData);

bool move_into_stack = block->as_root.vars[var.var].ever_placed;
move_into_stack = move_into_stack && (var.type->base.isfloat ? id_f < 8 : id_i < 6);
Expand Down Expand Up @@ -1609,11 +1592,6 @@ void vx_cg_x86stupid_gen(vx_CU* _cu, vx_IrBlock* _block, FILE* out) {
free(varsHotFirst);

for (vx_IrVar var = 0; var < block->as_root.vars_len; var ++) {
if (block->as_root.vars[var].heat == 0) {
varData[var].location = NULL;
continue;
}

if (varData[var].location) continue;
if (varData[var].type == NULL) continue;

Expand Down

0 comments on commit 53a9873

Please sign in to comment.