Skip to content

Commit

Permalink
so close
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed Jun 3, 2024
1 parent cc41772 commit 70b2573
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 20 deletions.
10 changes: 10 additions & 0 deletions ir/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

#include "ir.h"

bool vx_IrBlock_vardecl_is_in_ins(vx_IrBlock *block, vx_IrVar var) {
vx_IrBlock *root = vx_IrBlock_root(block);
for (size_t k = 0; k < root->ins_len; k ++) {
if (root->ins[k].var == var) {
return true;
}
}
return false;
}

// used for C IR transforms
//
// block is optional
Expand Down
1 change: 1 addition & 0 deletions ir/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void vx_IrBlock_add_op(vx_IrBlock *block,
vx_IrOp *new = vx_IrBlock_add_op_building(block);
*new = *op;

new->next = NULL;
new->parent = block;

// make sure that out variables and labels are in root block (add them if not)
Expand Down
5 changes: 2 additions & 3 deletions ir/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

vx_IrOp *vx_IrBlock_tail(vx_IrBlock *block) {
vx_IrOp *op = block->first;
if (!op)
return NULL;
while (op->next)
while (op && op->next) {
op = op->next;
}
return op;
}

Expand Down
1 change: 1 addition & 0 deletions ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ void vx_IrBlock_substitute_var(vx_IrBlock *block, vx_IrVar old, vx_IrValue newv)
bool vx_IrBlock_deep_traverse(vx_IrBlock *block, bool (*callback)(vx_IrOp *op, void *data), void *data);
bool vx_IrBlock_is_volatile(vx_IrBlock *block);
size_t vx_IrBlock_inline_cost(vx_IrBlock *block);
bool vx_IrBlock_vardecl_is_in_ins(vx_IrBlock *block, vx_IrVar var);

typedef enum {
VX_IR_NAME_OPERAND_A,
Expand Down
2 changes: 1 addition & 1 deletion ir/opt/ll_jumps.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ static void part1(vx_IrBlock *block) {
size_t label_id = vx_IrOp_param(op, VX_IR_NAME_ID)->id;

vx_IrOp *decl = root->as_root.labels[label_id].decl;

if (decl->parent != op->parent)
continue;

// TODO: SHOULD NOT WORK
if (decl < op) // can't optimize if label decl before this decl
continue;

Expand Down
2 changes: 1 addition & 1 deletion ir/opt/tailcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void vx_opt_ll_condtailcall(vx_IrBlock *block) {

size_t label = vx_IrOp_param(op, VX_IR_NAME_ID)->id;
vx_IrOp *label_op = block->as_root.labels[label].decl;
vx_IrOp *tailcall = label_op->next;
vx_IrOp *tailcall = label_op ? label_op->next : NULL;
if (tailcall == NULL)
continue;

Expand Down
27 changes: 17 additions & 10 deletions ir/verify_common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdlib.h>

#include "ir.h"
#include "verify.h"

void vx_error_param_type(vx_Errors *errors, const char *expected) {
Expand Down Expand Up @@ -156,6 +157,20 @@ void vx_IrBlock_verify_ssa_based(vx_Errors *dest, vx_IrBlock *block) {
(void) analyze_if(dest, op);
break;

case VX_LIR_GOTO:
case VX_LIR_COND: {
size_t label = vx_IrOp_param(op, VX_IR_NAME_ID)->id;
if (label >= root->as_root.labels_len || root->as_root.labels[label].decl == NULL) {
static char buf[256];
sprintf(buf, "Label %%%zu is never declared!", label);
vx_Error error = {
.error = "Undeclared label",
.additional = buf
};
vx_Errors_add(dest, &error);
}
} break;

default:
break;
}
Expand All @@ -177,16 +192,8 @@ void vx_IrBlock_verify_ssa_based(vx_Errors *dest, vx_IrBlock *block) {
.additional = buf
};
vx_Errors_add(dest, &error);
} else if (root->as_root.vars[var].decl == NULL) {
bool found = false;
for (size_t k = 0; k < root->ins_len; k ++) {
if (root->ins[k].var == var) {
found = true;
break;
}
}

if (!found) {
} else if (var >= root->as_root.vars_len || root->as_root.vars[var].decl == NULL) {
if (!vx_IrBlock_vardecl_is_in_ins(block, var)) {
static char buf[256];
sprintf(buf, "Variable %%%zu is never declared!", var);
vx_Error error = {
Expand Down
9 changes: 4 additions & 5 deletions ir/verify_ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct verify_vardecls_deeptraverse__data {
vx_IrVar var;
};

static void verify_vardecls_deeptraverse(vx_IrOp *op, void *dataIn) {
static bool verify_vardecls_deeptraverse(vx_IrOp *op, void *dataIn) {
struct verify_vardecls_deeptraverse__data *va = dataIn;

for (size_t i = 0; i < op->outs_len; i ++)
Expand All @@ -25,6 +25,8 @@ static void verify_vardecls_deeptraverse(vx_IrOp *op, void *dataIn) {
va->declcount ++;
}
}

return false;
}

vx_Errors vx_IrBlock_verify(vx_IrBlock *block) {
Expand All @@ -39,11 +41,10 @@ vx_Errors vx_IrBlock_verify(vx_IrBlock *block) {
if (block->as_root.vars[i].decl == NULL)
continue;

/*
struct verify_vardecls_deeptraverse__data dat;
dat.var = i;
dat.declcount = 0;
vx_IrView_deep_traverse(vx_IrView_of_all(block), verify_vardecls_deeptraverse, &dat);
vx_IrBlock_deep_traverse(block, verify_vardecls_deeptraverse, &dat);
// TODO: NEED TO SEARCH FROM ROOT

assert(dat.declcount > 0); // WE REMOVED VAR DECL WITHOUT REMOVING IT FROM INDEX
Expand All @@ -52,13 +53,11 @@ vx_Errors vx_IrBlock_verify(vx_IrBlock *block) {
static char buf[256];
sprintf(buf, "Variable %%%zu is assigned more than once!", i);
vx_Error error = {
.path = (vx_OpPath) { .ids = NULL, .len = 0 },
.error = "Variable assigned more than once",
.additional = buf
};
vx_Errors_add(&errors, &error);
}
*/
}
}

Expand Down

0 comments on commit 70b2573

Please sign in to comment.