aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/tads/tads2/run.cpp
blob: 5a9ede0f2b69a8154a81201c08f6cf969497236f (plain)
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
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
/* 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.
 *
 */

#include "glk/tads/tads2/run.h"
#include "glk/tads/tads2/data.h"
#include "glk/tads/tads2/error.h"
#include "glk/tads/tads2/list.h"
#include "glk/tads/tads2/os.h"
#include "glk/tads/tads2/post_compilation.h"
#include "glk/tads/tads2/vocabulary.h"
#include "glk/tads/os_glk.h"

namespace Glk {
namespace TADS {
namespace TADS2 {

/* forward declarations */
struct bifcxdef;

/*
*   Create a new object
*/
static void run_new(runcxdef *ctx, uchar *noreg *codepp,
	objnum callobj, prpnum callprop)
{
	objnum   sc = 0;
	objnum   objn;
	objdef  *objp;
	int      sccnt;
	vocidef *voci;

	/* get the superclass (nil means no superclass) */
	if (runtostyp(ctx) == DAT_NIL)
		sccnt = 0;
	else
	{
		/* get the superclass */
		sc = runpopobj(ctx);
		sccnt = 1;

		/* make sure it's not a dynamically-allocated object */
		voci = vocinh(ctx->runcxvoc, sc);
		if (voci->vociflg & VOCIFNEW)
			runsig(ctx, ERR_BADNEWSC);
	}

	/* create a new object and set its superclass */
	objp = objnew(ctx->runcxmem, sccnt, 64, &objn, FALSE);
	if (sccnt) oswp2(objsc(objp), sc);

	/* save undo for the object creation */
	vocdusave_newobj(ctx->runcxvoc, objn);

	/* touch and unlock the object */
	mcmtch(ctx->runcxmem, (mcmon)objn);
	mcmunlck(ctx->runcxmem, (mcmon)objn);

	/* add a vocabulary inheritance record for the new object */
	vociadd(ctx->runcxvoc, objn, MCMONINV, sccnt, &sc, VOCIFNEW | VOCIFVOC);

	/* set up its vocabulary, inheriting from the class */
	if (sccnt)
		supivoc1((struct supcxdef *)0, ctx->runcxvoc,
			vocinh(ctx->runcxvoc, objn), objn, TRUE, VOCFNEW);

	/* run the constructor */
	runpprop(ctx, codepp, callobj, callprop, objn, PRP_CONSTRUCT,
		FALSE, 0, objn);
#ifdef NEVER
	/*
	*   add it to its location's contents list by calling
	*   newobj.moveInto(newobj.location)
	*/
	runppr(ctx, objn, PRP_LOCATION, 0);
	if (runtostyp(ctx) == DAT_OBJECT)
		runppr(ctx, objn, PRP_MOVEINTO, 1);
	else
		rundisc(ctx);
#endif

	/* return the new object */
	runpobj(ctx, objn);
}

/*
*   Delete an object
*/
static void run_delete(runcxdef *ctx, uchar *noreg *codepp,
	objnum callobj, prpnum callprop)
{
	objnum    objn;
	vocidef  *voci;
	int       i;
	voccxdef *vctx = ctx->runcxvoc;

	/* get the object to be deleted */
	objn = runpopobj(ctx);

	/* make sure it was allocated with "new" */
	voci = vocinh(vctx, objn);
	if (voci == 0 || !(voci->vociflg & VOCIFNEW))
		runsig(ctx, ERR_BADDEL);

	/* run the destructor */
	runpprop(ctx, codepp, callobj, callprop, objn, PRP_DESTRUCT,
		FALSE, 0, objn);
#ifdef NEVER
	/* remove it from its location, if any, by using moveInto(nil) */
	runpnil(ctx);
	runppr(ctx, objn, PRP_MOVEINTO, 1);
#endif

	/* save undo for the object deletion */
	vocdusave_delobj(vctx, objn);

	/* delete the object's inheritance and vocabulary records */
	vocdel(vctx, objn);
	vocidel(vctx, objn);

	/* forget 'it' if the deleted object is 'it' (or 'them', etc) */
	if (vctx->voccxit == objn) vctx->voccxit = MCMONINV;
	if (vctx->voccxhim == objn) vctx->voccxhim = MCMONINV;
	if (vctx->voccxher == objn) vctx->voccxher = MCMONINV;
	for (i = 0; i < vctx->voccxthc; ++i)
	{
		if (vctx->voccxthm[i] == objn)
		{
			/* forget the entire 'them' list when deleting from it */
			vctx->voccxthc = 0;
			break;
		}
	}

	/* forget the 'again' statistics if necessary */
	if (vctx->voccxlsd.vocolobj == objn
		|| vctx->voccxlsi.vocolobj == objn
		|| vctx->voccxlsa == objn
		|| vctx->voccxlsv == objn
		|| vctx->voccxlsp == objn)
	{
		/* forget the verb */
		vctx->voccxlsv = MCMONINV;

		/*
		*   note in the flags why we lost the "again" verb, for better
		*   error reporting if the player tries to type "again"
		*/
		vctx->voccxflg |= VOCCXAGAINDEL;
	}

	/* delete the memory manager object */
	mcmfre(ctx->runcxmem, (mcmon)objn);
}


/*
*   invoke a function
*/
void runfn(runcxdef *ctx, noreg objnum  objn, int argc)
{
	uchar *fn;
	int    err;

	NOREG((&objn))

		/* get a lock on the object */
		fn = mcmlck(ctx->runcxmem, objn);

	/* catch any errors, so we can unlock the object */
	ERRBEGIN(ctx->runcxerr)

		/* execute the object */
		runexe(ctx, fn, MCMONINV, objn, (prpnum)0, argc);

	/* in case of error, unlock the object and resignal the error */
	ERRCATCH(ctx->runcxerr, err)
		mcmunlck(ctx->runcxmem, objn);    /* release the lock on the object */
	if (err < ERR_RUNEXIT || err > ERR_RUNEXITOBJ)
		dbgdump(ctx->runcxdbg);                       /* dump the stack */
	errrse(ctx->runcxerr);
	ERREND(ctx->runcxerr)

		/* we're done with the object, so unlock it */
		mcmunlck(ctx->runcxmem, objn);
}

/*
*   compress the heap - remove unreferenced items
*/
void runhcmp(runcxdef *ctx, uint siz, uint below,
	runsdef *val1, runsdef *val2, runsdef *val3)
{
	uchar   *hp = ctx->runcxheap;
	uchar   *htop = ctx->runcxhp;
	runsdef *stop = ctx->runcxsp + below;
	runsdef *stk = ctx->runcxstk;
	runsdef *sp;
	uchar   *dst = hp;
	uchar   *hnxt;
	int      ref;

	/* go through heap, finding references on stack */
	for (; hp < htop; hp = hnxt)
	{
		hnxt = hp + osrp2(hp);                /* remember next heap element */

		for (ref = FALSE, sp = stk; sp < stop; ++sp)
		{
			switch (sp->runstyp)
			{
			case DAT_SSTRING:
			case DAT_LIST:
				if (sp->runsv.runsvstr == hp)    /* reference to this item? */
				{
					ref = TRUE;             /* this heap item is referenced */
					sp->runsv.runsvstr = dst;      /* reflect imminent move */
				}
				break;

			default:                /* other types do not refer to the heap */
				break;
			}
		}

		/* check the explicitly referenced value pointers as well */
#define CHECK_VAL(val) \
        if (val && val->runsv.runsvstr == hp) \
            ref = TRUE, val->runsv.runsvstr = dst;
		CHECK_VAL(val1);
		CHECK_VAL(val2);
		CHECK_VAL(val3);
#undef CHECK_VAL

		/* if referenced, copy it to dst and advance dst */
		if (ref)
		{
			if (hp != dst) memmove(dst, hp, (size_t)osrp2(hp));
			dst += osrp2(dst);
		}
	}

	/* set heap pointer based on shuffled heap */
	ctx->runcxhp = dst;

	/* check for space requested, and signal error if not available */
	if ((uint)(ctx->runcxhtop - ctx->runcxhp) < siz)
		runsig(ctx, ERR_HPOVF);
}

/*
*   push a value onto the stack that's already been allocated in heap
*/
void runrepush(runcxdef *ctx, runsdef *val)
{
	/* check for stack overflow */
	runstkovf(ctx);

	OSCPYSTRUCT(*(ctx->runcxsp), *val);

	/* increment stack pointer */
	++(ctx->runcxsp);
}

/* push a counted-length string onto the stack */
void runpstr(runcxdef *ctx, char *str, int len, int sav)
{
	runsdef val;

	/* allocate space and set up new string */
	runhres(ctx, len + 2, sav);
	oswp2(ctx->runcxhp, len + 2);
	memcpy(ctx->runcxhp + 2, str, (size_t)len);

	/* push return value */
	val.runsv.runsvstr = ctx->runcxhp;
	val.runstyp = DAT_SSTRING;
	ctx->runcxhp += len + 2;
	runrepush(ctx, &val);
}

/* push a C-style string, converting escape codes */
void runpushcstr(runcxdef *ctx, char *str, size_t len, int sav)
{
	char    *p;
	char    *dst;
	size_t   need;
	runsdef  val;

	/* determine how much space we'll need after converting escapes */
	for (p = str, need = len; p < str + len; ++p)
	{
		switch (*p)
		{
		case '\\':
		case '\n':
		case '\r':
		case '\t':
			/* these characters need to be escaped */
			++need;
			break;

		default:
			break;
		}
	}

	/* reserve space */
	runhres(ctx, need + 2, sav);

	/* set up the length prefix */
	oswp2(ctx->runcxhp, need + 2);

	/* copy the string, expanding escapes */
	for (p = str, dst = (char *)ctx->runcxhp + 2; p < str + len; ++p)
	{
		switch (*p)
		{
		case '\\':
			*dst++ = '\\';
			*dst++ = '\\';
			break;

		case '\n':
		case '\r':
			*dst++ = '\\';
			*dst++ = 'n';
			break;

		case '\t':
			*dst++ = '\\';
			*dst++ = '\t';
			break;

		default:
			*dst++ = *p;
			break;
		}
	}

	/* push the return value */
	val.runsv.runsvstr = ctx->runcxhp;
	val.runstyp = DAT_SSTRING;
	ctx->runcxhp += need + 2;
	runrepush(ctx, &val);
}

/* push a value onto the stack */
void runpush(runcxdef *ctx, dattyp typ, runsdef *val)
{
	int len;

	/* check for stack overflow */
	runstkovf(ctx);

	OSCPYSTRUCT(*(ctx->runcxsp), *val);
	ctx->runcxsp->runstyp = typ;

	/* variable-length data must be copied into the heap */
	if (typ == DAT_SSTRING || typ == DAT_LIST)
	{
		len = osrp2(val->runsv.runsvstr);
		runhres(ctx, len, 0);                      /* reserve space in heap */
		memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len);
		ctx->runcxsp->runsv.runsvstr = ctx->runcxhp;
		ctx->runcxhp += len;
	}

	/* increment stack pointer */
	++(ctx->runcxsp);
}

/* push a number onto the stack */
void runpnum(runcxdef *ctx, long num)
{
	runsdef val;

	val.runsv.runsvnum = num;
	runpush(ctx, DAT_NUMBER, &val);
}

/* push an object onto the stack (or nil if obj is MCMONINV) */
void runpobj(runcxdef *ctx, objnum obj)
{
	runsdef val;

	if (obj == MCMONINV)
		runpnil(ctx);
	else
	{
		val.runsv.runsvobj = obj;
		runpush(ctx, DAT_OBJECT, &val);
	}
}

/* push nil */
void runpnil(runcxdef *ctx)
{
	runsdef val;
	runpush(ctx, DAT_NIL, &val);
}

/* copy datatype + value from a runsdef into a buffer (such as list) */
static void runputbuf(uchar *dstp, runsdef *val)
{
	*dstp++ = val->runstyp;
	switch (val->runstyp)
	{
	case DAT_LIST:
	case DAT_SSTRING:
		memcpy(dstp, val->runsv.runsvstr, (size_t)osrp2(val->runsv.runsvstr));
		break;

	case DAT_NUMBER:
		oswp4s(dstp, val->runsv.runsvnum);
		break;

	case DAT_PROPNUM:
		oswp2(dstp, val->runsv.runsvprp);
		break;

	case DAT_OBJECT:
	case DAT_FNADDR:
		oswp2(dstp, val->runsv.runsvobj);
		break;
	}
}

/* push a value from a buffer (list, property, etc) onto stack */
void runpbuf(runcxdef *ctx, int typ, void *valp)
{
	runsdef val;

	switch (typ)
	{
	case DAT_NUMBER:
		val.runsv.runsvnum = osrp4s(valp);
		break;

	case DAT_OBJECT:
	case DAT_FNADDR:
		val.runsv.runsvobj = osrp2(valp);
		break;

	case DAT_PROPNUM:
		val.runsv.runsvprp = osrp2(valp);
		break;

	case DAT_SSTRING:
	case DAT_LIST:
		val.runsv.runsvstr = (uchar *)valp;
		break;

	case DAT_NIL:
	case DAT_TRUE:
		break;
	}
	runpush(ctx, typ, &val);
}

/* compare items at top of stack for equality; TRUE->equal, FALSE->unequal */
int runeq(runcxdef *ctx)
{
	runsdef val1, val2;

	/* get values, and see if they have identical type; not equal if not */
	runpop(ctx, &val1);
	runpop(ctx, &val2);
	if (val1.runstyp != val2.runstyp) return(FALSE);

	/* types match, so check values */
	switch (val1.runstyp)
	{
	case DAT_NUMBER:
		return(val1.runsv.runsvnum == val2.runsv.runsvnum);

	case DAT_SSTRING:
	case DAT_LIST:
		return(osrp2(val1.runsv.runsvstr) == osrp2(val2.runsv.runsvstr)
			&& !memcmp(val1.runsv.runsvstr, val2.runsv.runsvstr,
			(size_t)osrp2(val1.runsv.runsvstr)));

	case DAT_PROPNUM:
		return(val1.runsv.runsvprp == val2.runsv.runsvprp);

	case DAT_OBJECT:
	case DAT_FNADDR:
		return(val1.runsv.runsvobj == val2.runsv.runsvobj);

	default:
		return(TRUE);
	}
}

/* compare magnitudes of numbers/strings at top of stack; strcmp-like value */
int runmcmp(runcxdef *ctx)
{
	if (runtostyp(ctx) == DAT_NUMBER)
	{
		long num2 = runpopnum(ctx);
		long num1 = runpopnum(ctx);

		if (num1 > num2) return(1);
		else if (num1 < num2) return(-1);
		else return(0);
	}
	else if (runtostyp(ctx) == DAT_SSTRING)
	{
		uchar *str2 = runpopstr(ctx);
		uchar *str1 = runpopstr(ctx);
		uint   len1 = osrp2(str1) - 2;
		uint   len2 = osrp2(str2) - 2;

		str1 += 2;
		str2 += 2;
		while (len1 && len2)
		{
			if (*str1 < *str2) return(-1);   /* character from 1 is greater */
			else if (*str1 > *str2) return(1);       /* char from 1 is less */

			++str1;
			++str2;
			--len1;
			--len2;
		}
		if (len1) return(1);    /* match up to len2, but string 1 is longer */
		else if (len2) return(-1);  /* match up to len1, but str2 is longer */
		else return(0);                            /* strings are identical */
	}
	else
	{
		runsig(ctx, ERR_INVCMP);
	}
	return 0;
}

/* determine size of a runsdef item */
int runsiz(runsdef *item) {
	switch (item->runstyp) {
	case DAT_NUMBER:
		return(4);
	case DAT_SSTRING:
	case DAT_LIST:
		return(osrp2(item->runsv.runsvstr));
	case DAT_PROPNUM:
	case DAT_OBJECT:
	case DAT_FNADDR:
		return(2);
	default:
		return(0);
	}
}

/* find a sublist within a list */
uchar *runfind(uchar *lst, runsdef *item)
{
	uint len;
	uint curlen;

	for (len = osrp2(lst) - 2, lst += 2; len; lst += curlen, len -= curlen)
	{
		if (*lst == item->runstyp)
		{
			switch (*lst)
			{
			case DAT_LIST:
			case DAT_SSTRING:
				if (osrp2(lst + 1) == osrp2(item->runsv.runsvstr) &&
					!memcmp(lst + 1, item->runsv.runsvstr, (size_t)osrp2(lst + 1)))
					return(lst);
				break;
			case DAT_NUMBER:
				if (osrp4s(lst + 1) == item->runsv.runsvnum)
					return(lst);
				break;

			case DAT_TRUE:
			case DAT_NIL:
				return(lst);

			case DAT_OBJECT:
			case DAT_FNADDR:
				if (osrp2(lst + 1) == item->runsv.runsvobj)
					return(lst);
				break;

			case DAT_PROPNUM:
				if (osrp2(lst + 1) == item->runsv.runsvprp)
					return(lst);
				break;
			}
		}
		curlen = datsiz(*lst, lst + 1) + 1;
	}
	return((uchar *)0);
}

/* add values */
void runadd(runcxdef *ctx, runsdef *val, runsdef *val2, uint below)
{
	if (val->runstyp == DAT_LIST)
	{
		int     len1 = osrp2(val->runsv.runsvstr);
		int     len2 = runsiz(val2);
		int     newlen;

		/* if concatenating a list, take out length + datatype from 2nd */
		if (val2->runstyp == DAT_LIST)
			newlen = len1 + len2 - 2;          /* leave out second list len */
		else
			newlen = len1 + len2 + 1;             /* add in datatype header */

												  /* get space in heap, copy first list, and set new length */
		runhres2(ctx, newlen, below, val, val2);
		memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len1);
		oswp2(ctx->runcxhp, newlen);

		/* append the new element or list of elements */
		if (val2->runstyp == DAT_LIST)
			memcpy(ctx->runcxhp + len1, val2->runsv.runsvstr + 2,
			(size_t)(len2 - 2));
		else
			runputbuf(ctx->runcxhp + len1, val2);

		/* set up return value and update heap pointer */
		val->runsv.runsvstr = ctx->runcxhp;
		ctx->runcxhp += newlen;
	}
	else if (val->runstyp == DAT_SSTRING && val2->runstyp == DAT_SSTRING)
	{
		int len1 = osrp2(val->runsv.runsvstr);
		int len2 = osrp2(val2->runsv.runsvstr);

		/* reserve space, and concatenate the two strings */
		runhres2(ctx, len1 + len2 - 2, below, val, val2);
		memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len1);
		memcpy(ctx->runcxhp + len1, val2->runsv.runsvstr + 2,
			(size_t)len2 - 2);

		/* set length to sum of two lengths, minus 2nd length word */
		oswp2(ctx->runcxhp, len1 + len2 - 2);
		val->runsv.runsvstr = ctx->runcxhp;
		ctx->runcxhp += len1 + len2 - 2;
	}
	else if (val->runstyp == DAT_NUMBER && val2->runstyp == DAT_NUMBER)
		val->runsv.runsvnum += val2->runsv.runsvnum;
	else
		runsig(ctx, ERR_INVADD);
}

/* returns TRUE if value changed */
int runsub(runcxdef *ctx, runsdef *val, runsdef *val2, uint below)
{
	if (val->runstyp == DAT_LIST)
	{
		uchar *sublist;
		int    subsize;
		int    listsize;
		int    part1sz;

		if (val2->runstyp == DAT_LIST)
		{
			uchar *p1;
			uchar *p2;
			uint   rem1;
			uint   rem2;
			uchar *dst;

			/* reserve space for another copy of first list */
			listsize = runsiz(val);
			runhres2(ctx, listsize, below, val, val2);
			dst = ctx->runcxhp + 2;

			/* get pointer to first list */
			p1 = val->runsv.runsvstr;
			rem1 = osrp2(p1) - 2;
			p1 += 2;

			/*
			*   loop through left list, copying elements to output if
			*   not in the right list
			*/
			for (; rem1; lstadv(&p1, &rem1))
			{
				int found = FALSE;

				/* find current element of first list in second list */
				p2 = val2->runsv.runsvstr;
				rem2 = osrp2(p2) - 2;
				p2 += 2;
				for (; rem2; lstadv(&p2, &rem2))
				{
					if (*p1 == *p2)
					{
						int siz1 = datsiz(*p1, p1 + 1);
						int siz2 = datsiz(*p2, p2 + 1);

						if (siz1 == siz2 &&
							(siz1 == 0 || !memcmp(p1 + 1, p2 + 1, (size_t)siz1)))
						{
							found = TRUE;
							break;
						}
					}
				}

				/* if this element wasn't found, copy to output list */
				if (!found)
				{
					uint siz;

					*dst++ = *p1;
					if ((siz = datsiz(*p1, p1 + 1)) != 0)
					{
						memcpy(dst, p1 + 1, siz);
						dst += siz;
					}
				}
			}

			/* we've built the list; write size and we're done */
			oswp2(ctx->runcxhp, dst - ctx->runcxhp);
			val->runsv.runsvstr = ctx->runcxhp;
			ctx->runcxhp = dst;
		}
		else if ((sublist = runfind(val->runsv.runsvstr, val2)) != 0)
		{
			subsize = datsiz(*sublist, sublist + 1) + 1;
			listsize = runsiz(val);
			part1sz = sublist - (uchar *)val->runsv.runsvstr;

			runhres2(ctx, listsize - subsize, below, val, val2);
			memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)part1sz);
			memcpy(ctx->runcxhp + part1sz, sublist + subsize,
				(size_t)(listsize - subsize - part1sz));
			oswp2(ctx->runcxhp, listsize - subsize);
			val->runsv.runsvstr = ctx->runcxhp;
			ctx->runcxhp += listsize - subsize;
		}
		else
		{
			return(FALSE);            /* no change - value can be re-pushed */
		}
	}
	else if (val->runstyp == DAT_NUMBER && val2->runstyp == DAT_NUMBER)
		val->runsv.runsvnum -= val2->runsv.runsvnum;
	else
		runsig(ctx, ERR_INVSUB);

	return(TRUE);                 /* value has changed; must be pushed anew */
}

/* return code pointer offset */
static uint runcpsav(runcxdef *ctx, uchar *noreg *cp, objnum obj, prpnum prop)
{
	uint ofs;

	VARUSED(prop);

	/* get offset from start of object */
	ofs = *cp - mcmobjptr(ctx->runcxmem, (mcmon)obj);

	/* clear the pointer so the caller knows the object is unlocked */
	*cp = 0;

	/* unlock the object, and return the derived offset */
	mcmunlck(ctx->runcxmem, (mcmon)obj);
	return(ofs);
}

/* restore code pointer based on object.property */
uchar *runcprst(runcxdef *ctx, uint ofs, objnum obj, prpnum prop)
{
	uchar *ptr;

	VARUSED(prop);

	/* lock object, and get pointer based on offset */
	ptr = mcmlck(ctx->runcxmem, (mcmon)obj) + ofs;

	return(ptr);
}

/* get offset of an element within a list */
static uint runindofs(runcxdef *ctx, uint indx, uchar *lstp)
{
	uint   lstsiz;
	uchar *orgp = lstp;

	/* verify that index is in range */
	if (indx <= 0) runsig(ctx, ERR_LOWINX);

	/* get list's size, and point to its data string */
	lstsiz = osrp2(lstp) - 2;
	lstp += 2;

	/* skip the first indx-1 elements */
	for (--indx; indx && lstsiz; --indx) lstadv(&lstp, &lstsiz);

	/* if we ran out of list, the index is out of range */
	if (!lstsiz) runsig(ctx, ERR_HIGHINX);

	/* return the offset */
	return((uint)(lstp - orgp));
}

/* push an indexed element of a list; index is tos, list is next on stack */
static void runpind(runcxdef *ctx, uint indx, uchar *lstp)
{
	uchar   *ele;
	runsdef  val;

	/* find the element we want to push */
	ele = lstp + runindofs(ctx, indx, lstp);

	/* reserve space first, in case lstp gets moved around */
	val.runstyp = DAT_LIST;
	val.runsv.runsvstr = lstp;
	runhres1(ctx, datsiz(*ele, ele + 1), 0, &val);
	if (val.runsv.runsvstr != lstp)
		ele = val.runsv.runsvstr + runindofs(ctx, indx, val.runsv.runsvstr);

	/* push the operand */
	runpbuf(ctx, *ele, ele + 1);
}

/*
*   Check a property to ensure that it's a data property.  Throws an
*   error if the property contains a method.  This is used for debugger
*   speculative evaluation to ensure that we don't call any methods from
*   within speculative expressions.
*/
static void runcheckpropdata(runcxdef *ctx, objnum obj, prpnum prop)
{
	uint    pofs;
	objnum  target;
	objdef *objptr;
	prpdef *prpptr;
	int     typ;

	/* if the object is invalid, it's an error */
	if (obj == MCMONINV)
		errsig(ctx->runcxerr, ERR_REQVOB);

	/* get the property */
	pofs = objgetap(ctx->runcxmem, obj, prop, &target, FALSE);

	/* if there's no property, it's okay - it will just return nil */
	if (pofs == 0)
		return;

	/* get the object */
	objptr = mcmlck(ctx->runcxmem, target);

	/* get the property */
	prpptr = (prpdef *)(((uchar *)objptr) + pofs);
	typ = prptype(prpptr);

	/* we're done with the object's memory now */
	mcmunlck(ctx->runcxmem, target);

	/* check the type */
	switch (typ)
	{
	case DAT_CODE:
	case DAT_DSTRING:
		/*
		*   we can't call code or evaluate (i.e., print) double-quoted
		*   strings during speculative evaluation
		*/
		errsig(ctx->runcxerr, ERR_RTBADSPECEXPR);

	default:
		/* other types do not involve method calls, so they're okay */
		break;
	}
}

/* push an object's property */
void runpprop(runcxdef *ctx, uchar *noreg *codepp,
	objnum callobj, prpnum callprop,
	noreg objnum obj, prpnum prop, int inh, int argc, objnum self)
{
	uint     pofs;
	uint     saveofs = 0;
	objdef  *objptr;
	prpdef  *prpptr;
	uchar   *val;
	int      typ;
	runsdef  sval;
	objnum   target;
	int      times_through = 0;
	int      err;
	objnum   otherobj = 0;

	NOREG((&obj, &codepp));

	if (obj == MCMONINV) runsig(ctx, ERR_RUNNOBJ);

startover:
	pofs = objgetap(ctx->runcxmem, obj, prop, &target, inh);

	/* if nothing was found, push nil */
	if (!pofs)
	{
		runpush(ctx, DAT_NIL, &sval);
		return;
	}

	/* found a property; get the prpdef, and the value and type of data */
	objptr = mcmlck(ctx->runcxmem, target);
	ERRBEGIN(ctx->runcxerr)         /* catch errors so we can unlock object */

		prpptr = (prpdef *)(((uchar *)objptr) + pofs);
	val = prpvalp(prpptr);
	typ = prptype(prpptr);

	/* determine what to do based on property type */
	switch (typ)
	{
	case DAT_CODE:
		/* save caller's code offset - caller's object may move */
		if (codepp)
			saveofs = runcpsav(ctx, codepp, callobj, callprop);

		/* execute the code */
		runexe(ctx, val, self, target, prop, argc);

		/* restore caller's code pointer in case object moved */
		if (codepp)
			*codepp = runcprst(ctx, saveofs, callobj, callprop);
		break;

	case DAT_REDIR:
		otherobj = osrp2(val);
		break;

	case DAT_DSTRING:
		outfmt(ctx->runcxtio, val);
		break;

	case DAT_DEMAND:
		break;

	default:
		runpbuf(ctx, typ, val);
		break;
	}

	/* we're done - unlock the object */
	mcmunlck(ctx->runcxmem, target);

	/* if it's redirected, redirect it now */
	if (typ == DAT_REDIR)
	{
		runpprop(ctx, codepp, callobj, callprop, otherobj, prop,
			FALSE, argc, otherobj);
	}

	/* if an error occurs, unlock the object, and resignal the error */
	ERRCATCH(ctx->runcxerr, err)
		mcmunlck(ctx->runcxmem, target);
	if (err < ERR_RUNEXIT || err > ERR_RUNEXITOBJ)
		dbgdump(ctx->runcxdbg);                       /* dump the stack */
	errrse(ctx->runcxerr);
	ERREND(ctx->runcxerr)

		/* apply special handling for set-on-first-use data */
		if (typ == DAT_DEMAND)
		{
			/*
			*   if we've already done this, the property isn't being set by
			*   the callback, so we'll never get out of this loop - abort if
			*   so
			*/
			if (++times_through != 1)
				runsig(ctx, ERR_DMDLOOP);

			/* save caller's code offset - caller's object may move */
			if (codepp)
				saveofs = runcpsav(ctx, codepp, callobj, callprop);

			/* invoke the callback to set the property on demand */
			(*ctx->runcxdmd)(ctx->runcxdmc, obj, prop);

			/* restore caller's code pointer */
			if (codepp)
				*codepp = runcprst(ctx, saveofs, callobj, callprop);

			/* try again now that it's been set up */
			goto startover;
		}
}

/* ======================================================================== */
/*
*   user exit callbacks
*/

/* External fnctions are now obsolete */
#if 0
static int runuftyp(runuxdef *ctx)
{
	return(runtostyp(ctx->runuxctx));
}

static long runufnpo(runuxdef *ctx)
{
	return(runpopnum(ctx->runuxctx));
}

static uchar *runufspo(runuxdef *ctx)
{
	return(runpopstr(ctx->runuxctx));
}

static void runufdsc(runuxdef *ctx)
{
	rundisc(ctx->runuxctx);
}

static void runufnpu(runuxdef *ctx, long num)
{
	runpnum(ctx->runuxctx, num);
}

static void runufspu(runuxdef *ctx, uchar *str)
{
	runsdef val;

	val.runstyp = DAT_SSTRING;
	val.runsv.runsvstr = str - 2;
	runrepush(ctx->runuxctx, &val);
}

static void runufcspu(runuxdef *ctx, char *str)
{
	runpstr(ctx->runuxctx, str, (int)strlen(str), ctx->runuxargc);
}

static uchar *runufsal(runuxdef *ctx, int len)
{
	uchar *ret;

	len += 2;
	runhres(ctx->runuxctx, len, ctx->runuxargc);
	ret = ctx->runuxctx->runcxhp;
	oswp2(ret, len);
	ret += 2;

	ctx->runuxctx->runcxhp += len;
	return(ret);
}

static void runuflpu(runuxdef *ctx, int typ)
{
	runsdef val;

	val.runstyp = typ;
	runrepush(ctx->runuxctx, &val);
}

#endif

/* convert an osrp2 value to a signed short value */
#define runrp2s(p) ((short)(ushort)osrp2(p))


/* ======================================================================== */
/*
*   execute p-code
*/
void runexe(runcxdef *ctx, uchar *p0, objnum self, objnum target,
	prpnum targprop, int argc)
{
	uchar    *noreg p = p0;
	uchar     opc;                     /* opcode we're currently working on */
	runsdef   val;                           /* stack element (for pushing) */
	runsdef   val2;     /* another one (for popping in two-op instructions) */
	uint      ofs;                   /* offset in code of current execution */
	prpnum    prop;                         /* property number, when needed */
	objnum    obj;                            /* object number, when needed */
	runsdef  *noreg rstsp;        /* sp to reset to on DISCARD instructions */
	uchar    *lstp;                                         /* list pointer */
	int       nargc;                   /* argument count of called function */
	runsdef  *valp;
	runsdef  *stkval;
	int       i;
	int       brkchk;

#ifndef DBG_OFF
	int       err;
#endif

	NOREG((&rstp, &p));

	/* save entry SP - this is reset point until ENTER */
	rstsp = ctx->runcxsp;

#ifndef DBG_OFF
	/*
	*   For the debugger's sake, set up an error frame so that we catch
	*   any errors thrown during p-code execution within this function.
	*   If an error occurs, and the debugger is present, we'll set the
	*   instruction pointer back to the start of the line that caused the
	*   error and enter the debugger with the error indication.  If the
	*   debugger isn't present, we'll simply re-throw the error.  This
	*   entire block can be compiled out of the execution engine when
	*   linking a stand-alone (non-debug) version of the run-time.
	*/
resume_from_error:
	ERRBEGIN(ctx->runcxerr)
#endif /* DBG_OFF */

		for (brkchk = 0;; ++brkchk)
		{
			/* check for break - signal if user has hit break */
			if (brkchk == 1000)
			{
				brkchk = 0;
				if (os_break()) runsig(ctx, ERR_USRINT);
			}

			opc = *p++;

			switch (opc)
			{
			case OPCPUSHNUM:
				val.runsv.runsvnum = osrp4s(p);
				runpush(ctx, DAT_NUMBER, &val);
				p += 4;
				break;

			case OPCPUSHOBJ:
				val.runsv.runsvobj = osrp2(p);
				runpush(ctx, DAT_OBJECT, &val);
				p += 2;
				break;

			case OPCPUSHSELF:
				val.runsv.runsvobj = self;
				runpush(ctx, DAT_OBJECT, &val);
				break;

			case OPCPUSHSTR:
				val.runsv.runsvstr = p;
				runpush(ctx, DAT_SSTRING, &val);
				p += osrp2(p);                              /* skip past string */
				break;

			case OPCPUSHLST:
				val.runsv.runsvstr = p;
				runpush(ctx, DAT_LIST, &val);
				p += osrp2(p);                                /* skip past list */
				break;

			case OPCPUSHNIL:
				runpush(ctx, DAT_NIL, &val);
				break;

			case OPCPUSHTRUE:
				runpush(ctx, DAT_TRUE, &val);
				break;

			case OPCPUSHFN:
				val.runsv.runsvobj = osrp2(p);
				runpush(ctx, DAT_FNADDR, &val);
				p += 2;
				break;

			case OPCPUSHPN:
				val.runsv.runsvprp = osrp2(p);
				runpush(ctx, DAT_PROPNUM, &val);
				p += 2;
				break;

			case OPCNEG:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = -runpopnum(ctx);
				runrepush(ctx, &val);
				break;

			case OPCBNOT:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = ~runpopnum(ctx);
				runrepush(ctx, &val);
				break;

			case OPCNOT:
				if (runtoslog(ctx))
					runpush(ctx, runclog(!runpoplog(ctx)), &val);
				else
					runpush(ctx, runclog(runpopnum(ctx)), &val);
				break;

			case OPCADD:
				runpop(ctx, &val2);    /* right op is pushed last -> popped 1st */
				runpop(ctx, &val);
				runadd(ctx, &val, &val2, 2);
				runrepush(ctx, &val);
				break;

			case OPCSUB:
				runpop(ctx, &val2);    /* right op is pushed last -> popped 1st */
				runpop(ctx, &val);
				(void)runsub(ctx, &val, &val2, 2);
				runrepush(ctx, &val);
				break;

			case OPCMUL:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = runpopnum(ctx);
				val.runsv.runsvnum *= runpopnum(ctx);
				runrepush(ctx, &val);
				break;

			case OPCBAND:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = runpopnum(ctx);
				val.runsv.runsvnum &= runpopnum(ctx);
				runrepush(ctx, &val);
				break;

			case OPCBOR:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = runpopnum(ctx);
				val.runsv.runsvnum |= runpopnum(ctx);
				runrepush(ctx, &val);
				break;

			case OPCSHL:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = runpopnum(ctx);
				val.runsv.runsvnum = runpopnum(ctx) << val.runsv.runsvnum;
				runrepush(ctx, &val);
				break;

			case OPCSHR:
				val.runstyp = DAT_NUMBER;
				val.runsv.runsvnum = runpopnum(ctx);
				val.runsv.runsvnum = runpopnum(ctx) >> val.runsv.runsvnum;
				runrepush(ctx, &val);
				break;

			case OPCXOR:
				/* allow logical ^ logical or number ^ number */
				if (runtoslog(ctx))
				{
					int a, b;

					/* logicals - return a logical value */
					a = runpoplog(ctx);
					b = runpoplog(ctx);
					val.runstyp = runclog(a ^ b);
				}
				else
				{
					/* numeric value - return binary xor */
					val.runstyp = DAT_NUMBER;
					val.runsv.runsvnum = runpopnum(ctx);
					val.runsv.runsvnum ^= runpopnum(ctx);
				}
				runrepush(ctx, &val);
				break;

			case OPCDIV:
				val.runsv.runsvnum = runpopnum(ctx);
				if (val.runsv.runsvnum == 0)
					runsig(ctx, ERR_DIVZERO);
				val.runsv.runsvnum = runpopnum(ctx) / val.runsv.runsvnum;
				val.runstyp = DAT_NUMBER;
				runrepush(ctx, &val);
				break;

			case OPCMOD:
				val.runsv.runsvnum = runpopnum(ctx);
				if (val.runsv.runsvnum == 0)
					runsig(ctx, ERR_DIVZERO);
				val.runsv.runsvnum = runpopnum(ctx) % val.runsv.runsvnum;
				val.runstyp = DAT_NUMBER;
				runrepush(ctx, &val);
				break;

#ifdef NEVER
			case OPCAND:
				if (runtostyp(ctx) == DAT_LIST)
					runlstisect(ctx);
				else
					runpush(ctx, runclog(runpoplog(ctx) && runpoplog(ctx)), &val);
				break;

			case OPCOR:
				runpush(ctx, runclog(runpoplog(ctx) || runpoplog(ctx)), &val);
				break;
#endif /* NEVER */

			case OPCEQ:
				runpush(ctx, runclog(runeq(ctx)), &val);
				break;

			case OPCNE:
				runpush(ctx, runclog(!runeq(ctx)), &val);
				break;

			case OPCLT:
				runpush(ctx, runclog(runmcmp(ctx) < 0), &val);
				break;

			case OPCLE:
				runpush(ctx, runclog(runmcmp(ctx) <= 0), &val);
				break;

			case OPCGT:
				runpush(ctx, runclog(runmcmp(ctx) > 0), &val);
				break;

			case OPCGE:
				runpush(ctx, runclog(runmcmp(ctx) >= 0), &val);
				break;

			case OPCCALL:
			{
				objnum o;

				/* get the argument count */
				nargc = *p++;

				/* ensure we have enough values to pass as arguments */
				runcheckargc(ctx, &nargc);

				/* object could move--save offset to restore 'p' after call */
				o = osrp2(p);
				ofs = runcpsav(ctx, &p, target, targprop);

				/* execute the function */
				runfn(ctx, o, nargc);

				/* restore code pointer in case target object moved */
				p = runcprst(ctx, ofs, target, targprop) + 2;
				break;
			}

			case OPCGETP:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = osrp2(p);
				p += 2;
				obj = runpopobj(ctx);
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
					obj);
				break;

			case OPCGETPDATA:
				prop = osrp2(p);
				p += 2;
				obj = runpopobj(ctx);
				runcheckpropdata(ctx, obj, prop);
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE, 0, obj);
				break;

			case OPCGETDBLCL:
#ifdef DBG_OFF
				/* non-debug mode - this will always throw an error */
				dbgfrfind(ctx->runcxdbg, 0, 0);
#else
				/* debug mode - look up the local in the stack frame */
				{
					objnum   frobj;
					uint     frofs;
					runsdef *otherbp;

					frobj = osrp2(p);
					frofs = osrp2(p + 2);
					otherbp = dbgfrfind(ctx->runcxdbg, frobj, frofs);
					runrepush(ctx, otherbp + runrp2s(p + 4) - 1);
					p += 6;
				}
				break;
#endif

			case OPCGETLCL:
				runrepush(ctx, ctx->runcxbp + runrp2s(p) - 1);
				p += 2;
				break;

			case OPCRETURN:
				runleave(ctx, argc /* was: osrp2(p) */);
				dbgleave(ctx->runcxdbg, DBGEXRET);
				goto done;

			case OPCRETVAL:
				/* if there's nothing on the stack, return nil */
				if (runtostyp(ctx) != DAT_BASEPTR)
					runpop(ctx, &val);
				else
					val.runstyp = DAT_NIL;

				runleave(ctx, argc /* was: osrp2(p) */);
				runrepush(ctx, &val);
				dbgleave(ctx->runcxdbg, DBGEXVAL);
				goto done;

			case OPCENTER:
				/* push old base pointer and set up new one */
				ctx->runcxsp = rstsp;
				val.runsv.runsvstr = (uchar *)ctx->runcxbp;
				runpush(ctx, DAT_BASEPTR, &val);
				ctx->runcxbp = ctx->runcxsp;

				/* add a trace record */
				dbgenter(ctx->runcxdbg, ctx->runcxbp, self, target, targprop,
					0, argc);

				/* initialize locals to nil */
				for (i = osrp2(p); i; --i) runpush(ctx, DAT_NIL, &val);
				p += 2;                         /* skip the local count operand */

												/* save stack pointer - reset sp to this value on DISCARD */
				rstsp = ctx->runcxsp;
				break;

			case OPCDISCARD:
				ctx->runcxsp = rstsp;
				break;

			case OPCSWITCH:
			{
				int      tostyp;
				int      match, typmatch;

				runpop(ctx, &val);
				tostyp = val.runstyp;
				switch (tostyp)
				{
				case DAT_SSTRING:
					tostyp = OPCPUSHSTR;
					break;
				case DAT_LIST:
					tostyp = OPCPUSHLST;
					break;
				case DAT_PROPNUM:
					tostyp = OPCPUSHPN;
					break;
				case DAT_FNADDR:
					tostyp = OPCPUSHFN;
					break;
				case DAT_TRUE:
					tostyp = OPCPUSHTRUE;
					break;
				case DAT_NIL:
					tostyp = OPCPUSHNIL;
					break;
				}

				p += osrp2(p);                         /* find the switch table */
				i = osrp2(p);                            /* get number of cases */

														 /* look for a matching case */
				for (match = FALSE; i && !match; --i)
				{
					p += 2;                     /* skip previous jump/size word */
					typmatch = (*p == tostyp);
					switch (*p++)
					{
					case OPCPUSHNUM:
						match = (typmatch
							&& val.runsv.runsvnum == osrp4s(p));
						p += 4;
						break;

					case OPCPUSHLST:
					case OPCPUSHSTR:
						match = (typmatch
							&& osrp2(val.runsv.runsvstr) == osrp2(p)
							&& !memcmp(val.runsv.runsvstr,
								p, (size_t)osrp2(p)));
						p += runrp2s(p);
						break;

					case OPCPUSHPN:
						match = (typmatch
							&& val.runsv.runsvprp == osrp2(p));
						p += 2;
						break;

					case OPCPUSHOBJ:
					case OPCPUSHFN:
						match = (typmatch
							&& val.runsv.runsvobj == osrp2(p));
						p += 2;
						break;

					case OPCPUSHSELF:
						match = (typmatch && val.runsv.runsvobj == self);
						break;

					case OPCPUSHTRUE:
					case OPCPUSHNIL:
						match = typmatch;
						break;
					}
				}

				if (!match) p += 2;         /* if default, skip to default case */
				p += runrp2s(p);      /* wherever we left off, p points to jump */
				break;
			}

			case OPCJMP:
				p += runrp2s(p);
				break;

			case OPCJT:
				if (runtoslog(ctx))
					p += (runpoplog(ctx) ? runrp2s(p) : 2);
				else
					p += (runpopnum(ctx) != 0 ? runrp2s(p) : 2);
				break;

			case OPCJF:
				if (runtoslog(ctx))
					p += ((!runpoplog(ctx)) ? runrp2s(p) : 2);
				else if (runtostyp(ctx) == DAT_NUMBER)
					p += ((runpopnum(ctx) == 0) ? runrp2s(p) : 2);
				else                      /* consider any other type to be true */
				{
					rundisc(ctx);  /* throw away the item considered to be true */
					p += 2;
				}
				break;

			case OPCSAY:
				outfmt(ctx->runcxtio, p);
				p += osrp2(p);                              /* skip past string */
				break;

			case OPCBUILTIN:
			{
				int      binum;
				runsdef *stkp;

				nargc = *p++;
				runcheckargc(ctx, &nargc);
				binum = osrp2(p);
				ofs = runcpsav(ctx, &p, target, targprop);
				stkp = ctx->runcxsp - nargc;

				dbgenter(ctx->runcxdbg, ctx->runcxsp + 1, MCMONINV, MCMONINV,
					(prpnum)0, binum, nargc);
				(*ctx->runcxbi[binum])((struct bifcxdef *)ctx->runcxbcx,
					nargc);
				dbgleave(ctx->runcxdbg,
					ctx->runcxsp != stkp ? DBGEXVAL : DBGEXRET);

				p = runcprst(ctx, ofs, target, targprop);
				p += 2;
				break;
			}

			case OPCPTRCALL:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				ofs = runcpsav(ctx, &p, target, targprop);
				runfn(ctx, runpopfn(ctx), nargc);
				p = runcprst(ctx, ofs, target, targprop);
				break;

			case OPCINHERIT:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = osrp2(p);
				p += 2;
				runpprop(ctx, &p, target, targprop, target, prop, TRUE, nargc,
					self);
				break;

			case OPCPTRINH:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = runpopprp(ctx);
				runpprop(ctx, &p, target, targprop, target, prop, TRUE, nargc,
					self);
				break;

			case OPCPTRGETP:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = runpopprp(ctx);
				obj = runpopobj(ctx);
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
					obj);
				break;

			case OPCPTRGETPDATA:
				prop = runpopprp(ctx);
				obj = runpopobj(ctx);
				runcheckpropdata(ctx, obj, prop);
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE, 0, obj);
				break;

			case OPCEXPINH:
				/* inheritance from explicit superclass */
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = osrp2(p);
				obj = osrp2(p + 2);
				p += 4;

				/*
				*   Evaluate the property of the given object, but keeping
				*   the same 'self' as is currently in effect.  Note that the
				*   'inherit' flag is FALSE in this call, even though we're
				*   inheriting, because the opcode explicitly specifies the
				*   object we want to inherit from.
				*/
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE,
					nargc, self);
				break;

			case OPCEXPINHPTR:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = runpopprp(ctx);
				obj = osrp2(p);
				p += 2;
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE,
					nargc, self);
				break;

			case OPCPASS:
				prop = osrp2(p);
				runleave(ctx, 0);
				dbgleave(ctx->runcxdbg, DBGEXPASS);
				runpprop(ctx, &p, target, targprop, target, prop, TRUE, argc,
					self);
				goto done;

			case OPCEXIT:
				errsig(ctx->runcxerr, ERR_RUNEXIT);
				/* NOTREACHED */

			case OPCABORT:
				errsig(ctx->runcxerr, ERR_RUNABRT);
				/* NOTREACHED */

			case OPCASKDO:
				errsig(ctx->runcxerr, ERR_RUNASKD);
				/* NOTREACHED */

			case OPCASKIO:
				errsig1(ctx->runcxerr, ERR_RUNASKI, ERRTINT, osrp2(p));
				/* NOTREACHED */

			case OPCJE:
				p += (runeq(ctx) ? runrp2s(p) : 2);
				break;

			case OPCJNE:
				p += (!runeq(ctx) ? runrp2s(p) : 2);
				break;

			case OPCJGT:
				p += (runmcmp(ctx) > 0 ? runrp2s(p) : 2);
				break;

			case OPCJGE:
				p += (runmcmp(ctx) >= 0 ? runrp2s(p) : 2);
				break;

			case OPCJLT:
				p += (runmcmp(ctx) < 0 ? runrp2s(p) : 2);
				break;

			case OPCJLE:
				p += (runmcmp(ctx) <= 0 ? runrp2s(p) : 2);
				break;

			case OPCJNAND:
				p += (!(runpoplog(ctx) && runpoplog(ctx)) ? runrp2s(p) : 2);
				break;

			case OPCJNOR:
				p += (!(runpoplog(ctx) || runpoplog(ctx)) ? runrp2s(p) : 2);
				break;

			case OPCGETPSELF:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = osrp2(p);
				p += 2;
				runpprop(ctx, &p, target, targprop, self, prop, FALSE, nargc,
					self);
				break;

			case OPCGETPSELFDATA:
				prop = osrp2(p);
				p += 2;
				runcheckpropdata(ctx, self, prop);
				runpprop(ctx, &p, target, targprop, self, prop, FALSE, 0, self);
				break;

			case OPCGETPPTRSELF:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				prop = runpopprp(ctx);
				runpprop(ctx, &p, target, targprop, self, prop, FALSE, nargc,
					self);
				break;

			case OPCGETPOBJ:
				nargc = *p++;
				runcheckargc(ctx, &nargc);
				obj = osrp2(p);
				prop = osrp2(p + 2);
				p += 4;
				runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
					obj);
				break;

			case OPCINDEX:
				i = runpopnum(ctx);                                /* get index */
				lstp = runpoplst(ctx);                          /* get the list */
				runpind(ctx, i, lstp);
				break;

			case OPCJST:
				if (runtostyp(ctx) == DAT_TRUE)
					p += runrp2s(p);
				else
				{
					(void)runpoplog(ctx);
					p += 2;
				}
				break;

			case OPCJSF:
				if (runtostyp(ctx) == DAT_NIL ||
					(runtostyp(ctx) == DAT_NUMBER &&
					(ctx->runcxsp - 1)->runsv.runsvnum == 0))
					p += runrp2s(p);
				else
				{
					runpop(ctx, &val);
					p += 2;
				}
				break;

			case OPCCALLEXT:
			{
#if 0 // external functions are now obsolete
				static runufdef uf =
				{
					runuftyp,  runufnpo,  runufspo,  runufdsc,
					runufnpu,  runufspu,  runufcspu, runufsal,
					runuflpu
				};
				int        fn;
				runxdef   *ex;

				runuxdef   ux;

				/* set up callback context */
				ux.runuxctx = ctx;
				ux.runuxvec = &uf;
				ux.runuxargc = *p++;

				fn = osrp2(p);
				p += 2;
				ex = &ctx->runcxext[fn];

				if (!ex->runxptr)
				{
					if ((ex->runxptr = os_exfil(ex->runxnam)) == 0)
						runsig1(ctx, ERR_EXTLOAD, ERRTSTR, ex->runxnam);
				}
				if (os_excall(ex->runxptr, &ux))
					runsig1(ctx, ERR_EXTRUN, ERRTSTR, ex->runxnam);
#else
				/* external functions are obsolete - throw an error */
				runxdef *ex;
				p += 1;
				ex = &ctx->runcxext[osrp2(p)];
				p += 2;
				runsig1(ctx, ERR_EXTRUN, ERRTSTR, ex->runxnam);
#endif
			}
			break;

			case OPCDBGRET:
				goto done;

			case OPCCONS:
			{
				uint    totsiz;
				uint    oldsiz;
				uint    tot;
				uint    cursiz;
				runsdef lstend;

				tot = i = osrp2(p);    /* get # of items to build into list */
				p += 2;

				/* reserve space for initial list (w/length word only) */
				runhres(ctx, 2, 0);

				/*
				*   Set up value to point to output list, making room
				*   for length prefix.  Remember size-so-far separately.
				*/
				lstend.runstyp = DAT_LIST;
				lstend.runsv.runsvstr = ctx->runcxhp;
				ctx->runcxhp += 2;
				totsiz = 2;

				while (i--)
				{
					runpop(ctx, &val);          /* get next value off stack */
					cursiz = runsiz(&val);

					/*
					*   Set up to allocate space.  Before doing so, make
					*   sure the list under construction is valid, to
					*   ensure that it stays around after garbage
					*   collection.
					*/
					oldsiz = totsiz;
					totsiz += cursiz + 1;
					oswp2(lstend.runsv.runsvstr, oldsiz);
					ctx->runcxhp = lstend.runsv.runsvstr + oldsiz;
					runhres2(ctx, cursiz + 1, tot - i, &val, &lstend);

					/* write this item to the list */
					runputbuf(lstend.runsv.runsvstr + oldsiz, &val);
				}
				oswp2(lstend.runsv.runsvstr, totsiz);
				ctx->runcxhp = lstend.runsv.runsvstr + totsiz;
				runrepush(ctx, &lstend);
			}
			break;

			case OPCARGC:
				val.runsv.runsvnum = argc;
				runpush(ctx, DAT_NUMBER, &val);
				break;

			case OPCCHKARGC:
				if ((*p & 0x80) ? argc < (*p & 0x7f) : argc != *p)
				{
					char namebuf[128];
					size_t namelen;

					/*
					*   debugger is present - look up the name of the current
					*   function or method, so that we can report it in the
					*   error message
					*/
					if (targprop == 0)
					{
						/* we're in a function */
						namelen = dbgnam(ctx->runcxdbg, namebuf, TOKSTFUNC,
							target);
					}
					else
					{
						/* we're in an object.method */
						namelen = dbgnam(ctx->runcxdbg, namebuf, TOKSTOBJ,
							target);
						namebuf[namelen++] = '.';
						namelen += dbgnam(ctx->runcxdbg, namebuf + namelen,
							TOKSTPROP, targprop);
					}
					namebuf[namelen] = '\0';
					runsig1(ctx, ERR_ARGC, ERRTSTR, namebuf);
				}
				++p;
				break;

			case OPCLINE:
			case OPCBP:
			{
				uchar *ptr = mcmobjptr(ctx->runcxmem, (mcmon)target);
				uchar  instr;

				/* set up the debugger frame record for this line */
				dbgframe(ctx->runcxdbg, osrp2(p + 1), p - ptr);

				/* remember the instruction */
				instr = *(p - 1);

				/* remember the offset of the line record */
				ctx->runcxlofs = ofs = (p + 2 - ptr);

				/* skip to the next instruction */
				p += *p;

				/* let the debugger take over, if it wants to */
				dbgssi(ctx->runcxdbg, ofs, instr, 0, &p);
				break;
			}

			case OPCFRAME:
				/* this is a frame record - just jump past it */
				p += osrp2(p);
				break;

			case OPCASI_MASK | OPCASIDIR | OPCASILCL:
				runpop(ctx, &val);
				OSCPYSTRUCT(*(ctx->runcxbp + runrp2s(p) - 1), val);
				stkval = &val;
				p += 2;
				goto no_assign;

			case OPCASI_MASK | OPCASIDIR | OPCASIPRP:
				obj = runpopobj(ctx);
				prop = osrp2(p);
				p += 2;
				runpop(ctx, &val);
				stkval = valp = &val;
				goto assign_property;

			case OPCASI_MASK | OPCASIDIR | OPCASIPRPPTR:
				prop = runpopprp(ctx);
				obj = runpopobj(ctx);
				runpop(ctx, &val);
				stkval = valp = &val;
				goto assign_property;

			case OPCNEW:
				run_new(ctx, &p, target, targprop);
				break;

			case OPCDELETE:
				run_delete(ctx, &p, target, targprop);
				break;

			default:
				if ((opc & OPCASI_MASK) == OPCASI_MASK)
				{
					runsdef  val3;
					int      asityp;
					int      asiext;
					int      lclnum;

					valp = &val;
					stkval = &val;

					asityp = (opc & OPCASITYP_MASK);
					if (asityp == OPCASIEXT)
						asiext = *p++;

					/* get list element/property number if needed */
					switch (opc & OPCASIDEST_MASK)
					{
					case OPCASIPRP:
						obj = runpopobj(ctx);
						prop = osrp2(p);
						p += 2;
						break;

					case OPCASIPRPPTR:
						prop = runpopprp(ctx);
						obj = runpopobj(ctx);
						break;

					case OPCASIIND:
						i = runpopnum(ctx);
						lstp = runpoplst(ctx);
						break;

					case OPCASILCL:
						lclnum = runrp2s(p);
						p += 2;
						break;
					}

					if (asityp != OPCASIDIR)
					{
						/* we have an <op>= operator - get lval, modify, & set */
						switch (opc & OPCASIDEST_MASK)
						{
						case OPCASILCL:
							OSCPYSTRUCT(val, *(ctx->runcxbp + lclnum - 1));
							break;

						case OPCASIPRP:
						case OPCASIPRPPTR:
							runpprop(ctx, &p, target, targprop, obj, prop,
								FALSE, 0, obj);
							runpop(ctx, &val);
							break;

						case OPCASIIND:
							runpind(ctx, i, lstp);
							runpop(ctx, &val);
							break;
						}

						/* if saving pre-inc/dec value, get the value now */
						if ((opc & OPCASIPRE_MASK) == OPCASIPOST)
						{
							OSCPYSTRUCT(val3, val);
							stkval = &val3;
						}
					}

					/* get rvalue, except for inc/dec operations */
					if (asityp != OPCASIINC && asityp != OPCASIDEC)
						runpop(ctx, &val2);

					/* now apply operation to lvalue using rvalue */
					switch (asityp)
					{
					case OPCASIADD:
						if ((opc & OPCASIIND) != 0)
						{
							runsdef val4;

							/*
							*   we're adding to an indexed value out of a list -
							*   we need to make sure the list is protected from
							*   garbage collection, so push it back on the stack
							*   while we're working
							*/
							val4.runstyp = DAT_LIST;
							val4.runsv.runsvstr = lstp;
							runrepush(ctx, &val4);

							/* carry out the addition */
							runadd(ctx, &val, &val2, 2);

							/*
							*   in case the list got moved during garbage
							*   collection, retrieve it from the stack
							*/
							lstp = runpoplst(ctx);
						}
						else
						{
							/* no list indexing - just carry out the addition */
							runadd(ctx, &val, &val2, 2);
						}
						break;

					case OPCASISUB:
						if ((opc & OPCASIIND) != 0)
						{
							runsdef val4;
							int result;

							/* as with adding, protect the list from GC */
							val4.runstyp = DAT_LIST;
							val4.runsv.runsvstr = lstp;
							runrepush(ctx, &val4);

							/* carry out the subtraction and note the result */
							result = runsub(ctx, &val, &val2, 2);

							/* recover the list pointer */
							lstp = runpoplst(ctx);

							/* check to see if we have an assignment */
							if (!result)
								goto no_assign;
						}
						else
						{
							/* no list indexing - just do the subtraction */
							if (!runsub(ctx, &val, &val2, 2))
								goto no_assign;
						}
						break;

					case OPCASIMUL:
						if (val.runstyp != DAT_NUMBER
							|| val2.runstyp != DAT_NUMBER)
							runsig(ctx, ERR_REQNUM);
						val.runsv.runsvnum *= val2.runsv.runsvnum;
						break;

					case OPCASIDIV:
						if (val.runstyp != DAT_NUMBER
							|| val2.runstyp != DAT_NUMBER)
							runsig(ctx, ERR_REQNUM);
						if (val2.runsv.runsvnum == 0)
							runsig(ctx, ERR_DIVZERO);
						val.runsv.runsvnum /= val2.runsv.runsvnum;
						break;

					case OPCASIINC:
						if (val.runstyp != DAT_NUMBER)
							runsig(ctx, ERR_REQNUM);
						++(val.runsv.runsvnum);
						break;

					case OPCASIDEC:
						if (val.runstyp != DAT_NUMBER)
							runsig(ctx, ERR_REQNUM);
						--(val.runsv.runsvnum);
						break;

					case OPCASIDIR:
						valp = stkval = &val2;
						break;

					case OPCASIEXT:
						switch (asiext)
						{
						case OPCASIMOD:
							if (val.runstyp != DAT_NUMBER
								|| val2.runstyp != DAT_NUMBER)
								runsig(ctx, ERR_REQNUM);
							if (val2.runsv.runsvnum == 0)
								runsig(ctx, ERR_DIVZERO);
							val.runsv.runsvnum %= val2.runsv.runsvnum;
							break;

						case OPCASIBAND:
							if ((val.runstyp == DAT_TRUE
								|| val.runstyp == DAT_NIL)
								&& (val2.runstyp == DAT_TRUE
									|| val2.runstyp == DAT_NIL))
							{
								int a, b;

								a = (val.runstyp == DAT_TRUE ? 1 : 0);
								b = (val2.runstyp == DAT_TRUE ? 1 : 0);
								val.runstyp = runclog(a && b);
							}
							else if (val.runstyp == DAT_NUMBER
								&& val2.runstyp == DAT_NUMBER)
								val.runsv.runsvnum &= val2.runsv.runsvnum;
							else
								runsig(ctx, ERR_REQNUM);
							break;

						case OPCASIBOR:
							if ((val.runstyp == DAT_TRUE
								|| val.runstyp == DAT_NIL)
								&& (val2.runstyp == DAT_TRUE
									|| val2.runstyp == DAT_NIL))
							{
								int a, b;

								a = (val.runstyp == DAT_TRUE ? 1 : 0);
								b = (val2.runstyp == DAT_TRUE ? 1 : 0);
								val.runstyp = runclog(a || b);
							}
							else if (val.runstyp == DAT_NUMBER
								&& val2.runstyp == DAT_NUMBER)
								val.runsv.runsvnum |= val2.runsv.runsvnum;
							else
								runsig(ctx, ERR_REQNUM);
							break;

						case OPCASIXOR:
							if ((val.runstyp == DAT_TRUE || val.runstyp == DAT_NIL)
								&& (val2.runstyp == DAT_TRUE
									|| val2.runstyp == DAT_NIL))
							{
								int a, b;

								a = (val.runstyp == DAT_TRUE ? 1 : 0);
								b = (val2.runstyp == DAT_TRUE ? 1 : 0);
								val.runstyp = runclog(a ^ b);
							}
							else if (val.runstyp == DAT_NUMBER
								&& val2.runstyp == DAT_NUMBER)
								val.runsv.runsvnum ^= val2.runsv.runsvnum;
							else
								runsig(ctx, ERR_REQNUM);
							break;

						case OPCASISHL:
							if (val.runstyp != DAT_NUMBER
								|| val2.runstyp != DAT_NUMBER)
								runsig(ctx, ERR_REQNUM);
							val.runsv.runsvnum <<= val2.runsv.runsvnum;
							break;

						case OPCASISHR:
							if (val.runstyp != DAT_NUMBER
								|| val2.runstyp != DAT_NUMBER)
								runsig(ctx, ERR_REQNUM);
							val.runsv.runsvnum >>= val2.runsv.runsvnum;
							break;

						default:
							runsig(ctx, ERR_INVOPC);
						}
						break;

					default:
						runsig(ctx, ERR_INVOPC);
					}

					/* write the rvalue at *valp to the lvalue */
					switch (opc & OPCASIDEST_MASK)
					{
					case OPCASILCL:
						OSCPYSTRUCT(*(ctx->runcxbp + lclnum - 1), *valp);
						break;

					case OPCASIPRP:
					case OPCASIPRPPTR:
					assign_property:
					{
						void    *valbuf;
						uchar    outbuf[4];

						switch (valp->runstyp)
						{
						case DAT_LIST:
						case DAT_SSTRING:
							valbuf = valp->runsv.runsvstr;
							break;

						case DAT_NUMBER:
							valbuf = outbuf;
							oswp4s(outbuf, valp->runsv.runsvnum);
							break;

						case DAT_OBJECT:
						case DAT_FNADDR:
							valbuf = outbuf;
							oswp2(outbuf, valp->runsv.runsvobj);
							break;

						case DAT_PROPNUM:
							valbuf = outbuf;
							oswp2(outbuf, valp->runsv.runsvprp);
							break;

						default:
							valbuf = &valp->runsv;
							break;
						}

						ofs = runcpsav(ctx, &p, target, targprop);
						objsetp(ctx->runcxmem, obj, prop, valp->runstyp,
							valbuf, ctx->runcxundo);
						p = runcprst(ctx, ofs, target, targprop);
						break;
					}

					case OPCASIIND:
					{
						uint   newtot;
						uint   newsiz;
						uint   remsiz;
						uint   delsiz;
						uchar *delp;
						uchar *remp;

						/* compute sizes and pointers to various parts */
						ofs = runindofs(ctx, i, lstp);
						delp = lstp + ofs;        /* ptr to item to replace */
						delsiz = datsiz(*delp, delp + 1);  /* size of *delp */
						remsiz = osrp2(lstp) - ofs - delsiz - 1;
						newsiz = runsiz(valp);          /* size of new item */
						newtot = osrp2(lstp) + newsiz - delsiz;  /* new tot */

						/* reserve space for the new list & copy first part */
						{
							runsdef rval3;

							/* make sure lstp stays valid before and after */
							rval3.runstyp = DAT_LIST;
							rval3.runsv.runsvstr = lstp;
							runhres3(ctx, newtot, 3, &val, &val2, &rval3);

							/* update all of the pointers within lstp */
							lstp = rval3.runsv.runsvstr;
							delp = lstp + ofs;
							remp = lstp + ofs + delsiz + 1;
						}
						memcpy(ctx->runcxhp + 2, lstp + 2, (size_t)(ofs - 2));

						/* set size of new list */
						oswp2(ctx->runcxhp, newtot);

						/* copy new item into buffer */
						runputbuf(ctx->runcxhp + ofs, valp);

						/* copy remainder and update heap pointer */
						memcpy(ctx->runcxhp + ofs + newsiz + 1, remp,
							(size_t)remsiz);
						val.runstyp = DAT_LIST;
						val.runsv.runsvstr = ctx->runcxhp;
						stkval = &val;
						ctx->runcxhp += newtot;
						break;
					}
					}

				no_assign:   /* skip assignment - operation didn't change value */
					if (*p == OPCDISCARD)
					{
						/* next assignment is DISCARD - deal with it now */
						++p;
						ctx->runcxsp = rstsp;
					}
					else
						runrepush(ctx, stkval);
				}
				else
					errsig(ctx->runcxerr, ERR_INVOPC);
			}
		}

	/*
	*   come here to return - don't use 'return' directly, since that
	*   would not properly exit the error frame
	*/
done:;

#ifndef DBG_OFF
	/*
	*   Come here to catch any errors that occur during execution of this
	*   p-code
	*/
	ERRCATCH(ctx->runcxerr, err)
	{
		/*
		*   if the debugger isn't present, or we're already in the
		*   debugger, or if the debugger can't resume from errors, or if
		*   we're not in user code (in which case the debugger can't
		*   resume from this error even if it normally could resume from
		*   an error), simply re-signal the error
		*/
		if (!dbgpresent()
			|| ctx->runcxdbg->dbgcxfcn == 0
			|| !dbgu_err_resume(ctx->runcxdbg)
			|| (ctx->runcxdbg->dbgcxflg & DBGCXFIND) != 0)
			errrse(ctx->runcxerr);

		/* check the error code */
		switch (err)
		{
		case ERR_RUNEXIT:
		case ERR_RUNABRT:
		case ERR_RUNASKD:
		case ERR_RUNASKI:
		case ERR_RUNQUIT:
		case ERR_RUNRESTART:
		case ERR_RUNEXITOBJ:
			/* don't trap these errors - resignal it immediately */
			errrse(ctx->runcxerr);

		default:
			/* trap other errors to the debugger */
			break;
		}

		/* if the object was unlocked, re-lock it */
		if (p == 0)
			mcmlck(ctx->runcxmem, target);

		/* set up after the last OPCLINE instruction */
		p = mcmobjptr(ctx->runcxmem, (mcmon)target) + ctx->runcxlofs - 2;
		p += *p;

		/*
		*   Keep the current error's arguments around for handling
		*   outside of this handler, since we'll need them in dbgssi.
		*/
		errkeepargs(ctx->runcxerr);

		/* enter the debugger with the error code */
		dbgssi(ctx->runcxdbg, ctx->runcxlofs, OPCLINE, err, &p);

		/* check the error again */
		switch (err)
		{
		case ERR_ARGC:
			/* we can't continue from this - simply return */
			break;

		default:
			/* resume execution */
			goto resume_from_error;
		}
	}
	ERREND(ctx->runcxerr);
#endif /* DBG_OFF */
}

/*
*   Signal a run-time error.  This function first calls the debugger
*   single-step function to allow the debugger to trap the error, then
*   signals the error as usual when the debugger returns.
*/
void runsign(runcxdef *ctx, int err)
{
	/*
	*   If the debugger isn't capable of resuming from a run-time error,
	*   trap to the debugger now so that the user can see what happened.
	*   Do not trap to the debugger here if the debugger can resume from
	*   an error; instead, we'll trap in the p-code loop, since we'll be
	*   able to resume execution from the point of the error.
	*
	*   Note that we can't resume from an error when there's no stack
	*   frame, so we'll trap to the debugger here in that case.
	*/
	if (ctx->runcxdbg->dbgcxfcn == 0
		|| !dbgu_err_resume(ctx->runcxdbg))
		dbgssi(ctx->runcxdbg, ctx->runcxlofs, OPCLINE, err, 0);

	/* signal the error */
	errsign(ctx->runcxerr, err, "TADS");
}

} // End of namespace TADS2
} // End of namespace TADS
} // End of namespace Glk