remote.c: remove autotoolized conditional includes
[rofl0r-VisualBoyAdvance.git] / src / exprNode.c
blob85cea26a7111352ab1e166801d9c24ab02b3c79a
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)
8 // any later version.
9 //
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.
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
23 #include "GBA.h"
24 #include "Port.h"
25 #include "elf.h"
26 #include "exprNode.h"
28 #ifndef __GNUC__
29 #define strdup _strdup
30 #endif
32 extern char *yytext;
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);
60 exprNodeClean(n);
62 n->print = exprNodeIdentifierPrint;
63 n->resolve = exprNodeIdentifierResolve;
64 return n;
67 bool exprNodeIdentifierResolve(Node *n, Function *f, CompileUnit *u)
69 Object *o;
70 if(elfGetObject(n->name, f, u, &o)) {
71 n->type = o->type;
72 n->location = elfDecodeLocation(f, o->location, &n->locType);
73 return true;
74 } else {
75 printf("Object %s not found\n", n->name);
77 return false;
80 void exprNodeIdentifierPrint(Node *n)
82 printf("%s", n->name);
85 Node *exprNodeNumber()
87 Node *n = (Node *)calloc(1, sizeof(Node));
89 exprNodeClean(n);
90 n->location = atoi(yytext);
91 n->type = &exprNodeType;
92 n->locType = LOCATION_value;
93 n->print = exprNodeNumberPrint;
94 n->resolve = exprNodeNumberResolve;
95 return n;
98 bool exprNodeNumberResolve(Node *n, Function *f, CompileUnit *u)
100 return true;
103 void exprNodeNumberPrint(Node *n)
105 printf("%d", n->location);
108 Node *exprNodeStar(Node *exp)
110 Node *n = (Node *)calloc(1, sizeof(Node));
111 exprNodeClean(n);
113 n->expression = exp;
115 n->print = exprNodeStarPrint;
116 n->resolve = exprNodeStarResolve;
117 return n;
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;
129 } else {
130 n->location = n->expression->location;
132 n->type = n->expression->type->pointer;
133 n->locType = LOCATION_memory;
134 return true;
135 } else {
136 printf("Object is not of pointer type\n");
139 return false;
142 void exprNodeStarPrint(Node *n)
144 printf("*");
145 n->expression->print(n->expression);
148 Node *exprNodeDot(Node *exp, Node *ident)
150 Node *n = (Node *)calloc(1, sizeof(Node));
151 exprNodeClean(n);
153 n->expression = exp;
154 n->name = ident->name;
156 n->print = exprNodeDotPrint;
157 n->resolve = exprNodeDotResolve;
158 return n;
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 ||
167 tt == TYPE_union) {
168 u32 loc = n->expression->location;
169 Type *t = n->expression->type;
170 int count = t->structure->memberCount;
171 int i = 0;
172 while(i < count) {
173 Member *m = &t->structure->members[i];
174 if(strcmp(m->name, n->name) == 0) {
175 // found member
176 n->type = m->type;
177 if(tt == TYPE_struct) {
178 n->location = elfDecodeLocation4(f, m->location, &n->locType,
179 loc);
180 n->objLocation = loc;
181 } else {
182 n->location = loc;
183 n->locType = n->expression->locType;
184 n->objLocation = loc;
186 n->member = m;
187 return true;
189 i++;
191 printf("Member %s not found\n", n->name);
192 } else {
193 printf("Object is not of structure type\n");
196 return false;
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));
208 exprNodeClean(n);
210 n->expression = exp;
211 n->name = ident->name;
213 n->print = exprNodeArrowPrint;
214 n->resolve = exprNodeArrowResolve;
215 return n;
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");
224 return false;
226 tt = n->expression->type->pointer->type;
228 if(tt == TYPE_struct ||
229 tt == TYPE_union) {
230 u32 loc = debuggerReadMemory(n->expression->location);
231 Type *t = n->expression->type->pointer;
232 int count = t->structure->memberCount;
233 int i = 0;
234 while(i < count) {
235 Member *m = &t->structure->members[i];
236 if(strcmp(m->name, n->name) == 0) {
237 // found member
238 n->type = m->type;
239 if(tt == TYPE_struct) {
240 n->location = elfDecodeLocation4(f, m->location, &n->locType,
241 loc);
242 n->objLocation = loc;
243 } else {
244 n->location = loc;
245 n->objLocation = loc;
247 n->locType = LOCATION_memory;
248 n->member = m;
249 return true;
251 i++;
253 printf("Member %s not found\n", n->name);
254 } else {
255 printf("Object is not of structure type\n");
258 return false;
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));
270 exprNodeClean(n);
272 n->expression = exp;
274 n->print = exprNodeAddrPrint;
275 n->resolve = exprNodeAddrResolve;
276 return n;
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);
288 } else {
289 printf("Direct value is %d\n", n->location);
291 return true;
293 return false;
296 void exprNodeAddrPrint(Node *n)
298 printf("*");
299 n->expression->print(n->expression);
302 Node *exprNodeSizeof(Node *exp)
304 Node *n = (Node *)calloc(1, sizeof(Node));
305 exprNodeClean(n);
307 n->expression = exp;
309 n->print = exprNodeSizeofPrint;
310 n->resolve = exprNodeSizeofResolve;
311 return n;
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;
320 return true;
322 return false;
325 void exprNodeSizeofPrint(Node *n)
327 printf("sizeof(");
328 n->expression->print(n->expression);
329 printf(")");
332 Node *exprNodeArray(Node *exp, Node *number)
334 Node *n = (Node *)calloc(1, sizeof(Node));
335 exprNodeClean(n);
337 n->expression = exp;
338 n->value = number->location;
340 n->print = exprNodeArrayPrint;
341 n->resolve = exprNodeArrayResolve;
342 return n;
345 int exprNodeGetSize(Array *a, int index)
347 index++;
348 if(index == a->maxBounds) {
349 return a->type->size;
350 } else {
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];
356 return size;
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");
367 return false;
370 if(tt == TYPE_array) {
371 Array *a = n->expression->type->array;
373 u32 loc = n->expression->location;
374 Type *t = a->type;
375 if(a->maxBounds > 1) {
376 int index = n->expression->index;
378 if(index == a->maxBounds) {
379 printf("Too many indices for array\n");
380 return false;
383 if((index+1) < a->maxBounds) {
384 n->type = n->expression->type;
385 n->index = index+1;
386 n->locType = LOCATION_memory;
387 n->location = n->expression->location +
388 n->value * exprNodeGetSize(a, index);
389 return true;
392 n->type = t;
393 n->location = loc + n->value * t->size;
394 n->locType = LOCATION_memory;
395 } else {
396 Type *t = n->expression->type->pointer;
397 u32 loc = n->expression->location;
398 if(n->expression->locType == LOCATION_register)
399 loc = reg[loc].I;
400 else
401 loc = debuggerReadMemory(loc);
402 n->type = t;
403 n->location = loc + n->value * t->size;
404 n->locType = LOCATION_memory;
406 return true;
408 return false;
411 void exprNodeArrayPrint(Node *n)
413 n->expression->print(n->expression);
414 printf("[%d]", n->value);