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
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
|
/* Program to write C++-suitable header files from a Java(TM) .class
file. This is similar to SUN's javah.
Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc. */
/* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include <math.h>
#include "jcf.h"
#include "tree.h"
#include "javaop.h"
#include "java-tree.h"
#include "java-opcodes.h"
#include "hashtab.h"
#include <getopt.h>
/* The output file. */
FILE *out = NULL;
/* Nonzero on failure. */
static int found_error = 0;
/* Nonzero if we're generating JNI output. */
static int flag_jni = 0;
/* When nonzero, warn when source file is newer than matching class
file. */
int flag_newer = 1;
/* Directory to place resulting files in. Set by -d option. */
const char *output_directory = "";
/* Directory to place temporary file. Set by -td option. Currently unused. */
const char *temp_directory = "/tmp";
/* Number of friend functions we have to declare. */
static int friend_count;
/* A class can optionally have a `friend' function declared. If
non-NULL, this is that function. */
static char **friend_specs = NULL;
/* Number of lines we are prepending before the class. */
static int prepend_count;
/* We can prepend extra lines before the class's start. */
static char **prepend_specs = NULL;
/* Number of lines we are appending at the end of the class. */
static int add_count;
/* We can append extra lines just before the class's end. */
static char **add_specs = NULL;
/* Number of lines we are appending after the class. */
static int append_count;
/* We can append extra lines after the class's end. */
static char **append_specs = NULL;
int verbose = 0;
int stubs = 0;
struct JCF *current_jcf;
/* This holds access information for the last field we examined. They
let us generate "private:", "public:", and "protected:" properly.
If 0 then we haven't previously examined any field. */
static JCF_u2 last_access;
/* Pass this macro the flags for a class and for a method. It will
return true if the method should be considered `final'. */
#define METHOD_IS_FINAL(Class, Method) \
(((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
/* Pass this macro the flags for a method. It will return true if the
method is native. */
#define METHOD_IS_NATIVE(Method) \
((Method) & ACC_NATIVE)
/* We keep a linked list of all method names we have seen. This lets
us determine if a method name and a field name are in conflict. */
struct method_name
{
unsigned char *name;
int length;
unsigned char *signature;
int sig_length;
struct method_name *next;
};
/* List of method names we've seen. */
static struct method_name *method_name_list;
static void print_field_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
static void print_mangled_classname PARAMS ((FILE*, JCF*, const char*, int));
static int print_cxx_classname PARAMS ((FILE*, const char*, JCF*, int));
static void print_method_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
static void print_c_decl PARAMS ((FILE*, JCF*, int, int, int, const char *,
int));
static void print_stub_or_jni PARAMS ((FILE*, JCF*, int, int, int,
const char *, int));
static void print_full_cxx_name PARAMS ((FILE*, JCF*, int, int, int,
const char *, int));
static void decompile_method PARAMS ((FILE*, JCF*, int));
static void add_class_decl PARAMS ((FILE*, JCF*, JCF_u2));
static int java_float_finite PARAMS ((jfloat));
static int java_double_finite PARAMS ((jdouble));
static void print_name PARAMS ((FILE *, JCF *, int));
static void print_base_classname PARAMS ((FILE *, JCF *, int));
static int utf8_cmp PARAMS ((const unsigned char *, int, const char *));
static char *cxx_keyword_subst PARAMS ((const unsigned char *, int));
static void generate_access PARAMS ((FILE *, JCF_u2));
static int name_is_method_p PARAMS ((const unsigned char *, int));
static char *get_field_name PARAMS ((JCF *, int, JCF_u2));
static void print_field_name PARAMS ((FILE *, JCF *, int, JCF_u2));
static const unsigned char *super_class_name PARAMS ((JCF *, int *));
static void print_include PARAMS ((FILE *, const unsigned char *, int));
static int gcjh_streq PARAMS ((const void *p1, const void *p2));
static int throwable_p PARAMS ((const unsigned char *signature));
static const unsigned char *decode_signature_piece
PARAMS ((FILE *, const unsigned char *, const unsigned char *, int *));
static void print_class_decls PARAMS ((FILE *, JCF *, int));
static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
static int overloaded_jni_method_exists_p PARAMS ((const unsigned char *, int,
const char *, int));
static void jni_print_char PARAMS ((FILE *, int));
static void decompile_return_statement PARAMS ((FILE *, JCF *, int, int, int));
JCF_u2 current_field_name;
JCF_u2 current_field_value;
JCF_u2 current_field_signature;
JCF_u2 current_field_flags;
#define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
/* We pass over fields twice. The first time we just note the types
of the fields and then the start of the methods. Then we go back
and parse the fields for real. This is ugly. */
static int field_pass;
/* Likewise we pass over methods twice. The first time we generate
class decl information; the second time we generate actual method
decls. */
static int method_pass;
#define HANDLE_END_FIELD() \
if (field_pass) \
{ \
if (out && ! stubs && ! flag_jni) \
print_field_info (out, jcf, current_field_name, \
current_field_signature, \
current_field_flags); \
} \
else if (! stubs && ! flag_jni) \
add_class_decl (out, jcf, current_field_signature);
#define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
static int method_declared = 0;
static int method_access = 0;
static int method_printed = 0;
static int method_synthetic = 0;
static int method_signature = 0;
#define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
{ \
method_synthetic = 0; \
method_printed = 0; \
decompiled = 0; \
method_signature = SIGNATURE; \
if (ATTRIBUTE_COUNT) \
method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
(const char *)"Synthetic", 9); \
/* If a synthetic methods have been declared, its attribute aren't \
worth reading (and triggering side-effects). We skip them an \
set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
jcf_parse_one_method. */ \
if (method_synthetic) \
{ \
skip_attribute (jcf, ATTRIBUTE_COUNT); \
ATTRIBUTE_COUNT = 0; \
} \
if (method_pass && !method_synthetic) \
{ \
if (out) \
print_method_info (out, jcf, NAME, SIGNATURE, \
ACCESS_FLAGS); \
} \
else if (!method_synthetic) \
{ \
print_method_info (NULL, jcf, NAME, SIGNATURE, \
ACCESS_FLAGS); \
if (! stubs && ! flag_jni) \
add_class_decl (out, jcf, SIGNATURE); \
} \
}
#define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
static int decompiled = 0;
#define HANDLE_END_METHOD() \
if (out && method_printed && !method_synthetic) \
fputs (decompiled || stubs ? "\n" : ";\n", out);
/* We're going to need {peek,skip}_attribute, enable their definition. */
#define NEED_PEEK_ATTRIBUTE
#define NEED_SKIP_ATTRIBUTE
#include "jcf-reader.c"
/* Some useful constants. */
#define F_NAN_MASK 0x7f800000
#if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
#define D_NAN_MASK 0x000000007ff00000LL
#else
#define D_NAN_MASK 0x7ff0000000000000LL
#endif
/* Return 1 if F is not Inf or NaN. */
static int
java_float_finite (f)
jfloat f;
{
union Word u;
u.f = f;
/* We happen to know that F_NAN_MASK will match all NaN values, and
also positive and negative infinity. That's why we only need one
test here. See The Java Language Specification, section 20.9. */
return (u.i & F_NAN_MASK) != F_NAN_MASK;
}
/* Return 1 if D is not Inf or NaN. */
static int
java_double_finite (d)
jdouble d;
{
union DWord u;
u.d = d;
/* Now check for all NaNs. */
return (u.l & D_NAN_MASK) != D_NAN_MASK;
}
/* Print a character, appropriately mangled for JNI. */
static void
jni_print_char (stream, ch)
FILE *stream;
int ch;
{
if (! flag_jni)
jcf_print_char (stream, ch);
else if (ch == '(' || ch == ')')
{
/* Ignore. */
}
else if (ch == '_')
fputs ("_1", stream);
else if (ch == ';')
fputs ("_2", stream);
else if (ch == '[')
fputs ("_3", stream);
else if (ch == '/')
fputs ("_", stream);
else if (ISALNUM (ch))
fputc (ch, stream);
else
{
/* "Unicode" character. */
fprintf (stream, "_0%04x", ch);
}
}
/* Print a name from the class data. If the index does not point to a
string, an error results. */
static void
DEFUN(print_name, (stream, jcf, name_index),
FILE* stream AND JCF* jcf AND int name_index)
{
if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
{
fprintf (stream, "<not a UTF8 constant>");
found_error = 1;
}
else if (! flag_jni)
jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
JPOOL_UTF_LENGTH (jcf, name_index));
else
{
/* For JNI we must correctly quote each character. */
const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
int length = JPOOL_UTF_LENGTH (jcf, name_index);
const unsigned char *limit = str + length;
while (str < limit)
{
int ch = UTF8_GET (str, limit);
if (ch < 0)
{
fprintf (stream, "\\<invalid>");
return;
}
jni_print_char (stream, ch);
}
}
}
/* Print base name of class. The base name is everything after the
final separator. */
static void
print_base_classname (stream, jcf, index)
FILE *stream;
JCF *jcf;
int index;
{
int name_index = JPOOL_USHORT1 (jcf, index);
int len;
const unsigned char *s, *p, *limit;
s = JPOOL_UTF_DATA (jcf, name_index);
len = JPOOL_UTF_LENGTH (jcf, name_index);
limit = s + len;
p = s;
while (s < limit)
{
int c = UTF8_GET (s, limit);
if (c == '/')
p = s;
}
while (p < limit)
{
int ch = UTF8_GET (p, limit);
if (ch == '/')
fputs ("::", stream);
else
jcf_print_char (stream, ch);
}
}
/* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
and 1 if STR is "greater" than NAME. */
static int
utf8_cmp (str, length, name)
const unsigned char *str;
int length;
const char *name;
{
const unsigned char *limit = str + length;
int i;
for (i = 0; name[i]; ++i)
{
int ch = UTF8_GET (str, limit);
if (ch != name[i])
return ch - name[i];
}
return str == limit ? 0 : 1;
}
/* This is a sorted list of all C++ keywords. */
static const char *const cxx_keywords[] =
{
"_Complex",
"__alignof",
"__alignof__",
"__asm",
"__asm__",
"__attribute",
"__attribute__",
"__builtin_va_arg",
"__complex",
"__complex__",
"__const",
"__const__",
"__extension__",
"__imag",
"__imag__",
"__inline",
"__inline__",
"__label__",
"__null",
"__real",
"__real__",
"__restrict",
"__restrict__",
"__signed",
"__signed__",
"__typeof",
"__typeof__",
"__volatile",
"__volatile__",
"and",
"and_eq",
"asm",
"auto",
"bitand",
"bitor",
"bool",
"break",
"case",
"catch",
"char",
"class",
"compl",
"const",
"const_cast",
"continue",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"int",
"long",
"mutable",
"namespace",
"new",
"not",
"not_eq",
"operator",
"or",
"or_eq",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"short",
"signed",
"sizeof",
"static",
"static_cast",
"struct",
"switch",
"template",
"this",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"typeof",
"union",
"unsigned",
"using",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq"
};
/* If NAME is the name of a C++ keyword, then return an override name.
This is a name that can be used in place of the keyword.
Otherwise, return NULL. The return value is malloc()d. */
static char *
cxx_keyword_subst (str, length)
const unsigned char *str;
int length;
{
int last = ARRAY_SIZE (cxx_keywords);
int first = 0;
int mid = (last + first) / 2;
int old = -1;
for (mid = (last + first) / 2;
mid != old;
old = mid, mid = (last + first) / 2)
{
int kwl = strlen (cxx_keywords[mid]);
int min_length = kwl > length ? length : kwl;
int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
if (r == 0)
{
int i;
/* Skip all trailing `$'. */
for (i = min_length; i < length && str[i] == '$'; ++i)
;
/* We've only found a match if all the remaining characters
are `$'. */
if (i == length)
{
char *dup = xmalloc (2 + length - min_length + kwl);
strcpy (dup, cxx_keywords[mid]);
for (i = kwl; i < length + 1; ++i)
dup[i] = '$';
dup[i] = '\0';
return dup;
}
r = 1;
}
if (r < 0)
last = mid;
else
first = mid;
}
return NULL;
}
/* Generate an access control keyword based on FLAGS. */
static void
generate_access (stream, flags)
FILE *stream;
JCF_u2 flags;
{
if ((flags & ACC_VISIBILITY) == last_access)
return;
last_access = (flags & ACC_VISIBILITY);
switch (last_access)
{
case 0:
fputs ("public: // actually package-private\n", stream);
break;
case ACC_PUBLIC:
fputs ("public:\n", stream);
break;
case ACC_PRIVATE:
fputs ("private:\n", stream);
break;
case ACC_PROTECTED:
fputs ("public: // actually protected\n", stream);
break;
default:
found_error = 1;
fprintf (stream, "#error unrecognized visibility %d\n",
(flags & ACC_VISIBILITY));
break;
}
}
/* See if NAME is already the name of a method. */
static int
name_is_method_p (name, length)
const unsigned char *name;
int length;
{
struct method_name *p;
for (p = method_name_list; p != NULL; p = p->next)
{
if (p->length == length && ! memcmp (p->name, name, length))
return 1;
}
return 0;
}
/* If there is already a method named NAME, whose signature is not
SIGNATURE, then return true. Otherwise return false. */
static int
overloaded_jni_method_exists_p (name, length, signature, sig_length)
const unsigned char *name;
int length;
const char *signature;
int sig_length;
{
struct method_name *p;
for (p = method_name_list; p != NULL; p = p->next)
{
if (p->length == length
&& ! memcmp (p->name, name, length)
&& (p->sig_length != sig_length
|| memcmp (p->signature, signature, sig_length)))
return 1;
}
return 0;
}
/* Get name of a field. This handles renamings due to C++ clash. */
static char *
get_field_name (jcf, name_index, flags)
JCF *jcf;
int name_index;
JCF_u2 flags;
{
unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
int length = JPOOL_UTF_LENGTH (jcf, name_index);
char *override;
if (name_is_method_p (name, length))
{
/* This field name matches a method. So override the name with
a dummy name. This is yucky, but it isn't clear what else to
do. FIXME: if the field is static, then we'll be in real
trouble. */
if ((flags & ACC_STATIC))
{
fprintf (stderr, "static field has same name as method\n");
found_error = 1;
return NULL;
}
override = xmalloc (length + 3);
memcpy (override, name, length);
strcpy (override + length, "__");
}
else
override = cxx_keyword_subst (name, length);
return override;
}
/* Print a field name. Convenience function for use with
get_field_name. */
static void
print_field_name (stream, jcf, name_index, flags)
FILE *stream;
JCF *jcf;
int name_index;
JCF_u2 flags;
{
char *override = get_field_name (jcf, name_index, flags);
if (override)
{
fputs (override, stream);
free (override);
}
else
jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
JPOOL_UTF_LENGTH (jcf, name_index));
}
static void
DEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags),
FILE *stream AND JCF* jcf
AND int name_index AND int sig_index AND JCF_u2 flags)
{
char *override = NULL;
generate_access (stream, flags);
if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
{
fprintf (stream, "<not a UTF8 constant>");
found_error = 1;
return;
}
fputs (" ", out);
if ((flags & ACC_STATIC))
fputs ("static ", out);
if ((flags & ACC_FINAL))
{
if (current_field_value > 0)
{
char buffer[25];
int done = 1;
switch (JPOOL_TAG (jcf, current_field_value))
{
case CONSTANT_Integer:
{
jint num;
int most_negative = 0;
fputs ("const jint ", out);
print_field_name (out, jcf, name_index, 0);
fputs (" = ", out);
num = JPOOL_INT (jcf, current_field_value);
/* We single out the most negative number to print
specially. This avoids later warnings from g++. */
if (num == (jint) 0x80000000)
{
most_negative = 1;
++num;
}
format_int (buffer, (jlong) num, 10);
fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
}
break;
case CONSTANT_Long:
{
jlong num;
int most_negative = 0;
fputs ("const jlong ", out);
print_field_name (out, jcf, name_index, 0);
fputs (" = ", out);
num = JPOOL_LONG (jcf, current_field_value);
/* We single out the most negative number to print
specially.. This avoids later warnings from g++. */
if (num == (jlong) 0x8000000000000000LL)
{
most_negative = 1;
++num;
}
format_int (buffer, num, 10);
fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
}
break;
case CONSTANT_Float:
{
jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
fputs ("const jfloat ", out);
print_field_name (out, jcf, name_index, 0);
if (! java_float_finite (fnum))
fputs (";\n", out);
else
fprintf (out, " = %.10g;\n", fnum);
}
break;
case CONSTANT_Double:
{
jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
fputs ("const jdouble ", out);
print_field_name (out, jcf, name_index, 0);
if (! java_double_finite (dnum))
fputs (";\n", out);
else
fprintf (out, " = %.17g;\n", dnum);
}
break;
default:
/* We can't print this as a constant, but we can still
print something sensible. */
done = 0;
break;
}
if (done)
return;
}
}
override = get_field_name (jcf, name_index, flags);
print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
fputs (";\n", out);
if (override)
free (override);
}
static void
DEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags),
FILE *stream AND JCF* jcf
AND int name_index AND int sig_index AND JCF_u2 flags)
{
const unsigned char *str;
int length, is_init = 0;
char *override = NULL;
method_declared = 0;
method_access = flags;
if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
fprintf (stream, "<not a UTF8 constant>");
str = JPOOL_UTF_DATA (jcf, name_index);
length = JPOOL_UTF_LENGTH (jcf, name_index);
if (str[0] == '<')
{
/* Ignore the internally generated method <clinit>. However,
treat <init> as a constructor. */
if (! utf8_cmp (str, length, "<init>"))
is_init = 1;
else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
&& ! (flags & ACC_STATIC))
{
/* FIXME: i18n bug here. Order of prints should not be
fixed. */
fprintf (stderr, "ignored method `");
jcf_print_utf8 (stderr, str, length);
fprintf (stderr, "' marked virtual\n");
found_error = 1;
return;
}
else
return;
}
/* During the first method pass, build a list of method names. This will
be used to determine if field names conflict with method names. */
if (! stream)
{
struct method_name *nn;
nn = xmalloc (sizeof (struct method_name));
nn->name = xmalloc (length);
memcpy (nn->name, str, length);
nn->length = length;
nn->next = method_name_list;
nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
nn->signature = xmalloc (nn->sig_length);
memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
nn->sig_length);
method_name_list = nn;
/* The rest of this function doesn't matter. */
return;
}
/* We don't worry about overrides in JNI mode. */
if (! flag_jni)
{
/* We can't generate a method whose name is a C++ reserved word.
We can't just ignore the function, because that will cause
incorrect code to be generated if the function is virtual
(not only for calls to this function for for other functions
after it in the vtbl). So we give it a dummy name instead. */
override = cxx_keyword_subst (str, length);
}
if (! stubs && ! flag_jni)
{
method_printed = 1;
generate_access (stream, flags);
fputs (" ", out);
if ((flags & ACC_STATIC))
fputs ("static ", out);
else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
{
/* Don't print `virtual' if we have a constructor. */
if (! is_init)
fputs ("virtual ", out);
}
print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
if ((flags & ACC_ABSTRACT))
fputs (" = 0", out);
else
method_declared = 1;
}
else
{
if (METHOD_IS_NATIVE (flags))
{
method_printed = 1;
print_stub_or_jni (out, jcf, name_index, sig_index,
is_init, override, flags);
}
}
if (override)
free (override);
}
/* A helper for the decompiler which prints a `return' statement where
the type is a reference type. If METHODTYPE and OBJECTTYPE are not
identical, we emit a cast. We do this because the C++ compiler
doesn't know that a reference can be cast to the type of an
interface it implements. METHODTYPE is the index of the method's
signature. NAMEINDEX is the index of the field name; -1 for
`this'. OBJECTTYPE is the index of the object's type. */
static void
decompile_return_statement (out, jcf, methodtype, nameindex, objecttype)
FILE *out;
JCF *jcf;
int methodtype, nameindex, objecttype;
{
int cast = 0;
int obj_name_len, method_name_len;
const unsigned char *obj_data, *method_data;
obj_name_len = JPOOL_UTF_LENGTH (jcf, objecttype);
obj_data = JPOOL_UTF_DATA (jcf, objecttype);
method_name_len = JPOOL_UTF_LENGTH (jcf, methodtype);
method_data = JPOOL_UTF_DATA (jcf, methodtype);
/* Skip forward to return type part of method. */
while (*method_data != ')')
{
++method_data;
--method_name_len;
}
/* Skip past `)'. */
++method_data;
--method_name_len;
/* If we see an `L', skip it and the trailing `;'. */
if (method_data[0] == 'L' && method_data[method_name_len - 1] == ';')
{
++method_data;
method_name_len -= 2;
}
if (obj_data[0] == 'L' && obj_data[obj_name_len - 1] == ';')
{
++obj_data;
obj_name_len -= 2;
}
/* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
need a cast. Right now there is no way to determine if this is
the case. */
if (method_name_len != obj_name_len)
cast = 1;
else
{
int i;
for (i = 0; i < method_name_len; ++i)
{
if (method_data[i] != obj_data[i])
{
cast = 1;
break;
}
}
}
fputs (" { return ", out);
if (cast)
{
int array_depth = 0;
const unsigned char *limit;
fputs ("reinterpret_cast<", out);
while (*method_data == '[')
{
++method_data;
++array_depth;
--method_name_len;
fputs ("JArray<", out);
}
/* Leading space to avoid C++ digraphs. */
fputs (" ::", out);
/* If we see an `L', skip it and the trailing `;'. Only do this
if we've seen an array specification. If we don't have an
array then the `L' was stripped earlier. */
if (array_depth && method_data[0] == 'L'
&& method_data[method_name_len - 1] == ';')
{
++method_data;
method_name_len -= 2;
}
limit = method_data + method_name_len;
while (method_data < limit)
{
int ch = UTF8_GET (method_data, limit);
if (ch == '/')
fputs ("::", out);
else
jcf_print_char (out, ch);
}
fputs (" *", out);
/* Close each array. */
while (array_depth > 0)
{
fputs ("> *", out);
--array_depth;
}
/* Close the cast. */
fputs ("> (", out);
}
if (nameindex == -1)
fputs ("this", out);
else
print_field_name (out, jcf, nameindex, 0);
if (cast)
fputs (")", out);
fputs ("; }", out);
}
/* Try to decompile a method body. Right now we just try to handle a
simple case that we can do. Expand as desired. */
static void
decompile_method (out, jcf, code_len)
FILE *out;
JCF *jcf;
int code_len;
{
const unsigned char *codes = jcf->read_ptr;
int index;
uint16 name_and_type, name;
/* If the method is synchronized, don't touch it. */
if ((method_access & ACC_SYNCHRONIZED))
return;
if (code_len == 5
&& codes[0] == OPCODE_aload_0
&& codes[1] == OPCODE_getfield
&& (codes[4] == OPCODE_areturn
|| codes[4] == OPCODE_dreturn
|| codes[4] == OPCODE_freturn
|| codes[4] == OPCODE_ireturn
|| codes[4] == OPCODE_lreturn))
{
/* Found code like `return FIELD'. */
index = (codes[2] << 8) | codes[3];
/* FIXME: ensure that tag is CONSTANT_Fieldref. */
name_and_type = JPOOL_USHORT2 (jcf, index);
/* FIXME: ensure that tag is CONSTANT_NameAndType. */
name = JPOOL_USHORT1 (jcf, name_and_type);
if (codes[4] == OPCODE_areturn)
decompile_return_statement (out, jcf, method_signature,
name, JPOOL_USHORT2 (jcf, name_and_type));
else
{
fputs (" { return ", out);
/* FIXME: flags. */
print_field_name (out, jcf, name, 0);
fputs ("; }", out);
}
decompiled = 1;
}
else if (code_len == 2
&& codes[0] == OPCODE_aload_0
&& codes[1] == OPCODE_areturn
/* We're going to generate `return this'. This only makes
sense for non-static methods. */
&& ! (method_access & ACC_STATIC))
{
decompile_return_statement (out, jcf, method_signature, -1,
JPOOL_USHORT1 (jcf, jcf->this_class));
decompiled = 1;
}
else if (code_len == 1 && codes[0] == OPCODE_return)
{
/* Found plain `return'. */
fputs (" { }", out);
decompiled = 1;
}
else if (code_len == 2
&& codes[0] == OPCODE_aconst_null
&& codes[1] == OPCODE_areturn)
{
/* Found `return null'. We don't want to depend on NULL being
defined. */
fputs (" { return 0; }", out);
decompiled = 1;
}
}
/* Like strcmp, but invert the return result for the hash table. This
should probably be in hashtab.c to complement the existing string
hash function. */
static int
gcjh_streq (p1, p2)
const void *p1, *p2;
{
return ! strcmp ((char *) p1, (char *) p2);
}
/* Return 1 if the initial part of CLNAME names a subclass of throwable,
or 0 if not. CLNAME may be extracted from a signature, and can be
terminated with either `;' or NULL. */
static int
throwable_p (clname)
const unsigned char *clname;
{
int length;
unsigned char *current;
int i;
int result = 0;
/* We keep two hash tables of class names. In one we list all the
classes which are subclasses of Throwable. In the other we will
all other classes. We keep two tables to make the code a bit
simpler; we don't have to have a structure mapping class name to
a `throwable?' bit. */
static htab_t throw_hash;
static htab_t non_throw_hash;
static int init_done = 0;
if (! init_done)
{
PTR *slot;
const unsigned char *str;
/* Self-initializing. The cost of this really doesn't matter.
We also don't care about freeing these, either. */
throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
(htab_del) free);
non_throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
(htab_del) free);
/* Make sure the root classes show up in the tables. */
str = xstrdup ("java.lang.Throwable");
slot = htab_find_slot (throw_hash, str, INSERT);
*slot = (PTR) str;
str = xstrdup ("java.lang.Object");
slot = htab_find_slot (non_throw_hash, str, INSERT);
*slot = (PTR) str;
init_done = 1;
}
for (length = 0; clname[length] != ';' && clname[length] != '\0'; ++length)
;
current = ALLOC (length);
for (i = 0; i < length; ++i)
current[i] = clname[i] == '/' ? '.' : clname[i];
current[length] = '\0';
/* We don't compute the hash slot here because the table might be
modified by the recursion. In that case the slot could be
invalidated. */
if (htab_find (throw_hash, current))
result = 1;
else if (htab_find (non_throw_hash, current))
result = 0;
else
{
JCF jcf;
PTR *slot;
unsigned char *super, *tmp;
int super_length = -1;
const char *classfile_name = find_class (current, strlen (current),
&jcf, 0);
if (! classfile_name)
{
fprintf (stderr, "couldn't find class %s\n", current);
found_error = 1;
return 0;
}
if (jcf_parse_preamble (&jcf) != 0
|| jcf_parse_constant_pool (&jcf) != 0
|| verify_constant_pool (&jcf) > 0)
{
fprintf (stderr, "parse error while reading %s\n", classfile_name);
found_error = 1;
return 0;
}
jcf_parse_class (&jcf);
tmp = (unsigned char *) super_class_name (&jcf, &super_length);
super = ALLOC (super_length + 1);
memcpy (super, tmp, super_length);
super[super_length] = '\0';
result = throwable_p (super);
slot = htab_find_slot (result ? throw_hash : non_throw_hash,
current, INSERT);
*slot = current;
current = NULL;
JCF_FINISH (&jcf);
}
return result;
}
/* Print one piece of a signature. Returns pointer to next parseable
character on success, NULL on error. */
static const unsigned char *
decode_signature_piece (stream, signature, limit, need_space)
FILE *stream;
const unsigned char *signature, *limit;
int *need_space;
{
const char *ctype;
int array_depth = 0;
switch (signature[0])
{
case '[':
/* More spaghetti. */
array_loop:
for (signature++; (signature < limit
&& ISDIGIT (*signature)); signature++)
;
switch (*signature)
{
case 'B':
ctype = "jbyteArray";
break;
case 'C':
ctype = "jcharArray";
break;
case 'D':
ctype = "jdoubleArray";
break;
case 'F':
ctype = "jfloatArray";
break;
case 'I':
ctype = "jintArray";
break;
case 'S':
ctype = "jshortArray";
break;
case 'J':
ctype = "jlongArray";
break;
case 'Z':
ctype = "jbooleanArray";
break;
case '[':
/* We have a nested array. */
++array_depth;
if (! flag_jni)
fputs ("JArray<", stream);
goto array_loop;
case 'L':
/* We have to generate a reference to JArray here, so that
our output matches what the compiler does. */
++signature;
/* Space between `<' and `:' to avoid C++ digraphs. */
if (! flag_jni)
fputs ("JArray< ::", stream);
while (signature < limit && *signature != ';')
{
int ch = UTF8_GET (signature, limit);
if (! flag_jni)
{
if (ch == '/')
fputs ("::", stream);
else
jcf_print_char (stream, ch);
}
}
if (! flag_jni)
fputs (" *> *", stream);
*need_space = 0;
ctype = NULL;
break;
default:
/* Unparseable signature. */
return NULL;
}
/* If the previous iterations left us with something to print,
print it. For JNI, we always print `jobjectArray' in the
nested cases. */
if (flag_jni && (ctype == NULL || array_depth > 0))
{
ctype = "jobjectArray";
*need_space = 1;
}
/* The `printit' case will advance SIGNATURE for us. If we
don't go there, we must advance past the `;' ourselves. */
if (ctype != NULL)
goto printit;
++signature;
break;
case '(':
case ')':
/* This shouldn't happen. */
return NULL;
case 'B': ctype = "jbyte"; goto printit;
case 'C': ctype = "jchar"; goto printit;
case 'D': ctype = "jdouble"; goto printit;
case 'F': ctype = "jfloat"; goto printit;
case 'I': ctype = "jint"; goto printit;
case 'J': ctype = "jlong"; goto printit;
case 'S': ctype = "jshort"; goto printit;
case 'Z': ctype = "jboolean"; goto printit;
case 'V': ctype = "void"; goto printit;
case 'L':
if (flag_jni)
{
/* We know about certain types and special-case their names. */
if (! strncmp (signature, "Ljava/lang/String;",
sizeof ("Ljava/lang/String;") -1))
ctype = "jstring";
else if (! strncmp (signature, "Ljava/lang/Class;",
sizeof ("Ljava/lang/Class;") - 1))
ctype = "jclass";
/* Skip leading 'L' for throwable_p call. */
else if (throwable_p (signature + 1))
ctype = "jthrowable";
else
ctype = "jobject";
while (*signature && *signature != ';')
++signature;
goto printit;
}
/* Print a leading "::" so we look in the right namespace. */
fputs ("::", stream);
++signature;
while (*signature && *signature != ';')
{
int ch = UTF8_GET (signature, limit);
if (ch == '/')
fputs ("::", stream);
else
jcf_print_char (stream, ch);
}
fputs (" *", stream);
if (*signature == ';')
signature++;
*need_space = 0;
break;
default:
*need_space = 1;
jni_print_char (stream, *signature++);
break;
printit:
signature++;
*need_space = 1;
fputs (ctype, stream);
break;
}
if (! flag_jni)
{
while (array_depth-- > 0)
fputs ("> *", stream);
}
return signature;
}
static void
DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, is_init,
name_override, flags),
FILE* stream AND JCF* jcf
AND int name_index AND int signature_index
AND int is_init AND const char *name_override AND int flags)
{
if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
{
fprintf (stream, "<not a UTF8 constant>");
found_error = 1;
}
else
{
int length = JPOOL_UTF_LENGTH (jcf, signature_index);
const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
register const unsigned char *str = str0;
const unsigned char *limit = str + length;
int need_space = 0;
int is_method = str[0] == '(';
const unsigned char *next;
/* If printing a method, skip to the return signature and print
that first. However, there is no return value if this is a
constructor. */
if (is_method && ! is_init)
{
while (str < limit)
{
int ch = *str++;
if (ch == ')')
break;
}
}
/* If printing a field or an ordinary method, then print the
"return value" now. */
if (! is_method || ! is_init)
{
next = decode_signature_piece (stream, str, limit, &need_space);
if (! next)
{
fprintf (stderr, "unparseable signature: `%s'\n", str0);
found_error = 1;
return;
}
}
/* Now print the name of the thing. */
if (need_space)
fputs (" ", stream);
print_full_cxx_name (stream, jcf, name_index,
signature_index, is_init, name_override,
flags);
}
}
/* Print the unqualified method name followed by the signature. */
static void
DEFUN(print_full_cxx_name, (stream, jcf, name_index, signature_index,
is_init, name_override, flags),
FILE* stream AND JCF* jcf
AND int name_index AND int signature_index AND int is_init
AND const char *name_override AND int flags)
{
int length = JPOOL_UTF_LENGTH (jcf, signature_index);
const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
register const unsigned char *str = str0;
const unsigned char *limit = str + length;
int need_space = 0;
int is_method = str[0] == '(';
const unsigned char *next;
if (name_override)
fputs (name_override, stream);
else if (name_index)
{
/* Declare constructors specially. */
if (is_init)
print_base_classname (stream, jcf, jcf->this_class);
else
print_name (stream, jcf, name_index);
}
if (flag_jni)
{
unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
JPOOL_UTF_LENGTH (jcf, name_index),
signature, sig_len))
{
/* If this method is overloaded by another native method,
then include the argument information in the mangled
name. */
unsigned char *limit = signature + sig_len;
fputs ("__", stream);
while (signature < limit)
{
int ch = UTF8_GET (signature, limit);
jni_print_char (stream, ch);
if (ch == ')')
{
/* Done. */
break;
}
}
}
}
if (is_method)
{
/* Have a method or a constructor. Print signature pieces
until done. */
fputs (" (", stream);
str = str0 + 1;
/* In JNI mode, add extra arguments. */
if (flag_jni)
{
/* FIXME: it would be nice to know if we are printing a decl
or a definition, and only print `env' for the latter. */
fputs ("JNIEnv *env", stream);
fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
if (*str != ')')
fputs (", ", stream);
}
while (str < limit && *str != ')')
{
next = decode_signature_piece (stream, str, limit, &need_space);
if (! next)
{
fprintf (stderr, "unparseable signature: `%s'\n", str0);
found_error = 1;
return;
}
if (next < limit && *next != ')')
fputs (", ", stream);
str = next;
}
fputs (")", stream);
}
}
/* This is a helper for print_stub_or_jni. */
static void
DEFUN (print_name_for_stub_or_jni, (stream, jcf, name_index, signature_index,
is_init, name_override, flags),
FILE *stream AND JCF *jcf
AND int name_index AND int signature_index
AND int is_init AND const char *name_override AND int flags)
{
const char *const prefix = flag_jni ? "Java_" : "";
print_cxx_classname (stream, prefix, jcf, jcf->this_class);
fputs (flag_jni ? "_" : "::", stream);
print_full_cxx_name (stream, jcf, name_index,
signature_index, is_init, name_override,
flags);
}
static void
DEFUN(print_stub_or_jni, (stream, jcf, name_index, signature_index, is_init,
name_override, flags),
FILE* stream AND JCF* jcf
AND int name_index AND int signature_index
AND int is_init AND const char *name_override AND int flags)
{
if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
{
fprintf (stream, "<not a UTF8 constant>");
found_error = 1;
}
else
{
int length = JPOOL_UTF_LENGTH (jcf, signature_index);
const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
register const unsigned char *str = str0;
const unsigned char *limit = str + length;
int need_space = 0;
int is_method = str[0] == '(';
const unsigned char *next;
/* Don't print fields in the JNI case. */
if (! is_method && flag_jni)
return;
if (flag_jni && ! stubs)
fputs ("extern JNIEXPORT ", stream);
/* If printing a method, skip to the return signature and print
that first. However, there is no return value if this is a
constructor. */
if (is_method && ! is_init)
{
while (str < limit)
{
int ch = *str++;
if (ch == ')')
break;
}
}
/* If printing a field or an ordinary method, then print the
"return value" now. Note that a constructor can't be native,
so we don't bother checking this in the JNI case. */
if (! is_method || ! is_init)
{
next = decode_signature_piece (stream, str, limit, &need_space);
if (! next)
{
fprintf (stderr, "unparseable signature: `%s'\n", str0);
found_error = 1;
return;
}
}
/* When printing a JNI header we need to respect the space. In
other cases we're just going to insert a newline anyway. */
fputs (need_space && ! stubs ? " " : "\n", stream);
if (flag_jni && ! stubs)
fputs ("JNICALL ", stream);
/* Now print the name of the thing. */
print_name_for_stub_or_jni (stream, jcf, name_index,
signature_index, is_init, name_override,
flags);
/* Print the body. */
if (stubs)
{
if (flag_jni)
fputs ("\n{\n (*env)->FatalError (\"", stream);
else
fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream);
print_name_for_stub_or_jni (stream, jcf, name_index,
signature_index, is_init,
name_override,
flags);
fprintf (stream, " not implemented\")%s;\n}\n\n",
flag_jni ? "" : ")");
}
}
}
static void
DEFUN(print_mangled_classname, (stream, jcf, prefix, index),
FILE *stream AND JCF *jcf AND const char *prefix AND int index)
{
int name_index = JPOOL_USHORT1 (jcf, index);
fputs (prefix, stream);
jcf_print_utf8_replace (out,
JPOOL_UTF_DATA (jcf, name_index),
JPOOL_UTF_LENGTH (jcf, name_index),
'/', '_');
}
/* Print PREFIX, then a class name in C++ format. If the name refers
to an array, ignore it and don't print PREFIX. Returns 1 if
something was printed, 0 otherwise. */
static int
print_cxx_classname (stream, prefix, jcf, index)
FILE *stream;
const char *prefix;
JCF *jcf;
int index;
{
int name_index = JPOOL_USHORT1 (jcf, index);
int len, c;
const unsigned char *s, *p, *limit;
s = JPOOL_UTF_DATA (jcf, name_index);
len = JPOOL_UTF_LENGTH (jcf, name_index);
limit = s + len;
/* Explicitly omit arrays here. */
p = s;
c = UTF8_GET (p, limit);
if (c == '[')
return 0;
fputs (prefix, stream);
/* Print a leading "::" so we look in the right namespace. */
if (! flag_jni && ! stubs)
fputs ("::", stream);
while (s < limit)
{
c = UTF8_GET (s, limit);
if (c == '/')
fputs (flag_jni ? "_" : "::", stream);
else
jni_print_char (stream, c);
}
return 1;
}
int written_class_count = 0;
/* Return name of superclass. If LEN is not NULL, fill it with length
of name. */
static const unsigned char *
super_class_name (derived_jcf, len)
JCF *derived_jcf;
int *len;
{
int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
const unsigned char *supername =
JPOOL_UTF_DATA (derived_jcf, supername_index);
if (len)
*len = supername_length;
return supername;
}
/* We keep track of all the `#include's we generate, so we can avoid
duplicates. */
struct include
{
char *name;
struct include *next;
};
/* List of all includes. */
static struct include *all_includes = NULL;
/* Generate a #include. */
static void
print_include (out, utf8, len)
FILE *out;
const unsigned char *utf8;
int len;
{
struct include *incl;
if (! out)
return;
if (len == -1)
len = strlen (utf8);
for (incl = all_includes; incl; incl = incl->next)
{
/* We check the length because we might have a proper prefix. */
if (len == (int) strlen (incl->name)
&& ! strncmp (incl->name, utf8, len))
return;
}
incl = xmalloc (sizeof (struct include));
incl->name = xmalloc (len + 1);
strncpy (incl->name, utf8, len);
incl->name[len] = '\0';
incl->next = all_includes;
all_includes = incl;
fputs ("#include <", out);
jcf_print_utf8_replace (out, utf8, len,
'/',
flag_jni ? '_' : '/');
fputs (".h>\n", out);
}
/* This is used to represent part of a package or class name. */
struct namelet
{
/* The text of this part of the name. */
char *name;
/* True if this represents a class. */
int is_class;
/* Linked list of all classes and packages inside this one. */
struct namelet *subnamelets;
/* Pointer to next sibling. */
struct namelet *next;
};
static void add_namelet PARAMS ((const unsigned char *,
const unsigned char *, struct namelet *));
static void print_namelet PARAMS ((FILE *, struct namelet *, int));
/* The special root namelet. */
static struct namelet root =
{
NULL,
0,
NULL,
NULL
};
/* This extracts the next name segment from the full UTF-8 encoded
package or class name and links it into the tree. It does this
recursively. */
static void
add_namelet (name, name_limit, parent)
const unsigned char *name, *name_limit;
struct namelet *parent;
{
const unsigned char *p;
struct namelet *n = NULL, *np;
/* We want to skip the standard namespaces that we assume the
runtime already knows about. We only do this at the top level,
though, hence the check for `root'. */
if (parent == &root)
{
#define JAVALANG "java/lang/"
#define JAVAIO "java/io/"
#define JAVAUTIL "java/util/"
if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
&& ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
|| (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
&& ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
|| (name_limit - name >= (int) sizeof (JAVAIO) - 1
&& ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
return;
}
for (p = name; p < name_limit && *p != '/'; ++p)
;
/* Search for this name beneath the PARENT node. */
for (np = parent->subnamelets; np != NULL; np = np->next)
{
/* We check the length because we might have a proper prefix. */
if ((int) strlen (np->name) == p - name &&
! strncmp (name, np->name, p - name))
{
n = np;
break;
}
}
if (n == NULL)
{
n = xmalloc (sizeof (struct namelet));
n->name = xmalloc (p - name + 1);
strncpy (n->name, name, p - name);
n->name[p - name] = '\0';
n->is_class = (p == name_limit);
n->subnamelets = NULL;
n->next = parent->subnamelets;
parent->subnamelets = n;
}
/* We recurse if there is more text, and if the trailing piece does
not represent an inner class. */
if (p < name_limit)
add_namelet (p + 1, name_limit, n);
}
/* Print a single namelet. Destroys namelets while printing. */
static void
print_namelet (out, name, depth)
FILE *out;
struct namelet *name;
int depth;
{
int i, term = 0;
struct namelet *c;
if (name->name)
{
for (i = 0; i < depth; ++i)
fputc (' ', out);
fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
name->name);
if (name->is_class && name->subnamelets == NULL)
fputs (";\n", out);
else
{
term = 1;
fputs ("\n", out);
for (i = 0; i < depth; ++i)
fputc (' ', out);
fputs ("{\n", out);
}
}
c = name->subnamelets;
while (c != NULL)
{
struct namelet *next = c->next;
print_namelet (out, c, depth + 2);
c = next;
}
name->subnamelets = NULL;
if (name->name)
{
if (term)
{
for (i = 0; i < depth; ++i)
fputc (' ', out);
fputs ("}\n", out);
/* Only print a `;' when printing a class. C++ is evil. */
if (name->is_class)
fputs (";", out);
}
free (name->name);
free (name);
}
}
/* This is called to add some classes to the list of classes for which
we need decls. The signature argument can be a function
signature. */
static void
add_class_decl (out, jcf, signature)
FILE *out;
JCF *jcf;
JCF_u2 signature;
{
const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
int len = JPOOL_UTF_LENGTH (jcf, signature);
int i;
for (i = 0; i < len; ++i)
{
int start;
/* If we see an array, then we include the array header. */
if (s[i] == '[')
{
print_include (out, "gcj/array", -1);
continue;
}
/* We're looking for `L<stuff>;' -- everything else is
ignorable. */
if (s[i] != 'L')
continue;
for (start = ++i; i < len && s[i] != ';'; ++i)
;
add_namelet (&s[start], &s[i], &root);
}
}
/* Print declarations for all classes required by this class. Any
class or package in the `java' package is assumed to be handled
statically in libjava; we don't generate declarations for these.
This makes the generated headers a bit easier to read. */
static void
print_class_decls (out, jcf, self)
FILE *out;
JCF *jcf;
int self;
{
/* Make sure to always add the current class to the list of things
that should be declared. */
int name_index = JPOOL_USHORT1 (jcf, self);
int len;
const unsigned char *s;
s = JPOOL_UTF_DATA (jcf, name_index);
len = JPOOL_UTF_LENGTH (jcf, name_index);
add_namelet (s, s + len, &root);
if (root.subnamelets)
{
fputs ("extern \"Java\"\n{\n", out);
/* We use an initial offset of 0 because the root namelet
doesn't cause anything to print. */
print_namelet (out, &root, 0);
fputs ("};\n\n", out);
}
}
static void
DEFUN(process_file, (jcf, out),
JCF *jcf AND FILE *out)
{
int code, i;
uint32 field_start, method_end, method_start;
current_jcf = jcf;
last_access = -1;
if (jcf_parse_preamble (jcf) != 0)
{
fprintf (stderr, "Not a valid Java .class file.\n");
found_error = 1;
return;
}
/* Parse and possibly print constant pool */
code = jcf_parse_constant_pool (jcf);
if (code != 0)
{
fprintf (stderr, "error while parsing constant pool\n");
found_error = 1;
return;
}
code = verify_constant_pool (jcf);
if (code > 0)
{
fprintf (stderr, "error in constant pool entry #%d\n", code);
found_error = 1;
return;
}
jcf_parse_class (jcf);
if (written_class_count++ == 0 && out)
{
const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
if (flag_jni)
{
cstart = "/*";
cstart2 = " ";
cend = " */";
mode = "";
what = "JNI";
jflag = " -jni";
}
else
{
cstart = "//";
cstart2 = "//";
cend = "";
mode = " -*- c++ -*-";
what = "CNI";
jflag = "";
}
if (! stubs)
fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
cstart, mode, cend);
else
{
fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
%s\n\
%s This file is intended to give you a head start on implementing native\n\
%s methods using %s.\n\
%s Be aware: running `gcjh -stubs %s' once more for this class may\n\
%s overwrite any edits you have made to this file.%s\n\n",
cstart, jflag, mode,
cstart2,
cstart2,
cstart2,
what,
cstart2,
jflag,
cstart2,
cend);
}
}
if (out)
{
if (! stubs)
{
print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
fprintf (out, "__\n");
print_mangled_classname (out, jcf, "#define __", jcf->this_class);
fprintf (out, "__\n\n");
if (flag_jni)
{
fprintf (out, "#include <jni.h>\n\n");
fprintf (out, "#ifdef __cplusplus\n");
fprintf (out, "extern \"C\"\n");
fprintf (out, "{\n");
fprintf (out, "#endif\n");
}
else
{
/* We do this to ensure that inline methods won't be
`outlined' by g++. This works as long as method and
fields are not added by the user. */
fprintf (out, "#pragma interface\n");
if (jcf->super_class)
{
int super_length;
const unsigned char *supername =
super_class_name (jcf, &super_length);
fputs ("\n", out);
print_include (out, supername, super_length);
}
}
}
else
{
/* Strip off the ".class" portion of the name when printing
the include file name. */
char *name;
int i, len = strlen (jcf->classname);
if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
len -= 6;
/* Turn the class name into a file name. */
name = xmalloc (len + 1);
for (i = 0; i < len; ++i)
name[i] = jcf->classname[i] == '.' ? '/' : jcf->classname[i];
name[i] = '\0';
print_include (out, name, len);
free (name);
if (! flag_jni)
{
print_include (out, "gcj/cni", -1);
print_include (out, "java/lang/UnsupportedOperationException",
-1);
}
}
}
/* We want to parse the methods first. But we need to find where
they start. So first we skip the fields, then parse the methods.
Then we parse the fields and skip the methods. This is ugly, but
not too bad since we need two full passes to get class decl
information anyway. */
field_pass = 0;
field_start = JCF_TELL (jcf);
jcf_parse_fields (jcf);
method_start = JCF_TELL (jcf);
method_pass = 0;
jcf_parse_methods (jcf);
if (out)
fputs ("\n", out);
if (out && ! flag_jni)
{
if (! stubs)
print_class_decls (out, jcf, jcf->this_class);
for (i = 0; i < prepend_count; ++i)
fprintf (out, "%s\n", prepend_specs[i]);
if (prepend_count > 0)
fputc ('\n', out);
if (! stubs)
{
if (! print_cxx_classname (out, "class ", jcf, jcf->this_class))
{
fprintf (stderr, "class is of array type\n");
found_error = 1;
return;
}
if (jcf->super_class)
{
if (! print_cxx_classname (out, " : public ",
jcf, jcf->super_class))
{
fprintf (stderr, "base class is of array type\n");
found_error = 1;
return;
}
}
fputs ("\n{\n", out);
}
}
/* Now go back for second pass over methods and fields. */
JCF_SEEK (jcf, method_start);
method_pass = 1;
jcf_parse_methods (jcf);
method_end = JCF_TELL (jcf);
field_pass = 1;
JCF_SEEK (jcf, field_start);
jcf_parse_fields (jcf);
JCF_SEEK (jcf, method_end);
jcf_parse_final_attributes (jcf);
if (out && ! stubs)
{
if (flag_jni)
{
fprintf (out, "\n#ifdef __cplusplus\n");
fprintf (out, "}\n");
fprintf (out, "#endif\n");
}
else
{
/* Generate friend decl if we still must. */
for (i = 0; i < friend_count; ++i)
fprintf (out, " friend %s\n", friend_specs[i]);
/* Generate extra declarations. */
if (add_count > 0)
fputc ('\n', out);
for (i = 0; i < add_count; ++i)
fprintf (out, " %s\n", add_specs[i]);
/* Generate an entry for the class object. */
generate_access (out, ACC_PUBLIC);
fprintf (out, "\n static ::java::lang::Class class$;\n");
fputs ("}", out);
if (jcf->access_flags & ACC_INTERFACE)
fputs (" __attribute__ ((java_interface))", out);
fputs (";\n", out);
if (append_count > 0)
fputc ('\n', out);
for (i = 0; i < append_count; ++i)
fprintf (out, "%s\n", append_specs[i]);
}
print_mangled_classname (out, jcf,
"\n#endif /* __", jcf->this_class);
fprintf (out, "__ */\n");
}
}
/* This is used to mark options with no short value. */
#define LONG_OPT(Num) ((Num) + 128)
#define OPT_classpath LONG_OPT (0)
#define OPT_CLASSPATH OPT_classpath
#define OPT_bootclasspath LONG_OPT (1)
#define OPT_extdirs LONG_OPT (2)
#define OPT_HELP LONG_OPT (3)
#define OPT_TEMP LONG_OPT (4)
#define OPT_VERSION LONG_OPT (5)
#define OPT_PREPEND LONG_OPT (6)
#define OPT_FRIEND LONG_OPT (7)
#define OPT_ADD LONG_OPT (8)
#define OPT_APPEND LONG_OPT (9)
#define OPT_M LONG_OPT (10)
#define OPT_MM LONG_OPT (11)
#define OPT_MG LONG_OPT (12)
#define OPT_MD LONG_OPT (13)
#define OPT_MMD LONG_OPT (14)
static const struct option options[] =
{
{ "classpath", required_argument, NULL, OPT_classpath },
{ "bootclasspath", required_argument, NULL, OPT_bootclasspath },
{ "extdirs", required_argument, NULL, OPT_extdirs },
{ "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
{ "help", no_argument, NULL, OPT_HELP },
{ "stubs", no_argument, &stubs, 1 },
{ "td", required_argument, NULL, OPT_TEMP },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, OPT_VERSION },
{ "prepend", required_argument, NULL, OPT_PREPEND },
{ "friend", required_argument, NULL, OPT_FRIEND },
{ "add", required_argument, NULL, OPT_ADD },
{ "append", required_argument, NULL, OPT_APPEND },
{ "M", no_argument, NULL, OPT_M },
{ "MM", no_argument, NULL, OPT_MM },
{ "MG", no_argument, NULL, OPT_MG },
{ "MD", no_argument, NULL, OPT_MD },
{ "MMD", no_argument, NULL, OPT_MMD },
{ "jni", no_argument, &flag_jni, 1 },
{ NULL, no_argument, NULL, 0 }
};
static void
usage ()
{
fprintf (stderr, "Try `gcjh --help' for more information.\n");
exit (1);
}
static void
help ()
{
printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
printf ("Generate C++ header files from .class files\n\n");
printf (" -stubs Generate an implementation stub file\n");
printf (" -jni Generate a JNI header or stub\n");
printf ("\n");
printf (" -add TEXT Insert TEXT into class body\n");
printf (" -append TEXT Insert TEXT after class declaration\n");
printf (" -friend TEXT Insert TEXT as `friend' declaration\n");
printf (" -prepend TEXT Insert TEXT before start of class\n");
printf ("\n");
printf (" --classpath PATH Set path to find .class files\n");
printf (" -IDIR Append directory to class path\n");
printf (" --bootclasspath PATH Override built-in class path\n");
printf (" --extdirs PATH Set extensions directory path\n");
printf (" -d DIRECTORY Set output directory name\n");
printf (" -o FILE Set output file name\n");
printf (" -td DIRECTORY Set temporary directory name\n");
printf ("\n");
printf (" --help Print this help, then exit\n");
printf (" --version Print version number, then exit\n");
printf (" -v, --verbose Print extra information while running\n");
printf ("\n");
printf (" -M Print all dependencies to stdout;\n");
printf (" suppress ordinary output\n");
printf (" -MM Print non-system dependencies to stdout;\n");
printf (" suppress ordinary output\n");
printf (" -MD Print all dependencies to stdout\n");
printf (" -MMD Print non-system dependencies to stdout\n");
/* We omit -MG until it is implemented. */
printf ("\n");
printf ("For bug reporting instructions, please see:\n");
printf ("%s.\n", bug_report_url);
exit (0);
}
static void
version ()
{
printf ("gcjh (GCC) %s\n\n", version_string);
printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
printf ("This is free software; see the source for copying conditions. There is NO\n");
printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
exit (0);
}
int
DEFUN(main, (argc, argv),
int argc AND char** argv)
{
JCF jcf;
int argi;
char *output_file = NULL;
int emit_dependencies = 0, suppress_output = 0;
int opt;
if (argc <= 1)
{
fprintf (stderr, "gcjh: no classes specified\n");
usage ();
}
jcf_path_init ();
/* We use getopt_long_only to allow single `-' long options. For
some of our options this is more natural. */
while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
{
switch (opt)
{
case 0:
/* Already handled. */
break;
case 'o':
output_file = optarg;
break;
case 'd':
output_directory = optarg;
break;
case 'I':
jcf_path_include_arg (optarg);
break;
case 'v':
verbose++;
break;
case OPT_classpath:
jcf_path_classpath_arg (optarg);
break;
case OPT_bootclasspath:
jcf_path_bootclasspath_arg (optarg);
break;
case OPT_extdirs:
jcf_path_extdirs_arg (optarg);
break;
case OPT_HELP:
help ();
break;
case OPT_TEMP:
temp_directory = optarg;
break;
case OPT_VERSION:
version ();
break;
case OPT_PREPEND:
if (prepend_count == 0)
prepend_specs = ALLOC (argc * sizeof (char*));
prepend_specs[prepend_count++] = optarg;
break;
case OPT_FRIEND:
if (friend_count == 0)
friend_specs = ALLOC (argc * sizeof (char*));
friend_specs[friend_count++] = optarg;
break;
case OPT_ADD:
if (add_count == 0)
add_specs = ALLOC (argc * sizeof (char*));
add_specs[add_count++] = optarg;
break;
case OPT_APPEND:
if (append_count == 0)
append_specs = ALLOC (argc * sizeof (char*));
append_specs[append_count++] = optarg;
break;
case OPT_M:
emit_dependencies = 1;
suppress_output = 1;
jcf_dependency_init (1);
break;
case OPT_MM:
emit_dependencies = 1;
suppress_output = 1;
jcf_dependency_init (0);
break;
case OPT_MG:
fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
exit (1);
case OPT_MD:
emit_dependencies = 1;
jcf_dependency_init (1);
break;
case OPT_MMD:
emit_dependencies = 1;
jcf_dependency_init (0);
break;
default:
usage ();
break;
}
}
if (optind == argc)
{
fprintf (stderr, "gcjh: no classes specified\n");
usage ();
}
jcf_path_seal (verbose);
if (output_file && emit_dependencies)
{
fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
exit (1);
}
for (argi = optind; argi < argc; argi++)
{
char *classname = argv[argi];
char *current_output_file;
const char *classfile_name;
if (verbose)
fprintf (stderr, "Processing %s\n", classname);
if (! output_file)
jcf_dependency_reset ();
classfile_name = find_class (classname, strlen (classname), &jcf, 0);
if (classfile_name == NULL)
{
fprintf (stderr, "%s: no such class\n", classname);
exit (1);
}
if (verbose)
fprintf (stderr, "Found in %s\n", classfile_name);
if (output_file)
{
if (strcmp (output_file, "-") == 0)
out = stdout;
else if (out == NULL)
{
out = fopen (output_file, "w");
}
if (out == NULL)
{
perror (output_file);
exit (1);
}
current_output_file = output_file;
}
else
{
int dir_len = strlen (output_directory);
int i, classname_length = strlen (classname);
current_output_file = ALLOC (dir_len + classname_length + 5);
strcpy (current_output_file, output_directory);
if (dir_len > 0 && output_directory[dir_len-1] != '/')
current_output_file[dir_len++] = '/';
for (i = 0; classname[i] != '\0'; i++)
{
char ch = classname[i];
if (ch == '.')
ch = '/';
if (flag_jni && ch == '/')
ch = '_';
current_output_file[dir_len++] = ch;
}
if (emit_dependencies)
{
if (suppress_output)
{
jcf_dependency_set_dep_file ("-");
out = NULL;
}
else
{
/* We use `.hd' and not `.d' to avoid clashes with
dependency tracking from straight compilation. */
strcpy (current_output_file + dir_len, ".hd");
jcf_dependency_set_dep_file (current_output_file);
}
}
strcpy (current_output_file + dir_len,
stubs ? (flag_jni ? ".c" : ".cc") : ".h");
jcf_dependency_set_target (current_output_file);
if (! suppress_output)
{
out = fopen (current_output_file, "w");
if (out == NULL)
{
perror (current_output_file);
exit (1);
}
}
}
process_file (&jcf, out);
JCF_FINISH (&jcf);
if (current_output_file != output_file)
free (current_output_file);
jcf_dependency_write ();
}
if (out != NULL && out != stdout)
fclose (out);
return found_error;
}
|