| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "../../qcommon/q_platform.h" |
| 24 |
#include "cmdlib.h" |
| 25 |
#include "mathlib.h" |
| 26 |
#include "../../qcommon/qfiles.h" |
| 27 |
|
| 28 |
|
| 29 |
#define DEFAULT_HASHTABLE_SIZE 2048 |
| 30 |
|
| 31 |
char outputFilename[MAX_OS_PATH]; |
| 32 |
|
| 33 |
|
| 34 |
#define ZERO_PAGE_SIZE 0 // 256 |
| 35 |
|
| 36 |
typedef enum { |
| 37 |
OP_UNDEF, |
| 38 |
|
| 39 |
OP_IGNORE, |
| 40 |
|
| 41 |
OP_BREAK, |
| 42 |
|
| 43 |
OP_ENTER, |
| 44 |
OP_LEAVE, |
| 45 |
OP_CALL, |
| 46 |
OP_PUSH, |
| 47 |
OP_POP, |
| 48 |
|
| 49 |
OP_CONST, |
| 50 |
OP_LOCAL, |
| 51 |
|
| 52 |
OP_JUMP, |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
OP_EQ, |
| 57 |
OP_NE, |
| 58 |
|
| 59 |
OP_LTI, |
| 60 |
OP_LEI, |
| 61 |
OP_GTI, |
| 62 |
OP_GEI, |
| 63 |
|
| 64 |
OP_LTU, |
| 65 |
OP_LEU, |
| 66 |
OP_GTU, |
| 67 |
OP_GEU, |
| 68 |
|
| 69 |
OP_EQF, |
| 70 |
OP_NEF, |
| 71 |
|
| 72 |
OP_LTF, |
| 73 |
OP_LEF, |
| 74 |
OP_GTF, |
| 75 |
OP_GEF, |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
OP_LOAD1, |
| 80 |
OP_LOAD2, |
| 81 |
OP_LOAD4, |
| 82 |
OP_STORE1, |
| 83 |
OP_STORE2, |
| 84 |
OP_STORE4, |
| 85 |
OP_ARG, |
| 86 |
OP_BLOCK_COPY, |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
OP_SEX8, |
| 91 |
OP_SEX16, |
| 92 |
|
| 93 |
OP_NEGI, |
| 94 |
OP_ADD, |
| 95 |
OP_SUB, |
| 96 |
OP_DIVI, |
| 97 |
OP_DIVU, |
| 98 |
OP_MODI, |
| 99 |
OP_MODU, |
| 100 |
OP_MULI, |
| 101 |
OP_MULU, |
| 102 |
|
| 103 |
OP_BAND, |
| 104 |
OP_BOR, |
| 105 |
OP_BXOR, |
| 106 |
OP_BCOM, |
| 107 |
|
| 108 |
OP_LSH, |
| 109 |
OP_RSHI, |
| 110 |
OP_RSHU, |
| 111 |
|
| 112 |
OP_NEGF, |
| 113 |
OP_ADDF, |
| 114 |
OP_SUBF, |
| 115 |
OP_DIVF, |
| 116 |
OP_MULF, |
| 117 |
|
| 118 |
OP_CVIF, |
| 119 |
OP_CVFI |
| 120 |
} opcode_t; |
| 121 |
|
| 122 |
typedef struct { |
| 123 |
int imageBytes; |
| 124 |
int entryPoint; |
| 125 |
int stackBase; |
| 126 |
int stackSize; |
| 127 |
} executableHeader_t; |
| 128 |
|
| 129 |
typedef enum { |
| 130 |
CODESEG, |
| 131 |
DATASEG, |
| 132 |
LITSEG, |
| 133 |
BSSSEG, |
| 134 |
JTRGSEG, |
| 135 |
NUM_SEGMENTS |
| 136 |
} segmentName_t; |
| 137 |
|
| 138 |
#define MAX_IMAGE 0x400000 |
| 139 |
|
| 140 |
typedef struct { |
| 141 |
byte image[MAX_IMAGE]; |
| 142 |
int imageUsed; |
| 143 |
int segmentBase; |
| 144 |
} segment_t; |
| 145 |
|
| 146 |
typedef struct symbol_s { |
| 147 |
struct symbol_s *next; |
| 148 |
int hash; |
| 149 |
segment_t *segment; |
| 150 |
char *name; |
| 151 |
int value; |
| 152 |
} symbol_t; |
| 153 |
|
| 154 |
typedef struct hashchain_s { |
| 155 |
void *data; |
| 156 |
struct hashchain_s *next; |
| 157 |
} hashchain_t; |
| 158 |
|
| 159 |
typedef struct hashtable_s { |
| 160 |
int buckets; |
| 161 |
hashchain_t **table; |
| 162 |
} hashtable_t; |
| 163 |
|
| 164 |
int symtablelen = DEFAULT_HASHTABLE_SIZE; |
| 165 |
hashtable_t *symtable; |
| 166 |
hashtable_t *optable; |
| 167 |
|
| 168 |
segment_t segment[NUM_SEGMENTS]; |
| 169 |
segment_t *currentSegment; |
| 170 |
|
| 171 |
int passNumber; |
| 172 |
|
| 173 |
int numSymbols; |
| 174 |
int errorCount; |
| 175 |
|
| 176 |
typedef struct options_s { |
| 177 |
qboolean verbose; |
| 178 |
qboolean writeMapFile; |
| 179 |
qboolean vanillaQ3Compatibility; |
| 180 |
} options_t; |
| 181 |
|
| 182 |
options_t options = { 0 }; |
| 183 |
|
| 184 |
symbol_t *symbols; |
| 185 |
symbol_t *lastSymbol = 0; |
| 186 |
|
| 187 |
|
| 188 |
#define MAX_ASM_FILES 256 |
| 189 |
int numAsmFiles; |
| 190 |
char *asmFiles[MAX_ASM_FILES]; |
| 191 |
char *asmFileNames[MAX_ASM_FILES]; |
| 192 |
|
| 193 |
int currentFileIndex; |
| 194 |
char *currentFileName; |
| 195 |
int currentFileLine; |
| 196 |
|
| 197 |
|
| 198 |
int stackSize = 0x10000; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int currentLocals; |
| 204 |
int currentArgs; |
| 205 |
int currentArgOffset; |
| 206 |
|
| 207 |
#define MAX_LINE_LENGTH 1024 |
| 208 |
char lineBuffer[MAX_LINE_LENGTH]; |
| 209 |
int lineParseOffset; |
| 210 |
char token[MAX_LINE_LENGTH]; |
| 211 |
|
| 212 |
int instructionCount; |
| 213 |
|
| 214 |
typedef struct { |
| 215 |
char *name; |
| 216 |
int opcode; |
| 217 |
} sourceOps_t; |
| 218 |
|
| 219 |
sourceOps_t sourceOps[] = { |
| 220 |
#include "opstrings.h" |
| 221 |
}; |
| 222 |
|
| 223 |
#define NUM_SOURCE_OPS ( sizeof( sourceOps ) / sizeof( sourceOps[0] ) ) |
| 224 |
|
| 225 |
int opcodesHash[ NUM_SOURCE_OPS ]; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
static int vreport (const char* fmt, va_list vp) |
| 230 |
{ |
| 231 |
if (options.verbose != qtrue) |
| 232 |
return 0; |
| 233 |
return vprintf(fmt, vp); |
| 234 |
} |
| 235 |
|
| 236 |
static int report (const char *fmt, ...) |
| 237 |
{ |
| 238 |
va_list va; |
| 239 |
int retval; |
| 240 |
|
| 241 |
va_start(va, fmt); |
| 242 |
retval = vreport(fmt, va); |
| 243 |
va_end(va); |
| 244 |
return retval; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
static void hashtable_init (hashtable_t *H, int buckets) |
| 250 |
{ |
| 251 |
H->buckets = buckets; |
| 252 |
H->table = calloc(H->buckets, sizeof(*(H->table))); |
| 253 |
} |
| 254 |
|
| 255 |
static hashtable_t *hashtable_new (int buckets) |
| 256 |
{ |
| 257 |
hashtable_t *H; |
| 258 |
|
| 259 |
H = malloc(sizeof(hashtable_t)); |
| 260 |
hashtable_init(H, buckets); |
| 261 |
return H; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static void hashtable_add (hashtable_t *H, int hashvalue, void *datum) |
| 267 |
{ |
| 268 |
hashchain_t *hc, **hb; |
| 269 |
|
| 270 |
hashvalue = (abs(hashvalue) % H->buckets); |
| 271 |
hb = &(H->table[hashvalue]); |
| 272 |
if (*hb == 0) |
| 273 |
{ |
| 274 |
|
| 275 |
*hb = calloc(1, sizeof(**hb)); |
| 276 |
hc = *hb; |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
|
| 281 |
for (hc = *hb; hc && hc->next; hc = hc->next); |
| 282 |
hc->next = calloc(1, sizeof(*hc)); |
| 283 |
hc = hc->next; |
| 284 |
} |
| 285 |
hc->data = datum; |
| 286 |
hc->next = 0; |
| 287 |
} |
| 288 |
|
| 289 |
static hashchain_t *hashtable_get (hashtable_t *H, int hashvalue) |
| 290 |
{ |
| 291 |
hashvalue = (abs(hashvalue) % H->buckets); |
| 292 |
return (H->table[hashvalue]); |
| 293 |
} |
| 294 |
|
| 295 |
static void hashtable_stats (hashtable_t *H) |
| 296 |
{ |
| 297 |
int len, empties, longest, nodes; |
| 298 |
int i; |
| 299 |
float meanlen; |
| 300 |
hashchain_t *hc; |
| 301 |
|
| 302 |
report("Stats for hashtable %08X", H); |
| 303 |
empties = 0; |
| 304 |
longest = 0; |
| 305 |
nodes = 0; |
| 306 |
for (i = 0; i < H->buckets; i++) |
| 307 |
{ |
| 308 |
if (H->table[i] == 0) |
| 309 |
{ empties++; continue; } |
| 310 |
for (hc = H->table[i], len = 0; hc; hc = hc->next, len++); |
| 311 |
if (len > longest) { longest = len; } |
| 312 |
nodes += len; |
| 313 |
} |
| 314 |
meanlen = (float)(nodes) / (H->buckets - empties); |
| 315 |
#if 0 |
| 316 |
|
| 317 |
report(" Total buckets: %d\n", H->buckets); |
| 318 |
report(" Total stored nodes: %d\n", nodes); |
| 319 |
report(" Longest chain: %d\n", longest); |
| 320 |
report(" Empty chains: %d\n", empties); |
| 321 |
report(" Mean non-empty chain length: %f\n", meanlen); |
| 322 |
#else |
| 323 |
|
| 324 |
report(", %d buckets, %d nodes", H->buckets, nodes); |
| 325 |
report("\n"); |
| 326 |
report(" Longest chain: %d, empty chains: %d, mean non-empty: %f", longest, empties, meanlen); |
| 327 |
#endif |
| 328 |
report("\n"); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
static int hashtable_symbol_exists (hashtable_t *H, int hash, char *sym) |
| 336 |
{ |
| 337 |
hashchain_t *hc; |
| 338 |
symbol_t *s; |
| 339 |
|
| 340 |
hash = (abs(hash) % H->buckets); |
| 341 |
hc = H->table[hash]; |
| 342 |
if (hc == 0) |
| 343 |
{ |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
for (; hc; hc = hc->next) |
| 348 |
{ |
| 349 |
s = (symbol_t*)hc->data; |
| 350 |
|
| 351 |
|
| 352 |
if (strcmp(sym, s->name) == 0) |
| 353 |
{ |
| 354 |
|
| 355 |
return 1; |
| 356 |
} |
| 357 |
} |
| 358 |
return 0; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static int symlist_cmp (const void *e1, const void *e2) |
| 366 |
{ |
| 367 |
const symbol_t *a, *b; |
| 368 |
|
| 369 |
a = *(const symbol_t **)e1; |
| 370 |
b = *(const symbol_t **)e2; |
| 371 |
|
| 372 |
return ( a->value - b->value); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static void sort_symbols () |
| 382 |
{ |
| 383 |
int i, elems; |
| 384 |
symbol_t *s; |
| 385 |
symbol_t **symlist; |
| 386 |
|
| 387 |
if(!symbols) |
| 388 |
return; |
| 389 |
|
| 390 |
|
| 391 |
for (elems = 0, s = symbols; s; s = s->next, elems++) ; |
| 392 |
|
| 393 |
symlist = malloc(elems * sizeof(symbol_t*)); |
| 394 |
for (i = 0, s = symbols; s; s = s->next, i++) |
| 395 |
{ |
| 396 |
symlist[i] = s; |
| 397 |
} |
| 398 |
|
| 399 |
qsort(symlist, elems, sizeof(symbol_t*), symlist_cmp); |
| 400 |
|
| 401 |
s = symbols = symlist[0]; |
| 402 |
for (i = 1; i < elems; i++) |
| 403 |
{ |
| 404 |
s->next = symlist[i]; |
| 405 |
s = s->next; |
| 406 |
} |
| 407 |
lastSymbol = s; |
| 408 |
s->next = 0; |
| 409 |
|
| 410 |
for (i = 0, s = symbols; s; s = s->next, i++) ; |
| 411 |
|
| 412 |
free(symlist); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
#ifdef _MSC_VER |
| 417 |
#define INT64 __int64 |
| 418 |
#define atoi64 _atoi64 |
| 419 |
#else |
| 420 |
#define INT64 long long int |
| 421 |
#define atoi64 atoll |
| 422 |
#endif |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
static int atoiNoCap (const char *s) |
| 435 |
{ |
| 436 |
INT64 l; |
| 437 |
union { |
| 438 |
unsigned int u; |
| 439 |
signed int i; |
| 440 |
} retval; |
| 441 |
|
| 442 |
l = atoi64(s); |
| 443 |
|
| 444 |
if (l < 0) { |
| 445 |
retval.i = (int)l; |
| 446 |
} else { |
| 447 |
retval.u = (unsigned int)l; |
| 448 |
} |
| 449 |
return retval.i; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static unsigned int HashString (const char *key) |
| 461 |
{ |
| 462 |
static unsigned long randbox[] = { |
| 463 |
0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U, |
| 464 |
0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU, |
| 465 |
0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU, |
| 466 |
0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU, |
| 467 |
}; |
| 468 |
|
| 469 |
const char *str = key; |
| 470 |
unsigned int acc = 0; |
| 471 |
|
| 472 |
while (*str) { |
| 473 |
acc ^= randbox[(*str + acc) & 0xf]; |
| 474 |
acc = (acc << 1) | (acc >> 31); |
| 475 |
acc &= 0xffffffffU; |
| 476 |
acc ^= randbox[((*str++ >> 4) + acc) & 0xf]; |
| 477 |
acc = (acc << 2) | (acc >> 30); |
| 478 |
acc &= 0xffffffffU; |
| 479 |
} |
| 480 |
return acc; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
static void CodeError( char *fmt, ... ) { |
| 490 |
va_list argptr; |
| 491 |
|
| 492 |
errorCount++; |
| 493 |
|
| 494 |
fprintf( stderr, "%s:%i ", currentFileName, currentFileLine ); |
| 495 |
|
| 496 |
va_start( argptr,fmt ); |
| 497 |
vfprintf( stderr, fmt, argptr ); |
| 498 |
va_end( argptr ); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
static void EmitByte( segment_t *seg, int v ) { |
| 507 |
if ( seg->imageUsed >= MAX_IMAGE ) { |
| 508 |
Error( "MAX_IMAGE" ); |
| 509 |
} |
| 510 |
seg->image[ seg->imageUsed ] = v; |
| 511 |
seg->imageUsed++; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
static void EmitInt( segment_t *seg, int v ) { |
| 520 |
if ( seg->imageUsed >= MAX_IMAGE - 4) { |
| 521 |
Error( "MAX_IMAGE" ); |
| 522 |
} |
| 523 |
seg->image[ seg->imageUsed ] = v & 255; |
| 524 |
seg->image[ seg->imageUsed + 1 ] = ( v >> 8 ) & 255; |
| 525 |
seg->image[ seg->imageUsed + 2 ] = ( v >> 16 ) & 255; |
| 526 |
seg->image[ seg->imageUsed + 3 ] = ( v >> 24 ) & 255; |
| 527 |
seg->imageUsed += 4; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
static void DefineSymbol( char *sym, int value ) { |
| 538 |
|
| 539 |
symbol_t *s; |
| 540 |
char expanded[MAX_LINE_LENGTH]; |
| 541 |
int hash; |
| 542 |
|
| 543 |
if ( passNumber == 1 ) { |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
if ( sym[0] == '$' ) { |
| 549 |
snprintf( expanded, sizeof(expanded), "%s_%i", sym, currentFileIndex ); |
| 550 |
sym = expanded; |
| 551 |
} |
| 552 |
|
| 553 |
hash = HashString( sym ); |
| 554 |
|
| 555 |
if (hashtable_symbol_exists(symtable, hash, sym)) { |
| 556 |
CodeError( "Multiple definitions for %s\n", sym ); |
| 557 |
return; |
| 558 |
} |
| 559 |
|
| 560 |
s = malloc( sizeof( *s ) ); |
| 561 |
s->next = NULL; |
| 562 |
s->name = copystring( sym ); |
| 563 |
s->hash = hash; |
| 564 |
s->value = value; |
| 565 |
s->segment = currentSegment; |
| 566 |
|
| 567 |
hashtable_add(symtable, hash, s); |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
if (symbols == 0) { |
| 578 |
lastSymbol = symbols = s; |
| 579 |
} else { |
| 580 |
lastSymbol->next = s; |
| 581 |
lastSymbol = s; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static int LookupSymbol( char *sym ) { |
| 594 |
symbol_t *s; |
| 595 |
char expanded[MAX_LINE_LENGTH * 2]; |
| 596 |
int hash; |
| 597 |
hashchain_t *hc; |
| 598 |
|
| 599 |
if ( passNumber == 0 ) { |
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
if ( sym[0] == '$' ) { |
| 605 |
snprintf( expanded, sizeof(expanded), "%s_%i", sym, currentFileIndex ); |
| 606 |
sym = expanded; |
| 607 |
} |
| 608 |
|
| 609 |
hash = HashString( sym ); |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
for (hc = hashtable_get(symtable, hash); hc; hc = hc->next) { |
| 618 |
s = (symbol_t*)hc->data; |
| 619 |
if ( (hash == s->hash) && !strcmp(sym, s->name) ) { |
| 620 |
return s->segment->segmentBase + s->value; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
CodeError( "error: symbol %s undefined\n", sym ); |
| 625 |
passNumber = 0; |
| 626 |
DefineSymbol( sym, 0 ); |
| 627 |
passNumber = 1; |
| 628 |
return 0; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
static char *ExtractLine( char *data ) { |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
char *p, *q; |
| 650 |
|
| 651 |
currentFileLine++; |
| 652 |
|
| 653 |
lineParseOffset = 0; |
| 654 |
token[0] = 0; |
| 655 |
*lineBuffer = 0; |
| 656 |
|
| 657 |
p = q = data; |
| 658 |
if (!*q) { |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
for ( ; !((*p == 0) || (*p == '\n')); p++) ; |
| 663 |
|
| 664 |
if ((p - q) >= MAX_LINE_LENGTH) { |
| 665 |
CodeError( "MAX_LINE_LENGTH" ); |
| 666 |
return data; |
| 667 |
} |
| 668 |
|
| 669 |
memcpy( lineBuffer, data, (p - data) ); |
| 670 |
lineBuffer[(p - data)] = 0; |
| 671 |
p += (*p == '\n') ? 1 : 0; |
| 672 |
return p; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
static qboolean Parse( void ) { |
| 684 |
|
| 685 |
const char *p, *q; |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
*token = 0; |
| 690 |
|
| 691 |
|
| 692 |
for (p = lineBuffer + lineParseOffset; *p && (*p <= ' '); p++) ; |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
if ((*p == ';') || (*p == 0)) { |
| 697 |
lineParseOffset = p - lineBuffer; |
| 698 |
return qfalse; |
| 699 |
} |
| 700 |
|
| 701 |
q = p; |
| 702 |
|
| 703 |
for ( ; *p > 32; p++) ; |
| 704 |
|
| 705 |
strncpy(token, q, p - q); |
| 706 |
token[p - q] = 0; |
| 707 |
|
| 708 |
lineParseOffset = p - lineBuffer; |
| 709 |
|
| 710 |
return qtrue; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
static int ParseValue( void ) { |
| 720 |
Parse(); |
| 721 |
return atoiNoCap( token ); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
static int ParseExpression(void) { |
| 731 |
|
| 732 |
int i, j; |
| 733 |
char sym[MAX_LINE_LENGTH]; |
| 734 |
int v; |
| 735 |
|
| 736 |
|
| 737 |
for ( i = ((token[0] == '-') ? 1 : 0) ; i < MAX_LINE_LENGTH ; i++ ) { |
| 738 |
if ( token[i] == '+' || token[i] == '-' || token[i] == 0 ) { |
| 739 |
break; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
memcpy( sym, token, i ); |
| 744 |
sym[i] = 0; |
| 745 |
|
| 746 |
switch (*sym) { |
| 747 |
|
| 748 |
case '-': |
| 749 |
case '0': case '1': case '2': case '3': case '4': |
| 750 |
case '5': case '6': case '7': case '8': case '9': |
| 751 |
v = atoiNoCap(sym); |
| 752 |
break; |
| 753 |
default: |
| 754 |
v = LookupSymbol(sym); |
| 755 |
break; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
while ( token[i] != 0 ) { |
| 760 |
for ( j = i + 1 ; j < MAX_LINE_LENGTH ; j++ ) { |
| 761 |
if ( token[j] == '+' || token[j] == '-' || token[j] == 0 ) { |
| 762 |
break; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
memcpy( sym, token+i+1, j-i-1 ); |
| 767 |
sym[j-i-1] = 0; |
| 768 |
|
| 769 |
switch (token[i]) { |
| 770 |
case '+': |
| 771 |
v += atoiNoCap(sym); |
| 772 |
break; |
| 773 |
case '-': |
| 774 |
v -= atoiNoCap(sym); |
| 775 |
break; |
| 776 |
} |
| 777 |
|
| 778 |
i = j; |
| 779 |
} |
| 780 |
|
| 781 |
return v; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
static void HackToSegment( segmentName_t seg ) { |
| 802 |
if ( currentSegment == &segment[seg] ) { |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
currentSegment = &segment[seg]; |
| 807 |
if ( passNumber == 0 ) { |
| 808 |
lastSymbol->segment = currentSegment; |
| 809 |
lastSymbol->value = currentSegment->imageUsed; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
#define STAT(L) |
| 821 |
#define ASM(O) int TryAssemble##O () |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
ASM(CALL) |
| 832 |
{ |
| 833 |
if ( !strncmp( token, "CALL", 4 ) ) { |
| 834 |
STAT("CALL"); |
| 835 |
EmitByte( &segment[CODESEG], OP_CALL ); |
| 836 |
instructionCount++; |
| 837 |
currentArgOffset = 0; |
| 838 |
return 1; |
| 839 |
} |
| 840 |
return 0; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
ASM(ARG) |
| 845 |
{ |
| 846 |
if ( !strncmp( token, "ARG", 3 ) ) { |
| 847 |
STAT("ARG"); |
| 848 |
EmitByte( &segment[CODESEG], OP_ARG ); |
| 849 |
instructionCount++; |
| 850 |
if ( 8 + currentArgOffset >= 256 ) { |
| 851 |
CodeError( "currentArgOffset >= 256" ); |
| 852 |
return 1; |
| 853 |
} |
| 854 |
EmitByte( &segment[CODESEG], 8 + currentArgOffset ); |
| 855 |
currentArgOffset += 4; |
| 856 |
return 1; |
| 857 |
} |
| 858 |
return 0; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
ASM(RET) |
| 863 |
{ |
| 864 |
if ( !strncmp( token, "RET", 3 ) ) { |
| 865 |
STAT("RET"); |
| 866 |
EmitByte( &segment[CODESEG], OP_LEAVE ); |
| 867 |
instructionCount++; |
| 868 |
EmitInt( &segment[CODESEG], 8 + currentLocals + currentArgs ); |
| 869 |
return 1; |
| 870 |
} |
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
ASM(POP) |
| 877 |
{ |
| 878 |
if ( !strncmp( token, "pop", 3 ) ) { |
| 879 |
STAT("POP"); |
| 880 |
EmitByte( &segment[CODESEG], OP_POP ); |
| 881 |
instructionCount++; |
| 882 |
return 1; |
| 883 |
} |
| 884 |
return 0; |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
ASM(ADDRF) |
| 889 |
{ |
| 890 |
int v; |
| 891 |
if ( !strncmp( token, "ADDRF", 5 ) ) { |
| 892 |
STAT("ADDRF"); |
| 893 |
instructionCount++; |
| 894 |
Parse(); |
| 895 |
v = ParseExpression(); |
| 896 |
v = 16 + currentArgs + currentLocals + v; |
| 897 |
EmitByte( &segment[CODESEG], OP_LOCAL ); |
| 898 |
EmitInt( &segment[CODESEG], v ); |
| 899 |
return 1; |
| 900 |
} |
| 901 |
return 0; |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
ASM(ADDRL) |
| 906 |
{ |
| 907 |
int v; |
| 908 |
if ( !strncmp( token, "ADDRL", 5 ) ) { |
| 909 |
STAT("ADDRL"); |
| 910 |
instructionCount++; |
| 911 |
Parse(); |
| 912 |
v = ParseExpression(); |
| 913 |
v = 8 + currentArgs + v; |
| 914 |
EmitByte( &segment[CODESEG], OP_LOCAL ); |
| 915 |
EmitInt( &segment[CODESEG], v ); |
| 916 |
return 1; |
| 917 |
} |
| 918 |
return 0; |
| 919 |
} |
| 920 |
|
| 921 |
ASM(PROC) |
| 922 |
{ |
| 923 |
char name[1024]; |
| 924 |
if ( !strcmp( token, "proc" ) ) { |
| 925 |
STAT("PROC"); |
| 926 |
Parse(); |
| 927 |
strcpy( name, token ); |
| 928 |
|
| 929 |
DefineSymbol( token, instructionCount ); |
| 930 |
|
| 931 |
currentLocals = ParseValue(); |
| 932 |
currentLocals = ( currentLocals + 3 ) & ~3; |
| 933 |
currentArgs = ParseValue(); |
| 934 |
currentArgs = ( currentArgs + 3 ) & ~3; |
| 935 |
|
| 936 |
if ( 8 + currentLocals + currentArgs >= 32767 ) { |
| 937 |
CodeError( "Locals > 32k in %s\n", name ); |
| 938 |
} |
| 939 |
|
| 940 |
instructionCount++; |
| 941 |
EmitByte( &segment[CODESEG], OP_ENTER ); |
| 942 |
EmitInt( &segment[CODESEG], 8 + currentLocals + currentArgs ); |
| 943 |
return 1; |
| 944 |
} |
| 945 |
return 0; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
ASM(ENDPROC) |
| 950 |
{ |
| 951 |
if ( !strcmp( token, "endproc" ) ) { |
| 952 |
STAT("ENDPROC"); |
| 953 |
Parse(); |
| 954 |
ParseValue(); |
| 955 |
ParseValue(); |
| 956 |
|
| 957 |
|
| 958 |
instructionCount++; |
| 959 |
EmitByte( &segment[CODESEG], OP_PUSH ); |
| 960 |
|
| 961 |
instructionCount++; |
| 962 |
EmitByte( &segment[CODESEG], OP_LEAVE ); |
| 963 |
EmitInt( &segment[CODESEG], 8 + currentLocals + currentArgs ); |
| 964 |
|
| 965 |
return 1; |
| 966 |
} |
| 967 |
return 0; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
ASM(ADDRESS) |
| 972 |
{ |
| 973 |
int v; |
| 974 |
if ( !strcmp( token, "address" ) ) { |
| 975 |
STAT("ADDRESS"); |
| 976 |
Parse(); |
| 977 |
v = ParseExpression(); |
| 978 |
|
| 979 |
|
| 980 |
HackToSegment( DATASEG ); |
| 981 |
EmitInt( currentSegment, v ); |
| 982 |
if( passNumber == 1 && token[ 0 ] == '$' ) |
| 983 |
EmitInt( &segment[ JTRGSEG ], v ); |
| 984 |
return 1; |
| 985 |
} |
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
ASM(EXPORT) |
| 990 |
{ |
| 991 |
if ( !strcmp( token, "export" ) ) { |
| 992 |
STAT("EXPORT"); |
| 993 |
return 1; |
| 994 |
} |
| 995 |
return 0; |
| 996 |
} |
| 997 |
|
| 998 |
ASM(IMPORT) |
| 999 |
{ |
| 1000 |
if ( !strcmp( token, "import" ) ) { |
| 1001 |
STAT("IMPORT"); |
| 1002 |
return 1; |
| 1003 |
} |
| 1004 |
return 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
ASM(CODE) |
| 1008 |
{ |
| 1009 |
if ( !strcmp( token, "code" ) ) { |
| 1010 |
STAT("CODE"); |
| 1011 |
currentSegment = &segment[CODESEG]; |
| 1012 |
return 1; |
| 1013 |
} |
| 1014 |
return 0; |
| 1015 |
} |
| 1016 |
|
| 1017 |
ASM(BSS) |
| 1018 |
{ |
| 1019 |
if ( !strcmp( token, "bss" ) ) { |
| 1020 |
STAT("BSS"); |
| 1021 |
currentSegment = &segment[BSSSEG]; |
| 1022 |
return 1; |
| 1023 |
} |
| 1024 |
return 0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
ASM(DATA) |
| 1028 |
{ |
| 1029 |
if ( !strcmp( token, "data" ) ) { |
| 1030 |
STAT("DATA"); |
| 1031 |
currentSegment = &segment[DATASEG]; |
| 1032 |
return 1; |
| 1033 |
} |
| 1034 |
return 0; |
| 1035 |
} |
| 1036 |
|
| 1037 |
ASM(LIT) |
| 1038 |
{ |
| 1039 |
if ( !strcmp( token, "lit" ) ) { |
| 1040 |
STAT("LIT"); |
| 1041 |
currentSegment = &segment[LITSEG]; |
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
return 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
ASM(LINE) |
| 1048 |
{ |
| 1049 |
if ( !strcmp( token, "line" ) ) { |
| 1050 |
STAT("LINE"); |
| 1051 |
return 1; |
| 1052 |
} |
| 1053 |
return 0; |
| 1054 |
} |
| 1055 |
|
| 1056 |
ASM(FILE) |
| 1057 |
{ |
| 1058 |
if ( !strcmp( token, "file" ) ) { |
| 1059 |
STAT("FILE"); |
| 1060 |
return 1; |
| 1061 |
} |
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
ASM(EQU) |
| 1066 |
{ |
| 1067 |
char name[1024]; |
| 1068 |
if ( !strcmp( token, "equ" ) ) { |
| 1069 |
STAT("EQU"); |
| 1070 |
Parse(); |
| 1071 |
strcpy( name, token ); |
| 1072 |
Parse(); |
| 1073 |
DefineSymbol( name, atoiNoCap(token) ); |
| 1074 |
return 1; |
| 1075 |
} |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
ASM(ALIGN) |
| 1080 |
{ |
| 1081 |
int v; |
| 1082 |
if ( !strcmp( token, "align" ) ) { |
| 1083 |
STAT("ALIGN"); |
| 1084 |
v = ParseValue(); |
| 1085 |
currentSegment->imageUsed = (currentSegment->imageUsed + v - 1 ) & ~( v - 1 ); |
| 1086 |
return 1; |
| 1087 |
} |
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
|
| 1091 |
ASM(SKIP) |
| 1092 |
{ |
| 1093 |
int v; |
| 1094 |
if ( !strcmp( token, "skip" ) ) { |
| 1095 |
STAT("SKIP"); |
| 1096 |
v = ParseValue(); |
| 1097 |
currentSegment->imageUsed += v; |
| 1098 |
return 1; |
| 1099 |
} |
| 1100 |
return 0; |
| 1101 |
} |
| 1102 |
|
| 1103 |
ASM(BYTE) |
| 1104 |
{ |
| 1105 |
int i, v, v2; |
| 1106 |
if ( !strcmp( token, "byte" ) ) { |
| 1107 |
STAT("BYTE"); |
| 1108 |
v = ParseValue(); |
| 1109 |
v2 = ParseValue(); |
| 1110 |
|
| 1111 |
if ( v == 1 ) { |
| 1112 |
|
| 1113 |
HackToSegment( LITSEG ); |
| 1114 |
} else if ( v == 4 ) { |
| 1115 |
|
| 1116 |
HackToSegment( DATASEG ); |
| 1117 |
} else if ( v == 2 ) { |
| 1118 |
|
| 1119 |
CodeError( "16 bit initialized data not supported" ); |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
for ( i = 0 ; i < v ; i++ ) { |
| 1124 |
EmitByte( currentSegment, (v2 & 0xFF) ); |
| 1125 |
v2 >>= 8; |
| 1126 |
} |
| 1127 |
return 1; |
| 1128 |
} |
| 1129 |
return 0; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
ASM(LABEL) |
| 1137 |
{ |
| 1138 |
if ( !strncmp( token, "LABEL", 5 ) ) { |
| 1139 |
STAT("LABEL"); |
| 1140 |
Parse(); |
| 1141 |
if ( currentSegment == &segment[CODESEG] ) { |
| 1142 |
DefineSymbol( token, instructionCount ); |
| 1143 |
} else { |
| 1144 |
DefineSymbol( token, currentSegment->imageUsed ); |
| 1145 |
} |
| 1146 |
return 1; |
| 1147 |
} |
| 1148 |
return 0; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
static void AssembleLine( void ) { |
| 1160 |
hashchain_t *hc; |
| 1161 |
sourceOps_t *op; |
| 1162 |
int i; |
| 1163 |
int hash; |
| 1164 |
|
| 1165 |
Parse(); |
| 1166 |
if ( !token[0] ) { |
| 1167 |
return; |
| 1168 |
} |
| 1169 |
|
| 1170 |
hash = HashString( token ); |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
for (hc = hashtable_get(optable, hash); hc; hc = hc->next) { |
| 1179 |
op = (sourceOps_t*)(hc->data); |
| 1180 |
i = op - sourceOps; |
| 1181 |
if ((hash == opcodesHash[i]) && (!strcmp(token, op->name))) { |
| 1182 |
int opcode; |
| 1183 |
int expression; |
| 1184 |
|
| 1185 |
if ( op->opcode == OP_UNDEF ) { |
| 1186 |
CodeError( "Undefined opcode: %s\n", token ); |
| 1187 |
} |
| 1188 |
if ( op->opcode == OP_IGNORE ) { |
| 1189 |
return; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
opcode = op->opcode; |
| 1194 |
if ( opcode == OP_SEX8 ) { |
| 1195 |
Parse(); |
| 1196 |
if ( token[0] == '1' ) { |
| 1197 |
opcode = OP_SEX8; |
| 1198 |
} else if ( token[0] == '2' ) { |
| 1199 |
opcode = OP_SEX16; |
| 1200 |
} else { |
| 1201 |
CodeError( "Bad sign extension: %s\n", token ); |
| 1202 |
return; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
Parse(); |
| 1208 |
if ( token[0] && op->opcode != OP_CVIF |
| 1209 |
&& op->opcode != OP_CVFI ) { |
| 1210 |
expression = ParseExpression(); |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
if ( opcode == OP_BLOCK_COPY ) { |
| 1217 |
expression = ( expression + 3 ) & ~3; |
| 1218 |
} |
| 1219 |
|
| 1220 |
EmitByte( &segment[CODESEG], opcode ); |
| 1221 |
EmitInt( &segment[CODESEG], expression ); |
| 1222 |
} else { |
| 1223 |
EmitByte( &segment[CODESEG], opcode ); |
| 1224 |
} |
| 1225 |
|
| 1226 |
instructionCount++; |
| 1227 |
return; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
#undef ASM |
| 1263 |
#define ASM(O) if (TryAssemble##O ()) return; |
| 1264 |
|
| 1265 |
ASM(ADDRL) |
| 1266 |
ASM(BYTE) |
| 1267 |
ASM(LINE) |
| 1268 |
ASM(ARG) |
| 1269 |
ASM(IMPORT) |
| 1270 |
ASM(LABEL) |
| 1271 |
ASM(ADDRF) |
| 1272 |
ASM(CALL) |
| 1273 |
ASM(POP) |
| 1274 |
ASM(RET) |
| 1275 |
ASM(ALIGN) |
| 1276 |
ASM(EXPORT) |
| 1277 |
ASM(PROC) |
| 1278 |
ASM(ENDPROC) |
| 1279 |
ASM(ADDRESS) |
| 1280 |
ASM(SKIP) |
| 1281 |
ASM(EQU) |
| 1282 |
ASM(CODE) |
| 1283 |
ASM(LIT) |
| 1284 |
ASM(FILE) |
| 1285 |
ASM(BSS) |
| 1286 |
ASM(DATA) |
| 1287 |
|
| 1288 |
CodeError( "Unknown token: %s\n", token ); |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
void InitTables( void ) { |
| 1297 |
int i; |
| 1298 |
|
| 1299 |
symtable = hashtable_new(symtablelen); |
| 1300 |
optable = hashtable_new(100); |
| 1301 |
|
| 1302 |
for ( i = 0 ; i < NUM_SOURCE_OPS ; i++ ) { |
| 1303 |
opcodesHash[i] = HashString( sourceOps[i].name ); |
| 1304 |
hashtable_add(optable, opcodesHash[i], sourceOps + i); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
static void WriteMapFile( void ) { |
| 1315 |
FILE *f; |
| 1316 |
symbol_t *s; |
| 1317 |
char imageName[MAX_OS_PATH]; |
| 1318 |
int seg; |
| 1319 |
|
| 1320 |
strcpy( imageName, outputFilename ); |
| 1321 |
StripExtension( imageName ); |
| 1322 |
strcat( imageName, ".map" ); |
| 1323 |
|
| 1324 |
report( "Writing %s...\n", imageName ); |
| 1325 |
|
| 1326 |
f = SafeOpenWrite( imageName ); |
| 1327 |
for ( seg = CODESEG ; seg <= BSSSEG ; seg++ ) { |
| 1328 |
for ( s = symbols ; s ; s = s->next ) { |
| 1329 |
if ( s->name[0] == '$' ) { |
| 1330 |
continue; |
| 1331 |
} |
| 1332 |
if ( &segment[seg] != s->segment ) { |
| 1333 |
continue; |
| 1334 |
} |
| 1335 |
fprintf( f, "%i %8x %s\n", seg, s->value, s->name ); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
fclose( f ); |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
static void WriteVmFile( void ) { |
| 1347 |
char imageName[MAX_OS_PATH]; |
| 1348 |
vmHeader_t header; |
| 1349 |
FILE *f; |
| 1350 |
int headerSize; |
| 1351 |
|
| 1352 |
report( "%i total errors\n", errorCount ); |
| 1353 |
|
| 1354 |
strcpy( imageName, outputFilename ); |
| 1355 |
StripExtension( imageName ); |
| 1356 |
strcat( imageName, ".qvm" ); |
| 1357 |
|
| 1358 |
remove( imageName ); |
| 1359 |
|
| 1360 |
report( "code segment: %7i\n", segment[CODESEG].imageUsed ); |
| 1361 |
report( "data segment: %7i\n", segment[DATASEG].imageUsed ); |
| 1362 |
report( "lit segment: %7i\n", segment[LITSEG].imageUsed ); |
| 1363 |
report( "bss segment: %7i\n", segment[BSSSEG].imageUsed ); |
| 1364 |
report( "instruction count: %i\n", instructionCount ); |
| 1365 |
|
| 1366 |
if ( errorCount != 0 ) { |
| 1367 |
report( "Not writing a file due to errors\n" ); |
| 1368 |
return; |
| 1369 |
} |
| 1370 |
|
| 1371 |
if( !options.vanillaQ3Compatibility ) { |
| 1372 |
header.vmMagic = VM_MAGIC_VER2; |
| 1373 |
headerSize = sizeof( header ); |
| 1374 |
} else { |
| 1375 |
header.vmMagic = VM_MAGIC; |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
headerSize = sizeof( header ) - sizeof( header.jtrgLength ); |
| 1382 |
} |
| 1383 |
|
| 1384 |
header.instructionCount = instructionCount; |
| 1385 |
header.codeOffset = headerSize; |
| 1386 |
header.codeLength = segment[CODESEG].imageUsed; |
| 1387 |
header.dataOffset = header.codeOffset + segment[CODESEG].imageUsed; |
| 1388 |
header.dataLength = segment[DATASEG].imageUsed; |
| 1389 |
header.litLength = segment[LITSEG].imageUsed; |
| 1390 |
header.bssLength = segment[BSSSEG].imageUsed; |
| 1391 |
header.jtrgLength = segment[JTRGSEG].imageUsed; |
| 1392 |
|
| 1393 |
report( "Writing to %s\n", imageName ); |
| 1394 |
|
| 1395 |
#ifdef Q3_BIG_ENDIAN |
| 1396 |
{ |
| 1397 |
int i; |
| 1398 |
|
| 1399 |
|
| 1400 |
for ( i = 0 ; i < sizeof( vmHeader_t ) / 4 ; i++ ) { |
| 1401 |
((int *)&header)[i] = LittleLong( ((int *)&header)[i] ); |
| 1402 |
} |
| 1403 |
} |
| 1404 |
#endif |
| 1405 |
|
| 1406 |
CreatePath( imageName ); |
| 1407 |
f = SafeOpenWrite( imageName ); |
| 1408 |
SafeWrite( f, &header, headerSize ); |
| 1409 |
SafeWrite( f, &segment[CODESEG].image, segment[CODESEG].imageUsed ); |
| 1410 |
SafeWrite( f, &segment[DATASEG].image, segment[DATASEG].imageUsed ); |
| 1411 |
SafeWrite( f, &segment[LITSEG].image, segment[LITSEG].imageUsed ); |
| 1412 |
|
| 1413 |
if( !options.vanillaQ3Compatibility ) { |
| 1414 |
SafeWrite( f, &segment[JTRGSEG].image, segment[JTRGSEG].imageUsed ); |
| 1415 |
} |
| 1416 |
|
| 1417 |
fclose( f ); |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
static void Assemble( void ) { |
| 1426 |
int i; |
| 1427 |
char filename[MAX_OS_PATH]; |
| 1428 |
char *ptr; |
| 1429 |
|
| 1430 |
report( "outputFilename: %s\n", outputFilename ); |
| 1431 |
|
| 1432 |
for ( i = 0 ; i < numAsmFiles ; i++ ) { |
| 1433 |
strcpy( filename, asmFileNames[ i ] ); |
| 1434 |
DefaultExtension( filename, ".asm" ); |
| 1435 |
LoadFile( filename, (void **)&asmFiles[i] ); |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
for ( passNumber = 0 ; passNumber < 2 ; passNumber++ ) { |
| 1440 |
segment[LITSEG].segmentBase = segment[DATASEG].imageUsed; |
| 1441 |
segment[BSSSEG].segmentBase = segment[LITSEG].segmentBase + segment[LITSEG].imageUsed; |
| 1442 |
segment[JTRGSEG].segmentBase = segment[BSSSEG].segmentBase + segment[BSSSEG].imageUsed; |
| 1443 |
for ( i = 0 ; i < NUM_SEGMENTS ; i++ ) { |
| 1444 |
segment[i].imageUsed = 0; |
| 1445 |
} |
| 1446 |
segment[DATASEG].imageUsed = 4; |
| 1447 |
instructionCount = 0; |
| 1448 |
|
| 1449 |
for ( i = 0 ; i < numAsmFiles ; i++ ) { |
| 1450 |
currentFileIndex = i; |
| 1451 |
currentFileName = asmFileNames[ i ]; |
| 1452 |
currentFileLine = 0; |
| 1453 |
report("pass %i: %s\n", passNumber, currentFileName ); |
| 1454 |
fflush( NULL ); |
| 1455 |
ptr = asmFiles[i]; |
| 1456 |
while ( ptr ) { |
| 1457 |
ptr = ExtractLine( ptr ); |
| 1458 |
AssembleLine(); |
| 1459 |
} |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
for ( i = 0 ; i < NUM_SEGMENTS ; i++ ) { |
| 1464 |
segment[i].imageUsed = (segment[i].imageUsed + 3) & ~3; |
| 1465 |
} |
| 1466 |
if (passNumber == 0) { |
| 1467 |
sort_symbols(); |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
DefineSymbol( "_stackStart", segment[BSSSEG].imageUsed ); |
| 1473 |
segment[BSSSEG].imageUsed += stackSize; |
| 1474 |
DefineSymbol( "_stackEnd", segment[BSSSEG].imageUsed ); |
| 1475 |
|
| 1476 |
|
| 1477 |
WriteVmFile(); |
| 1478 |
|
| 1479 |
|
| 1480 |
if( options.writeMapFile ) { |
| 1481 |
WriteMapFile(); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
static void ParseOptionFile( const char *filename ) { |
| 1493 |
char expanded[MAX_OS_PATH]; |
| 1494 |
char *text, *text_p; |
| 1495 |
|
| 1496 |
strcpy( expanded, filename ); |
| 1497 |
DefaultExtension( expanded, ".q3asm" ); |
| 1498 |
LoadFile( expanded, (void **)&text ); |
| 1499 |
if ( !text ) { |
| 1500 |
return; |
| 1501 |
} |
| 1502 |
|
| 1503 |
text_p = text; |
| 1504 |
|
| 1505 |
while( ( text_p = COM_Parse( text_p ) ) != 0 ) { |
| 1506 |
if ( !strcmp( com_token, "-o" ) ) { |
| 1507 |
|
| 1508 |
text_p = COM_Parse( text_p ); |
| 1509 |
if ( text_p ) { |
| 1510 |
strcpy( outputFilename, com_token ); |
| 1511 |
} |
| 1512 |
continue; |
| 1513 |
} |
| 1514 |
|
| 1515 |
asmFileNames[ numAsmFiles ] = copystring( com_token ); |
| 1516 |
numAsmFiles++; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
static void ShowHelp( char *argv0 ) { |
| 1521 |
Error("Usage: %s [OPTION]... [FILES]...\n\ |
| 1522 |
Assemble LCC bytecode assembly to Q3VM bytecode.\n\ |
| 1523 |
\n\ |
| 1524 |
-o OUTPUT Write assembled output to file OUTPUT.qvm\n\ |
| 1525 |
-f LISTFILE Read options and list of files to assemble from LISTFILE.q3asm\n\ |
| 1526 |
-b BUCKETS Set symbol hash table to BUCKETS buckets\n\ |
| 1527 |
-m Generate a mapfile for each OUTPUT.qvm\n\ |
| 1528 |
-v Verbose compilation report\n\ |
| 1529 |
-vq3 Produce a qvm file compatible with Q3 1.32b\n\ |
| 1530 |
-h --help -? Show this help\n\ |
| 1531 |
", argv0); |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
int main( int argc, char **argv ) { |
| 1540 |
int i; |
| 1541 |
double start, end; |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
if ( argc < 2 ) { |
| 1546 |
ShowHelp( argv[0] ); |
| 1547 |
} |
| 1548 |
|
| 1549 |
start = I_FloatTime (); |
| 1550 |
|
| 1551 |
|
| 1552 |
strcpy( outputFilename, "q3asm" ); |
| 1553 |
numAsmFiles = 0; |
| 1554 |
|
| 1555 |
for ( i = 1 ; i < argc ; i++ ) { |
| 1556 |
if ( argv[i][0] != '-' ) { |
| 1557 |
break; |
| 1558 |
} |
| 1559 |
if( !strcmp( argv[ i ], "-h" ) || |
| 1560 |
!strcmp( argv[ i ], "--help" ) || |
| 1561 |
!strcmp( argv[ i ], "-?") ) { |
| 1562 |
ShowHelp( argv[0] ); |
| 1563 |
} |
| 1564 |
|
| 1565 |
if ( !strcmp( argv[i], "-o" ) ) { |
| 1566 |
if ( i == argc - 1 ) { |
| 1567 |
Error( "-o must precede a filename" ); |
| 1568 |
} |
| 1569 |
|
| 1570 |
strcpy( outputFilename, argv[ i+1 ] ); |
| 1571 |
i++; |
| 1572 |
continue; |
| 1573 |
} |
| 1574 |
|
| 1575 |
if ( !strcmp( argv[i], "-f" ) ) { |
| 1576 |
if ( i == argc - 1 ) { |
| 1577 |
Error( "-f must precede a filename" ); |
| 1578 |
} |
| 1579 |
ParseOptionFile( argv[ i+1 ] ); |
| 1580 |
i++; |
| 1581 |
continue; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (!strcmp(argv[i], "-b")) { |
| 1585 |
if (i == argc - 1) { |
| 1586 |
Error("-b requires an argument"); |
| 1587 |
} |
| 1588 |
i++; |
| 1589 |
symtablelen = atoiNoCap(argv[i]); |
| 1590 |
continue; |
| 1591 |
} |
| 1592 |
|
| 1593 |
if( !strcmp( argv[ i ], "-v" ) ) { |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
options.verbose = qtrue; |
| 1600 |
continue; |
| 1601 |
} |
| 1602 |
|
| 1603 |
if( !strcmp( argv[ i ], "-m" ) ) { |
| 1604 |
options.writeMapFile = qtrue; |
| 1605 |
continue; |
| 1606 |
} |
| 1607 |
|
| 1608 |
if( !strcmp( argv[ i ], "-vq3" ) ) { |
| 1609 |
options.vanillaQ3Compatibility = qtrue; |
| 1610 |
continue; |
| 1611 |
} |
| 1612 |
|
| 1613 |
Error( "Unknown option: %s", argv[i] ); |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
for ( ; i < argc ; i++ ) { |
| 1618 |
asmFileNames[ numAsmFiles ] = copystring( argv[ i ] ); |
| 1619 |
numAsmFiles++; |
| 1620 |
} |
| 1621 |
|
| 1622 |
if ( numAsmFiles == 0 ) { |
| 1623 |
Error( "No file to assemble" ); |
| 1624 |
} |
| 1625 |
|
| 1626 |
InitTables(); |
| 1627 |
Assemble(); |
| 1628 |
|
| 1629 |
{ |
| 1630 |
symbol_t *s; |
| 1631 |
|
| 1632 |
for ( i = 0, s = symbols ; s ; s = s->next, i++ ) ; |
| 1633 |
|
| 1634 |
if (options.verbose) |
| 1635 |
{ |
| 1636 |
report("%d symbols defined\n", i); |
| 1637 |
hashtable_stats(symtable); |
| 1638 |
hashtable_stats(optable); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
end = I_FloatTime (); |
| 1643 |
report ("%5.0f seconds elapsed\n", end-start); |
| 1644 |
|
| 1645 |
return errorCount; |
| 1646 |
} |
| 1647 |
|