1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #define strdup _strdup
34 #define debuggerReadMemory(addr) \
35 READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask]))
37 void *exprNodeCleanUpList
[100];
38 int exprNodeCleanUpCount
= 0;
39 Type exprNodeType
= { 0, TYPE_base
, "int", DW_ATE_signed
, 4, 0, {0}, 0 };
41 void exprNodeClean(void *m
)
43 exprNodeCleanUpList
[exprNodeCleanUpCount
++] = m
;
46 void exprNodeCleanUp()
48 for(int i
= 0; i
< exprNodeCleanUpCount
; i
++) {
49 free(exprNodeCleanUpList
[i
]);
51 exprNodeCleanUpCount
= 0;
54 Node
*exprNodeIdentifier()
56 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
57 n
->name
= strdup(yytext
);
59 exprNodeClean(n
->name
);
62 n
->print
= exprNodeIdentifierPrint
;
63 n
->resolve
= exprNodeIdentifierResolve
;
67 bool exprNodeIdentifierResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
70 if(elfGetObject(n
->name
, f
, u
, &o
)) {
72 n
->location
= elfDecodeLocation(f
, o
->location
, &n
->locType
);
75 printf("Object %s not found\n", n
->name
);
80 void exprNodeIdentifierPrint(Node
*n
)
82 printf("%s", n
->name
);
85 Node
*exprNodeNumber()
87 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
90 n
->location
= atoi(yytext
);
91 n
->type
= &exprNodeType
;
92 n
->locType
= LOCATION_value
;
93 n
->print
= exprNodeNumberPrint
;
94 n
->resolve
= exprNodeNumberResolve
;
98 bool exprNodeNumberResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
103 void exprNodeNumberPrint(Node
*n
)
105 printf("%d", n
->location
);
108 Node
*exprNodeStar(Node
*exp
)
110 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
115 n
->print
= exprNodeStarPrint
;
116 n
->resolve
= exprNodeStarResolve
;
120 bool exprNodeStarResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
122 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
123 if(n
->expression
->type
->type
== TYPE_pointer
) {
124 n
->location
= n
->expression
->location
;
125 if(n
->expression
->locType
== LOCATION_memory
) {
126 n
->location
= debuggerReadMemory(n
->location
);
127 } else if(n
->expression
->locType
== LOCATION_register
) {
128 n
->location
= reg
[n
->expression
->location
].I
;
130 n
->location
= n
->expression
->location
;
132 n
->type
= n
->expression
->type
->pointer
;
133 n
->locType
= LOCATION_memory
;
136 printf("Object is not of pointer type\n");
142 void exprNodeStarPrint(Node
*n
)
145 n
->expression
->print(n
->expression
);
148 Node
*exprNodeDot(Node
*exp
, Node
*ident
)
150 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
154 n
->name
= ident
->name
;
156 n
->print
= exprNodeDotPrint
;
157 n
->resolve
= exprNodeDotResolve
;
161 bool exprNodeDotResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
163 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
164 TypeEnum tt
= n
->expression
->type
->type
;
166 if(tt
== TYPE_struct
||
168 u32 loc
= n
->expression
->location
;
169 Type
*t
= n
->expression
->type
;
170 int count
= t
->structure
->memberCount
;
173 Member
*m
= &t
->structure
->members
[i
];
174 if(strcmp(m
->name
, n
->name
) == 0) {
177 if(tt
== TYPE_struct
) {
178 n
->location
= elfDecodeLocation4(f
, m
->location
, &n
->locType
,
180 n
->objLocation
= loc
;
183 n
->locType
= n
->expression
->locType
;
184 n
->objLocation
= loc
;
191 printf("Member %s not found\n", n
->name
);
193 printf("Object is not of structure type\n");
199 void exprNodeDotPrint(Node
*n
)
201 n
->expression
->print(n
->expression
);
202 printf(".%s", n
->name
);
205 Node
*exprNodeArrow(Node
*exp
, Node
*ident
)
207 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
211 n
->name
= ident
->name
;
213 n
->print
= exprNodeArrowPrint
;
214 n
->resolve
= exprNodeArrowResolve
;
218 bool exprNodeArrowResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
220 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
221 TypeEnum tt
= n
->expression
->type
->type
;
222 if(tt
!= TYPE_pointer
) {
223 printf("Object not of pointer type\n");
226 tt
= n
->expression
->type
->pointer
->type
;
228 if(tt
== TYPE_struct
||
230 u32 loc
= debuggerReadMemory(n
->expression
->location
);
231 Type
*t
= n
->expression
->type
->pointer
;
232 int count
= t
->structure
->memberCount
;
235 Member
*m
= &t
->structure
->members
[i
];
236 if(strcmp(m
->name
, n
->name
) == 0) {
239 if(tt
== TYPE_struct
) {
240 n
->location
= elfDecodeLocation4(f
, m
->location
, &n
->locType
,
242 n
->objLocation
= loc
;
245 n
->objLocation
= loc
;
247 n
->locType
= LOCATION_memory
;
253 printf("Member %s not found\n", n
->name
);
255 printf("Object is not of structure type\n");
261 void exprNodeArrowPrint(Node
*n
)
263 n
->expression
->print(n
->expression
);
264 printf("->%s", n
->name
);
267 Node
*exprNodeAddr(Node
*exp
)
269 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
274 n
->print
= exprNodeAddrPrint
;
275 n
->resolve
= exprNodeAddrResolve
;
279 bool exprNodeAddrResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
281 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
282 if(n
->expression
->locType
== LOCATION_memory
) {
283 n
->location
= n
->expression
->location
;
284 n
->locType
= LOCATION_value
;
285 n
->type
= &exprNodeType
;
286 } else if(n
->expression
->locType
== LOCATION_register
) {
287 printf("Value is in register %d\n", n
->expression
->location
);
289 printf("Direct value is %d\n", n
->location
);
296 void exprNodeAddrPrint(Node
*n
)
299 n
->expression
->print(n
->expression
);
302 Node
*exprNodeSizeof(Node
*exp
)
304 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
309 n
->print
= exprNodeSizeofPrint
;
310 n
->resolve
= exprNodeSizeofResolve
;
314 bool exprNodeSizeofResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
316 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
317 n
->location
= n
->expression
->type
->size
;
318 n
->locType
= LOCATION_value
;
319 n
->type
= &exprNodeType
;
325 void exprNodeSizeofPrint(Node
*n
)
328 n
->expression
->print(n
->expression
);
332 Node
*exprNodeArray(Node
*exp
, Node
*number
)
334 Node
*n
= (Node
*)calloc(1, sizeof(Node
));
338 n
->value
= number
->location
;
340 n
->print
= exprNodeArrayPrint
;
341 n
->resolve
= exprNodeArrayResolve
;
345 int exprNodeGetSize(Array
*a
, int index
)
348 if(index
== a
->maxBounds
) {
349 return a
->type
->size
;
351 int size
= a
->bounds
[a
->maxBounds
-1] * a
->type
->size
;
353 for(int i
= index
; i
< a
->maxBounds
-1; i
++) {
354 size
*= a
->bounds
[i
];
360 bool exprNodeArrayResolve(Node
*n
, Function
*f
, CompileUnit
*u
)
362 if(n
->expression
->resolve(n
->expression
, f
, u
)) {
363 TypeEnum tt
= n
->expression
->type
->type
;
364 if(tt
!= TYPE_array
&&
365 tt
!= TYPE_pointer
) {
366 printf("Object not of array or pointer type\n");
370 if(tt
== TYPE_array
) {
371 Array
*a
= n
->expression
->type
->array
;
373 u32 loc
= n
->expression
->location
;
375 if(a
->maxBounds
> 1) {
376 int index
= n
->expression
->index
;
378 if(index
== a
->maxBounds
) {
379 printf("Too many indices for array\n");
383 if((index
+1) < a
->maxBounds
) {
384 n
->type
= n
->expression
->type
;
386 n
->locType
= LOCATION_memory
;
387 n
->location
= n
->expression
->location
+
388 n
->value
* exprNodeGetSize(a
, index
);
393 n
->location
= loc
+ n
->value
* t
->size
;
394 n
->locType
= LOCATION_memory
;
396 Type
*t
= n
->expression
->type
->pointer
;
397 u32 loc
= n
->expression
->location
;
398 if(n
->expression
->locType
== LOCATION_register
)
401 loc
= debuggerReadMemory(loc
);
403 n
->location
= loc
+ n
->value
* t
->size
;
404 n
->locType
= LOCATION_memory
;
411 void exprNodeArrayPrint(Node
*n
)
413 n
->expression
->print(n
->expression
);
414 printf("[%d]", n
->value
);