| 1 |
/* |
| 2 |
the DASM macro assembler (aka small systems cross assembler) |
| 3 |
|
| 4 |
Copyright (c) 1988-2002 by Matthew Dillon. |
| 5 |
Copyright (c) 1995 by Olaf "Rhialto" Seibert. |
| 6 |
Copyright (c) 2003-2008 by Andrew Davie. |
| 7 |
Copyright (c) 2008 by Peter H. Froehlich. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 21 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* EXP.C |
| 26 |
* |
| 27 |
* Handle expression evaluation and addressing mode decode. |
| 28 |
* |
| 29 |
* NOTE! If you use the string field in an expression you must clear |
| 30 |
* the SYM_MACRO and SYM_STRING bits in the flags before calling |
| 31 |
* FreeSymbolList()! |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "asm.h" |
| 35 |
|
| 36 |
#define UNION 0 |
| 37 |
|
| 38 |
#if UNION /* warning: ANSI disallows cast to union type */ |
| 39 |
typedef void (unop)(long v1, int f1); |
| 40 |
typedef void (binop)(long v1, long v2, int f1, int f2); |
| 41 |
|
| 42 |
union unibin { |
| 43 |
unop *unary; |
| 44 |
binop *binary; |
| 45 |
}; |
| 46 |
|
| 47 |
typedef union unibin opfunc_t; |
| 48 |
#define _unary .unary |
| 49 |
#define _binary .binary |
| 50 |
#else /* warning: Calling functions without prototype */ |
| 51 |
|
| •52 |
typedef void (*opfunc_t)(...); |
| 53 |
#define _unary |
| 54 |
#define _binary |
| 55 |
|
| 56 |
#endif |
| 57 |
|
| 58 |
static void stackarg(long val, int flags, const char *ptr1); |
| 59 |
|
| 60 |
void doop(opfunc_t, int pri); |
| 61 |
void evaltop(void); |
| 62 |
void op_mult(long v1, long v2, int f1, int f2), |
| 63 |
op_div(long v1, long v2, int f1, int f2), |
| 64 |
op_mod(long v1, long v2, int f1, int f2), |
| 65 |
op_add(long v1, long v2, int f1, int f2), |
| 66 |
op_sub(long v1, long v2, int f1, int f2), |
| 67 |
op_shiftleft(long v1, long v2, int f1, int f2), |
| 68 |
op_shiftright(long v1, long v2, int f1, int f2), |
| 69 |
op_greater(long v1, long v2, int f1, int f2), |
| 70 |
op_greatereq(long v1, long v2, int f1, int f2), |
| 71 |
op_smaller(long v1, long v2, int f1, int f2), |
| 72 |
op_smallereq(long v1, long v2, int f1, int f2), |
| 73 |
op_eqeq(long v1, long v2, int f1, int f2), |
| 74 |
op_noteq(long v1, long v2, int f1, int f2), |
| 75 |
op_andand(long v1, long v2, int f1, int f2), |
| 76 |
op_oror(long v1, long v2, int f1, int f2), |
| 77 |
op_xor(long v1, long v2, int f1, int f2), |
| 78 |
op_and(long v1, long v2, int f1, int f2), |
| 79 |
op_or(long v1, long v2, int f1, int f2), |
| 80 |
op_question(long v1, long v2, int f1, int f2); |
| 81 |
|
| 82 |
void op_takelsb(long v1, int f1), |
| 83 |
op_takemsb(long v1, int f1), |
| 84 |
op_negate(long v1, int f1), |
| 85 |
op_invert(long v1, int f1), |
| 86 |
op_not(long v1, int f1); |
| 87 |
|
| 88 |
|
| 89 |
const char *pushsymbol(const char *str); |
| 90 |
const char *pushstr(const char *str); |
| 91 |
const char *pushbin(const char *str); |
| 92 |
const char *pushoct(const char *str); |
| 93 |
const char *pushdec(const char *str); |
| 94 |
const char *pushhex(const char *str); |
| 95 |
const char *pushchar(const char *str); |
| 96 |
|
| 97 |
int IsAlphaNum( int c ); |
| 98 |
|
| 99 |
/* |
| 100 |
* evaluate an expression. Figure out the addressing mode: |
| 101 |
* |
| 102 |
* implied |
| 103 |
* #val immediate |
| 104 |
* val zero page or absolute |
| 105 |
* val,x zero,x or absolute,x |
| 106 |
* val,y zero,y or absolute,y |
| 107 |
* val,sp stack pointer indexed + offset |
| 108 |
* (val) indirect |
| 109 |
* (val,x) zero indirect x |
| 110 |
* (val),y zero indirect y |
| 111 |
* |
| 112 |
* exp, exp,.. LIST of expressions |
| 113 |
* |
| 114 |
* an absolute may be returned as zero page |
| 115 |
* a relative may be returned as zero page or absolute |
| 116 |
* |
| 117 |
* unary: - ~ ! < > |
| 118 |
* binary: (^)(* / %)(+ -)(>> <<)(& |)(`)(&& ||)(== != < > <= >=) |
| 119 |
* |
| 120 |
* values: symbol, octal, decimal, $hex, %binary, 'c "str" |
| 121 |
* |
| 122 |
*/ |
| 123 |
|
| 124 |
#define MAXOPS 32 |
| 125 |
#define MAXARGS 64 |
| 126 |
|
| 127 |
unsigned char Argflags[MAXARGS]; |
| 128 |
long Argstack[MAXARGS]; |
| 129 |
char *Argstring[MAXARGS]; |
| 130 |
int Oppri[MAXOPS]; |
| 131 |
opfunc_t Opdis[MAXOPS]; |
| 132 |
|
| 133 |
int Argi, Opi, Lastwasop; |
| 134 |
int Argibase, Opibase; |
| 135 |
|
| 136 |
char ucasm_indexed_notation = false; |
| 137 |
|
| 138 |
SYMBOL *eval(const char *str, int wantmode) |
| 139 |
{ |
| 140 |
SYMBOL *base, *cur; |
| 141 |
int oldargibase = Argibase; |
| 142 |
int oldopibase = Opibase; |
| 143 |
int scr; |
| 144 |
|
| 145 |
const char *pLine = str; |
| 146 |
|
| 147 |
Argibase = Argi; |
| 148 |
Opibase = Opi; |
| 149 |
Lastwasop = 1; |
| 150 |
base = cur = allocsymbol(); |
| 151 |
|
| 152 |
|
| 153 |
while (*str) |
| 154 |
{ |
| 155 |
if (Xdebug) |
| 156 |
printf("char '%c'\n", *str); |
| 157 |
|
| 158 |
switch(*str) |
| 159 |
{ |
| 160 |
case ' ': |
| 161 |
case '\n': |
| 162 |
++str; |
| 163 |
break; |
| 164 |
|
| 165 |
case '~': |
| 166 |
if (Lastwasop) |
| 167 |
doop((opfunc_t)op_invert, 128); |
| 168 |
else |
| 169 |
asmerr( ERROR_SYNTAX_ERROR, false, pLine ); |
| 170 |
++str; |
| 171 |
break; |
| 172 |
|
| 173 |
case '*': |
| 174 |
if (Lastwasop) { |
| 175 |
pushsymbol("."); |
| 176 |
} else |
| 177 |
doop((opfunc_t)op_mult, 20); |
| 178 |
++str; |
| 179 |
break; |
| 180 |
|
| 181 |
case '/': |
| 182 |
doop((opfunc_t)op_div, 20); |
| 183 |
++str; |
| 184 |
break; |
| 185 |
|
| 186 |
case '%': |
| 187 |
if (Lastwasop) { |
| 188 |
str = pushbin(str+1); |
| 189 |
} else { |
| 190 |
doop((opfunc_t)op_mod, 20); |
| 191 |
++str; |
| 192 |
} |
| 193 |
break; |
| 194 |
|
| 195 |
case '?': /* 10 */ |
| 196 |
doop((opfunc_t)op_question, 10); |
| 197 |
++str; |
| 198 |
break; |
| 199 |
|
| 200 |
case '+': /* 19 */ |
| 201 |
doop((opfunc_t)op_add, 19); |
| 202 |
++str; |
| 203 |
break; |
| 204 |
|
| 205 |
case '-': /* 19: - (or - unary) */ |
| 206 |
if (Lastwasop) { |
| 207 |
doop((opfunc_t)op_negate, 128); |
| 208 |
} else { |
| 209 |
doop((opfunc_t)op_sub, 19); |
| 210 |
} |
| 211 |
++str; |
| 212 |
break; |
| 213 |
|
| 214 |
case '>': /* 18: >> << 17: > >= <= < */ |
| 215 |
|
| 216 |
if (Lastwasop) |
| 217 |
{ |
| 218 |
doop((opfunc_t)op_takemsb, 128); |
| 219 |
++str; |
| 220 |
break; |
| 221 |
} |
| 222 |
|
| 223 |
if (str[1] == '>') |
| 224 |
{ |
| 225 |
doop((opfunc_t)op_shiftright, 18); |
| 226 |
++str; |
| 227 |
} |
| 228 |
|
| 229 |
else if (str[1] == '=') |
| 230 |
{ |
| 231 |
doop((opfunc_t)op_greatereq, 17); |
| 232 |
++str; |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
doop((opfunc_t)op_greater, 17); |
| 237 |
} |
| 238 |
++str; |
| 239 |
break; |
| 240 |
|
| 241 |
case '<': |
| 242 |
|
| 243 |
if (Lastwasop) |
| 244 |
{ |
| 245 |
doop((opfunc_t)op_takelsb, 128); |
| 246 |
++str; |
| 247 |
break; |
| 248 |
} |
| 249 |
|
| 250 |
if (str[1] == '<') |
| 251 |
{ |
| 252 |
doop((opfunc_t)op_shiftleft, 18); |
| 253 |
++str; |
| 254 |
} |
| 255 |
else if (str[1] == '=') |
| 256 |
{ |
| 257 |
doop((opfunc_t)op_smallereq, 17); |
| 258 |
++str; |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
doop((opfunc_t)op_smaller, 17); |
| 263 |
} |
| 264 |
++str; |
| 265 |
break; |
| 266 |
|
| 267 |
case '=': /* 16: == (= same as ==) */ |
| 268 |
|
| 269 |
if (str[1] == '=') |
| 270 |
++str; |
| 271 |
doop((opfunc_t)op_eqeq, 16); |
| 272 |
++str; |
| 273 |
break; |
| 274 |
|
| 275 |
case '!': /* 16: != */ |
| 276 |
|
| 277 |
if (Lastwasop) |
| 278 |
{ |
| 279 |
doop((opfunc_t)op_not, 128); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
doop((opfunc_t)op_noteq, 16); |
| 284 |
++str; |
| 285 |
} |
| 286 |
++str; |
| 287 |
break; |
| 288 |
|
| 289 |
case '&': /* 15: & 12: && */ |
| 290 |
|
| 291 |
if (str[1] == '&') |
| 292 |
{ |
| 293 |
doop((opfunc_t)op_andand, 12); |
| 294 |
++str; |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
doop((opfunc_t)op_and, 15); |
| 299 |
} |
| 300 |
++str; |
| 301 |
break; |
| 302 |
|
| 303 |
case '^': /* 14: ^ */ |
| 304 |
|
| 305 |
doop((opfunc_t)op_xor, 14); |
| 306 |
++str; |
| 307 |
break; |
| 308 |
|
| 309 |
case '|': /* 13: | 11: || */ |
| 310 |
|
| 311 |
if (str[1] == '|') |
| 312 |
{ |
| 313 |
doop((opfunc_t)op_oror, 11); |
| 314 |
++str; |
| 315 |
} |
| 316 |
else |
| 317 |
{ |
| 318 |
doop((opfunc_t)op_or, 13); |
| 319 |
} |
| 320 |
++str; |
| 321 |
break; |
| 322 |
|
| 323 |
|
| 324 |
case '(': |
| 325 |
|
| 326 |
if (wantmode) |
| 327 |
{ |
| 328 |
cur->addrmode = AM_INDWORD; |
| 329 |
++str; |
| 330 |
break; |
| 331 |
} |
| 332 |
|
| 333 |
/* fall thru OK */ |
| 334 |
|
| 335 |
case '[': /* eventually an argument */ |
| 336 |
ucasm_indexed_notation = false; |
| 337 |
|
| 338 |
if ((((((str[1]|0x20) == 'x') || ((str[1]|0x20) == 'y')) && (str[2] == '+')) || // X- or Y-indexed address mode |
| 339 |
(((str[1]|0x20) == 's') && ((str[2]|0x20) == 'p') && (str[3] == '+'))) && // SP-indexed address mode |
| 340 |
((Processor == 68705) || (Processor == 6811) || (Processor == 68908))) |
| 341 |
{ |
| 342 |
ucasm_indexed_notation = true; |
| 343 |
// UCASM compatibility, allow notations [X+255], [Y+3], [SP+5] |
| 344 |
switch(str[1]|0x20) { |
| 345 |
case 'x': |
| 346 |
cur->addrmode = AM_BYTEADRX; |
| 347 |
//FIX: OPCODE.FORCE / Mnext adaption moved to ops.c |
| 348 |
break; |
| 349 |
|
| 350 |
case 'y': cur->addrmode = AM_BYTEADRY; break; |
| 351 |
|
| 352 |
case 's': |
| 353 |
cur->addrmode = AM_BYTEADR_SP; |
| 354 |
//FIX: OPCODE.FORCE / Mnext adaption moved to ops.c |
| 355 |
break; |
| 356 |
} |
| 357 |
str += 3; /* skip '[',{x,y,s},'+' */ |
| 358 |
if ((cur->addrmode == AM_BYTEADR_SP) || (cur->addrmode == AM_WORDADR_SP)) { |
| 359 |
++str; /* skip also 'p' */ |
| 360 |
} |
| 361 |
} else { |
| 362 |
ucasm_indexed_notation = false; |
| 363 |
if (Opi == MAXOPS) |
| 364 |
puts("too many ops"); |
| 365 |
else |
| 366 |
Oppri[Opi++] = 0; |
| 367 |
++str; |
| 368 |
} |
| 369 |
break; |
| 370 |
|
| 371 |
case ')': |
| 372 |
|
| 373 |
if (wantmode) |
| 374 |
{ |
| 375 |
if (cur->addrmode == AM_INDWORD && |
| 376 |
str[1] == ',' && (str[2]|0x20) == 'y') |
| 377 |
{ |
| 378 |
cur->addrmode = AM_INDBYTEY; |
| 379 |
str += 2; |
| 380 |
} |
| 381 |
//FIX: detect illegal opc (zp),x syntax... |
| 382 |
if (cur->addrmode == AM_INDWORD && str[1] == ',' && (str[2]|0x20) == 'x') |
| 383 |
{ |
| 384 |
char sBuffer[128]; |
| 385 |
sprintf( sBuffer, "%s", str ); |
| 386 |
asmerr( ERROR_ILLEGAL_ADDRESSING_MODE,false, pLine ); |
| 387 |
++Redo; |
| 388 |
Redo_why |= REASON_MNEMONIC_NOT_RESOLVED; |
| 389 |
|
| 390 |
//we treat the opcode as valid to allow passes to continue, which should |
| 391 |
//allow other errors (like phase errros) to resolve before our "++Redo" |
| 392 |
//ultimately forces a failure. |
| 393 |
} |
| 394 |
++str; |
| 395 |
break; |
| 396 |
} |
| 397 |
|
| 398 |
/* fall thru OK */ |
| 399 |
|
| 400 |
case ']': |
| 401 |
|
| 402 |
while(Opi != Opibase && Oppri[Opi-1]) |
| 403 |
evaltop(); |
| 404 |
if (Opi != Opibase) |
| 405 |
--Opi; |
| 406 |
++str; |
| 407 |
|
| 408 |
if (ucasm_indexed_notation) { |
| 409 |
ucasm_indexed_notation = false; |
| 410 |
} else { |
| 411 |
if (Argi == Argibase) |
| 412 |
{ |
| 413 |
puts("']' error, no arg on stack"); |
| 414 |
break; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
if (*str == 'd') |
| 419 |
{ /* STRING CONVERSION */ |
| 420 |
char buf[32]; |
| 421 |
++str; |
| 422 |
if (Argflags[Argi-1] == 0) |
| 423 |
{ |
| 424 |
sprintf(buf,"%ld",Argstack[Argi-1]); |
| 425 |
Argstring[Argi-1] = strcpy(ckmalloc(strlen(buf)+1),buf); |
| 426 |
} |
| 427 |
} |
| 428 |
Lastwasop = 0; |
| 429 |
break; |
| 430 |
|
| 431 |
case '#': |
| 432 |
|
| 433 |
cur->addrmode = AM_IMM8; |
| 434 |
++str; |
| 435 |
/* |
| 436 |
* No other addressing mode is possible from now on |
| 437 |
* so we might as well allow () instead of []. |
| 438 |
*/ |
| 439 |
wantmode = 0; |
| 440 |
break; |
| 441 |
|
| 442 |
case ',': |
| 443 |
|
| 444 |
while(Opi != Opibase) |
| 445 |
evaltop(); |
| 446 |
Lastwasop = 1; |
| 447 |
scr = str[1]|0x20; /* to lower case */ |
| 448 |
|
| 449 |
if (cur->addrmode == AM_INDWORD && scr == 'x' && !IsAlphaNum( str[2] )) |
| 450 |
{ |
| 451 |
cur->addrmode = AM_INDBYTEX; |
| 452 |
++str; |
| 453 |
} |
| 454 |
//FIX: detect illegal opc (zp,y) syntax... |
| 455 |
else if ((cur->addrmode == AM_INDWORD && scr == 'y' && str[2]==')')&&(wantmode)) |
| 456 |
{ |
| 457 |
char sBuffer[128]; |
| 458 |
sprintf( sBuffer, "%s", str ); |
| 459 |
asmerr( ERROR_ILLEGAL_ADDRESSING_MODE,false, pLine ); |
| 460 |
++Redo; |
| 461 |
Redo_why |= REASON_MNEMONIC_NOT_RESOLVED; |
| 462 |
|
| 463 |
//we treat the opcode as valid to allow passes to continue, which should |
| 464 |
//allow other errors (like phase errros) to resolve before our "++Redo" |
| 465 |
//ultimately forces a failure. |
| 466 |
cur->addrmode = AM_0Y; |
| 467 |
++str; |
| 468 |
|
| 469 |
} |
| 470 |
else if (scr == 'x' && !IsAlphaNum(str[2])) |
| 471 |
{ |
| 472 |
cur->addrmode = AM_0X; |
| 473 |
++str; |
| 474 |
//FIX: OPCODE.FORCE / Mnext adaption moved to ops.c |
| 475 |
} |
| 476 |
else if (scr == 'y' && !IsAlphaNum(str[2])) |
| 477 |
{ |
| 478 |
cur->addrmode = AM_0Y; |
| 479 |
++str; |
| 480 |
//FIX: OPCODE.FORCE / Mnext adaption moved to ops.c |
| 481 |
} |
| 482 |
else if ((scr == 's') && ((str[2]|0x20) == 'p') && !IsAlphaNum(str[3])) // stack pointer indexed address mode |
| 483 |
{ |
| 484 |
cur->addrmode = AM_BYTEADR_SP; |
| 485 |
++str; |
| 486 |
++str; |
| 487 |
//FIX: OPCODE.FORCE / Mnext adaption moved to ops.c |
| 488 |
} |
| 489 |
else |
| 490 |
{ |
| 491 |
SYMBOL *pNewSymbol = allocsymbol(); |
| 492 |
cur->next = pNewSymbol; |
| 493 |
--Argi; |
| 494 |
if (Argi < Argibase) |
| 495 |
asmerr( ERROR_SYNTAX_ERROR, false, pLine ); |
| 496 |
if (Argi > Argibase) |
| 497 |
asmerr( ERROR_SYNTAX_ERROR, false, pLine ); |
| 498 |
cur->value = Argstack[Argi]; |
| 499 |
cur->flags = Argflags[Argi]; |
| 500 |
|
| 501 |
if ((cur->string = (void *)Argstring[Argi]) != NULL) |
| 502 |
{ |
| 503 |
cur->flags |= SYM_STRING; |
| 504 |
if (Xdebug) |
| 505 |
printf("STRING: %s\n", cur->string); |
| 506 |
} |
| 507 |
cur = pNewSymbol; |
| 508 |
} |
| 509 |
++str; |
| 510 |
break; |
| 511 |
|
| 512 |
case '$': |
| 513 |
str = pushhex(str+1); |
| 514 |
break; |
| 515 |
|
| 516 |
case '\'': |
| 517 |
str = pushchar(str+1); |
| 518 |
break; |
| 519 |
|
| 520 |
case '\"': |
| 521 |
str = pushstr(str+1); |
| 522 |
break; |
| 523 |
|
| 524 |
default: |
| 525 |
{ |
| 526 |
const char *dol = str; |
| 527 |
while (*dol >= '0' && *dol <= '9') |
| 528 |
dol++; |
| 529 |
if (*dol == '$') |
| 530 |
{ |
| 531 |
str = pushsymbol(str); |
| 532 |
break; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
if (*str == '0') |
| 537 |
{ |
| 538 |
if (str[1] == 'x') { // allow also '0xAA' notation for '$AA' |
| 539 |
++str; |
| 540 |
str = (char *)pushhex(str+1); |
| 541 |
} else { |
| 542 |
str = pushoct(str); |
| 543 |
} |
| 544 |
} |
| 545 |
else |
| 546 |
{ |
| 547 |
if (*str > '0' && *str <= '9') |
| 548 |
str = pushdec(str); |
| 549 |
else |
| 550 |
str = pushsymbol(str); |
| 551 |
} |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
while(Opi != Opibase) |
| 557 |
evaltop(); |
| 558 |
|
| 559 |
if (Argi != Argibase) |
| 560 |
{ |
| 561 |
--Argi; |
| 562 |
cur->value = Argstack[Argi]; |
| 563 |
cur->flags = Argflags[Argi]; |
| 564 |
if ((cur->string = (void *)Argstring[Argi]) != NULL) |
| 565 |
{ |
| 566 |
cur->flags |= SYM_STRING; |
| 567 |
if (Xdebug) |
| 568 |
printf("STRING: %s\n", cur->string); |
| 569 |
} |
| 570 |
if (base->addrmode == 0) |
| 571 |
base->addrmode = AM_BYTEADR; |
| 572 |
} |
| 573 |
|
| 574 |
if (Argi != Argibase || Opi != Opibase) |
| 575 |
asmerr( ERROR_SYNTAX_ERROR, false, pLine ); |
| 576 |
|
| 577 |
|
| 578 |
Argi = Argibase; |
| 579 |
Opi = Opibase; |
| 580 |
Argibase = oldargibase; |
| 581 |
Opibase = oldopibase; |
| 582 |
return base; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
int IsAlphaNum( int c ) |
| 587 |
{ |
| 588 |
return ((c >= 'a' && c <= 'z') |
| 589 |
|| (c >= 'A' && c <= 'Z') |
| 590 |
|| (c >= '0' && c <= '9')); |
| 591 |
} |
| 592 |
|
| 593 |
void evaltop(void) |
| 594 |
{ |
| 595 |
if (Xdebug) |
| 596 |
printf("evaltop @(A,O) %d %d\n", Argi, Opi); |
| 597 |
|
| 598 |
if (Opi <= Opibase) { |
| 599 |
asmerr( ERROR_SYNTAX_ERROR, false, NULL ); |
| 600 |
Opi = Opibase; |
| 601 |
return; |
| 602 |
} |
| 603 |
--Opi; |
| 604 |
|
| 605 |
if (Opdis[Opi] == NULL) { |
| 606 |
asmerr( ERROR_AVOID_SEGFAULT, true, "operator function table" ); |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
if (Oppri[Opi] == 128) { |
| 611 |
if (Argi < Argibase + 1) { |
| 612 |
asmerr( ERROR_SYNTAX_ERROR, false, NULL ); |
| 613 |
Argi = Argibase; |
| 614 |
return; |
| 615 |
} |
| 616 |
--Argi; |
| 617 |
(*Opdis[Opi]_unary)(Argstack[Argi], Argflags[Argi]); |
| 618 |
} |
| 619 |
else |
| 620 |
{ |
| 621 |
if (Argi < Argibase + 2) |
| 622 |
{ |
| 623 |
asmerr( ERROR_SYNTAX_ERROR, false, NULL ); |
| 624 |
Argi = Argibase; |
| 625 |
return; |
| 626 |
} |
| 627 |
|
| 628 |
Argi -= 2; |
| 629 |
(*Opdis[Opi]_binary)(Argstack[Argi], Argstack[Argi+1], |
| 630 |
Argflags[Argi], Argflags[Argi+1]); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
static void stackarg(long val, int flags, const char *ptr1) |
| 635 |
{ |
| 636 |
char *str = NULL; |
| 637 |
|
| 638 |
if (Xdebug) |
| 639 |
printf("stackarg %ld (@%d)\n", val, Argi); |
| 640 |
|
| 641 |
Lastwasop = 0; |
| 642 |
if (flags & SYM_STRING) |
| 643 |
{ |
| 644 |
/* |
| 645 |
Why unsigned char? Looks like we're converting to |
| 646 |
long in a very strange way... [phf] |
| 647 |
*/ |
| 648 |
const unsigned char *ptr = (const unsigned char *)ptr1; |
| 649 |
char *new; |
| 650 |
int len; |
| 651 |
val = len = 0; |
| 652 |
while (*ptr && *ptr != '\"') |
| 653 |
{ |
| 654 |
val = (val << 8) | *ptr; |
| 655 |
++ptr; |
| 656 |
++len; |
| 657 |
} |
| 658 |
new = ckmalloc(len + 1); |
| 659 |
memcpy(new, ptr1, len); |
| 660 |
new[len] = 0; |
| 661 |
flags &= ~SYM_STRING; |
| 662 |
str = new; |
| 663 |
} |
| 664 |
Argstack[Argi] = val; |
| 665 |
Argstring[Argi] = str; |
| 666 |
Argflags[Argi] = flags; |
| 667 |
if (++Argi == MAXARGS) { |
| 668 |
puts("stackarg: maxargs stacked"); |
| 669 |
Argi = Argibase; |
| 670 |
} |
| 671 |
while (Opi != Opibase && Oppri[Opi-1] == 128) |
| 672 |
evaltop(); |
| 673 |
} |
| 674 |
|
| 675 |
void doop(opfunc_t func, int pri) |
| 676 |
{ |
| 677 |
if (Xdebug) |
| 678 |
puts("doop"); |
| 679 |
|
| 680 |
Lastwasop = 1; |
| 681 |
|
| 682 |
if (Opi == Opibase || pri == 128) |
| 683 |
{ |
| 684 |
if (Xdebug) |
| 685 |
printf("doop @ %d unary\n", Opi); |
| 686 |
|
| 687 |
if (Opi >= MAXOPS) { |
| 688 |
fprintf(stderr,"doop: error: operator index(%d) > MAXOPS(%d), probably too deep recursion", Opi, MAXOPS); |
| 689 |
asmerr(ERROR_RECURSION_TOO_DEEP, true, "doop()"); |
| 690 |
return; |
| 691 |
} |
| 692 |
Opdis[Opi] = func; |
| 693 |
Oppri[Opi] = pri; |
| 694 |
++Opi; |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
while (Opi != Opibase && Oppri[Opi-1] && pri <= Oppri[Opi-1]) |
| 699 |
evaltop(); |
| 700 |
|
| 701 |
if (Xdebug) |
| 702 |
printf("doop @ %d\n", Opi); |
| 703 |
|
| 704 |
Opdis[Opi] = func; |
| 705 |
Oppri[Opi] = pri; |
| 706 |
++Opi; |
| 707 |
|
| 708 |
if (Opi == MAXOPS) |
| 709 |
{ |
| 710 |
puts("doop: too many operators"); |
| 711 |
Opi = Opibase; |
| 712 |
} |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
void op_takelsb(long v1, int f1) |
| 717 |
{ |
| 718 |
stackarg(v1 & 0xFFL, f1, NULL); |
| 719 |
} |
| 720 |
|
| 721 |
void op_takemsb(long v1, int f1) |
| 722 |
{ |
| 723 |
stackarg((v1 >> 8) & 0xFF, f1, NULL); |
| 724 |
} |
| 725 |
|
| 726 |
void op_negate(long v1, int f1) |
| 727 |
{ |
| 728 |
stackarg(-v1, f1, NULL); |
| 729 |
} |
| 730 |
|
| 731 |
void op_invert(long v1, int f1) |
| 732 |
{ |
| 733 |
stackarg(~v1, f1, NULL); |
| 734 |
} |
| 735 |
|
| 736 |
void op_not(long v1, int f1) |
| 737 |
{ |
| 738 |
stackarg(!v1, f1, NULL); |
| 739 |
} |
| 740 |
|
| 741 |
void op_mult(long v1, long v2, int f1, int f2) |
| 742 |
{ |
| 743 |
stackarg(v1 * v2, f1|f2, NULL); |
| 744 |
Lastwasop = 1; |
| 745 |
} |
| 746 |
|
| 747 |
void op_div(long v1, long v2, int f1, int f2) |
| 748 |
{ |
| 749 |
Lastwasop = 1; |
| 750 |
if (f1|f2) { |
| 751 |
stackarg(0L, f1|f2, NULL); |
| 752 |
return; |
| 753 |
} |
| 754 |
if (v2 == 0) |
| 755 |
{ |
| 756 |
asmerr( ERROR_DIVISION_BY_0, true, NULL ); |
| 757 |
stackarg(0L, 0, NULL); |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
stackarg(v1 / v2, 0, NULL); |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
void op_mod(long v1, long v2, int f1, int f2) |
| 766 |
{ |
| 767 |
if (f1|f2) { |
| 768 |
stackarg(0L, f1|f2, NULL); |
| 769 |
return; |
| 770 |
} |
| 771 |
if (v2 == 0) |
| 772 |
stackarg(v1, 0, NULL); |
| 773 |
else |
| 774 |
stackarg(v1 % v2, 0, NULL); |
| 775 |
Lastwasop = 1; |
| 776 |
} |
| 777 |
|
| 778 |
void op_question(long v1, long v2, int f1, int f2) |
| 779 |
{ |
| 780 |
if (f1) |
| 781 |
stackarg(0L, f1, NULL); |
| 782 |
else |
| 783 |
stackarg((long)((v1) ? v2 : 0), ((v1) ? f2 : 0), NULL); |
| 784 |
} |
| 785 |
|
| 786 |
void op_add(long v1, long v2, int f1, int f2) |
| 787 |
{ |
| 788 |
stackarg(v1 + v2, f1|f2, NULL); |
| 789 |
Lastwasop = 1; |
| 790 |
} |
| 791 |
|
| 792 |
void op_sub(long v1, long v2, int f1, int f2) |
| 793 |
{ |
| 794 |
stackarg(v1 - v2, f1|f2, NULL); |
| 795 |
Lastwasop = 1; |
| 796 |
} |
| 797 |
|
| 798 |
void op_shiftright(long v1, long v2, int f1, int f2) |
| 799 |
{ |
| 800 |
if (f1|f2) |
| 801 |
stackarg(0L, f1|f2, NULL); |
| 802 |
else |
| 803 |
stackarg((long)(v1 >> v2), 0, NULL); |
| 804 |
} |
| 805 |
|
| 806 |
void op_shiftleft(long v1, long v2, int f1, int f2) |
| 807 |
{ |
| 808 |
if (f1|f2) |
| 809 |
stackarg(0L, f1|f2, NULL); |
| 810 |
else |
| 811 |
stackarg((long)(v1 << v2), 0, NULL); |
| 812 |
} |
| 813 |
|
| 814 |
void op_greater(long v1, long v2, int f1, int f2) |
| 815 |
{ |
| 816 |
stackarg((long)(v1 > v2), f1|f2, NULL); |
| 817 |
} |
| 818 |
|
| 819 |
void op_greatereq(long v1, long v2, int f1, int f2) |
| 820 |
{ |
| 821 |
stackarg((long)(v1 >= v2), f1|f2, NULL); |
| 822 |
} |
| 823 |
|
| 824 |
void op_smaller(long v1, long v2, int f1, int f2) |
| 825 |
{ |
| 826 |
stackarg((long)(v1 < v2), f1|f2, NULL); |
| 827 |
} |
| 828 |
|
| 829 |
void op_smallereq(long v1, long v2, int f1, int f2) |
| 830 |
{ |
| 831 |
stackarg((long)(v1 <= v2), f1|f2, NULL); |
| 832 |
} |
| 833 |
|
| 834 |
void op_eqeq(long v1, long v2, int f1, int f2) |
| 835 |
{ |
| 836 |
stackarg((long)(v1 == v2), f1|f2, NULL); |
| 837 |
} |
| 838 |
|
| 839 |
void op_noteq(long v1, long v2, int f1, int f2) |
| 840 |
{ |
| 841 |
stackarg((long)(v1 != v2), f1|f2, NULL); |
| 842 |
} |
| 843 |
|
| 844 |
void op_andand(long v1, long v2, int f1, int f2) |
| 845 |
{ |
| 846 |
if ((!f1 && !v1) || (!f2 && !v2)) { |
| 847 |
stackarg(0L, 0, NULL); |
| 848 |
return; |
| 849 |
} |
| 850 |
stackarg(1L, f1|f2, NULL); |
| 851 |
} |
| 852 |
|
| 853 |
void op_oror(long v1, long v2, int f1, int f2) |
| 854 |
{ |
| 855 |
if ((!f1 && v1) || (!f2 && v2)) { |
| 856 |
stackarg(1L, 0, NULL); |
| 857 |
return; |
| 858 |
} |
| 859 |
stackarg(0L, f1|f2, NULL); |
| 860 |
} |
| 861 |
|
| 862 |
void op_xor(long v1, long v2, int f1, int f2) |
| 863 |
{ |
| 864 |
stackarg(v1^v2, f1|f2, NULL); |
| 865 |
} |
| 866 |
|
| 867 |
void op_and(long v1, long v2, int f1, int f2) |
| 868 |
{ |
| 869 |
stackarg(v1&v2, f1|f2, NULL); |
| 870 |
} |
| 871 |
|
| 872 |
void op_or(long v1, long v2, int f1, int f2) |
| 873 |
{ |
| 874 |
stackarg(v1|v2, f1|f2, NULL); |
| 875 |
} |
| 876 |
|
| 877 |
const char *pushchar(const char *str) |
| 878 |
{ |
| 879 |
if (*str) { |
| 880 |
stackarg((long)*str, 0, NULL); |
| 881 |
++str; |
| 882 |
} else { |
| 883 |
stackarg((long)' ', 0, NULL); |
| 884 |
} |
| 885 |
return str; |
| 886 |
} |
| 887 |
|
| 888 |
const char *pushhex(const char *str) |
| 889 |
{ |
| 890 |
long val = 0; |
| 891 |
for (;; ++str) { |
| 892 |
if (*str >= '0' && *str <= '9') { |
| 893 |
val = (val << 4) + (*str - '0'); |
| 894 |
continue; |
| 895 |
} |
| 896 |
if ((*str >= 'a' && *str <= 'f') || (*str >= 'A' && *str <= 'F')) { |
| 897 |
val = (val << 4) + ((*str&0x1F) + 9); |
| 898 |
continue; |
| 899 |
} |
| 900 |
break; |
| 901 |
} |
| 902 |
stackarg(val, 0, NULL); |
| 903 |
return str; |
| 904 |
} |
| 905 |
|
| 906 |
const char *pushoct(const char *str) |
| 907 |
{ |
| 908 |
long val = 0; |
| 909 |
while (*str >= '0' && *str <= '7') { |
| 910 |
val = (val << 3) + (*str - '0'); |
| 911 |
++str; |
| 912 |
} |
| 913 |
stackarg(val, 0, NULL); |
| 914 |
return str; |
| 915 |
} |
| 916 |
|
| 917 |
const char *pushdec(const char *str) |
| 918 |
{ |
| 919 |
long val = 0; |
| 920 |
while (*str >= '0' && *str <= '9') { |
| 921 |
val = (val * 10) + (*str - '0'); |
| 922 |
++str; |
| 923 |
} |
| 924 |
stackarg(val, 0, NULL); |
| 925 |
return str; |
| 926 |
} |
| 927 |
|
| 928 |
const char *pushbin(const char *str) |
| 929 |
{ |
| 930 |
long val = 0; |
| 931 |
while (*str == '0' || *str == '1') { |
| 932 |
val = (val << 1) | (*str - '0'); |
| 933 |
++str; |
| 934 |
} |
| 935 |
stackarg(val, 0, NULL); |
| 936 |
return str; |
| 937 |
} |
| 938 |
|
| 939 |
const char *pushstr(const char *str) |
| 940 |
{ |
| 941 |
stackarg(0, SYM_STRING, str); |
| 942 |
while (*str && *str != '\"') |
| 943 |
++str; |
| 944 |
if (*str == '\"') |
| 945 |
++str; |
| 946 |
return str; |
| 947 |
} |
| 948 |
|
| 949 |
static int symbolRecursionCount = 0; |
| 950 |
|
| 951 |
const char *pushsymbol(const char *str) |
| 952 |
{ |
| 953 |
SYMBOL *sym; |
| 954 |
const char *ptr; |
| 955 |
unsigned char macro = 0; |
| 956 |
|
| 957 |
symbolRecursionCount++; |
| 958 |
|
| 959 |
if (symbolRecursionCount > 1000) { |
| 960 |
fprintf(stderr, "error: %s:%d: recursion > 1000, too deep, aborting\n",__FILE__,__LINE__); |
| 961 |
asmerr(ERROR_RECURSION_TOO_DEEP, true, "pushsymbol()"); |
| 962 |
} |
| 963 |
|
| 964 |
for (ptr = str; |
| 965 |
*ptr == '_' || |
| 966 |
*ptr == '.' || |
| 967 |
(*ptr >= 'a' && *ptr <= 'z') || |
| 968 |
(*ptr == '@') || // UCASM compatibility, allow at-sign to apear in label names |
| 969 |
(*ptr >= 'A' && *ptr <= 'Z') || |
| 970 |
(*ptr >= '0' && *ptr <= '9'); |
| 971 |
++ptr |
| 972 |
); |
| 973 |
if (ptr == str) { |
| 974 |
asmerr( ERROR_ILLEGAL_CHARACTER, false, str ); |
| 975 |
printf("char = '%c' %d (-1: %d)\n", *str, *str, *(str-1)); |
| 976 |
if (F_listfile) |
| 977 |
fprintf(FI_listfile, "char = '%c' code %d\n", *str, *str); |
| 978 |
return str+1; |
| 979 |
} |
| 980 |
|
| 981 |
if (*ptr == '$') |
| 982 |
ptr++; |
| 983 |
|
| 984 |
if ((sym = findsymbol(str, ptr - str)) != NULL) |
| 985 |
{ |
| 986 |
if (sym->flags & SYM_UNKNOWN) |
| 987 |
++Redo_eval; |
| 988 |
|
| 989 |
if (sym->flags & SYM_MACRO) |
| 990 |
{ |
| 991 |
macro = 1; |
| 992 |
sym = eval(sym->string, 0); |
| 993 |
} |
| 994 |
|
| 995 |
if (sym->flags & SYM_STRING) |
| 996 |
stackarg(0, SYM_STRING, sym->string); |
| 997 |
|
| 998 |
else |
| 999 |
stackarg(sym->value, sym->flags & SYM_UNKNOWN, NULL); |
| 1000 |
|
| 1001 |
sym->flags |= SYM_REF|SYM_MASREF; |
| 1002 |
|
| 1003 |
if (macro) |
| 1004 |
FreeSymbolList(sym); |
| 1005 |
} |
| 1006 |
else |
| 1007 |
{ |
| 1008 |
stackarg(0L, SYM_UNKNOWN, NULL); |
| 1009 |
sym = CreateSymbol( str, ptr - str ); |
| 1010 |
sym->flags = SYM_REF|SYM_MASREF|SYM_UNKNOWN; |
| 1011 |
++Redo_eval; |
| 1012 |
} |
| 1013 |
symbolRecursionCount--; |
| 1014 |
return ptr; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|