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
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "common/debug.h"
#include "common/stack.h"
#include "sci/sci.h"
#include "sci/debug.h" // for g_debug_weak_validations
#include "sci/resource.h"
#include "sci/engine/state.h"
#include "sci/engine/intmap.h"
#include "sci/engine/kernel.h"
#include "sci/engine/kernel_types.h"
#include "sci/engine/seg_manager.h"
#include "sci/engine/gc.h"
#include "sci/sfx/misc.h" // for sfx_reset_player
namespace Sci {
reg_t NULL_REG = {0, 0};
//#define VM_DEBUG_SEND
#undef STRICT_SEND // Disallows variable sends with more than one parameter
#undef STRICT_READ // Disallows reading from out-of-bounds parameters and locals
int script_abort_flag = 0; // Set to 1 to abort execution. Set to 2 to force a replay afterwards // FIXME: Avoid non-const global vars
int script_step_counter = 0; // Counts the number of steps executed // FIXME: Avoid non-const global vars
int script_gc_interval = GC_INTERVAL; // Number of steps in between gcs // FIXME: Avoid non-const global vars
static bool breakpointFlag = false; // FIXME: Avoid non-const global vars
static reg_t _dummy_register; // FIXME: Avoid non-const global vars
// validation functionality
#ifndef DISABLE_VALIDATIONS
static reg_t &validate_property(Object *obj, int index) {
if (!obj) {
debugC(2, kDebugLevelVM, "[VM] Sending to disposed object!\n");
_dummy_register = NULL_REG;
return _dummy_register;
}
if (index < 0 || (uint)index >= obj->_variables.size()) {
debugC(2, kDebugLevelVM, "[VM] Invalid property #%d (out of [0..%d]) requested!\n",
index, obj->_variables.size());
_dummy_register = NULL_REG;
return _dummy_register;
}
return obj->_variables[index];
}
static StackPtr validate_stack_addr(EngineState *s, StackPtr sp) {
if (sp >= s->stack_base && sp < s->stack_top)
return sp;
error("[VM] Stack index %d out of valid range [%d..%d]\n",
(int)(sp - s->stack_base), 0, (int)(s->stack_top - s->stack_base - 1));
return 0;
}
static int validate_arithmetic(reg_t reg) {
if (reg.segment) {
if (g_debug_weak_validations)
warning("[VM] Attempt to read arithmetic value from non-zero segment [%04x]\n", reg.segment);
else
error("[VM] Attempt to read arithmetic value from non-zero segment [%04x]\n", reg.segment);
return 0;
}
return reg.offset;
}
static int signed_validate_arithmetic(reg_t reg) {
if (reg.segment) {
if (g_debug_weak_validations)
warning("[VM] Attempt to read arithmetic value from non-zero segment [%04x]\n", reg.segment);
else
error("[VM] Attempt to read arithmetic value from non-zero segment [%04x]\n", reg.segment);
return 0;
}
if (reg.offset&0x8000)
return (signed)(reg.offset) - 65536;
else
return reg.offset;
}
static int validate_variable(reg_t *r, reg_t *stack_base, int type, int max, int index, int line) {
const char *names[4] = {"global", "local", "temp", "param"};
if (index < 0 || index >= max) {
char txt[200];
char tmp[40];
sprintf(txt, "[VM] validate_variable(): Attempt to use invalid %s variable %04x ", names[type], index);
if (max == 0)
strcat(txt, "(variable type invalid)");
else {
sprintf(tmp, "(out of range [%d..%d])", 0, max - 1);
strcat(txt, tmp);
}
if (g_debug_weak_validations)
warning(txt);
else
error(txt);
#ifdef STRICT_READ
return 1;
#else // !STRICT_READ
if (type == VAR_PARAM || type == VAR_TEMP) {
int total_offset = r - stack_base;
if (total_offset < 0 || total_offset >= VM_STACK_SIZE) {
sciprintf("[VM] Access would be outside even of the stack (%d); access denied\n", total_offset);
return 1;
} else {
sciprintf("[VM] Access within stack boundaries; access granted.\n");
return 0;
}
}
#endif
}
return 0;
}
static reg_t validate_read_var(reg_t *r, reg_t *stack_base, int type, int max, int index, int line, reg_t default_value) {
if (!validate_variable(r, stack_base, type, max, index, line))
return r[index];
else
return default_value;
}
static void validate_write_var(reg_t *r, reg_t *stack_base, int type, int max, int index, int line, reg_t value) {
if (!validate_variable(r, stack_base, type, max, index, line))
r[index] = value;
}
# define ASSERT_ARITHMETIC(v) validate_arithmetic(v)
#else
// Non-validating alternatives
# define validate_stack_addr(s, sp) sp
# define validate_arithmetic(r) ((r).offset)
# define signed_validate_arithmetic(r) ((int) ((r).offset) & 0x8000 ? (signed) ((r).offset) - 65536 : ((r).offset))
# define validate_variable(r, sb, t, m, i, l)
# define validate_read_var(r, sb, t, m, i, l) ((r)[i])
# define validate_write_var(r, sb, t, m, i, l, v) ((r)[i] = (v))
# define validate_property(o, p) ((o)->_variables[p])
# define ASSERT_ARITHMETIC(v) (v).offset
#endif
#define READ_VAR(type, index, def) validate_read_var(variables[type], s->stack_base, type, variables_max[type], index, __LINE__, def)
#define WRITE_VAR(type, index, value) validate_write_var(variables[type], s->stack_base, type, variables_max[type], index, __LINE__, value)
#define WRITE_VAR16(type, index, value) WRITE_VAR(type, index, make_reg(0, value));
#define ACC_ARITHMETIC_L(op) make_reg(0, (op validate_arithmetic(s->r_acc)))
#define ACC_AUX_LOAD() aux_acc = signed_validate_arithmetic(s->r_acc)
#define ACC_AUX_STORE() s->r_acc = make_reg(0, aux_acc)
#define OBJ_PROPERTY(o, p) (validate_property(o, p))
int script_error(EngineState *s, const char *file, int line, const char *reason) {
error("Script error in file %s, line %d: %s\n", file, line, reason);
return 0;
}
#define CORE_ERROR(area, msg) script_error(s, "[" area "] " __FILE__, __LINE__, msg)
reg_t get_class_address(EngineState *s, int classnr, int lock, reg_t caller) {
if (NULL == s) {
warning("vm.c: get_class_address(): NULL passed for \"s\"");
return NULL_REG;
}
if (classnr < 0 || (int)s->_classtable.size() <= classnr || s->_classtable[classnr].script < 0) {
error("[VM] Attempt to dereference class %x, which doesn't exist (max %x)", classnr, s->_classtable.size());
return NULL_REG;
} else {
Class *the_class = &s->_classtable[classnr];
if (!the_class->reg.segment) {
script_get_segment(s, the_class->script, lock);
if (!the_class->reg.segment) {
error("[VM] Trying to instantiate class %x by instantiating script 0x%x (%03d) failed;"
" Entering debugger.", classnr, the_class->script, the_class->script);
return NULL_REG;
}
} else
if (caller.segment != the_class->reg.segment)
s->seg_manager->getScript(the_class->reg.segment)->incrementLockers();
return the_class->reg;
}
}
// Operating on the stack
// 16 bit:
#define PUSH(v) PUSH32(make_reg(0, v))
#define POP() (validate_arithmetic(POP32()))
// 32 bit:
#define PUSH32(a) (*(validate_stack_addr(s, (xs->sp)++)) = (a))
#define POP32() (*(validate_stack_addr(s, --(xs->sp))))
// Getting instruction parameters
#define GET_OP_BYTE() ((uint8)code_buf[(xs->addr.pc.offset)++])
#define GET_OP_WORD() (READ_LE_UINT16(code_buf + ((xs->addr.pc.offset) += 2) - 2))
#define GET_OP_FLEX() ((opcode & 1)? GET_OP_BYTE() : GET_OP_WORD())
#define GET_OP_SIGNED_BYTE() ((int8)(code_buf[(xs->addr.pc.offset)++]))
#define GET_OP_SIGNED_WORD() (((int16)READ_LE_UINT16(code_buf + ((xs->addr.pc.offset) += 2) - 2)))
#define GET_OP_SIGNED_FLEX() ((opcode & 1)? GET_OP_SIGNED_BYTE() : GET_OP_SIGNED_WORD())
ExecStack *execute_method(EngineState *s, uint16 script, uint16 pubfunct, StackPtr sp, reg_t calling_obj, uint16 argc, StackPtr argp) {
int seg = s->seg_manager->segGet(script);
Script *scr = s->seg_manager->getScriptIfLoaded(seg);
if (!scr) // Script not present yet?
seg = script_instantiate(s, script);
else
scr->unmarkDeleted();
int temp = s->seg_manager->validateExportFunc(pubfunct, seg);
if (!temp) {
error("Request for invalid exported function 0x%x of script 0x%x\n", pubfunct, script);
return NULL;
}
// Check if a breakpoint is set on this method
if (s->have_bp & BREAK_EXPORT) {
Breakpoint *bp;
uint32 bpaddress;
bpaddress = (script << 16 | pubfunct);
bp = s->bp_list;
while (bp) {
if (bp->type == BREAK_EXPORT && bp->data.address == bpaddress) {
sciprintf("Break on script %d, export %d\n", script, pubfunct);
breakpointFlag = true;
break;
}
bp = bp->next;
}
}
return add_exec_stack_entry(s, make_reg(seg, temp), sp, calling_obj, argc, argp, -1, calling_obj, s->_executionStack.size()-1, seg);
}
static void _exec_varselectors(EngineState *s) {
// Executes all varselector read/write ops on the TOS
while (!s->_executionStack.empty() && s->_executionStack.back().type == EXEC_STACK_TYPE_VARSELECTOR) {
ExecStack &xs = s->_executionStack.back();
// varselector access?
if (xs.argc) { // write?
*(xs.getVarPointer(s)) = xs.variables_argp[1];
} else // No, read
s->r_acc = *(xs.getVarPointer(s));
s->_executionStack.pop_back();
}
}
ExecStack *send_selector(EngineState *s, reg_t send_obj, reg_t work_obj, StackPtr sp, int framesize, StackPtr argp) {
// send_obj and work_obj are equal for anything but 'super'
// Returns a pointer to the TOS exec_stack element
assert(s);
reg_t funcp;
int selector;
int argc;
int origin = s->_executionStack.size()-1; // Origin: Used for debugging
int print_send_action = 0;
// We return a pointer to the new active ExecStack
// The selector calls we catch are stored below:
Common::Stack<CallsStruct> sendCalls;
while (framesize > 0) {
selector = validate_arithmetic(*argp++);
argc = validate_arithmetic(*argp);
if (argc > 0x800) { // More arguments than the stack could possibly accomodate for
CORE_ERROR("SEND", "More than 0x800 arguments to function call\n");
return NULL;
}
// Check if a breakpoint is set on this method
if (s->have_bp & BREAK_SELECTOR) {
Breakpoint *bp;
char method_name [256];
sprintf(method_name, "%s::%s", obj_get_name(s, send_obj), s->_kernel->getSelectorName(selector).c_str());
bp = s->bp_list;
while (bp) {
int cmplen = strlen(bp->data.name);
if (bp->data.name[cmplen - 1] != ':')
cmplen = 256;
if (bp->type == BREAK_SELECTOR && !strncmp(bp->data.name, method_name, cmplen)) {
sciprintf("Break on %s (in [%04x:%04x])\n", method_name, PRINT_REG(send_obj));
print_send_action = 1;
breakpointFlag = true;
break;
}
bp = bp->next;
}
}
#ifdef VM_DEBUG_SEND
sciprintf("Send to %04x:%04x, selector %04x (%s):", PRINT_REG(send_obj), selector, s->_selectorNames[selector].c_str());
#endif // VM_DEBUG_SEND
ObjVarRef varp;
switch (lookup_selector(s, send_obj, selector, &varp, &funcp)) {
case kSelectorNone:
// WORKAROUND: LSL6 tries to access the invalid 'keep' selector of the game object.
// FIXME: Find out if this is a game bug.
if ((s->_gameName == "LSL6") && (selector == 0x18c)) {
debug("LSL6 detected, continuing...");
break;
}
error("Send to invalid selector 0x%x of object at %04x:%04x\n", 0xffff & selector, PRINT_REG(send_obj));
break;
case kSelectorVariable:
#ifdef VM_DEBUG_SEND
sciprintf("Varselector: ");
if (argc)
sciprintf("Write %04x:%04x\n", PRINT_REG(argp[1]));
else
sciprintf("Read\n");
#endif // VM_DEBUG_SEND
switch (argc) {
case 0: // Read selector
if (print_send_action) {
sciprintf("[read selector]\n");
print_send_action = 0;
}
// fallthrough
case 1:
#ifndef STRICT_SEND
default:
#endif
{ // Argument is supplied -> Selector should be set
if (print_send_action) {
reg_t oldReg = *varp.getPointer(s);
reg_t newReg = argp[1];
sciprintf("[write to selector: change %04x:%04x to %04x:%04x]\n", PRINT_REG(oldReg), PRINT_REG(newReg));
print_send_action = 0;
}
CallsStruct call;
call.address.var = varp; // register the call
call.argp = argp;
call.argc = argc;
call.selector = selector;
call.type = EXEC_STACK_TYPE_VARSELECTOR; // Register as a varselector
sendCalls.push(call);
}
break;
#ifdef STRICT_SEND
default:
sciprintf("Send error: Variable selector %04x in %04x:%04x called with %04x params\n", selector, PRINT_REG(send_obj), argc);
script_debug_flag = 1; // Enter debug mode
g_debug_seeking = g_debug_step_running = 0;
#endif
}
break;
case kSelectorMethod:
#ifdef VM_DEBUG_SEND
sciprintf("Funcselector(");
for (int i = 0; i < argc; i++) {
sciprintf(PREG, PRINT_REG(argp[i+1]));
if (i + 1 < argc)
sciprintf(", ");
}
sciprintf(") at %04x:%04x\n", PRINT_REG(funcp));
#endif // VM_DEBUG_SEND
if (print_send_action) {
sciprintf("[invoke selector]\n");
print_send_action = 0;
}
CallsStruct call;
call.address.func = funcp; // register call
call.argp = argp;
call.argc = argc;
call.selector = selector;
call.type = EXEC_STACK_TYPE_CALL;
call.sp = sp;
sp = CALL_SP_CARRY; // Destroy sp, as it will be carried over
sendCalls.push(call);
break;
} // switch(lookup_selector())
framesize -= (2 + argc);
argp += argc + 1;
}
// Iterate over all registered calls in the reverse order. This way, the first call is
// placed on the TOS; as soon as it returns, it will cause the second call to be executed.
while (!sendCalls.empty()) {
CallsStruct call = sendCalls.pop();
if (call.type == EXEC_STACK_TYPE_VARSELECTOR) // Write/read variable?
add_exec_stack_varselector(s, work_obj, call.argc, call.argp,
call.selector, call.address.var, origin);
else
add_exec_stack_entry(s, call.address.func, call.sp, work_obj,
call.argc, call.argp,
call.selector, send_obj, origin, SCI_XS_CALLEE_LOCALS);
}
_exec_varselectors(s);
if (s->_executionStack.empty())
return NULL;
return &(s->_executionStack.back());
}
ExecStack *add_exec_stack_varselector(EngineState *s, reg_t objp, int argc, StackPtr argp, Selector selector, const ObjVarRef& address, int origin) {
ExecStack *xstack = add_exec_stack_entry(s, NULL_REG, 0, objp, argc, argp, selector, objp, origin, SCI_XS_CALLEE_LOCALS);
// Store selector address in sp
xstack->addr.varp = address;
xstack->type = EXEC_STACK_TYPE_VARSELECTOR;
return xstack;
}
ExecStack *add_exec_stack_entry(EngineState *s, reg_t pc, StackPtr sp, reg_t objp, int argc,
StackPtr argp, Selector selector, reg_t sendp, int origin, SegmentId locals_segment) {
// Returns new TOS element for the execution stack
// locals_segment may be -1 if derived from the called object
//sciprintf("Exec stack: [%d/%d], origin %d, at %p\n", s->execution_stack_pos, s->_executionStack.size(), origin, s->execution_stack);
ExecStack xstack;
xstack.objp = objp;
if (locals_segment != SCI_XS_CALLEE_LOCALS)
xstack.local_segment = locals_segment;
else
xstack.local_segment = pc.segment;
xstack.sendp = sendp;
xstack.addr.pc = pc;
xstack.fp = xstack.sp = sp;
xstack.argc = argc;
xstack.variables_argp = argp; // Parameters
*argp = make_reg(0, argc); // SCI code relies on the zeroeth argument to equal argc
// Additional debug information
xstack.selector = selector;
xstack.origin = origin;
xstack.type = EXEC_STACK_TYPE_CALL; // Normal call
s->_executionStack.push_back(xstack);
return &(s->_executionStack.back());
}
#ifdef DISABLE_VALIDATONS
# define kernel_matches_signature(a, b, c, d) 1
#endif
void vm_handle_fatal_error(EngineState *s, int line, const char *file) {
error("Fatal VM error in %s, L%d; aborting...", file, line);
}
static Script *script_locate_by_segment(EngineState *s, SegmentId seg) {
return s->seg_manager->getScriptIfLoaded(seg);
}
static reg_t pointer_add(EngineState *s, reg_t base, int offset) {
MemObject *mobj = GET_SEGMENT_ANY(*s->seg_manager, base.segment);
if (!mobj) {
error("[VM] Error: Attempt to add %d to invalid pointer %04x:%04x!", offset, PRINT_REG(base));
return NULL_REG;
}
switch (mobj->getType()) {
case MEM_OBJ_LOCALS:
base.offset += 2 * offset;
return base;
case MEM_OBJ_SCRIPT:
case MEM_OBJ_STACK:
case MEM_OBJ_DYNMEM:
base.offset += offset;
return base;
break;
default:
sciprintf("[VM] Error: Attempt to add %d to pointer %04x:%04x: Pointer arithmetics of this type unsupported!", offset, PRINT_REG(base));
return NULL_REG;
}
}
static void gc_countdown(EngineState *s) {
if (s->gc_countdown-- <= 0) {
s->gc_countdown = script_gc_interval;
run_gc(s);
}
}
static const byte _fake_return_buffer[2] = {op_ret << 1, op_ret << 1};
void run_vm(EngineState *s, int restoring) {
assert(s);
reg_t *variables[4]; // global, local, temp, param, as immediate pointers
reg_t *variables_base[4]; // Used for referencing VM ops
SegmentId variables_seg[4]; // Same as above, contains segment IDs
#ifndef DISABLE_VALIDATIONS
int variables_max[4]; // Max. values for all variables
unsigned int code_buf_size = 0 ; // (Avoid spurious warning)
#endif
int temp;
int16 aux_acc; // Auxiliary 16 bit accumulator
reg_t r_temp; // Temporary register
StackPtr s_temp; // Temporary stack pointer
int16 opparams[4]; // opcode parameters
int restadjust = s->r_amp_rest;
// &rest adjusts the parameter count by this value
// Current execution data:
ExecStack *xs = &(s->_executionStack.back());
ExecStack *xs_new = NULL;
Object *obj = obj_get(s, xs->objp);
Script *local_script = script_locate_by_segment(s, xs->local_segment);
int old_execution_stack_base = s->execution_stack_base;
// Used to detect the stack bottom, for "physical" returns
const byte *code_buf = NULL; // (Avoid spurious warning)
if (!local_script) {
script_error(s, __FILE__, __LINE__, "Program Counter gone astray");
return;
}
if (!restoring)
s->execution_stack_base = s->_executionStack.size()-1;
#ifndef DISABLE_VALIDATIONS
// Initialize maximum variable count
if (s->script_000->locals_block)
variables_max[VAR_GLOBAL] = s->script_000->locals_block->_locals.size();
else
variables_max[VAR_GLOBAL] = 0;
#endif
variables_seg[VAR_GLOBAL] = s->script_000->locals_segment;
variables_seg[VAR_TEMP] = variables_seg[VAR_PARAM] = s->stack_segment;
variables_base[VAR_TEMP] = variables_base[VAR_PARAM] = s->stack_base;
// SCI code reads the zeroeth argument to determine argc
if (s->script_000->locals_block)
variables_base[VAR_GLOBAL] = variables[VAR_GLOBAL] = s->script_000->locals_block->_locals.begin();
else
variables_base[VAR_GLOBAL] = variables[VAR_GLOBAL] = NULL;
s->_executionStackPosChanged = true; // Force initialization
while (1) {
byte opcode;
int old_pc_offset;
StackPtr old_sp;
byte opnumber;
int var_type; // See description below
int var_number;
old_pc_offset = xs->addr.pc.offset;
old_sp = xs->sp;
if (s->_executionStackPosChanged) {
Script *scr;
xs = &(s->_executionStack.back());
s->_executionStackPosChanged = false;
scr = script_locate_by_segment(s, xs->addr.pc.segment);
if (!scr) {
// No script? Implicit return via fake instruction buffer
warning("Running on non-existant script in segment %x", xs->addr.pc.segment);
code_buf = _fake_return_buffer;
#ifndef DISABLE_VALIDATIONS
code_buf_size = 2;
#endif
xs->addr.pc.offset = 1;
scr = NULL;
obj = NULL;
} else {
obj = obj_get(s, xs->objp);
code_buf = scr->buf;
#ifndef DISABLE_VALIDATIONS
code_buf_size = scr->buf_size;
#endif
local_script = script_locate_by_segment(s, xs->local_segment);
if (!local_script) {
warning("Could not find local script from segment %x", xs->local_segment);
local_script = NULL;
variables_base[VAR_LOCAL] = variables[VAR_LOCAL] = NULL;
#ifndef DISABLE_VALIDATIONS
variables_max[VAR_LOCAL] = 0;
#endif
} else {
variables_seg[VAR_LOCAL] = local_script->locals_segment;
if (local_script->locals_block)
variables_base[VAR_LOCAL] = variables[VAR_LOCAL] = local_script->locals_block->_locals.begin();
else
variables_base[VAR_LOCAL] = variables[VAR_LOCAL] = NULL;
#ifndef DISABLE_VALIDATIONS
if (local_script->locals_block)
variables_max[VAR_LOCAL] = local_script->locals_block->_locals.size();
else
variables_max[VAR_LOCAL] = 0;
variables_max[VAR_TEMP] = xs->sp - xs->fp;
variables_max[VAR_PARAM] = xs->argc + 1;
#endif
}
variables[VAR_TEMP] = xs->fp;
variables[VAR_PARAM] = xs->variables_argp;
}
}
if (script_abort_flag)
return; // Emergency
// TODO: re-enable this
#if 0
// Debug if this has been requested:
if (script_debug_flag || sci_debug_flags) {
script_debug(s, &(xs->addr.pc), &(xs->sp), &(xs->fp), &(xs->objp), &restadjust, variables_seg, variables, variables_base,
#ifdef DISABLE_VALIDATIONS
NULL,
#else
variables_max,
#endif
breakpointFlag);
breakpointFlag = false;
}
#endif
#ifndef DISABLE_VALIDATIONS
if (xs->sp < xs->fp)
script_error(s, "[VM] "__FILE__, __LINE__, "Stack underflow");
variables_max[VAR_TEMP] = xs->sp - xs->fp;
if (xs->addr.pc.offset >= code_buf_size)
script_error(s, "[VM] "__FILE__, __LINE__, "Program Counter gone astray");
#endif
opcode = GET_OP_BYTE(); // Get opcode
opnumber = opcode >> 1;
for (temp = 0; g_opcode_formats[opnumber][temp]; temp++)
switch (g_opcode_formats[opnumber][temp]) {
case Script_Byte:
opparams[temp] = GET_OP_BYTE();
break;
case Script_SByte:
opparams[temp] = GET_OP_SIGNED_BYTE();
break;
case Script_Word:
opparams[temp] = GET_OP_WORD();
break;
case Script_SWord:
opparams[temp] = GET_OP_SIGNED_WORD();
break;
case Script_Variable:
case Script_Property:
case Script_Local:
case Script_Temp:
case Script_Global:
case Script_Param:
opparams[temp] = GET_OP_FLEX();
break;
case Script_SVariable:
case Script_SRelative:
opparams[temp] = GET_OP_SIGNED_FLEX();
break;
case Script_Offset:
opparams[temp] = GET_OP_FLEX();
break;
case Script_None:
case Script_End:
break;
case Script_Invalid:
default:
error("opcode %02x: Invalid", opcode);
}
// TODO: Replace the following by an opcode table, and several methods for
// each opcode.
switch (opnumber) {
case 0x00: // bnot
s->r_acc = ACC_ARITHMETIC_L(0xffff ^ /*acc*/);
break;
case 0x01: // add
r_temp = POP32();
if (r_temp.segment || s->r_acc.segment) {
reg_t r_ptr = NULL_REG;
int offset;
// Pointer arithmetics!
if (s->r_acc.segment) {
if (r_temp.segment) {
error("Attempt to add two pointers, stack=%04x:%04x and acc=%04x:%04x",
PRINT_REG(r_temp), PRINT_REG(s->r_acc));
offset = 0;
} else {
r_ptr = s->r_acc;
offset = r_temp.offset;
}
} else {
r_ptr = r_temp;
offset = s->r_acc.offset;
}
s->r_acc = pointer_add(s, r_ptr, offset);
} else
s->r_acc = make_reg(0, r_temp.offset + s->r_acc.offset);
break;
case 0x02: // sub
r_temp = POP32();
if (r_temp.segment || s->r_acc.segment) {
reg_t r_ptr = NULL_REG;
int offset;
// Pointer arithmetics!
if (s->r_acc.segment) {
if (r_temp.segment) {
error("Attempt to subtract two pointers, stack=%04x:%04x and acc=%04x:%04x",
PRINT_REG(r_temp), PRINT_REG(s->r_acc));
offset = 0;
} else {
r_ptr = s->r_acc;
offset = r_temp.offset;
}
} else {
r_ptr = r_temp;
offset = s->r_acc.offset;
}
s->r_acc = pointer_add(s, r_ptr, -offset);
} else
s->r_acc = make_reg(0, r_temp.offset - s->r_acc.offset);
break;
case 0x03: // mul
s->r_acc = ACC_ARITHMETIC_L(((int16)POP()) * (int16)/*acc*/);
break;
case 0x04: // div
ACC_AUX_LOAD();
aux_acc = aux_acc != 0 ? ((int16)POP()) / aux_acc : 0;
ACC_AUX_STORE();
break;
case 0x05: // mod
ACC_AUX_LOAD();
aux_acc = aux_acc != 0 ? ((int16)POP()) % aux_acc : 0;
ACC_AUX_STORE();
break;
case 0x06: // shr
s->r_acc = ACC_ARITHMETIC_L(((uint16) POP()) >> /*acc*/);
break;
case 0x07: // shl
s->r_acc = ACC_ARITHMETIC_L(((uint16)POP()) << /*acc*/);
break;
case 0x08: // xor
s->r_acc = ACC_ARITHMETIC_L(POP() ^ /*acc*/);
break;
case 0x09: // and
s->r_acc = ACC_ARITHMETIC_L(POP() & /*acc*/);
break;
case 0x0a: // or
s->r_acc = ACC_ARITHMETIC_L(POP() | /*acc*/);
break;
case 0x0b: // neg
s->r_acc = ACC_ARITHMETIC_L(-/*acc*/);
break;
case 0x0c: // not
s->r_acc = make_reg(0, !(s->r_acc.offset || s->r_acc.segment));
// Must allow pointers to be negated, as this is used for checking whether objects exist
break;
case 0x0d: // eq?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, r_temp == s->r_acc);
// Explicitly allow pointers to be compared
break;
case 0x0e: // ne?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, r_temp != s->r_acc);
// Explicitly allow pointers to be compared
break;
case 0x0f: // gt?
s->r_prev = s->r_acc;
s->r_acc = ACC_ARITHMETIC_L((int16)POP() > (int16)/*acc*/);
break;
case 0x10: // ge?
s->r_prev = s->r_acc;
s->r_acc = ACC_ARITHMETIC_L((int16)POP() >= (int16)/*acc*/);
break;
case 0x11: // lt?
s->r_prev = s->r_acc;
s->r_acc = ACC_ARITHMETIC_L((int16)POP() < (int16)/*acc*/);
break;
case 0x12: // le?
s->r_prev = s->r_acc;
s->r_acc = ACC_ARITHMETIC_L((int16)POP() <= (int16)/*acc*/);
break;
case 0x13: // ugt?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, (r_temp.segment == s->r_acc.segment) && r_temp.offset > s->r_acc.offset);
break;
case 0x14: // uge?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, (r_temp.segment == s->r_acc.segment) && r_temp.offset >= s->r_acc.offset);
break;
case 0x15: // ult?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, (r_temp.segment == s->r_acc.segment) && r_temp.offset < s->r_acc.offset);
break;
case 0x16: // ule?
s->r_prev = s->r_acc;
r_temp = POP32();
s->r_acc = make_reg(0, (r_temp.segment == s->r_acc.segment) && r_temp.offset <= s->r_acc.offset);
break;
case 0x17: // bt
if (s->r_acc.offset || s->r_acc.segment)
xs->addr.pc.offset += opparams[0];
break;
case 0x18: // bnt
if (!(s->r_acc.offset || s->r_acc.segment))
xs->addr.pc.offset += opparams[0];
break;
case 0x19: // jmp
xs->addr.pc.offset += opparams[0];
break;
case 0x1a: // ldi
s->r_acc = make_reg(0, opparams[0]);
break;
case 0x1b: // push
PUSH32(s->r_acc);
break;
case 0x1c: // pushi
PUSH(opparams[0]);
break;
case 0x1d: // toss
xs->sp--;
break;
case 0x1e: // dup
r_temp = xs->sp[-1];
PUSH32(r_temp);
break;
case 0x1f: { // link
int i;
for (i = 0; i < opparams[0]; i++)
xs->sp[i] = NULL_REG;
xs->sp += opparams[0];
break;
}
case 0x20: { // call
int argc = (opparams[1] >> 1) // Given as offset, but we need count
+ 1 + restadjust;
StackPtr call_base = xs->sp - argc;
xs->sp[1].offset += restadjust;
xs_new = add_exec_stack_entry(s, make_reg(xs->addr.pc.segment, xs->addr.pc.offset + opparams[0]),
xs->sp, xs->objp, (validate_arithmetic(*call_base)) + restadjust,
call_base, NULL_SELECTOR, xs->objp, s->_executionStack.size()-1, xs->local_segment);
restadjust = 0; // Used up the &rest adjustment
xs->sp = call_base;
s->_executionStackPosChanged = true;
break;
}
case 0x21: // callk
gc_countdown(s);
xs->sp -= (opparams[1] >> 1) + 1;
if (!(s->_flags & GF_SCI0_OLD)) {
xs->sp -= restadjust;
s->r_amp_rest = 0; // We just used up the restadjust, remember?
}
if (opparams[0] >= (int)s->_kernel->_kernelFuncs.size()) {
error("Invalid kernel function 0x%x requested\n", opparams[0]);
} else {
int argc = ASSERT_ARITHMETIC(xs->sp[0]);
if (!(s->_flags & GF_SCI0_OLD))
argc += restadjust;
if (s->_kernel->_kernelFuncs[opparams[0]].signature
&& !kernel_matches_signature(s, s->_kernel->_kernelFuncs[opparams[0]].signature, argc, xs->sp + 1)) {
error("[VM] Invalid arguments to kernel call %x\n", opparams[0]);
} else {
s->r_acc = s->_kernel->_kernelFuncs[opparams[0]].fun(s, opparams[0], argc, xs->sp + 1);
}
// Call kernel function
// Calculate xs again: The kernel function might
// have spawned a new VM
xs_new = &(s->_executionStack.back());
s->_executionStackPosChanged = true;
if (!(s->_flags & GF_SCI0_OLD))
restadjust = s->r_amp_rest;
}
break;
case 0x22: // callb
temp = ((opparams[1] >> 1) + restadjust + 1);
s_temp = xs->sp;
xs->sp -= temp;
xs->sp[0].offset += restadjust;
xs_new = execute_method(s, 0, opparams[0], s_temp, xs->objp, xs->sp[0].offset, xs->sp);
restadjust = 0; // Used up the &rest adjustment
if (xs_new) // in case of error, keep old stack
s->_executionStackPosChanged = true;
break;
case 0x23: // calle
temp = ((opparams[2] >> 1) + restadjust + 1);
s_temp = xs->sp;
xs->sp -= temp;
xs->sp[0].offset += restadjust;
xs_new = execute_method(s, opparams[0], opparams[1], s_temp, xs->objp, xs->sp[0].offset, xs->sp);
restadjust = 0; // Used up the &rest adjustment
if (xs_new) // in case of error, keep old stack
s->_executionStackPosChanged = true;
break;
case 0x24: // ret
do {
StackPtr old_sp2 = xs->sp;
StackPtr old_fp = xs->fp;
ExecStack *old_xs = &(s->_executionStack.back());
if ((int)s->_executionStack.size()-1 == s->execution_stack_base) { // Have we reached the base?
s->execution_stack_base = old_execution_stack_base; // Restore stack base
s->_executionStack.pop_back();
s->_executionStackPosChanged = true;
s->r_amp_rest = restadjust; // Update &rest
return; // "Hard" return
}
if (old_xs->type == EXEC_STACK_TYPE_VARSELECTOR) {
// varselector access?
if (old_xs->argc) // write?
*(old_xs->getVarPointer(s)) = old_xs->variables_argp[1];
else // No, read
s->r_acc = *(old_xs->getVarPointer(s));
}
// Not reached the base, so let's do a soft return
s->_executionStack.pop_back();
s->_executionStackPosChanged = true;
xs = &(s->_executionStack.back());
if (xs->sp == CALL_SP_CARRY // Used in sends to 'carry' the stack pointer
|| xs->type != EXEC_STACK_TYPE_CALL) {
xs->sp = old_sp2;
xs->fp = old_fp;
}
} while (xs->type == EXEC_STACK_TYPE_VARSELECTOR);
// Iterate over all varselector accesses
s->_executionStackPosChanged = true;
xs_new = xs;
break;
case 0x25: // send
s_temp = xs->sp;
xs->sp -= ((opparams[0] >> 1) + restadjust); // Adjust stack
xs->sp[1].offset += restadjust;
xs_new = send_selector(s, s->r_acc, s->r_acc, s_temp, (int)(opparams[0] >> 1) + (uint16)restadjust, xs->sp);
if (xs_new && xs_new != xs)
s->_executionStackPosChanged = true;
restadjust = 0;
break;
case 0x28: // class
s->r_acc = get_class_address(s, (unsigned)opparams[0], SCRIPT_GET_LOCK, xs->addr.pc);
break;
case 0x2a: // self
s_temp = xs->sp;
xs->sp -= ((opparams[0] >> 1) + restadjust); // Adjust stack
xs->sp[1].offset += restadjust;
xs_new = send_selector(s, xs->objp, xs->objp, s_temp, (int)(opparams[0] >> 1) + (uint16)restadjust, xs->sp);
if (xs_new && xs_new != xs)
s->_executionStackPosChanged = true;
restadjust = 0;
break;
case 0x2b: // super
r_temp = get_class_address(s, opparams[0], SCRIPT_GET_LOAD, xs->addr.pc);
if (!r_temp.segment)
CORE_ERROR("VM", "Invalid superclass in object");
else {
s_temp = xs->sp;
xs->sp -= ((opparams[1] >> 1) + restadjust); // Adjust stack
xs->sp[1].offset += restadjust;
xs_new = send_selector(s, r_temp, xs->objp, s_temp, (int)(opparams[1] >> 1) + (uint16)restadjust, xs->sp);
if (xs_new && xs_new != xs)
s->_executionStackPosChanged = true;
restadjust = 0;
}
break;
case 0x2c: // &rest
temp = (uint16) opparams[0]; // First argument
restadjust = xs->argc - temp + 1; // +1 because temp counts the paramcount while argc doesn't
if (restadjust < 0)
restadjust = 0;
for (; temp <= xs->argc; temp++)
PUSH32(xs->variables_argp[temp]);
break;
case 0x2d: // lea
temp = (uint16) opparams[0] >> 1;
var_number = temp & 0x03; // Get variable type
// Get variable block offset
r_temp.segment = variables_seg[var_number];
r_temp.offset = variables[var_number] - variables_base[var_number];
if (temp & 0x08) // Add accumulator offset if requested
r_temp.offset += signed_validate_arithmetic(s->r_acc);
r_temp.offset += opparams[1]; // Add index
r_temp.offset *= sizeof(reg_t);
// That's the immediate address now
s->r_acc = r_temp;
break;
case 0x2e: // selfID
s->r_acc = xs->objp;
break;
case 0x30: // pprev
PUSH32(s->r_prev);
break;
case 0x31: // pToa
s->r_acc = OBJ_PROPERTY(obj, (opparams[0] >> 1));
break;
case 0x32: // aTop
OBJ_PROPERTY(obj, (opparams[0] >> 1)) = s->r_acc;
break;
case 0x33: // pTos
PUSH32(OBJ_PROPERTY(obj, opparams[0] >> 1));
break;
case 0x34: // sTop
OBJ_PROPERTY(obj, (opparams[0] >> 1)) = POP32();
break;
case 0x35: // ipToa
s->r_acc = OBJ_PROPERTY(obj, (opparams[0] >> 1));
s->r_acc = OBJ_PROPERTY(obj, (opparams[0] >> 1)) = ACC_ARITHMETIC_L(1 + /*acc*/);
break;
case 0x36: // dpToa
s->r_acc = OBJ_PROPERTY(obj, (opparams[0] >> 1));
s->r_acc = OBJ_PROPERTY(obj, (opparams[0] >> 1)) = ACC_ARITHMETIC_L(-1 + /*acc*/);
break;
case 0x37: // ipTos
ASSERT_ARITHMETIC(OBJ_PROPERTY(obj, (opparams[0] >> 1)));
temp = ++OBJ_PROPERTY(obj, (opparams[0] >> 1)).offset;
PUSH(temp);
break;
case 0x38: // dpTos
ASSERT_ARITHMETIC(OBJ_PROPERTY(obj, (opparams[0] >> 1)));
temp = --OBJ_PROPERTY(obj, (opparams[0] >> 1)).offset;
PUSH(temp);
break;
case 0x39: // lofsa
s->r_acc.segment = xs->addr.pc.segment;
if (s->_version >= SCI_VERSION_1_1) {
s->r_acc.offset = opparams[0] + local_script->script_size;
} else {
if (s->_flags & GF_SCI1_LOFSABSOLUTE)
s->r_acc.offset = opparams[0];
else
s->r_acc.offset = xs->addr.pc.offset + opparams[0];
}
#ifndef DISABLE_VALIDATIONS
if (s->r_acc.offset >= code_buf_size) {
error("VM: lofsa operation overflowed: %04x:%04x beyond end"
" of script (at %04x)\n", PRINT_REG(s->r_acc), code_buf_size);
}
#endif
break;
case 0x3a: // lofss
r_temp.segment = xs->addr.pc.segment;
if (s->_flags & GF_SCI1_LOFSABSOLUTE)
r_temp.offset = opparams[0];
else
r_temp.offset = xs->addr.pc.offset + opparams[0];
#ifndef DISABLE_VALIDATIONS
if (r_temp.offset >= code_buf_size) {
error("VM: lofss operation overflowed: %04x:%04x beyond end"
" of script (at %04x)\n", PRINT_REG(r_temp), code_buf_size);
}
#endif
PUSH32(r_temp);
break;
case 0x3b: // push0
PUSH(0);
break;
case 0x3c: // push1
PUSH(1);
break;
case 0x3d: // push2
PUSH(2);
break;
case 0x3e: // pushSelf
PUSH32(xs->objp);
break;
case 0x40: // lag
case 0x41: // lal
case 0x42: // lat
case 0x43: // lap
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
s->r_acc = READ_VAR(var_type, var_number, s->r_acc);
break;
case 0x44: // lsg
case 0x45: // lsl
case 0x46: // lst
case 0x47: // lsp
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
PUSH32(READ_VAR(var_type, var_number, s->r_acc));
break;
case 0x48: // lagi
case 0x49: // lali
case 0x4a: // lati
case 0x4b: // lapi
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
s->r_acc = READ_VAR(var_type, var_number, s->r_acc);
break;
case 0x4c: // lsgi
case 0x4d: // lsli
case 0x4e: // lsti
case 0x4f: // lspi
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
PUSH32(READ_VAR(var_type, var_number, s->r_acc));
break;
case 0x50: // sag
case 0x51: // sal
case 0x52: // sat
case 0x53: // sap
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
WRITE_VAR(var_type, var_number, s->r_acc);
break;
case 0x54: // ssg
case 0x55: // ssl
case 0x56: // sst
case 0x57: // ssp
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
WRITE_VAR(var_type, var_number, POP32());
break;
case 0x58: // sagi
case 0x59: // sali
case 0x5a: // sati
case 0x5b: // sapi
// Special semantics because it wouldn't really make a whole lot
// of sense otherwise, with acc being used for two things
// simultaneously...
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
WRITE_VAR(var_type, var_number, s->r_acc = POP32());
break;
case 0x5c: // ssgi
case 0x5d: // ssli
case 0x5e: // ssti
case 0x5f: // sspi
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
WRITE_VAR(var_type, var_number, POP32());
break;
case 0x60: // +ag
case 0x61: // +al
case 0x62: // +at
case 0x63: // +ap
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
s->r_acc = make_reg(0, 1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
WRITE_VAR(var_type, var_number, s->r_acc);
break;
case 0x64: // +sg
case 0x65: // +sl
case 0x66: // +st
case 0x67: // +sp
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
r_temp = make_reg(0, 1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
PUSH32(r_temp);
WRITE_VAR(var_type, var_number, r_temp);
break;
case 0x68: // +agi
case 0x69: // +ali
case 0x6a: // +ati
case 0x6b: // +api
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
s->r_acc = make_reg(0, 1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
WRITE_VAR(var_type, var_number, s->r_acc);
break;
case 0x6c: // +sgi
case 0x6d: // +sli
case 0x6e: // +sti
case 0x6f: // +spi
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
r_temp = make_reg(0, 1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
PUSH32(r_temp);
WRITE_VAR(var_type, var_number, r_temp);
break;
case 0x70: // -ag
case 0x71: // -al
case 0x72: // -at
case 0x73: // -ap
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
s->r_acc = make_reg(0, -1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
WRITE_VAR(var_type, var_number, s->r_acc);
break;
case 0x74: // -sg
case 0x75: // -sl
case 0x76: // -st
case 0x77: // -sp
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0];
r_temp = make_reg(0, -1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
PUSH32(r_temp);
WRITE_VAR(var_type, var_number, r_temp);
break;
case 0x78: // -agi
case 0x79: // -ali
case 0x7a: // -ati
case 0x7b: // -api
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
s->r_acc = make_reg(0, -1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
WRITE_VAR(var_type, var_number, s->r_acc);
break;
case 0x7c: // -sgi
case 0x7d: // -sli
case 0x7e: // -sti
case 0x7f: // -spi
var_type = (opcode >> 1) & 0x3; // Gets the variable type: g, l, t or p
var_number = opparams[0] + signed_validate_arithmetic(s->r_acc);
r_temp = make_reg(0, -1 + validate_arithmetic(READ_VAR(var_type, var_number, s->r_acc)));
PUSH32(r_temp);
WRITE_VAR(var_type, var_number, r_temp);
break;
default:
script_error(s, __FILE__, __LINE__, "Illegal opcode");
} // switch(opcode >> 1)
if (s->_executionStackPosChanged) // Force initialization
xs = xs_new;
//#ifndef DISABLE_VALIDATIONS
if (xs != &(s->_executionStack.back())) {
warning("xs is stale (%p vs %p); last command was %02x",
(void *)xs, (void *)&(s->_executionStack.back()),
opnumber);
}
//#endif
#if 0
if (script_error_flag) {
g_debug_step_running = 0; // Stop multiple execution
g_debug_seeking = 0; // Stop special seeks
xs->addr.pc.offset = old_pc_offset;
xs->sp = old_sp;
} else
#endif
++script_step_counter;
}
}
static int _obj_locate_varselector(EngineState *s, Object *obj, Selector slc) {
// Determines if obj explicitly defines slc as a varselector
// Returns -1 if not found
if (s->_version < SCI_VERSION_1_1) {
int varnum = obj->variable_names_nr;
int selector_name_offset = varnum * 2 + SCRIPT_SELECTOR_OFFSET;
int i;
byte *buf = obj->base_obj + selector_name_offset;
obj->base_vars = (uint16 *) buf;
for (i = 0; i < varnum; i++)
if (READ_LE_UINT16(buf + (i << 1)) == slc) // Found it?
return i; // report success
return -1; // Failed
} else {
byte *buf = (byte *) obj->base_vars;
int i;
int varnum = obj->_variables[1].offset;
if (!(obj->_variables[SCRIPT_INFO_SELECTOR].offset & SCRIPT_INFO_CLASS))
buf = ((byte *) obj_get(s, obj->_variables[SCRIPT_SUPERCLASS_SELECTOR])->base_vars);
for (i = 0; i < varnum; i++)
if (READ_LE_UINT16(buf + (i << 1)) == slc) // Found it?
return i; // report success
return -1; // Failed
}
}
static int _class_locate_funcselector(EngineState *s, Object *obj, Selector slc) {
// Determines if obj is a class and explicitly defines slc as a funcselector
// Does NOT say anything about obj's superclasses, i.e. failure may be
// returned even if one of the superclasses defines the funcselector.
int funcnum = obj->methods_nr;
int i;
for (i = 0; i < funcnum; i++)
if (VM_OBJECT_GET_FUNCSELECTOR(obj, i) == slc) // Found it?
return i; // report success
return -1; // Failed
}
static SelectorType _lookup_selector_function(EngineState *s, int seg_id, Object *obj, Selector selector_id, reg_t *fptr) {
int index;
// "recursive" lookup
while (obj) {
index = _class_locate_funcselector(s, obj, selector_id);
if (index >= 0) {
if (fptr) {
*fptr = VM_OBJECT_READ_FUNCTION(obj, index);
}
return kSelectorMethod;
} else {
seg_id = obj->_variables[SCRIPT_SUPERCLASS_SELECTOR].segment;
obj = obj_get(s, obj->_variables[SCRIPT_SUPERCLASS_SELECTOR]);
}
}
return kSelectorNone;
}
SelectorType lookup_selector(EngineState *s, reg_t obj_location, Selector selector_id, ObjVarRef *varp, reg_t *fptr) {
Object *obj = obj_get(s, obj_location);
Object *species;
int index;
// Early SCI versions used the LSB in the selector ID as a read/write
// toggle, meaning that we must remove it for selector lookup.
if (s->_flags & GF_SCI0_OLD)
selector_id &= ~1;
if (!obj) {
CORE_ERROR("SLC-LU", "Attempt to send to non-object or invalid script");
sciprintf("Address was %04x:%04x\n", PRINT_REG(obj_location));
return kSelectorNone;
}
if (IS_CLASS(obj))
species = obj;
else
species = obj_get(s, obj->_variables[SCRIPT_SPECIES_SELECTOR]);
if (!obj) {
CORE_ERROR("SLC-LU", "Error while looking up Species class");
sciprintf("Original address was %04x:%04x\n", PRINT_REG(obj_location));
sciprintf("Species address was %04x:%04x\n", PRINT_REG(obj->_variables[SCRIPT_SPECIES_SELECTOR]));
return kSelectorNone;
}
index = _obj_locate_varselector(s, obj, selector_id);
if (index >= 0) {
// Found it as a variable
if (varp) {
varp->obj = obj_location;
varp->varindex = index;
}
return kSelectorVariable;
}
return _lookup_selector_function(s, obj_location.segment, obj, selector_id, fptr);
}
SegmentId script_get_segment(EngineState *s, int script_nr, int load) {
SegmentId segment;
if ((load & SCRIPT_GET_LOAD) == SCRIPT_GET_LOAD)
script_instantiate(s, script_nr);
segment = s->seg_manager->segGet(script_nr);
if (segment > 0) {
if ((load & SCRIPT_GET_LOCK) == SCRIPT_GET_LOCK)
s->seg_manager->getScript(segment)->incrementLockers();
return segment;
} else
return 0;
}
reg_t script_lookup_export(EngineState *s, int script_nr, int export_index) {
SegmentId seg = script_get_segment(s, script_nr, SCRIPT_GET_DONT_LOAD);
Script *script = NULL;
#ifndef DISABLE_VALIDATIONS
if (!seg) {
CORE_ERROR("EXPORTS", "Script invalid or not loaded");
sciprintf("Script was script.%03d (0x%x)\n",
script_nr, script_nr);
return NULL_REG;
}
#endif
script = script_locate_by_segment(s, seg);
#ifndef DISABLE_VALIDATIONS
if (script
&& export_index < script->exports_nr
&& export_index >= 0)
#endif
return make_reg(seg, READ_LE_UINT16((byte *)(script->export_table + export_index)));
#ifndef DISABLE_VALIDATIONS
else {
CORE_ERROR("EXPORTS", "Export invalid or script missing ");
if (!script)
sciprintf("(script.%03d missing)\n", script_nr);
else
sciprintf("(script.%03d: Sought export %d/%d)\n",
script_nr, export_index, script->exports_nr);
return NULL_REG;
}
#endif
}
#define INST_LOOKUP_CLASS(id) ((id == 0xffff)? NULL_REG : get_class_address(s, id, SCRIPT_GET_LOCK, reg))
int script_instantiate_common(EngineState *s, int script_nr, Resource **script, Resource **heap, int *was_new) {
int seg_id;
reg_t reg;
*was_new = 1;
*script = s->resmgr->findResource(ResourceId(kResourceTypeScript, script_nr), 0);
if (s->_version >= SCI_VERSION_1_1)
*heap = s->resmgr->findResource(ResourceId(kResourceTypeHeap, script_nr), 0);
if (!*script || (s->_version >= SCI_VERSION_1_1 && !heap)) {
sciprintf("Script 0x%x requested but not found\n", script_nr);
if (s->_version >= SCI_VERSION_1_1) {
if (*heap)
sciprintf("Inconsistency: heap resource WAS found\n");
else if (*script)
sciprintf("Inconsistency: script resource WAS found\n");
}
return 0;
}
if (NULL == s) {
sciprintf("vm.c: script_instantiate(): NULL passed for \"s\"\n");
return 0;
}
seg_id = s->seg_manager->segGet(script_nr);
Script *scr = script_locate_by_segment(s, seg_id);
if (scr) {
if (!scr->isMarkedAsDeleted()) {
scr->incrementLockers();
return seg_id;
} else {
scr->freeScript();
}
} else {
scr = s->seg_manager->allocateScript(s, script_nr, &seg_id);
if (!scr) { // ALL YOUR SCRIPT BASE ARE BELONG TO US
error("Not enough heap space for script size 0x%x of script 0x%x (Should this happen?)", (*script)->size, script_nr);
return 0;
}
}
s->seg_manager->initialiseScript(*scr, s, script_nr);
reg.segment = seg_id;
reg.offset = 0;
// Set heap position (beyond the size word)
scr->setLockers(1);
scr->setExportTableOffset(0);
scr->setSynonymsOffset(0);
scr->setSynonymsNr(0);
*was_new = 0;
return seg_id;
}
int script_instantiate_sci0(EngineState *s, int script_nr) {
int objtype;
unsigned int objlength;
reg_t reg;
int seg_id;
int relocation = -1;
int magic_pos_adder; // Usually 0; 2 for older SCI versions
Resource *script;
int was_new;
seg_id = script_instantiate_common(s, script_nr, &script, NULL, &was_new);
if (was_new)
return seg_id;
reg.segment = seg_id;
reg.offset = 0;
Script *scr = s->seg_manager->getScript(seg_id);
if (s->_flags & GF_SCI0_OLD) {
//
int locals_nr = READ_LE_UINT16(script->data);
// Old script block
// There won't be a localvar block in this case
// Instead, the script starts with a 16 bit int specifying the
// number of locals we need; these are then allocated and zeroed.
scr->mcpyInOut(0, script->data, script->size);
magic_pos_adder = 2; // Step over the funny prefix
if (locals_nr)
s->seg_manager->scriptInitialiseLocalsZero(reg.segment, locals_nr);
} else {
scr->mcpyInOut(0, script->data, script->size);
magic_pos_adder = 0;
}
// Now do a first pass through the script objects to find the
// export table and local variable block
objlength = 0;
reg.offset = magic_pos_adder;
do {
reg_t data_base;
reg_t addr;
reg.offset += objlength; // Step over the last checked object
objtype = scr->getHeap(reg.offset);
if (!objtype) break;
objlength = scr->getHeap(reg.offset + 2);
data_base = reg;
data_base.offset += 4;
addr = data_base;
switch (objtype) {
case SCI_OBJ_EXPORTS: {
scr->setExportTableOffset(data_base.offset);
}
break;
case SCI_OBJ_SYNONYMS:
scr->setSynonymsOffset(addr.offset); // +4 is to step over the header
scr->setSynonymsNr((objlength) / 4);
break;
case SCI_OBJ_LOCALVARS:
s->seg_manager->scriptInitialiseLocals(data_base);
break;
case SCI_OBJ_CLASS: {
int classpos = addr.offset - SCRIPT_OBJECT_MAGIC_OFFSET;
int species;
species = scr->getHeap(addr.offset - SCRIPT_OBJECT_MAGIC_OFFSET + SCRIPT_SPECIES_OFFSET);
if (species < 0 || species >= (int)s->_classtable.size()) {
error("Invalid species %d(0x%x) not in interval "
"[0,%d) while instantiating script %d\n",
species, species, s->_classtable.size(),
script_nr);
return 1;
}
s->_classtable[species].script = script_nr;
s->_classtable[species].reg = addr;
s->_classtable[species].reg.offset = classpos;
// Set technical class position-- into the block allocated for it
}
break;
default:
break;
}
} while (objtype != 0);
// And now a second pass to adjust objects and class pointers, and the general pointers
objlength = 0;
reg.offset = magic_pos_adder; // Reset counter
do {
reg_t addr;
reg.offset += objlength; // Step over the last checked object
objtype = scr->getHeap(reg.offset);
if (!objtype) break;
objlength = scr->getHeap(reg.offset + 2);
reg.offset += 4; // Step over header
addr = reg;
switch (objtype) {
case SCI_OBJ_CODE:
s->seg_manager->scriptAddCodeBlock(addr);
break;
case SCI_OBJ_OBJECT:
case SCI_OBJ_CLASS: { // object or class?
Object *obj = s->seg_manager->scriptObjInit(s, addr);
Object *base_obj;
// Instantiate the superclass, if neccessary
obj->_variables[SCRIPT_SPECIES_SELECTOR] = INST_LOOKUP_CLASS(obj->_variables[SCRIPT_SPECIES_SELECTOR].offset);
base_obj = obj_get(s, obj->_variables[SCRIPT_SPECIES_SELECTOR]);
obj->variable_names_nr = base_obj->_variables.size();
obj->base_obj = base_obj->base_obj;
// Copy base from species class, as we need its selector IDs
obj->_variables[SCRIPT_SUPERCLASS_SELECTOR] = INST_LOOKUP_CLASS(obj->_variables[SCRIPT_SUPERCLASS_SELECTOR].offset);
} // if object or class
break;
case SCI_OBJ_POINTERS: // A relocation table
relocation = addr.offset;
break;
default:
break;
}
reg.offset -= 4; // Step back on header
} while ((objtype != 0) && (((unsigned)reg.offset) < script->size - 2));
if (relocation >= 0)
s->seg_manager->scriptRelocate(make_reg(reg.segment, relocation));
return reg.segment; // instantiation successful
}
int script_instantiate_sci11(EngineState *s, int script_nr) {
Resource *script, *heap;
int seg_id;
int heap_start;
reg_t reg;
int was_new;
seg_id = script_instantiate_common(s, script_nr, &script, &heap, &was_new);
if (was_new)
return seg_id;
Script *scr = s->seg_manager->getScript(seg_id);
heap_start = script->size;
if (script->size & 2)
heap_start ++;
scr->mcpyInOut(0, script->data, script->size);
scr->mcpyInOut(heap_start, heap->data, heap->size);
if (READ_LE_UINT16(script->data + 6) > 0)
scr->setExportTableOffset(6);
reg.segment = seg_id;
reg.offset = heap_start + 4;
s->seg_manager->scriptInitialiseLocals(reg);
s->seg_manager->scriptRelocateExportsSci11(seg_id);
s->seg_manager->scriptInitialiseObjectsSci11(s, seg_id);
reg.offset = READ_LE_UINT16(heap->data);
s->seg_manager->heapRelocate(reg);
return seg_id;
}
int script_instantiate(EngineState *s, int script_nr) {
if (s->_version >= SCI_VERSION_1_1)
return script_instantiate_sci11(s, script_nr);
else
return script_instantiate_sci0(s, script_nr);
}
void script_uninstantiate_sci0(EngineState *s, int script_nr, SegmentId seg) {
reg_t reg = make_reg(seg, (s->_flags & GF_SCI0_OLD) ? 2 : 0);
int objtype, objlength;
Script *scr = s->seg_manager->getScript(seg);
// Make a pass over the object in order uninstantiate all superclasses
objlength = 0;
do {
reg.offset += objlength; // Step over the last checked object
objtype = scr->getHeap(reg.offset);
if (!objtype)
break;
objlength = scr->getHeap(reg.offset + 2); // use SEG_UGET_HEAP ??
reg.offset += 4; // Step over header
if ((objtype == SCI_OBJ_OBJECT) || (objtype == SCI_OBJ_CLASS)) { // object or class?
int superclass;
reg.offset -= SCRIPT_OBJECT_MAGIC_OFFSET;
superclass = scr->getHeap(reg.offset + SCRIPT_SUPERCLASS_OFFSET); // Get superclass...
if (superclass >= 0) {
int superclass_script = s->_classtable[superclass].script;
if (superclass_script == script_nr) {
if (scr->getLockers())
scr->decrementLockers(); // Decrease lockers if this is us ourselves
} else
script_uninstantiate(s, superclass_script);
// Recurse to assure that the superclass lockers number gets decreased
}
reg.offset += SCRIPT_OBJECT_MAGIC_OFFSET;
} // if object or class
reg.offset -= 4; // Step back on header
} while (objtype != 0);
}
void script_uninstantiate(EngineState *s, int script_nr) {
reg_t reg = make_reg(0, (s->_flags & GF_SCI0_OLD) ? 2 : 0);
reg.segment = s->seg_manager->segGet(script_nr);
Script *scr = script_locate_by_segment(s, reg.segment);
if (!scr) { // Is it already loaded?
//warning("unloading script 0x%x requested although not loaded", script_nr);
// This is perfectly valid SCI behaviour
return;
}
scr->decrementLockers(); // One less locker
if (scr->getLockers() > 0)
return;
// Free all classtable references to this script
for (uint i = 0; i < s->_classtable.size(); i++)
if (s->_classtable[i].reg.segment == reg.segment)
s->_classtable[i].reg = NULL_REG;
if (s->_version < SCI_VERSION_1_1)
script_uninstantiate_sci0(s, script_nr, reg.segment);
else
sciprintf("FIXME: Add proper script uninstantiation for SCI 1.1\n");
if (scr->getLockers())
return; // if xxx.lockers > 0
// Otherwise unload it completely
// Explanation: I'm starting to believe that this work is done by SCI itself.
scr->markDeleted();
debugC(kDebugLevelScripts, "Unloaded script 0x%x.\n", script_nr);
return;
}
static void _init_stack_base_with_selector(EngineState *s, Selector selector) {
s->stack_base[0] = make_reg(0, (uint16)selector);
s->stack_base[1] = NULL_REG;
}
static EngineState *_game_run(EngineState *&s, int restoring) {
EngineState *successor = NULL;
int game_is_finished = 0;
do {
s->_executionStackPosChanged = false;
run_vm(s, (successor || restoring) ? 1 : 0);
if (s->restarting_flags & SCI_GAME_IS_RESTARTING_NOW) { // Restart was requested?
successor = NULL;
s->_executionStack.clear();
s->_executionStackPosChanged = false;
game_exit(s);
script_free_engine(s);
script_init_engine(s);
game_init(s);
sfx_reset_player();
_init_stack_base_with_selector(s, s->_kernel->_selectorMap.play);
send_selector(s, s->game_obj, s->game_obj, s->stack_base, 2, s->stack_base);
script_abort_flag = 0;
s->restarting_flags = SCI_GAME_WAS_RESTARTED | SCI_GAME_WAS_RESTARTED_AT_LEAST_ONCE;
} else {
successor = s->successor;
if (successor) {
game_exit(s);
script_free_vm_memory(s);
delete s;
s = successor;
if (script_abort_flag == 2) {
sciprintf("Restarting with replay()\n");
s->_executionStack.clear(); // Restart with replay
_init_stack_base_with_selector(s, s->_kernel->_selectorMap.replay);
send_selector(s, s->game_obj, s->game_obj, s->stack_base, 2, s->stack_base);
}
script_abort_flag = 0;
} else
game_is_finished = 1;
}
} while (!game_is_finished);
return s;
}
int printObject(EngineState *s, reg_t pos);
int game_run(EngineState **_s) {
EngineState *s = *_s;
sciprintf(" Calling %s::play()\n", s->_gameName.c_str());
_init_stack_base_with_selector(s, s->_kernel->_selectorMap.play); // Call the play selector
// Now: Register the first element on the execution stack-
if (!send_selector(s, s->game_obj, s->game_obj, s->stack_base, 2, s->stack_base)) {
printObject(s, s->game_obj);
sciprintf("Failed to run the game! Aborting...\n");
return 1;
}
// and ENGAGE!
_game_run(*_s, 0);
sciprintf(" Game::play() finished.\n");
return 0;
}
Object *obj_get(EngineState *s, reg_t offset) {
MemObject *mobj = GET_OBJECT_SEGMENT(*s->seg_manager, offset.segment);
Object *obj = NULL;
int idx;
if (mobj != NULL) {
if (mobj->getType() == MEM_OBJ_CLONES) {
CloneTable *ct = (CloneTable *)mobj;
if (ct->isValidEntry(offset.offset))
obj = &(ct->_table[offset.offset]);
} else if (mobj->getType() == MEM_OBJ_SCRIPT) {
Script *scr = (Script *)mobj;
if (offset.offset <= scr->buf_size && offset.offset >= -SCRIPT_OBJECT_MAGIC_OFFSET
&& RAW_IS_OBJECT(scr->buf + offset.offset)) {
idx = RAW_GET_CLASS_INDEX(scr, offset);
if (idx >= 0 && (uint)idx < scr->_objects.size())
obj = &scr->_objects[idx];
}
}
}
return obj;
}
const char *obj_get_name(EngineState *s, reg_t pos) {
Object *obj = obj_get(s, pos);
if (!obj)
return "<no such object>";
reg_t nameReg = obj->_variables[SCRIPT_NAME_SELECTOR];
if (nameReg.isNull())
return "<no name>";
const char *name = (const char*)s->seg_manager->dereference(obj->_variables[SCRIPT_NAME_SELECTOR], NULL);
if (!name)
return "<invalid name>";
return name;
}
void quit_vm() {
script_abort_flag = 1; // Terminate VM
g_debugstate_valid = 0;
g_debug_seeking = 0;
g_debug_step_running = 0;
}
void shrink_execution_stack(EngineState *s, uint size) {
assert(s->_executionStack.size() >= size);
Common::List<ExecStack>::iterator iter;
iter = s->_executionStack.begin();
for (uint i = 0; i < size; ++i)
++iter;
s->_executionStack.erase(iter, s->_executionStack.end());
}
reg_t* ObjVarRef::getPointer(EngineState *s) const {
Object *o = obj_get(s, obj);
if (!o) return 0;
return &(o->_variables[varindex]);
}
reg_t* ExecStack::getVarPointer(EngineState *s) const {
assert(type == EXEC_STACK_TYPE_VARSELECTOR);
return addr.varp.getPointer(s);
}
} // End of namespace Sci
|