Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1623 lines (1444 sloc) 25.878 kb
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
#
# HEX5 for Linux-i386-ELF
#
# Copyright (C) 2001, Edmund GRIMLEY EVANS <edmundo@rano.org>
#

###
### ELF headers
###

# Elf32_Ehdr
  '7f '45 '4c '46 '01 '01 '01 # e_ident
  '00 '00 '00 '00 '00 '00 '00 '00 '00
  '02 '00 # e_type
  '03 '00 # e_machine
  '01 '00 '00 '00 # e_version
  '54 '80 '04 '08 # e_entry = 0x08048000 + _start
  '34 '00 '00 '00 # e_phoff = len(ehdr)
  '00 '00 '00 '00 # e_shoff
  '00 '00 '00 '00 # e_flags
  '34 '00 # e_ehsize = len(ehdr)
  '20 '00 # e_phentsize = len(phdr)
  '01 '00 # e_phnum
  '00 '00 # e_shentsize
  '00 '00 # e_shnum
  '00 '00 # e_shstrndx

# Elf32_Phdr
  '01 '00 '00 '00 # p_type
  '00 '00 '00 '00 # p_offset
  '00 '80 '04 '08 # p_vaddr = 0x08048000
  '00 '80 '04 '08 # p_paddr = 0x08048000
  '00 '40 '00 '00 #FIXME # p_filesz = len(ehdr) + len(phdr) + len(prog)
  '00 '40 '00 '00 #FIXME # p_memsz = len(ehdr) + len(phdr) + len(prog)
  '07 '00 '00 '00 # p_flags
  '00 '10 '00 '00 # p_align

######################################################################

# Enter here:

def _start
{
0 main exit
}

######################################################################

###
### Support for functions: arguments, variables, return values
###

_def xreturn0
'5b # pop %ebx
'5b # pop %ebx
'c9 # leave
'58 # pop %eax
'8d '24 '9c # lea (%esp,%ebx,4),%esp
'50 # push %eax
'c3 # ret

_def xreturn1
'5b # pop %ebx
'5b # pop %ebx
'59 # pop %ecx
'c9 # leave
'58 # pop %eax
'8d '24 '9c # lea (%esp,%ebx,4),%esp
'51 # push %ecx
'50 # push %eax
'c3 # ret

###
### Stack manipulation
###

_def drop
'5b # pop %ebx
'58 # pop %eax
'ff 'e3 # jmp *%ebx

_def swap
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'50 # push %eax
'51 # push %ecx
'ff 'e3 # jmp *%ebx

_def dup
'5b # pop %ebx
'58 # pop %eax
'50 # push %eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def twodup
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'51 # push %ecx
'50 # push %eax
'51 # push %ecx
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def rot
'58 # pop %eax
'5b # pop %ebx
'59 # pop %ecx
'5a # pop %edx
'51 # push %ecx
'53 # push %ebx
'52 # push %edx
'50 # push %eax
'c3

_def pick
'8b '44 '24 '04 # mov 4(%esp),%eax
'01 'c0 # add %eax,%eax
'01 'c0 # add %eax,%eax
'8d '5c '24 '08 # lea 8(%esp),%ebx
'01 'd8 # add %ebx,%eax
'8b '00 # mov (%eax),%eax
'89 '44 '24 '04 # mov %eax,4(%esp)
'c3 # ret

###
### Arithmetic
###

_def -
'5b # pop %ebx
'58 # pop %eax
'29 '04 '24 # sub %eax,(%esp)
'ff 'e3 # jmp *%ebx

_def +
'5b # pop %ebx
'58 # pop %eax
'01 '04 '24 # add %eax,(%esp)
'ff 'e3 # jmp *%ebx

_def bitand
'5b # pop %ebx
'58 # pop %eax
'21 '04 '24 # and %eax,(%esp)
'ff 'e3 # jmp *%ebx

_def *
'5b # pop %ebx
'58 # pop %eax
'0f 'af '04 '24 # imul (%esp),%eax
'89 '04 '24 # mov %eax,(%esp)
'ff 'e3 # jmp *%ebx

_def <<
'5b # pop %ebx
'59 # pop %ecx
'58 # pop %eax
'd3 'e0 # shl %cl,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def >>
'5b # pop %ebx
'59 # pop %ecx
'58 # pop %eax
'd3 'f8 # sar %cl,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

# Comparisons

_def <
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'39 'c1 # cmp %eax,%ecx
'0f '9c 'c0 # setl %al
'25 'ff '00 '00 '00 # and $0xff,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def <=
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'39 'c1 # cmp %eax,%ecx
'0f '9e 'c0 # setle %al
'25 'ff '00 '00 '00 # and $0xff,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def ==
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'39 'c1 # cmp %eax,%ecx
'0f '94 'c0 # sete %al
'25 'ff '00 '00 '00 # and $0xff,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

_def !=
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'39 'c1 # cmp %eax,%ecx
'0f '95 'c0 # setne %al
'25 'ff '00 '00 '00 # and $0xff,%eax
'50 # push %eax
'ff 'e3 # jmp *%ebx

###
### Memory access
###

_def @
'5b # pop %ebx
'58 # pop %eax
'8b '00 # mov (%eax),%eax
'50 # push %eax
'53 # push %ebx
'c3 # ret

_def =
'5b # pop %ebx
'58 # pop %eax
'59 # pop %ecx
'89 '08 # mov %ecx,(%eax)
'53 # push %ebx
'c3 # ret

_def c[]
'5b # pop %ebx
'5a # pop %edx
'58 # pop %eax
'0f 'be '04 '10 # movsbl (%eax,%edx,1),%eax
'50 # push %eax
'53 # push %ebx
'c3 # ret

_def c[]=
'5b # pop %ebx
'5a # pop %edx
'58 # pop %eax
'59 # pop %ecx
'88 '0c '10 # mov %cl,(%eax,%edx,1)
'53 # push %ebx
'c3 # ret

_def c[]&
'5b # pop %ebx
'5a # pop %edx
'58 # pop %eax
'8d '04 '10 # lea (%eax,%edx,1),%eax
'50 # push %eax
'53 # push %ebx
'c3 # ret

###
### Flow of control
###

_def jump
'58 # pop %eax
'5b # pop %ebx
'01 'c3 # add %eax,%ebx
'83 'eb '05 # sub $5,%ebx
'53 # push %ebx
'c3 # ret

_def branch
'58 # pop %eax
'5b # pop %ebx
'59 # pop %ecx
'85 'c9 # test %ecx,%ecx
'75 '02 # jne +2
'50 # push %eax
'c3 # ret
'01 'c3 # add %eax,%ebx
'83 'eb '05 # sub $5,%ebx
'53 # push %ebx
'c3 # ret

_def call
'58 # pop %eax
'5b # pop %ebx
'50 # push %eax
'53 # push %ebx
'c3 # ret

######################################################################

###
### System calls
###

_def exit
'5b # pop %ebx
'5b # pop %ebx
'31 'c0 # xor %eax,%eax
'40 # inc %eax
'cd '80 # int $0x80

_def _brk
'58 # pop %eax
'5b # pop %ebx
'50 # push %eax
'b8 '2d '00 '00 '00 # mov $0x45,%eax
'cd '80 # int $0x80
'5b # pop %ebx
'50 # push %eax
'53 # push %ebx
'c3 # ret

_def getchar
'6a '00 # push $0x0
'31 'db # xor %ebx,%ebx
'89 'e1 # mov %esp,%ecx
'31 'd2 # xor %edx,%edx
'42 # inc %edx
'b8 '03 '00 '00 '00 # mov $0x3,%eax
'cd '80 # int $0x80
'5b # pop %ebx
'85 'c0 # test %eax,%eax
'75 '01 # jne +1
'4b # dec %ebx
'58 # pop %eax
'53 # push %ebx
'50 # push %eax
'c3 # ret

_def putchar
'31 'db # xor %ebx,%ebx
'43 # inc %ebx
'8d '4c '24 '04 # lea 0x4(%esp,1),%ecx
'89 'da # mov %ebx,%edx
'b8 '04 '00 '00 '00 # mov $0x4,%eax
'cd '80 # int $0x80
'5b # pop %ebx
'58 # pop %eax
'53 # push %ebx
'c3 # ret

def increment sbrk
{
var pos

0 _brk pos=
pos increment + dup _brk == if
pos 1 xreturn1
fi
0 1 - 1 xreturn1
}

######################################################################

###
### Debugging
###

# A stackless implementation of exit(42), for debugging:
# '31 'c0 '40 'b3 '2a 'cd '80

def w outw
{
w
dup putchar 8 >>
dup putchar 8 >>
dup putchar 8 >>
putchar
1 xreturn0
}

def outs
{
3735928559 # 0xdeadbeef
outw outw outw outw
outw outw outw outw
outw outw outw outw
0 exit
}

######################################################################

###
### A bogus implementation of malloc, realloc, free
###

def wsize
{
4 0 xreturn1
}

def size malloc
{
size wsize + sbrk
dup size swap =
wsize + 1 xreturn1
}

def ptr size realloc
{
var old_size
var new_ptr

ptr 0 == if
# just call malloc
size malloc 2 xreturn1
fi
ptr wsize - @ old_size=
size old_size <= if
# keep the same bit of memory
ptr 2 xreturn1
fi
size malloc new_ptr=
# copy old data to new memory
{
old_size 0 == if break fi
old_size 1 - old_size=
ptr old_size c[] new_ptr old_size c[]=
continue
}
new_ptr 2 xreturn1
}

def free
{
# do nothing
1 xreturn0
}

######################################################################

###
### The program itself
###

def x not
{
x if
0 1 xreturn1
else
1 1 xreturn1
fi
}

def a b cmp
{
a b < if
0 1 - 2 xreturn1
fi
a b == if
0 2 xreturn1
fi
1 2 xreturn1
}

def p q strcmp
{
var i
var r
0 i=
{
p i c[] q i c[] cmp r=
r if break fi
p i c[] not if break fi
i 1 + i=
continue
}
r 2 xreturn1
}

def s strlen
{
var n
0 n=
{
s n c[] not if break fi
n 1 + n=
continue
}
n 1 xreturn1
}

def s strdup
{
var n
var s1
0 n=
s strlen 1 + malloc s1=
{
s n c[] not if break fi
s n c[] s1 n c[]=
n 1 + n=
continue
}
s1 1 xreturn1
}

def s prints
{
s
0
{
twodup c[] dup not if 1 xreturn0 fi
putchar
1 +
continue
}
}

###
### Binary image
###

_def _var_address
'58 # pop %eax
'83 'c0 '02 # add $0x2,%eax
'c9 # leave
'5b # pop %ebx
'50 # push %eax
'53 # push %ebx
'c3 # ret

var binary
var binary_size
var pc

def out_of_memory
{
123 exit
}

def addr data emit
{
addr binary_size < not if
addr 1 + 1 << binary_size=
binary binary_size realloc binary=
binary not if
out_of_memory
fi
fi
data binary addr c[]=
2 xreturn0
}

def dump
{
var i
0 i=
{
i pc == if break fi
binary i c[] putchar
i 1 + i=
continue
}
0 xreturn0
}

def pos n store_int
{
n binary pos c[]& =
2 xreturn0
}

def pos fetch_int
{
binary pos c[]& @
1 xreturn1
}

###
### Lexical analysis
###

def token_eof
{ _var_address }
'00
def token_def
{ _var_address }
'64 '65 '66 '00
def token__def
{ _var_address }
'5f '64 '65 '66 '00
def token_ob
{ _var_address }
'7b '00
def token_cb
{ _var_address }
'7d '00
def token_var
{ _var_address }
'76 '61 '72 '00
def token_if
{ _var_address }
'69 '66 '00
def token_else
{ _var_address }
'65 '6c '73 '65 '00
def token_fi
{ _var_address }
'66 '69 '00
def token_break
{ _var_address }
'62 '72 '65 '61 '6b '00
def token_continue
{ _var_address }
'63 '6f '6e '74 '69 '6e '75 '65 '00
def token_until
{ _var_address }
'75 '6e '74 '69 '6c '00
def token_while
{ _var_address }
'77 '68 '69 '6c '65 '00
def token_string
{ _var_address }
'73 '74 '72 '69 '6e '67 '00
def token_return0
{ _var_address }
'72 '65 '74 '75 '72 '6e '30 '00
def token_return1
{ _var_address }
'72 '65 '74 '75 '72 '6e '31 '00

def s is_symbol
{
s token_eof strcmp not if 0 1 xreturn1 fi
s token_def strcmp not if 0 1 xreturn1 fi
s token_ob strcmp not if 0 1 xreturn1 fi
s token_cb strcmp not if 0 1 xreturn1 fi
s token_var strcmp not if 0 1 xreturn1 fi
s token_if strcmp not if 0 1 xreturn1 fi
s token_else strcmp not if 0 1 xreturn1 fi
s token_fi strcmp not if 0 1 xreturn1 fi
s token_break strcmp not if 0 1 xreturn1 fi
s token_continue strcmp not if 0 1 xreturn1 fi
s token_until strcmp not if 0 1 xreturn1 fi
s token_while strcmp not if 0 1 xreturn1 fi
s token_return0 strcmp not if 0 1 xreturn1 fi
s token_return1 strcmp not if 0 1 xreturn1 fi
1 1 xreturn1
}

def next_token
{ _var_address }
# A silly fixed-length buffer
'00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00
'00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00
'00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00
'00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00 '00

# comment = /#[^\n]*\n?/
# space = /\s/
# string_literal = /"([^"]|\\.)*"/
# token = /\S+/

def get_next_token
{
var c
var p

getchar c=
c 0 1 - == if
0 next_token 0 c[]=
0 xreturn0
fi
c 32 <= if
continue
fi
c 35 == if
{
getchar c=
c 10 == if break fi
c 0 1 - == if
0 next_token 0 c[]=
0 xreturn0
fi
continue
}
continue
fi

c 34 == if
next_token p=
{
c p 0 c[]=
p 1 + p=
getchar c=
c 34 == if
c p 0 c[]=
p 1 + p=
break
fi
c 92 == if
getchar c=
fi
c 0 1 - == if
96 exit # unterminated string
fi
continue
}
else
next_token p=
{
c p 0 c[]=
p 1 + p=
getchar c=
c 32 <= not if continue fi
}
fi
0 p 0 c[]=
0 xreturn0
}

def eof
{
next_token 0 c[] 0 == 0 xreturn1
}

def s token
{
next_token s strcmp if 0 1 xreturn1 fi
get_next_token
1 1 xreturn1
}

def syntax_error
{
19 exit
}

def x require
{
x not if syntax_error fi
1 xreturn0
}

###
### Symbol tables
###

var globals
var locals
var local_args_num

def symbol.sizeof
{
wsize 4 * 0 xreturn1
}

def sym symbol.name&
{
sym 1 xreturn1
}

def sym symbol.type&
{
sym wsize + 1 xreturn1
}

def sym symbol.value&
{
sym wsize 2 * + 1 xreturn1
}

def sym symbol.next&
{
sym wsize 3 * + 1 xreturn1
}

def sym symbol.type
{
sym symbol.type& @ 1 xreturn1
}

def type sym symbol.type=
{
type sym symbol.type& = 2 xreturn0
}

def sym symbol.value
{
sym symbol.value& @ 1 xreturn1
}

def value sym symbol.value=
{
value sym symbol.value& = 2 xreturn0
}

def table s symbol_found
{
var t
table t=
{
t @ not if
0 2 xreturn1
fi
t @ symbol.name& @ s strcmp not if
t @ 2 xreturn1
fi
t @ symbol.next& t=
continue
}
}

def table s symbol_find
{
var t
table t=
{
t @ not if
symbol.sizeof malloc
dup t =
dup symbol.name& s strdup swap =
dup symbol.type& 0 swap =
dup symbol.value& 0 swap =
dup symbol.next& 0 swap =
2 xreturn1
fi
t @ symbol.name& @ s strcmp not if
t @ 2 xreturn1
fi
t @ symbol.next& t=
continue
}
}

def table symbol_table_clear
{
# FIXME: leaks
0 table =
}


###
### Code generation
###

def a n generate_int
{
# little-endian
a n 255 bitand emit
a 1 + n 8 >> 255 bitand emit
a 2 + n 16 >> 255 bitand emit
a 3 + n 24 >> 255 bitand emit
2 xreturn0
}

def a n generate_call
{
a 232 emit # 0xe8 = call N
a 1 + n a - 5 - generate_int
a 5 + 2 xreturn1
}

def a n generate_jump
{
a 233 emit # 0xe9 = jmp N
a 1 + n a - 5 - generate_int
a 5 + 2 xreturn1
}

def a n generate_branch_false
{
a 88 emit # 0x58 = pop %eax
a 1 + 133 emit
a 2 + 192 emit # 0x85 0xc0 = test %eax,%eax
a 3 + 15 emit
a 4 + 132 emit # 0x0f 0x84 = je N
a 5 + n a - 9 - generate_int
a 9 + 2 xreturn1
}

def a n generate_branch_true # unused
{
a 88 emit # 0x58 = pop %eax
a 1 + 133 emit
a 2 + 192 emit # 0x85 0xc0 = test %eax,%eax
a 3 + 15 emit
a 4 + 133 emit # 0x0f 0x85 = jne N
a 5 + n a - 9 - generate_int
a 9 + 2 xreturn1
}

def a n generate_constant
{
a 104 emit # 0x68 = push N
a 1 + n generate_int
a 5 + 2 xreturn1
}

def a generate_proc_start
{
a 85 emit # 0x55 = push %ebp
a 1 + 137 emit
a 2 + 229 emit # 0x89 0xe5 = mov %esp,%ebp
a 3 + 1 xreturn1
}

def a generate_proc_end
{
a 201 emit # 0xc9 = leave
a 1 + 195 emit # 0xc3 = ret
a 2 + 1 xreturn1
}

def a n generate_arg_load
{
a 139 emit
a 1 + 133 emit # 0x8b 0x85 = mov N(%ebp),%eax
a 2 + 2 local_args_num + n - 4 * generate_int
a 6 + 80 emit # 0x50 = push %eax
a 7 + 2 xreturn1
}

def a n generate_stack_frame
{
n not if
a 2 xreturn1
fi
a 129 emit
a 1 + 236 emit # 0x81 0xec = sub $N,%esp
a 2 + n 4 * generate_int
a 6 + 2 xreturn1
}

def a n generate_var_load
{
a 139 emit
a 1 + 133 emit # 0x8b 0x85 = mov N(%ebp),%eax
a 2 + 0 n - 4 * generate_int
a 6 + 80 emit # 0x50 = push %eax
a 7 + 2 xreturn1
}

def a n generate_var_store
{
a 88 emit # 0x58 = pop %eax
a 1 + 137 emit
a 2 + 133 emit # 0x89 0x85 = mov %eax,N(%ebp)
a 3 + 0 n - 4 * generate_int
a 7 + 2 xreturn1
}

def a n generate_var_addr
{
a 141 emit
a 1 + 133 emit # 0x8d 0x85 = lea N(%ebp),%eax
a 2 + 0 n - 4 * generate_int
a 6 + 80 emit # 0x50 = push %eax
a 7 + 2 xreturn1
}

def a generate_global
{
a 0 generate_int
a 4 + 1 xreturn1
}

def a n generate_global_load
{
a 232 emit # 0xe8 = call N
a 1 + 0 generate_int
a 5 + 88 emit # 0x58 = pop %eax
a 6 + 139 emit
a 7 + 128 emit # 0x8b 0x80 = mov N(%eax),%eax
a 8 + n a - 5 - generate_int
a 12 + 80 emit # 0x50 = push %eax
a 13 + 2 xreturn1
}

def a n generate_global_store
{
a 232 emit # 0xe8 = call N
a 1 + 0 generate_int
a 5 + 88 emit # 0x58 = pop %eax
a 6 + 91 emit # 0x5b = pop %ebx
a 7 + 137 emit
a 8 + 152 emit # 0x89 0x98 = mov %ebx,N(%eax)
a 9 + n a - 5 - generate_int
a 13 + 2 xreturn1
}

def a n generate_global_addr
{
a 232 emit # 0xe8 = call N
a 1 + 0 generate_int
a 5 + 88 emit # 0x58 = pop %eax
a 6 + 141 emit
a 7 + 128 emit # 0x8d 0x80 = lea N(%eax),%eax
a 8 + n a - 5 - generate_int
a 12 + 80 emit # 0x50 = push %eax
a 13 + 2 xreturn1
}

def a generate_skip_jump_if_false
{
a 88 emit # 0x58 = pop %eax
a 1 + 133 emit
a 2 + 192 emit # 0x85 0xc0 = test %eax,%eax
a 3 + 116 emit
a 4 + 5 emit # 0x74 0x05 = je +5
a 5 + 1 xreturn1
}

def a generate_skip_jump_if_true
{
a 88 emit # 0x58 = pop %eax
a 1 + 133 emit
a 2 + 192 emit # 0x85 0xc0 = test %eax,%eax
a 3 + 117 emit
a 4 + 5 emit # 0x75 0x05 = jne +5
a 5 + 1 xreturn1
}

def a n generate_return0
{
a 201 emit # 0xc9 = leave
a 1 + 91 emit # 0x5b = pop %ebx
a 2 + 129 emit
a 3 + 196 emit # 0x81 0xc4 = add $N,%esp
a 4 + n 4 * generate_int
a 8 + 83 emit # 0x53 = push %ebx
a 9 + 195 emit # 0xc3 = ret
a 10 + 2 xreturn1
}

def a n generate_return1
{
a 88 emit # 0x58 = pop %eax
a 1 + 201 emit # 0xc9 = leave
a 2 + 91 emit # 0x5b = pop %ebx
a 3 + 129 emit
a 4 + 196 emit # 0x81 0xc4 = add $N,%esp
a 5 + n 4 * generate_int
a 9 + 80 emit # 0x50 = push %eax
a 10 + 83 emit # 0x53 = push %ebx
a 11 + 195 emit # 0xc3 = ret
a 12 + 2 xreturn1
}

###
### Parser
###

def unimplemented
{
77 exit
}

def end label_define
{
end
{
dup 0 1 - == if 1 xreturn0 fi
dup fetch_int
swap pc generate_jump drop
continue
}
}

def compile_number
{
var n
var p
var c
var neg

next_token p=
p 0 c[] 45 == if
1 neg=
p 1 + p=
else
0 neg=
fi

p 0 c[] c=
c 48 < if
0 0 xreturn1
fi
c 58 < not if
0 0 xreturn1
fi
c 48 - n=
{
p 1 + p=
p 0 c[] c=
c not if
break
fi
c 48 < if
0 0 xreturn1
fi
c 58 < not if
0 0 xreturn1
fi
n 10 * c + 48 - n=
continue
}

neg if
0 n - n=
fi
pc n generate_constant pc=
get_next_token
1 0 xreturn1
}

def s compile_local_symbol
{
var sym
var c

locals& s symbol_found sym=
sym if
sym symbol.type
dup 0 == if
0 0 xreturn1
fi
dup 1 == if
pc sym symbol.value generate_arg_load pc=
1 0 xreturn1
fi
dup 2 == if
pc sym symbol.value generate_var_load pc=
1 0 xreturn1
fi
97 exit # internal error
fi

s s strlen 1 - c[] c=
c 61 != if # '='
c 38 != if # '&'
0 1 xreturn1
fi
fi

# Chop and restore the last char - what a hack
0 s s strlen 1 - c[]=
locals& s symbol_found sym=
c s s strlen c[]=

sym if
sym symbol.type
dup 1 == if
101 exit # arg_addr or arg_store
fi
dup 2 == if
c 61 == if
pc sym symbol.value generate_var_store pc=
else
pc sym symbol.value generate_var_addr pc=
fi
1 0 xreturn1
fi
fi

0 1 xreturn1
}

# FIXME: The following function should be merged with the previous one.

def s compile_global_symbol
{
var sym
var c

globals& s symbol_found sym=
sym if
sym symbol.type 1 == if
pc
pc 0 generate_call pc=
dup sym symbol.value store_int
sym symbol.value=
1 1 xreturn1
fi
sym symbol.type 2 == if
pc sym symbol.value generate_call pc=
1 1 xreturn1
fi
sym symbol.type 3 == if
pc sym symbol.value generate_global_load pc=
1 1 xreturn1
fi
sym symbol.type 4 == if
pc sym symbol.value generate_global_addr pc=
1 1 xreturn1
fi
unimplemented
fi

s s strlen 1 - c[] c=
c 61 != if # '='
c 38 != if # '&'
globals& s symbol_find sym=
0 1 - sym symbol.value=
1 sym symbol.type=
s compile_global_symbol
1 1 xreturn1
fi
fi

# Chop and restore the last char - what a hack
0 s s strlen 1 - c[]=
globals& s symbol_found sym=
c s s strlen c[]=

sym if
sym symbol.type
dup 3 == if
c 61 == if
pc sym symbol.value generate_global_store pc=
else
pc sym symbol.value generate_global_addr pc=
fi
1 1 xreturn1
fi
unimplemented
fi

globals& s symbol_find sym=
0 1 - sym symbol.value=
1 sym symbol.type=
s compile_global_symbol
1 1 xreturn1
}

def compile_word
{
next_token is_symbol not if
0 0 xreturn1
fi
next_token compile_local_symbol if
get_next_token
1 0 xreturn1
fi
next_token compile_global_symbol if
get_next_token
1 0 xreturn1
fi
0 0 xreturn1
}

def compile_loop
{
var end

token_ob token not if
0 0 xreturn1
fi
0 1 - end=
pc end& compile_body
end label_define
token_cb token require
1 0 xreturn1
}

def begin end compile_if
{
var pos1
var pos2

token_if token not if
0 2 xreturn1
fi
pc pos1=
pos1 0 generate_branch_false pc=
begin end compile_body require
token_fi token if
pos1 pc generate_branch_false
1 2 xreturn1
fi
token_else token require
pc pos2=
pos2 0 generate_jump pc=
pos1 pc generate_branch_false
begin end compile_body require
token_fi token require
pos2 pc generate_jump
1 2 xreturn1
}

def end compile_break
{
token_break token not if
0 1 xreturn1
fi
pc 0 generate_jump
pc end @ store_int
pc end =
pc=
1 1 xreturn1
}

def begin compile_continue
{
token_continue token not if
0 1 xreturn1
fi
pc begin generate_jump pc=
1 1 xreturn1
}

def end compile_until
{
token_until token not if
0 1 xreturn1
fi
pc generate_skip_jump_if_false pc=
pc 0 generate_jump
pc end @ store_int
pc end =
pc=
1 1 xreturn1
}

def end compile_while
{
token_while token not if
0 1 xreturn1
fi
pc generate_skip_jump_if_true pc=
pc 0 generate_jump
pc end @ store_int
pc end =
pc=
1 1 xreturn1
}

def begin end compile_jump
{
{
end compile_break if break fi
begin compile_continue if break fi
end compile_until if break fi
end compile_while if break fi
0 2 xreturn1
}
1 2 xreturn1
}

def compile_return
{
token_return0 token if
pc local_args_num generate_return0 pc=
1 0 xreturn1
fi
token_return1 token if
pc local_args_num generate_return1 pc=
1 0 xreturn1
fi
0 0 xreturn1
}

def begin end compile_body
{
compile_number if continue fi
compile_word if continue fi
compile_loop if continue fi
begin end compile_if if continue fi
begin end compile_jump if continue fi
compile_return if continue fi
1 2 xreturn1
}

def compile_vars
{
var count
var sym

0 count=
{
token_var token not if break fi
next_token is_symbol require
locals& next_token symbol_find sym=
sym symbol.type if
54 exit # variable redefined
fi
2 sym symbol.type=
count 1 + count=
count sym symbol.value=
get_next_token
continue
}
pc count generate_stack_frame pc=
1 0 xreturn1
}

def sym compile_define_proc_1
{
sym
dup symbol.value
{
dup 0 1 - == if
drop dup 2 swap symbol.type=
pc swap symbol.value=
1 xreturn0
fi
dup fetch_int swap
pc generate_call drop
continue
}
}

def s compile_define_proc
{
globals& s symbol_find
dup symbol.type 0 == if
dup 0 1 - swap symbol.value=
compile_define_proc_1
1 xreturn0
fi
dup symbol.type 1 == if
compile_define_proc_1
1 xreturn0
fi
dup symbol.type 2 == if
32 exit # symbol redefined
fi
dup symbol.type 3 == if
32 exit # symbol redefined
fi
unimplemented
}

def s compile_define_var
{
globals& s symbol_find
dup symbol.type 0 == if
dup 3 swap symbol.type=
pc swap symbol.value=
pc generate_global pc=
1 xreturn0
fi
33 exit # symbol redefined
}

# FIXME: combine these functions?

def s compile_define_string
{
globals& s symbol_find
dup symbol.type 0 == if
dup 4 swap symbol.type=
pc swap symbol.value=
1 xreturn0
fi
33 exit # symbol redefined
}

def compile_args_name
{
var count
var sym

0 count=
next_token is_symbol require
{
next_token is_symbol not if break fi
locals& next_token symbol_find sym=
sym symbol.type if
34 exit # local symbol reused
fi
1 sym symbol.type=
count 1 + count=
count sym symbol.value=
get_next_token
continue
}

# The last one was the procedure name, not an argument!
0 sym symbol.type=
sym symbol.name& @ compile_define_proc

count 1 - local_args_num=
1 0 xreturn1
}

def compile_procedure
{
var end

token_def token not if 0 0 xreturn1 fi
compile_args_name require
pc generate_proc_start pc=
token_ob token require
compile_vars require
0 1 - end=
pc end& compile_body require
end label_define
pc generate_proc_end pc=
token_cb token require
locals& symbol_table_clear
1 0 xreturn1
}

def compile_string_literal
{
var p
next_token p=
p 0 c[] 34 != if
0 0 xreturn1
fi
{
p 1 + p=
p 0 c[]
dup require
dup 34 == if
p 1 c[] 0 == require
pc 0 emit
pc 1 + pc=
break
fi
dup 92 == if
p 1 c[] require
pc p 1 c[] emit
pc 1 + pc=
p 1 + p=
fi
pc swap emit
pc 1 + pc=
continue
}
get_next_token
1 0 xreturn1
}

def compile_global
{
token_var token if
next_token is_symbol require
next_token compile_define_var
get_next_token
1 0 xreturn1
fi
token_string token if
next_token is_symbol require
next_token compile_define_string
get_next_token
compile_string_literal require
1 0 xreturn1
fi
0 0 xreturn1
}

def character convert_hex
{
character
dup 48 < if 0 1 - 1 xreturn1 fi
dup 58 < if 48 - 1 xreturn1 fi
dup 97 < if 0 1 - 1 xreturn1 fi
dup 103 < if 87 - 1 xreturn1 fi
0 1 - 1 xreturn1
}

def compile_hexbyte
{
var n

next_token
dup 0 c[] 39 != if 0 0 xreturn1 fi
dup 1 c[] convert_hex dup 0 1 - == if 0 0 xreturn1 fi
n=
dup 2 c[] convert_hex dup 0 1 - == if 0 0 xreturn1 fi
n 4 << + n=
3 c[] 0 != if 0 0 xreturn1 fi
pc n emit pc 1 + pc=
get_next_token
1 0 xreturn1
}

def compile_hexitem
{
compile_hexbyte if
1 0 xreturn1
fi
token__def token if
next_token is_symbol require
next_token compile_define_proc
get_next_token
1 0 xreturn1
fi
0 0 xreturn1
}

def compile_program
{
compile_hexitem if continue fi
compile_global if continue fi
compile_procedure if continue fi
1 0 xreturn1
}

# program = (hexitem | global | procedure)*
# hexitem = hexbyte | "_def" symbol
# hexbyte = /'[0-9a-f][0-9a-f]/
# global = "var" symbol | "string" symbol string_literal
# string_literal = /"([^"]|\\.)*"/
# procedure = "def" args name "{" vars body "}"
# args = symbol*
# name = symbol
# vars = "var" symbol
# body = (number | word | loop | jump | if | return0 | return1)*
# number = /-?[0-9]+/
# word = symbol
# loop = "{" body "}"
# jump = "break" | "continue" | "until" | "while"
# if = "if" body "fi" | "if" body "else" body "fi"
# symbol = /.+/ except ...

def main
{
get_next_token
compile_program require
eof require
dump
0 0 xreturn1
}
Something went wrong with that request. Please try again.