11 #define RT(op) ((op >> 16) & 0x1F)
12 #define RS(op) ((op >> 21) & 0x1F)
13 #define RD(op) ((op >> 11) & 0x1F)
14 #define SA(op) ((op >> 6) & 0x1F)
15 #define IMM(op) ((signed short) (op & 0xFFFF))
16 #define IMMU(op) ((unsigned short) (op & 0xFFFF))
18 /* Subroutine decompilation status */
19 #define SUBROUTINE_EXTRACTED 1
20 #define SUBROUTINE_CFG_EXTRACTED 2
21 #define SUBROUTINE_OPERATIONS_EXTRACTED 4
22 #define SUBROUTINE_LIVE_REGISTERS 8
23 #define SUBROUTINE_CFG_TRAVERSE 16
24 #define SUBROUTINE_CFG_TRAVERSE_REV 32
25 #define SUBROUTINE_FIXUP_CALL_ARGS 64
26 #define SUBROUTINE_SSA 128
27 #define SUBROUTINE_CONSTANTS_EXTRACTED 256
28 #define SUBROUTINE_VARIABLES_EXTRACTED 512
29 #define SUBROUTINE_STRUCTURES_EXTRACTED 1024
32 #define REGISTER_GPR_ZERO 0
33 #define REGISTER_GPR_AT 1
34 #define REGISTER_GPR_V0 2
35 #define REGISTER_GPR_V1 3
36 #define REGISTER_GPR_A0 4
37 #define REGISTER_GPR_A1 5
38 #define REGISTER_GPR_A2 6
39 #define REGISTER_GPR_A3 7
40 #define REGISTER_GPR_T0 8
41 #define REGISTER_GPR_T1 9
42 #define REGISTER_GPR_T2 10
43 #define REGISTER_GPR_T3 11
44 #define REGISTER_GPR_T4 12
45 #define REGISTER_GPR_T5 13
46 #define REGISTER_GPR_T6 14
47 #define REGISTER_GPR_T7 15
48 #define REGISTER_GPR_S0 16
49 #define REGISTER_GPR_S1 17
50 #define REGISTER_GPR_S2 18
51 #define REGISTER_GPR_S3 19
52 #define REGISTER_GPR_S4 20
53 #define REGISTER_GPR_S5 21
54 #define REGISTER_GPR_S6 22
55 #define REGISTER_GPR_S7 23
56 #define REGISTER_GPR_T8 24
57 #define REGISTER_GPR_T9 25
58 #define REGISTER_GPR_K0 26
59 #define REGISTER_GPR_K1 27
60 #define REGISTER_GPR_GP 28
61 #define REGISTER_GPR_SP 29
62 #define REGISTER_GPR_FP 30
63 #define REGISTER_GPR_RA 31
64 #define REGISTER_LO 32
65 #define REGISTER_HI 33
66 #define NUM_REGISTERS 34
67 #define NUM_REGMASK ((NUM_REGISTERS + 31) >> 4)
70 #define IS_BIT_SET(flags, bit) ((1 << ((bit) & 31)) & ((flags)[(bit) >> 5]))
71 #define BIT_SET(flags, bit) ((flags)[(bit) >> 5]) |= 1 << ((bit) & 31)
73 extern const uint32 regmask_call_gen
[NUM_REGMASK
];
74 extern const uint32 regmask_call_kill
[NUM_REGMASK
];
75 extern const uint32 regmask_subend_gen
[NUM_REGMASK
];
76 extern const uint32 regmask_localvars
[NUM_REGMASK
];
79 /* Possible reachable status */
80 enum locationreachable
{
81 LOCATION_UNREACHABLE
= 0, /* Location is not reachable by any means */
82 LOCATION_REACHABLE
, /* Location is reachable by some path */
83 LOCATION_DELAY_SLOT
/* Location is a delay slot of a reachable branch/jump */
86 /* If an error was detected one a location */
88 ERROR_NONE
= 0, /* No error */
89 ERROR_INVALID_OPCODE
, /* Opcode is not recognized */
90 ERROR_DELAY_SLOT
, /* Branch/jump inside a delay slot */
91 ERROR_TARGET_OUTSIDE_FILE
, /* Branch/jump target outside the code */
92 ERROR_ILLEGAL_BRANCH
/* Branch with a condition that can never occur, such as `bne $0, $0, target' */
95 /* Represents a location in the code */
97 uint32 opc
; /* The opcode (little-endian) */
98 uint32 address
; /* The virtual address of the location */
100 const struct allegrex_instruction
*insn
; /* The decoded instruction or null (illegal opcode) */
101 struct location
*target
; /* A possible target of a branch/jump */
103 list references
; /* Number of references to this target inside the same subroutine */
104 int branchalways
; /* True if this location is a branch that always occurs */
105 enum locationreachable reachable
; /* Reachable status */
106 enum locationerror error
; /* Error status */
108 struct subroutine
*sub
; /* Owner subroutine */
109 struct basicblock
*block
; /* Basic block mark (used when extracting basic blocks) */
110 struct codeswitch
*cswitch
; /* Code switch mark */
113 /* Represents a switch in the code */
115 struct prx_reloc
*jumpreloc
;
116 struct prx_reloc
*switchreloc
;
117 struct location
*location
; /* The location that loads the base address of the switch */
118 struct location
*jumplocation
; /* The location of the jump instruction */
119 list references
; /* A list of possible target locations (without repeating) */
120 int count
; /* How many possible targets this switch have */
121 int checked
; /* Is this switch valid? */
126 struct code
*code
; /* The owner code of this subroutine */
127 struct prx_function
*export
; /* Is this a function export? */
128 struct prx_function
*import
; /* Is this a function import? */
130 struct location
*begin
; /* Where the subroutine begins */
131 struct location
*end
; /* Where the subroutine ends */
133 struct basicblock
*startblock
; /* Points to the START basic block of this subroutine */
134 struct basicblock
*firstblock
; /* Points to the first SIMPLE basic block of this subroutine */
135 struct basicblock
*endblock
; /* Points to the END basic block of this subroutine */
136 list blocks
; /* A list of the basic blocks of this subroutine */
137 list dfsblocks
, revdfsblocks
; /* Blocks ordered in DFS and Reverse-DFS order */
139 list whereused
; /* A list of basic blocks calling this subroutine */
140 list callblocks
; /* Inner blocks of type CALL */
144 int numregargs
, numregout
;
146 int haserror
, status
; /* Subroutine decompilation status */
150 /* Represents a pair of integers */
156 /* Abstract node in DFS and DOM trees (or reverse DFS and DOM trees) */
157 struct basicblocknode
{
158 int dfsnum
; /* The Depth-First search number */
159 struct intpair domdfsnum
; /* To determine ancestry information in the dominator tree */
160 struct basicblocknode
*dominator
; /* The dominator node */
161 struct basicblocknode
*parent
; /* The parent node (in the depth-first search) */
162 element blockel
; /* An element inside the list (dfsblocks or revdfsblocks) */
163 list children
; /* Children in the DFS tree */
164 list domchildren
; /* Children in the dominator tree */
165 list frontier
; /* The dominator frontier */
168 /* The type of the basic block */
169 enum basicblocktype
{
170 BLOCK_START
= 0, /* The first basic block in a subroutine */
171 BLOCK_SIMPLE
, /* A simple block */
172 BLOCK_CALL
, /* A block that represents a call */
173 BLOCK_END
/* The last basic block */
176 /* The basic block */
178 enum basicblocktype type
; /* The type of the basic block */
179 element blockel
; /* An element inside the list sub->blocks */
182 struct location
*begin
; /* The start of the simple block */
183 struct location
*end
; /* The end of the simple block */
184 struct location
*jumploc
; /* The jump/branch location inside the block */
187 struct subroutine
*calltarget
; /* The target of the call */
191 uint32 reg_gen
[NUM_REGMASK
], reg_kill
[NUM_REGMASK
];
192 uint32 reg_live_in
[NUM_REGMASK
], reg_live_out
[NUM_REGMASK
];
194 struct subroutine
*sub
; /* The owner subroutine */
196 struct basicblocknode node
; /* Node info for DFS and DOM trees */
197 struct basicblocknode revnode
; /* Node info for the reverse DFS and DOM trees */
199 list inrefs
, outrefs
; /* A list of in- and out-edges of this block */
201 struct loopstructure
*loopst
;
202 struct ifstructure
*ifst
;
204 int haslabel
, identsize
;
220 struct basicblock
*from
, *to
;
221 element fromel
, toel
;
235 struct variable
*variable
;
246 VARIABLE_CONSTANTUNK
,
251 enum variabletype type
;
256 struct operation
*def
;
272 enum operationtype type
;
273 struct basicblock
*block
;
277 enum allegrex_insn insn
;
278 struct location
*loc
;
281 struct location
*begin
, *end
;
301 struct loopstructure
{
303 struct basicblock
*start
;
304 struct basicblock
*end
;
310 struct basicblock
*end
;
316 /* Represents the entire PRX code */
318 struct prx
*file
; /* The PRX file */
320 uint32 baddr
, numopc
; /* The code segment base address and number of opcodes */
321 struct location
*base
; /* The code segment start */
322 struct location
*end
; /* The code segment end */
324 list subroutines
; /* The list of subroutines */
327 fixedpool switchpool
;
329 fixedpool blockspool
;
339 struct code
* code_analyse (struct prx
*p
);
340 void code_free (struct code
*c
);
342 int decode_instructions (struct code
*c
);
343 uint32
location_gpr_used (struct location
*loc
);
344 uint32
location_gpr_defined (struct location
*loc
);
345 int location_branch_may_swap (struct location
*branch
);
347 void extract_switches (struct code
*c
);
348 void extract_subroutines (struct code
*c
);
350 void extract_cfg (struct subroutine
*sub
);
351 void cfg_traverse (struct subroutine
*sub
, int reverse
);
352 int dom_isancestor (struct basicblocknode
*ancestor
, struct basicblocknode
*node
);
353 struct basicblocknode
*dom_common (struct basicblocknode
*n1
, struct basicblocknode
*n2
);
356 struct operation
*operation_alloc (struct basicblock
*block
);
357 struct value
*value_append (struct subroutine
*sub
, list l
, enum valuetype type
, uint32 value
);
358 void extract_operations (struct subroutine
*sub
);
359 void merge_block_operations (struct subroutine
*sub
);
360 void fixup_call_arguments (struct subroutine
*sub
);
361 void remove_call_arguments (struct subroutine
*sub
);
363 void live_registers (struct code
*c
);
364 void live_registers_imports (struct code
*c
);
366 void build_ssa (struct subroutine
*sub
);
367 void unbuild_ssa (struct subroutine
*sub
);
369 void abi_check (struct subroutine
*sub
);
371 void propagate_constants (struct subroutine
*sub
);
372 void extract_variables (struct subroutine
*sub
);
375 void reset_marks (struct subroutine
*sub
);
376 void extract_structures (struct subroutine
*sub
);
378 #endif /* __CODE_H */