File: external.c

package info (click to toggle)
scheme48 1.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,332 kB
  • sloc: lisp: 88,907; ansic: 87,519; sh: 3,224; makefile: 771
file content (2448 lines) | stat: -rw-r--r-- 70,603 bytes parent folder | download | duplicates (4)
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
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
/*
 * Part of Scheme 48 1.9.  See file COPYING for notices and license.
 *
 * Authors: Richard Kelsey, Jonathan Rees, Marcus Crestani, Mike Sperber,
 * Robert Ransom, Harald Glab-Phlak, Marcel Turino
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <stdarg.h>

#include "c-mods.h"
#include "scheme48.h"
#include "scheme48vm.h"
#include "bignum.h"
#include "ffi.h"

/*
 * The Joy of C
 * I don't understand why we need this, but we do.
 */

struct s_jmp_buf {
  jmp_buf buf;
};

/*
 * Longjump target set up by the most recent call into C.
 */
static struct s_jmp_buf	current_return_point;

/*
 * The name of the procedure we are currently executing; used for error messages.
 */
static s48_ref_t current_procedure = NULL;

/*
 * Stack of Scheme stack-block records which represent portions of the process
 * stack.
 */
static s48_ref_t current_stack_block = NULL;

/*
 * These need to agree with the record definition in callback.scm.
 */
#define STACK_BLOCK_FREE(stack_block)	S48_UNSAFE_RECORD_REF(stack_block, 0)
#define STACK_BLOCK_UNWIND(stack_block)	S48_UNSAFE_RECORD_REF(stack_block, 1)
#define STACK_BLOCK_PROC(stack_block)	S48_UNSAFE_RECORD_REF(stack_block, 2)
#define STACK_BLOCK_THREAD(stack_block)	S48_UNSAFE_RECORD_REF(stack_block, 3)
#define STACK_BLOCK_NEXT(stack_block)	S48_UNSAFE_RECORD_REF(stack_block, 4)

#define STACK_BLOCK_FREE_2(c, stack_block)	\
  s48_unsafe_record_ref_2(c, stack_block, 0)
#define STACK_BLOCK_UNWIND_2(c, stack_block)	\
  s48_unsafe_record_ref_2(c, stack_block, 1)
#define STACK_BLOCK_PROC_2(c, stack_block)	\
  s48_unsafe_record_ref_2(c, stack_block, 2)
#define STACK_BLOCK_THREAD_2(c, stack_block)	\
  s48_unsafe_record_ref_2(c, stack_block, 3)
#define STACK_BLOCK_NEXT_2(c, stack_block)	\
  s48_unsafe_record_ref_2(c, stack_block, 4)

#define s48_push_2(c, x) s48_push(s48_deref(x))

#ifdef DEBUG_FFI
/*
 * For debugging.
 */

static int callback_depth()
{
  int depth = 0;
  s48_value stack = s48_deref(current_stack_block);

  for(; stack != S48_FALSE; depth++, stack = STACK_BLOCK_NEXT(stack));

  return depth;
}
#endif

/*
 * The value being returned from an external call.  The returns may be preceded
 * by a longjmp(), so we stash the value here.
 */
static s48_value external_return_value;

/* Exports to Scheme */
static s48_value	s48_clear_stack_top(void);
static s48_ref_t	s48_system_2(s48_call_t call, s48_ref_t string);

/* Imports from Scheme */
static s48_ref_t 	the_record_type_binding       = NULL;
static s48_ref_t 	stack_block_type_binding      = NULL;
static s48_ref_t 	callback_binding              = NULL;
static s48_ref_t 	delay_callback_return_binding = NULL;

#ifdef DEBUG_FFI
static s48_value	s48_trampoline(s48_value proc, s48_value nargs);
#endif
static s48_ref_t        s48_trampoline_2(s48_call_t call, s48_ref_t proc, s48_ref_t nargs);

void
s48_initialize_external()
{
  the_record_type_binding = 
    s48_get_imported_binding_2("s48-the-record-type");

  stack_block_type_binding = 
    s48_get_imported_binding_2("s48-stack-block-type");

  callback_binding = 
    s48_get_imported_binding_2("s48-callback");

  delay_callback_return_binding =
    s48_get_imported_binding_2("s48-delay-callback-return");

  current_stack_block = s48_make_global_ref(_s48_value_false);
  current_procedure = s48_make_global_ref(_s48_value_false);

  S48_EXPORT_FUNCTION(s48_clear_stack_top);
  S48_EXPORT_FUNCTION(s48_system_2);

#ifdef DEBUG_FFI
  S48_EXPORT_FUNCTION(s48_trampoline);
#endif
  S48_EXPORT_FUNCTION(s48_trampoline_2);

#ifdef DEBUG_FFI
  init_debug_ffi ();
#endif
}

/* The three reasons for an extern-call longjump. */

#define NO_THROW        0
#define EXCEPTION_THROW 1
#define CLEANUP_THROW   2

/*
 * Used to call `proc' from Scheme code. `nargs' the number of arguments in
 * vector `argv'.  If `spread_p' is true the procedure is applied to the
 * arguments, otherwise `proc' is just called on `nargs' and `argv'.
 *
 * We do a setjmp() to get a return point for clearing off this portion of
 * the process stack.  This is used when `proc' calls back to Scheme and
 * then a throw transfers control up past the call to `proc'.
 */

typedef s48_value (*proc_0_t)(void);
typedef s48_value (*proc_1_t)(s48_value);
typedef s48_value (*proc_2_t)(s48_value, s48_value);
typedef s48_value (*proc_3_t)(s48_value, s48_value, s48_value);
typedef s48_value (*proc_4_t)(s48_value, s48_value, s48_value, s48_value);
typedef s48_value (*proc_5_t)(s48_value, s48_value, s48_value, s48_value,
			      s48_value);
typedef s48_value (*proc_6_t)(s48_value, s48_value, s48_value, s48_value,
			      s48_value, s48_value);
typedef s48_value (*proc_7_t)(s48_value, s48_value, s48_value, s48_value,
			      s48_value, s48_value, s48_value);
typedef s48_value (*proc_8_t)(s48_value, s48_value, s48_value, s48_value,
			      s48_value, s48_value, s48_value, s48_value);
typedef s48_value (*proc_9_t)(s48_value, s48_value, s48_value, s48_value,
			      s48_value, s48_value, s48_value, s48_value,
			      s48_value);
typedef s48_value (*proc_10_t)(s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value);
typedef s48_value (*proc_11_t)(s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value, s48_value);
typedef s48_value (*proc_12_t)(s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value, s48_value, s48_value,
			       s48_value, s48_value, s48_value, s48_value);
typedef s48_value (*proc_n_t)(int, s48_value []);

s48_value
s48_external_call(s48_value sch_proc, s48_value proc_name,
		  long nargs, char *char_argv)
{
  volatile char *gc_roots_marker;	/* volatile to survive longjumps */
  volatile s48_value name = proc_name;	/* volatile to survive longjumps */
  
#ifdef DEBUG_FFI
  int depth; 	/* debugging */
#endif

  long *argv = (long *) char_argv;

  proc_0_t proc = S48_EXTRACT_VALUE(sch_proc, proc_0_t);

  int throw_reason;

  s48_setref(current_procedure, name);

  S48_CHECK_VALUE(sch_proc);
  S48_CHECK_STRING(name);

  gc_roots_marker = s48_set_gc_roots_baseB();

#ifdef DEBUG_FFI
  depth = callback_depth();
  fprintf(stderr, "[external_call at depth %d]\n", depth);
#endif

  throw_reason = setjmp(current_return_point.buf);

  if (throw_reason == NO_THROW) {	/* initial entry */
    switch (nargs) {
    case 0:
      external_return_value = proc();
      break;
    case 1:
      external_return_value = ((proc_1_t)proc)(argv[0]);
      break;
    case 2:
      external_return_value = ((proc_2_t)proc)(argv[1], argv[0]);
      break;
    case 3:
      external_return_value = ((proc_3_t)proc)(argv[2], argv[1], argv[0]);
      break;
    case 4:
      external_return_value = ((proc_4_t)proc)(argv[3], argv[2], argv[1], argv[0]);
      break;
    case 5:
      external_return_value = ((proc_5_t)proc)(argv[4],
					       argv[3], argv[2], argv[1], argv[0]);
      break;
    case 6:
      external_return_value = ((proc_6_t)proc)(argv[5], argv[4],
					       argv[3], argv[2], argv[1], argv[0]);
      break;
    case 7:
      external_return_value = ((proc_7_t)proc)(argv[6], argv[5], argv[4],
					       argv[3], argv[2], argv[1], argv[0]);
      break;
    case 8:
      external_return_value = ((proc_8_t)proc)(argv[7], argv[6], argv[5], argv[4],
					       argv[3], argv[2], argv[1], argv[0]);
      break;
    case 9:
      external_return_value = ((proc_9_t)proc)(argv[8],
					       argv[7], argv[6], argv[5], argv[4],
					       argv[3], argv[2], argv[1], argv[0]);
      break;
    case 10:
      external_return_value = ((proc_10_t)proc)(argv[9], argv[8],
						argv[7], argv[6], argv[5], argv[4],
						argv[3], argv[2], argv[1], argv[0]);
      break;
    case 11:
      external_return_value = ((proc_11_t)proc)(argv[10], argv[9], argv[8],
						argv[7], argv[6], argv[5], argv[4],
						argv[3], argv[2], argv[1], argv[0]);
      break;
    case 12:
      external_return_value = ((proc_12_t)proc)(argv[11], argv[10], argv[9], argv[8],
						argv[7], argv[6], argv[5], argv[4],
						argv[3], argv[2], argv[1], argv[0]);
      break;
    default:
      external_return_value = ((proc_n_t)proc)((int)nargs, (s48_value *)argv);
    }

    /* Raise an exception if the user neglected to pop off some gc roots. */
    
    if (! s48_release_gc_roots_baseB((char *)gc_roots_marker)) {
      s48_raise_scheme_exception(S48_EXCEPTION_GC_PROTECTION_MISMATCH, 0);
    }
    
    /* Clear any free stack-blocks off of the top of the stack-block stack and
       then longjmp past the corresponding portions of the process stack. */
    
    if (s48_deref(current_stack_block) != S48_FALSE &&
	STACK_BLOCK_FREE(s48_deref(current_stack_block)) == S48_TRUE) {

      s48_value bottom_free_block;
      
      do {
	bottom_free_block = s48_deref(current_stack_block);
	s48_setref(current_stack_block, STACK_BLOCK_NEXT(s48_deref(current_stack_block)));
      }
      while (s48_deref(current_stack_block) != S48_FALSE &&
	     STACK_BLOCK_FREE(s48_deref(current_stack_block)) == S48_TRUE);
      
#ifdef DEBUG_FFI
      fprintf(stderr, "[Freeing stack blocks from %d to %d]\n",
	      depth,
	      callback_depth());
#endif

      longjmp(S48_EXTRACT_VALUE_POINTER(STACK_BLOCK_UNWIND(bottom_free_block),
					struct s_jmp_buf)->buf,
	      CLEANUP_THROW);
    }
  }
  else {	/* throwing an exception or unwinding the stack */
#ifdef DEBUG_FFI
    fprintf(stderr, "[external_call throw; was %d and now %d]\n",
	    depth,
	    callback_depth());
    fprintf(stderr, "[throw unrolling to %ld]\n", gc_roots_marker);
#endif
    s48_release_gc_roots_baseB((char *)gc_roots_marker);
  }

  /* Check to see if a thread is waiting to return to the next block down. */
     
  if (s48_deref(current_stack_block) != S48_FALSE &&
      STACK_BLOCK_THREAD(s48_deref(current_stack_block)) != S48_FALSE) {
#ifdef DEBUG_FFI
    fprintf(stderr, "[releasing return at %d]\n", callback_depth());
#endif

    if (throw_reason == EXCEPTION_THROW) {
      /* We are in the midst of raising an exception, so we need to piggyback
	 our exception on that one. */
      s48_value old_exception
	= s48_resetup_external_exception(S48_EXCEPTION_CALLBACK_RETURN_UNCOVERED,
					 2);
      s48_push(old_exception);
      s48_push(s48_deref(current_stack_block));
      external_return_value = S48_UNSPECIFIC;
    }
    else {
      s48_setup_external_exception(S48_EXCEPTION_CALLBACK_RETURN_UNCOVERED, 2);
      s48_push(s48_deref(current_stack_block));
      s48_push(external_return_value);
      external_return_value = S48_UNSPECIFIC;
    }
  }

  return external_return_value;
}

/*
 * The value being returned from an external call.  The returns may be preceded
 * by a longjmp(), so we stash the value here.
 */
static s48_ref_t cexternal_return_value;

typedef s48_ref_t (*cproc_0_t)(s48_call_t);
typedef s48_ref_t (*cproc_1_t)(s48_call_t,
			       s48_ref_t);
typedef s48_ref_t (*cproc_2_t)(s48_call_t,
			       s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_3_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_4_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_5_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t);
typedef s48_ref_t (*cproc_6_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_7_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_8_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_9_t)(s48_call_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
			       s48_ref_t);
typedef s48_ref_t (*cproc_10_t)(s48_call_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_11_t)(s48_call_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_12_t)(s48_call_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t,
				s48_ref_t, s48_ref_t, s48_ref_t, s48_ref_t);
typedef s48_ref_t (*cproc_n_t)(s48_call_t, int, s48_ref_t  []);

s48_value
s48_external_ecall(s48_call_t call,
		   s48_value sch_proc, s48_value proc_name,
		   long nargs, char *char_argv)
{
  volatile char *gc_roots_marker;	/* volatile to survive longjumps */
  volatile s48_value name = proc_name;	/* volatile to survive longjumps */
  s48_call_t new_call;
  s48_ref_t argv_ref[12];
  s48_ref_t sch_proc_ref, proc_name_ref;
  s48_value result;

#ifdef DEBUG_FFI
  int depth = callback_depth(); 	/* debugging */
#endif

  long *argv = (long *) char_argv;

  cproc_0_t cproc = S48_EXTRACT_VALUE(sch_proc, cproc_0_t);

  int throw_reason;

  s48_ref_t sbt = NULL;

  s48_setref(current_procedure, name);

  S48_CHECK_VALUE(sch_proc);
  S48_CHECK_STRING(name);

  gc_roots_marker = s48_set_gc_roots_baseB();

#ifdef DEBUG_FFI
  fprintf(stderr, "[external_call_2 at depth %d]\n", depth);
#endif

  throw_reason = setjmp(current_return_point.buf);

  if (throw_reason == NO_THROW) {	/* initial entry */
    long i;
    new_call = s48_push_call (call);
    for (i = 0; i < nargs; i++) 
      argv_ref[i] = s48_make_local_ref (new_call, argv[i]);
    sch_proc_ref = s48_make_local_ref (new_call, sch_proc);
    proc_name_ref = s48_make_local_ref (new_call, proc_name);

    switch (nargs) {
    case 0:
      cexternal_return_value = ((cproc_0_t)cproc)(new_call);
      break;
    case 1:
      cexternal_return_value = ((cproc_1_t)cproc)(new_call, argv_ref[0]);
      break;
    case 2:
      cexternal_return_value = ((cproc_2_t)cproc)(new_call, argv_ref[1], argv_ref[0]);
      break;
    case 3:
      cexternal_return_value = ((cproc_3_t)cproc)(new_call, argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 4:
      cexternal_return_value = ((cproc_4_t)cproc)(new_call,
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 5:
      cexternal_return_value = ((cproc_5_t)cproc)(new_call, argv_ref[4],
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 6:
      cexternal_return_value = ((cproc_6_t)cproc)(new_call, argv_ref[5], argv_ref[4],
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 7:
      cexternal_return_value = ((cproc_7_t)cproc)(new_call, argv_ref[6], argv_ref[5], argv_ref[4],
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 8:
      cexternal_return_value = ((cproc_8_t)cproc)(new_call, 
						  argv_ref[7], argv_ref[6], argv_ref[5], argv_ref[4],
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 9:
      cexternal_return_value = ((cproc_9_t)cproc)(new_call, argv_ref[8],
						  argv_ref[7], argv_ref[6], argv_ref[5], argv_ref[4],
						  argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 10:
      cexternal_return_value = ((cproc_10_t)cproc)(new_call, argv_ref[9], argv_ref[8],
						   argv_ref[7], argv_ref[6], argv_ref[5], argv_ref[4],
						   argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 11:
      cexternal_return_value = ((cproc_11_t)cproc)(new_call, argv_ref[10], argv_ref[9], argv_ref[8],
						   argv_ref[7], argv_ref[6], argv_ref[5], argv_ref[4],
						   argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    case 12:
      cexternal_return_value = ((cproc_12_t)cproc)(new_call,
						   argv_ref[11], argv_ref[10], argv_ref[9], argv_ref[8],
						   argv_ref[7], argv_ref[6], argv_ref[5], argv_ref[4],
						   argv_ref[3], argv_ref[2], argv_ref[1], argv_ref[0]);
      break;
    default:
      cexternal_return_value = ((cproc_n_t)cproc)(new_call, (int) nargs, argv_ref);
    }

    /* Raise an exception if the user neglected to pop off some gc roots. */
    
    if (! s48_release_gc_roots_baseB((char *)gc_roots_marker)) {
      s48_raise_scheme_exception(S48_EXCEPTION_GC_PROTECTION_MISMATCH, 0);
    }
    
    /* Clear any free stack-blocks off of the top of the stack-block stack and
       then longjmp past the corresponding portions of the process stack. */
    
    if (!s48_false_p_2(new_call, current_stack_block) &&
	s48_true_p_2(new_call, STACK_BLOCK_FREE_2(new_call, current_stack_block))) {

      s48_ref_t bottom_free_block;
      
      do {
	s48_setref(bottom_free_block, s48_deref(current_stack_block));
	s48_setref(current_stack_block, s48_deref(STACK_BLOCK_NEXT_2(new_call, current_stack_block)));
      }
      while (!s48_false_p_2(new_call, current_stack_block) &&
	     s48_false_p_2(new_call, STACK_BLOCK_FREE_2(new_call, current_stack_block)));
      
#ifdef DEBUG_FFI
      fprintf(stderr, "[Freeing stack blocks from %d to %d]\n",
	      depth,
	      callback_depth());
#endif

      longjmp(s48_extract_value_pointer_2(new_call, 
					  STACK_BLOCK_UNWIND_2(new_call, bottom_free_block),
					  struct s_jmp_buf)->buf,
	      CLEANUP_THROW);
    }
  }
  else {	/* throwing an exception or unwinding the stack */
#ifdef DEBUG_FFI
    fprintf(stderr, "[external_call_2 throw; was %d and now %d]\n",
	    depth,
	    callback_depth());
    fprintf(stderr, "[throw unrolling to %ld]\n", gc_roots_marker);
#endif
    s48_release_gc_roots_baseB((char *)gc_roots_marker);
  }

  /* otherwise the pop_to will kill us */
  if (cexternal_return_value)
	cexternal_return_value = s48_copy_local_ref(call, cexternal_return_value);

  s48_pop_to (call);

  if (cexternal_return_value)
    result = s48_deref(cexternal_return_value);
  else
    result = S48_UNSPECIFIC;

  /* Check to see if a thread is waiting to return to the next block down. */
  if (!s48_false_p_2(call, current_stack_block) &&
      !s48_false_p_2(call, sbt = STACK_BLOCK_THREAD_2(call, current_stack_block))) {
#ifdef DEBUG_FFI
    fprintf(stderr, "[releasing return at %d]\n", callback_depth());
#endif

    if (throw_reason == EXCEPTION_THROW) {
      /* We are in the midst of raising an exception, so we need to piggyback
	 our exception on that one. */
      s48_value old_exception
	= s48_resetup_external_exception(S48_EXCEPTION_CALLBACK_RETURN_UNCOVERED,
					 2);
      s48_push(old_exception);
      s48_push_2(call, current_stack_block);

      if (cexternal_return_value)
	s48_free_local_ref(call, cexternal_return_value);

      result = S48_UNSPECIFIC;
    } else {
      if (cexternal_return_value) {
      s48_setup_external_exception(S48_EXCEPTION_CALLBACK_RETURN_UNCOVERED, 2);
      s48_push_2(call, current_stack_block);
      s48_push_2(call, cexternal_return_value);
      } else {
	s48_setup_external_exception(S48_EXCEPTION_CALLBACK_RETURN_UNCOVERED, 1);
	s48_push_2(call, current_stack_block);
      }
      result = S48_UNSPECIFIC;
    }
  } else {
    if (cexternal_return_value)
      s48_free_local_ref(call, cexternal_return_value);
  }

  if(sbt != NULL)
      s48_free_local_ref(call, sbt);

  return result;
}

s48_value
s48_external_call_2(s48_value sch_proc, s48_value proc_name,
		    long nargs, char *char_argv)
{
  return s48_external_ecall (s48_get_current_call(), sch_proc,
			     proc_name, nargs, char_argv);
}

/*
 * Call Scheme function `proc' from C.  We push the call-back depth, `proc',
 * and the arguments on the Scheme stack and then restart the VM.  The restarted
 * VM calls the Scheme procedure `callback' which wraps the call to `proc' with
 * a dynamic-wind.  This prevents downward throws back into the call to `proc',
 * which C can't handle, and allows the C stack to be cleaned up if an upward
 * throw occurs.
 *
 * The maximum number of arguments is determined by the amount of space reserved
 * on the Scheme stack for exceptions. See the definition of stack-slack in
 * scheme/vm/stack.scm.
 */
s48_value
s48_call_scheme(s48_value proc, long nargs, ...)
{
  int i;
  va_list arguments;
  s48_value value;
  s48_value unwind, stack_block;
  S48_DECLARE_GC_PROTECT(2);

  S48_GC_PROTECT_2(unwind, proc);
  
  va_start(arguments, nargs);

  S48_SHARED_BINDING_CHECK(s48_deref(callback_binding));

  /* It would be nice to push a list of the arguments, but we have no way
     of preserving them across a cons. */
  if (nargs < 0 || 12 < nargs) {  /* DO NOT INCREASE THIS NUMBER */
    s48_value sch_nargs = s48_enter_integer(nargs);  /* `proc' is protected */
    s48_raise_scheme_exception(S48_EXCEPTION_TOO_MANY_ARGUMENTS_IN_CALLBACK,
			       2, proc, sch_nargs);
  }

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme, %ld args, depth %d]\n",
	  nargs, callback_depth());
#endif

  s48_push(S48_UNSPECIFIC);	/* placeholder */
  s48_push(proc);
  for (i = 0; i < nargs; i++)
    s48_push(va_arg(arguments, s48_value));

  va_end(arguments);

  /* With everything safely on the stack we can do the necessary allocation. */

  unwind = S48_MAKE_VALUE(struct s_jmp_buf);
  S48_EXTRACT_VALUE(unwind, struct s_jmp_buf) = current_return_point;

  stack_block = s48_make_record(s48_deref(stack_block_type_binding));
  STACK_BLOCK_UNWIND(stack_block) = unwind;
  STACK_BLOCK_PROC(stack_block) = s48_deref(current_procedure);
  STACK_BLOCK_NEXT(stack_block) = s48_deref(current_stack_block);
  STACK_BLOCK_FREE(stack_block) = S48_FALSE;
  STACK_BLOCK_THREAD(stack_block) = S48_FALSE;

  S48_GC_UNPROTECT();		/* no more references to `unwind' or `proc'. */

  s48_setref(current_stack_block, stack_block);

#ifdef DEBUG_FFI
  if(s48_stack_ref(nargs + 1) != S48_UNSPECIFIC)
    fprintf(stderr, "[stack_block set missed]\n");
#endif

  s48_stack_setB(nargs + 1, stack_block);

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme, %ld args, depth %d, off we go]\n",
	  nargs, callback_depth());
#endif

  value = s48_restart(S48_UNSAFE_SHARED_BINDING_REF(s48_deref(callback_binding)),
		      nargs + 2);

  for (;s48_Scallback_return_stack_blockS != s48_deref(current_stack_block);) {
    if (s48_Scallback_return_stack_blockS == S48_FALSE) {

#ifdef DEBUG_FFI
      fprintf(stderr, "[s48_call_scheme returning from VM %ld]\n", callback_depth());
#endif

      exit(value);
    }
    else {

      /* Someone has returned (because of threads) to the wrong section of the
	 C stack.  We call back to a Scheme procedure that will suspend until
	 our block is at the top of the stack. */

      s48_push(s48_Scallback_return_stack_blockS);
      s48_push(S48_UNSAFE_SHARED_BINDING_REF(s48_deref(delay_callback_return_binding)));
      s48_push(s48_Scallback_return_stack_blockS);
      s48_push(value);

#ifdef DEBUG_FFI
      fprintf(stderr, "[Premature return, %ld args, depth %d, back we go]\n",
	      nargs, callback_depth());
#endif

      s48_disable_interruptsB();
      value = s48_restart(S48_UNSAFE_SHARED_BINDING_REF(s48_deref(callback_binding)), 4);
    }
  }

  /* Restore the state of the current stack block. */

  unwind = STACK_BLOCK_UNWIND(s48_deref(current_stack_block));
  current_return_point = S48_EXTRACT_VALUE(unwind, struct s_jmp_buf);
  s48_setref(current_procedure, STACK_BLOCK_PROC(s48_deref(current_stack_block)));
  s48_setref(current_stack_block, STACK_BLOCK_NEXT(s48_deref(current_stack_block)));

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme returns from depth %d]\n", callback_depth());
#endif

  return value;
}

s48_ref_t 
s48_call_scheme_2(s48_call_t call, s48_ref_t proc, long nargs, ...)
{
  int i;
  va_list arguments;
  s48_value value;
  s48_ref_t unwind;
  s48_value stack_block;

  va_start(arguments, nargs);

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme_2, %ld args, depth %d]\n",
	  nargs, callback_depth());
#endif
  
  s48_copy_local_bvs_to_scheme (call);

  s48_shared_binding_check_2(call, callback_binding);

  /* It would be nice to push a list of the arguments, but we have no way
     of preserving them across a cons. */
  if (nargs < 0 || 12 < nargs) {  /* DO NOT INCREASE THIS NUMBER */
    s48_value sch_nargs = s48_enter_integer(nargs);  /* `proc' is protected */
    s48_raise_scheme_exception(S48_EXCEPTION_TOO_MANY_ARGUMENTS_IN_CALLBACK,
			       2, s48_deref(proc), sch_nargs);
  }

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme_2, %ld args, depth %d]\n",
	  nargs, callback_depth());
#endif

  s48_push(S48_UNSPECIFIC);	/* placeholder */
  s48_push(s48_deref(proc));
  for (i = 0; i < nargs; i++) {
    s48_ref_t ref = va_arg(arguments, s48_ref_t);
#ifdef DEBUG_FFI
    fprintf(stderr, "call_scheme_2: pushing arg %d ref %x\n", i, ref);
#endif
    s48_push(s48_deref(ref));
  }

  va_end(arguments);

  /* With everything safely on the stack we can do the necessary allocation. */

  unwind = s48_make_value_2(call, struct s_jmp_buf);
  s48_extract_value_2(call, unwind, struct s_jmp_buf) = current_return_point;

  stack_block = s48_make_record(s48_deref(stack_block_type_binding));
  STACK_BLOCK_UNWIND(stack_block) = s48_deref(unwind);
  STACK_BLOCK_PROC(stack_block) = s48_deref(current_procedure);
  STACK_BLOCK_NEXT(stack_block) = s48_deref(current_stack_block);
  STACK_BLOCK_FREE(stack_block) = S48_FALSE;
  STACK_BLOCK_THREAD(stack_block) = S48_FALSE;

  s48_setref(current_stack_block, stack_block);

#ifdef DEBUG_FFI
  if(s48_stack_ref(nargs + 1) != S48_UNSPECIFIC)
    fprintf(stderr, "[stack_block set missed]\n");
#endif

  s48_stack_setB(nargs + 1, stack_block);

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme_2, %ld args, depth %d, off we go]\n",
	  nargs, callback_depth());
#endif

  value = s48_restart(s48_deref(s48_unsafe_shared_binding_ref_2(call, callback_binding)),
		      nargs + 2);

  for (;s48_Scallback_return_stack_blockS != s48_deref(current_stack_block);) {
    if (s48_Scallback_return_stack_blockS == S48_FALSE) {

#ifdef DEBUG_FFI
      fprintf(stderr, "[s48_call_scheme_2 returning from VM %ld]\n", callback_depth());
#endif

      exit(value);
    }
    else {

      /* Someone has returned (because of threads) to the wrong section of the
	 C stack.  We call back to a Scheme procedure that will suspend until
	 our block is at the top of the stack. */

      s48_push(s48_Scallback_return_stack_blockS);
      s48_push_2(call, s48_unsafe_shared_binding_ref_2(call, delay_callback_return_binding));
      s48_push(s48_Scallback_return_stack_blockS);
      s48_push(value);

#ifdef DEBUG_FFI
      fprintf(stderr, "[Premature return, %ld args, depth %d, back we go]\n",
	      nargs, callback_depth());
#endif

      s48_disable_interruptsB();
      value = s48_restart(s48_deref(s48_unsafe_shared_binding_ref_2(call, callback_binding)), 4);
    }
  }

  /* Restore the state of the current stack block. */

  unwind = STACK_BLOCK_UNWIND_2(call, current_stack_block);
  current_return_point = s48_extract_value_2(call, unwind, struct s_jmp_buf);
  s48_setref(current_procedure, s48_deref(STACK_BLOCK_PROC_2(call, current_stack_block)));
  s48_setref(current_stack_block, s48_deref(STACK_BLOCK_NEXT_2(call, current_stack_block)));

#ifdef DEBUG_FFI
  fprintf(stderr, "[s48_call_scheme_2 returns from depth %d]\n", callback_depth());
#endif

  s48_copy_local_bvs_from_scheme (call);

  return s48_make_local_ref (call, value);
}

/*
 * Because the top of the stack is cleared on the return from every external
 * call, this doesn't have to do anything but exist.
 */
static s48_value
s48_clear_stack_top()
{
#ifdef DEBUG_FFI
  fprintf(stderr, "[Clearing stack top]\n");
#endif
  return S48_UNSPECIFIC;
}

#ifdef DEBUG_FFI
/*
 * For testing callbacks.  This just calls its argument on the specified number
 * of values.
 */
static s48_value
s48_trampoline(s48_value proc, s48_value nargs)
{

  fprintf(stderr, "[C trampoline, %ld args]\n", S48_UNSAFE_EXTRACT_FIXNUM(nargs));

  switch (s48_extract_fixnum(nargs)) {
  case -2: { /* provoke exception: GC protection mismatch */
    S48_DECLARE_GC_PROTECT(1);
    
    S48_GC_PROTECT_1(proc);

    return S48_FALSE;
  }
  case -1: { /* this is broken, dunno what this should do, anyway --Marcus */
    long n = - s48_extract_integer(proc);
    fprintf(stderr, "[extract magnitude is %ld (%lx)]\n", n, n);
    return s48_enter_integer(n);
  }
  case 0: {
    s48_value value = s48_call_scheme(proc, 0);
    if (value == S48_FALSE)
      s48_assertion_violation("s48_trampoline", "trampoline bouncing", 0);
    return value;
  }
  case 1:
    return s48_call_scheme(proc, 1, s48_enter_fixnum(100));
  case 2:
    return s48_call_scheme(proc, 2, s48_enter_fixnum(100), s48_enter_fixnum(200));
  case 3:
    return s48_call_scheme(proc, 3, s48_enter_fixnum(100), s48_enter_fixnum(200),
		    s48_enter_fixnum(300));
  default:
    s48_assertion_violation("s48_trampoline", "invalid number of arguments", 1, nargs);
    return S48_UNDEFINED; /* not that we ever get here */
  }
}
#endif

static s48_ref_t 
s48_trampoline_2(s48_call_t call, s48_ref_t proc, s48_ref_t nargs)
{
#ifdef DEBUG_FFI
  fprintf(stderr, "[C trampoline_2, %ld args]\n", s48_unsafe_extract_long_2(call, nargs));
#endif
  switch (s48_extract_long_2(call, nargs)) {
  case -2: { /* provoke exception: GC protection mismatch */
    S48_DECLARE_GC_PROTECT(1);
    
    S48_GC_PROTECT_1(proc);

    return s48_false_2(call);
  }
  case 0: {
    s48_ref_t result = s48_call_scheme_2(call, proc, 0);
    if (s48_false_p_2(call, result))
      s48_assertion_violation_2(call, "s48_trampoline_2", "trampoline bouncing", 0);
    return result;
  }
  case 1:
    return s48_call_scheme_2(call, proc, 1, 
			     s48_make_local_ref (call, s48_enter_fixnum(100)));
  case 2:
    return s48_call_scheme_2(call, proc, 2, 
			     s48_make_local_ref (call, s48_enter_fixnum(100)),
			     s48_make_local_ref (call, s48_enter_fixnum(200)));
  case 3:
    return s48_call_scheme_2(call, proc, 3, 
			     s48_make_local_ref (call, s48_enter_fixnum(100)),
			     s48_make_local_ref (call, s48_enter_fixnum(200)),
			     s48_make_local_ref (call, s48_enter_fixnum(300)));
  default:
    s48_assertion_violation_2(call, "s48_trampoline_2", "invalid number of arguments", 1, nargs);
    return s48_undefined_2(call); /* not that we ever get here */
  }
}

static s48_ref_t
s48_system_2(s48_call_t call, s48_ref_t string)
{
  return s48_enter_long_2(call, 
			  system(s48_false_p_2(call, string)
				 ? NULL
				 : s48_extract_byte_vector_readonly_2(call, string)));
}

/********************************/
/*
 * Raising exceptions.  We push the arguments on the stack end then throw out
 * of the most recent call from Scheme.
 *
 * The maximum number of arguments is determined by the amount of space reserved
 * on the Scheme stack for exceptions. See the definition of stack-slack in
 * scheme/vm/interp/stack.scm.
 */

static long
raise_scheme_exception_prelude(long why, long nargs)
{
  s48_setup_external_exception(why, nargs);
  
  if (11 < nargs) {   /* DO NOT INCREASE THIS NUMBER */
    fprintf(stderr, "too many arguments to external exception, discarding surplus\n");
    nargs = 11;
  }
  return nargs;
}

static long
raise_scheme_exception_prelude_2(s48_call_t call, long why, long nargs)
{
  s48_copy_local_bvs_to_scheme(call);
  return raise_scheme_exception_prelude(why, nargs);
}

static void
raise_scheme_exception_postlude(void)
{
   external_return_value = S48_UNSPECIFIC;
   longjmp(current_return_point.buf, EXCEPTION_THROW);
}

void
s48_raise_scheme_exception(long why, long nargs, ...)
{
  int i;
  va_list irritants;

  nargs = raise_scheme_exception_prelude(why, nargs + 1) - 1;

  s48_push(s48_deref(current_procedure));

  va_start(irritants, nargs);

  for (i = 0; i < nargs; i++)
    s48_push(va_arg(irritants, s48_value));

  va_end(irritants);

  raise_scheme_exception_postlude();
}

void
s48_raise_scheme_exception_2(s48_call_t call, long why, long nargs, ...)
{
  int i;
  va_list irritants;

  nargs = raise_scheme_exception_prelude_2(call, why, nargs + 1) - 1;

  s48_push_2(call, current_procedure);

  va_start(irritants, nargs);

  for (i = 0; i < nargs; i++)
    s48_push_2(call, va_arg(irritants, s48_ref_t));

  va_end(irritants);

  raise_scheme_exception_postlude();
}

static void
raise_scheme_standard_exception(long why, const char* who, const char* message,
				long irritant_count, va_list irritants)
{
  int i;
  long nargs = irritant_count + 2; /* who and message */

  nargs = raise_scheme_exception_prelude(why, nargs);
  irritant_count = nargs - 2;
  
  for (i = 0; i < irritant_count; i++)
    s48_push(va_arg(irritants, s48_value));

  va_end(irritants);

  /* these must be last because of GC protection */
  if (who == NULL)
    s48_push(s48_deref(current_procedure));
  else
    s48_push(s48_enter_string_utf_8((char*)who));
  s48_push(s48_enter_byte_string((char*)message));

  raise_scheme_exception_postlude();
}

static void
raise_scheme_standard_exception_2(s48_call_t call, long why, const char* who, const char* message,
				  long irritant_count, va_list irritants)
{
  int i;
  long nargs = irritant_count + 2; /* who and message */

  nargs = raise_scheme_exception_prelude_2(call, why, nargs);
  irritant_count = nargs - 2;
  
  for (i = 0; i < irritant_count; i++)
    s48_push_2(call, va_arg(irritants, s48_ref_t));

  va_end(irritants);

  /* these must be last because of GC protection */
  if (who == NULL)
    s48_push_2(call, current_procedure);
  else
    s48_push_2(call, s48_enter_string_utf_8_2(call, (char*) who));
  s48_push_2(call, s48_enter_byte_string_2(call, (char*) message));

  raise_scheme_exception_postlude();
}

/* Specific exceptions */

void
s48_error(const char* who, const char* message,
	  long irritant_count, ...)
{
  va_list irritants;
  va_start(irritants, irritant_count);
  raise_scheme_standard_exception(S48_EXCEPTION_EXTERNAL_ERROR,
				  who, message, irritant_count, irritants);
}

void
s48_error_2(s48_call_t call, const char* who, const char* message,
	    long irritant_count, ...)
{
  va_list irritants;
  va_start(irritants, irritant_count);
  raise_scheme_standard_exception_2(call, S48_EXCEPTION_EXTERNAL_ERROR,
				    who, message, irritant_count, irritants);
}

void
s48_assertion_violation(const char* who, const char* message,
			long irritant_count, ...)
{
  va_list irritants;
  va_start(irritants, irritant_count);
  raise_scheme_standard_exception(S48_EXCEPTION_EXTERNAL_ASSERTION_VIOLATION,
				  who, message, irritant_count, irritants);
}

void
s48_assertion_violation_2(s48_call_t call, const char* who, const char* message,
			  long irritant_count, ...)
{
  va_list irritants;
  va_start(irritants, irritant_count);
  raise_scheme_standard_exception_2(call, S48_EXCEPTION_EXTERNAL_ASSERTION_VIOLATION,
				    who, message, irritant_count, irritants);
}

void
s48_os_error(const char* who, int the_errno,
	     long irritant_count, ...)
{
  int i;
  long nargs = irritant_count + 2; /* who and errno */
  va_list irritants;

  nargs = raise_scheme_exception_prelude(S48_EXCEPTION_EXTERNAL_OS_ERROR, nargs);
  irritant_count = nargs - 2;
  
  va_start(irritants, irritant_count);

  for (i = 0; i < irritant_count; i++)
    s48_push(va_arg(irritants, s48_value));

  va_end(irritants);

  /* last because of GC protection */
  if (who == NULL)
    s48_push(s48_deref(current_procedure));
  else
    s48_push(s48_enter_string_utf_8((char*)who));
  s48_push(s48_enter_fixnum(the_errno));

  raise_scheme_exception_postlude();
}

void
s48_os_error_2(s48_call_t call, const char* who, int the_errno, 
	       long irritant_count, ...)
{
  int i;
  long nargs = irritant_count + 2; /* who and errno */
  va_list irritants;

  nargs = raise_scheme_exception_prelude_2(call, S48_EXCEPTION_EXTERNAL_OS_ERROR, nargs);
  irritant_count = nargs - 2;
  
  va_start(irritants, irritant_count);

  for (i = 0; i < irritant_count; i++)
    s48_push_2(call, va_arg(irritants, s48_ref_t));

  va_end(irritants);

  /* last because of GC protection */
  if (who == NULL)
    s48_push_2(call, current_procedure);
  else
    s48_push_2(call, s48_enter_string_utf_8_2(call, who));
  s48_push_2(call, s48_enter_long_as_fixnum_2(call, the_errno));

  raise_scheme_exception_postlude();
}

void
s48_out_of_memory_error()
{
  s48_raise_scheme_exception(S48_EXCEPTION_OUT_OF_MEMORY, 0);
}

void
s48_out_of_memory_error_2(s48_call_t call)
{
  s48_raise_scheme_exception_2(call, S48_EXCEPTION_OUT_OF_MEMORY, 0);
}

/* For internal use by the VM: */

void
s48_argument_type_violation(s48_value value) {
  s48_assertion_violation(NULL, "argument-type violation", 1, value);
}

void
s48_argument_type_violation_2(s48_call_t call, s48_ref_t value) {
  s48_assertion_violation_2(call, NULL, "argument-type violation", 1, value);
}

void
s48_range_violation(s48_value value, s48_value min, s48_value max) {
  s48_assertion_violation(NULL, "argument out of range", 3, value, min, max);
}

void
s48_range_violation_2(s48_call_t call, s48_ref_t value, s48_ref_t min, s48_ref_t max) {
  s48_assertion_violation_2(call, NULL, "argument out of range", 3, value, min, max);
}

/* The following are deprecated: */

void
s48_raise_argument_type_error(s48_value value) {
  s48_raise_scheme_exception(S48_EXCEPTION_WRONG_TYPE_ARGUMENT, 1, value);
}

void
s48_raise_argument_number_error(s48_value value, s48_value min, s48_value max) {
  s48_raise_scheme_exception(S48_EXCEPTION_WRONG_NUMBER_OF_ARGUMENTS,
			     3, value, min, max);
}

void
s48_raise_range_error(s48_value value, s48_value min, s48_value max) {
  s48_raise_scheme_exception(S48_EXCEPTION_INDEX_OUT_OF_RANGE,
			     3, value, min, max);
}

void
s48_raise_closed_channel_error() {
  s48_raise_scheme_exception(S48_EXCEPTION_CLOSED_CHANNEL, 0);
}

void
s48_raise_os_error(int the_errno) {
  s48_os_error(NULL, the_errno, 0);
}

void
s48_raise_string_os_error(char *reason) {
  s48_error(NULL, (const char*)s48_enter_string_latin_1(reason), 0);
}

void
s48_raise_out_of_memory_error() {
  s48_raise_scheme_exception(S48_EXCEPTION_OUT_OF_MEMORY, 0);
}


/********************************/
/* Support routines for external code */

/*
 * Type-safe procedures for checking types and dereferencing and setting slots.
 */

int
s48_stob_has_type(s48_value thing, int type)
{
  return S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type);
}

int
s48_stob_has_type_2(s48_call_t call, s48_ref_t thing, int type)
{
  return s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type);
}

long
s48_stob_length(s48_value thing, int type)
{
  if (!(S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type)))
    s48_assertion_violation("s48_stob_length", "not a stob", 1, thing);
  
  return S48_STOB_DESCRIPTOR_LENGTH(thing);
}

long
s48_stob_length_2(s48_call_t call, s48_ref_t thing, int type)
{
  if (!(s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type)))
    s48_assertion_violation_2(call, "s48_stob_length_2", "not a stob", 1, thing);
  
  return s48_unsafe_stob_descriptor_length_2(call, thing);
}

long
s48_stob_byte_length(s48_value thing, int type)
{
  if (!(S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type)))
    s48_assertion_violation("s48_stob_byte_length", "not a stob", 1, thing);

  if (type == S48_STOBTYPE_STRING)
    return S48_STOB_BYTE_LENGTH(thing) - 1;
  else
    return S48_STOB_BYTE_LENGTH(thing);
}

long
s48_stob_byte_length_2(s48_call_t call, s48_ref_t thing, int type)
{
  if (!(s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type)))
    s48_assertion_violation_2(call, "s48_stob_byte_length_2", "not a stob", 1, thing);

  if (type == S48_STOBTYPE_STRING)
    return s48_unsafe_stob_byte_length_2(call, thing) - 1;
  else
    return s48_unsafe_stob_byte_length_2(call, thing);
}


s48_value
s48_stob_ref(s48_value thing, int type, long offset)
{
  long length;

  if (!(S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type)))
    s48_assertion_violation("s48_stob_ref", "not a stob", 1, thing);

  length = S48_STOB_DESCRIPTOR_LENGTH(thing);

  if (offset < 0 || length <= offset)
    s48_assertion_violation("s48_stob_ref", "invalid stob index", 3,
			    s48_enter_integer(offset),
			    S48_UNSAFE_ENTER_FIXNUM(0),
			    S48_UNSAFE_ENTER_FIXNUM(length - 1));
			  
  return S48_STOB_REF(thing, offset);
}

s48_ref_t
s48_stob_ref_2(s48_call_t call, s48_ref_t thing, int type, long offset)
{
  long length;

  if (!(s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type)))
    s48_assertion_violation_2(call, "s48_stob_ref_2", "not a stob", 1, thing);

  length = s48_unsafe_stob_descriptor_length_2(call, thing);

  if (offset < 0 || length <= offset)
    s48_assertion_violation_2(call, "s48_stob_ref_2", "invalid stob index", 3,
			      s48_enter_long_2(call, offset),
			      s48_unsafe_enter_long_as_fixnum_2(call, 0),
			      s48_unsafe_enter_long_as_fixnum_2(call, length - 1));
			  
  return s48_unsafe_stob_ref_2(call, thing, offset);
}

void
s48_stob_set(s48_value thing, int type, long offset, s48_value value)
{
  long length;
  
  if (!(S48_STOB_P(thing) &&
	(S48_STOB_TYPE(thing) == type) &&
	!S48_STOB_IMMUTABLEP(thing)))
    s48_assertion_violation("s48_stob_set", "not a mutable stob", 1, thing);
  
  length = S48_STOB_DESCRIPTOR_LENGTH(thing);

  if (offset < 0 || length <= offset)
    s48_assertion_violation("s48_stob_set", "invalid stob index", 3,
			    s48_enter_integer(offset),
			    S48_UNSAFE_ENTER_FIXNUM(0),
			    S48_UNSAFE_ENTER_FIXNUM(length - 1));
			  
  S48_STOB_SET(thing, offset, value);
}

void
s48_stob_set_2(s48_call_t call, s48_ref_t thing, int type, long offset, s48_ref_t value)
{
  long length;
  
  if (!(s48_stob_p_2(call, thing) &&
	(s48_stob_type_2(call, thing) == type) &&
	!s48_stob_immutablep_2(call, thing)))
    s48_assertion_violation_2(call, "s48_stob_set_2",
			      "not a mutable stob", 1, thing);
  
  length = s48_unsafe_stob_descriptor_length_2(call, thing);

  if (offset < 0 || length <= offset)
    s48_assertion_violation_2(call, "s48_stob_set_2", "invalid stob index", 3,
			      s48_enter_integer(offset),
			      s48_unsafe_enter_long_as_fixnum_2(call, 0),
			      s48_unsafe_enter_long_as_fixnum_2(call, length - 1));
			  
  s48_unsafe_stob_set_2(call, thing, offset, value);
}

char
s48_stob_byte_ref(s48_value thing, int type, long offset)
{
  long length;

  if (!(S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type)))
    s48_assertion_violation("s48_stob_byte_ref", "not a stob", 1, thing);
  
  length = (type == S48_STOBTYPE_STRING) ?
           S48_STOB_BYTE_LENGTH(thing) - 1 :
           S48_STOB_BYTE_LENGTH(thing);
  
  if (offset < 0 || length <= offset)
    s48_assertion_violation("s48_stob_byte_ref", "invalid stob index", 3,
			    s48_enter_integer(offset),
			    S48_UNSAFE_ENTER_FIXNUM(0),
			    S48_UNSAFE_ENTER_FIXNUM(length - 1));
			  
  return S48_STOB_BYTE_REF(thing, offset);
}

char
s48_stob_byte_ref_2(s48_call_t call, s48_ref_t thing, int type, long offset)
{
  long length;

  if (!(s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type)))
    s48_assertion_violation_2(call, "s48_stob_byte_ref_2", "not a stob", 1, thing);
  
  length = (type == s48_stobtype_string) ?
           s48_unsafe_stob_byte_length_2(call, thing) - 1 :
           s48_unsafe_stob_byte_length_2(call, thing);
  
  if (offset < 0 || length <= offset)
    s48_assertion_violation_2(call, "s48_stob_byte_ref_2", "invalid stob index", 3,
			      s48_enter_integer(offset),
			      s48_unsafe_enter_long_as_fixnum_2(call, 0),
			      s48_unsafe_enter_long_as_fixnum_2(call, length - 1));
			  
  return s48_unsafe_stob_byte_ref_2(call, thing, offset);
}

void
s48_stob_byte_set(s48_value thing, int type, long offset, char value)
{
  long length;

  if (!(S48_STOB_P(thing) && (S48_STOB_TYPE(thing) == type)))
    s48_assertion_violation("s48_stob_byte_set", "not a stob", 1, thing);
  
  length = (type == S48_STOBTYPE_STRING) ?
           S48_STOB_BYTE_LENGTH(thing) - 1 :
           S48_STOB_BYTE_LENGTH(thing);
  
  if (offset < 0 || length <= offset)
    s48_assertion_violation("s48_stob_byte_set", "invalid stob index", 3,
			    s48_enter_integer(offset),
			    S48_UNSAFE_ENTER_FIXNUM(0),
			    S48_UNSAFE_ENTER_FIXNUM(length - 1));
			  
  S48_STOB_BYTE_SET(thing, offset, value);
}

void
s48_stob_byte_set_2(s48_call_t call, s48_ref_t thing, int type, long offset, char value)
{
  long length;

  if (!(s48_stob_p_2(call, thing) && (s48_stob_type_2(call, thing) == type)))
    s48_assertion_violation_2(call, "s48_stob_byte_set_2", "not a stob", 1, thing);
  
  length = (type == S48_STOBTYPE_STRING) ?
           s48_unsafe_stob_byte_length_2(call, thing) - 1 :
           s48_unsafe_stob_byte_length_2(call, thing);
  
  if (offset < 0 || length <= offset)
    s48_assertion_violation_2(call, "s48_stob_byte_set_2", "invalid stob index", 3,
			      s48_enter_integer(offset),
			      s48_unsafe_enter_long_as_fixnum_2(call, 0),
			      s48_unsafe_enter_long_as_fixnum_2(call, length - 1));
			  
  s48_unsafe_stob_byte_set_2(call, thing, offset, value);
}

void *
s48_value_pointer(s48_value value)
{
  S48_CHECK_VALUE(value);

  return S48_ADDRESS_AFTER_HEADER(value, void *);
}

void *
s48_value_pointer_2(s48_call_t call, s48_ref_t value)
{
  s48_check_value_2(call, value);

  return s48_address_after_header_2(call, value, void *);
}

/********************************/
/* Numbers, characters, and pointers. */

/*
 * These two functions have the same range as the unsafe macros, but they signal
 * an error if things go wrong, instead of silently producing garbage.  Unlike
 * the integer versions they cannot cause a GC.
 */

s48_value
s48_enter_fixnum(long value)
{
  if (value < S48_MIN_FIXNUM_VALUE || S48_MAX_FIXNUM_VALUE < value)
    s48_assertion_violation("s48_enter_fixnum", "not a fixnum", 1, s48_enter_integer(value));

  return S48_UNSAFE_ENTER_FIXNUM(value);
}

s48_ref_t
s48_enter_long_as_fixnum_2(s48_call_t call, long value)
{
  if (value < S48_MIN_FIXNUM_VALUE || S48_MAX_FIXNUM_VALUE < value)
    s48_assertion_violation_2(call, "s48_enter_long_as_fixnum_2", "not a fixnum",
			      1, s48_enter_long_2(call, value));

  return s48_unsafe_enter_long_as_fixnum_2(call, value);
}

long
s48_extract_fixnum(s48_value value)
{
  if (! S48_FIXNUM_P(value))
    s48_assertion_violation("s48_extract_fixnum", "not a fixnum", 1, value);
  
  return S48_UNSAFE_EXTRACT_FIXNUM(value);
}

/* If we have a fixnum we just extract it. For bignums call the
 * functions in bignum.c.
 */

s48_ref_t
s48_enter_long_2(s48_call_t call, long value)
{
  return s48_make_local_ref(call, s48_enter_integer(value));
}

long
s48_extract_integer(s48_value value)
{
  if (S48_FIXNUM_P(value))
    return S48_UNSAFE_EXTRACT_FIXNUM(value);

  if (S48_BIGNUM_P(value)){
    bignum_type bignum = S48_ADDRESS_AFTER_HEADER(value, long);
    
    if (! s48_bignum_fits_in_word_p(bignum, 32, 1))
      s48_assertion_violation("s48_extract_integer", "does not fit in word", 1, value);
    else return s48_bignum_to_long(bignum);
  }
  else s48_assertion_violation("s48_extract_integer", "not an exact integer", 1, value);
}

long
s48_extract_long_2(s48_call_t call, s48_ref_t value)
{
  if (s48_fixnum_p_2(call, value))
    return s48_unsafe_extract_long_2(call, value);

  if (s48_bignum_p_2(call, value)){
    bignum_type bignum = s48_address_after_header_2(call, value, long);
    
    if (! s48_bignum_fits_in_word_p(bignum, sizeof(long) * BITS_PER_BYTE, 1))
      s48_assertion_violation_2(call, "s48_extract_long_2",
				"does not fit in word", 1, value);
    else return s48_bignum_to_long(bignum);
  }
  else s48_assertion_violation_2(call, "s48_extract_long_2",
				 "not an exact integer", 1, value);
}

s48_ref_t
s48_enter_unsigned_long_2(s48_call_t call, unsigned long value)
{
  return s48_make_local_ref(call, s48_enter_unsigned_integer(value));
}

unsigned long
s48_extract_unsigned_integer(s48_value value)
{
  if (S48_FIXNUM_P(value))
    {
      long fixnum = S48_UNSAFE_EXTRACT_FIXNUM(value);
      if (fixnum < 0)
	s48_assertion_violation("s48_extract_unsigned_integer", "negative argument", 1,
				value);
      return (unsigned long) fixnum;
    }

  if (S48_BIGNUM_P(value)){
    bignum_type bignum = S48_ADDRESS_AFTER_HEADER(value, long);
    
    if (! s48_bignum_fits_in_word_p(bignum, 32, 0))
      s48_assertion_violation("s48_extract_unsigned_integer", "does not fit in word", 1,
			      value);
    else return s48_bignum_to_ulong(bignum);
  }
  else s48_assertion_violation("s48_extract_unsigned_integer", "not an exact integer", 1,
			       value);
}

unsigned long
s48_extract_unsigned_long_2(s48_call_t call, s48_ref_t value)
{
  if (s48_fixnum_p_2(call, value))
    {
      long fixnum = s48_unsafe_extract_long_2(call, value);
      if (fixnum < 0)
	s48_assertion_violation_2(call, "s48_extract_unsigned_long_2",
				  "negative argument", 1, value);
      return (unsigned long) fixnum;
    }

  if (s48_bignum_p_2(call, value)){
    bignum_type bignum = s48_address_after_header_2(call, value, long);
    
    if (! s48_bignum_fits_in_word_p(bignum, sizeof(long) * BITS_PER_BYTE, 0))
      s48_assertion_violation_2(call, "s48_extract_unsigned_long_2",
			      "does not fit in word", 1, value);
    else return s48_bignum_to_ulong(bignum);
  }
  else s48_assertion_violation_2(call, "s48_extract_unsigned_long_2",
				 "not an exact integer", 1, value);
}

/*
 * Strings from and to encodings
 */

/*
 * These are just wrappers to ensure the right types.
 */

s48_ref_t
s48_enter_string_latin_1_2(s48_call_t call, const char *s)
{
  return s48_make_local_ref(call, s48_enter_string_latin_1((char*) s));
}

s48_ref_t
s48_enter_string_latin_1_n_2(s48_call_t call, const char *s, long count)
{
  return s48_make_local_ref(call, s48_enter_string_latin_1_n((char*) s, count));
}

void
s48_copy_string_to_latin_1_2(s48_call_t call, s48_ref_t sch_s, char *s)
{
  s48_copy_string_to_latin_1(s48_deref(sch_s), s);
}

void
s48_copy_string_to_latin_1_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count, char *s)
{
  s48_copy_string_to_latin_1_n(s48_deref(sch_s), start, count, s);
}

void
s48_copy_latin_1_to_string_2(s48_call_t call, const char *s, s48_ref_t sch_s)
{
  s48_copy_latin_1_to_string((char*) s, s48_deref(sch_s));
}

void
s48_copy_latin_1_to_string_n_2(s48_call_t call, const char *s, long len, s48_ref_t sch_s)
{
  s48_copy_latin_1_to_string_n((char*) s, len, s48_deref(sch_s));
}

s48_ref_t
s48_enter_string_utf_8_2(s48_call_t call, const char *s)
{
  return s48_make_local_ref(call, s48_enter_string_utf_8((char*) s));
}

s48_value
s48_enter_string_utf_16be(const uint16_t *s)
{
  return s48_enter_string_utf_16beU((char*) s);
}

s48_ref_t
s48_enter_string_utf_16be_2(s48_call_t call, const uint16_t *s)
{
  return s48_make_local_ref(call, s48_enter_string_utf_16beU((char*) s));
}

s48_value
s48_enter_string_utf_16be_n(const uint16_t * s, long l)
{
  return s48_enter_string_utf_16be_nU((char*) s, l);
}

s48_ref_t
s48_enter_string_utf_16be_n_2(s48_call_t call, const uint16_t * s, long l)
{
  return s48_make_local_ref(call, s48_enter_string_utf_16be_nU((char*) s, l));
}

long
s48_copy_string_to_utf_16be(s48_value sch_s, uint16_t * s)
{
  return s48_copy_string_to_utf_16beU(sch_s, (char*) s);
}

long
s48_copy_string_to_utf_16be_2(s48_call_t call, s48_ref_t sch_s, uint16_t * s)
{
  return s48_copy_string_to_utf_16beU(s48_deref(sch_s), (char*) s);
}

long
s48_copy_string_to_utf_16be_n(s48_value sch_s, long start, long count, uint16_t *s)
{
  return s48_copy_string_to_utf_16be_nU(sch_s, start, count, (char*) s);
}

long
s48_copy_string_to_utf_16be_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count, uint16_t *s)
{
  return s48_copy_string_to_utf_16be_nU(s48_deref(sch_s), start, count, (char*) s);
}

s48_value
s48_enter_string_utf_16le(const uint16_t *s)
{ 
  return s48_enter_string_utf_16leU((char *) s);
}

s48_ref_t
s48_enter_string_utf_16le_2(s48_call_t call, const uint16_t *s)
{ 
  return s48_make_local_ref(call, s48_enter_string_utf_16leU((char *) s));
}


s48_value
s48_enter_string_utf_16le_n(const uint16_t *s, long l)
{
  return s48_enter_string_utf_16le_nU((char *) s,l);
}

s48_ref_t
s48_enter_string_utf_16le_n_2(s48_call_t call, const uint16_t *s, long l)
{
  return s48_make_local_ref(call, s48_enter_string_utf_16le_nU((char *) s,l));
}

long
s48_copy_string_to_utf_16le(s48_value sch_s, uint16_t *s)
{
  return s48_copy_string_to_utf_16leU(sch_s, (char *) s);
}

long
s48_copy_string_to_utf_16le_2(s48_call_t call, s48_ref_t sch_s, uint16_t *s)
{
  return s48_copy_string_to_utf_16leU(s48_deref(sch_s), (char *) s);
}

long
s48_copy_string_to_utf_16le_n(s48_value sch_s, long start, long count, uint16_t *s)
{
  return s48_copy_string_to_utf_16le_nU(sch_s, start, count, (char *) s);
}

long
s48_copy_string_to_utf_16le_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count, uint16_t *s)
{
  return s48_copy_string_to_utf_16le_nU(s48_deref(sch_s), start, count, (char *) s);
}

s48_ref_t
s48_enter_string_utf_8_n_2(s48_call_t call, const char* s, long count)
{
  return s48_make_local_ref(call, s48_enter_string_utf_8_n((char*) s, count));
}

long
s48_string_utf_8_length_2(s48_call_t call, s48_ref_t s)
{ 
  return s48_string_utf_8_length(s48_deref(s));
}

long
s48_string_utf_8_length_n_2(s48_call_t call, s48_ref_t s, long start, long count)
{
  return s48_string_utf_8_length_n(s48_deref(s), start, count);
}

long
s48_copy_string_to_utf_8_2(s48_call_t call, s48_ref_t sch_s, char* s)
{
  return s48_copy_string_to_utf_8(s48_deref(sch_s), s);
}

long
s48_copy_string_to_utf_8_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count, char* s)
{
  return s48_copy_string_to_utf_8_n(s48_deref(sch_s), start, count, s);
}

long
s48_string_utf_16be_length_2(s48_call_t call, s48_ref_t sch_s)
{
  return s48_string_utf_16be_length(s48_deref(sch_s));
}

long
s48_string_utf_16be_length_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count)
{
  return s48_string_utf_16be_length_n(s48_deref(sch_s), start, count);
}

long
s48_string_utf_16le_length_2(s48_call_t call, s48_ref_t sch_s)
{
  return s48_string_utf_16le_length(s48_deref(sch_s));
}

long
s48_string_utf_16le_length_n_2(s48_call_t call, s48_ref_t sch_s, long start, long count)
{
  return s48_string_utf_16le_length_n(s48_deref(sch_s), start, count);
}

long
s48_string_length_2(s48_call_t call, s48_ref_t string)
{
  return s48_string_length(s48_deref(string));
}

long
s48_string_latin_1_length_2(s48_call_t call, s48_ref_t string)
{
  return s48_string_length_2(call, string);
}

long
s48_string_latin_1_length_n_2(s48_call_t call, s48_ref_t string, long start, long count)
{
  return count;
}

void
s48_string_set_2(s48_call_t call, s48_ref_t s, long i, long c)
{
  s48_string_set(s48_deref(s), i, c);
}

long
s48_string_ref_2(s48_call_t call, s48_ref_t s, long i)
{
  return s48_string_ref(s48_deref(s), i);
}

/*
 * Extract strings to local buffer
 */

#define MAKE_STRING_EXTRACT_FUNCTION(encoding)				\
  char *s48_extract_##encoding##_from_string_2(s48_call_t call, s48_ref_t sch_s) { \
    char *buf = s48_make_local_buf(call, s48_string_##encoding##_length_2(call, sch_s)); \
    s48_copy_string_to_##encoding##_2(call, sch_s, buf);		\
    return buf;								\
  }

char *
s48_extract_latin_1_from_string_2(s48_call_t call, s48_ref_t sch_s) {
  long size = s48_string_latin_1_length_2(call, sch_s) + 1;
  char *buf = s48_make_local_buf(call, size + 1);
  s48_copy_string_to_latin_1_2(call, sch_s, buf);
  buf[size] = '\0';
  return buf;
}

char *
s48_extract_utf_8_from_string_2(s48_call_t call, s48_ref_t sch_s) {
  long size = s48_string_utf_8_length_2(call, sch_s) + 1;
  char *buf = s48_make_local_buf(call, size + 1);
  s48_copy_string_to_utf_8_2(call, sch_s, buf);
  buf[size] = '\0';
  return buf;
}

uint16_t *
s48_extract_utf_16be_from_string_2(s48_call_t call, s48_ref_t sch_s) {
  long size = s48_string_utf_16be_length_2(call, sch_s);
  uint16_t *buf = 
    (uint16_t *) s48_make_local_buf(call, (size + 1) * sizeof(uint16_t));
  s48_copy_string_to_utf_16be_2(call, sch_s, buf);
  buf[size] = 0;
  return buf;
}

uint16_t *
s48_extract_utf_16le_from_string_2(s48_call_t call, s48_ref_t sch_s) {
  long size = s48_string_utf_16le_length_2(call, sch_s);
  uint16_t *buf = 
    (uint16_t *) s48_make_local_buf(call, (size + 1) * sizeof(uint16_t));
  s48_copy_string_to_utf_16le_2(call, sch_s, buf);
  buf[size] = 0;
  return buf;
}

/*
 * Doubles and characters are straightforward.
 */

s48_value
s48_enter_double(double value)
{
  s48_value obj;

  obj = s48_allocate_stob(S48_STOBTYPE_DOUBLE, sizeof(double));
  S48_UNSAFE_EXTRACT_DOUBLE(obj) = value;

  return obj;
}

s48_ref_t
s48_enter_double_2(s48_call_t call, double value)
{
  s48_ref_t ref;
  ref = s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_DOUBLE, sizeof(double)));
  s48_unsafe_extract_double_2(call, ref) = value;

  return ref;
}

double
s48_extract_double(s48_value s48_double)
{
  if (! S48_DOUBLE_P(s48_double))
    s48_assertion_violation("s48_extract_double", "not a double", 1, s48_double);
  
  return S48_UNSAFE_EXTRACT_DOUBLE(s48_double);
}

double
s48_extract_double_2(s48_call_t call, s48_ref_t s48_double)
{
  if (! s48_double_p_2(call, s48_double))
    s48_assertion_violation_2(call, "s48_extract_double_2",
			      "not a double", 1, s48_double);
  
  return s48_unsafe_extract_double_2(call, s48_double);
}

s48_value
s48_enter_char(long a_char)
{
  if (! ((a_char >= 0)
	 && ((a_char <= 0xd7ff)
	     || ((a_char >= 0xe000) && (a_char <= 0x10ffff)))))
    s48_assertion_violation("s48_enter_char", "not a scalar value", 1, s48_enter_fixnum(a_char));

  return S48_UNSAFE_ENTER_CHAR(a_char);
}

s48_ref_t
s48_enter_char_2(s48_call_t call, long a_char)
{
  if (! ((a_char >= 0)
	 && ((a_char <= 0xd7ff)
	     || ((a_char >= 0xe000) && (a_char <= 0x10ffff)))))
    s48_assertion_violation_2(call, "s48_enter_char_2",
			      "not a scalar value", 1, s48_enter_long_as_fixnum_2(call, a_char));

  return s48_unsafe_enter_char_2(call, a_char);
}

long
s48_extract_char(s48_value a_char)
{
  if (! S48_CHAR_P(a_char))
    s48_assertion_violation("s48_extract_char", "not a char", 1, a_char);
  
  return S48_UNSAFE_EXTRACT_CHAR(a_char);
}

long
s48_extract_char_2(s48_call_t call, s48_ref_t a_char)
{
  if (! s48_char_p_2(call, a_char))
    s48_assertion_violation_2(call, "s48_extract_char_2", "not a char", 1, a_char);
  
  return s48_unsafe_extract_char_2(call, a_char);
}

/********************************/
/* Allocation */

s48_value
s48_enter_pointer(void *pointer)
{
  s48_value obj;

  obj = s48_allocate_stob(S48_STOBTYPE_BYTE_VECTOR, sizeof(void *));
  *(S48_ADDRESS_AFTER_HEADER(obj, void *)) = pointer;

  return obj;
}

s48_ref_t
s48_enter_pointer_2(s48_call_t call, void *pointer)
{
  s48_ref_t ref;

  ref = s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_BYTE_VECTOR, sizeof(void *)));
  *(s48_address_after_header_2(call, ref, void *)) = pointer;

  return ref;
}

void*
s48_extract_pointer(s48_value sch_pointer)
{
  S48_CHECK_VALUE(sch_pointer);
  return *(S48_ADDRESS_AFTER_HEADER(sch_pointer, void *));
}

void*
s48_extract_pointer_2(s48_call_t call, s48_ref_t sch_pointer)
{
  s48_check_value_2(call, sch_pointer);
  return *(s48_address_after_header_2(call, sch_pointer, void *));
}

s48_ref_t
s48_get_imported_binding_2(char *name)
{
  return s48_make_global_ref(s48_get_imported_binding(name));
}

s48_ref_t
s48_get_imported_binding_local_2(s48_call_t call, char *name)
{
  return s48_make_local_ref(call, s48_get_imported_binding(name)); 
}

s48_ref_t
s48_define_exported_binding_2(s48_call_t call, char *name, s48_ref_t binding)
{
  return s48_make_local_ref(call, s48_define_exported_binding(name, s48_deref(binding)));
}

s48_value
s48_cons(s48_value v1, s48_value v2)
{
  s48_value obj;
  S48_DECLARE_GC_PROTECT(2);

  S48_GC_PROTECT_2(v1, v2);

  obj = s48_allocate_stob(S48_STOBTYPE_PAIR, 2);
  S48_UNSAFE_SET_CAR(obj, v1);
  S48_UNSAFE_SET_CDR(obj, v2);

  S48_GC_UNPROTECT();
  return obj;
}

s48_ref_t
s48_cons_2(s48_call_t call, s48_ref_t v1, s48_ref_t v2)
{
  s48_ref_t ref;
  ref = s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_PAIR, 2));
  s48_unsafe_set_car_2(call, ref, v1);
  s48_unsafe_set_cdr_2(call, ref, v2);
  return ref;
}

s48_value
s48_make_weak_pointer(s48_value value)
{
  s48_value obj;
  S48_DECLARE_GC_PROTECT(1);

  S48_GC_PROTECT_1(value);

  obj = s48_allocate_weak_stob(S48_STOBTYPE_WEAK_POINTER, 1);
  S48_STOB_SET(obj, 0, value);

  S48_GC_UNPROTECT();
  return obj;
}

s48_ref_t
s48_make_weak_pointer_2(s48_call_t call, s48_ref_t value)
{
  s48_ref_t ref = s48_make_local_ref(call, s48_allocate_weak_stob(S48_STOBTYPE_WEAK_POINTER, 1));
  s48_unsafe_stob_set_2(call, ref, 0, value);
  return ref;
}

/*
 * Entering and extracting byte vectors.
 */

s48_value
s48_enter_byte_vector(char *bytes, long length)
{
  s48_value obj = s48_make_byte_vector(length);
  memcpy(S48_UNSAFE_EXTRACT_BYTE_VECTOR(obj), bytes, length);
  return obj;
}

s48_ref_t
s48_enter_byte_vector_2(s48_call_t call, const char *bytes, long length)
{
  s48_ref_t ref = s48_make_byte_vector_2(call, length);
  s48_enter_byte_vector_region_2(call, ref, 0, length, (char *) bytes);
  return ref;
}

s48_value
s48_enter_unmovable_byte_vector(char *bytes, long length)
{
  s48_value obj = s48_make_unmovable_byte_vector(length);
  memcpy(S48_UNSAFE_EXTRACT_BYTE_VECTOR(obj), bytes, length);
  return obj;
}

s48_ref_t
s48_enter_unmovable_byte_vector_2(s48_call_t call, const char *bytes, long length)
{
  s48_ref_t ref = s48_make_unmovable_byte_vector_2(call, length);
  s48_enter_byte_vector_region_2(call, ref, 0, length, (char *) bytes);
  return ref;
}

char *
s48_extract_byte_vector(s48_value byte_vector)
{
  S48_CHECK_VALUE(byte_vector);

  return S48_UNSAFE_EXTRACT_BYTE_VECTOR(byte_vector);
}

char *
s48_extract_byte_vector_2(s48_call_t call, s48_ref_t byte_vector)
{
  long s = s48_byte_vector_length_2(call, byte_vector);
  char *buf = s48_make_local_bv(call, byte_vector, s);
  return buf;
}

char *
s48_extract_byte_vector_readonly_2(s48_call_t call, s48_ref_t byte_vector)
{
  long s = s48_byte_vector_length_2(call, byte_vector);
  char *buf = s48_make_local_bv_readonly(call, byte_vector, s);
  return buf;
}

void
s48_extract_byte_vector_region_2(s48_call_t call, s48_ref_t byte_vector,
				 long start, long length, char *buf)
{
  char *scheme_buf;
  s48_check_value_2(call, byte_vector);
  scheme_buf = s48_unsafe_extract_byte_vector_2(call, byte_vector);
  memcpy(buf, scheme_buf + start, length);
}

void
s48_enter_byte_vector_region_2(s48_call_t call, s48_ref_t byte_vector,
			       long start, long length, char *buf)
{
  char *scheme_buf;
  s48_check_value_2(call, byte_vector);
  scheme_buf = s48_unsafe_extract_byte_vector_2(call, byte_vector);
  memcpy(scheme_buf + start, buf, length);
}

void
s48_copy_from_byte_vector_2(s48_call_t call, s48_ref_t byte_vector, char *buf)
{
  s48_extract_byte_vector_region_2(call, byte_vector, 0,
				   s48_byte_vector_length_2(call, byte_vector), buf);
}

void
s48_copy_to_byte_vector_2(s48_call_t call, s48_ref_t byte_vector, char *buf)
{
  s48_enter_byte_vector_region_2(call, byte_vector, 0,
				   s48_byte_vector_length_2(call, byte_vector), buf);
}

psbool
s48_unmovable_p(s48_call_t call, s48_ref_t ref)
{
  return s48_unmovableP(s48_deref(ref));
}

char *
s48_extract_unmovable_byte_vector_2(s48_call_t call, s48_ref_t byte_vector)
{
  s48_check_value_2(call, byte_vector);
  if (!s48_unmovable_p(call, byte_vector))
    s48_assertion_violation("s48_extract_unmovable_byte_vector_2",
			    "not an unmovable byte vector", 1, byte_vector);
  return s48_unsafe_extract_byte_vector_2(call, byte_vector);
}

/* 
   The returned byte vector by s48_extract_byte_vector_unmanaged_2 may
   be a copy of the Scheme byte vector, changes made to the returned
   byte vector will not necessarily be reflected in Scheme until
   s48_release_byte_vector_2 is called. 
*/
char *
s48_extract_byte_vector_unmanaged_2(s48_call_t call, s48_ref_t byte_vector)
{
  if (s48_unmovable_p(call, byte_vector))
    {
      return s48_extract_unmovable_byte_vector_2(call, byte_vector);
    }
  else
    {
      long len = s48_byte_vector_length_2(call, byte_vector);
      char *buf = s48_make_local_buf(call, len);
      s48_extract_byte_vector_region_2(call, byte_vector, 0, len, buf);
      return buf;
    }
}

void
s48_release_byte_vector_2(s48_call_t call, s48_ref_t byte_vector, char *buf)
{
  if (!s48_unmovable_p(call, byte_vector))
    s48_copy_to_byte_vector_2(call, byte_vector, buf);
}

/*
 * Making various kinds of stored objects.
 */

s48_value
s48_make_string(int length, long init)
{
  int i;
  s48_value obj = s48_allocate_string(length);
  /* We should probably offer a VM function for this. */
  for (i = 0; i < length; ++i)
    s48_string_set(obj, i, init);
  return obj;
}

s48_ref_t
s48_make_string_2(s48_call_t call, int length, long init)
{
  int i;
  s48_ref_t ref = s48_make_local_ref(call, s48_allocate_string(length));
  /* We should probably offer a VM function for this. */
  for (i = 0; i < length; ++i)
    s48_string_set(s48_deref(ref), i, init);
  return ref;
}

s48_value
s48_make_vector(long length, s48_value init)
{
  long i;
  s48_value obj;
  S48_DECLARE_GC_PROTECT(1);

  S48_GC_PROTECT_1(init);

  obj = s48_allocate_stob(S48_STOBTYPE_VECTOR, length);
  for (i = 0; i < length; ++i)
    S48_UNSAFE_VECTOR_SET(obj, i, init);

  S48_GC_UNPROTECT();

  return obj;
}

s48_ref_t
s48_make_vector_2(s48_call_t call, long length, s48_ref_t init)
{
  long i;
  s48_ref_t ref = s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_VECTOR, length));
  for (i = 0; i < length; ++i)
    s48_unsafe_vector_set_2(call, ref, i, init);
  return ref;
}

s48_value
s48_make_byte_vector(long length)
{
    return s48_allocate_stob(S48_STOBTYPE_BYTE_VECTOR, length);
}

s48_ref_t
s48_make_byte_vector_2(s48_call_t call, long length)
{
  return s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_BYTE_VECTOR, length));
}

s48_value
s48_make_unmovable_byte_vector(long length)
{
    return s48_allocate_unmovable_stob(S48_STOBTYPE_BYTE_VECTOR, length);
}

s48_ref_t
s48_make_unmovable_byte_vector_2(s48_call_t call, long length)
{
  return s48_make_local_ref(call, s48_allocate_unmovable_stob(S48_STOBTYPE_BYTE_VECTOR, length));
}

s48_value
s48_enter_byte_substring(char *str, long length)
{
  s48_value obj = s48_allocate_stob(S48_STOBTYPE_BYTE_VECTOR, length + 1);
  memcpy(S48_UNSAFE_EXTRACT_BYTE_VECTOR(obj), str, length);
  *(S48_UNSAFE_EXTRACT_BYTE_VECTOR(obj) + length) = '\0';
  return obj;
}

s48_ref_t
s48_enter_byte_substring_2(s48_call_t call, const char *str, long length)
{
  s48_ref_t ref = s48_make_byte_vector_2(call, length + 1);
  s48_enter_byte_vector_region_2(call, ref, 0, length, (char *) str);
  s48_byte_vector_set_2(call, ref, length, '\0');
  return ref;
}

s48_value
s48_enter_byte_string(char *str)
{
  return s48_enter_byte_substring(str, strlen(str));
}

s48_ref_t
s48_enter_byte_string_2(s48_call_t call, const char *str)
{
  return s48_enter_byte_substring_2(call, str, strlen(str));
}

s48_value
s48_make_record(s48_value type_shared_binding)
{
    long i, number_of_fields;
    s48_value record = S48_FALSE;
    s48_value record_type = S48_FALSE;
    S48_DECLARE_GC_PROTECT(1);

    S48_GC_PROTECT_1(record_type);

    S48_SHARED_BINDING_CHECK(type_shared_binding);
    S48_SHARED_BINDING_CHECK(s48_deref(the_record_type_binding));

    record_type = S48_SHARED_BINDING_REF(type_shared_binding);

    s48_check_record_type(record_type, s48_deref(the_record_type_binding));

    number_of_fields =
      S48_UNSAFE_EXTRACT_FIXNUM(S48_RECORD_TYPE_NUMBER_OF_FIELDS(record_type));

    record = s48_allocate_stob(S48_STOBTYPE_RECORD, number_of_fields + 1);

    S48_UNSAFE_RECORD_SET(record, -1, record_type);
    for (i = 0; i < number_of_fields; ++i)
      S48_UNSAFE_RECORD_SET(record, i, S48_UNSPECIFIC);

    S48_GC_UNPROTECT();

    return record;
}

s48_ref_t
s48_make_record_2(s48_call_t call, s48_ref_t type_shared_binding)
{
    long i, number_of_fields;
    s48_ref_t record;
    s48_ref_t record_type;

    s48_shared_binding_check_2(call, type_shared_binding);
    s48_shared_binding_check_2(call, the_record_type_binding);

    record_type = s48_shared_binding_ref_2(call, type_shared_binding);

    s48_check_record_type_2(call, record_type, the_record_type_binding);

    number_of_fields =
      s48_unsafe_extract_long_2(call, 
				s48_record_type_number_of_fields_2(call, record_type));

    record = s48_make_local_ref(call, s48_allocate_stob(S48_STOBTYPE_RECORD, number_of_fields + 1));

    s48_unsafe_record_set_2(call, record, -1, record_type);
    for (i = 0; i < number_of_fields; ++i)
      s48_unsafe_record_set_2(call, record, i, s48_unspecific_2(call));

    return record;
}

/*
 * Raise an exception if `record' is not a record whose type is the one
 * found in `type_binding'.
 */
void
s48_check_record_type(s48_value record, s48_value type_binding)
{
  if (! S48_RECORD_P(S48_SHARED_BINDING_REF(type_binding)))
    s48_raise_scheme_exception(S48_EXCEPTION_UNBOUND_EXTERNAL_NAME, 1,
			       S48_SHARED_BINDING_NAME(type_binding));

  if ((! S48_RECORD_P(record)) ||
      (S48_UNSAFE_SHARED_BINDING_REF(type_binding) !=
       S48_UNSAFE_RECORD_REF(record, -1)))
    s48_assertion_violation("s48_check_record_type", "not a record of the appropriate type", 2,
			    record, S48_SHARED_BINDING_REF(type_binding));
}

void
s48_check_record_type_2(s48_call_t call, s48_ref_t record, s48_ref_t type_binding)
{
  if (! s48_record_p_2(call, s48_shared_binding_ref_2(call, type_binding)))
    s48_raise_scheme_exception_2(call,S48_EXCEPTION_UNBOUND_EXTERNAL_NAME, 1,
				 s48_shared_binding_name_2(call, type_binding));

  if ((! s48_record_p_2(call, record)) ||
      (!s48_eq_p_2(call,
		   s48_unsafe_shared_binding_ref_2(call, type_binding),
		   s48_unsafe_record_ref_2(call, record, -1))))
    s48_assertion_violation_2(call, "s48_check_record_type_2",
			      "not a record of the appropriate type", 2,
			      record, s48_shared_binding_ref_2(call, type_binding));
}

long
s48_length(s48_value list)
{
  long i = 0;

  while (!(S48_EQ(list, S48_NULL)))
    {
      list = S48_CDR(list);
      ++i;
    }
  return S48_UNSAFE_ENTER_FIXNUM(i);
}

s48_ref_t
s48_length_2(s48_call_t call, s48_ref_t list)
{
  s48_ref_t l = s48_copy_local_ref(call, list);
  long i = 0;
  while (!(s48_null_p_2(call, l)))
    {
      s48_ref_t temp = l;
      l = s48_cdr_2(call, l);
      s48_free_local_ref(call, temp);
      ++i;
    }
  return s48_unsafe_enter_long_as_fixnum_2(call, i);
}