Skip to content

Commit

Permalink
refactor done
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed Nov 5, 2024
1 parent c10b3a7 commit cc12971
Show file tree
Hide file tree
Showing 34 changed files with 208 additions and 186 deletions.
2 changes: 1 addition & 1 deletion build.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct CompileData ir_files[] = {
struct CompileData ir_opt_files[] = {
DIR("build"),
DIR("build/ir"),
SP(CT_C, "ir/opt.c"),
SP(CT_C, "ir/passes.c"),

DIR("build/ir/opt"),
SP(CT_C, "ir/opt/tailcall.c"),
Expand Down
2 changes: 0 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/bash

set -e

: ${CFLAGS:="-Wall -Wextra -Wno-unused -Wno-unused-parameter -Wno-comment -Wno-format -Wno-sign-compare -Wno-char-subscripts -Wno-implicit-fallthrough -Wno-missing-braces -Werror -g -glldb"}
Expand Down
2 changes: 1 addition & 1 deletion build_c
Submodule build_c updated 1 files
+10 −5 cdef.py
12 changes: 4 additions & 8 deletions cg/x86_stupid/cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1320,11 +1320,7 @@ static vx_IrOp* emiti(vx_IrBlock* block, vx_IrOp *prev, vx_IrOp* op, FILE* file)
return op->next;
}

vx_cg_x86stupid vx_cg_x86stupid_options = (vx_cg_x86stupid) {
.use_red_zone = true,
};

void vx_cg_x86stupid_gen(vx_IrBlock* block, FILE* out) {
void vx_cg_x86stupid_gen(vx_CU* cu, vx_IrBlock* block, FILE* out) {
fprintf(out, "%s:\n", block->name);

assert(block->is_root);
Expand Down Expand Up @@ -1652,18 +1648,18 @@ void vx_cg_x86stupid_gen(vx_IrBlock* block, FILE* out) {

bool needProlog = (stackOff > 0 && !is_leaf) ||
stackOff > 128 ||
(stackOff > 0 && !vx_cg_x86stupid_options.use_red_zone);
(stackOff > 0 && cu->target.flags.x86[vx_Target_X86__NO_REDZONE]);

needEpilog = false;
if (anyPlaced || needProlog) {
emiti_enter(out);
needEpilog = true;
}

vx_IrBlock_ll_finalize(block, needEpilog);
vx_IrBlock_ll_finalize(cu, block, needEpilog);

if (needProlog) {
if (is_leaf && vx_cg_x86stupid_options.use_red_zone) {
if (is_leaf && cu->target.flags.x86[vx_Target_X86__NO_REDZONE]) {
size_t v = 128 - stackOff;
fprintf(out, "sub rsp, %zu\n", v);
} else {
Expand Down
2 changes: 1 addition & 1 deletion common/targets.cdef
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ table vx_TargetArch (
entry ETCA name "etca"
;

// add target
# // add target
}
87 changes: 4 additions & 83 deletions ir/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,6 @@ vx_IrOp *vx_IrBlock_tail(vx_IrBlock *block) {
return op;
}

void vx_IrBlock_llir_compact(vx_IrBlock *root) {
assert(root->is_root);

for (vx_IrVar idx = 0; idx < root->as_root.vars_len; idx ++) {
if (root->as_root.vars[idx].ll_type == NULL) {
if (idx + 1 >= root->as_root.vars_len) break;
for (vx_IrVar after = idx + 1; after < root->as_root.vars_len; after ++) {
assert(root != NULL && root->is_root);
vx_IrBlock_renameVar(root, after, after - 1, VX_RENAME_VAR_BOTH);
}
}
}
}

// TODO: remove cir checks and make sure fn called after cir type expand

size_t vx_IrType_size(vx_IrType *ty) {
Expand Down Expand Up @@ -176,71 +162,6 @@ vx_IrTypeRef vx_IrValue_type(vx_IrBlock* root, vx_IrValue value) {
}
}

void vx_CIrBlock_fix(vx_IrBlock* block) {
vx_IrBlock* root = vx_IrBlock_root(block);

for (size_t i = 0; i < block->ins_len; i ++)
vx_IrBlock_putVar(root, block->ins[i].var, NULL);

for (vx_IrOp* op = block->first; op; op = op->next) {
for (size_t i = 0; i < op->outs_len; i ++)
vx_IrBlock_putVar(root, op->outs[i].var, op);

FOR_INPUTS(op, inp, {
if (inp.type == VX_IR_VAL_BLOCK)
vx_CIrBlock_fix(inp.block);
});
}
}

void vx_IrBlock_llir_fix_decl(vx_IrBlock *root) {
assert(root->is_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);

for (size_t i = 0; i < root->ins_len; i ++) {
vx_IrVar v = root->ins[i].var;
assert(v < root->as_root.vars_len);
root->as_root.vars[v].ll_type = root->ins[i].type;
}

for (vx_IrOp *op = root->first; op; op = op->next) {
for (size_t j = 0; j < op->outs_len; j ++) {
vx_IrTypedVar out = op->outs[j];
if (vx_IrType_size(out.type) == 0) {
fprintf(stderr, "size of type %s in 0", out.type->debugName);
exit(1);
}
assert(out.var < root->as_root.vars_len);
vx_IrOp **decl = &root->as_root.vars[out.var].decl;
if (*decl == NULL) {
*decl = op;
root->as_root.vars[out.var].ll_type = out.type;
}
}

if (op->id == VX_IR_OP_LABEL) {
size_t id = vx_IrOp_param(op, VX_IR_NAME_ID)->id;
assert(id < root->as_root.labels_len);
vx_IrOp **decl = &root->as_root.labels[id].decl;
if (*decl == NULL) {
*decl = op;
}
}
}

for (vx_IrOp* op = root->first; op; op = op->next) {
if (op->id == VX_IR_OP_PLACE) {
vx_IrValue val = *vx_IrOp_param(op, VX_IR_NAME_VAR);
assert(val.type == VX_IR_VAL_VAR && "inliner fucked up (VX_IR_OP_PLACE)");
root->as_root.vars[val.var].ever_placed = true;
}
}
}

void vx_IrOp_warn(vx_IrOp *op, const char *optMsg0, const char *optMsg1) {
vx_IrBlock *parent = op ? op->parent : NULL;
if (parent) {
Expand Down Expand Up @@ -402,20 +323,20 @@ int vx_CU_compile(vx_CU * cu,
FOR_BLOCK({
vx_IrBlock_llir_preLower_loops(cu, block);
vx_IrBlock_llir_preLower_ifs(cu, block);
opt_preLower(cu, block);
vx_opt_preLower(cu, block);
vx_IrBlock_llir_lower(cu, block);
vx_IrBlock_llir_fix_decl(cu, block);
});

FOR_BLOCK({
opt_ll(cu, block);
vx_opt_ll(cu, block);

if (optionalOptimizedLlIr != NULL)
vx_IrBlock_dump(block, optionalOptimizedLlIr, 0);

llir_prep_lower(cu, block);

vx_IrBlock_dump(cu, block, stdout, 0);
vx_IrBlock_dump(block, stdout, 0);
});

if (optionalAsm) {
Expand All @@ -428,7 +349,7 @@ int vx_CU_compile(vx_CU * cu,
fprintf(optionalAsm, " global %s\n", cb->v.ir->name);
switch (cu->target.arch)
{
case VX_TARGET_X86:
case vx_TargetArch_X86_64:
vx_cg_x86stupid_gen(cu, cb->v.ir, optionalAsm);
break;

Expand Down
4 changes: 2 additions & 2 deletions ir/ops.cdef
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ table vx_IrOpType (
volatile bool ;
sideEffect bool ;

boolInv ? str ;
boolInv ? str ;
)
enum_entry_prefix "VX_IR_OP_"
{
Expand Down Expand Up @@ -182,7 +182,7 @@ enum_entry_prefix "VX_IR_OP_"

entry SUB args "a: value, b: value"
debug "sub"
descr "subctract the float or integer b from the float or intege a ; both values have to have the same type as the output type"
descr "subtract the float or integer b from the float or intege a ; both values have to have the same type as the output type"
inlineCost 1
execCost 2
endsFlow false
Expand Down
15 changes: 8 additions & 7 deletions ir/opt/cmov.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "../opt.h"
#include "../passes.h"

static vx_IrVar block_res_as_var(vx_IrBlock *parent, vx_IrBlock *block) {
vx_IrBlock* root = vx_IrBlock_root(parent);
assert(root);

vx_IrBlock_addAllOp(parent, block);
return block->outs[0];
}
Expand All @@ -24,10 +21,14 @@ static vx_IrVar block_res_as_var(vx_IrBlock *parent, vx_IrBlock *block) {
// cost of then + else is small enough
// exactly one out

void vx_opt_cmov(vx_IrBlock *block)
void vx_opt_cmov(vx_CU* cu, vx_IrBlock *block)
{
if (!cu->info.cmov_opt)
return;

vx_IrBlock* root = vx_IrBlock_root(block);
for (vx_IrOp *op = block->first; op; op = op->next) {
for (vx_IrOp *op = block->first; op; op = op->next)
{
if (op->id != VX_IR_OP_IF)
continue;

Expand All @@ -53,7 +54,7 @@ void vx_opt_cmov(vx_IrBlock *block)
continue;

size_t total_cost = vx_IrBlock_inlineCost(then) + vx_IrBlock_inlineCost(els);
if (total_cost > vx_g_optconfig.max_total_cmov_inline_cost)
if (total_cost > cu->opt.max_total_cmov_inline_cost)
continue;

vx_IrValue condv = *vx_IrOp_param(op, VX_IR_NAME_COND);
Expand Down
10 changes: 6 additions & 4 deletions ir/opt/comparisions.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../opt.h"
#include "../passes.h"

/**
* `a <= constant` -> `a < constant + 1`
Expand All @@ -8,10 +8,12 @@
* `!cmp(a,b)` -> `ncmp(a,b)`
* `constant == a` -> `a == constant`
*/
void vx_opt_comparisions(vx_IrBlock *block)
void vx_opt_comparisions(vx_CU* cu, vx_IrBlock *block)
{
for (vx_IrOp *op = block->first; op; op = op->next) {
switch (op->id) {
for (vx_IrOp *op = block->first; op; op = op->next)
{
switch (op->id)
{
case VX_IR_OP_NOT: {
vx_IrValue* oldres = vx_IrOp_param(op, VX_IR_NAME_VALUE);
vx_IrOp* cmp;
Expand Down
5 changes: 2 additions & 3 deletions ir/opt/constant_eval.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "../opt.h"

#include <assert.h>
#include "../passes.h"

void vx_opt_constant_eval(vx_IrBlock *block)
void vx_opt_constant_eval(vx_CU* cu, vx_IrBlock *block)
{
for (vx_IrOp *op = block->first; op; op = op->next) {
#define BINARY(typedest, what) { \
Expand Down
6 changes: 4 additions & 2 deletions ir/opt/dce.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../opt.h"
#include "../passes.h"

static void rmcode_before_label(vx_IrOp *op) {
for (; op; op = op->next) {
Expand All @@ -20,7 +20,9 @@ static bool labelUsed(vx_IrBlock* block, size_t label)
return false;
}

void vx_opt_ll_dce(vx_IrBlock *block) {
// TODO: rename to vx_opt_dce
void vx_opt_ll_dce(vx_CU* cu, vx_IrBlock *block)
{
for (vx_IrOp *op = block->first; op; op = op->next) {
if (op->id == VX_IR_OP_LABEL) {
size_t id = vx_IrOp_param(op, VX_IR_NAME_ID)->id;
Expand Down
6 changes: 3 additions & 3 deletions ir/opt/if_opts.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../opt.h"
#include "../passes.h"

// TODO: move to ir/transform.c
static vx_IrVar genIntoVar(vx_IrBlock* block, vx_IrValue val)
Expand All @@ -14,7 +14,7 @@ static vx_IrVar genIntoVar(vx_IrBlock* block, vx_IrValue val)
return var;
}

void vx_opt_if_opts(vx_IrBlock* block)
void vx_opt_if_opts(vx_CU* cu, vx_IrBlock* block)
{
// move code after if statements that partially end flow into the branch that doesn't end flow
for (vx_IrOp* op = block->first; op; op = op->next)
Expand Down Expand Up @@ -54,7 +54,7 @@ void vx_opt_if_opts(vx_IrBlock* block)
break; // there is no next anymore
}

vx_opt_ll_dce(block); // it says ll but also works in ssa
vx_opt_ll_dce(cu, block); // it says ll but also works in ssa

for (vx_IrOp* op = block->first; op; op = op->next)
{
Expand Down
17 changes: 11 additions & 6 deletions ir/opt/inline_vars.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "../opt.h"
#include "../passes.h"

// TODO: move substitude to transform

struct vx_IrView_substitute_var__data {
struct vx_IrView_substitute_var__data
{
vx_IrBlock *block;
vx_IrVar old;
vx_IrValue new;
vx_IrOp* newSrc;
};

static bool vx_IrView_substitute_var__trav(vx_IrOp *op, void *dataIn) {
static bool vx_IrView_substitute_var__trav(vx_IrOp *op, void *dataIn)
{
struct vx_IrView_substitute_var__data *data = dataIn;

/*
Expand Down Expand Up @@ -43,7 +46,8 @@ static bool vx_IrView_substitute_var__trav(vx_IrOp *op, void *dataIn) {
return false;
}

static void vx_IrBlock_substitute_var(vx_IrBlock *block, vx_IrVar old, vx_IrValue new, vx_IrOp* newSrc) {
static void vx_IrBlock_substitute_var(vx_IrBlock *block, vx_IrVar old, vx_IrValue new, vx_IrOp* newSrc)
{
struct vx_IrView_substitute_var__data data = { .block = block, .old = old, .new = new, .newSrc = newSrc };
vx_IrBlock_deepTraverse(block, vx_IrView_substitute_var__trav, &data);
}
Expand All @@ -67,11 +71,12 @@ static bool trav (vx_IrOp *op, void *data)
return false;
}

void vx_opt_inline_vars(vx_IrBlock *block) {
void vx_opt_inline_vars(vx_CU* cu, vx_IrBlock *block)
{
vx_IrBlock_deepTraverse(block, trav, block);
}

void vx_opt_ll_inlineVars(vx_IrBlock *block)
void vx_opt_ll_inlineVars(vx_CU* cu, vx_IrBlock *block)
{
for (vx_IrOp* imm = block->first; imm; imm = imm->next)
{
Expand Down
6 changes: 4 additions & 2 deletions ir/opt/join_compute.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "../opt.h"
#include <assert.h>
#include <string.h>
#include "../passes.h"

// TODO: move to analysis
static bool eq(vx_IrOp* a, vx_IrOp* b)
{
if (!( a->id == b->id
Expand All @@ -25,6 +26,7 @@ static bool eq(vx_IrOp* a, vx_IrOp* b)
return true;
}

// TODO: move to analysis
static vx_IrOp* findMatchingOutBefore(vx_IrBlock* block, vx_IrOp* before, vx_IrOp* cmp, bool stepIn, bool stepOut)
{
for (vx_IrOp* op = block->first; op && op != before; op = op->next)
Expand Down Expand Up @@ -56,7 +58,7 @@ static vx_IrOp* findMatchingOutBefore(vx_IrBlock* block, vx_IrOp* before, vx_IrO
/**
* Find identical computations and join them
*/
void vx_opt_join_compute(vx_IrBlock *block)
void vx_opt_join_compute(vx_CU* cu, vx_IrBlock *block)
{
for (vx_IrOp *op = block->first; op; op = op->next) {
if (vx_IrOp_isVolatile(op) || vx_IrOp_hasSideEffect(op))
Expand Down
4 changes: 2 additions & 2 deletions ir/opt/ll_binary.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../opt.h"
#include "../passes.h"

OPT_PASS void vx_opt_ll_binary(vx_IrBlock *block) {
void vx_opt_ll_binary(vx_CU* cu, vx_IrBlock *block) {
for (vx_IrOp* op = block->first; op; op = op->next) {
switch (op->id) {
case VX_IR_OP_EQ:
Expand Down
Loading

0 comments on commit cc12971

Please sign in to comment.