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
|
/* 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 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 "jcf.h"
#include "java-opcodes.h"
#include <math.h>
/* The output file. */
FILE *out = NULL;
/* Nonzero on failure. */
static int found_error = 0;
/* 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;
struct JCF *main_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;
#define ACC_VISIBILITY (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED)
/* 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;
struct method_name *next;
};
/* List of method names we've seen. */
static struct method_name *method_name_list;
static void print_field_info PROTO ((FILE *, JCF*, int, int, JCF_u2));
static void print_method_info PROTO ((FILE *, JCF*, int, int, JCF_u2));
static void print_c_decl PROTO ((FILE*, JCF*, int, int, JCF_u2, int, const char *));
static void decompile_method PROTO ((FILE *, JCF *, 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 start
of the methods. Then we go back and parse the fields for real.
This is ugly. */
static int field_pass;
#define HANDLE_END_FIELD() \
if (out && field_pass) print_field_info (out, jcf, current_field_name, \
current_field_signature, \
current_field_flags);
#define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
static int method_declared = 0;
static int method_access = 0;
#define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
if (out) { decompiled = 0; \
print_method_info (out, jcf, NAME, SIGNATURE, ACCESS_FLAGS); \
}
#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) fputs (decompiled ? "\n" : ";\n", out);
#include "jcf-reader.c"
/* Some useful constants. */
#define F_NAN_MASK 0x7f800000
#define D_NAN_MASK 0x7ff0000000000000LL
/* Return 1 if F is not Inf or NaN. */
static int
java_float_finite (f)
jfloat f;
{
union {
jfloat f;
int32 i;
} 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 {
jdouble d;
int64 i;
} u;
u.d = d;
/* Now check for all NaNs. */
return (u.i & D_NAN_MASK) != D_NAN_MASK;
}
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>");
else
jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
JPOOL_UTF_LENGTH (jcf, name_index));
}
/* 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;
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, nonzero otherwise. */
static int
utf8_cmp (str, length, name)
unsigned char *str;
int length;
char *name;
{
unsigned char *limit = str + length;
int i;
for (i = 0; name[i]; ++i)
{
int ch = UTF8_GET (str, limit);
if (ch != name[i])
return 1;
}
return str != limit;
}
/* Generate an access control keyword based on FLAGS. Returns 0 if
FLAGS matches the saved access information, nonzero otherwise. */
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)
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;
}
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;
if (flags & ACC_FINAL)
{
if (current_field_value > 0)
{
char buffer[25];
generate_access (stream, flags);
switch (JPOOL_TAG (jcf, current_field_value))
{
case CONSTANT_Integer:
{
jint num;
int most_negative = 0;
fputs (" static const jint ", out);
print_name (out, jcf, name_index);
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 == 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 (" static const jlong ", out);
print_name (out, jcf, name_index);
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 == 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 (" static const jfloat ", out);
print_name (out, jcf, name_index);
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 (" static const jdouble ", out);
print_name (out, jcf, name_index);
if (! java_double_finite (dnum))
fputs (";\n", out);
else
fprintf (out, " = %.17g;\n", dnum);
}
break;
default:
fprintf(out, " <<inappropriate constant type>>\n");
}
return;
}
}
generate_access (stream, flags);
fputs (" ", out);
if ((flags & ACC_STATIC))
fputs ("static ", out);
if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
{
fprintf (stream, "<not a UTF8 constant>");
found_error = 1;
}
else
{
unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
int length = JPOOL_UTF_LENGTH (jcf, name_index);
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;
}
override = (char *) malloc (length + 3);
memcpy (override, name, length);
strcpy (override + length, "__");
}
}
print_c_decl (out, jcf, name_index, sig_index, flags, 0, override);
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)
{
unsigned char *str;
int length, is_init = 0;
const char *override = NULL;
method_declared = 0;
method_access = flags;
if (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 internally generated methods like <clinit>. However,
treat <init> as a constructor. */
if (! utf8_cmp (str, length, "<init>"))
is_init = 1;
else
return;
}
else
{
struct method_name *nn;
nn = (struct method_name *) malloc (sizeof (struct method_name));
nn->name = (char *) malloc (length);
memcpy (nn->name, str, length);
nn->length = length;
nn->next = method_name_list;
method_name_list = nn;
}
/* We can't generate a method whose name is a C++ reserved word.
For now the only problem has been `delete'; add more here as
required. 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. */
if (! utf8_cmp (str, length, "delete"))
{
/* If the method is static, we can safely skip it. If we don't
skip it then we'll have problems since the mangling will be
wrong. FIXME. */
if ((flags & ACC_STATIC))
return;
override = "__dummy_delete";
}
generate_access (stream, flags);
fputs (" ", out);
if ((flags & ACC_STATIC))
fputs ("static ", out);
else if (! (flags & ACC_FINAL) && ! (jcf->access_flags & ACC_FINAL))
{
/* Don't print `virtual' if we have a constructor. */
if (! is_init)
fputs ("virtual ", out);
}
print_c_decl (out, jcf, name_index, sig_index, flags, is_init, override);
if ((flags & ACC_ABSTRACT))
fputs (" = 0", out);
else
method_declared = 1;
}
/* 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;
{
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'. */
fputs (" { return ", out);
index = (codes[2] << 8) | codes[3];
/* FIXME: ensure that tag is CONSTANT_Fieldref. */
/* FIXME: ensure that the field's class is this class. */
name_and_type = JPOOL_USHORT2 (jcf, index);
/* FIXME: ensure that tag is CONSTANT_NameAndType. */
name = JPOOL_USHORT1 (jcf, name_and_type);
print_name (out, jcf, name);
fputs ("; }", out);
decompiled = 1;
}
else if (code_len == 2
&& codes[0] == OPCODE_aload_0
&& codes[1] == OPCODE_areturn)
{
/* Found `return this'. */
fputs (" { return this; }", out);
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;
}
}
/* Print one piece of a signature. Returns pointer to next parseable
character on success, NULL on error. */
static unsigned char *
decode_signature_piece (stream, signature, limit, need_space)
FILE *stream;
unsigned char *signature, *limit;
int *need_space;
{
const char *ctype;
switch (signature[0])
{
case '[':
for (signature++; (signature < limit
&& *signature >= '0'
&& *signature <= '9'); signature++)
;
switch (*signature)
{
case 'B': ctype = "jbyteArray"; goto printit;
case 'C': ctype = "jcharArray"; goto printit;
case 'D': ctype = "jdoubleArray"; goto printit;
case 'F': ctype = "jfloatArray"; goto printit;
case 'I': ctype = "jintArray"; goto printit;
case 'S': ctype = "jshortArray"; goto printit;
case 'J': ctype = "jlongArray"; goto printit;
case 'Z': ctype = "jbooleanArray"; goto printit;
case '[': ctype = "jobjectArray"; goto printit;
case 'L':
/* We have to generate a reference to JArray here,
so that our output matches what the compiler
does. */
++signature;
fputs ("JArray<", stream);
while (signature < limit && *signature != ';')
{
int ch = UTF8_GET (signature, limit);
if (ch == '/')
fputs ("::", stream);
else
jcf_print_char (stream, ch);
}
fputs (" *> *", stream);
*need_space = 0;
++signature;
break;
default:
/* Unparseable signature. */
return NULL;
}
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':
++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;
jcf_print_char (stream, *signature++);
break;
printit:
signature++;
*need_space = 1;
fputs (ctype, stream);
break;
}
return signature;
}
static void
DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, flags, is_init,
name_override),
FILE* stream AND JCF* jcf
AND int name_index AND int signature_index AND JCF_u2 flags
AND int is_init AND const char *name_override)
{
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);
unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
register unsigned char *str = str0;
unsigned char *limit = str + length;
int need_space = 0;
int is_method = str[0] == '(';
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);
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 (is_method)
{
/* Have a method or a constructor. Print signature pieces
until done. */
fputs (" (", stream);
str = str0 + 1;
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);
}
}
}
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;
char *prefix;
JCF *jcf;
int index;
{
int name_index = JPOOL_USHORT1 (jcf, index);
int len, c;
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);
while (s < limit)
{
c = UTF8_GET (s, limit);
if (c == '/')
fputs ("::", stream);
else
jcf_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 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);
unsigned char *supername = JPOOL_UTF_DATA (derived_jcf, supername_index);
if (len)
*len = supername_length;
return supername;
}
/* Print declarations for all classes required by this class. FIXME:
the current implementation just prints every class name from the
constant pool. This is too much. We really only need to print a
declaration for each class which is the type of a return value, a
field, or an argument. */
static void
print_class_decls (out, jcf)
FILE *out;
JCF *jcf;
{
int i, seen_one = 0;
for (i = 1; i < JPOOL_SIZE (jcf); ++i)
{
int kind = JPOOL_TAG (jcf, i);
if (kind == CONSTANT_Class)
{
if (print_cxx_classname (out, "class ", jcf, i))
fputs (";\n", out);
seen_one = 1;
}
}
if (seen_one)
fputs ("\n", out);
}
static void
DEFUN(process_file, (jcf, out),
JCF *jcf AND FILE *out)
{
int code, i;
uint32 field_start, method_end;
current_jcf = main_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)
fputs ("// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-\n\n",
out);
if (out)
{
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");
/* 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\n");
}
if (jcf->super_class && out)
{
int super_length;
unsigned char *supername = super_class_name (jcf, &super_length);
fputs ("#include <", out);
jcf_print_utf8 (out, supername, super_length);
fputs (".h>\n", out);
/* FIXME: If our superclass is Object, then we include
java-array.h. The right thing to do here is look at all the
methods and fields and see if an array is in use. Only then
would we need to include java-array.h. */
if (! utf8_cmp (supername, super_length, "java/lang/Object"))
fputs ("#include <java-array.h>\n", out);
fputs ("\n", out);
}
if (out)
{
print_class_decls (out, jcf);
for (i = 0; i < prepend_count; ++i)
fprintf (out, "%s\n", prepend_specs[i]);
if (prepend_count > 0)
fputc ('\n', out);
}
if (out && ! print_cxx_classname (out, "class ", jcf, jcf->this_class))
{
fprintf (stderr, "class is of array type\n");
found_error = 1;
return;
}
if (out && 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;
}
}
if (out)
fputs ("\n{\n", out);
/* We make a single pass over the file, printing methods and fields
as we see them. We have to list the methods in the same order
that they appear in the class file, so that the Java and C++
vtables have the same layout. */
/* 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. FIXME:
this is ugly. */
field_pass = 0;
field_start = JCF_TELL (jcf);
jcf_parse_fields (jcf);
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)
{
/* 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]);
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");
}
}
static void
usage ()
{
fprintf (stderr, "gcjh: no classes specified\n");
exit (1);
}
static void
help ()
{
printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
printf ("Generate C++ header files from .class files\n\n");
printf (" --classpath PATH Set path to find .class files\n");
printf (" --CLASSPATH PATH Set path to find .class files\n");
printf (" -IDIR Append directory to class path\n");
printf (" -d DIRECTORY Set output directory name\n");
printf (" --help Print this help, then exit\n");
printf (" -o FILE Set output file name\n");
printf (" -td DIRECTORY Set temporary directory name\n");
printf (" -v, --verbose Print extra information while running\n");
printf (" --version Print version number, then exit\n");
/* FIXME: print bug-report information. */
exit (0);
}
static void
java_no_argument (opt)
char *opt;
{
fprintf (stderr, "gcjh: no argument given for option `%s'\n", opt);
exit (1);
}
static void
version ()
{
/* FIXME: use version.c? */
printf ("gcjh (GNU gcc) 0.0\n\n");
printf ("Copyright (C) 1998 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;
if (argc <= 1)
usage ();
jcf_path_init ();
for (argi = 1; argi < argc; argi++)
{
char *arg = argv[argi];
if (arg[0] != '-' || ! strcmp (arg, "--"))
break;
/* Just let all arguments be given in either "-" or "--" form. */
if (arg[1] == '-')
++arg;
if (strcmp (arg, "-o") == 0)
{
if (argi + 1 < argc)
output_file = argv[++argi];
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-d") == 0)
{
if (argi + 1 < argc)
output_directory = argv[++argi];
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-td") == 0)
{
if (argi + 1 < argc)
temp_directory = argv[++argi];
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-prepend") == 0)
{
if (argi + 1 < argc)
{
if (prepend_count == 0)
prepend_specs = (char**) ALLOC ((argc-argi) * sizeof (char*));
prepend_specs[prepend_count++] = argv[++argi];
}
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-friend") == 0)
{
if (argi + 1 < argc)
{
if (friend_count == 0)
friend_specs = (char**) ALLOC ((argc-argi) * sizeof (char*));
friend_specs[friend_count++] = argv[++argi];
}
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-add") == 0)
{
if (argi + 1 < argc)
{
if (add_count == 0)
add_specs = (char**) ALLOC ((argc-argi) * sizeof (char*));
add_specs[add_count++] = argv[++argi];
}
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-append") == 0)
{
if (argi + 1 < argc)
{
if (append_count == 0)
append_specs = (char**) ALLOC ((argc-argi) * sizeof (char*));
append_specs[append_count++] = argv[++argi];
}
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-classpath") == 0)
{
if (argi + 1 < argc)
jcf_path_classpath_arg (argv[++argi]);
else
java_no_argument (argv[argi]);
}
else if (strcmp (arg, "-CLASSPATH") == 0)
{
if (argi + 1 < argc)
jcf_path_CLASSPATH_arg (argv[++argi]);
else
java_no_argument (argv[argi]);
}
else if (strncmp (arg, "-I", 2) == 0)
jcf_path_include_arg (arg + 2);
else if (strcmp (arg, "-verbose") == 0 || strcmp (arg, "-v") == 0)
verbose++;
else if (strcmp (arg, "-stubs") == 0)
stubs++;
else if (strcmp (arg, "-help") == 0)
help ();
else if (strcmp (arg, "-version") == 0)
version ();
else if (strcmp (arg, "-M") == 0)
{
emit_dependencies = 1;
suppress_output = 1;
jcf_dependency_init (1);
}
else if (strcmp (arg, "-MM") == 0)
{
emit_dependencies = 1;
suppress_output = 1;
jcf_dependency_init (0);
}
else if (strcmp (arg, "-MG") == 0)
{
fprintf (stderr, "gcjh: `%s' option is unimplemented\n", argv[argi]);
exit (1);
}
else if (strcmp (arg, "-MD") == 0)
{
emit_dependencies = 1;
jcf_dependency_init (1);
}
else if (strcmp (arg, "-MMD") == 0)
{
emit_dependencies = 1;
jcf_dependency_init (0);
}
else
{
fprintf (stderr, "%s: illegal argument\n", argv[argi]);
exit (1);
}
}
if (argi == argc)
usage ();
jcf_path_seal ();
if (output_file && emit_dependencies)
{
fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
exit (1);
}
for (; argi < argc; argi++)
{
char *classname = argv[argi];
char *classfile_name, *current_output_file;
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 = (char*) ALLOC (dir_len + classname_length + 4);
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 = '/';
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, ".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;
}
/* TODO:
* Do whatever the javah -stubs flag does.
* Emit "structure forward declarations" when needed.
* Generate C headers, like javah
*/
|