Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed May 7, 2024
1 parent 383f114 commit db2fe56
Show file tree
Hide file tree
Showing 30 changed files with 1,173 additions and 1,055 deletions.
7 changes: 0 additions & 7 deletions README.md

This file was deleted.

21 changes: 11 additions & 10 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
typedef struct {
size_t *ids;
size_t len;
} OpPath;
} vx_OpPath;

vx_OpPath vx_OpPath_copy_add(vx_OpPath path, size_t id);

typedef struct {
OpPath path;
vx_OpPath path;

const char *error;
const char *additional;
} VerifyError;
} vx_Error;

typedef struct {
VerifyError *items;
vx_Error *items;
size_t len;
} VerifyErrors;
} vx_Errors;

void verifyerrors_free(VerifyErrors errors);
void verifyerrors_print(VerifyErrors errors, FILE *dest);
OpPath oppath_copy_add(OpPath path, size_t id);
void verifyerrors_add(VerifyErrors *errors, const VerifyError *error);
void verifyerrors_add_all_and_free(VerifyErrors *dest, VerifyErrors *src);
void vx_Errors_add(vx_Errors *errors, const vx_Error *error);
void vx_Errors_add_all_and_free(vx_Errors *dest, vx_Errors *src);
void vx_Errors_free(vx_Errors errors);
void vx_Errors_print(vx_Errors errors, FILE *dest);

#endif //COMMON_H
20 changes: 10 additions & 10 deletions common/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@

#include "../common.h"

void verifyerrors_free(const VerifyErrors errors) {
void vx_Errors_free(const vx_Errors errors) {
for (size_t i = 0; i < errors.len; i ++) {
free(errors.items[i].path.ids);
}
free(errors.items);
}

OpPath oppath_copy_add(const OpPath path, const size_t id) {
OpPath new_path = path;
vx_OpPath vx_OpPath_copy_add(const vx_OpPath path, const size_t id) {
vx_OpPath new_path = path;
new_path.ids = malloc(sizeof(size_t) * (new_path.len + 1));
memcpy(new_path.ids, path.ids, sizeof(size_t) * new_path.len);
new_path.ids[new_path.len++] = id;
return new_path;
}

void verifyerrors_add(VerifyErrors *errors, const VerifyError *error) {
errors->items = realloc(errors->items, sizeof(VerifyError) * (errors->len + 1));
memcpy(&errors->items[errors->len ++], error, sizeof(VerifyError));
void vx_Errors_add(vx_Errors *errors, const vx_Error *error) {
errors->items = realloc(errors->items, sizeof(vx_Error) * (errors->len + 1));
memcpy(&errors->items[errors->len ++], error, sizeof(vx_Error));
}

void verifyerrors_add_all_and_free(VerifyErrors *dest, VerifyErrors *src) {
void vx_Errors_add_all_and_free(vx_Errors *dest, vx_Errors *src) {
for (size_t i = 0; i < src->len; i++) {
verifyerrors_add(dest, &src->items[i]);
vx_Errors_add(dest, &src->items[i]);
}
free(src->items);
// we don't free path on purpose!
}

void verifyerrors_print(const VerifyErrors errors, FILE *dest) {
void vx_Errors_print(const vx_Errors errors, FILE *dest) {
for (size_t i = 0; i < errors.len; i ++) {
const VerifyError err = errors.items[i];
const vx_Error err = errors.items[i];

fputs("In operation ", dest);
for (size_t j = 0; j < err.path.len; j ++) {
Expand Down
158 changes: 88 additions & 70 deletions ir/analysis.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <assert.h>

#include "ir.h"

bool irview_find(SsaView *view, const SsaOpType type) {
bool vx_IrView_find(view, type)
vx_IrView *view;
const vx_IrOpType type;
{
for (size_t i = view->start; i < view->end; i ++) {
if (view->block->ops[i].id == type) {
view->start = i;
Expand All @@ -11,24 +16,27 @@ bool irview_find(SsaView *view, const SsaOpType type) {
return false;
}

/** You should use `irblock_root(block)->as_root.vars[id].decl` instead! */
SsaOp *irblock_finddecl_var(const SsaBlock *block, const SsaVar var) {
/** You should use `vx_IrBlock_root(block)->as_root.vars[id].decl` instead! */
vx_IrOp *vx_IrBlock_find_var_decl(block, var)
const vx_IrBlock *block;
const vx_IrVar var;
{
for (size_t i = 0; i < block->ops_len; i ++) {
SsaOp *op = &block->ops[i];
vx_IrOp *op = &block->ops[i];

for (size_t j = 0; j < op->outs_len; j ++)
if (op->outs[j].var == var)
return op;

for (size_t j = 0; j < op->params_len; j ++) {
const SsaValue param = op->params[j].val;
const vx_IrValue param = op->params[j].val;

if (param.type == SSA_VAL_BLOCK) {
if (param.type == VX_IR_VALBLOCK) {
for (size_t k = 0; k < param.block->ins_len; k ++)
if (param.block->ins[k] == var)
return op;

SsaOp *res = irblock_finddecl_var(param.block, var);
vx_IrOp *res = vx_IrBlock_find_var_decl(param.block, var);
if (res != NULL)
return res;
}
Expand All @@ -39,19 +47,22 @@ SsaOp *irblock_finddecl_var(const SsaBlock *block, const SsaVar var) {
}


bool irblock_var_used(const SsaBlock *block, const SsaVar var) {
bool vx_IrBlock_var_used(block, var)
const vx_IrBlock *block;
const vx_IrVar var;
{
for (size_t i = 0; i < block->outs_len; i++)
if (block->outs[i] == var)
return true;

for (long int i = block->ops_len - 1; i >= 0; i--) {
const SsaOp *op = &block->ops[i];
const vx_IrOp *op = &block->ops[i];
for (size_t j = 0; j < op->params_len; j++) {
if (op->params[j].val.type == SSA_VAL_BLOCK) {
if (irblock_var_used(op->params[j].val.block, var)) {
if (op->params[j].val.type == VX_IR_VALBLOCK) {
if (vx_IrBlock_var_used(op->params[j].val.block, var)) {
return true;
}
} else if (op->params[j].val.type == SSA_VAL_VAR) {
} else if (op->params[j].val.type == VX_IR_VALVAR) {
if (op->params[j].val.var == var) {
return true;
}
Expand All @@ -62,17 +73,13 @@ bool irblock_var_used(const SsaBlock *block, const SsaVar var) {
return false;
}

// TODO: search more paths
bool irop_anyparam_hasvar(SsaOp *op, SsaVar var) {
for (size_t i = 0; i < op->params_len; i ++)
if (op->params[i].val.type == SSA_VAL_VAR && op->params[i].val.var == var)
return true;
return false;
}

SsaOp *irblock_inside_out_vardecl_before(const SsaBlock *block, const SsaVar var, size_t before) {
vx_IrOp *vx_IrBlock_inside_out_vardecl_before(block, var, before)
const vx_IrBlock *block;
const vx_IrVar var;
size_t before;
{
while (before --> 0) {
SsaOp *op = &block->ops[before];
vx_IrOp *op = &block->ops[before];

for (size_t i = 0; i < op->outs_len; i ++)
if (op->outs[i].var == var)
Expand All @@ -82,63 +89,74 @@ SsaOp *irblock_inside_out_vardecl_before(const SsaBlock *block, const SsaVar var
if (block->parent == NULL)
return NULL;

return irblock_inside_out_vardecl_before(block->parent, var, block->parent_index);
return vx_IrBlock_inside_out_vardecl_before(block->parent, var, block->parent_index);
}

bool irop_is_pure(SsaOp *op) {
bool vx_IrOp_is_volatile(op)
vx_IrOp *op;
{
switch (op->id) {
case SSA_OP_NOP:
case SSA_OP_IMM:
case SSA_OP_REINTERPRET:
case SSA_OP_ZEROEXT:
case SSA_OP_SIGNEXT:
case SSA_OP_TOFLT:
case SSA_OP_FROMFLT:
case SSA_OP_BITCAST:
case SSA_OP_ADD:
case SSA_OP_SUB:
case SSA_OP_MUL:
case SSA_OP_DIV:
case SSA_OP_MOD:
case SSA_OP_GT:
case SSA_OP_GTE:
case SSA_OP_LT:
case SSA_OP_LTE:
case SSA_OP_EQ:
case SSA_OP_NEQ:
case SSA_OP_NOT:
case SSA_OP_AND:
case SSA_OP_OR:
case SSA_OP_BITWISE_NOT:
case SSA_OP_BITWISE_AND:
case SSA_OP_BITIWSE_OR:
case SSA_OP_SHL:
case SSA_OP_SHR:
case SSA_OP_FOR:
return true;
case VX_IR_OP_NOP:
case VX_IR_OP_IMM:
case VX_IR_OP_REINTERPRET:
case VX_IR_OP_ZEROEXT:
case VX_IR_OP_SIGNEXT:
case VX_IR_OP_TOFLT:
case VX_IR_OP_FROMFLT:
case VX_IR_OP_BITCAST:
case VX_IR_OP_ADD:
case VX_IR_OP_SUB:
case VX_IR_OP_MUL:
case VX_IR_OP_DIV:
case VX_IR_OP_MOD:
case VX_IR_OP_GT:
case VX_IR_OP_GTE:
case VX_IR_OP_LT:
case VX_IR_OP_LTE:
case VX_IR_OP_EQ:
case VX_IR_OP_NEQ:
case VX_IR_OP_NOT:
case VX_IR_OP_AND:
case VX_IR_OP_OR:
case VX_IR_OP_BITWISE_NOT:
case VX_IR_OP_BITWISE_AND:
case VX_IR_OP_BITIWSE_OR:
case VX_IR_OP_SHL:
case VX_IR_OP_SHR:
case VX_IR_OP_FOR:
case VX_IR_OP_LOAD:
case VX_IR_OP_STORE:
case VX_IR_OP_PLACE:
return false;

case SSA_OP_REPEAT:
case CIR_OP_CFOR:
case SSA_OP_IF:
case SSA_OP_FLATTEN_PLEASE:
case SSA_OP_INFINITE:
case SSA_OP_WHILE:
case SSA_OP_FOREACH:
case SSA_OP_FOREACH_UNTIL:
for (size_t i = 0; i < op->params_len; i ++)
if (op->params[i].val.type == SSA_VAL_BLOCK)
if (!irblock_is_pure(op->params[i].val.block))
return false;
case VX_IR_OP_LOAD_VOLATILE:
case VX_IR_OP_STORE_VOLATILE:
return true;

case VX_IR_OP_REPEAT:
case CVX_IR_OP_CFOR:
case VX_IR_OP_IF:
case VX_IR_OP_FLATTEN_PLEASE:
case VX_IR_OP_INFINITE:
case VX_IR_OP_WHILE:
case VX_IR_OP_FOREACH:
case VX_IR_OP_FOREACH_UNTIL:
for (size_t i = 0; i < op->params_len; i ++)
if (op->params[i].val.type == VX_IR_VALBLOCK)
if (vx_IrBlock_is_volatile(op->params[i].val.block))
return true;
return false;

default:
return false;
assert(false);
}
}

bool irblock_is_pure(const SsaBlock *block) {
bool vx_IrBlock_is_volatile(block)
const vx_IrBlock *block;
{
for (size_t j = 0; j < block->ops_len; j ++)
if (!irop_is_pure(&block->ops[j]))
return false;
return true;
if (vx_IrOp_is_volatile(&block->ops[j]))
return true;
return false;
}
Loading

0 comments on commit db2fe56

Please sign in to comment.