/
plugin.c
1088 lines (832 loc) · 24.4 KB
/
plugin.c
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
/* GCC plugin APIs.
Copyright (C) 2009, 2010, 2011 Mingjie Xing, mingjie.xing@gmail.com.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#define _GNU_SOURCE
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
/* GCC header files. */
#include "gcc-plugin.h"
#include "plugin.h"
#include "plugin-version.h"
#include "tree.h"
#include "gimple.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "cfgloop.h"
#include "cgraph.h"
#include "options.h"
/* plugin license check */
int plugin_is_GPL_compatible;
#define MAX_RECORD_TYPES 1000
#define MAX_LINE_NUMBER 16000
tree record_types[MAX_RECORD_TYPES];
int record_type_count = 0;
#define MAX_GLOBAL_VARS 1000
tree global_vars[MAX_GLOBAL_VARS];
int global_var_count = 0;
FILE *json_f;
int json_indent_level = 0;
int json_needs_comma = 0;
#define JSON_ARRAY 1
#define JSON_OBJECT 2
//#define TRACE(...) printf(__VA_ARGS__)
#define TRACE(...) do { if(0) printf(__VA_ARGS__); } while(0)
typedef struct json_context {
int needs_comma;
int type;
int indent;
} json_context;
// GCC won't let us malloc and I don't want to mess
// around with GCC's internal memory management stuff,
// so we'll just use a fixed-size stack
json_context json_context_stack[128];
int json_context_head = 0;
void json_context_push(int type) {
json_context_head ++;
json_context_stack[json_context_head].needs_comma = false;
json_context_stack[json_context_head].type = type;
}
void json_context_pop() {
json_context_head--;
}
/* Json writing functions */
void json_indent() {
int i;
for(i=0;i<json_context_head;++i) {
fprintf(json_f, " ");
}
}
void json_pre_value() {
json_context *context = &json_context_stack[json_context_head];
if(context->type == JSON_ARRAY) {
if(context->needs_comma) {
fprintf(json_f, ",");
}
context->needs_comma = 1;
fprintf(json_f, "\n");
json_indent();
}
}
void json_start_object() {
json_pre_value();
fprintf(json_f, "{");
json_context_push(JSON_OBJECT);
}
void json_start_array() {
fprintf(json_f, "[");
json_context_push(JSON_ARRAY);
}
void json_null() {
json_pre_value();
fprintf(json_f, "null");
}
void json_field(const char *name) {
json_context *context = &json_context_stack[json_context_head];
if(context->needs_comma) {
fprintf(json_f, ",");
}
context->needs_comma = 1;
fprintf(json_f, "\n");
json_indent();
fprintf(json_f, "\"%s\": ", name);
json_context_stack[json_context_head].needs_comma = 1;
}
void json_string(const char *value, int length) {
putc('"', json_f);
while (--length >= 0) {
char ch = *value++;
if (ch >= ' ' && ch < 127) {
if(ch == '\\' || ch == '"') {
putc('\\', json_f);
}
putc(ch, json_f);
} else {
fprintf(json_f, "\\u%04x", ch);
}
}
putc('"', json_f);
}
void json_string_field(const char *name, const char *value) {
json_field(name);
json_string(value, strlen(value));
}
void json_string_field2(const char *name, const char *value, int length) {
json_field(name);
json_string(value, length);
}
void json_int(int value) {
json_pre_value();
fprintf(json_f, "%d", value);
}
void json_ptr(void *p) {
json_pre_value();
fprintf(json_f, "\"%p\"", p);
}
void json_int_field(const char *name, int value) {
json_field(name);
json_int(value);
}
void json_real_field(const char *name, REAL_VALUE_TYPE value) {
json_field(name);
if (REAL_VALUE_ISINF (value)) {
fprintf(json_f, "\"%s\"", REAL_VALUE_NEGATIVE (value) ? "-Inf" : "Inf");
} else if(REAL_VALUE_ISNAN (value)) {
fprintf(json_f, "\"%s\"", "NaN");
} else {
char string[100];
real_to_decimal (string, &value, sizeof (string), 0, 1);
fprintf(json_f, "%s", string);
}
}
void json_bool_field(const char *name, int value) {
json_field(name);
fprintf(json_f, value ? "true" : "false");
}
void json_array_field(const char *name) {
json_field(name);
json_start_array();
}
void json_end_array() {
fprintf(json_f, "\n");
json_context_pop();
json_indent();
fprintf(json_f, "]");
}
void json_end_object() {
json_context_pop();
fprintf(json_f, "\n");
json_indent();
fprintf(json_f, "}");
}
/* Post pass */
static void dump_type(tree type);
static void dump_op(tree op);
static void dump_function_type(tree type) {
TRACE("dump_function_type: dumping returnType\n");
json_field("returnType");
dump_type(TREE_TYPE(type));
// The TYPE_ARG_TYPES are a TREE_LIST of the argument types.
// The TREE_VALUE of each node in this list is the type of the
// corresponding argument; the TREE_PURPOSE is an expression
// for the default argument value, if any. If the last node in
// the list is void_list_node (a TREE_LIST node whose TREE_VALUE
// is the void_type_node), then functions of this type do not
// take variable arguments.
// Otherwise, they do take a variable number of arguments.
int variable_arguments;
TRACE("dump_function_type: dumping argumentTypes\n");
tree last_arg = TYPE_ARG_TYPES(type);
if(last_arg != NULL_TREE) {
tree next_arg = TREE_CHAIN(last_arg);
json_array_field("argumentTypes");
while(next_arg != NULL_TREE) {
dump_type(TREE_VALUE(last_arg));
last_arg = next_arg;
next_arg = TREE_CHAIN(next_arg);
}
TRACE("dump_function_type: dumping lastArg\n");
if(TREE_CODE(TREE_VALUE(last_arg)) == VOID_TYPE) {
variable_arguments = 0;
} else {
variable_arguments = 1;
dump_type(TREE_VALUE(last_arg));
}
json_end_array();
}
json_bool_field("variableArguments", variable_arguments);
}
static const char* get_type_name(tree node) {
enum tree_code_class tclass = TREE_CODE_CLASS (TREE_CODE(node));
if(tclass == tcc_declaration) {
TRACE("get_type_name: tclass == tc/c_declaration\n");
if (DECL_NAME (node)) {
return IDENTIFIER_POINTER(DECL_NAME (node));
}
} else if(tclass == tcc_type) {
TRACE("get_type_name: tclass == tcc_type\n");
if (TYPE_NAME (node)) {
if (TREE_CODE (TYPE_NAME (node)) == IDENTIFIER_NODE)
{
return IDENTIFIER_POINTER (TYPE_NAME (node));
} else if (TREE_CODE (TYPE_NAME (node)) == TYPE_DECL
&& DECL_NAME (TYPE_NAME (node)))
{
return IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (node)));
}
}
}
return NULL;
}
static void dump_record_type(tree type) {
TRACE("dump_record_type: entering\n");
// the same type may have multiple AST nodes
// http://www.codesynthesis.com/~boris/blog/2010/05/17/parsing-cxx-with-gcc-plugin-part-3/
type = TYPE_MAIN_VARIANT(type);
json_field("id");
json_ptr(type);
// have we already encountered this record type?
int i;
for(i=0;i<record_type_count;++i) {
if(record_types[i] == type) {
TRACE("dump_record_type: already seen, exiting\n");
return;
}
}
// nope, add it to the list
if(record_type_count < MAX_RECORD_TYPES) {
record_types[record_type_count] = type;
record_type_count++;
} else {
printf("TOO MANY RECORD TYPES!!!!");
// TODO: throw error here
}
TRACE("dump_record_type: added to the list, exiting\n");
}
static void dump_record_type_decl(tree type) {
TRACE("dump_record_type_decl: entering\n");
json_start_object();
json_field("id");
json_ptr(type);
json_bool_field("union", (TREE_CODE(type) == UNION_TYPE));
TRACE("dump_record_type_decl: writing name\n");
const char *name = get_type_name(type);
if(name) {
json_string_field("name", name);
TRACE("dump_record_type_decl: name = %s\n", name);
}
TRACE("dump_record_type_decl: writing fields\n");
tree field = TYPE_FIELDS(type);
json_array_field("fields");
while(field) {
json_start_object();
if(DECL_NAME(field)) {
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(field)));
}
json_field("type");
dump_type(TREE_TYPE(field));
json_end_object();
field = TREE_CHAIN(field);
}
json_end_array();
json_end_object();
TRACE("dump_record_type_decl: exiting\n");
}
static void dump_constructor(tree node) {
unsigned HOST_WIDE_INT ix;
tree field, val;
bool is_struct_init = FALSE;
json_array_field("elements");
if (TREE_CODE (TREE_TYPE (node)) == RECORD_TYPE
|| TREE_CODE (TREE_TYPE (node)) == UNION_TYPE)
is_struct_init = TRUE;
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (node), ix, field, val)
{
json_start_object();
if (field && is_struct_init)
{
json_field("field");
dump_op(field);
}
json_field("value");
dump_op(val);
json_end_object();
}
json_end_array();
}
static void dump_type(tree type) {
TRACE("dump_type: entering: %s\n", tree_code_name[TREE_CODE(type)]);
json_start_object();
json_string_field("type", tree_code_name[TREE_CODE(type)]);
if(TYPE_SIZE(type)) {
json_int_field("size", TREE_INT_CST_LOW(TYPE_SIZE(type)));
}
switch(TREE_CODE(type)) {
case INTEGER_TYPE:
json_bool_field("unsigned", TYPE_UNSIGNED(type));
break;
case REAL_TYPE:
break;
case POINTER_TYPE:
case REFERENCE_TYPE:
json_field("baseType");
dump_type(TREE_TYPE(type));
break;
case ARRAY_TYPE:
json_field("componentType");
dump_type(TREE_TYPE(type));
if(TYPE_DOMAIN(type)) {
tree domain = TYPE_DOMAIN(type);
json_field("lbound");
json_int(TREE_INT_CST_LOW(TYPE_MIN_VALUE(domain)));
if(TYPE_MAX_VALUE(domain)) {
json_field("ubound");
json_int(TREE_INT_CST_LOW(TYPE_MAX_VALUE(domain)));
}
}
break;
case FUNCTION_TYPE:
dump_function_type(type);
break;
case UNION_TYPE:
case RECORD_TYPE:
dump_record_type(type);
break;
}
json_end_object();
TRACE("dump_type: exiting: %s\n", tree_code_name[TREE_CODE(type)]);
}
static void dump_global_var_ref(tree decl) {
int i;
for(i=0;i!=global_var_count;++i) {
if(global_vars[i] == decl) {
return;
}
}
global_vars[global_var_count] = decl;
global_var_count++;
}
static void dump_op(tree op) {
REAL_VALUE_TYPE d;
TRACE("dump_op: entering\n");
if(op) {
if(TREE_CODE(op)) {
TRACE("dump_op: code = %s\n", tree_code_name[TREE_CODE(op)]);
} else {
TRACE("dump_op: TREE_CODE(op) == NULL\n");
}
// keep track of global variable references
if(TREE_CODE(op) == VAR_DECL &&
DECL_CONTEXT(op) &&
(TREE_CODE(DECL_CONTEXT(op)) == NULL_TREE || TREE_CODE(DECL_CONTEXT(op)) != FUNCTION_DECL)) {
dump_global_var_ref(op);
}
json_start_object();
json_string_field("code", tree_code_name[TREE_CODE(op)]);
json_field("type");
dump_type(TREE_TYPE(op));
TRACE("dump_op: starting switch\n");
switch(TREE_CODE(op)) {
case FUNCTION_DECL:
case PARM_DECL:
case FIELD_DECL:
case VAR_DECL:
json_int_field("id", DEBUG_TEMP_UID (op));
if(DECL_NAME(op)) {
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(op)));
}
break;
case CONST_DECL:
json_field("value");
dump_op(DECL_INITIAL(op));
break;
case INTEGER_CST:
json_int_field("value", TREE_INT_CST_LOW (op));
json_field("type");
dump_type(TREE_TYPE(op));
break;
case REAL_CST:
json_real_field("value", TREE_REAL_CST(op));
json_field("type");
dump_type(TREE_TYPE(op));
break;
case COMPLEX_CST:
json_field("real");
dump_op(TREE_REALPART(op));
json_field("im");
dump_op(TREE_IMAGPART(op));
break;
case STRING_CST:
json_string_field2("value",
TREE_STRING_POINTER(op),
TREE_STRING_LENGTH (op));
json_field("type");
dump_type(TREE_TYPE(op));
break;
case MEM_REF:
json_field("pointer");
dump_op(TREE_OPERAND(op, 0));
json_field("offset");
dump_op(TREE_OPERAND(op, 1));
break;
case NOP_EXPR:
json_field("value");
dump_op(TREE_OPERAND(op, 0));
break;
case ARRAY_REF:
json_field("array");
dump_op(TREE_OPERAND(op, 0));
json_field("index");
dump_op(TREE_OPERAND(op, 1));
break;
case REALPART_EXPR:
case IMAGPART_EXPR:
json_field("complexValue");
dump_op(TREE_OPERAND(op, 0));
break;
case ADDR_EXPR:
json_field("value");
dump_op(TREE_OPERAND(op, 0));
// json_field("offset");
// dump_op(TREE_OPERAND(op, 1));
break;
case CONSTRUCTOR:
dump_constructor(op);
break;
case COMPONENT_REF:
json_field("value");
TRACE("dump_op: writing COMPONENT_REF value\n");
dump_op(TREE_OPERAND(op, 0));
json_field("member");
TRACE("dump_op: writing COMPONENT_REF member\n");
dump_op(TREE_OPERAND(op, 1));
//
// if(TREE_CODE(TREE_OPERAND(op, 1)) == FIELD_DECL) {
// json_string_field("member",
// IDENTIFIER_POINTER(DECL_NAME(TREE_OPERAND(op, 1))));
// }
break;
}
json_end_object();
} else {
json_null();
}
TRACE("dump_op: exiting\n");
}
static void dump_ops(gimple stmt) {
int numops = gimple_num_ops(stmt);
if(numops > 0) {
json_array_field("operands");
int i;
for(i=0;i<numops;++i) {
tree op = gimple_op(stmt, i);
if(op) {
dump_op(op);
}
}
json_end_array();
}
}
static void dump_assignment(gimple stmt) {
json_start_object();
json_string_field("type", "assign");
json_int_field("line", gimple_lineno(stmt));
json_string_field("operator", tree_code_name[gimple_assign_rhs_code(stmt)]);
json_field("lhs");
dump_op(gimple_assign_lhs(stmt));
tree rhs1 = gimple_assign_rhs1(stmt);
tree rhs2 = gimple_assign_rhs2(stmt);
tree rhs3 = gimple_assign_rhs3(stmt);
json_array_field("operands");
if(rhs1) dump_op(rhs1);
if(rhs2) dump_op(rhs2);
if(rhs3) dump_op(rhs3);
json_end_array();
json_end_object();
}
static void dump_cond(basic_block bb, gimple stmt) {
json_start_object();
json_string_field("type", "conditional");
json_int_field("line", gimple_lineno(stmt));
json_string_field("operator", tree_code_name[gimple_assign_rhs_code(stmt)]);
dump_ops(stmt);
edge true_edge, false_edge;
extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
json_int_field("trueLabel", true_edge->dest->index);
json_int_field("falseLabel", false_edge->dest->index);
json_end_object();
}
static void dump_nop(gimple stmt) {
json_start_object();
json_string_field("type", "nop");
json_int_field("line", gimple_lineno(stmt));
json_end_object();
}
static void dump_return(gimple stmt) {
json_start_object();
json_string_field("type", "return");
json_int_field("line", gimple_lineno(stmt));
tree retval = gimple_return_retval(stmt);
if(retval) {
json_field("value");
dump_op(retval);
}
json_end_object();
}
static void dump_call(gimple stmt) {
json_start_object();
json_string_field("type", "call");
json_int_field("line", gimple_lineno(stmt));
json_field("lhs");
dump_op(gimple_call_lhs(stmt));
json_field("function");
dump_op(gimple_call_fn(stmt));
int numargs = gimple_call_num_args(stmt);
if(numargs > 0) {
json_array_field("arguments");
int i;
for(i=0;i<numargs;++i) {
dump_op(gimple_call_arg(stmt,i));
}
json_end_array();
}
json_end_object();
}
static void dump_switch(gimple stmt) {
json_start_object();
int num_ops = gimple_num_ops(stmt);
json_string_field("type", "switch");
json_int_field("line", gimple_lineno(stmt));
json_field("value");
dump_op(gimple_op(stmt, 0));
json_array_field("cases");
int default_case = -1;
int i;
for(i=1;i<num_ops;++i) {
tree t = gimple_op(stmt, i);
basic_block bb = label_to_block (CASE_LABEL (t));
if(!CASE_LOW(t)) {
// this is the default case
default_case = bb->index;
} else {
// proper case
json_start_object();
json_int_field("low",
TREE_INT_CST_LOW(CASE_LOW(t)));
if (CASE_HIGH (t)) {
json_int_field("high",
TREE_INT_CST_LOW(CASE_HIGH(t)));
} else {
json_int_field("high",
TREE_INT_CST_LOW(CASE_LOW(t)));
}
json_int_field("basicBlockIndex", bb->index);
json_end_object();
}
}
json_end_array();
json_field("defaultCase");
json_start_object();
json_int_field("basicBlockIndex", default_case);
json_end_object();
json_end_object();
}
static void dump_statement(basic_block bb, gimple stmt) {
TRACE("dump_statement: entering\n");
if(gimple_code(stmt) == VAR_DECL) {
// not exactly sure what this does, but GCC seems to dump
// this as:
// predicted unlikely by continue predictor
return;
}
switch(gimple_code(stmt)) {
case GIMPLE_ASSIGN:
dump_assignment(stmt);
break;
case GIMPLE_CALL:
dump_call(stmt);
break;
case GIMPLE_COND:
dump_cond(bb, stmt);
break;
case GIMPLE_NOP:
dump_nop(stmt);
break;
case GIMPLE_RETURN:
dump_return(stmt);
break;
case OFFSET_TYPE:
dump_switch(stmt);
break;
case BLOCK:
// this represents a label expression, usually generated
// in conjunction with a switch statement. We do the
// label to basic block translation there, so we don't need
// these nodes
break;
default:
json_start_object();
json_string_field("type",
tree_code_name[gimple_code(stmt)]);
dump_ops(stmt);
json_end_object();
}
TRACE("dump_statement: exiting\n");
}
static void dump_argument(tree arg) {
TRACE("dump_argument: entering\n");
json_start_object();
if(DECL_NAME(arg)) {
TRACE("dump_argument: dumping name = '%s'\n", IDENTIFIER_POINTER(DECL_NAME(arg)));
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(arg)));
}
TRACE("dump_argument: dumping id\n");
json_int_field("id", DEBUG_TEMP_UID (arg));
TRACE("dump_argument: dumping type\n");
json_field("type");
dump_type(TREE_TYPE(arg));
json_end_object();
TRACE("dump_argument: exiting\n");
}
static void dump_arguments(tree decl) {
tree arg = DECL_ARGUMENTS(decl);
if(arg) {
json_array_field("parameters");
while(arg) {
TRACE("dump_arguments: about to call dump_argument\n");
dump_argument(arg);
TRACE("dump_arguments: called dump_argument\n");
arg = TREE_CHAIN(arg);
}
json_end_array();
}
TRACE("dump_arguments: exiting\n");
}
static void dump_local_decl(tree decl) {
json_start_object();
if(DECL_NAME(decl)) {
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(decl)));
}
json_int_field("id", DEBUG_TEMP_UID (decl));
if(TREE_TYPE(decl)) {
json_field("type");
dump_type(TREE_TYPE(decl));
}
if(DECL_INITIAL(decl)) {
json_field("value");
dump_op(DECL_INITIAL(decl));
}
json_end_object();
}
static void dump_local_decls(struct function *fun) {
unsigned ix;
tree var;
json_array_field("variableDeclarations");
FOR_EACH_LOCAL_DECL (fun, ix, var)
{
dump_local_decl(var);
}
json_end_array();
}
static void dump_basic_block(basic_block bb) {
json_start_object();
json_int_field("index", bb->index);
json_array_field("statements");
gimple_stmt_iterator gsi;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
dump_statement(bb, gsi_stmt (gsi));
}
edge e = find_fallthru_edge (bb->succs);
if (e && e->dest != bb->next_bb)
{
json_start_object();
json_string_field("type", "goto");
json_int_field("target", e->dest->index);
json_end_object();
}
json_end_array();
json_end_object();
}
static unsigned int dump_function (void)
{
if(errorcount > 0) {
return 0;
}
TRACE("dump_function: entering %s\n", IDENTIFIER_POINTER(DECL_NAME(cfun->decl)) );
basic_block bb;
json_start_object();
json_int_field("id", DEBUG_TEMP_UID (cfun->decl));
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(cfun->decl)));
json_bool_field("extern", TREE_PUBLIC(cfun->decl));
TRACE("dump_function: dumping arguments...\n");
dump_arguments(cfun->decl);
TRACE("dump_function: dumping locals...\n");
dump_local_decls(cfun);
TRACE("dump_function: dumping basic blocks...\n");
json_array_field("basicBlocks");
FOR_EACH_BB (bb)
{
dump_basic_block(bb);
}
json_end_array();
json_field("returnType");
TRACE("dump_function: dumping return type\n");
dump_type(TREE_TYPE(DECL_RESULT(cfun->decl)));
TRACE("dump_function: finished dumping return type\n");
json_end_object();
TRACE("dump_function: exiting %s\n", IDENTIFIER_POINTER(DECL_NAME(cfun->decl)) );
return 0;
}
static void dump_type_decl (void *event_data, void *data)
{
tree type = (tree) event_data;
json_start_object();
if(TYPE_NAME(type)) {
json_string_field("name", IDENTIFIER_POINTER (TYPE_NAME (type)));
}
json_string_field("type", tree_code_name[TREE_CODE (type)]);
json_end_object();
}
static void dump_global_var(tree var) {
TRACE("dump_global_var: entering\n");
json_start_object();
json_int_field("id", DEBUG_TEMP_UID(var));
if(DECL_NAME(var)) {
json_string_field("name", IDENTIFIER_POINTER(DECL_NAME(var)));
}
json_bool_field("extern", TREE_PUBLIC(var));
json_field("type");
dump_type(TREE_TYPE(var));
json_bool_field("const", DECL_INITIAL(var) && TREE_CONSTANT(DECL_INITIAL(var)));
if(DECL_INITIAL(var)) {
json_field("value");
dump_op(DECL_INITIAL(var));
}
json_end_object();
TRACE("dump_global_var: exiting\n");
}
static void start_unit_callback (void *gcc_data, void *user_data)
{
json_start_object();
json_array_field("functions");