From 6d7597267eaebd96c68361d7d3ce91ce2bad521a Mon Sep 17 00:00:00 2001 From: Humberto Date: Thu, 7 May 2009 13:08:03 -0300 Subject: [PATCH] Renomeando as estruturas --- analyser.c | 8 ++++---- code.h | 34 ++++++++++++++++++++-------------- constants.c | 54 +++++++++++++++++++++++++++--------------------------- dataflow.c | 24 ++++++++++++------------ module.c | 2 ++ operations.c | 13 ------------- outcode.c | 24 ++++++++---------------- output.c | 32 +++++++++++++++++++++++++------- output.h | 1 + prx.h | 1 + ssa.c | 12 ++++++------ subroutines.c | 2 ++ 12 files changed, 108 insertions(+), 99 deletions(-) diff --git a/analyser.c b/analyser.c index 8f27c7c..f841e4d 100644 --- a/analyser.c +++ b/analyser.c @@ -17,7 +17,7 @@ struct code *code_alloc (void) c->subspool = fixedpool_create (sizeof (struct subroutine), 1024, TRUE); c->blockspool = fixedpool_create (sizeof (struct basicblock), 4096, TRUE); c->edgespool = fixedpool_create (sizeof (struct basicedge), 8192, TRUE); - c->varspool = fixedpool_create (sizeof (struct variable), 4096, TRUE); + c->ssavarspool = fixedpool_create (sizeof (struct ssavar), 4096, TRUE); c->opspool = fixedpool_create (sizeof (struct operation), 8192, TRUE); c->valspool = fixedpool_create (sizeof (struct value), 8192, TRUE); c->loopspool = fixedpool_create (sizeof (struct loopstructure), 256, TRUE); @@ -136,9 +136,9 @@ void code_free (struct code *c) fixedpool_destroy (c->edgespool, NULL, NULL); c->edgespool = NULL; - if (c->varspool) - fixedpool_destroy (c->varspool, NULL, NULL); - c->varspool = NULL; + if (c->ssavarspool) + fixedpool_destroy (c->ssavarspool, NULL, NULL); + c->ssavarspool = NULL; if (c->opspool) fixedpool_destroy (c->opspool, NULL, NULL); diff --git a/code.h b/code.h index 4afb5b9..6f1b59e 100644 --- a/code.h +++ b/code.h @@ -28,6 +28,10 @@ #define SUBROUTINE_VARIABLES_EXTRACTED 512 #define SUBROUTINE_STRUCTURES_EXTRACTED 1024 +/* Operation status */ +#define OPERATION_DEFERRED 1 + + /* Register values */ #define REGISTER_GPR_ZERO 0 #define REGISTER_GPR_AT 1 @@ -67,6 +71,9 @@ #define NUM_REGMASK ((NUM_REGISTERS + 31) >> 4) +#define MAX_SUB_ARGS 8 + + #define IS_BIT_SET(flags, bit) ((1 << ((bit) & 31)) & ((flags)[(bit) >> 5])) #define BIT_SET(flags, bit) ((flags)[(bit) >> 5]) |= 1 << ((bit) & 31) @@ -232,23 +239,23 @@ struct value { enum valuetype type; union { uint32 intval; - struct variable *variable; + struct ssavar *variable; } val; }; -enum variabletype { - VARIABLE_UNK = 0, - VARIABLE_LOCAL, - VARIABLE_ARGUMENT, - VARIABLE_TEMP, - VARIABLE_CONSTANT, - VARIABLE_CONSTANTUNK, - VARIABLE_INVALID +enum ssavartype { + SSAVAR_UNK = 0, + SSAVAR_LOCAL, + SSAVAR_ARGUMENT, + SSAVAR_TEMP, + SSAVAR_CONSTANT, + SSAVAR_CONSTANTUNK, + SSAVAR_INVALID }; -struct variable { - enum variabletype type; +struct ssavar { + enum ssavartype type; struct value name; uint32 info; @@ -286,7 +293,7 @@ struct operation { } callop; } info; - int deferred; + int status; list results; list operands; @@ -328,7 +335,7 @@ struct code { fixedpool subspool; fixedpool blockspool; fixedpool edgespool; - fixedpool varspool; + fixedpool ssavarspool; fixedpool opspool; fixedpool valspool; fixedpool loopspool; @@ -356,7 +363,6 @@ struct basicblocknode *dom_common (struct basicblocknode *n1, struct basicblockn struct operation *operation_alloc (struct basicblock *block); struct value *value_append (struct subroutine *sub, list l, enum valuetype type, uint32 value); void extract_operations (struct subroutine *sub); -void merge_block_operations (struct subroutine *sub); void fixup_call_arguments (struct subroutine *sub); void remove_call_arguments (struct subroutine *sub); diff --git a/constants.c b/constants.c index 4c74551..220a1d4 100644 --- a/constants.c +++ b/constants.c @@ -13,32 +13,32 @@ uint32 get_constant_value (struct value *val) } static -void combine_constants (struct variable *out, struct value *val) +void combine_constants (struct ssavar *out, struct value *val) { uint32 constant; - if (out->type == VARIABLE_UNK) return; + if (out->type == SSAVAR_UNK) return; if (val->type == VAL_REGISTER) { - out->type = VARIABLE_UNK; + out->type = SSAVAR_UNK; return; } if (val->type == VAL_VARIABLE) { - if (val->val.variable->type == VARIABLE_CONSTANTUNK) + if (val->val.variable->type == SSAVAR_CONSTANTUNK) return; - if (val->val.variable->type == VARIABLE_UNK) { - out->type = VARIABLE_UNK; + if (val->val.variable->type == SSAVAR_UNK) { + out->type = SSAVAR_UNK; return; } } constant = get_constant_value (val); - if (out->type == VARIABLE_CONSTANTUNK) { - out->type = VARIABLE_CONSTANT; + if (out->type == SSAVAR_CONSTANTUNK) { + out->type = SSAVAR_CONSTANT; out->info = constant; } else { if (out->info != constant) - out->type = VARIABLE_UNK; + out->type = SSAVAR_UNK; } } @@ -49,28 +49,28 @@ void propagate_constants (struct subroutine *sub) varel = list_head (sub->variables); while (varel) { - struct variable *var = element_getvalue (varel); - var->type = VARIABLE_CONSTANTUNK; + struct ssavar *var = element_getvalue (varel); + var->type = SSAVAR_CONSTANTUNK; if (var->def->type == OP_ASM || var->def->type == OP_CALL || var->def->type == OP_START || !(IS_BIT_SET (regmask_localvars, var->name.val.intval))) - var->type = VARIABLE_UNK; + var->type = SSAVAR_UNK; else list_inserttail (worklist, var); varel = element_next (varel); } while (list_size (worklist) != 0) { - struct variable *var = list_removehead (worklist); - struct variable temp; + struct ssavar *var = list_removehead (worklist); + struct ssavar temp; struct value *val; element opel; - if (var->type == VARIABLE_UNK) continue; + if (var->type == SSAVAR_UNK) continue; if (var->def->type == OP_PHI) { - temp.type = VARIABLE_CONSTANTUNK; + temp.type = SSAVAR_CONSTANTUNK; opel = list_head (var->def->operands); while (opel) { @@ -79,27 +79,27 @@ void propagate_constants (struct subroutine *sub) opel = element_next (opel); } } else { - temp.type = VARIABLE_CONSTANT; + temp.type = SSAVAR_CONSTANT; opel = list_head (var->def->operands); while (opel) { val = element_getvalue (opel); if (val->type == VAL_CONSTANT) { } else if (val->type == VAL_VARIABLE) { - if (val->val.variable->type == VARIABLE_UNK) - temp.type = VARIABLE_UNK; - else if (val->val.variable->type == VARIABLE_CONSTANTUNK && - temp.type != VARIABLE_UNK) - temp.type = VARIABLE_CONSTANTUNK; + if (val->val.variable->type == SSAVAR_UNK) + temp.type = SSAVAR_UNK; + else if (val->val.variable->type == SSAVAR_CONSTANTUNK && + temp.type != SSAVAR_UNK) + temp.type = SSAVAR_CONSTANTUNK; } opel = element_next (opel); } - if (temp.type == VARIABLE_CONSTANT) { + if (temp.type == SSAVAR_CONSTANT) { if (var->def->type == OP_MOVE) { val = list_headvalue (var->def->operands); temp.info = get_constant_value (val); - var->def->deferred = TRUE; + var->def->status |= OPERATION_DEFERRED; } else if (var->def->type == OP_INSTRUCTION) { uint32 val1, val2; switch (var->def->info.iop.insn) { @@ -108,16 +108,16 @@ void propagate_constants (struct subroutine *sub) val1 = get_constant_value (list_headvalue (var->def->operands)); val2 = get_constant_value (list_tailvalue (var->def->operands)); temp.info = val1 + val2; - var->def->deferred = TRUE; + var->def->status |= OPERATION_DEFERRED; break; case I_OR: val1 = get_constant_value (list_headvalue (var->def->operands)); val2 = get_constant_value (list_tailvalue (var->def->operands)); temp.info = val1 | val2; - var->def->deferred = TRUE; + var->def->status |= OPERATION_DEFERRED; break; default: - temp.type = VARIABLE_UNK; + temp.type = SSAVAR_UNK; break; } } diff --git a/dataflow.c b/dataflow.c index 135d532..b3d6cc6 100644 --- a/dataflow.c +++ b/dataflow.c @@ -5,7 +5,7 @@ const uint32 regmask_localvars[NUM_REGMASK] = { 0x43FFFFFE, 0x00000000 }; static -void mark_variable (struct variable *var, enum variabletype type, int num) +void mark_variable (struct ssavar *var, enum ssavartype type, int num) { element useel, phiel; struct value *val; @@ -19,13 +19,13 @@ void mark_variable (struct variable *var, enum variabletype type, int num) phiel = list_head (use->operands); while (phiel) { struct value *val = element_getvalue (phiel); - if (val->val.variable->type == VARIABLE_UNK) { + if (val->val.variable->type == SSAVAR_UNK) { mark_variable (val->val.variable, type, num); } phiel = element_next (phiel); } val = list_headvalue (use->results); - if (val->val.variable->type == VARIABLE_UNK) + if (val->val.variable->type == SSAVAR_UNK) mark_variable (val->val.variable, type, num); } useel = element_next (useel); @@ -35,7 +35,7 @@ void mark_variable (struct variable *var, enum variabletype type, int num) phiel = list_head (var->def->operands); while (phiel) { struct value *val = element_getvalue (phiel); - if (val->val.variable->type == VARIABLE_UNK) { + if (val->val.variable->type == SSAVAR_UNK) { mark_variable (val->val.variable, type, num); } phiel = element_next (phiel); @@ -50,14 +50,14 @@ void extract_variables (struct subroutine *sub) varel = list_head (sub->variables); while (varel) { - struct variable *var = element_getvalue (varel); - if (var->type == VARIABLE_UNK) { + struct ssavar *var = element_getvalue (varel); + if (var->type == SSAVAR_UNK) { if (IS_BIT_SET (regmask_localvars, var->name.val.intval)) { if (var->def->type == OP_START) { - mark_variable (var, VARIABLE_ARGUMENT, var->name.val.intval); + mark_variable (var, SSAVAR_ARGUMENT, var->name.val.intval); } else if (var->def->type == OP_CALL && var->name.val.intval != REGISTER_GPR_V0 && var->name.val.intval != REGISTER_GPR_V1) { - var->type = VARIABLE_INVALID; + var->type = SSAVAR_INVALID; } else { int istemp = FALSE; @@ -81,15 +81,15 @@ void extract_variables (struct subroutine *sub) } if (istemp) { - var->def->deferred = TRUE; - var->type = VARIABLE_TEMP; + var->def->status |= OPERATION_DEFERRED; + var->type = SSAVAR_TEMP; var->info = 0; } else { - mark_variable (var, VARIABLE_LOCAL, ++count); + mark_variable (var, SSAVAR_LOCAL, ++count); } } } else { - var->type = VARIABLE_ARGUMENT; + var->type = SSAVAR_ARGUMENT; } } varel = element_next (varel); diff --git a/module.c b/module.c index 43d04fe..6b68e3d 100644 --- a/module.c +++ b/module.c @@ -153,6 +153,7 @@ int load_module_import (struct prx *p, struct prx_import *imp) f->libname = imp->name; f->name = NULL; f->numargs = -1; + f->pfunc = NULL; } } @@ -236,6 +237,7 @@ int load_module_export (struct prx *p, struct prx_export *exp) f->name = NULL; f->libname = exp->name; f->numargs = -1; + f->pfunc = NULL; offset += 4; if (exp->namevaddr == 0) { f->name = resolve_syslib_nid (f->nid); diff --git a/operations.c b/operations.c index a164a75..8c31f84 100644 --- a/operations.c +++ b/operations.c @@ -498,19 +498,6 @@ void extract_operations (struct subroutine *sub) } -void merge_block_operations (struct subroutine *sub) -{ - element el, ref; - el = list_head (sub->blocks); - - while (el) { - struct basicblock *block = element_getvalue (el); - el = element_next (el); - } - -} - - void fixup_call_arguments (struct subroutine *sub) { struct operation *op; diff --git a/outcode.c b/outcode.c index 56ad6a7..8e63fdd 100644 --- a/outcode.c +++ b/outcode.c @@ -17,7 +17,7 @@ void print_block (FILE *out, struct basicblock *block, int reversecond) opel = list_head (block->operations); while (opel) { struct operation *op = element_getvalue (opel); - if (!op->deferred) { + if (!(op->status & OPERATION_DEFERRED)) { if (op->type == OP_INSTRUCTION) { if (op->info.iop.loc->insn->flags & (INSN_JUMP | INSN_BRANCH)) jumpop = op; @@ -153,15 +153,9 @@ void print_block_recursive (FILE *out, struct basicblock *block, int verbosity) static void print_subroutine (FILE *out, struct subroutine *sub, int verbosity) { - if (sub->import) { - fprintf (out, "/* "); - print_subroutine_name (out, sub); - fprintf (out, " num args %d out %d */\n", sub->numregargs, sub->numregout); - return; - } + if (sub->import) { return; } fprintf (out, "/**\n * Subroutine at address 0x%08X\n", sub->begin->address); - fprintf (out, " * Numargs: %d Numout: %d\n", sub->numregargs, sub->numregout); if (verbosity > 1 && !sub->haserror) { struct location *loc = sub->begin; for (loc = sub->begin; ; loc++) { @@ -170,9 +164,8 @@ void print_subroutine (FILE *out, struct subroutine *sub, int verbosity) } } fprintf (out, " */\n"); - fprintf (out, "void "); - print_subroutine_name (out, sub); - fprintf (out, " (void)\n{\n"); + print_subroutine_declaration (out, sub); + fprintf (out, "\n{\n"); if (sub->haserror) { struct location *loc; @@ -210,11 +203,10 @@ void print_source (FILE *out, struct code *c, char *headerfilename, int verbosit fprintf (out, "/*\n * Imports from library: %s\n */\n", imp->name); for (j = 0; j < imp->nfuncs; j++) { struct prx_function *func = &imp->funcs[j]; - fprintf (out, "extern "); - if (func->name) { - fprintf (out, "void %s (void);\n",func->name); - } else { - fprintf (out, "void %s_%08X (void);\n", imp->name, func->nid); + if (func->pfunc) { + fprintf (out, "extern "); + print_subroutine_declaration (out, func->pfunc); + fprintf (out, ";\n"); } } fprintf (out, "\n"); diff --git a/output.c b/output.c index 6dd2b4f..b9e2f37 100644 --- a/output.c +++ b/output.c @@ -46,13 +46,31 @@ void print_subroutine_name (FILE *out, struct subroutine *sub) } } +void print_subroutine_declaration (FILE *out, struct subroutine *sub) +{ + int i; + if (sub->numregout > 0) + fprintf (out, "int "); + else + fprintf (out, "void "); + + print_subroutine_name (out, sub); + + fprintf (out, " ("); + for (i = 0; i < sub->numregargs; i++) { + if (i != 0) fprintf (out, ", "); + fprintf (out, "int arg%d", i + 1); + } + fprintf (out, ")"); +} + void print_value (FILE *out, struct value *val) { switch (val->type) { case VAL_CONSTANT: fprintf (out, "0x%08X", val->val.intval); break; case VAL_VARIABLE: switch (val->val.variable->type) { - case VARIABLE_ARGUMENT: + case SSAVAR_ARGUMENT: if (val->val.variable->name.val.intval >= REGISTER_GPR_A0 && val->val.variable->name.val.intval <= REGISTER_GPR_T3) { fprintf (out, "arg%d", val->val.variable->name.val.intval - REGISTER_GPR_A0 + 1); @@ -60,13 +78,13 @@ void print_value (FILE *out, struct value *val) print_value (out, &val->val.variable->name); } break; - case VARIABLE_LOCAL: + case SSAVAR_LOCAL: fprintf (out, "local%d", val->val.variable->info); break; - case VARIABLE_CONSTANT: + case SSAVAR_CONSTANT: fprintf (out, "0x%08X", val->val.variable->info); break; - case VARIABLE_TEMP: + case SSAVAR_TEMP: if (val->val.variable->def->type != OP_MOVE) fprintf (out, "("); print_operation (out, val->val.variable->def, 0, TRUE); @@ -165,7 +183,7 @@ void print_complexop (FILE *out, struct operation *op, const char *opsymbol, int struct value *val; val = element_getvalue (el); if (val->type == VAL_VARIABLE) { - if (val->val.variable->type == VARIABLE_INVALID) break; + if (val->val.variable->type == SSAVAR_INVALID) break; } if (el != list_head (op->operands)) fprintf (out, ", "); @@ -202,7 +220,7 @@ void print_call (FILE *out, struct operation *op, int options) struct value *val; val = element_getvalue (el); if (val->type == VAL_VARIABLE) { - if (val->val.variable->type == VARIABLE_INVALID) break; + if (val->val.variable->type == SSAVAR_INVALID) break; } if (el != list_head (op->info.callop.arguments)) fprintf (out, ", "); @@ -382,7 +400,7 @@ void print_memory_address (FILE *out, struct operation *op, int size, int isunsi val = list_headvalue (op->operands); if (val->type == VAL_VARIABLE) { - if (val->val.variable->type == VARIABLE_CONSTANT) { + if (val->val.variable->type == SSAVAR_CONSTANT) { address = val->val.variable->type; val = list_tailvalue (op->operands); address += val->val.intval; diff --git a/output.h b/output.h index 4203442..0fd2df7 100644 --- a/output.h +++ b/output.h @@ -26,6 +26,7 @@ void print_value (FILE *out, struct value *val); void print_operation (FILE *out, struct operation *op, int identsize, int options); void print_complexop (FILE *out, struct operation *op, const char *opsymbol, int options); void print_subroutine_name (FILE *out, struct subroutine *sub); +void print_subroutine_declaration (FILE *out, struct subroutine *sub); int print_code (struct code *c, char *filename, int verbosity); int print_graph (struct code *c, char *prxname, int options); diff --git a/prx.h b/prx.h index 12d19bf..20e88e4 100644 --- a/prx.h +++ b/prx.h @@ -184,6 +184,7 @@ struct prx_function { uint32 nid; const char *name; const char *libname; + void *pfunc; int numargs; }; diff --git a/ssa.c b/ssa.c index e721215..425596a 100644 --- a/ssa.c +++ b/ssa.c @@ -3,10 +3,10 @@ #include "utils.h" static -struct variable *alloc_variable (struct basicblock *block) +struct ssavar *alloc_variable (struct basicblock *block) { - struct variable *var; - var = fixedpool_alloc (block->sub->code->varspool); + struct ssavar *var; + var = fixedpool_alloc (block->sub->code->ssavarspool); var->uses = list_alloc (block->sub->code->lstpool); return var; } @@ -76,7 +76,7 @@ void ssa_search (struct basicblock *block, list *vars) el = list_head (block->operations); while (el) { struct operation *op; - struct variable *var; + struct ssavar *var; struct value *val; element opel, rel; @@ -251,9 +251,9 @@ void unbuild_ssa (struct subroutine *sub) varel = list_head (sub->variables); while (varel) { - struct variable *var = element_getvalue (varel); + struct ssavar *var = element_getvalue (varel); list_free (var->uses); - fixedpool_free (sub->code->varspool, var); + fixedpool_free (sub->code->ssavarspool, var); varel = element_next (varel); } list_free (sub->variables); diff --git a/subroutines.c b/subroutines.c index f08a421..cba26c1 100644 --- a/subroutines.c +++ b/subroutines.c @@ -158,6 +158,7 @@ void extract_from_exports (struct code *c) loc = &c->base[tgt]; new_subroutine (c, loc, NULL, func); + func->pfunc = loc->sub; } } } @@ -184,6 +185,7 @@ void extract_from_imports (struct code *c) loc = &c->base[tgt]; new_subroutine (c, loc, func, NULL); + func->pfunc = loc->sub; loc->sub->numregargs = func->numargs; } } -- 2.11.4.GIT