1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 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 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
|
/*
* NASM-compatible parser
*
* Copyright (C) 2001-2007 Peter Johnson, Michael Urman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <util.h>
#include <libyasm.h>
#include <math.h>
#include "modules/parsers/nasm/nasm-parser.h"
#include "modules/preprocs/nasm/nasm.h"
typedef enum {
NORM_EXPR,
DIR_EXPR, /* Can't have seg:off or WRT anywhere */
DV_EXPR /* Can't have registers anywhere */
} expr_type;
static yasm_bytecode *parse_line(yasm_parser_nasm *parser_nasm);
static int parse_directive_valparams(yasm_parser_nasm *parser_nasm,
/*@out@*/ yasm_valparamhead *vps);
static yasm_bytecode *parse_times(yasm_parser_nasm *parser_nasm);
static yasm_bytecode *parse_exp(yasm_parser_nasm *parser_nasm);
static yasm_bytecode *parse_instr(yasm_parser_nasm *parser_nasm);
static yasm_insn_operand *parse_operand(yasm_parser_nasm *parser_nasm);
static yasm_insn_operand *parse_memaddr(yasm_parser_nasm *parser_nasm);
static yasm_expr *parse_expr(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_bexpr(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr0(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr1(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr2(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr3(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr4(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr5(yasm_parser_nasm *parser_nasm, expr_type type);
static yasm_expr *parse_expr6(yasm_parser_nasm *parser_nasm, expr_type type);
static void nasm_parser_directive
(yasm_parser_nasm *parser_nasm, const char *name,
/*@null@*/ yasm_valparamhead *valparams,
/*@null@*/ yasm_valparamhead *objext_valparams);
static void set_nonlocal_label(yasm_parser_nasm *parser_nasm, const char *name);
static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name,
unsigned int size);
static void yasm_ea_set_implicit_size_segment(yasm_parser_nasm *parser_nasm,
yasm_effaddr *ea, yasm_expr *e)
{
if (parser_nasm->tasm) {
const char *segment = yasm_expr_segment(e);
ea->data_len = yasm_expr_size(e);
if (segment) {
const char *segreg = tasm_get_segment_register(segment);
if (segreg)
yasm_arch_parse_check_regtmod(p_object->arch, segreg,
strlen(segreg), &ea->segreg);
}
}
}
#define is_eol_tok(tok) ((tok) == 0)
#define is_eol() is_eol_tok(curtok)
#define get_next_token() (curtok = nasm_parser_lex(&curval, parser_nasm))
static void
get_peek_token(yasm_parser_nasm *parser_nasm)
{
char savech = parser_nasm->tokch;
if (parser_nasm->peek_token != NONE)
yasm_internal_error(N_("only can have one token of lookahead"));
parser_nasm->peek_token =
nasm_parser_lex(&parser_nasm->peek_tokval, parser_nasm);
parser_nasm->peek_tokch = parser_nasm->tokch;
parser_nasm->tokch = savech;
}
static void
destroy_curtok_(yasm_parser_nasm *parser_nasm)
{
if (curtok < 256)
;
else switch ((enum tokentype)curtok) {
case INTNUM:
yasm_intnum_destroy(curval.intn);
break;
case FLTNUM:
yasm_floatnum_destroy(curval.flt);
break;
case DIRECTIVE_NAME:
case FILENAME:
case ID:
case LOCAL_ID:
case SPECIAL_ID:
case NONLOCAL_ID:
yasm_xfree(curval.str_val);
break;
case STRING:
yasm_xfree(curval.str.contents);
break;
case INSN:
yasm_bc_destroy(curval.bc);
break;
default:
break;
}
curtok = NONE; /* sanity */
}
#define destroy_curtok() destroy_curtok_(parser_nasm)
/* Eat all remaining tokens to EOL, discarding all of them. If there's any
* intervening tokens, generates an error (junk at end of line).
*/
static void
demand_eol_(yasm_parser_nasm *parser_nasm)
{
if (is_eol())
return;
yasm_error_set(YASM_ERROR_SYNTAX,
N_("junk at end of line, first unrecognized character is `%c'"),
parser_nasm->tokch);
do {
destroy_curtok();
get_next_token();
} while (!is_eol());
}
#define demand_eol() demand_eol_(parser_nasm)
static const char *
describe_token(int token)
{
static char strch[] = "` '";
const char *str;
switch (token) {
case 0: str = "end of line"; break;
case INTNUM: str = "integer"; break;
case FLTNUM: str = "floating point value"; break;
case DIRECTIVE_NAME: str = "directive name"; break;
case FILENAME: str = "filename"; break;
case STRING: str = "string"; break;
case SIZE_OVERRIDE: str = "size override"; break;
case DECLARE_DATA: str = "DB/DW/etc."; break;
case RESERVE_SPACE: str = "RESB/RESW/etc."; break;
case INCBIN: str = "INCBIN"; break;
case EQU: str = "EQU"; break;
case TIMES: str = "TIMES"; break;
case SEG: str = "SEG"; break;
case WRT: str = "WRT"; break;
case NOSPLIT: str = "NOSPLIT"; break;
case STRICT: str = "STRICT"; break;
case INSN: str = "instruction"; break;
case PREFIX: str = "instruction prefix"; break;
case REG: str = "register"; break;
case REGGROUP: str = "register group"; break;
case SEGREG: str = "segment register"; break;
case TARGETMOD: str = "target modifier"; break;
case LEFT_OP: str = "<<"; break;
case RIGHT_OP: str = ">>"; break;
case SIGNDIV: str = "//"; break;
case SIGNMOD: str = "%%"; break;
case START_SECTION_ID: str = "$$"; break;
case ID: str = "identifier"; break;
case LOCAL_ID: str = ".identifier"; break;
case SPECIAL_ID: str = "..identifier"; break;
case NONLOCAL_ID: str = "..@identifier"; break;
case LINE: str = "%line"; break;
default:
strch[1] = token;
str = strch;
break;
}
return str;
}
static int
expect_(yasm_parser_nasm *parser_nasm, int token)
{
if (curtok == token)
return 1;
yasm_error_set(YASM_ERROR_PARSE, "expected %s", describe_token(token));
destroy_curtok();
return 0;
}
#define expect(token) expect_(parser_nasm, token)
void
nasm_parser_parse(yasm_parser_nasm *parser_nasm)
{
unsigned char *line;
while ((line = (unsigned char *)
yasm_preproc_get_line(parser_nasm->preproc)) != NULL) {
yasm_bytecode *bc = NULL, *temp_bc;
parser_nasm->s.bot = line;
parser_nasm->s.tok = line;
parser_nasm->s.ptr = line;
parser_nasm->s.cur = line;
parser_nasm->s.lim = line + strlen((char *)line)+1;
parser_nasm->s.top = parser_nasm->s.lim;
get_next_token();
if (!is_eol()) {
bc = parse_line(parser_nasm);
demand_eol();
}
if (parser_nasm->abspos) {
/* If we're inside an absolute section, just add to the absolute
* position rather than appending bytecodes to a section.
* Only RES* are allowed in an absolute section, so this is easy.
*/
if (bc) {
/*@null@*/ const yasm_expr *numitems, *multiple;
unsigned int itemsize;
numitems = yasm_bc_reserve_numitems(bc, &itemsize);
if (numitems) {
yasm_expr *e;
e = yasm_expr_create(YASM_EXPR_MUL,
yasm_expr_expr(yasm_expr_copy(numitems)),
yasm_expr_int(yasm_intnum_create_uint(itemsize)),
cur_line);
multiple = yasm_bc_get_multiple_expr(bc);
if (multiple)
e = yasm_expr_create_tree(e, YASM_EXPR_MUL,
yasm_expr_copy(multiple),
cur_line);
parser_nasm->abspos = yasm_expr_create_tree(
parser_nasm->abspos, YASM_EXPR_ADD, e, cur_line);
} else
yasm_error_set(YASM_ERROR_SYNTAX,
N_("only RES* allowed within absolute section"));
yasm_bc_destroy(bc);
}
temp_bc = NULL;
} else if (bc) {
temp_bc = yasm_section_bcs_append(cursect, bc);
if (temp_bc)
parser_nasm->prev_bc = temp_bc;
} else
temp_bc = NULL;
yasm_errwarn_propagate(parser_nasm->errwarns, cur_line);
if (parser_nasm->save_input)
yasm_linemap_add_source(parser_nasm->linemap, temp_bc,
(char *)line);
yasm_linemap_goto_next(parser_nasm->linemap);
yasm_xfree(line);
}
}
/* All parse_* functions expect to be called with curtok being their first
* token. They should return with curtok being the token *after* their
* information.
*/
static yasm_bytecode *
parse_line(yasm_parser_nasm *parser_nasm)
{
yasm_bytecode *bc;
bc = parse_exp(parser_nasm);
if (bc)
return bc;
switch (curtok) {
case LINE: /* LINE INTNUM '+' INTNUM FILENAME */
{
yasm_intnum *line, *incr;
char *filename;
get_next_token();
if (!expect(INTNUM)) return NULL;
line = INTNUM_val;
get_next_token();
if (!expect('+')) return NULL;
get_next_token();
if (!expect(INTNUM)) return NULL;
incr = INTNUM_val;
get_next_token();
if (!expect(FILENAME)) return NULL;
filename = FILENAME_val;
get_next_token();
/* %line indicates the line number of the *next* line, so subtract
* out the increment when setting the line number.
*/
yasm_linemap_set(parser_nasm->linemap, filename, 0,
yasm_intnum_get_uint(line) - yasm_intnum_get_uint(incr),
yasm_intnum_get_uint(incr));
yasm_intnum_destroy(line);
yasm_intnum_destroy(incr);
yasm_xfree(filename);
return NULL;
}
case '[': /* [ directive ] */
{
char *dirname;
yasm_valparamhead dir_vps;
int have_vps = 1;
parser_nasm->state = DIRECTIVE;
get_next_token();
if (!expect(DIRECTIVE_NAME))
return NULL;
dirname = DIRECTIVE_NAME_val;
get_next_token();
/* ignore [warning]. TODO: actually implement */
if (yasm__strcasecmp(dirname, "warning") == 0) {
yasm_warn_set(YASM_WARN_GENERAL,
N_("[warning] directive not supported; ignored"));
/* throw away the rest of the directive tokens */
while (!is_eol() && curtok != ']')
{
destroy_curtok();
get_next_token();
}
expect(']');
get_next_token();
return NULL;
}
if (curtok == ']' || curtok == ':')
have_vps = 0;
else if (!parse_directive_valparams(parser_nasm, &dir_vps)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("invalid arguments to [%s]"), dirname);
yasm_xfree(dirname);
return NULL;
}
if (curtok == ':') {
yasm_valparamhead ext_vps;
get_next_token();
if (!parse_directive_valparams(parser_nasm, &ext_vps)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("invalid arguments to [%s]"), dirname);
yasm_xfree(dirname);
return NULL;
}
nasm_parser_directive(parser_nasm, dirname,
have_vps ? &dir_vps : NULL, &ext_vps);
} else
nasm_parser_directive(parser_nasm, dirname,
have_vps ? &dir_vps : NULL, NULL);
yasm_xfree(dirname);
expect(']');
get_next_token();
return NULL;
}
case TIMES: /* TIMES expr exp */
get_next_token();
return parse_times(parser_nasm);
case ID:
case SPECIAL_ID:
case NONLOCAL_ID:
case LOCAL_ID:
{
char *name = ID_val;
int local = parser_nasm->tasm
? (curtok == ID || curtok == LOCAL_ID ||
(curtok == SPECIAL_ID && name[0] == '@'))
: (curtok != ID);
unsigned int size = 0;
get_next_token();
if (is_eol()) {
/* label alone on the line */
yasm_warn_set(YASM_WARN_ORPHAN_LABEL,
N_("label alone on a line without a colon might be in error"));
if (!local)
set_nonlocal_label(parser_nasm, name);
define_label(parser_nasm, name, 0);
return NULL;
}
if (curtok == ':')
get_next_token();
if (curtok == EQU || (parser_nasm->tasm && curtok == '=')) {
/* label EQU expr */
yasm_expr *e;
get_next_token();
if (parser_nasm->tasm && curtok == SIZE_OVERRIDE) {
size = SIZE_OVERRIDE_val;
get_next_token();
}
e = parse_expr(parser_nasm, NORM_EXPR);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression expected after %s"), "EQU");
yasm_xfree(name);
return NULL;
}
yasm_symtab_define_equ(p_symtab, name, e, cur_line);
yasm_xfree(name);
return NULL;
}
if (parser_nasm->tasm && curtok == LABEL)
get_next_token();
if (parser_nasm->tasm && curtok == SIZE_OVERRIDE) {
size = SIZE_OVERRIDE_val;
get_next_token();
}
if (!local)
set_nonlocal_label(parser_nasm, name);
if (is_eol()) {
define_label(parser_nasm, name, size);
return NULL;
}
if (curtok == TIMES) {
define_label(parser_nasm, name, size);
get_next_token();
return parse_times(parser_nasm);
}
bc = parse_exp(parser_nasm);
if (!parser_nasm->tasm && !bc)
yasm_error_set(YASM_ERROR_SYNTAX,
N_("instruction expected after label"));
if (parser_nasm->tasm && bc && !size)
size = yasm_bc_elem_size(bc);
define_label(parser_nasm, name, size);
return bc;
}
default:
yasm_error_set(YASM_ERROR_SYNTAX,
N_("label or instruction expected at start of line"));
return NULL;
}
}
static int
parse_directive_valparams(yasm_parser_nasm *parser_nasm,
/*@out@*/ yasm_valparamhead *vps)
{
yasm_vps_initialize(vps);
for (;;) {
yasm_valparam *vp;
yasm_expr *e;
char *id = NULL;
/* Look for value first */
if (curtok == ID) {
get_peek_token(parser_nasm);
if (parser_nasm->peek_token == '=') {
id = ID_val;
get_next_token(); /* id */
get_next_token(); /* '=' */
}
}
/* Look for parameter */
switch (curtok) {
case STRING:
vp = yasm_vp_create_string(id, STRING_val.contents);
get_next_token();
break;
case ID:
/* We need a peek token, but avoid error if we have one
* already; we need to work whether or not we hit the
* "value=" if test above.
*
* We cheat and peek ahead to see if this is just an ID or
* the ID is part of an expression. We assume a + or - means
* that it's part of an expression (e.g. "x+y" is parsed as
* the expression "x+y" and not as "x", "+y").
*/
if (parser_nasm->peek_token == NONE)
get_peek_token(parser_nasm);
switch (parser_nasm->peek_token) {
case '|': case '^': case '&': case LEFT_OP: case RIGHT_OP:
case '+': case '-':
case '*': case '/': case '%': case SIGNDIV: case SIGNMOD:
break;
default:
/* Just an id */
vp = yasm_vp_create_id(id, ID_val, '$');
get_next_token();
goto next;
}
/*@fallthrough@*/
default:
e = parse_expr(parser_nasm, DIR_EXPR);
if (!e) {
yasm_vps_delete(vps);
return 0;
}
vp = yasm_vp_create_expr(id, e);
break;
}
next:
yasm_vps_append(vps, vp);
if (curtok == ',')
get_next_token();
if (curtok == ']' || curtok == ':' || is_eol())
return 1;
}
}
static yasm_bytecode *
parse_times(yasm_parser_nasm *parser_nasm)
{
yasm_expr *multiple;
yasm_bytecode *bc;
multiple = parse_bexpr(parser_nasm, DV_EXPR);
if (!multiple) {
yasm_error_set(YASM_ERROR_SYNTAX, N_("expression expected after %s"),
"TIMES");
return NULL;
}
bc = parse_exp(parser_nasm);
if (!bc) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("instruction expected after TIMES expression"));
yasm_expr_destroy(multiple);
return NULL;
}
yasm_bc_set_multiple(bc, multiple);
return bc;
}
static yasm_bytecode *
parse_exp(yasm_parser_nasm *parser_nasm)
{
yasm_bytecode *bc;
bc = parse_instr(parser_nasm);
if (bc)
return bc;
switch (curtok) {
case DECLARE_DATA:
{
unsigned int size = DECLARE_DATA_val/8;
yasm_datavalhead dvs;
yasm_dataval *dv;
yasm_expr *e, *e2;
get_next_token();
yasm_dvs_initialize(&dvs);
for (;;) {
if (curtok == STRING) {
/* Peek ahead to see if we're in an expr. If we're not,
* then generate a real string dataval.
*/
get_peek_token(parser_nasm);
if (parser_nasm->peek_token == ','
|| is_eol_tok(parser_nasm->peek_token)) {
dv = yasm_dv_create_string(STRING_val.contents,
STRING_val.len);
get_next_token();
goto dv_done;
}
}
if (curtok == '?') {
yasm_dvs_delete(&dvs);
get_next_token();
if (! is_eol_tok(curtok)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("can not handle more than one '?'"));
return NULL;
}
return yasm_bc_create_reserve(
p_expr_new_ident(yasm_expr_int(
yasm_intnum_create_uint(1))),
size, cur_line);
}
if (!(e = parse_bexpr(parser_nasm, DV_EXPR))) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression or string expected"));
yasm_dvs_delete(&dvs);
return NULL;
}
if (curtok == DUP) {
get_next_token();
if (curtok != '(') {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected ( after DUP"));
goto error;
}
get_next_token();
if (curtok == '?') {
get_next_token();
if (curtok != ')') {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected ) after DUPlicated expression"));
goto error;
}
get_next_token();
if (! is_eol_tok(curtok)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("can not handle more than one '?'"));
goto error;
}
yasm_dvs_delete(&dvs);
return yasm_bc_create_reserve(e, size, cur_line);
} else if ((e2 = parse_bexpr(parser_nasm, DV_EXPR))) {
if (curtok != ')') {
yasm_expr_destroy(e2);
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected ) after DUPlicated expression"));
goto error;
}
get_next_token();
dv = yasm_dv_create_expr(e2);
yasm_dv_set_multiple(dv, e);
} else {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression or string expected"));
error:
yasm_expr_destroy(e);
yasm_dvs_delete(&dvs);
return NULL;
}
} else
dv = yasm_dv_create_expr(e);
dv_done:
yasm_dvs_append(&dvs, dv);
if (is_eol())
break;
if (!expect(',')) {
yasm_dvs_delete(&dvs);
return NULL;
}
get_next_token();
if (is_eol()) /* allow trailing , on list */
break;
}
return yasm_bc_create_data(&dvs, size, 0, p_object->arch,
cur_line);
}
case RESERVE_SPACE:
{
unsigned int size = RESERVE_SPACE_val/8;
yasm_expr *e;
get_next_token();
e = parse_bexpr(parser_nasm, DV_EXPR);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression expected after %s"), "RESx");
return NULL;
}
return yasm_bc_create_reserve(e, size, cur_line);
}
case INCBIN:
{
char *filename;
yasm_expr *start = NULL, *maxlen = NULL;
get_next_token();
if (!expect(STRING)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("filename string expected after INCBIN"));
return NULL;
}
filename = STRING_val.contents;
get_next_token();
/* optional start expression */
if (curtok == ',')
get_next_token();
if (is_eol())
goto incbin_done;
start = parse_bexpr(parser_nasm, DV_EXPR);
if (!start) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression expected for INCBIN start"));
return NULL;
}
/* optional maxlen expression */
if (curtok == ',')
get_next_token();
if (is_eol())
goto incbin_done;
maxlen = parse_bexpr(parser_nasm, DV_EXPR);
if (!maxlen) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expression expected for INCBIN maximum length"));
return NULL;
}
incbin_done:
return yasm_bc_create_incbin(filename, start, maxlen,
parser_nasm->linemap, cur_line);
}
default:
return NULL;
}
}
static yasm_bytecode *
parse_instr(yasm_parser_nasm *parser_nasm)
{
yasm_bytecode *bc;
switch (curtok) {
case INSN:
{
yasm_insn *insn;
bc = INSN_val;
insn = yasm_bc_get_insn(bc);
get_next_token();
if (is_eol())
return bc; /* no operands */
/* parse operands */
for (;;) {
yasm_insn_operand *op = parse_operand(parser_nasm);
if (!op) {
if (insn->num_operands == 0)
yasm_error_set(YASM_ERROR_SYNTAX,
N_("unexpected %s after instruction"),
describe_token(curtok));
else
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected operand, got %s"),
describe_token(curtok));
yasm_bc_destroy(bc);
return NULL;
}
yasm_insn_ops_append(insn, op);
if (is_eol())
break;
if (!expect(',')) {
yasm_bc_destroy(bc);
return NULL;
}
get_next_token();
}
return bc;
}
case PREFIX:
{
uintptr_t prefix = PREFIX_val;
get_next_token();
bc = parse_instr(parser_nasm);
if (!bc)
bc = yasm_arch_create_empty_insn(p_object->arch, cur_line);
yasm_insn_add_prefix(yasm_bc_get_insn(bc), prefix);
return bc;
}
case SEGREG:
{
uintptr_t segreg = SEGREG_val;
get_next_token();
bc = parse_instr(parser_nasm);
if (!bc)
bc = yasm_arch_create_empty_insn(p_object->arch, cur_line);
yasm_insn_add_seg_prefix(yasm_bc_get_insn(bc), segreg);
return bc;
}
default:
return NULL;
}
}
static yasm_insn_operand *
parse_operand(yasm_parser_nasm *parser_nasm)
{
yasm_insn_operand *op;
switch (curtok) {
case '[':
{
get_next_token();
op = parse_memaddr(parser_nasm);
expect(']');
get_next_token();
if (!op) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("memory address expected"));
return NULL;
}
if (parser_nasm->tasm && !is_eol() && curtok != ',') {
yasm_expr *e = NULL, *f;
yasm_effaddr *ea;
switch (op->type) {
case YASM_INSN__OPERAND_IMM:
e = op->data.val;
break;
case YASM_INSN__OPERAND_MEMORY:
if (op->data.ea->disp.rel) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("relative adressing not supported\n"));
return NULL;
}
e = yasm_expr_copy(op->data.ea->disp.abs);
yasm_arch_ea_destroy(p_object->arch, op->data.ea);
break;
case YASM_INSN__OPERAND_REG:
case YASM_INSN__OPERAND_SEGREG:
yasm_error_set(YASM_ERROR_SYNTAX,
N_("register adressing not supported\n"));
return NULL;
}
yasm_xfree(op);
f = parse_bexpr(parser_nasm, NORM_EXPR);
if (!f) {
yasm_expr_destroy(e);
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after ]"));
return NULL;
}
e = p_expr_new_tree(e, YASM_EXPR_ADD, f);
ea = yasm_arch_ea_create(p_object->arch, e);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, e);
op = yasm_operand_create_mem(ea);
}
return op;
}
case OFFSET:
{
yasm_insn_operand *op2;
get_next_token();
if (parser_nasm->masm && curtok == ID && !yasm__strcasecmp(ID_val, "flat")) {
get_next_token();
if (curtok == ':') {
get_next_token();
}
}
op = parse_operand(parser_nasm);
if (!op) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("memory address expected"));
return NULL;
}
if (op->type == YASM_INSN__OPERAND_IMM)
return op;
if (op->type != YASM_INSN__OPERAND_MEMORY) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("OFFSET applied to non-memory operand"));
return NULL;
}
if (op->data.ea->disp.rel) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("OFFSET applied to non-absolute memory operand"));
return NULL;
}
if (op->data.ea->disp.abs)
op2 = yasm_operand_create_imm(op->data.ea->disp.abs);
else
op2 = yasm_operand_create_imm(p_expr_new_ident(
yasm_expr_int(yasm_intnum_create_uint(0))));
yasm_xfree(op);
return op2;
}
case SEGREG:
{
uintptr_t segreg = SEGREG_val;
get_next_token();
if (parser_nasm->tasm && curtok == ':') {
get_next_token();
op = parse_operand(parser_nasm);
if (!op)
return NULL;
if (op->type == YASM_INSN__OPERAND_IMM) {
yasm_effaddr *ea = yasm_arch_ea_create(p_object->arch,
op->data.val);
yasm_insn_operand *op2;
yasm_ea_set_implicit_size_segment(parser_nasm, ea,
op->data.val);
op2 = yasm_operand_create_mem(ea);
op2->size = op->size;
yasm_xfree(op);
op = op2;
}
if (op->type != YASM_INSN__OPERAND_MEMORY) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("segment applied to non-memory operand"));
return NULL;
}
yasm_ea_set_segreg(op->data.ea, segreg);
return op;
}
op = yasm_operand_create_segreg(segreg);
return op;
}
case REG:
op = yasm_operand_create_reg(REG_val);
get_next_token();
return op;
case REGGROUP:
{
unsigned long regindex;
uintptr_t reg = REGGROUP_val;
get_next_token(); /* REGGROUP */
if (curtok != '(')
return yasm_operand_create_reg(reg);
get_next_token(); /* '(' */
if (!expect(INTNUM)) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("integer register index expected"));
return NULL;
}
regindex = yasm_intnum_get_uint(INTNUM_val);
get_next_token(); /* INTNUM */
if (!expect(')')) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("missing closing parenthesis for register index"));
return NULL;
}
get_next_token(); /* ')' */
reg = yasm_arch_reggroup_get_reg(p_object->arch, reg, regindex);
if (reg == 0) {
yasm_error_set(YASM_ERROR_SYNTAX, N_("bad register index `%u'"),
regindex);
return NULL;
}
return yasm_operand_create_reg(reg);
}
case STRICT:
get_next_token();
op = parse_operand(parser_nasm);
if (op)
op->strict = 1;
return op;
case SIZE_OVERRIDE:
{
unsigned int size = SIZE_OVERRIDE_val;
get_next_token();
if (parser_nasm->masm && curtok == ID && !yasm__strcasecmp(ID_val, "ptr")) {
get_next_token();
}
op = parse_operand(parser_nasm);
if (!op)
return NULL;
if (op->type == YASM_INSN__OPERAND_REG &&
yasm_arch_get_reg_size(p_object->arch, op->data.reg) != size)
yasm_error_set(YASM_ERROR_TYPE,
N_("cannot override register size"));
else {
/* Silently override others unless a warning is turned on.
* This is to allow overrides such as:
* %define arg1 dword [bp+4]
* cmp word arg1, 2
* Which expands to:
* cmp word dword [bp+4], 2
*/
if (op->size != 0) {
if (op->size != size)
yasm_warn_set(YASM_WARN_SIZE_OVERRIDE,
N_("overriding operand size from %u-bit to %u-bit"),
op->size, size);
else
yasm_warn_set(YASM_WARN_SIZE_OVERRIDE,
N_("double operand size override"));
}
op->size = size;
}
return op;
}
case TARGETMOD:
{
uintptr_t tmod = TARGETMOD_val;
get_next_token();
op = parse_operand(parser_nasm);
if (op)
op->targetmod = tmod;
return op;
}
case ID:
case LOCAL_ID:
case NONLOCAL_ID:
if (parser_nasm->tasm) {
get_peek_token(parser_nasm);
if (parser_nasm->peek_token == '[') {
yasm_symrec *sym = yasm_symtab_use(p_symtab, ID_val,
cur_line);
yasm_expr *e = p_expr_new_ident(yasm_expr_sym(sym)), *f;
yasm_effaddr *ea;
yasm_xfree(ID_val);
get_next_token();
get_next_token();
f = parse_bexpr(parser_nasm, NORM_EXPR);
if (!f) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after ["));
return NULL;
}
e = p_expr_new_tree(e, YASM_EXPR_ADD, f);
if (!expect(']')) {
yasm_error_set(YASM_ERROR_SYNTAX, N_("missing closing bracket"));
return NULL;
}
get_next_token();
ea = yasm_arch_ea_create(p_object->arch, e);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, e);
op = yasm_operand_create_mem(ea);
return op;
}
}
/* Fallthrough */
default:
{
yasm_expr *e = parse_bexpr(parser_nasm, NORM_EXPR);
if (!e)
return NULL;
if (curtok != ':') {
if (parser_nasm->tasm && yasm_expr_size(e)) {
yasm_effaddr *ea = yasm_arch_ea_create(p_object->arch, e);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, e);
op = yasm_operand_create_mem(ea);
return op;
} else if (curtok == '[') {
yasm_expr *f;
yasm_effaddr *ea;
yasm_insn_operand *op2;
op = parse_operand(parser_nasm);
if (!op)
return NULL;
f = op->data.ea->disp.abs;
e = p_expr_new_tree(e, YASM_EXPR_ADD, f);
ea = yasm_arch_ea_create(p_object->arch, e);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, e);
op2 = yasm_operand_create_mem(ea);
yasm_xfree(op);
return op2;
} else {
return yasm_operand_create_imm(e);
}
} else {
yasm_expr *off;
get_next_token();
off = parse_bexpr(parser_nasm, NORM_EXPR);
if (!off) {
yasm_expr_destroy(e);
return NULL;
}
op = yasm_operand_create_imm(off);
op->seg = e;
return op;
}
}
}
}
/* memory addresses */
static yasm_insn_operand *
parse_memaddr(yasm_parser_nasm *parser_nasm)
{
yasm_insn_operand *op;
switch (curtok) {
case SEGREG:
{
uintptr_t segreg = SEGREG_val;
get_next_token();
if (!expect(':')) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("`:' required after segment register"));
return NULL;
}
get_next_token();
op = parse_memaddr(parser_nasm);
if (op)
yasm_ea_set_segreg(op->data.ea, segreg);
return op;
}
case SIZE_OVERRIDE:
{
unsigned int size = SIZE_OVERRIDE_val;
get_next_token();
op = parse_memaddr(parser_nasm);
if (op)
op->data.ea->disp.size = size;
return op;
}
case NOSPLIT:
get_next_token();
op = parse_memaddr(parser_nasm);
if (op)
op->data.ea->nosplit = 1;
return op;
case REL:
get_next_token();
op = parse_memaddr(parser_nasm);
if (op) {
op->data.ea->pc_rel = 1;
op->data.ea->not_pc_rel = 0;
}
return op;
case ABS:
get_next_token();
op = parse_memaddr(parser_nasm);
if (op) {
op->data.ea->pc_rel = 0;
op->data.ea->not_pc_rel = 1;
}
return op;
default:
{
yasm_expr *e = parse_bexpr(parser_nasm, NORM_EXPR);
if (!e)
return NULL;
if (curtok != ':') {
yasm_effaddr *ea = yasm_arch_ea_create(p_object->arch, e);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, e);
return yasm_operand_create_mem(ea);
} else {
yasm_effaddr *ea;
yasm_expr *off;
get_next_token();
off = parse_bexpr(parser_nasm, NORM_EXPR);
if (!off) {
yasm_expr_destroy(e);
return NULL;
}
ea = yasm_arch_ea_create(p_object->arch, off);
yasm_ea_set_implicit_size_segment(parser_nasm, ea, off);
op = yasm_operand_create_mem(ea);
op->seg = e;
return op;
}
}
}
}
/* Expression grammar parsed is:
*
* expr : bexpr [ : bexpr ]
* bexpr : expr0 [ WRT expr6 ]
* expr0 : expr1 [ {|} expr1...]
* expr1 : expr2 [ {^} expr2...]
* expr2 : expr3 [ {&} expr3...]
* expr3 : expr4 [ {<<,>>} expr4...]
* expr4 : expr5 [ {+,-} expr5...]
* expr5 : expr6 [ {*,/,%,//,%%} expr6...]
* expr6 : { ~,+,-,SEG } expr6
* | (expr)
* | symbol
* | $
* | number
*/
#define parse_expr_common(leftfunc, tok, rightfunc, op) \
do { \
yasm_expr *e, *f; \
e = leftfunc(parser_nasm, type); \
if (!e) \
return NULL; \
\
while (curtok == tok) { \
get_next_token(); \
f = rightfunc(parser_nasm, type); \
if (!f) { \
yasm_error_set(YASM_ERROR_SYNTAX, \
N_("expected expression after %s"), \
describe_token(op)); \
yasm_expr_destroy(e); \
return NULL; \
} \
e = p_expr_new_tree(e, op, f); \
} \
return e; \
} while(0)
static yasm_expr *
parse_expr(yasm_parser_nasm *parser_nasm, expr_type type)
{
switch (type) {
case DIR_EXPR:
/* directive expressions can't handle seg:off or WRT */
return parse_expr0(parser_nasm, type);
default:
parse_expr_common(parse_bexpr, ':', parse_bexpr, YASM_EXPR_SEGOFF);
}
/*@notreached@*/
return NULL;
}
static yasm_expr *
parse_bexpr(yasm_parser_nasm *parser_nasm, expr_type type)
{
parse_expr_common(parse_expr0, WRT, parse_expr6, YASM_EXPR_WRT);
}
static yasm_expr *
parse_expr0(yasm_parser_nasm *parser_nasm, expr_type type)
{
parse_expr_common(parse_expr1, '|', parse_expr1, YASM_EXPR_OR);
}
static yasm_expr *
parse_expr1(yasm_parser_nasm *parser_nasm, expr_type type)
{
parse_expr_common(parse_expr2, '^', parse_expr2, YASM_EXPR_XOR);
}
static yasm_expr *
parse_expr2(yasm_parser_nasm *parser_nasm, expr_type type)
{
parse_expr_common(parse_expr3, '&', parse_expr3, YASM_EXPR_AND);
}
static yasm_expr *
parse_expr3(yasm_parser_nasm *parser_nasm, expr_type type)
{
yasm_expr *e, *f;
e = parse_expr4(parser_nasm, type);
if (!e)
return NULL;
while (curtok == LEFT_OP || curtok == RIGHT_OP) {
int op = curtok;
get_next_token();
f = parse_expr4(parser_nasm, type);
if (!f) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"),
describe_token(op));
yasm_expr_destroy(e);
return NULL;
}
switch (op) {
case LEFT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHL, f); break;
case RIGHT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHR, f); break;
}
}
return e;
}
static yasm_expr *
parse_expr4(yasm_parser_nasm *parser_nasm, expr_type type)
{
yasm_expr *e, *f;
e = parse_expr5(parser_nasm, type);
if (!e)
return NULL;
while (curtok == '+' || curtok == '-') {
int op = curtok;
get_next_token();
f = parse_expr5(parser_nasm, type);
if (!f) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"),
describe_token(op));
yasm_expr_destroy(e);
return NULL;
}
switch (op) {
case '+': e = p_expr_new_tree(e, YASM_EXPR_ADD, f); break;
case '-': e = p_expr_new_tree(e, YASM_EXPR_SUB, f); break;
}
}
return e;
}
static yasm_expr *
parse_expr5(yasm_parser_nasm *parser_nasm, expr_type type)
{
yasm_expr *e, *f;
e = parse_expr6(parser_nasm, type);
if (!e)
return NULL;
while (curtok == '*' || curtok == '/' || curtok == '%'
|| curtok == SIGNDIV || curtok == SIGNMOD) {
int op = curtok;
get_next_token();
f = parse_expr6(parser_nasm, type);
if (!f) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"),
describe_token(op));
yasm_expr_destroy(e);
return NULL;
}
switch (op) {
case '*': e = p_expr_new_tree(e, YASM_EXPR_MUL, f); break;
case '/': e = p_expr_new_tree(e, YASM_EXPR_DIV, f); break;
case '%': e = p_expr_new_tree(e, YASM_EXPR_MOD, f); break;
case SIGNDIV: e = p_expr_new_tree(e, YASM_EXPR_SIGNDIV, f); break;
case SIGNMOD: e = p_expr_new_tree(e, YASM_EXPR_SIGNMOD, f); break;
}
}
return e;
}
static yasm_expr *
parse_expr6(yasm_parser_nasm *parser_nasm, expr_type type)
{
yasm_expr *e;
yasm_symrec *sym;
switch (curtok) {
case '+':
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "`+'");
}
return e;
case '-':
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "`-'");
return NULL;
}
return p_expr_new_branch(YASM_EXPR_NEG, e);
case '~':
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "`~'");
return NULL;
}
return p_expr_new_branch(YASM_EXPR_NOT, e);
case LOW:
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "LOW");
return NULL;
}
return p_expr_new_tree(e, YASM_EXPR_AND,
p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0xff))));
case HIGH:
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "HIGH");
return NULL;
}
return p_expr_new_tree(
p_expr_new_tree(e, YASM_EXPR_SHR,
p_expr_new_ident(yasm_expr_int(
yasm_intnum_create_uint(8)))),
YASM_EXPR_AND,
p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0xff))));
case SEG:
get_next_token();
e = parse_expr6(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "SEG");
return NULL;
}
return p_expr_new_branch(YASM_EXPR_SEG, e);
case '(':
get_next_token();
e = parse_expr(parser_nasm, type);
if (!e) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("expected expression after %s"), "`('");
return NULL;
}
if (!expect(')')) {
yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis"));
return NULL;
}
get_next_token();
return e;
case INTNUM:
e = p_expr_new_ident(yasm_expr_int(INTNUM_val));
get_next_token();
return e;
case REG:
if (type == DV_EXPR) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("data values can't have registers"));
return NULL;
}
e = p_expr_new_ident(yasm_expr_reg(REG_val));
get_next_token();
return e;
}
/* directives allow very little and handle IDs specially */
if (type == DIR_EXPR) {
switch (curtok) {
case ID:
sym = yasm_symtab_use(p_symtab, ID_val, cur_line);
e = p_expr_new_ident(yasm_expr_sym(sym));
yasm_xfree(ID_val);
break;
default:
return NULL;
}
} else switch (curtok) {
case FLTNUM:
e = p_expr_new_ident(yasm_expr_float(FLTNUM_val));
break;
case STRING:
{
yasm_intnum *intn;
if (parser_nasm->tasm)
intn = yasm_intnum_create_charconst_tasm(STRING_val.contents);
else
intn = yasm_intnum_create_charconst_nasm(STRING_val.contents);
e = p_expr_new_ident(yasm_expr_int(intn));
yasm_xfree(STRING_val.contents);
break;
}
case SPECIAL_ID:
sym = yasm_objfmt_get_special_sym(p_object, ID_val+2, "nasm");
if (sym) {
e = p_expr_new_ident(yasm_expr_sym(sym));
yasm_xfree(ID_val);
break;
}
/*@fallthrough@*/
case ID:
case LOCAL_ID:
case NONLOCAL_ID:
sym = yasm_symtab_use(p_symtab, ID_val, cur_line);
e = p_expr_new_ident(yasm_expr_sym(sym));
yasm_xfree(ID_val);
break;
case '$':
/* "$" references the current assembly position */
if (parser_nasm->abspos)
e = yasm_expr_copy(parser_nasm->abspos);
else {
sym = yasm_symtab_define_curpos(p_symtab, "$",
parser_nasm->prev_bc, cur_line);
e = p_expr_new_ident(yasm_expr_sym(sym));
}
break;
case START_SECTION_ID:
/* "$$" references the start of the current section */
if (parser_nasm->absstart)
e = yasm_expr_copy(parser_nasm->absstart);
else {
sym = yasm_symtab_define_label(p_symtab, "$$",
yasm_section_bcs_first(cursect), 0, cur_line);
e = p_expr_new_ident(yasm_expr_sym(sym));
}
break;
default:
return NULL;
}
get_next_token();
return e;
}
static void
set_nonlocal_label(yasm_parser_nasm *parser_nasm, const char *name)
{
if (!parser_nasm->tasm || tasm_locals) {
if (parser_nasm->locallabel_base)
yasm_xfree(parser_nasm->locallabel_base);
parser_nasm->locallabel_base_len = strlen(name);
parser_nasm->locallabel_base =
yasm_xmalloc(parser_nasm->locallabel_base_len+1);
strcpy(parser_nasm->locallabel_base, name);
}
}
static void
define_label(yasm_parser_nasm *parser_nasm, char *name, unsigned int size)
{
yasm_symrec *symrec;
if (parser_nasm->abspos)
symrec = yasm_symtab_define_equ(p_symtab, name,
yasm_expr_copy(parser_nasm->abspos),
cur_line);
else
symrec = yasm_symtab_define_label(p_symtab, name, parser_nasm->prev_bc,
1, cur_line);
yasm_symrec_set_size(symrec, size);
yasm_symrec_set_segment(symrec, tasm_segment);
yasm_xfree(name);
}
static void
dir_align(yasm_object *object, yasm_valparamhead *valparams,
yasm_valparamhead *objext_valparams, unsigned long line)
{
yasm_valparam *vp = yasm_vps_first(valparams);
yasm_expr *boundval = yasm_vp_expr(vp, object->symtab, line);
/*@depedent@*/ yasm_intnum *boundintn;
/* Largest .align in the section specifies section alignment.
* Note: this doesn't match NASM behavior, but is a lot more
* intelligent!
*/
if (boundval && (boundintn = yasm_expr_get_intnum(&boundval, 0))) {
unsigned long boundint = yasm_intnum_get_uint(boundintn);
/* Alignments must be a power of two. */
if (is_exp2(boundint)) {
if (boundint > yasm_section_get_align(object->cur_section))
yasm_section_set_align(object->cur_section, boundint, line);
}
}
/* As this directive is called only when nop is used as fill, always
* use arch (nop) fill.
*/
yasm_section_bcs_append(object->cur_section,
yasm_bc_create_align(boundval, NULL, NULL,
/*yasm_section_is_code(object->cur_section) ?*/
yasm_arch_get_fill(object->arch)/* : NULL*/,
line));
}
static void
nasm_parser_directive(yasm_parser_nasm *parser_nasm, const char *name,
yasm_valparamhead *valparams,
yasm_valparamhead *objext_valparams)
{
unsigned long line = cur_line;
yasm_valparam *vp;
if (!yasm_object_directive(p_object, name, "nasm", valparams,
objext_valparams, line))
;
else if (yasm__strcasecmp(name, "absolute") == 0) {
if (!valparams) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("directive `%s' requires an argument"),
"absolute");
} else {
vp = yasm_vps_first(valparams);
if (parser_nasm->absstart)
yasm_expr_destroy(parser_nasm->absstart);
if (parser_nasm->abspos)
yasm_expr_destroy(parser_nasm->abspos);
parser_nasm->absstart = yasm_vp_expr(vp, p_object->symtab, line);
parser_nasm->abspos = yasm_expr_copy(parser_nasm->absstart);
cursect = NULL;
parser_nasm->prev_bc = NULL;
}
} else if (yasm__strcasecmp(name, "align") == 0) {
/* Really, we shouldn't end up with an align directive in an absolute
* section (as it's supposed to be only used for nop fill), but handle
* it gracefully anyway.
*/
if (parser_nasm->abspos) {
yasm_expr *boundval, *e;
vp = yasm_vps_first(valparams);
boundval = yasm_vp_expr(vp, p_object->symtab, line);
e = yasm_expr_create_tree(
yasm_expr_create_tree(yasm_expr_copy(parser_nasm->absstart),
YASM_EXPR_SUB,
yasm_expr_copy(parser_nasm->abspos),
cur_line),
YASM_EXPR_AND,
yasm_expr_create(YASM_EXPR_SUB, yasm_expr_expr(boundval),
yasm_expr_int(yasm_intnum_create_uint(1)),
cur_line),
cur_line);
parser_nasm->abspos = yasm_expr_create_tree(
parser_nasm->abspos, YASM_EXPR_ADD, e, cur_line);
} else if (!valparams) {
yasm_error_set(YASM_ERROR_SYNTAX,
N_("directive `%s' requires an argument"), "align");
} else
dir_align(p_object, valparams, objext_valparams, line);
} else if (yasm__strcasecmp(name, "default") == 0) {
if (!valparams)
;
else {
vp = yasm_vps_first(valparams);
while (vp) {
const char *id = yasm_vp_id(vp);
if (id) {
if (yasm__strcasecmp(id, "rel") == 0)
yasm_arch_set_var(p_object->arch, "default_rel", 1);
else if (yasm__strcasecmp(id, "abs") == 0)
yasm_arch_set_var(p_object->arch, "default_rel", 0);
else
yasm_error_set(YASM_ERROR_SYNTAX,
N_("unrecognized default `%s'"), id);
} else
yasm_error_set(YASM_ERROR_SYNTAX,
N_("unrecognized default value"));
vp = yasm_vps_next(vp);
}
}
} else
yasm_error_set(YASM_ERROR_SYNTAX, N_("unrecognized directive `%s'"),
name);
if (parser_nasm->absstart && cursect) {
/* We switched to a new section. Get out of absolute section mode. */
yasm_expr_destroy(parser_nasm->absstart);
parser_nasm->absstart = NULL;
if (parser_nasm->abspos) {
yasm_expr_destroy(parser_nasm->abspos);
parser_nasm->abspos = NULL;
}
}
if (cursect) {
/* In case cursect changed or a bytecode was added, update prev_bc. */
parser_nasm->prev_bc = yasm_section_bcs_last(cursect);
}
if (valparams)
yasm_vps_delete(valparams);
if (objext_valparams)
yasm_vps_delete(objext_valparams);
}
yasm_bytecode *
gas_intel_syntax_parse_instr(yasm_parser_nasm *parser_nasm, unsigned char *instr)
{
yasm_bytecode *bc = NULL;
char *sinstr = (char *) instr;
parser_nasm->s.bot = instr;
parser_nasm->s.tok = instr;
parser_nasm->s.ptr = instr;
parser_nasm->s.cur = instr;
parser_nasm->s.lim = instr + strlen(sinstr) + 1;
parser_nasm->s.top = parser_nasm->s.lim;
parser_nasm->peek_token = NONE;
get_next_token();
if (!is_eol()) {
bc = parse_instr(parser_nasm);
}
return bc;
}
|