Fazendo loops
[pspdecompiler.git] / code.h
blob100bb3cdafa776b880dcabd9df86559ec1c3f3a4
1 #ifndef __CODE_H
2 #define __CODE_H
4 #include "prx.h"
5 #include "allegrex.h"
6 #include "alloc.h"
7 #include "lists.h"
8 #include "types.h"
10 #define ERROR_INVALID_OPCODE 1
11 #define ERROR_DELAY_SLOT 2
12 #define ERROR_TARGET_OUTSIDE_FILE 3
13 #define ERROR_DELAY_SLOT_TARGET 4
14 #define ERROR_ILLEGAL_BRANCH 5
16 #define LOCATION_REACHABLE 1
17 #define LOCATION_DELAY_SLOT 2
19 #define VARIABLES_NUM 32
20 #define VARIABLE_TYPE_REGISTER 1
21 #define VARIABLE_TYPE_STACK 2
23 #define VARDEF_TYPE_LOCATION 1
24 #define VARDEF_TYPE_CALL 2
25 #define VARDEF_TYPE_SUBINPUT 3
26 #define VARDEF_TYPE_SUBOUTPUT 4
27 #define VARDEF_TYPE_PHI 5
31 struct location {
32 uint32 opc;
33 uint32 address;
35 const struct allegrex_instruction *insn;
36 struct location *target;
38 list references;
39 int branchalways;
40 int reachable;
41 int error;
43 list usedvars;
44 list definedvars;
46 struct subroutine *sub;
47 struct basicblock *block;
48 struct codeswitch *cswitch;
51 struct codeswitch {
52 struct prx_reloc *jumpreloc;
53 struct prx_reloc *switchreloc;
54 struct location *location;
55 struct location *jumplocation;
56 list references;
57 int count;
58 int checked;
61 struct subroutine {
62 struct prx_function *export;
63 struct prx_function *import;
65 struct location *begin;
66 struct location *end;
68 struct basicblock *endblock;
69 list blocks, dfsblocks, revdfsblocks;
71 list loops;
72 list variables;
74 int haserror;
75 int dfscount;
78 struct vardef {
79 int type;
80 union {
81 struct basicedge *edge;
82 struct subroutine *sub;
83 struct location *loc;
84 list phiargs;
85 } value;
88 struct varuse {
89 int type;
90 union {
91 struct basicedge *edge;
92 struct subroutine *sub;
93 struct location *loc;
94 struct vardef *def;
95 } value;
98 struct variable {
99 int type;
100 int num;
102 struct vardef def;
103 list uses;
106 struct basicblock {
107 struct location *begin;
108 struct location *end;
110 struct location *jumploc;
112 struct basicblock *dominator;
113 struct basicblock *parent;
114 list frontier;
116 struct basicblock *revdominator;
117 struct basicblock *revparent;
119 list outrefs, inrefs;
120 int dfsnum, revdfsnum;
122 struct loopstruct *loop;
123 int mark1, mark2;
126 struct basicedge {
127 struct basicblock *from, *to;
129 struct subroutine *calltarget;
130 int hascall;
132 struct codeswitch *cswitch;
133 int switchnum;
136 struct ifstruct {
137 struct basicblock *begin;
138 struct basicblock *end;
141 struct loopstruct {
142 struct basicblock *start;
143 struct loopstruct *parent;
144 int maxdfsnum;
145 list edges;
148 struct code {
149 struct prx *file;
151 uint32 baddr, numopc;
152 struct location *base;
153 struct location *end;
155 list subroutines;
157 listpool lstpool;
158 fixedpool switchpool;
159 fixedpool subspool;
160 fixedpool blockspool;
161 fixedpool edgespool;
162 fixedpool varspool;
163 fixedpool ifpool;
164 fixedpool looppool;
168 struct code* code_analyse (struct prx *p);
169 void code_free (struct code *c);
171 int decode_instructions (struct code *c);
172 uint32 location_gpr_used (struct location *loc);
173 uint32 location_gpr_defined (struct location *loc);
175 void extract_switches (struct code *c);
176 void extract_subroutines (struct code *c);
178 void extract_cfg (struct code *c, struct subroutine *sub);
180 int cfg_dfs (struct subroutine *sub);
181 int cfg_revdfs (struct subroutine *sub);
182 struct basicblock *dom_intersect (struct basicblock *b1, struct basicblock *b2);
183 void cfg_dominance (struct subroutine *sub);
184 struct basicblock *dom_revintersect (struct basicblock *b1, struct basicblock *b2);
185 void cfg_revdominance (struct subroutine *sub);
186 void extract_loops (struct code *c, struct subroutine *sub);
189 void build_ssa (struct code *c, struct subroutine *sub);
192 #endif /* __CODE_H */