Mercurial > vba-clojure
diff src/sdl/exprNode.cpp @ 28:2efb971df515
bringing in SDL package
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 04 Mar 2012 21:06:50 -0600 |
parents | f9f4f1b99eed |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdl/exprNode.cpp Sun Mar 04 21:06:50 2012 -0600 1.3 @@ -0,0 +1,411 @@ 1.4 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. 1.5 +// Copyright (C) 1999-2003 Forgotten 1.6 +// Copyright (C) 2004 Forgotten and the VBA development team 1.7 + 1.8 +// This program is free software; you can redistribute it and/or modify 1.9 +// it under the terms of the GNU General Public License as published by 1.10 +// the Free Software Foundation; either version 2, or(at your option) 1.11 +// any later version. 1.12 +// 1.13 +// This program is distributed in the hope that it will be useful, 1.14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +// GNU General Public License for more details. 1.17 +// 1.18 +// You should have received a copy of the GNU General Public License 1.19 +// along with this program; if not, write to the Free Software Foundation, 1.20 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.21 + 1.22 +#include <string.h> 1.23 +#include <stdio.h> 1.24 +#include <stdlib.h> 1.25 + 1.26 +#include "Port.h" 1.27 +#include "gba/GBAGlobals.h" 1.28 +#include "gba/elf.h" 1.29 +#include "exprNode.h" 1.30 + 1.31 +extern char *yytext; 1.32 + 1.33 +#define debuggerReadMemory(addr) \ 1.34 + READ32LE((&map[(addr)>>24].address[(addr) & map[(addr)>>24].mask])) 1.35 + 1.36 +void *exprNodeCleanUpList[100]; 1.37 +int exprNodeCleanUpCount = 0; 1.38 +Type exprNodeType = { 0, TYPE_base, "int", DW_ATE_signed, 4, 0, {0}, 0 }; 1.39 + 1.40 +void exprNodeClean(void *m) 1.41 +{ 1.42 + exprNodeCleanUpList[exprNodeCleanUpCount++] = m; 1.43 +} 1.44 + 1.45 +void exprNodeCleanUp() 1.46 +{ 1.47 + for(int i = 0; i < exprNodeCleanUpCount; i++) { 1.48 + free(exprNodeCleanUpList[i]); 1.49 + } 1.50 + exprNodeCleanUpCount = 0; 1.51 +} 1.52 + 1.53 +Node *exprNodeIdentifier() 1.54 +{ 1.55 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.56 + n->name = strdup(yytext); 1.57 + 1.58 + exprNodeClean(n->name); 1.59 + exprNodeClean(n); 1.60 + 1.61 + n->print = exprNodeIdentifierPrint; 1.62 + n->resolve = exprNodeIdentifierResolve; 1.63 + return n; 1.64 +} 1.65 + 1.66 +bool exprNodeIdentifierResolve(Node *n, Function *f, CompileUnit *u) 1.67 +{ 1.68 + Object *o; 1.69 + if(elfGetObject(n->name, f, u, &o)) { 1.70 + n->type = o->type; 1.71 + n->location = elfDecodeLocation(f, o->location, &n->locType); 1.72 + return true; 1.73 + } else { 1.74 + printf("Object %s not found\n", n->name); 1.75 + } 1.76 + return false; 1.77 +} 1.78 + 1.79 +void exprNodeIdentifierPrint(Node *n) 1.80 +{ 1.81 + printf("%s", n->name); 1.82 +} 1.83 + 1.84 +Node *exprNodeNumber() 1.85 +{ 1.86 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.87 + 1.88 + exprNodeClean(n); 1.89 + n->location = atoi(yytext); 1.90 + n->type = &exprNodeType; 1.91 + n->locType = LOCATION_value; 1.92 + n->print = exprNodeNumberPrint; 1.93 + n->resolve = exprNodeNumberResolve; 1.94 + return n; 1.95 +} 1.96 + 1.97 +bool exprNodeNumberResolve(Node *n, Function *f, CompileUnit *u) 1.98 +{ 1.99 + return true; 1.100 +} 1.101 + 1.102 +void exprNodeNumberPrint(Node *n) 1.103 +{ 1.104 + printf("%d", n->location); 1.105 +} 1.106 + 1.107 +Node *exprNodeStar(Node *exp) 1.108 +{ 1.109 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.110 + exprNodeClean(n); 1.111 + 1.112 + n->expression = exp; 1.113 + 1.114 + n->print = exprNodeStarPrint; 1.115 + n->resolve = exprNodeStarResolve; 1.116 + return n; 1.117 +} 1.118 + 1.119 +bool exprNodeStarResolve(Node *n, Function *f, CompileUnit *u) 1.120 +{ 1.121 + if(n->expression->resolve(n->expression, f, u)) { 1.122 + if(n->expression->type->type == TYPE_pointer) { 1.123 + n->location = n->expression->location; 1.124 + if(n->expression->locType == LOCATION_memory) { 1.125 + n->location = debuggerReadMemory(n->location); 1.126 + } else if(n->expression->locType == LOCATION_register) { 1.127 + n->location = reg[n->expression->location].I; 1.128 + } else { 1.129 + n->location = n->expression->location; 1.130 + } 1.131 + n->type = n->expression->type->pointer; 1.132 + n->locType = LOCATION_memory; 1.133 + return true; 1.134 + } else { 1.135 + printf("Object is not of pointer type\n"); 1.136 + } 1.137 + } 1.138 + return false; 1.139 +} 1.140 + 1.141 +void exprNodeStarPrint(Node *n) 1.142 +{ 1.143 + printf("*"); 1.144 + n->expression->print(n->expression); 1.145 +} 1.146 + 1.147 +Node *exprNodeDot(Node *exp, Node *ident) 1.148 +{ 1.149 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.150 + exprNodeClean(n); 1.151 + 1.152 + n->expression = exp; 1.153 + n->name = ident->name; 1.154 + 1.155 + n->print = exprNodeDotPrint; 1.156 + n->resolve = exprNodeDotResolve; 1.157 + return n; 1.158 +} 1.159 + 1.160 +bool exprNodeDotResolve(Node *n, Function *f, CompileUnit *u) 1.161 +{ 1.162 + if(n->expression->resolve(n->expression, f, u)) { 1.163 + TypeEnum tt = n->expression->type->type; 1.164 + 1.165 + if(tt == TYPE_struct || 1.166 + tt == TYPE_union) { 1.167 + u32 loc = n->expression->location; 1.168 + Type *t = n->expression->type; 1.169 + int count = t->structure->memberCount; 1.170 + int i = 0; 1.171 + while(i < count) { 1.172 + Member *m = &t->structure->members[i]; 1.173 + if(strcmp(m->name, n->name) == 0) { 1.174 + // found member 1.175 + n->type = m->type; 1.176 + if(tt == TYPE_struct) { 1.177 + n->location = elfDecodeLocation(f, m->location, &n->locType, 1.178 + loc); 1.179 + n->objLocation = loc; 1.180 + } else { 1.181 + n->location = loc; 1.182 + n->locType = n->expression->locType; 1.183 + n->objLocation = loc; 1.184 + } 1.185 + n->member = m; 1.186 + return true; 1.187 + } 1.188 + i++; 1.189 + } 1.190 + printf("Member %s not found\n", n->name); 1.191 + } else { 1.192 + printf("Object is not of structure type\n"); 1.193 + } 1.194 + } 1.195 + return false; 1.196 +} 1.197 + 1.198 +void exprNodeDotPrint(Node *n) 1.199 +{ 1.200 + n->expression->print(n->expression); 1.201 + printf(".%s", n->name); 1.202 +} 1.203 + 1.204 +Node *exprNodeArrow(Node *exp, Node *ident) 1.205 +{ 1.206 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.207 + exprNodeClean(n); 1.208 + 1.209 + n->expression = exp; 1.210 + n->name = ident->name; 1.211 + 1.212 + n->print = exprNodeArrowPrint; 1.213 + n->resolve = exprNodeArrowResolve; 1.214 + return n; 1.215 +} 1.216 + 1.217 +bool exprNodeArrowResolve(Node *n, Function *f, CompileUnit *u) 1.218 +{ 1.219 + if(n->expression->resolve(n->expression, f, u)) { 1.220 + TypeEnum tt = n->expression->type->type; 1.221 + if(tt != TYPE_pointer) { 1.222 + printf("Object not of pointer type\n"); 1.223 + return false; 1.224 + } 1.225 + tt = n->expression->type->pointer->type; 1.226 + 1.227 + if(tt == TYPE_struct || 1.228 + tt == TYPE_union) { 1.229 + u32 loc = debuggerReadMemory(n->expression->location); 1.230 + Type *t = n->expression->type->pointer; 1.231 + int count = t->structure->memberCount; 1.232 + int i = 0; 1.233 + while(i < count) { 1.234 + Member *m = &t->structure->members[i]; 1.235 + if(strcmp(m->name, n->name) == 0) { 1.236 + // found member 1.237 + n->type = m->type; 1.238 + if(tt == TYPE_struct) { 1.239 + n->location = elfDecodeLocation(f, m->location, &n->locType, 1.240 + loc); 1.241 + n->objLocation = loc; 1.242 + } else { 1.243 + n->location = loc; 1.244 + n->objLocation = loc; 1.245 + } 1.246 + n->locType = LOCATION_memory; 1.247 + n->member = m; 1.248 + return true; 1.249 + } 1.250 + i++; 1.251 + } 1.252 + printf("Member %s not found\n", n->name); 1.253 + } else { 1.254 + printf("Object is not of structure type\n"); 1.255 + } 1.256 + } 1.257 + return false; 1.258 +} 1.259 + 1.260 +void exprNodeArrowPrint(Node *n) 1.261 +{ 1.262 + n->expression->print(n->expression); 1.263 + printf("->%s", n->name); 1.264 +} 1.265 + 1.266 +Node *exprNodeAddr(Node *exp) 1.267 +{ 1.268 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.269 + exprNodeClean(n); 1.270 + 1.271 + n->expression = exp; 1.272 + 1.273 + n->print = exprNodeAddrPrint; 1.274 + n->resolve = exprNodeAddrResolve; 1.275 + return n; 1.276 +} 1.277 + 1.278 +bool exprNodeAddrResolve(Node *n, Function *f, CompileUnit *u) 1.279 +{ 1.280 + if(n->expression->resolve(n->expression, f, u)) { 1.281 + if(n->expression->locType == LOCATION_memory) { 1.282 + n->location = n->expression->location; 1.283 + n->locType = LOCATION_value; 1.284 + n->type = &exprNodeType; 1.285 + } else if(n->expression->locType == LOCATION_register) { 1.286 + printf("Value is in register %d\n", n->expression->location); 1.287 + } else { 1.288 + printf("Direct value is %d\n", n->location); 1.289 + } 1.290 + return true; 1.291 + } 1.292 + return false; 1.293 +} 1.294 + 1.295 +void exprNodeAddrPrint(Node *n) 1.296 +{ 1.297 + printf("*"); 1.298 + n->expression->print(n->expression); 1.299 +} 1.300 + 1.301 +Node *exprNodeSizeof(Node *exp) 1.302 +{ 1.303 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.304 + exprNodeClean(n); 1.305 + 1.306 + n->expression = exp; 1.307 + 1.308 + n->print = exprNodeSizeofPrint; 1.309 + n->resolve = exprNodeSizeofResolve; 1.310 + return n; 1.311 +} 1.312 + 1.313 +bool exprNodeSizeofResolve(Node *n, Function *f, CompileUnit *u) 1.314 +{ 1.315 + if(n->expression->resolve(n->expression, f, u)) { 1.316 + n->location = n->expression->type->size; 1.317 + n->locType = LOCATION_value; 1.318 + n->type = &exprNodeType; 1.319 + return true; 1.320 + } 1.321 + return false; 1.322 +} 1.323 + 1.324 +void exprNodeSizeofPrint(Node *n) 1.325 +{ 1.326 + printf("sizeof("); 1.327 + n->expression->print(n->expression); 1.328 + printf(")"); 1.329 +} 1.330 + 1.331 +Node *exprNodeArray(Node *exp, Node *number) 1.332 +{ 1.333 + Node *n = (Node *)calloc(1, sizeof(Node)); 1.334 + exprNodeClean(n); 1.335 + 1.336 + n->expression = exp; 1.337 + n->value = number->location; 1.338 + 1.339 + n->print = exprNodeArrayPrint; 1.340 + n->resolve = exprNodeArrayResolve; 1.341 + return n; 1.342 +} 1.343 + 1.344 +int exprNodeGetSize(Array *a, int index) 1.345 +{ 1.346 + index++; 1.347 + if(index == a->maxBounds) { 1.348 + return a->type->size; 1.349 + } else { 1.350 + int size = a->bounds[a->maxBounds-1] * a->type->size; 1.351 + 1.352 + for(int i = index; i < a->maxBounds-1; i++) { 1.353 + size *= a->bounds[i]; 1.354 + } 1.355 + return size; 1.356 + } 1.357 +} 1.358 + 1.359 +bool exprNodeArrayResolve(Node *n, Function *f, CompileUnit *u) 1.360 +{ 1.361 + if(n->expression->resolve(n->expression, f, u)) { 1.362 + TypeEnum tt = n->expression->type->type; 1.363 + if(tt != TYPE_array && 1.364 + tt != TYPE_pointer) { 1.365 + printf("Object not of array or pointer type\n"); 1.366 + return false; 1.367 + } 1.368 + 1.369 + if(tt == TYPE_array) { 1.370 + Array *a = n->expression->type->array; 1.371 + 1.372 + u32 loc = n->expression->location; 1.373 + Type *t = a->type; 1.374 + if(a->maxBounds > 1) { 1.375 + int index = n->expression->index; 1.376 + 1.377 + if(index == a->maxBounds) { 1.378 + printf("Too many indices for array\n"); 1.379 + return false; 1.380 + } 1.381 + 1.382 + if((index+1) < a->maxBounds) { 1.383 + n->type = n->expression->type; 1.384 + n->index = index+1; 1.385 + n->locType = LOCATION_memory; 1.386 + n->location = n->expression->location + 1.387 + n->value * exprNodeGetSize(a, index); 1.388 + return true; 1.389 + } 1.390 + } 1.391 + n->type = t; 1.392 + n->location = loc + n->value * t->size; 1.393 + n->locType = LOCATION_memory; 1.394 + } else { 1.395 + Type *t = n->expression->type->pointer; 1.396 + u32 loc = n->expression->location; 1.397 + if(n->expression->locType == LOCATION_register) 1.398 + loc = reg[loc].I; 1.399 + else 1.400 + loc = debuggerReadMemory(loc); 1.401 + n->type = t; 1.402 + n->location = loc + n->value * t->size; 1.403 + n->locType = LOCATION_memory; 1.404 + } 1.405 + return true; 1.406 + } 1.407 + return false; 1.408 +} 1.409 + 1.410 +void exprNodeArrayPrint(Node *n) 1.411 +{ 1.412 + n->expression->print(n->expression); 1.413 + printf("[%d]", n->value); 1.414 +}