Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed Nov 14, 2024
1 parent c10b3a7 commit 8de56bb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
18 changes: 16 additions & 2 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ int vx_Target_parse(vx_Target* dest, const char * str)
return 0;
}

static bool x86_need_move_ret_to_arg(vx_CU* cu, vx_IrBlock* block, size_t ret_id)
{
// TODO
return ret_id >= 2;
}

static bool etca_need_move_ret_to_arg(vx_CU*, vx_IrBlock* block, size_t ret_id)
{
// TODO
return ret_id >= 1;
}

void vx_Target_info(vx_TargetInfo* dest, vx_Target const* target)
{
memset(dest, 0, sizeof(vx_TargetInfo));
Expand All @@ -50,13 +62,15 @@ void vx_Target_info(vx_TargetInfo* dest, vx_Target const* target)
dest->cmov_opt = true; // TODO: target->flags.x86[vx_Target_X86_CMOV];
dest->tailcall_opt = true;
dest->ea_opt = true;
} break;
dest->need_move_ret_to_arg = x86_need_move_ret_to_arg;
} break;

case vx_TargetArch_ETCA:
{
dest->cmov_opt = target->flags.etca[vx_Target_ETCA_condExec];
dest->tailcall_opt = true;
} break;
dest->need_move_ret_to_arg = etca_need_move_ret_to_arg;
} break;

// add target
}
Expand Down
8 changes: 0 additions & 8 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,4 @@ typedef struct {
/** format: "arch" or "arch:ext"; 0 is ok */
int vx_Target_parse(vx_Target* dest, const char * str);

typedef struct {
bool cmov_opt;
bool tailcall_opt;
bool ea_opt;
} vx_TargetInfo;

void vx_Target_info(vx_TargetInfo* dest, vx_Target const* target);

#endif //COMMON_H
15 changes: 13 additions & 2 deletions ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,19 @@ typedef struct {
bool if_eval;
} vx_OptConfig;

/** single compilation unit */
typedef struct vx_CU vx_CU;

typedef struct {
bool cmov_opt;
bool tailcall_opt;
bool ea_opt;
bool (*need_move_ret_to_arg)(vx_CU* cu, vx_IrBlock* block, size_t ret_id);
} vx_TargetInfo;

void vx_Target_info(vx_TargetInfo* dest, vx_Target const* target);

/** single compilation unit */
struct vx_CU {
vx_Target target;
vx_TargetInfo info;

Expand All @@ -217,7 +228,7 @@ typedef struct {
// TODO: rename to symbols
vx_CUBlock * blocks;
size_t blocks_len;
} vx_CU;
};
/** targetStr is "arch:ext1,ext2" or "arch" */
void vx_CU_init(vx_CU* dest, const char * targetStr);

Expand Down
11 changes: 0 additions & 11 deletions ir/ops.cdef
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,4 @@ enum_entry_prefix "VX_IR_OP_"
sideEffect true
;

entry MEMSET args "addr: int, size: int, value: int"
debug "memset"
descr "fill every byte from addr to addr+size with value"
inlineCost 3 # bcz call to memset
execCost 4
endsFlow false
hasEffect true
volatile false
sideEffect true
;

}
50 changes: 50 additions & 0 deletions ir/transform/rets_to_args.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "../passes.h"

void vx_tra_move_rets_to_arg(vx_CU* cu, vx_IrBlock* block)
{
assert(block->is_root);

vx_IrType* ptrTy = vx_ptrType(block).ptr;

vx_IrTypedVar* newArgs = malloc(sizeof(vx_IrTypedVar) * block->outs_len);
size_t newArgs_count = 0;

vx_IrVar* newRets = malloc(sizeof(vx_IrVar) * block->outs_len);
size_t newRets_count = 0;

for (size_t i = 0; i < block->outs_len; i ++)
{
vx_IrVar var = block->outs[i];

if (!cu->info.move_ret_to_arg(cu, block, i)) {
newRets[newRets_count ++] = var;
continue;
}

vx_IrOp* mov = vx_IrBlock_addOpBuilding(block);
vx_IrOp_init(mov, VX_IR_OP_STORE, block);

vx_IrVar resVar = vx_IrBlock_newVar(block, mov);
newArgs[newArgs_count ++] = (vx_IrTypedVar) { .var = resVar, .type = ptrTy };

vx_IrOp_addParam_s(mov, VX_IR_NAME_ADDR, VX_IR_VALUE_VAR(resVar));
vx_IrOp_addParam_s(mov, VX_IR_NAME_VALUE, VX_IR_VALUE_VAR(var));
}

if (newArgs_count == 0)
return;

free(block->outs);
block->outs = newRets;
block->outs_len = newRets_count;

// TODO: multiple insert function
vx_IrTypedVar* oldIns = block->ins;
block->ins = malloc(sizeof(vx_IrTypedVar) * (block->ins_len + newArgs_count));
memcpy(block->ins, newArgs, sizeof(vx_IrTypedVar) * newArgs_count);
memcpy(block->ins + newArgs_count, oldIns, sizeof(vx_IrTypedVar) * block->ins_len);
block->ins_len += newArgs_count;
free(oldIns);

free(newArgs);
}

0 comments on commit 8de56bb

Please sign in to comment.