Script Engine code generator.
710{
715
723
724
725
726
727 OperatorSymbol =
ToSymbol(Operator, Error);
728
729#ifdef _SCRIPT_ENGINE_CODEGEN_DBG_EN
730
731
732
736
737 printf(
"Semantic Stack:\n");
740
744#endif
745
747 {
748 if (!strcmp(Operator->
Value,
"@INCLUDE"))
749 {
750 char * IncludeFilePath;
752 char * IncludeFileBuffer;
754
755 Temp =
Pop(MatchedStack);
756 IncludeFilePath = Temp->
Value;
757
759
761 {
763 break;
764 }
765
767 {
769 break;
770 }
771
773 {
776 }
777 else
778 {
780
782 {
783 if (!strcmp(Node->FilePath, FullPath))
784 {
786 break;
787 }
788 }
789
790 if (!IncludedPath && PrevNode)
791 {
794 }
795 }
796
797 if (!IncludedPath)
798 {
800 }
801 }
802
803 else if (!strcmp(Operator->
Value,
"@START_OF_USER_DEFINED_FUNCTION"))
804 {
805 Op0 =
Pop(MatchedStack);
807
809 {
811 break;
812 }
813
814
815
816
822
823
824
825
826
829 JumpAddressSymbol->
Value = 0xffffffffffffffff;
832
835 {
837 }
841
848
849
850
851
857
860 TempSymbol->
Value = 0;
863
864
865
866
872
875 TempSymbol->
Value = 0;
878
881 TempSymbol->
Value = 0;
884
885
886
887
893
896 TempSymbol->
Value = 0xffffffffffffffff;
899
902 TempSymbol->
Value = 0;
905
908 TempSymbol->
Value = 0;
911 }
912 else if (!strcmp(Operator->
Value,
"@FUNCTION_PARAMETER"))
913 {
914 Op0 =
Pop(MatchedStack);
916
918 {
920 break;
921 }
922
925 }
926 else if (!strcmp(Operator->
Value,
"@END_OF_USER_DEFINED_FUNCTION"))
927 {
928 UINT64 CurrentPointer = CodeBuffer->
Pointer;
930
932 {
934 break;
935 }
936
937
938
939
941 {
942 Symbol = CodeBuffer->
Head + i;
944 {
947 }
948
950 {
951 UINT64 VariableCount = Symbol->
Value;
952 for (UINT64 j = 0; j < VariableCount; j++)
953 {
954 Symbol = CodeBuffer->
Head + i + j + 1;
956 {
959 }
960 }
961 i += VariableCount;
962 }
963 }
964
965
966
967
970
971
972
973
975 {
976 Symbol = CodeBuffer->
Head + i;
978 {
979 (CodeBuffer->
Head + i + 1)->
Value = CurrentPointer;
980 i++;
981 }
982 }
983
984
985
986
992
995 TempSymbol->
Value = 0;
998
1001 TempSymbol->
Value = 0;
1004
1005
1006
1007
1013
1016 TempSymbol->
Value = 0;
1019
1025
1028
1030 }
1031 else if (!strcmp(Operator->
Value,
"@RETURN_OF_USER_DEFINED_FUNCTION_WITHOUT_VALUE"))
1032 {
1034 {
1036 break;
1037 }
1039 {
1041 break;
1042 }
1043
1044
1045
1046
1052
1055 Symbol->
Value = 0xfffffffffffffff0;
1058 }
1059 else if (!strcmp(Operator->
Value,
"@RETURN_OF_USER_DEFINED_FUNCTION_WITH_VALUE"))
1060 {
1062 {
1064 break;
1065 }
1067 {
1069 break;
1070 }
1071
1072
1073
1074
1080
1081 Op0 =
Pop(MatchedStack);
1085
1091
1092
1093
1094
1100
1103 Symbol->
Value = 0xfffffffffffffff0;
1106
1108 {
1109 break;
1110 }
1111 }
1112 else if (!strcmp(Operator->
Value,
"@END_OF_CALLING_USER_DEFINED_FUNCTION_WITHOUT_RETURNING_VALUE") || !strcmp(Operator->
Value,
"@END_OF_CALLING_USER_DEFINED_FUNCTION_WITH_RETURNING_VALUE"))
1113 {
1116 int VariableNum = 0;
1118
1119 while (MatchedStack->
Pointer > 0)
1120 {
1121 FunctionToken =
Pop(MatchedStack);
1122
1124 {
1125 break;
1126 }
1127 else
1128 {
1129 VariableNum++;
1135
1136 Symbol =
ToSymbol(FunctionToken, Error);
1140 }
1141 }
1142
1144
1145 if (!Node)
1146 {
1148 break;
1149 }
1150
1152 {
1154 break;
1155 }
1156
1162
1168
1174
1180
1185
1190
1191 if (!strcmp(Operator->
Value,
"@END_OF_CALLING_USER_DEFINED_FUNCTION_WITH_RETURNING_VALUE"))
1192 {
1194 {
1196 break;
1197 }
1198
1199
1200
1201
1203 Push(MatchedStack, Temp);
1204
1210
1216
1217 TempSymbol =
ToSymbol(Temp, Error);
1220
1222 {
1223 break;
1224 }
1225 }
1226
1228 }
1229 else if (!strcmp(Operator->
Value,
"@MULTIPLE_ASSIGNMENT"))
1230 {
1231 int Op1Capacity = 8;
1232 int Op1Count = 0;
1237
1238 Op0 =
Pop(MatchedStack);
1240
1241 for (
int i = MatchedStack->
Pointer; i > 0; i--)
1242 {
1243 Op1 =
Top(MatchedStack);
1244
1245 if (Op1Count >= Op1Capacity)
1246 {
1247 Op1Capacity *= 2;
1249 }
1250 Op1Array[Op1Count++] = Op1;
1251
1253 {
1256 break;
1257 }
1259 {
1262
1264 free((
void *)Op1Symbol->
Value);
1269 }
1271 {
1274
1276 free((
void *)Op1Symbol->
Value);
1282 }
1284 {
1287
1292 }
1293 else
1294 {
1295 break;
1296 }
1297 }
1298
1300 Op1 = 0;
1301
1303 {
1305 break;
1306 }
1307
1308 if (MatchedStack->
Pointer > 0)
1309 {
1311 {
1313
1315 {
1317 break;
1318 }
1319
1320 for (int i = 0; i < Op1Count; i++)
1321 {
1322 Op1 = Op1Array[i];
1324 {
1326 }
1328 {
1330 }
1331 }
1332
1333 Op1 = 0;
1334 }
1335 }
1336
1337
1338
1339
1342 {
1343 break;
1344 }
1345
1346 free(Op1Array);
1347 }
1348 else if (!strcmp(Operator->
Value,
"@ARRAY_INDEX_READ") || !strcmp(Operator->
Value,
"@ARRAY_INDEX_WRITE"))
1349 {
1350 int ArrayCapacity = 8;
1351 int TokenCount = 0;
1355 int ElementOffset = 0;
1359
1361 {
1362 if (TokenCount >= ArrayCapacity)
1363 {
1364 TokenCount *= 2;
1366 }
1367
1368 Temp =
Top(MatchedStack);
1369 if (!strcmp(
Top(MatchedStack)->
Value,
"@ARRAY_DIM_NUMBER"))
1370 {
1372 TokenArray[TokenCount] =
Pop(MatchedStack);
1373 TokenCount++;
1374 }
1375 else
1376 {
1377 break;
1378 }
1379 }
1380
1381 IdToken =
Pop(MatchedStack);
1382 IdSymbol =
ToSymbol(IdToken, Error);
1383
1385 {
1387 break;
1388 }
1389
1390 for (int i = 0; i < TokenCount / 2; i++)
1391 {
1393 TokenArray[i] = TokenArray[TokenCount - i - 1];
1394 TokenArray[TokenCount - i - 1] = tmp;
1395 }
1396
1399 TempSymbol =
ToSymbol(Temp, Error);
1401 OffsetSymbol =
ToSymbol(OffsetToken, Error);
1402
1407
1412
1414
1415 for (int i = 0; i < TokenCount; i++)
1416 {
1417 if (!VariableType->
Base)
1418 {
1420 break;
1421 }
1422
1427
1432
1433 Symbol =
ToSymbol(TokenArray[i], Error);
1435
1437
1442
1446
1447 VariableType = VariableType->
Base;
1448 }
1449
1451 {
1452 break;
1453 }
1454
1459
1463
1464 if (!strcmp(Operator->
Value,
"@ARRAY_INDEX_READ"))
1465 {
1470
1473 }
1474 else if (!strcmp(Operator->
Value,
"@ARRAY_INDEX_WRITE"))
1475 {
1477 }
1478
1479 Push(MatchedStack, OffsetToken);
1480
1482 }
1483 else if (!strcmp(Operator->
Value,
"@MOV"))
1484 {
1486 Op0 =
Pop(MatchedStack);
1488
1489 Op1 =
Pop(MatchedStack);
1491 {
1493 free((
void *)Op1Symbol->
Value);
1496 }
1498 {
1500 free((
void *)Op1Symbol->
Value);
1503 }
1504 else
1505 {
1507 }
1508
1509 if (MatchedStack->
Pointer > 0)
1510 {
1512 {
1513 if (!strcmp(
Top(MatchedStack)->
Value,
"@DECLARE_POINTER_TYPE"))
1514 {
1517 PointerVariableType->
Size = 8;
1518 PointerVariableType->
Align = 8;
1521 }
1522
1524
1526 {
1528 break;
1529 }
1530
1531 if (PointerVariableType)
1532 {
1533 PointerVariableType->
Base = VariableType;
1534 VariableType = PointerVariableType;
1535 }
1536
1538 {
1540 }
1542 {
1544 }
1545 }
1546 }
1547
1550
1551
1552
1553
1557 {
1558 break;
1559 }
1560 }
1561 else if (!strcmp(Operator->
Value,
"@DECLARE_POINTER_TYPE"))
1562 {
1564 }
1565 else if (!strcmp(Operator->
Value,
"@ARRAY_DIM_NUMBER") || !strcmp(Operator->
Value,
"@ARRAY_LEFT_BRACKET") || !strcmp(Operator->
Value,
"@ARRAY_L_VALUE"))
1566 {
1568 }
1569 else if (!strcmp(Operator->
Value,
"@DEREFERENCE"))
1570 {
1572
1573 Op0 =
Pop(MatchedStack);
1574 Op1 =
Pop(MatchedStack);
1575
1577 {
1579 break;
1580 }
1581
1583 {
1587
1590
1592 TempSymbol =
ToSymbol(Temp, Error);
1593
1599 }
1601 {
1605
1608
1610 TempSymbol =
ToSymbol(Temp, Error);
1611
1617 }
1618
1620 {
1624
1627
1629 TempSymbol =
ToSymbol(Temp, Error);
1630
1636 }
1637 }
1638 else if (!strcmp(Operator->
Value,
"@ARRAY_DECLARITION"))
1639 {
1640 int TokenCapacity = 8;
1641 int TokenCount = 0;
1645 ;
1647 int Last_ARRAY_DIM_NUMBER_Idx = 0;
1648 int ArrayElementCount = 0;
1650 int BaseTypeSize = 0;
1651
1652 for (
int i = MatchedStack->
Pointer; i > 0; i--)
1653 {
1654 Temp =
Pop(MatchedStack);
1655 if (!strcmp(Temp->
Value,
"@ARRAY_L_VALUE"))
1656 {
1657 break;
1658 }
1659
1660 if (TokenCount >= TokenCapacity)
1661 {
1662 TokenCapacity *= 2;
1664 }
1665 TokenArray[TokenCount++] = Temp;
1666 }
1667
1668 for (SIZE_T i = 0; i < TokenCount / 2; i++)
1669 {
1671 TokenArray[i] = TokenArray[TokenCount - i - 1];
1672 TokenArray[TokenCount - i - 1] = tmp;
1673 }
1674
1675 IdToken =
Pop(MatchedStack);
1676
1677 if (MatchedStack->
Pointer > 0)
1678 {
1680 {
1682
1684 {
1686 break;
1687 }
1688 }
1689 }
1690
1691 if (!VariableType)
1692 {
1694 }
1695
1696 BaseTypeSize = VariableType->
Size;
1697
1698 for (int i = TokenCount - 1; i >= 0; i--)
1699 {
1700 if (!strcmp(TokenArray[i]->
Value,
"@ARRAY_DIM_NUMBER"))
1701 {
1702 Last_ARRAY_DIM_NUMBER_Idx = i;
1703 break;
1704 }
1705 }
1706
1707 for (int i = Last_ARRAY_DIM_NUMBER_Idx; i >= 0; i--)
1708 {
1709 if (!strcmp(TokenArray[i]->
Value,
"@ARRAY_DIM_NUMBER"))
1710 {
1713 VariableType2->
Size = VariableType->
Size * atoi(TokenArray[i - 1]->
Value);
1715 VariableType2->
Base = VariableType;
1717 VariableType = VariableType2;
1718 i--;
1719 }
1720 }
1721
1723 {
1727 }
1728
1729 for (int i = Last_ARRAY_DIM_NUMBER_Idx + 1; i < TokenCount; i++)
1730 {
1732 {
1733 if ((ArrayElementCount * BaseTypeSize) > VariableType->
Size)
1734 {
1736 break;
1737 }
1738
1743
1746 Symbol->
Value = BaseTypeSize;
1748
1751 Symbol->
Value = ArrayElementCount;
1753
1755 TempSymbol =
ToSymbol(Temp, Error);
1757
1761
1766
1769
1770 if (BaseTypeSize == 4)
1771 {
1773 }
1774 else if (BaseTypeSize == 8)
1775 {
1777 }
1778 else if (BaseTypeSize == 1)
1779 {
1781 }
1782
1784
1785 Symbol =
ToSymbol(TokenArray[i], Error);
1787
1789
1791
1793
1794 ArrayElementCount++;
1795 }
1796 }
1797
1799 {
1800 break;
1801 }
1802
1803 while ((ArrayElementCount * BaseTypeSize) < VariableType->
Size)
1804 {
1809
1812 Symbol->
Value = BaseTypeSize;
1814
1817 Symbol->
Value = ArrayElementCount;
1819
1821 TempSymbol =
ToSymbol(Temp, Error);
1823
1827
1832
1835
1836 if (BaseTypeSize == 4)
1837 {
1839 }
1840 else if (BaseTypeSize == 8)
1841 {
1843 }
1844 else if (BaseTypeSize == 1)
1845 {
1847 }
1848
1850
1855
1857
1859
1861
1862 ArrayElementCount++;
1863 }
1864 }
1866 {
1868 Op0 =
Pop(MatchedStack);
1870
1873 {
1874 break;
1875 }
1876 }
1878 {
1880 Op0 =
Pop(MatchedStack);
1883
1885 Push(MatchedStack, Temp);
1886 TempSymbol =
ToSymbol(Temp, Error);
1887
1890
1893 {
1894 break;
1895 }
1896 }
1898 {
1902 do
1903 {
1904 if (Op1)
1905 {
1907 }
1908 Op1 =
Pop(MatchedStack);
1910 {
1912
1916
1917 OperandCount++;
1919 {
1921 break;
1922 }
1923 }
1924
1926
1928 {
1929 break;
1930 }
1931 Op0 =
Pop(MatchedStack);
1934
1935 char * Format = Op0->
Value;
1936
1939 OperandCountSymbol->
Value = OperandCount;
1940
1943
1946 {
1948 break;
1949 }
1950
1951 unsigned int FirstArgPointer = CodeBuffer->
Pointer;
1952
1954 unsigned int ArgCount = TempStack->
Pointer;
1955 for (
int i = TempStack->
Pointer - 1; i >= 0; i--)
1956 {
1957 Symbol = TempStack->
Head + i;
1959 }
1961 (
unsigned long long)(FirstArgPointer *
sizeof(
SYMBOL)));
1963
1965 char * Str = Format;
1966 do
1967 {
1968
1969
1970
1971 if (*Str == '%')
1972 {
1973 CHAR Temp = *(Str + 1);
1974
1975 if (Temp == 'd' || Temp == 'i' || Temp == 'u' || Temp == 'o' ||
1976 Temp == 'x' || Temp == 'c' || Temp == 'p' || Temp == 's' ||
1977
1978 !strncmp(Str, "%ws", 3) || !strncmp(Str, "%ls", 3) ||
1979
1980 !strncmp(Str, "%ld", 3) || !strncmp(Str, "%li", 3) ||
1981 !strncmp(Str, "%lu", 3) || !strncmp(Str, "%lo", 3) ||
1982 !strncmp(Str, "%lx", 3) ||
1983
1984 !strncmp(Str, "%hd", 3) || !strncmp(Str, "%hi", 3) ||
1985 !strncmp(Str, "%hu", 3) || !strncmp(Str, "%ho", 3) ||
1986 !strncmp(Str, "%hx", 3) ||
1987
1988 !strncmp(Str, "%lld", 4) || !strncmp(Str, "%lli", 4) ||
1989 !strncmp(Str, "%llu", 4) || !strncmp(Str, "%llo", 4) ||
1990 !strncmp(Str, "%llx", 4)
1991
1992 )
1993 {
1994 if (i < ArgCount)
1995 {
1996 Symbol = FirstArg + i;
1997 }
1998 else
1999 {
2001 break;
2002 }
2003 Symbol->
Type &= 0xffffffff;
2004 Symbol->
Type |= (UINT64)(Str - Format - 1) << 32;
2005 i++;
2006 }
2007 }
2008 Str++;
2009 } while (*Str);
2010 if (i != ArgCount)
2011 {
2013 }
2014
2016 {
2017 break;
2018 }
2019 }
2021 {
2023 }
2025 {
2027
2029 Push(MatchedStack, Temp);
2030 TempSymbol =
ToSymbol(Temp, Error);
2031
2033
2035 {
2036 break;
2037 }
2038 }
2039 else if (!strcmp(Operator->
Value,
"@IGNORE_LVALUE"))
2040 {
2041 Op0 =
Pop(MatchedStack);
2042 }
2044 {
2046 Op0 =
Pop(MatchedStack);
2048
2049 Op1 =
Pop(MatchedStack);
2051
2055 {
2056 break;
2057 }
2058
2060 Push(MatchedStack, Temp);
2061 TempSymbol =
ToSymbol(Temp, Error);
2063
2064
2065
2066
2069 }
2071 {
2073 Op0 =
Pop(MatchedStack);
2075
2076 Op1 =
Pop(MatchedStack);
2078
2082 {
2083 break;
2084 }
2085
2086
2087
2090 }
2092 {
2094 Op0 =
Pop(MatchedStack);
2096
2097 Op1 =
Pop(MatchedStack);
2099
2100 Op2 =
Pop(MatchedStack);
2102
2106
2108 Push(MatchedStack, Temp);
2109 TempSymbol =
ToSymbol(Temp, Error);
2111
2113
2114
2115
2116
2120 {
2121 break;
2122 }
2123 }
2125 {
2127 Op0 =
Pop(MatchedStack);
2129
2130 Op1 =
Pop(MatchedStack);
2132
2133 Op2 =
Pop(MatchedStack);
2135
2140 {
2141 break;
2142 }
2143
2144
2145
2149 }
2151 {
2154
2156 {
2157 if (!strcmp(Operator->
Value,
"@ADD_ASSIGNMENT"))
2158 {
2160
2165
2170
2171 Op0 =
Pop(MatchedStack);
2174
2176 TempSymbol =
ToSymbol(Temp, Error);
2178
2183
2185
2186 Op1 =
Pop(MatchedStack);
2190
2193 {
2194 break;
2195 }
2196 }
2197 else if (!strcmp(Operator->
Value,
"@SUB_ASSIGNMENT"))
2198 {
2200
2205
2210
2211 Op0 =
Pop(MatchedStack);
2214
2216 TempSymbol =
ToSymbol(Temp, Error);
2218
2223
2225
2226 Op1 =
Pop(MatchedStack);
2230
2233 {
2234 break;
2235 }
2236 }
2237 else
2238 {
2240 Op0 =
Pop(MatchedStack);
2241 Op1 =
Pop(MatchedStack);
2242 }
2244 }
2245
2246 if (!Handled)
2247 {
2249 Op0 =
Pop(MatchedStack);
2251
2252 Op1 =
Pop(MatchedStack);
2254
2258
2259
2260
2261
2264 {
2265 break;
2266 }
2267 }
2268 }
2270 {
2274
2275 if (!strcmp(Operator->
Value,
"@ADD") || !strcmp(Operator->
Value,
"@SUB"))
2276 {
2278 {
2280 Op0 =
Pop(MatchedStack);
2281 Op1 =
Pop(MatchedStack);
2283 }
2285 {
2286 if (!strcmp(Operator->
Value,
"@SUB"))
2287 {
2289 Op0 =
Pop(MatchedStack);
2290 Op1 =
Pop(MatchedStack);
2291 }
2292 else
2293 {
2295
2296 Op0 =
Pop(MatchedStack);
2297 Op1 =
Pop(MatchedStack);
2300
2305
2310
2313
2315 TempSymbol =
ToSymbol(Temp, Error);
2317
2323
2324 Push(MatchedStack, Temp);
2325
2326
2327
2328
2332 {
2333 break;
2334 }
2335 }
2337 }
2339 {
2341
2342 Op0 =
Pop(MatchedStack);
2343 Op1 =
Pop(MatchedStack);
2346
2351
2356
2358
2360 TempSymbol =
ToSymbol(Temp, Error);
2362
2367
2368 Push(MatchedStack, Temp);
2369
2370
2371
2372
2376 {
2377 break;
2378 }
2379
2381 }
2382
2384 {
2386 Op0 =
Pop(MatchedStack);
2387 Op1 =
Pop(MatchedStack);
2389 }
2391 {
2392 if (!strcmp(Operator->
Value,
"@SUB"))
2393 {
2395 Op0 =
Pop(MatchedStack);
2396 Op1 =
Pop(MatchedStack);
2397 }
2398 else
2399 {
2401 int VariableBaseSize = 0;
2403 while (VariableType->
Base)
2404 {
2405 VariableType = VariableType->
Base;
2406 }
2407 VariableBaseSize = VariableType->
Size;
2408
2409 Op0 =
Pop(MatchedStack);
2410 Op1 =
Pop(MatchedStack);
2413
2418
2421 Symbol->
Value = VariableBaseSize;
2423
2426
2428 TempSymbol =
ToSymbol(Temp, Error);
2430
2436
2437 Push(MatchedStack, Temp);
2438
2439
2440
2441
2445 {
2446 break;
2447 }
2448 }
2450 }
2452 {
2454 int VariableBaseSize = 0;
2456 while (VariableType->
Base)
2457 {
2458 VariableType = VariableType->
Base;
2459 }
2460 VariableBaseSize = VariableType->
Size;
2461
2462 Op0 =
Pop(MatchedStack);
2463 Op1 =
Pop(MatchedStack);
2466
2471
2474 Symbol->
Value = VariableBaseSize;
2476
2478
2480 TempSymbol =
ToSymbol(Temp, Error);
2482
2487
2488 Push(MatchedStack, Temp);
2489
2490
2491
2492
2496 {
2497 break;
2498 }
2499
2501 }
2502 }
2503
2504 if (!Handled)
2505 {
2507 Op0 =
Pop(MatchedStack);
2509
2510 Op1 =
Pop(MatchedStack);
2512
2515 Push(MatchedStack, Temp);
2516 TempSymbol =
ToSymbol(Temp, Error);
2517
2521
2522
2523
2524
2528 {
2529 break;
2530 }
2531 }
2532 }
2534 {
2536 Op0 =
Top(MatchedStack);
2537
2539 {
2540 if (!strcmp(Operator->
Value,
"@INC"))
2541 {
2543
2548
2553
2554 Op0 =
Pop(MatchedStack);
2558
2561 {
2562 break;
2563 }
2565 }
2566
2567 else if (!strcmp(Operator->
Value,
"@DEC"))
2568 {
2570
2575
2580
2581 Op0 =
Pop(MatchedStack);
2585
2588 {
2589 break;
2590 }
2592 }
2593 }
2594
2595 if (!Handled)
2596 {
2598 Op0 =
Pop(MatchedStack);
2600
2602
2603
2604
2605
2608 {
2609 break;
2610 }
2611 }
2612 }
2613 else if (!strcmp(Operator->
Value,
"@VARGSTART"))
2614 {
2616 Push(MatchedStack, OperatorCopy);
2617 }
2618 else if (!strcmp(Operator->
Value,
"@START_OF_IF"))
2619 {
2621 Push(MatchedStack, OperatorCopy);
2622 }
2623 else if (!strcmp(Operator->
Value,
"@JZ"))
2624 {
2625
2627
2630 JumpAddressSymbol->
Value = 0xffffffffffffffff;
2633
2634 Op0 =
Pop(MatchedStack);
2636
2638
2639 char str[20] = {0};
2640 sprintf(str,
"%llu", (UINT64)CodeBuffer->
Pointer);
2642 Push(MatchedStack, CurrentAddressToken);
2643
2646 {
2647 break;
2648 }
2649 }
2650 else if (!strcmp(Operator->
Value,
"@JMP_TO_END_AND_JZCOMPLETED"))
2651 {
2652
2653
2654
2655 UINT64 CurrentPointer = CodeBuffer->
Pointer;
2659 JumpAddressSymbol->
Value = CurrentPointer + 2;
2661
2662
2663
2664
2670
2671
2672
2673
2676 JumpAddressSymbol->
Value = 0xffffffffffffffff;
2679
2680
2681
2682
2683 char str[20] = {0};
2684 sprintf(str, "%llu", CurrentPointer);
2686 Push(MatchedStack, CurrentAddressToken);
2687 }
2688 else if (!strcmp(Operator->
Value,
"@END_OF_IF"))
2689 {
2690 UINT64 CurrentPointer = CodeBuffer->
Pointer;
2693 while (strcmp(JumpSemanticAddressToken->
Value,
"@START_OF_IF"))
2694 {
2696 JumpAddressSymbol = (
PSYMBOL)(CodeBuffer->
Head + JumpSemanticAddress + 1);
2697 JumpAddressSymbol->
Value = CurrentPointer;
2698
2700 JumpSemanticAddressToken =
Pop(MatchedStack);
2701 }
2703 }
2704 else if (!strcmp(Operator->
Value,
"@START_OF_WHILE"))
2705 {
2706
2707
2708
2710 Push(MatchedStack, OperatorCopy);
2711
2712 char str[20] = {0};
2713 sprintf(str,
"%llu", (UINT64)CodeBuffer->
Pointer);
2715 Push(MatchedStack, CurrentAddressToken);
2716 }
2717 else if (!strcmp(Operator->
Value,
"@START_OF_WHILE_COMMANDS"))
2718 {
2719 UINT64 CurrentPointer = CodeBuffer->
Pointer;
2721
2723 OperatorSymbol =
ToSymbol(JzToken, Error);
2725
2728 JumpAddressSymbol->
Value = 0xffffffffffffffff;
2729
2730 Op0 =
Pop(MatchedStack);
2732
2734
2735 char str[20];
2736 sprintf(str, "%llu", CurrentPointer + 1);
2738 Push(MatchedStack, CurrentAddressToken);
2739 Push(MatchedStack, StartOfWhileToken);
2740
2743
2745
2747
2750 {
2751 break;
2752 }
2753 }
2754 else if (!strcmp(Operator->
Value,
"@END_OF_WHILE"))
2755 {
2756
2757
2758
2764
2765
2766
2767
2771
2775 {
2776 break;
2777 }
2778
2779
2780
2781
2782
2783 UINT64 CurrentPointer = CodeBuffer->
Pointer;
2784
2785 do
2786 {
2788 JumpAddressToken =
Pop(MatchedStack);
2789 if (!strcmp(JumpAddressToken->
Value,
"@START_OF_WHILE"))
2790 {
2791 break;
2792 }
2794 JumpAddressSymbol = (
PSYMBOL)(CodeBuffer->
Head + JumpAddress);
2795 JumpAddressSymbol->
Value = CurrentPointer;
2796
2799 }
2800 else if (!strcmp(Operator->
Value,
"@START_OF_DO_WHILE"))
2801 {
2802
2803
2804
2806 Push(MatchedStack, OperatorCopy);
2807
2808 char str[20];
2809 sprintf(str,
"%llu", (UINT64)CodeBuffer->
Pointer);
2811 Push(MatchedStack, CurrentAddressToken);
2812 }
2813 else if (!strcmp(Operator->
Value,
"@END_OF_DO_WHILE"))
2814 {
2815
2816
2817
2823
2824
2825
2826
2827 Op0 =
Pop(MatchedStack);
2829
2830
2831
2832
2835
2837
2840
2842
2844
2846 {
2847 break;
2848 }
2849
2850
2851
2852
2853
2854 UINT64 CurrentPointer = CodeBuffer->
Pointer;
2855
2856 do
2857 {
2859 JumpAddressToken =
Pop(MatchedStack);
2860 if (!strcmp(JumpAddressToken->
Value,
"@START_OF_DO_WHILE"))
2861 {
2862 break;
2863 }
2865
2866#ifdef _SCRIPT_ENGINE_LL1_DBG_EN
2867 printf(
"Jz Jump Address = %lld\n", JumpAddress);
2868#endif
2869 JumpAddressSymbol = (
PSYMBOL)(CodeBuffer->
Head + JumpAddress);
2870 JumpAddressSymbol->
Value = CurrentPointer;
2871
2874 }
2875 else if (!strcmp(Operator->
Value,
"@START_OF_FOR"))
2876 {
2877
2878
2879
2881 Push(MatchedStack, OperatorCopy);
2882
2883
2884
2885
2886 char str[20] = {0};
2887 sprintf(str,
"%llu", (UINT64)CodeBuffer->
Pointer);
2889 Push(MatchedStack, CurrentAddressToken);
2890 }
2891 else if (!strcmp(Operator->
Value,
"@FOR_INC_DEC"))
2892 {
2893
2894
2895
2896
2897
2898
2899
2905
2906
2907
2908
2911 JnzAddressSymbol->
Value = 0xffffffffffffffff;
2914
2915
2916
2917
2918 Op0 =
Pop(MatchedStack);
2920
2923 {
2924 break;
2925 }
2926
2927
2928
2929
2930
2931
2932
2938
2939
2940
2941
2944 JumpAddressSymbol->
Value = 0xffffffffffffffff;
2947
2948
2949
2950
2952
2953
2954
2955
2956 char str[20] = {0};
2957 sprintf(str,
"%llu", (UINT64)CodeBuffer->
Pointer);
2959 Push(MatchedStack, CurrentAddressToken);
2960
2961
2962
2963
2964 Push(MatchedStack, StartOfForAddressToken);
2965 }
2966 else if (!strcmp(Operator->
Value,
"@START_OF_FOR_COMMANDS"))
2967 {
2968
2969
2970
2971
2972
2973
2974
2980
2981
2982
2983
2986
2988
2993 {
2994 break;
2995 }
2996
2997
2998
2999
3000 UINT64 CurrentPointer = CodeBuffer->
Pointer;
3001 JumpAddressToken =
Pop(MatchedStack);
3003
3004 JumpAddressSymbol = (
PSYMBOL)(CodeBuffer->
Head + JumpAddress - 1);
3005 JumpAddressSymbol->
Value = CurrentPointer;
3006
3007
3008
3009
3010 char str[20] = {0};
3011 sprintf(str, "%llu", JumpAddress - 4);
3013 Push(MatchedStack, JzAddressToken);
3014
3015
3016
3017
3019 Push(MatchedStack, IncDecToken);
3020
3021
3022
3023
3024 Push(MatchedStack, JumpAddressToken);
3025 }
3026 else if (!strcmp(Operator->
Value,
"@END_OF_FOR"))
3027 {
3028
3029
3030
3031
3032
3033
3034
3040
3041
3042
3043
3046
3048
3052
3054 {
3055 break;
3056 }
3057
3058 JumpAddressToken =
Pop(MatchedStack);
3059
3060
3061
3062
3063
3064 UINT64 CurrentPointer = CodeBuffer->
Pointer;
3065
3066 do
3067 {
3069 JumpAddressToken =
Pop(MatchedStack);
3070 if (!strcmp(JumpAddressToken->
Value,
"@START_OF_FOR"))
3071 {
3072 break;
3073 }
3074 else
3075 {
3077
3078 JumpAddressSymbol = (
PSYMBOL)(CodeBuffer->
Head + JumpAddress);
3079 JumpAddressSymbol->
Value = CurrentPointer;
3080 }
3081
3084 }
3085 else if (!strcmp(Operator->
Value,
"@BREAK"))
3086 {
3087
3088
3089
3090
3093 do
3094 {
3095 TempToken =
Pop(MatchedStack);
3096
3097 if ((!strcmp(TempToken->
Value,
"@START_OF_FOR")) ||
3098 (!strcmp(TempToken->
Value,
"@START_OF_WHILE")) ||
3099 (!strcmp(TempToken->
Value,
"@START_OF_DO_WHILE")))
3100 {
3101
3102
3103
3104 Push(MatchedStack, TempToken);
3105
3106
3107
3108
3109
3110 UINT64 CurrentPointer = CodeBuffer->
Pointer + 1;
3111 char str[20];
3112 sprintf(str, "%llu", CurrentPointer);
3114 Push(MatchedStack, CurrentAddressToken);
3115
3116
3117
3118
3119
3120
3121
3122
3128
3129
3130
3131
3134 JumpAddressSymbol->
Value = 0xffffffffffffffff;
3137
3138
3139
3140
3141 do
3142 {
3143 TempToken =
Pop(TempStack);
3144 Push(MatchedStack, TempToken);
3145
3146 }
while (TempStack->
Pointer != 0);
3147 break;
3148 }
3149 else if (MatchedStack->
Pointer == 0)
3150 {
3153 break;
3154 }
3155 else
3156 {
3157 Push(TempStack, TempToken);
3158 }
3159
3162 }
3163 else if (!strcmp(Operator->
Value,
"@CONTINUE"))
3164 {
3165
3166
3167
3170 do
3171 {
3172 TempToken =
Pop(MatchedStack);
3173
3174 if (!strcmp(TempToken->
Value,
"@INC_DEC"))
3175 {
3176
3177
3178
3179 Push(MatchedStack, TempToken);
3180
3181
3182
3183
3189
3190
3191
3192
3193 TempToken =
Pop(TempStack);
3194 Push(MatchedStack, TempToken);
3195
3201
3202
3203
3204
3205 do
3206 {
3207 TempToken =
Pop(TempStack);
3208 Push(MatchedStack, TempToken);
3209
3210 }
while (TempStack->
Pointer != 0);
3211 break;
3212 }
3213 else if (MatchedStack->
Pointer == 0)
3214 {
3216 break;
3217 }
3218 else
3219 {
3220 Push(TempStack, TempToken);
3221 }
3222
3224
3226 }
3228 {
3230 Op0 =
Pop(MatchedStack);
3232
3234 Push(MatchedStack, Temp);
3235 TempSymbol =
ToSymbol(Temp, Error);
3236
3239
3242 {
3243 break;
3244 }
3245 }
3247 {
3249 Op0 =
Pop(MatchedStack);
3251
3252 Op1 =
Pop(MatchedStack);
3254
3258 {
3259 break;
3260 }
3261
3263 Push(MatchedStack, Temp);
3264 TempSymbol =
ToSymbol(Temp, Error);
3266
3267
3268
3269
3272 }
3274 {
3276 Op0 =
Pop(MatchedStack);
3278
3279 Op1 =
Pop(MatchedStack);
3281
3282 Op2 =
Pop(MatchedStack);
3284
3288
3290 Push(MatchedStack, Temp);
3291 TempSymbol =
ToSymbol(Temp, Error);
3293
3294
3295
3296
3301 {
3302 break;
3303 }
3304 }
3306 {
3308 Op0 =
Pop(MatchedStack);
3310
3312 Push(MatchedStack, Temp);
3313 TempSymbol =
ToSymbol(Temp, Error);
3314
3317
3320 {
3321 break;
3322 }
3323 }
3325 {
3327 Op0 =
Pop(MatchedStack);
3329
3330 Op1 =
Pop(MatchedStack);
3332
3336 {
3337 break;
3338 }
3339
3341 Push(MatchedStack, Temp);
3342 TempSymbol =
ToSymbol(Temp, Error);
3344
3345
3346
3347
3350 }
3351
3353 {
3355 Op0 =
Pop(MatchedStack);
3357
3358 Op1 =
Pop(MatchedStack);
3360
3361 Op2 =
Pop(MatchedStack);
3363
3367
3369 Push(MatchedStack, Temp);
3370 TempSymbol =
ToSymbol(Temp, Error);
3372
3373
3374
3375
3380 {
3381 break;
3382 }
3383 }
3384 else
3385 {
3387 }
3388 break;
3389 }
3390
3391#ifdef _SCRIPT_ENGINE_CODEGEN_DBG_EN
3392
3393
3394
3395 printf(
"Semantic Stack:\n");
3398
3399 printf(
"Code Buffer:\n");
3401 printf(
"------------------------------------------\n\n");
3402#endif
3403
3404 if (Op0)
3406
3407 if (Op1)
3409
3410 if (Op2)
3412
3414
3415 if (Op0Symbol)
3417
3418 if (Op1Symbol)
3420
3421 if (Op2Symbol)
3423
3424 if (TempSymbol)
3426
3427 return;
3428}
PINCLUDE_NODE IncludeHead
Definition globals.c:17
PUSER_DEFINED_FUNCTION_NODE UserDefinedFunctionHead
Definition globals.c:15
PUSER_DEFINED_FUNCTION_NODE CurrentUserDefinedFunction
Definition globals.c:16
RequestedActionOfThePacket Value(0x1) 00000000
NULL()
Definition test-case-generator.py:530
char IsType9Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OneOpFunc3.
Definition common.c:889
char IsType4Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is VarArgFunc1.
Definition common.c:789
PSCRIPT_ENGINE_TOKEN Top(PSCRIPT_ENGINE_TOKEN_LIST TokenList)
Returns last Token of a TokenList.
Definition common.c:523
char IsType2Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OneOpFunc2.
Definition common.c:729
char IsType6Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is TwoOpFunc1.
Definition common.c:829
char IsType1Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OneOpFunc1.
Definition common.c:709
PSCRIPT_ENGINE_TOKEN CopyToken(PSCRIPT_ENGINE_TOKEN Token)
Copies a PTOKEN.
Definition common.c:326
char IsType12Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OneOpFunc4.
Definition common.c:949
char IsType14Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ThreeOpFunc2.
Definition common.c:989
PSCRIPT_ENGINE_TOKEN TopIndexed(PSCRIPT_ENGINE_TOKEN_LIST TokenList, int Index)
Returns the token at a specific index from the top of the token list.
Definition common.c:543
PSCRIPT_ENGINE_TOKEN NewTemp(PSCRIPT_ENGINE_ERROR_TYPE Error)
Allocates a new temporary variable and returns it.
Definition common.c:655
char IsAssignmentOperator(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is assignment operator.
Definition common.c:1049
char IsType10Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is TwoOpFunc3.
Definition common.c:909
char IsType16Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ZeroOpFunc2.
Definition common.c:1029
PSCRIPT_ENGINE_TOKEN_LIST Push(PSCRIPT_ENGINE_TOKEN_LIST TokenList, PSCRIPT_ENGINE_TOKEN Token)
Adds Token to the last empty position of TokenList.
Definition common.c:441
char IsType15Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ThreeOpFunc4.
Definition common.c:1009
void RemoveTokenList(PSCRIPT_ENGINE_TOKEN_LIST TokenList)
Removes allocated memory of a SCRIPT_ENGINE_TOKEN_LIST.
Definition common.c:402
void SetType(unsigned long long *Val, unsigned char Type)
Set the Type object.
Definition common.c:1392
char IsTwoOperandOperator(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OperatorsTwoOperandList.
Definition common.c:749
void RemoveToken(PSCRIPT_ENGINE_TOKEN *Token)
Removes allocated memory of a token.
Definition common.c:116
PSCRIPT_ENGINE_TOKEN NewToken(SCRIPT_ENGINE_TOKEN_TYPE Type, char *Value)
Allocates a new token with given type and value.
Definition common.c:69
char IsType7Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is TwoOpFunc2.
Definition common.c:849
PSCRIPT_ENGINE_TOKEN Pop(PSCRIPT_ENGINE_TOKEN_LIST TokenList)
Removes last Token of a TokenList and returns it.
Definition common.c:502
PSCRIPT_ENGINE_TOKEN_LIST NewTokenList(void)
Allocates a new SCRIPT_ENGINE_TOKEN_LIST.
Definition common.c:364
char IsType13Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is TwoOpFunc4.
Definition common.c:969
VOID FreeTemp(PSCRIPT_ENGINE_TOKEN Temp)
Frees the memory allocated by Temp.
Definition common.c:693
unsigned long long DecimalToInt(char *str)
Converts an decimal string to a integer.
Definition common.c:1404
void PrintToken(PSCRIPT_ENGINE_TOKEN Token)
Prints token.
Definition common.c:132
char IsOneOperandOperator(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is OperatorsOneOperandList.
Definition common.c:769
char IsType11Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ThreeOpFunc3.
Definition common.c:929
char IsType8Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ThreeOpFunc1.
Definition common.c:869
void PrintTokenList(PSCRIPT_ENGINE_TOKEN_LIST TokenList)
Prints each Token inside a TokenList.
Definition common.c:423
char IsType5Func(PSCRIPT_ENGINE_TOKEN Operator)
Checks whether this Token type is ZeroOpFunc1.
Definition common.c:809
PUSER_DEFINED_FUNCTION_NODE GetUserDefinedFunctionNode(PSCRIPT_ENGINE_TOKEN Token)
Definition script-engine.c:4549
VOID SetLocalIdentifierVariableType(PSCRIPT_ENGINE_TOKEN Token, VARIABLE_TYPE *VariableType)
Definition script-engine.c:4473
int NewFunctionParameterIdentifier(PSCRIPT_ENGINE_TOKEN Token)
Definition script-engine.c:4514
PSYMBOL_BUFFER PushSymbol(PSYMBOL_BUFFER SymbolBuffer, const PSYMBOL Symbol)
Gets a symbol and push it into the symbol buffer.
Definition script-engine.c:3966
void PrintSymbolBuffer(const PVOID SymbolBuffer)
Prints a symbol buffer.
Definition script-engine.c:4083
PSYMBOL NewSymbol(void)
Allocates a new SYMBOL and returns the reference to it.
Definition script-engine.c:3666
unsigned long long NewLocalIdentifier(PSCRIPT_ENGINE_TOKEN Token, unsigned int VariableSize)
Allocates a new local variable and returns the integer assigned to it.
Definition script-engine.c:4457
VOID SetGlobalIdentifierVariableType(PSCRIPT_ENGINE_TOKEN Token, VARIABLE_TYPE *VariableType)
Definition script-engine.c:4417
PSYMBOL ToSymbol(PSCRIPT_ENGINE_TOKEN Token, PSCRIPT_ENGINE_ERROR_TYPE Error)
Converts Token to Symbol and returns the reference to it.
Definition script-engine.c:3815
void RemoveSymbolBuffer(PVOID SymbolBuffer)
Frees the memory allocated by SymbolBuffer.
Definition script-engine.c:3949
int NewGlobalIdentifier(PSCRIPT_ENGINE_TOKEN Token)
Allocates a new global variable and returns the integer assigned to it.
Definition script-engine.c:4404
void RemoveSymbol(PSYMBOL *Symbol)
Frees the memory allocate by this Symbol.
Definition script-engine.c:3764
PSYMBOL_BUFFER NewSymbolBuffer(void)
allocates a new Symbol Buffer and returns the reference to it
Definition script-engine.c:3923
BOOLEAN FileExists(const char *Path)
Checks whether a file exists at the given path.
Definition script_include.c:152
VOID ResolveIncludePath(const char *IncludeFilePath, char *OutPath)
Resolves an include file path to a full absolute path.
Definition script_include.c:121
char * InsertStrNew(char *Str, int InputIdx, const char *Buf)
Inserts a string into another string at a given index.
Definition script_include.c:73
BOOLEAN ParseIncludeFile(char *IncludeFile, char **Buffer)
Reads and parses an include file into a buffer.
Definition script_include.c:173
#define MAX_PATH_LEN
Definition script_include.h:15
struct _INCLUDE_NODE * NextNode
Definition common.h:296
char * FilePath
Definition common.h:295
unsigned int Pointer
Definition common.h:86
SCRIPT_ENGINE_TOKEN_TYPE Type
Definition common.h:72
VARIABLE_TYPE * VariableType
Definition common.h:76
long long unsigned Address
Definition common.h:279
long long unsigned VariableType
Definition common.h:280
struct _USER_DEFINED_FUNCTION_NODE * NextNode
Definition common.h:287
long long unsigned ParameterNumber
Definition common.h:281
struct _VARIABLE_TYPE * Base
Definition type.h:45
int Size
Definition type.h:42
int Align
Definition type.h:43
int ArrayLen
Definition type.h:46
VARIABLE_TYPE_KIND Kind
Definition type.h:41
BOOLEAN IsUnsigned
Definition type.h:44
unsigned int Pointer
Definition ScriptEngineCommonDefinitions.h:24
PSYMBOL Head
Definition ScriptEngineCommonDefinitions.h:23
Definition ScriptEngineCommonDefinitions.h:6
long long unsigned Value
Definition ScriptEngineCommonDefinitions.h:9
long long unsigned Type
Definition ScriptEngineCommonDefinitions.h:7
VARIABLE_TYPE * VARIABLE_TYPE_LONG
Definition type.c:23
VARIABLE_TYPE * HandleType(PSCRIPT_ENGINE_TOKEN_LIST PtokenStack)
Return a variable type based on the token stack.
Definition type.c:41
VARIABLE_TYPE * GetCommonVariableType(VARIABLE_TYPE *Ty1, VARIABLE_TYPE *Ty2)
Returns the common variable type between two types.
Definition type.c:188
@ TY_PTR
Definition type.h:31
@ TY_VOID
Definition type.h:21
@ TY_ARRAY
Definition type.h:33
@ TY_UNKNOWN
Definition type.h:20
@ SCRIPT_ENGINE_ERROR_UNDEFINED_FUNCTION
Definition type.h:76
@ SCRIPT_ENGINE_ERROR_NON_VOID_FUNCTION_NOT_RETURNING_VALUE
Definition type.h:79
@ SCRIPT_ENGINE_ERROR_UNHANDLED_SEMANTIC_RULE
Definition type.h:74
@ SCRIPT_ENGINE_ERROR_UNDEFINED_VARIABLE_TYPE
Definition type.h:77
@ SCRIPT_ENGINE_ERROR_VOID_FUNCTION_RETURNING_VALUE
Definition type.h:78
@ SCRIPT_ENGINE_ERROR_SYNTAX
Definition type.h:71
@ SCRIPT_ENGINE_ERROR_FREE
Definition type.h:70
struct _VARIABLE_TYPE VARIABLE_TYPE