File: dict.c

package info (click to toggle)
gnu-smalltalk 3.2.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,688 kB
  • ctags: 14,104
  • sloc: ansic: 87,424; sh: 22,729; asm: 8,465; perl: 4,513; cpp: 3,548; xml: 1,669; awk: 1,581; yacc: 1,357; makefile: 1,237; lisp: 855; lex: 843; sed: 258; objc: 124
file content (2240 lines) | stat: -rw-r--r-- 66,538 bytes parent folder | download | duplicates (2)
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
/******************************** -*- C -*- ****************************
 *
 *	Dictionary Support Module.
 *
 *
 ***********************************************************************/

/***********************************************************************
 *
 * Copyright 1988,89,90,91,92,94,95,99,2000,2001,2002,2003,2005,2006,2007,2008,2009
 * Free Software Foundation, Inc.
 * Written by Steve Byrne.
 *
 * This file is part of GNU Smalltalk.
 *
 * GNU Smalltalk 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, or (at your option) any later 
 * version.
 * 
 * Linking GNU Smalltalk statically or dynamically with other modules is
 * making a combined work based on GNU Smalltalk.  Thus, the terms and
 * conditions of the GNU General Public License cover the whole
 * combination.
 *
 * In addition, as a special exception, the Free Software Foundation
 * give you permission to combine GNU Smalltalk with free software
 * programs or libraries that are released under the GNU LGPL and with
 * independent programs running under the GNU Smalltalk virtual machine.
 *
 * You may copy and distribute such a system following the terms of the
 * GNU GPL for GNU Smalltalk and the licenses of the other code
 * concerned, provided that you include the source code of that other
 * code when and as the GNU GPL requires distribution of source code.
 *
 * Note that people who make modified versions of GNU Smalltalk are not
 * obligated to grant this special exception for their modified
 * versions; it is their choice whether to do so.  The GNU General
 * Public License gives permission to release a modified version without
 * this exception; this exception also makes it possible to release a
 * modified version which carries forward this exception.
 *
 * GNU Smalltalk 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
 * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  
 *
 ***********************************************************************/


#include "gstpriv.h"

/* this must be big enough that the Smalltalk dictionary does not have to
   grow between the time gst_dictionary is loaded and the time the kernel is
   initialized.  Otherwise some of the methods needed to grow the dictionary
   might not be defined yet!!  */
#define INITIAL_SMALLTALK_SIZE		512

typedef struct class_definition
{
  OOP *classVar;
  OOP *superClassPtr;
  intptr_t instanceSpec;
  mst_Boolean reloadAddress;
  int numFixedFields;
  const char *name;
  const char *instVarNames;
  const char *classVarNames;
  const char *sharedPoolNames;
}
class_definition;

/* Primary class variables.  These variables hold the class objects for
   most of the builtin classes in the system */
OOP _gst_abstract_namespace_class = NULL;
OOP _gst_array_class = NULL;
OOP _gst_arrayed_collection_class = NULL;
OOP _gst_association_class = NULL;
OOP _gst_behavior_class = NULL;
OOP _gst_binding_dictionary_class = NULL;
OOP _gst_block_closure_class = NULL;
OOP _gst_block_context_class = NULL;
OOP _gst_boolean_class = NULL;
OOP _gst_byte_array_class = NULL;
OOP _gst_c_callable_class = NULL;
OOP _gst_c_callback_descriptor_class = NULL;
OOP _gst_c_func_descriptor_class = NULL;
OOP _gst_c_object_class = NULL;
OOP _gst_c_type_class = NULL;
OOP _gst_callin_process_class = NULL;
OOP _gst_char_class = NULL;
OOP _gst_character_array_class = NULL;
OOP _gst_class_class = NULL;
OOP _gst_class_description_class = NULL;
OOP _gst_collection_class = NULL;
OOP _gst_compiled_block_class = NULL;
OOP _gst_compiled_code_class = NULL;
OOP _gst_compiled_method_class = NULL;
OOP _gst_context_part_class = NULL;
OOP _gst_continuation_class = NULL;
OOP _gst_date_class = NULL;
OOP _gst_deferred_variable_binding_class = NULL;
OOP _gst_dictionary_class = NULL;
OOP _gst_directed_message_class = NULL;
OOP _gst_false_class = NULL;
OOP _gst_file_descriptor_class = NULL;
OOP _gst_file_segment_class = NULL;
OOP _gst_file_stream_class = NULL;
OOP _gst_float_class = NULL;
OOP _gst_floatd_class = NULL;
OOP _gst_floate_class = NULL;
OOP _gst_floatq_class = NULL;
OOP _gst_fraction_class = NULL;
OOP _gst_hashed_collection_class = NULL;
OOP _gst_homed_association_class = NULL;
OOP _gst_identity_dictionary_class = NULL;
OOP _gst_identity_set_class = NULL;
OOP _gst_integer_class = NULL;
OOP _gst_interval_class = NULL;
OOP _gst_iterable_class = NULL;
OOP _gst_large_integer_class = NULL;
OOP _gst_large_negative_integer_class = NULL;
OOP _gst_large_positive_integer_class = NULL;
OOP _gst_large_zero_integer_class = NULL;
OOP _gst_link_class = NULL;
OOP _gst_linked_list_class = NULL;
OOP _gst_lookup_key_class = NULL;
OOP _gst_lookup_table_class = NULL;
OOP _gst_magnitude_class = NULL;
OOP _gst_memory_class = NULL;
OOP _gst_message_class = NULL;
OOP _gst_metaclass_class = NULL;
OOP _gst_method_context_class = NULL;
OOP _gst_method_dictionary_class = NULL;
OOP _gst_method_info_class = NULL;
OOP _gst_namespace_class = NULL;
OOP _gst_number_class = NULL;
OOP _gst_object_class = NULL;
OOP _gst_object_memory_class = NULL;
OOP _gst_ordered_collection_class = NULL;
OOP _gst_permission_class = NULL;
OOP _gst_positionable_stream_class = NULL;
OOP _gst_process_class = NULL;
OOP _gst_processor_scheduler_class = NULL;
OOP _gst_read_stream_class = NULL;
OOP _gst_read_write_stream_class = NULL;
OOP _gst_root_namespace_class = NULL;
OOP _gst_security_policy_class = NULL;
OOP _gst_semaphore_class = NULL;
OOP _gst_sequenceable_collection_class = NULL;
OOP _gst_set_class = NULL;
OOP _gst_small_integer_class = NULL;
OOP _gst_smalltalk_dictionary = NULL;
OOP _gst_sorted_collection_class = NULL;
OOP _gst_stream_class = NULL;
OOP _gst_string_class = NULL;
OOP _gst_sym_link_class = NULL;
OOP _gst_symbol_class = NULL;
OOP _gst_system_dictionary_class = NULL;
OOP _gst_time_class = NULL;
OOP _gst_true_class = NULL;
OOP _gst_undefined_object_class = NULL;
OOP _gst_unicode_character_class = NULL;
OOP _gst_unicode_string_class = NULL;
OOP _gst_variable_binding_class = NULL;
OOP _gst_weak_array_class = NULL;
OOP _gst_weak_set_class = NULL;
OOP _gst_weak_key_dictionary_class = NULL;
OOP _gst_weak_value_lookup_table_class = NULL;
OOP _gst_weak_identity_set_class = NULL;
OOP _gst_weak_key_identity_dictionary_class = NULL;
OOP _gst_weak_value_identity_dictionary_class = NULL;
OOP _gst_write_stream_class = NULL;
OOP _gst_processor_oop = NULL;

/* Answer the number of slots that are in a dictionary of
   OLDNUMFIELDS items after growing it.  */
static size_t new_num_fields (size_t oldNumFields);

/* Instantiate the OOPs that are created before the first classes
   (true, false, nil, the Smalltalk dictionary, the symbol table
   and Processor, the sole instance of ProcessorScheduler.  */
static void init_proto_oops (void);

/* Look for the index at which KEYOOP resides in IDENTITYDICTIONARYOOP
   and answer it or, if not found, answer -1.  */
static ssize_t identity_dictionary_find_key (OOP identityDictionaryOOP,
					     OOP keyOOP);

/* Look for the index at which KEYOOP resides in IDENTITYDICTIONARYOOP
   or, if not found, find a nil slot which can be replaced by that
   key.  */
static size_t identity_dictionary_find_key_or_nil (OOP identityDictionaryOOP,
						   OOP keyOOP);

/* assume the value is an integer already or key does not exist, increase the
   value by inc or set the value to inc */
static int _gst_identity_dictionary_at_inc (OOP identityDictionaryOOP,
                                            OOP keyOOP,
                                            int inc);

/* Create a new instance of CLASSOOP (an IdentityDictionary subclass)
   and answer it.  */
static OOP identity_dictionary_new (OOP classOOP,
				    int size);

/* Create a new instance of Namespace with the given SIZE, NAME and
   superspace (SUPERSPACEOOP).  */
static OOP namespace_new (int size,
			  const char *name,
			  OOP superspaceOOP);

/* Create new class whose instances have a shape defined by CI.  */
static void create_class (const class_definition *ci);

/* Create a new metaclass for CLASS_OOP; reserve space for NUMSUBCLASSES
   classes in the instance variable "subclasses" of the class, and for
   NUMMETACLASSSUBCLASSES in the instance variable "subclasses" of the
   metaclass.  */
static void create_metaclass (OOP class_oop,
			      int numSubClasses,
			      int numMetaclassSubClasses);

/* Finish initializing the metaclass METACLASSOOP.  */
static void init_metaclass (OOP metaclassOOP);

/* Finish initializing the class CLASSOOP, using information from CI.  */
static void init_class (OOP classOOP, const class_definition *ci);

/* This creates the SystemDictionary called Smalltalk and initializes
   some of the variables in it.  */
static void init_smalltalk_dictionary (void);

/* This fills MAP so that it associates primitive numbers in the saved
   image to primitive numbers in this VM.  */
static void prepare_primitive_numbers_table (void);

/* Add a global named GLOBALNAME and give it the value GLOBALVALUE.
   Return GLOBALVALUE.  */
static OOP add_smalltalk (const char *globalName,
			  OOP globalValue);

/* Create N class objects described in the array starting at CI,
   establishing the instance shape and the link between a class
   and its superclass.  */
static void create_classes_pass1 (const class_definition *ci,
				  int n);

/* Create the subclasses variable of the N classes described in the
   array starting at CI (which being an Array must be created after
   the class objects are stored in the global variables).  Also
   create the metaclass hierarchy and make the class objects point
   to it.  */
static void create_classes_pass2 (const class_definition *ci,
				  int n);

/* Add a subclass SUBCLASSOOP to the subclasses array of
   SUPERCLASSOOP.  Subclasses are stored from the last index to the
   first, and the first slot of the Array indicates the index of the
   last free slot.  */
static void add_subclass (OOP superClassOOP,
			  OOP subClassOOP);

/* Adds to Smalltalk a global named FILEOBJECTNAME which is a
   FileStream referring to file descriptor FD.  */
static void add_file_stream_object (int fd,
				    int access,
				    const char *fileObjectName);

/* Creates the Symbols that the VM knows about, and initializes
   the globals in the Smalltalk dictionary.  */
static void init_runtime_objects (void);

/* Creates the VMPrimitives dictionary, which maps primitive names
   to primitive numbers.  */
static void init_primitives_dictionary (void);

/* Creates the CSymbols pool dictionary, which gives access from
   Smalltalk to some definitions in float.h and config.h.  */
static void init_c_symbols (void);

static const char *feature_strings[] = {
#ifdef ENABLE_DLD
    "DLD",
#endif
  NULL
};



/* The class definition structure.  From this structure, the initial
   set of Smalltalk classes are defined.  */

static const class_definition class_info[] = {
  {&_gst_object_class, &_gst_nil_oop,
   GST_ISP_FIXED, true, 0,
   "Object", NULL, "Dependencies FinalizableObjects", "VMPrimitives" },

  {&_gst_object_memory_class, &_gst_object_class,
   GST_ISP_FIXED, true, 34,
   "ObjectMemory", "bytesPerOOP bytesPerOTE "
   "edenSize survSpaceSize oldSpaceSize fixedSpaceSize "
   "edenUsedBytes survSpaceUsedBytes oldSpaceUsedBytes "
   "fixedSpaceUsedBytes rememberedTableEntries "
   "numScavenges numGlobalGCs numCompactions numGrowths "
   "numOldOOPs numFixedOOPs numWeakOOPs numOTEs numFreeOTEs "
   "timeBetweenScavenges timeBetweenGlobalGCs timeBetweenGrowths "
   "timeToScavenge timeToCollect timeToCompact "
   "reclaimedBytesPerScavenge tenuredBytesPerScavenge "
   "reclaimedBytesPerGlobalGC reclaimedPercentPerScavenge "
   "allocFailures allocMatches allocSplits allocProbes", NULL, NULL },

  {&_gst_message_class, &_gst_object_class,
   GST_ISP_FIXED, true, 2,
   "Message", "selector args", NULL, NULL },

  {&_gst_directed_message_class, &_gst_message_class,
   GST_ISP_FIXED, false, 1,
   "DirectedMessage", "receiver", NULL, NULL },

  {&_gst_magnitude_class, &_gst_object_class,
   GST_ISP_FIXED, false, 0,
   "Magnitude", NULL, NULL, NULL },

  {&_gst_char_class, &_gst_magnitude_class,
   GST_ISP_FIXED, true, 1,
   "Character", "codePoint", "Table UpperTable LowerTable", NULL },

  {&_gst_unicode_character_class, &_gst_char_class,
   GST_ISP_FIXED, true, 0,
   "UnicodeCharacter", NULL, NULL, NULL },

  {&_gst_time_class, &_gst_magnitude_class,
   GST_ISP_FIXED, false, 1,
   "Time", "seconds",
   "SecondClockAdjustment ClockOnStartup", NULL },

  {&_gst_date_class, &_gst_magnitude_class,
   GST_ISP_FIXED, false, 4,
   "Date", "days day month year",
   "DayNameDict MonthNameDict", NULL },

  {&_gst_number_class, &_gst_magnitude_class,
   GST_ISP_FIXED, false, 0,
   "Number", NULL, NULL, NULL },

  {&_gst_float_class, &_gst_number_class,
   GST_ISP_UCHAR, true, 0,
   "Float", NULL, NULL, "CSymbols" },

  {&_gst_floatd_class, &_gst_float_class,
   GST_ISP_UCHAR, true, 0,
   "FloatD", NULL, NULL, "CSymbols" },

  {&_gst_floate_class, &_gst_float_class,
   GST_ISP_UCHAR, true, 0,
   "FloatE", NULL, NULL, "CSymbols" },

  {&_gst_floatq_class, &_gst_float_class,
   GST_ISP_UCHAR, true, 0,
   "FloatQ", NULL, NULL, "CSymbols" },

  {&_gst_fraction_class, &_gst_number_class,
   GST_ISP_FIXED, false, 2,
   "Fraction", "numerator denominator", "Zero One", NULL },

  {&_gst_integer_class, &_gst_number_class,
   GST_ISP_FIXED, true, 0,
   "Integer", NULL, NULL, "CSymbols" },

  {&_gst_small_integer_class, &_gst_integer_class,
   GST_ISP_FIXED, true, 0,
   "SmallInteger", NULL, NULL, NULL },

  {&_gst_large_integer_class, &_gst_integer_class,	/* these four
							   classes
							   added by */
   GST_ISP_UCHAR, true, 0,	/* pb Sep 10 18:06:49 1998 */
   "LargeInteger", NULL,
   "Zero One ZeroBytes OneBytes LeadingZeros TrailingZeros", NULL },

  {&_gst_large_positive_integer_class, &_gst_large_integer_class,
   GST_ISP_UCHAR, true, 0,
   "LargePositiveInteger", NULL, NULL, NULL },

  {&_gst_large_zero_integer_class, &_gst_large_positive_integer_class,
   GST_ISP_UCHAR, true, 0,
   "LargeZeroInteger", NULL, NULL, NULL },

  {&_gst_large_negative_integer_class, &_gst_large_integer_class,
   GST_ISP_UCHAR, true, 0,
   "LargeNegativeInteger", NULL, NULL, NULL },

  {&_gst_lookup_key_class, &_gst_magnitude_class,
   GST_ISP_FIXED, true, 1,
   "LookupKey", "key", NULL, NULL },

  {&_gst_deferred_variable_binding_class, &_gst_lookup_key_class,
   GST_ISP_FIXED, true, 4,
   "DeferredVariableBinding", "class defaultDictionary association path",
   NULL, NULL },

  {&_gst_association_class, &_gst_lookup_key_class,
   GST_ISP_FIXED, true, 1,
   "Association", "value", NULL, NULL },

  {&_gst_homed_association_class, &_gst_association_class,
   GST_ISP_FIXED, false, 1,
   "HomedAssociation", "environment", NULL, NULL },

  {&_gst_variable_binding_class, &_gst_homed_association_class,
   GST_ISP_FIXED, true, 0,
   "VariableBinding", NULL, NULL, NULL },

  {&_gst_link_class, &_gst_object_class,
   GST_ISP_FIXED, false, 1,
   "Link", "nextLink", NULL, NULL },

  {&_gst_process_class, &_gst_link_class,
   GST_ISP_FIXED, true, 7,
   "Process",
   "suspendedContext priority myList name environment interrupts interruptLock",
   NULL, NULL },

  {&_gst_callin_process_class, &_gst_process_class,
   GST_ISP_FIXED, true, 1,
   "CallinProcess",
   "returnedValue",
   NULL, NULL },

  {&_gst_sym_link_class, &_gst_link_class,
   GST_ISP_FIXED, true, 1,
   "SymLink", "symbol", NULL, NULL },

  {&_gst_iterable_class, &_gst_object_class,
   GST_ISP_FIXED, false, 0,
   "Iterable", NULL, NULL, NULL },

  {&_gst_collection_class, &_gst_iterable_class,
   GST_ISP_FIXED, false, 0,
   "Collection", NULL, NULL, NULL },

  {&_gst_sequenceable_collection_class, &_gst_collection_class,
   GST_ISP_FIXED, false, 0,
   "SequenceableCollection", NULL, NULL, NULL },

  {&_gst_linked_list_class, &_gst_sequenceable_collection_class,
   GST_ISP_FIXED, false, 2,
   "LinkedList", "firstLink lastLink", NULL, NULL },

  {&_gst_semaphore_class, &_gst_linked_list_class,
   GST_ISP_FIXED, true, 2,
   "Semaphore", "signals name", NULL, NULL },

  {&_gst_arrayed_collection_class, &_gst_sequenceable_collection_class,
   GST_ISP_POINTER, false, 0,
   "ArrayedCollection", NULL, NULL, NULL },

  {&_gst_array_class, &_gst_arrayed_collection_class,
   GST_ISP_POINTER, true, 0,
   "Array", NULL, NULL, NULL },

  {&_gst_weak_array_class, &_gst_array_class,
   GST_ISP_FIXED, false, 2,
   "WeakArray", "values nilValues", NULL, NULL },

  {&_gst_character_array_class, &_gst_arrayed_collection_class,
   GST_ISP_ULONG, false, 0,
   "CharacterArray", NULL, NULL, NULL },

  {&_gst_string_class, &_gst_character_array_class,
   GST_ISP_CHARACTER, true, 0,
   "String", NULL, NULL, NULL },

  {&_gst_unicode_string_class, &_gst_character_array_class,
   GST_ISP_UTF32, true, 0,
   "UnicodeString", NULL, NULL, NULL },

  {&_gst_symbol_class, &_gst_string_class,
   GST_ISP_CHARACTER, true, 0,
   "Symbol", NULL, NULL, NULL },

  {&_gst_byte_array_class, &_gst_arrayed_collection_class,
   GST_ISP_UCHAR, true, 0,
   "ByteArray", NULL, NULL, "CSymbols" },

  {&_gst_compiled_code_class, &_gst_arrayed_collection_class,
   GST_ISP_UCHAR, false, 2,
   "CompiledCode", "literals header",
   NULL, NULL },

  {&_gst_compiled_block_class, &_gst_compiled_code_class,
   GST_ISP_UCHAR, true, 1,
   "CompiledBlock", "method",
   NULL, NULL },

  {&_gst_compiled_method_class, &_gst_compiled_code_class,
   GST_ISP_UCHAR, true, 1,
   "CompiledMethod", "descriptor ",
   NULL, NULL },

  {&_gst_interval_class, &_gst_arrayed_collection_class,
   GST_ISP_FIXED, true, 3,
   "Interval", "start stop step", NULL, NULL },

  {&_gst_ordered_collection_class, &_gst_sequenceable_collection_class,
   GST_ISP_POINTER, false, 2,
   "OrderedCollection", "firstIndex lastIndex", NULL, NULL },

  {&_gst_sorted_collection_class, &_gst_ordered_collection_class,
   GST_ISP_POINTER, false, 3,
   "SortedCollection", "lastOrdered sorted sortBlock",
   "DefaultSortBlock",
   NULL },

  {&_gst_hashed_collection_class, &_gst_collection_class,
   GST_ISP_POINTER, false, 1,
   "HashedCollection", "tally", NULL, NULL },

  {&_gst_set_class, &_gst_hashed_collection_class,
   GST_ISP_POINTER, false, 0,
   "Set", NULL, NULL, NULL },

  {&_gst_weak_set_class, &_gst_set_class,
   GST_ISP_POINTER, false, 0,
   "WeakSet", NULL, NULL, NULL },

  {&_gst_identity_set_class, &_gst_set_class,
   GST_ISP_POINTER, false, 0,
   "IdentitySet", NULL, NULL, NULL },

  {&_gst_weak_identity_set_class, &_gst_weak_set_class,
   GST_ISP_POINTER, false, 0,
   "WeakIdentitySet", NULL, NULL, NULL },

  {&_gst_dictionary_class, &_gst_hashed_collection_class,
   GST_ISP_POINTER, true, 0,
   "Dictionary", NULL, NULL, NULL },

  {&_gst_weak_key_dictionary_class, &_gst_dictionary_class,
   GST_ISP_POINTER, false, 1,
   "WeakKeyDictionary", "keys", NULL, NULL },

  {&_gst_weak_key_identity_dictionary_class, &_gst_weak_key_dictionary_class,
   GST_ISP_POINTER, false, 0,
   "WeakKeyIdentityDictionary", NULL, NULL, NULL },

  {&_gst_lookup_table_class, &_gst_dictionary_class,
   GST_ISP_POINTER, false, 0,
   "LookupTable", NULL, NULL, NULL },

  {&_gst_weak_value_lookup_table_class, &_gst_lookup_table_class,
   GST_ISP_POINTER, false, 1,
   "WeakValueLookupTable", "values", NULL, NULL },

  {&_gst_weak_value_identity_dictionary_class, &_gst_weak_value_lookup_table_class,
   GST_ISP_POINTER, false, 0,
   "WeakValueIdentityDictionary", NULL, NULL, NULL },

  {&_gst_identity_dictionary_class, &_gst_lookup_table_class,
   GST_ISP_POINTER, true, 0,
   "IdentityDictionary", NULL, NULL, NULL },

  {&_gst_method_dictionary_class, &_gst_lookup_table_class,
   GST_ISP_POINTER, true, 0,
   "MethodDictionary", NULL, NULL, NULL },

  /* These five MUST have the same structure as dictionary; they're
     used interchangeably within the C portion of the system */
  {&_gst_binding_dictionary_class, &_gst_dictionary_class,
   GST_ISP_POINTER, true, 1,
   "BindingDictionary", "environment", NULL, NULL },

  {&_gst_abstract_namespace_class, &_gst_binding_dictionary_class,
   GST_ISP_POINTER, true, 3,
   "AbstractNamespace", "name subspaces sharedPools", NULL, NULL },

  {&_gst_root_namespace_class, &_gst_abstract_namespace_class,
   GST_ISP_POINTER, false, 0,
   "RootNamespace", NULL, NULL, NULL },

  {&_gst_namespace_class, &_gst_abstract_namespace_class,
   GST_ISP_POINTER, true, 0,
   "Namespace", NULL, "Current", NULL },

  {&_gst_system_dictionary_class, &_gst_root_namespace_class,
   GST_ISP_POINTER, false, 0,
   "SystemDictionary", NULL, NULL, NULL },

  {&_gst_stream_class, &_gst_iterable_class,
   GST_ISP_FIXED, false, 0,
   "Stream", NULL, NULL, NULL },

  {&_gst_positionable_stream_class, &_gst_stream_class,
   GST_ISP_FIXED, false, 4,
   "PositionableStream", "collection ptr endPtr access", NULL, NULL },

  {&_gst_read_stream_class, &_gst_positionable_stream_class,
   GST_ISP_FIXED, false, 0,
   "ReadStream", NULL, NULL, NULL },

  {&_gst_write_stream_class, &_gst_positionable_stream_class,
   GST_ISP_FIXED, false, 0,
   "WriteStream", NULL, NULL, NULL },

  {&_gst_read_write_stream_class, &_gst_write_stream_class,
   GST_ISP_FIXED, false, 0,
   "ReadWriteStream", NULL, NULL, NULL },

  {&_gst_file_descriptor_class, &_gst_stream_class,
   GST_ISP_FIXED, true, 6,
   "FileDescriptor", "access fd file isPipe atEnd peek", "AllOpenFiles", NULL },

  {&_gst_file_stream_class, &_gst_file_descriptor_class,
   GST_ISP_FIXED, true, 5,
   "FileStream", "collection ptr endPtr writePtr writeEnd", "Verbose Record Includes", NULL },

  {&_gst_undefined_object_class, &_gst_object_class,
   GST_ISP_FIXED, true, 0,
   "UndefinedObject", NULL, NULL, NULL },

  {&_gst_boolean_class, &_gst_object_class,
   GST_ISP_FIXED, true, 0,
   "Boolean", NULL, NULL, NULL },

  {&_gst_false_class, &_gst_boolean_class,
   GST_ISP_FIXED, true, 1,
   "False", "truthValue", NULL, NULL },

  {&_gst_true_class, &_gst_boolean_class,
   GST_ISP_FIXED, true, 1,
   "True", "truthValue", NULL, NULL },

  {&_gst_processor_scheduler_class, &_gst_object_class,
   GST_ISP_FIXED, false, 6,
   "ProcessorScheduler",
   "processLists activeProcess idleTasks processTimeslice gcSemaphore gcArray",
   NULL, NULL },

  /* Change this, classDescription, or gst_class, and you must change
     the implementaion of new_metaclass some */
  {&_gst_behavior_class, &_gst_object_class,
   GST_ISP_FIXED, true, 5,
   "Behavior",
   "superClass methodDictionary instanceSpec subClasses instanceVariables",
   NULL, NULL },

  {&_gst_class_description_class, &_gst_behavior_class,
   GST_ISP_FIXED, true, 0,
   "ClassDescription", NULL, NULL, NULL },

  {&_gst_class_class, &_gst_class_description_class,
   GST_ISP_FIXED, true, 8,
   "Class",
   "name comment category environment classVariables sharedPools "
   "securityPolicy pragmaHandlers",
   NULL, NULL },

  {&_gst_metaclass_class, &_gst_class_description_class,
   GST_ISP_FIXED, true, 1,
   "Metaclass", "instanceClass", NULL, NULL },

  {&_gst_context_part_class, &_gst_object_class,
   GST_ISP_POINTER, true, 6,
   "ContextPart", "parent nativeIP ip sp receiver method ",
   NULL, NULL },

  {&_gst_method_context_class, &_gst_context_part_class,
   GST_ISP_POINTER, true, 1,
   "MethodContext", "flags ", NULL, NULL },

  {&_gst_block_context_class, &_gst_context_part_class,
   GST_ISP_POINTER, true, 1,
   "BlockContext", "outerContext ", NULL, NULL },

  {&_gst_continuation_class, &_gst_object_class,
   GST_ISP_FIXED, true, 1,
   "Continuation", "stack ", NULL, NULL },

  {&_gst_block_closure_class, &_gst_object_class,
   GST_ISP_FIXED, true, 3,
   "BlockClosure", "outerContext block receiver", NULL, NULL },

  {&_gst_permission_class, &_gst_object_class,
   GST_ISP_FIXED, true, 4,
   "Permission", "name actions target positive", NULL, NULL },

  {&_gst_security_policy_class, &_gst_object_class,
   GST_ISP_FIXED, true, 2,
   "SecurityPolicy", "dictionary owner", NULL, NULL },

  {&_gst_c_object_class, &_gst_object_class,
   GST_ISP_ULONG, true, 2,
   "CObject", "type storage", NULL, "CSymbols" },

  {&_gst_c_type_class, &_gst_object_class,
   GST_ISP_FIXED, true, 1,
   "CType", "cObjectType", NULL, NULL },

  {&_gst_c_callable_class, &_gst_c_object_class,
   GST_ISP_ULONG, true, 2,
   "CCallable",
   "returnType argTypes",
   NULL, NULL },

  {&_gst_c_func_descriptor_class, &_gst_c_callable_class,
   GST_ISP_ULONG, false, 1,
   "CFunctionDescriptor",
   "cFunctionName",
   NULL, NULL },

  {&_gst_c_callback_descriptor_class, &_gst_c_callable_class,
   GST_ISP_ULONG, true, 1,
   "CCallbackDescriptor",
   "block",
   NULL, NULL },

  {&_gst_memory_class, &_gst_object_class,
   GST_ISP_FIXED, false, 0,
   "Memory", NULL, NULL, NULL },

  {&_gst_method_info_class, &_gst_object_class,
   GST_ISP_POINTER, true, 4,
   "MethodInfo", "sourceCode category class selector", NULL, NULL },

  {&_gst_file_segment_class, &_gst_object_class,
   GST_ISP_FIXED, true, 3,
   "FileSegment", "file startPos size", NULL, NULL }

/* Classes not defined here (like Point/Rectangle/RunArray) are
   defined after the kernel has been fully initialized.  */
};

signed char _gst_log2_sizes[32] = {
  0, -1, 0, -1, 0, -1,
  1, -1, 1, -1, 
  2, -1, 2, -1, 2, -1, 
  3, -1, 3, -1, 3, -1,
  2, -1,
  -1, -1, -1, -1, -1, -1,
  sizeof (long) == 4 ? 2 : 3, -1
};





void
init_proto_oops()
{
  gst_namespace smalltalkDictionary;
  gst_object symbolTable, processorScheduler;
  int numWords;

  /* We can do this now that the classes are defined */
  _gst_init_builtin_objects_classes ();

  /* Also finish the creation of the OOPs with reserved indices in
     oop.h */

  /* the symbol table ...  */
  numWords = OBJ_HEADER_SIZE_WORDS + SYMBOL_TABLE_SIZE;
  symbolTable = _gst_alloc_words (numWords);
  SET_OOP_OBJECT (_gst_symbol_table, symbolTable);

  symbolTable->objClass = _gst_array_class;
  nil_fill (symbolTable->data,
	    numWords - OBJ_HEADER_SIZE_WORDS);

  /* 5 is the # of fixed instvars in gst_namespace */
  numWords = OBJ_HEADER_SIZE_WORDS + INITIAL_SMALLTALK_SIZE + 5;

  /* ... now the Smalltalk dictionary ...  */
  smalltalkDictionary = (gst_namespace) _gst_alloc_words (numWords);
  SET_OOP_OBJECT (_gst_smalltalk_dictionary, smalltalkDictionary);

  smalltalkDictionary->objClass = _gst_system_dictionary_class;
  smalltalkDictionary->tally = FROM_INT(0);
  smalltalkDictionary->name = _gst_smalltalk_namespace_symbol;
  smalltalkDictionary->superspace = _gst_nil_oop;
  smalltalkDictionary->subspaces = _gst_nil_oop;
  smalltalkDictionary->sharedPools = _gst_nil_oop;
  nil_fill (smalltalkDictionary->assoc,
	    INITIAL_SMALLTALK_SIZE);

  /* ... and finally Processor */
  numWords = sizeof (struct gst_processor_scheduler) / sizeof (PTR);
  processorScheduler = _gst_alloc_words (numWords);
  SET_OOP_OBJECT (_gst_processor_oop, processorScheduler);

  processorScheduler->objClass = _gst_processor_scheduler_class;
  nil_fill (processorScheduler->data,
	    numWords - OBJ_HEADER_SIZE_WORDS);
}

void
_gst_init_dictionary (void)
{
  memcpy (_gst_primitive_table, _gst_default_primitive_table,
          sizeof (_gst_primitive_table));

  /* The order of this must match the indices defined in oop.h!! */
  _gst_smalltalk_dictionary = alloc_oop (NULL, _gst_mem.active_flag);
  _gst_processor_oop = alloc_oop (NULL, _gst_mem.active_flag);
  _gst_symbol_table = alloc_oop (NULL, _gst_mem.active_flag);

  _gst_init_symbols_pass1 ();

  create_classes_pass1 (class_info, sizeof (class_info) / sizeof (class_info[0]));

  init_proto_oops();
  _gst_init_symbols_pass2 ();
  init_smalltalk_dictionary ();

  create_classes_pass2 (class_info, sizeof (class_info) / sizeof (class_info[0]));

  init_runtime_objects ();
  _gst_tenure_all_survivors ();
}

void
create_classes_pass1 (const class_definition *ci,
		      int n)
{
  OOP superClassOOP;
  int nilSubclasses;
  gst_class classObj, superclass;

  for (nilSubclasses = 0; n--; ci++)
    {
      superClassOOP = *ci->superClassPtr;
      create_class (ci);

      if (IS_NIL (superClassOOP))
	nilSubclasses++;
      else
	{
          superclass = (gst_class) OOP_TO_OBJ (superClassOOP);
          superclass->subClasses =
	    FROM_INT (TO_INT (superclass->subClasses) + 1);
	}
    }

  /* Object class being a subclass of gst_class is not an apparent link,
     and so the index which is the number of subclasses of the class
     is off by the number of subclasses of nil.  We correct that here.

     On the other hand, we don't want the meta class to have a subclass
     (`Class class' and `Class' are unique in that they don't have the
     same number of subclasses), so since we have the information here,
     we special case the Class class and create its metaclass here.  */
  classObj = (gst_class) OOP_TO_OBJ (_gst_class_class);
  create_metaclass (_gst_class_class,
		    TO_INT (classObj->subClasses),
		    TO_INT (classObj->subClasses) + nilSubclasses);
}

void
create_classes_pass2 (const class_definition *ci,
		      int n)
{
  OOP class_oop;
  gst_class class;
  int numSubclasses;

  for (; n--; ci++)
    {
      class_oop = *ci->classVar;
      class = (gst_class) OOP_TO_OBJ (class_oop);

      if (!class->objClass)
	{
          numSubclasses = TO_INT (class->subClasses);
	  create_metaclass (class_oop, numSubclasses, numSubclasses);
	}

      init_metaclass (class->objClass);
      init_class (class_oop, ci);
    }
}

void
create_metaclass (OOP class_oop,
	          int numMetaclassSubClasses,
	          int numSubClasses)
{
  gst_class class;
  gst_metaclass metaclass;
  gst_object subClasses;

  class = (gst_class) OOP_TO_OBJ (class_oop);
  metaclass = (gst_metaclass) new_instance (_gst_metaclass_class,
					    &class->objClass);

  metaclass->instanceClass = class_oop;

  subClasses = new_instance_with (_gst_array_class, numSubClasses,
				  &class->subClasses);
  if (numSubClasses > 0)
    subClasses->data[0] = FROM_INT (numSubClasses);

  subClasses = new_instance_with (_gst_array_class, numMetaclassSubClasses,
		     		  &metaclass->subClasses);
  if (numMetaclassSubClasses > 0)
    subClasses->data[0] = FROM_INT (numMetaclassSubClasses);
}

void
init_metaclass (OOP metaclassOOP)
{
  gst_metaclass metaclass;
  OOP class_oop, superClassOOP;

  metaclass = (gst_metaclass) OOP_TO_OBJ (metaclassOOP);
  class_oop = metaclass->instanceClass;
  superClassOOP = SUPERCLASS (class_oop);

  if (IS_NIL (superClassOOP))
    /* Object case: make this be gst_class to close the circularity */
    metaclass->superclass = _gst_class_class;
  else
    metaclass->superclass = OOP_CLASS (superClassOOP);

  add_subclass (metaclass->superclass, metaclassOOP);

  /* the specifications here should match what a class should have:
     instance variable names, the right number of instance variables,
     etc.  We could take three passes, and use the instance variable
     spec for classes once it's established, but it's easier to create
     them here by hand */
  metaclass->instanceVariables =
    _gst_make_instance_variable_array (_gst_nil_oop,
				       "superClass methodDictionary instanceSpec subClasses "
				       "instanceVariables name comment category environment "
				       "classVariables sharedPools securityPolicy "
				       "pragmaHandlers");

  metaclass->instanceSpec = GST_ISP_INTMARK | GST_ISP_FIXED |
    (((sizeof (struct gst_class) -
       sizeof (gst_object_header)) /
      sizeof (OOP)) << ISP_NUMFIXEDFIELDS);

  metaclass->methodDictionary = _gst_nil_oop;
}

void
init_class (OOP class_oop, const class_definition *ci)
{
  gst_class class;

  class = (gst_class) OOP_TO_OBJ (class_oop);
  class->name = _gst_intern_string (ci->name);
  add_smalltalk (ci->name, class_oop);

  if (!IS_NIL (class->superclass))
    add_subclass (class->superclass, class_oop);

  class->environment = _gst_smalltalk_dictionary;
  class->instanceVariables =
    _gst_make_instance_variable_array (class->superclass, ci->instVarNames);
  class->classVariables =
    _gst_make_class_variable_dictionary (ci->classVarNames, class_oop);

  class->sharedPools = _gst_make_pool_array (ci->sharedPoolNames);

  /* Other fields are set by the Smalltalk code.  */
  class->methodDictionary = _gst_nil_oop;
  class->comment = _gst_nil_oop;
  class->category = _gst_nil_oop;
  class->securityPolicy = _gst_nil_oop;
  class->pragmaHandlers = _gst_nil_oop;
}

void
add_subclass (OOP superClassOOP,
	      OOP subClassOOP)
{
  gst_class_description superclass;
  int index;

  superclass = (gst_class_description) OOP_TO_OBJ (superClassOOP);

#ifndef OPTIMIZE
  if (NUM_WORDS (OOP_TO_OBJ (superclass->subClasses)) == 0)
    {
      _gst_errorf ("Attempt to add subclass to zero sized class");
      abort ();
    }
#endif

  index = TO_INT (ARRAY_AT (superclass->subClasses, 1));
  ARRAY_AT_PUT (superclass->subClasses, 1, FROM_INT (index - 1));
  ARRAY_AT_PUT (superclass->subClasses, index, subClassOOP);
}

void
init_smalltalk_dictionary (void)
{
  OOP featuresArrayOOP;
  gst_object featuresArray;
  char fullVersionString[200];
  int i, numFeatures;

  _gst_current_namespace = _gst_smalltalk_dictionary;
  for (numFeatures = 0; feature_strings[numFeatures]; numFeatures++);

  featuresArray = new_instance_with (_gst_array_class, numFeatures,
		     		     &featuresArrayOOP);

  for (i = 0; i < numFeatures; i++)
    featuresArray->data[i] = _gst_intern_string (feature_strings[i]);

  sprintf (fullVersionString, "GNU Smalltalk version %s",
	   VERSION PACKAGE_GIT_REVISION);

  add_smalltalk ("Smalltalk", _gst_smalltalk_dictionary);
  add_smalltalk ("Version", _gst_string_new (fullVersionString));
  add_smalltalk ("KernelFilePath", _gst_string_new (_gst_kernel_file_path));
  add_smalltalk ("KernelInitialized", _gst_false_oop);
  add_smalltalk ("SymbolTable", _gst_symbol_table);
  add_smalltalk ("Processor", _gst_processor_oop);
  add_smalltalk ("Features", featuresArrayOOP);

  /* Add subspaces */
  add_smalltalk ("CSymbols",
    namespace_new (32, "CSymbols", _gst_smalltalk_dictionary));

  init_primitives_dictionary ();

  add_smalltalk ("Undeclared",
    namespace_new (32, "Undeclared", _gst_nil_oop));
  add_smalltalk ("SystemExceptions",
    namespace_new (32, "SystemExceptions", _gst_smalltalk_dictionary));
  add_smalltalk ("NetClients",
    namespace_new (32, "NetClients", _gst_smalltalk_dictionary));
  add_smalltalk ("VFS",
    namespace_new (32, "VFS", _gst_smalltalk_dictionary));

  _gst_init_process_system ();
}

static OOP
add_smalltalk (const char *globalName,
	       OOP globalValue)
{
  NAMESPACE_AT_PUT (_gst_smalltalk_dictionary,
		    _gst_intern_string (globalName), globalValue);

  return globalValue;
}

static OOP
relocate_path_oop (const char *s)
{
  OOP resultOOP;
  char *path = _gst_relocate_path (s);
  if (path)
    resultOOP = _gst_string_new (path);
  else
    resultOOP = _gst_nil_oop;

  free (path);
  return resultOOP;
}

void
init_runtime_objects (void)
{
  add_smalltalk ("UserFileBasePath", _gst_string_new (_gst_user_file_base_path));

  add_smalltalk ("SystemKernelPath", relocate_path_oop (KERNEL_PATH));
  add_smalltalk ("ModulePath", relocate_path_oop (MODULE_PATH));
  add_smalltalk ("LibexecPath", relocate_path_oop (LIBEXEC_PATH));
  add_smalltalk ("Prefix", relocate_path_oop (PREFIX));
  add_smalltalk ("ExecPrefix", relocate_path_oop (EXEC_PREFIX));
  add_smalltalk ("ImageFilePath", _gst_string_new (_gst_image_file_path));
  add_smalltalk ("ExecutableFileName", _gst_string_new (_gst_executable_path));
  add_smalltalk ("ImageFileName", _gst_string_new (_gst_binary_image_name));
  add_smalltalk ("OutputVerbosity", FROM_INT (_gst_verbosity));
  add_smalltalk ("RegressionTesting",
		 _gst_regression_testing ? _gst_true_oop : _gst_false_oop);

#ifdef WORDS_BIGENDIAN
  add_smalltalk ("Bigendian", _gst_true_oop);
#else
  add_smalltalk ("Bigendian", _gst_false_oop);
#endif

  add_file_stream_object (0, O_RDONLY, "stdin");
  add_file_stream_object (1, O_WRONLY, "stdout");
  add_file_stream_object (2, O_WRONLY, "stderr");

  init_c_symbols ();

  /* Add the root among the roots :-) to the root set */
  _gst_register_oop (_gst_smalltalk_dictionary);
}

void
init_c_symbols ()
{
  OOP cSymbolsOOP = dictionary_at (_gst_smalltalk_dictionary,
				   _gst_intern_string ("CSymbols"));

  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("HostSystem"),
		    _gst_string_new (HOST_SYSTEM));

  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CIntSize"),
		    FROM_INT (sizeof (int)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CShortSize"),
		    FROM_INT (sizeof (short)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongSize"),
		    FROM_INT (sizeof (long)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatSize"),
		    FROM_INT (sizeof (float)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleSize"),
		    FROM_INT (sizeof (double)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleSize"),
		    FROM_INT (sizeof (long double)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CPtrSize"),
		    FROM_INT (sizeof (PTR)));

#ifndef INFINITY
#define INFINITY LDBL_MAX * 2
#endif
#ifndef NAN
#define NAN (0.0 / 0.0)
#endif

#if defined WIN32 && !defined __CYGWIN__
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("PathSeparator"),
		    CHAR_OOP_AT ('\\'));
#else
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("PathSeparator"),
		    CHAR_OOP_AT ('/'));
#endif

  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleMin"),
		    floatd_new (DBL_MIN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleMax"),
		    floatd_new (DBL_MAX));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoublePInf"),
		    floatd_new ((double) INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleNInf"),
		    floatd_new ((double) -INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleNaN"),
		    floatd_new ((double) NAN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleDigits"),
		    FROM_INT (ceil (DBL_MANT_DIG * 0.301029995663981)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleBinaryDigits"),
		    FROM_INT (DBL_MANT_DIG));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleMinExp"),
		    FROM_INT (DBL_MIN_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleMaxExp"),
		    FROM_INT (DBL_MAX_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CDoubleAlignment"),
		    FROM_INT (ALIGNOF_DOUBLE));

  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatPInf"),
		    floate_new ((float) INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatNInf"),
		    floate_new ((float) -INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatNaN"),
		    floate_new ((float) NAN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatMin"),
		    floate_new (FLT_MIN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatMax"),
		    floate_new (FLT_MAX));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatDigits"),
		    FROM_INT (ceil (FLT_MANT_DIG * 0.301029995663981)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatBinaryDigits"),
		    FROM_INT (FLT_MANT_DIG));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatMinExp"),
		    FROM_INT (FLT_MIN_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatMaxExp"),
		    FROM_INT (FLT_MAX_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CFloatAlignment"),
		    FROM_INT (sizeof (float)));

  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoublePInf"),
		    floatq_new ((long double) INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleNInf"),
		    floatq_new ((long double) -INFINITY));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleNaN"),
		    floatq_new ((long double) NAN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleMin"),
		    floatq_new (LDBL_MIN));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleMax"),
		    floatq_new (LDBL_MAX));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleDigits"),
		    FROM_INT (ceil (LDBL_MANT_DIG * 0.301029995663981)));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleBinaryDigits"),
		    FROM_INT (LDBL_MANT_DIG));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleMinExp"),
		    FROM_INT (LDBL_MIN_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleMaxExp"),
		    FROM_INT (LDBL_MAX_EXP));
  NAMESPACE_AT_PUT (cSymbolsOOP, _gst_intern_string ("CLongDoubleAlignment"),
		    FROM_INT (ALIGNOF_LONG_DOUBLE));
}

void
init_primitives_dictionary ()
{
  OOP primDictionaryOOP = _gst_dictionary_new (512);
  int i;

  add_smalltalk ("VMPrimitives", primDictionaryOOP);
  for (i = 0; i < NUM_PRIMITIVES; i++)
    {
      prim_table_entry *pte = _gst_get_primitive_attributes (i);

      if (pte->name)
	{
	  OOP keyOOP = _gst_intern_string (pte->name);
	  OOP valueOOP = FROM_INT (i);
	  DICTIONARY_AT_PUT (primDictionaryOOP, keyOOP, valueOOP);
	}
    }
}

void
add_file_stream_object (int fd,
			int access,
			const char *fileObjectName)
{
  OOP fileStreamOOP;
  OOP keyOOP;

  keyOOP = _gst_intern_string (fileObjectName);
  fileStreamOOP = dictionary_at (_gst_smalltalk_dictionary, keyOOP);
  if (IS_NIL (fileStreamOOP))
    instantiate (_gst_file_stream_class, &fileStreamOOP);

  _gst_set_file_stream_file (fileStreamOOP, fd,
			     _gst_string_new (fileObjectName),
			     _gst_is_pipe (fd), access, true);

  add_smalltalk (fileObjectName, fileStreamOOP);
}

void
create_class (const class_definition *ci)
{
  gst_class class;
  intptr_t superInstanceSpec;
  OOP classOOP, superClassOOP;
  int numFixedFields;

  numFixedFields = ci->numFixedFields;
  superClassOOP = *ci->superClassPtr;
  if (!IS_NIL (superClassOOP))
    {
      /* adjust the number of instance variables to account for
         inheritance */
      superInstanceSpec = CLASS_INSTANCE_SPEC (superClassOOP);
      numFixedFields += superInstanceSpec >> ISP_NUMFIXEDFIELDS;
    }

  class = (gst_class) _gst_alloc_obj (sizeof (struct gst_class), &classOOP);

  class->objClass = NULL;
  class->superclass = superClassOOP;
  class->instanceSpec = GST_ISP_INTMARK
    | ci->instanceSpec
    | (numFixedFields << ISP_NUMFIXEDFIELDS);

  class->subClasses = FROM_INT (0);

  *ci->classVar = classOOP;
}


mst_Boolean
_gst_init_dictionary_on_image_load (mst_Boolean prim_table_matches)
{
  const class_definition *ci;

  _gst_smalltalk_dictionary = OOP_AT (SMALLTALK_OOP_INDEX);
  _gst_processor_oop = OOP_AT (PROCESSOR_OOP_INDEX);
  _gst_symbol_table = OOP_AT (SYM_TABLE_OOP_INDEX);

  if (IS_NIL (_gst_processor_oop) || IS_NIL (_gst_symbol_table)
      || IS_NIL (_gst_smalltalk_dictionary))
    return (false);

  _gst_restore_symbols ();

  for (ci = class_info; 
       ci < class_info + sizeof(class_info) / sizeof(class_definition);
       ci++)
    if (ci->reloadAddress)
      {
	*ci->classVar = dictionary_at (_gst_smalltalk_dictionary,
				       _gst_intern_string (ci->name));
        if UNCOMMON (IS_NIL (*ci->classVar))
	  return (false);
      }

  _gst_current_namespace =
    dictionary_at (_gst_class_variable_dictionary (_gst_namespace_class),
		   _gst_intern_string ("Current"));

  _gst_init_builtin_objects_classes ();

  /* Important: this is called *after* _gst_init_symbols
     fills in _gst_vm_primitives_symbol! */
  if (prim_table_matches)
    memcpy (_gst_primitive_table, _gst_default_primitive_table,
            sizeof (_gst_primitive_table));
  else
    prepare_primitive_numbers_table ();

  init_runtime_objects ();
  return (true);
}

void
prepare_primitive_numbers_table ()
{
  int i;
  OOP primitivesDictionaryOOP;

  primitivesDictionaryOOP = dictionary_at (_gst_smalltalk_dictionary, 
					   _gst_vm_primitives_symbol);

  for (i = 0; i < NUM_PRIMITIVES; i++)
    _gst_set_primitive_attributes (i, NULL);

  for (i = 0; i < NUM_PRIMITIVES; i++)
    {
      prim_table_entry *pte = _gst_get_primitive_attributes (i);
      OOP symbolOOP, valueOOP;
      int old_index;

      if (!pte->name)
	continue;

      symbolOOP = _gst_intern_string (pte->name);
      valueOOP = dictionary_at (primitivesDictionaryOOP, symbolOOP);

      /* Do nothing if the primitive is unknown to the image.  */
      if (IS_NIL (valueOOP))
        continue;

      old_index = TO_INT (valueOOP);
      _gst_set_primitive_attributes (old_index, pte);
    }
}



OOP
_gst_get_class_symbol (OOP class_oop)
{
  gst_class class;

  class = (gst_class) OOP_TO_OBJ (class_oop);
  return (class->name);
  /* this is the case when we have a metaclass, ??? I don't think that
     this is right, but I don't know what else to do here */
}



OOP
_gst_find_class (OOP classNameOOP)
{
  return (dictionary_at (_gst_smalltalk_dictionary, classNameOOP));
}



OOP
_gst_valid_class_method_dictionary (OOP class_oop)
{
  gst_class class;

  /* ??? check for non-class objects */
  class = (gst_class) OOP_TO_OBJ (class_oop);
  if (IS_NIL (class->methodDictionary))
    {
      OOP identDict;
      identDict = identity_dictionary_new (_gst_method_dictionary_class, 32);
      class = (gst_class) OOP_TO_OBJ (class_oop);
      class->methodDictionary = identDict;
    }

  return (class->methodDictionary);
}

OOP
_gst_find_class_method (OOP class_oop,
			OOP selector)
{
  gst_class class;
  gst_identity_dictionary methodDictionary;
  OOP method_dictionary_oop;
  int index;

  class = (gst_class) OOP_TO_OBJ (class_oop);
  method_dictionary_oop = class->methodDictionary;
  if (IS_NIL (method_dictionary_oop))
    return (_gst_nil_oop);

  index =
    identity_dictionary_find_key (method_dictionary_oop,
				  selector);

  if (index < 0)
    return (_gst_nil_oop);

  methodDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (method_dictionary_oop);

  return (methodDictionary->keys[index]);
}

OOP
_gst_class_variable_dictionary (OOP class_oop)
{
  gst_class class;

  /* ??? check for non-class objects */
  class = (gst_class) OOP_TO_OBJ (class_oop);
  return (class->classVariables);
}

OOP
_gst_instance_variable_array (OOP class_oop)
{
  gst_class class;

  /* ??? check for non-class objects */
  class = (gst_class) OOP_TO_OBJ (class_oop);
  return (class->instanceVariables);
}

OOP
_gst_shared_pool_dictionary (OOP class_oop)
{
  gst_class class;

  /* ??? check for non-class objects */
  class = (gst_class) OOP_TO_OBJ (class_oop);
  return (class->sharedPools);
}


OOP
_gst_namespace_association_at (OOP poolOOP,
			       OOP symbol)
{
  OOP assocOOP;
  gst_namespace pool;

  if (is_a_kind_of (OOP_CLASS (poolOOP), _gst_class_class))
    poolOOP = _gst_class_variable_dictionary (poolOOP);

  for (;;)
    {
      if (!is_a_kind_of (OOP_CLASS (poolOOP), _gst_dictionary_class))
        return (_gst_nil_oop);

      assocOOP = dictionary_association_at (poolOOP, symbol);
      if (!IS_NIL (assocOOP))
        return (assocOOP);

      /* Try to find a super-namespace */
      if (!is_a_kind_of (OOP_CLASS (poolOOP), _gst_abstract_namespace_class))
        return (_gst_nil_oop);

      pool = (gst_namespace) OOP_TO_OBJ (poolOOP);
      poolOOP = pool->superspace;
    }
}

OOP
_gst_namespace_at (OOP poolOOP,
		   OOP symbol)
{
  OOP assocOOP = _gst_namespace_association_at (poolOOP, symbol);
  if (IS_NIL (assocOOP))
    return assocOOP;
  else
    return ASSOCIATION_VALUE (assocOOP);
}


size_t
new_num_fields (size_t oldNumFields)
{
  /* Find a power of two that is larger than oldNumFields */

  int n = 1;

  /* Already a power of two? duplicate the size */
  if COMMON ((oldNumFields & (oldNumFields - 1)) == 0)
    return oldNumFields * 2;

  /* Find the next power of two by setting all bits to the right of
     the leftmost 1 bit to 1, and then incrementing.  */
  for (; oldNumFields & (oldNumFields + 1); n <<= 1)
    oldNumFields |= oldNumFields >> n;

  return oldNumFields + 1;
}

static int
find_key_or_nil (OOP dictionaryOOP,
		 OOP keyOOP)
{
  size_t count, numFields, numFixedFields;
  intptr_t index;
  gst_object dictionary;
  OOP associationOOP;
  gst_association association;

  dictionary = (gst_object) OOP_TO_OBJ (dictionaryOOP);
  numFixedFields = OOP_FIXED_FIELDS (dictionaryOOP);
  numFields = NUM_WORDS (dictionary) - numFixedFields;
  index = scramble (OOP_INDEX (keyOOP));
  count = numFields;

  for (; count; count--)
    {
      index &= numFields - 1;
      associationOOP = dictionary->data[numFixedFields + index];
      if COMMON (IS_NIL (associationOOP))
	return (index);

      association = (gst_association) OOP_TO_OBJ (associationOOP);

      if (association->key == keyOOP)
	return (index);

      /* linear reprobe -- it is simple and guaranteed */
      index++;
    }

  _gst_errorf
    ("Error - searching dictionary for nil, but it is full!\n");

  abort ();
}

gst_object
_gst_grow_dictionary (OOP oldDictionaryOOP)
{
  gst_object oldDictionary, dictionary;
  size_t oldNumFields, numFields, i, index, numFixedFields;
  OOP associationOOP;
  gst_association association;
  OOP dictionaryOOP;

  oldDictionary = OOP_TO_OBJ (oldDictionaryOOP);
  numFixedFields = OOP_FIXED_FIELDS (oldDictionaryOOP);
  oldNumFields = NUM_WORDS (oldDictionary) - numFixedFields;

  numFields = new_num_fields (oldNumFields);

  /* no need to use the incubator here.  We are instantiating just one
     object, the new dictionary itself */

  dictionary = instantiate_with (OOP_CLASS (oldDictionaryOOP), 
				 numFields, &dictionaryOOP);
  memcpy (dictionary->data, oldDictionary->data, sizeof (PTR) * numFixedFields);
  oldDictionary = OOP_TO_OBJ (oldDictionaryOOP);

  /* rehash all associations from old dictionary into new one */
  for (i = 0; i < oldNumFields; i++)
    {
      associationOOP = oldDictionary->data[numFixedFields + i];
      if COMMON (!IS_NIL (associationOOP))
	{
	  association = (gst_association) OOP_TO_OBJ (associationOOP);
	  index = find_key_or_nil (dictionaryOOP, association->key);
	  dictionary->data[numFixedFields + index] = associationOOP;
	}
    }

  _gst_swap_objects (dictionaryOOP, oldDictionaryOOP);
  return (OOP_TO_OBJ (oldDictionaryOOP));
}

gst_identity_dictionary
_gst_grow_identity_dictionary (OOP oldIdentityDictionaryOOP)
{
  gst_identity_dictionary oldIdentityDictionary, identityDictionary;
  OOP key, identityDictionaryOOP;
  size_t oldNumFields, numFields, i, index;

  oldIdentityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (oldIdentityDictionaryOOP);
  oldNumFields =
    (NUM_WORDS (oldIdentityDictionary) - 1) / 2;

  numFields = new_num_fields (oldNumFields);

  identityDictionary = (gst_identity_dictionary)
    instantiate_with (OOP_CLASS (oldIdentityDictionaryOOP), numFields * 2,
		      &identityDictionaryOOP);

  oldIdentityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (oldIdentityDictionaryOOP);

  identityDictionary->tally = oldIdentityDictionary->tally;

  /* rehash all associations from old dictionary into new one */
  for (i = 0; i < oldNumFields; i++)
    {
      key = oldIdentityDictionary->keys[i * 2];
      if COMMON (!IS_NIL (key))
	{
	  index =
	    identity_dictionary_find_key_or_nil (identityDictionaryOOP,
						 key);
	  identityDictionary->keys[index - 1] = key;
	  identityDictionary->keys[index] = oldIdentityDictionary->keys[i*2+1];
	}
    }

  _gst_swap_objects (identityDictionaryOOP, oldIdentityDictionaryOOP);
  return ((gst_identity_dictionary) OOP_TO_OBJ (oldIdentityDictionaryOOP));
}


ssize_t
identity_dictionary_find_key (OOP identityDictionaryOOP,
			      OOP keyOOP)
{
  gst_identity_dictionary identityDictionary;
  size_t index, count, numFields;

  identityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (identityDictionaryOOP);

  numFields = NUM_WORDS (identityDictionary) - 1;
  index = scramble (OOP_INDEX (keyOOP)) * 2;
  count = numFields / 2;
  /* printf ("%d %d %O\n", count, index & numFields - 1, keyOOP); */
  while (count--)
    {
      index &= numFields - 1;

      if COMMON (IS_NIL (identityDictionary->keys[index]))
	return (-1);

      if COMMON (identityDictionary->keys[index] == keyOOP)
	return (index + 1);

      /* linear reprobe -- it is simple and guaranteed */
      index += 2;
    }

  _gst_errorf
    ("Error - searching IdentityDictionary for nil, but it is full!\n");

  abort ();
}



size_t
identity_dictionary_find_key_or_nil (OOP identityDictionaryOOP,
				     OOP keyOOP)
{
  gst_identity_dictionary identityDictionary;
  size_t index, count, numFields;

  identityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (identityDictionaryOOP);

  numFields = NUM_WORDS (identityDictionary) - 1;
  index = scramble (OOP_INDEX (keyOOP)) * 2;
  count = numFields / 2;
  /* printf ("%d %d %O\n", count, index & numFields - 1, keyOOP); */
  while (count--)
    {
      index &= numFields - 1;

      if COMMON (IS_NIL (identityDictionary->keys[index]))
	return (index + 1);

      if COMMON (identityDictionary->keys[index] == keyOOP)
	return (index + 1);

      /* linear reprobe -- it is simple and guaranteed */
      index += 2;
    }

  _gst_errorf
    ("Error - searching IdentityDictionary for nil, but it is full!\n");

  abort ();
}

OOP
identity_dictionary_new (OOP classOOP, int size)
{
  gst_identity_dictionary identityDictionary;
  OOP identityDictionaryOOP;

  size = new_num_fields (size);

  identityDictionary = (gst_identity_dictionary)
    instantiate_with (classOOP, size * 2, &identityDictionaryOOP);

  identityDictionary->tally = FROM_INT (0);
  return (identityDictionaryOOP);
}

OOP
_gst_identity_dictionary_at_put (OOP identityDictionaryOOP,
				 OOP keyOOP,
				 OOP valueOOP)
{
  gst_identity_dictionary identityDictionary;
  intptr_t index;
  OOP oldValueOOP;

  identityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (identityDictionaryOOP);

  /* Never make dictionaries too full! For simplicity, we do this even
     if the key is present in the dictionary (because it will most
     likely resolve some collisions and make things faster).  */

  if UNCOMMON (TO_INT (identityDictionary->tally) >=
      	       TO_INT (identityDictionary->objSize) * 3 / 8)
    identityDictionary =
      _gst_grow_identity_dictionary (identityDictionaryOOP);

  index =
    identity_dictionary_find_key_or_nil (identityDictionaryOOP, keyOOP);

  if COMMON (IS_NIL (identityDictionary->keys[index - 1]))
    identityDictionary->tally = INCR_INT (identityDictionary->tally);

  identityDictionary->keys[index - 1] = keyOOP;
  oldValueOOP = identityDictionary->keys[index];
  identityDictionary->keys[index] = valueOOP;

  return (oldValueOOP);
}

OOP
_gst_identity_dictionary_at (OOP identityDictionaryOOP,
			     OOP keyOOP)
{
  gst_identity_dictionary identityDictionary;
  intptr_t index;

  identityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (identityDictionaryOOP);

  index =
    identity_dictionary_find_key_or_nil (identityDictionaryOOP, keyOOP);

  return identityDictionary->keys[index];
}

OOP
namespace_new (int size, const char *name, OOP superspaceOOP)
{
  gst_namespace ns;
  OOP namespaceOOP, classOOP;

  size = new_num_fields (size);
  classOOP = IS_NIL (superspaceOOP)
    ? _gst_root_namespace_class : _gst_namespace_class;

  ns = (gst_namespace) instantiate_with (classOOP, size, &namespaceOOP);

  ns->tally = FROM_INT (0);
  ns->superspace = superspaceOOP;
  ns->subspaces = _gst_nil_oop;
  ns->name = _gst_intern_string (name);

  return (namespaceOOP);
}

OOP
_gst_dictionary_new (int size)
{
  gst_dictionary dictionary;
  OOP dictionaryOOP;

  size = new_num_fields (size);
  dictionary = (gst_dictionary)
    instantiate_with (_gst_dictionary_class, size, &dictionaryOOP);

  dictionary->tally = FROM_INT (0);

  return (dictionaryOOP);
}

OOP
_gst_binding_dictionary_new (int size, OOP environmentOOP)
{
  gst_binding_dictionary dictionary;
  OOP dictionaryOOP;

  size = new_num_fields (size);
  dictionary = (gst_binding_dictionary)
    instantiate_with (_gst_binding_dictionary_class, size, &dictionaryOOP);

  dictionary->tally = FROM_INT (0);
  dictionary->environment = environmentOOP;

  return (dictionaryOOP);
}

OOP
_gst_dictionary_add (OOP dictionaryOOP,
		     OOP associationOOP)
{
  intptr_t index;
  gst_association association;
  gst_object dictionary;
  gst_dictionary dict;
  OOP value;
  inc_ptr incPtr;		/* I'm not sure clients are protecting
				   association OOP */

  incPtr = INC_SAVE_POINTER ();
  INC_ADD_OOP (associationOOP);

  association = (gst_association) OOP_TO_OBJ (associationOOP);
  dictionary = OOP_TO_OBJ (dictionaryOOP);
  dict = (gst_dictionary) dictionary;
  if UNCOMMON (TO_INT (dict->tally) >= 
	       TO_INT (dict->objSize) * 3 / 4)
    {
      dictionary = _gst_grow_dictionary (dictionaryOOP);
      dict = (gst_dictionary) dictionary;
    }

  index = find_key_or_nil (dictionaryOOP, association->key);
  index += OOP_FIXED_FIELDS (dictionaryOOP);
  if COMMON (IS_NIL (dictionary->data[index]))
    {
      dict->tally = INCR_INT (dict->tally);
      dictionary->data[index] = associationOOP;
    }
  else
    {
      value = ASSOCIATION_VALUE (associationOOP);
      associationOOP = dictionary->data[index];
      SET_ASSOCIATION_VALUE (associationOOP, value);
    }

  INC_RESTORE_POINTER (incPtr);
  return (associationOOP);
}


OOP
_gst_object_copy (OOP oop)
{
  gst_object old, new;
  OOP newOOP;
  size_t numFields;

  if UNCOMMON (IS_INT(oop) || IS_BUILTIN_OOP (oop))
    return (oop);

  numFields = NUM_INDEXABLE_FIELDS (oop);

  new = instantiate_with (OOP_CLASS (oop), numFields, &newOOP);
  old = OOP_TO_OBJ (oop);
  memcpy (new, old, SIZE_TO_BYTES (TO_INT (old->objSize)));

  newOOP->flags |= (oop->flags & F_CONTEXT);
  return (newOOP);
}



OOP
_gst_new_string (size_t len)
{
  OOP stringOOP;

  new_instance_with (_gst_string_class, len, &stringOOP);
  return (stringOOP);
}

OOP
_gst_string_new (const char *s)
{
  gst_string string;
  size_t len;
  OOP stringOOP;

  if (s)
    {
      len = strlen (s);
      string = (gst_string) new_instance_with (_gst_string_class, len,
					       &stringOOP);

      memcpy (string->chars, s, len);
    }
  else
    string = (gst_string) new_instance_with (_gst_string_class, 0,
					     &stringOOP);
  return (stringOOP);
}

OOP
_gst_unicode_string_new (const wchar_t *s)
{
  int i;
  gst_unicode_string string;
  size_t len;
  OOP stringOOP;

  if (s)
    {
      len = wcslen (s);
      string = (gst_unicode_string)
	new_instance_with (_gst_unicode_string_class, len, &stringOOP);

      if (sizeof (wchar_t) == sizeof (string->chars[0]))
	memcpy (string->chars, s, len * sizeof (wchar_t));
      else
	for (i = 0; i < len; i++)
	  string->chars[i] = *s++;
    }
  else
    string = (gst_unicode_string)
      new_instance_with (_gst_unicode_string_class, 0, &stringOOP);

  return (stringOOP);
}

OOP
_gst_counted_string_new (const char *s,
			 size_t len)
{
  gst_string string;
  OOP stringOOP;

  string = (gst_string) new_instance_with (_gst_string_class, len,
					   &stringOOP);

  if (len)
    memcpy (string->chars, s, len);

  return (stringOOP);
}

void
_gst_set_oopstring (OOP stringOOP,
		    const char *s)
{
  OOP newStringOOP;

  newStringOOP = _gst_string_new (s);
  _gst_swap_objects (stringOOP, newStringOOP);
}

void
_gst_set_oop_unicode_string (OOP unicodeStringOOP,
			     const wchar_t *s)
{
  OOP newStringOOP;

  newStringOOP = _gst_unicode_string_new (s);
  _gst_swap_objects (unicodeStringOOP, newStringOOP);
}

char *
_gst_to_cstring (OOP stringOOP)
{
  char *result;
  size_t len;
  gst_string string;

  string = (gst_string) OOP_TO_OBJ (stringOOP);
  len = oop_num_fields (stringOOP);
  result = (char *) xmalloc (len + 1);
  memcpy (result, string->chars, len);
  result[len] = '\0';

  return (result);
}

wchar_t *
_gst_to_wide_cstring (OOP stringOOP)
{
  wchar_t *result, *p;
  size_t len;
  gst_unicode_string string;
  int i;

  string = (gst_unicode_string) OOP_TO_OBJ (stringOOP);
  len = oop_num_fields (stringOOP);
  result = (wchar_t *) xmalloc (len + 1);
  if (sizeof (wchar_t) == 4)
    memcpy (result, string->chars, len * sizeof (wchar_t));
  else
    for (p = result, i = 0; i < len; i++)
      *p++ = string->chars[i];
  result[len] = '\0';

  return (result);
}

OOP
_gst_byte_array_new (const gst_uchar * bytes,
		     size_t len)
{
  gst_byte_array byteArray;
  OOP byteArrayOOP;

  byteArray = (gst_byte_array) new_instance_with (_gst_byte_array_class,
						  len, &byteArrayOOP);

  memcpy (byteArray->bytes, bytes, len);
  return (byteArrayOOP);
}



gst_uchar *
_gst_to_byte_array (OOP byteArrayOOP)
{
  gst_uchar *result;
  size_t len;
  gst_byte_array byteArray;

  byteArray = (gst_byte_array) OOP_TO_OBJ (byteArrayOOP);
  len = oop_num_fields (byteArrayOOP);
  result = (gst_uchar *) xmalloc (len);
  memcpy (result, byteArray->bytes, len);

  return (result);
}

void
_gst_set_oop_bytes (OOP byteArrayOOP,
		    gst_uchar * bytes)
{
  gst_byte_array byteArray;
  size_t len;

  len = oop_num_fields (byteArrayOOP);
  byteArray = (gst_byte_array) OOP_TO_OBJ (byteArrayOOP);
  memcpy (byteArray->bytes, bytes, len);
}



OOP
_gst_message_new_args (OOP selectorOOP,
		       OOP argsArray)
{
  gst_message message;
  OOP messageOOP;

  message = (gst_message) new_instance (_gst_message_class,
					&messageOOP);

  message->selector = selectorOOP;
  message->args = argsArray;

  return (messageOOP);
}

OOP
_gst_c_object_new_base (OOP baseOOP,
		        uintptr_t cObjOfs,
		        OOP typeOOP,
		        OOP defaultClassOOP)
{
  gst_cobject cObject;
  gst_ctype cType;
  OOP cObjectOOP;
  OOP classOOP;

  if (!IS_NIL (typeOOP))
    {
      cType = (gst_ctype) OOP_TO_OBJ (typeOOP);
      classOOP = ASSOCIATION_VALUE (cType->cObjectType);
    }
  else
    classOOP = defaultClassOOP;
    
  cObject = (gst_cobject) instantiate_with (classOOP, 1, &cObjectOOP);
  cObject->type = typeOOP;
  cObject->storage = baseOOP;
  SET_COBJECT_OFFSET_OBJ (cObject, cObjOfs);

  return (cObjectOOP);
}


void
_gst_free_cobject (OOP cObjOOP)
{
  gst_cobject cObject;

  cObject = (gst_cobject) OOP_TO_OBJ (cObjOOP);
  if (!IS_NIL (cObject->storage))
    cObject->storage = _gst_nil_oop;
  else
    xfree ((PTR) COBJECT_OFFSET_OBJ (cObject));

  /* make it not point to falsely valid storage */
  SET_COBJECT_OFFSET_OBJ (cObject, NULL);
}

void
_gst_set_file_stream_file (OOP fileStreamOOP,
			   int fd,
			   OOP fileNameOOP,
			   mst_Boolean isPipe,
			   int access,
			   mst_Boolean buffered)
{
  gst_file_stream fileStream;

  fileStream = (gst_file_stream) OOP_TO_OBJ (fileStreamOOP);

  switch (access & O_ACCMODE)
    {
    case O_RDONLY:
      fileStream->access = FROM_INT (1);
      break;
    case O_WRONLY:
      fileStream->access = FROM_INT (2);
      break;
    case O_RDWR:
      fileStream->access = FROM_INT (3);
      break;
    }

  if (buffered)
    {
      char buffer[1024];
      memset (buffer, 0, sizeof (buffer));
      fileStream->collection =
	_gst_counted_string_new (buffer, sizeof (buffer));
      fileStream->ptr = FROM_INT (1);
      fileStream->endPtr = FROM_INT (0);
      fileStream->writePtr = _gst_nil_oop;
      fileStream->writeEnd = _gst_nil_oop;
    }

  fileStream->fd = FROM_INT (fd);
  fileStream->file = fileNameOOP;
  fileStream->isPipe =
    isPipe == -1 ? _gst_nil_oop :
    isPipe ? _gst_true_oop : _gst_false_oop;
}

/* Profiling callback.  The profiler use a simple data structure
   to store the cost and the call graph, which is a 2 level
   IdentityDictionary. First level keys are the CompiledMethod or
   CompiledBlock, and the second level key is the CompiledMethod or
   CompiledBlock that it calls. Values are the number of calls made. There
   is a special key "true" in the second level whose corresponding value
   is the accumulative cost for this method.  */

void 
_gst_record_profile (OOP oldMethod, OOP newMethod, int ipOffset)
{
  OOP profile;
  inc_ptr incPtr;

  /* Protect oldMethod from GC here to avoid complicating the fast path
     in interp-bc.inl.  */
  incPtr = INC_SAVE_POINTER ();
  INC_ADD_OOP (oldMethod);

  profile = _gst_identity_dictionary_at (_gst_raw_profile, oldMethod);
  if UNCOMMON (IS_NIL (profile))
    {
      profile = identity_dictionary_new (_gst_identity_dictionary_class, 6);
      _gst_identity_dictionary_at_put (_gst_raw_profile, oldMethod, 
                                       profile);
    }

  _gst_identity_dictionary_at_inc (profile, _gst_true_oop, 
                                   _gst_bytecode_counter
				   - _gst_saved_bytecode_counter);
  _gst_saved_bytecode_counter = _gst_bytecode_counter;

  /* if ipOffset is 0 then it is a callin and not a return, so we also record 
     the call.  */
  if (ipOffset == 0) 
    _gst_identity_dictionary_at_inc (profile, newMethod, 1);

  INC_RESTORE_POINTER (incPtr);
}

/* Assume the value for KEYOOP is an integer already or the key does not exist;
   increase the value by INC or set it to INC if it does not exist.  */
int
_gst_identity_dictionary_at_inc (OOP identityDictionaryOOP,
				 OOP keyOOP,
				 int inc)
{
  gst_identity_dictionary identityDictionary;
  intptr_t index;
  int oldValue;

  identityDictionary =
    (gst_identity_dictionary) OOP_TO_OBJ (identityDictionaryOOP);

  /* Never make dictionaries too full! For simplicity, we do this even
     if the key is present in the dictionary (because it will most
     likely resolve some collisions and make things faster).  */

  if UNCOMMON (TO_INT (identityDictionary->tally) >=
      	       TO_INT (identityDictionary->objSize) * 3 / 8)
    identityDictionary =
      _gst_grow_identity_dictionary (identityDictionaryOOP);

  index =
    identity_dictionary_find_key_or_nil (identityDictionaryOOP, keyOOP);

  if UNCOMMON (IS_NIL (identityDictionary->keys[index - 1]))
    {
      identityDictionary->tally = INCR_INT (identityDictionary->tally);
      oldValue = 0;
    }
  else 
    oldValue = TO_INT(identityDictionary->keys[index]);
  
  identityDictionary->keys[index - 1] = keyOOP;
  identityDictionary->keys[index] = FROM_INT(inc+oldValue);

  return (oldValue);
}