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
|
// semantic analysis pass, beginnings of elaboration
// Copyright 2005 Red Hat Inc.
// GPL
#include "config.h"
#include "staptree.h"
#include "elaborate.h"
#include "translate.h"
#include <iostream>
using namespace std;
// ------------------------------------------------------------------------
// toy provider/unparser pair
struct test_derived_probe: public derived_probe
{
test_derived_probe (probe* p);
test_derived_probe (probe* p, probe_point* l);
void emit_registrations (translator_output* o, unsigned i);
void emit_deregistrations (translator_output* o, unsigned i);
void emit_probe_entries (translator_output* o, unsigned i);
};
void
symresolution_info::derive_probes (probe *p, vector<derived_probe*>& dps)
{
// XXX: there will be real ones coming later
for (unsigned i=0; i<p->locations.size(); i++)
dps.push_back (new test_derived_probe (p, p->locations[i]));
}
// unique name generator
static unsigned tmpidxgen;
struct test_unparser: public unparser, public visitor
{
systemtap_session* session;
translator_output* o;
derived_probe* current_probe;
unsigned current_probenum;
functiondecl* current_function;
test_unparser (systemtap_session *ss): session (ss), o (ss->op),
current_probe(0), current_function (0) {}
~test_unparser () {}
void emit_common_header ();
void emit_global (vardecl* v);
void emit_functionsig (functiondecl* v);
void emit_module_init ();
void emit_module_exit ();
void emit_function (functiondecl* v);
void emit_probe (derived_probe* v, unsigned i);
// XXX: for use by loop/nesting constructs
// vector<string> loop_break_labels;
// vector<string> loop_continue_labels;
string c_typename (exp_type e);
string c_varname (const string& e);
void visit_block (block *s);
void visit_null_statement (null_statement *s);
void visit_expr_statement (expr_statement *s);
void visit_if_statement (if_statement* s);
void visit_for_loop (for_loop* s);
void visit_return_statement (return_statement* s);
void visit_delete_statement (delete_statement* s);
void visit_literal_string (literal_string* e);
void visit_literal_number (literal_number* e);
void visit_binary_expression (binary_expression* e);
void visit_unary_expression (unary_expression* e);
void visit_pre_crement (pre_crement* e);
void visit_post_crement (post_crement* e);
void visit_logical_or_expr (logical_or_expr* e);
void visit_logical_and_expr (logical_and_expr* e);
void visit_array_in (array_in* e);
void visit_comparison (comparison* e);
void visit_concatenation (concatenation* e);
void visit_exponentiation (exponentiation* e);
void visit_ternary_expression (ternary_expression* e);
void visit_assignment (assignment* e);
void visit_symbol (symbol* e);
void visit_arrayindex (arrayindex* e);
void visit_functioncall (functioncall* e);
};
// ------------------------------------------------------------------------
// Perform inter-script dependency analysis [XXX: later],
// then provider elaboration and derived probe construction
// and finally semantic analysis
// on the reachable set of probes/functions from the user_file
int
resolution_pass (systemtap_session& s)
{
int rc = 0;
for (unsigned i=0; i<s.user_file->probes.size(); i++)
{
probe* p = s.user_file->probes[i];
// XXX: should of course be based on each probe_point
derived_probe *dp = new test_derived_probe (p);
s.probes.push_back (dp);
}
// XXX: merge functiondecls
// XXX: handle library files
// XXX: add builtin variables/functions
return rc;
}
// ------------------------------------------------------------------------
translator_output::translator_output (ostream& f):
o2 (0), o (f), tablevel (0)
{
}
translator_output::translator_output (const string& filename):
o2 (new ofstream (filename.c_str ())), o (*o2), tablevel (0)
{
}
translator_output::~translator_output ()
{
delete o2;
}
ostream&
translator_output::newline (int indent)
{
assert (indent > 0 || tablevel >= (unsigned)-indent);
tablevel += indent;
o << endl;
for (unsigned i=0; i<tablevel; i++)
o << " ";
return o;
}
void
translator_output::indent (int indent)
{
assert (indent > 0 || tablevel >= (unsigned)-indent);
tablevel += indent;
}
ostream&
translator_output::line ()
{
return o;
}
// ------------------------------------------------------------------------
test_derived_probe::test_derived_probe (probe* p): derived_probe (p)
{
}
test_derived_probe::test_derived_probe (probe* p, probe_point* l):
derived_probe (p, l)
{
}
void
test_derived_probe::emit_registrations (translator_output* o, unsigned i)
{
// XXX
o->newline() << "rc = 0; /* no registration for probe " << i << " */";
}
void
test_derived_probe::emit_deregistrations (translator_output* o, unsigned i)
{
// XXX
o->newline() << "rc = 0; /* no deregistration for probe " << i << " */";
}
void
test_derived_probe::emit_probe_entries (translator_output* o, unsigned j)
{
for (unsigned i=0; i<locations.size(); i++)
{
probe_point *l = locations[i];
o->newline() << "/* location " << i << ": " << *l << " */";
o->newline() << "static void enter_" << j << "_" << i << " ()";
o->newline() << "{";
o->newline(1) << "struct context* c = & contexts [0];";
// XXX: assert #0 is free; need locked search instead
o->newline() << "if (c->busy) { errorcount ++; return; }";
o->newline() << "c->busy ++;";
o->newline() << "c->actioncount = 0;";
o->newline() << "c->nesting = 0;";
// NB: locals are initialized by probe function itself
o->newline() << "probe_" << j << " (c);";
o->newline() << "c->busy --;";
o->newline(-1) << "}" << endl;
}
}
// ------------------------------------------------------------------------
void
test_unparser::emit_common_header ()
{
o->newline() << "#include <string.h>";
o->newline() << "#define NR_CPU 1";
o->newline() << "#define MAXNESTING 30";
o->newline() << "#define MAXCONCURRENCY NR_CPU";
o->newline() << "#define MAXSTRINGLEN 128";
o->newline() << "#define MAXACTION 1000";
o->newline();
o->newline() << "typedef char string_t[MAXSTRINGLEN];";
o->newline() << "typedef struct { int a; } stats_t;";
o->newline();
o->newline() << "unsigned errorcount;";
o->newline() << "struct context {";
o->indent(1);
o->newline() << "unsigned busy;";
o->newline() << "unsigned actioncount;";
o->newline() << "unsigned nesting;";
o->newline() << "union {";
o->indent(1);
// XXX: this handles only scalars!
for (unsigned i=0; i<session->probes.size(); i++)
{
o->newline() << "struct {";
derived_probe* dp = session->probes[i];
o->newline(1) << "/* local variables */";
for (unsigned j=0; j<dp->locals.size(); j++)
{
vardecl* v = dp->locals[j];
o->newline() << c_typename (v->type) << " "
<< c_varname (v->name) << ";";
}
o->newline(-1) << "} probe_" << i << ";";
}
for (unsigned i=0; i<session->functions.size(); i++)
{
o->newline() << "struct {";
functiondecl* fd = session->functions[i];
o->newline(1) << "/* local variables */";
for (unsigned j=0; j<fd->locals.size(); j++)
{
vardecl* v = fd->locals[j];
o->newline() << c_typename (v->type) << " "
<< c_varname (v->name) << ";";
}
o->newline() << "/* formal arguments */";
for (unsigned j=0; j<fd->formal_args.size(); j++)
{
vardecl* v = fd->formal_args[j];
o->newline() << c_typename (v->type) << " "
<< c_varname (v->name) << ";";
}
if (fd->type == pe_unknown)
o->newline() << "/* no return value */";
else
{
o->newline() << "/* return value */";
o->newline() << c_typename (fd->type) << " retvalue;";
}
o->newline(-1) << "} function_" << c_varname (fd->name) << ";";
}
// XXX: must allocate context temporary pool
o->newline(-1) << "} locals [MAXNESTING];";
o->newline(-1) << "} contexts [MAXCONCURRENCY];" << endl;
}
void
test_unparser::emit_global (vardecl *v)
{
o->newline() << "static "
<< c_typename (v->type)
<< " "
<< "global_" << c_varname (v->name)
<< ";";
/* XXX
o->line() << "static DEFINE_RWLOCK("
<< c_varname (v->name) << "_lock"
<< ");";
*/
}
void
test_unparser::emit_functionsig (functiondecl* v)
{
o->newline() << "static void function_" << v->name
<< " (struct context *c);";
}
void
test_unparser::emit_module_init ()
{
o->newline() << "static int STARTUP () {";
o->newline(1) << "int anyrc = 0;";
o->newline() << "int rc;";
// XXX: initialize globals
for (unsigned i=0; i<session->probes.size(); i++)
{
session->probes[i]->emit_registrations (o, i);
o->newline() << "anyrc |= rc;";
o->newline() << "if (rc) {";
o->indent(1);
if (i > 0)
for (unsigned j=i; j>0; j--)
session->probes[j-1]->emit_deregistrations (o, j-1);
// XXX: ignore rc
o->newline() << "goto out;";
o->newline(-1) << "}";
}
o->newline(-1) << "out:";
o->indent(1);
o->newline() << "return anyrc; /* if (anyrc) log badness */";
o->newline(-1) << "}" << endl;
}
void
test_unparser::emit_module_exit ()
{
o->newline() << "static int SHUTDOWN () {";
o->newline(1) << "int anyrc = 0;";
o->newline() << "int rc;";
for (unsigned i=0; i<session->probes.size(); i++)
{
session->probes[i]->emit_deregistrations (o, i);
o->newline() << "anyrc |= rc;";
}
// XXX: uninitialize globals
o->newline() << "return anyrc; /* if (anyrc) log badness */";
o->newline(-1) << "}" << endl;
}
void
test_unparser::emit_function (functiondecl* v)
{
o->newline() << "static void function_" << c_varname (v->name)
<< " (struct context* c) {";
o->indent(1);
this->current_probe = 0;
this->current_probenum = 0;
this->current_function = v;
v->body->visit (this);
this->current_function = 0;
o->newline(-1) << "out:";
o->newline(1) << ";";
o->newline(-1) << "}" << endl;
}
void
test_unparser::emit_probe (derived_probe* v, unsigned i)
{
o->newline() << "static void probe_" << i << " (struct context *c) {";
o->indent(1);
o->newline() << "/* initialize locals */";
for (unsigned j=0; j<v->locals.size(); j++)
{
if (v->locals[j]->index_types.size() > 0) // array?
throw semantic_error ("not yet implemented", v->tok);
else if (v->locals[j]->type == pe_long)
o->newline() << "c->locals[c->nesting]"
<< ".probe_" << i
<< "." << c_varname (v->locals[j]->name)
<< " = 0;";
else if (v->locals[j]->type == pe_string)
o->newline() << "c->locals[c->nesting]"
<< ".probe_" << i
<< "." << c_varname (v->locals[j]->name)
<< "[0] = '\\0';";
else
throw semantic_error ("unsupported local variable type",
v->locals[j]->tok);
}
this->current_function = 0;
this->current_probe = v;
this->current_probenum = i;
v->body->visit (this);
this->current_probe = 0;
this->current_probenum = 0; // not essential
o->newline(-1) << "out:";
o->newline(1) << ";";
// XXX: uninitialize locals
o->newline(-1) << "}" << endl;
v->emit_probe_entries (o, i);
}
string
test_unparser::c_typename (exp_type e)
{
switch (e)
{
case pe_long: return string("long");
case pe_string: return string("string_t");
case pe_stats: return string("stats_t");
case pe_unknown:
default:
throw semantic_error ("cannot expand unknown type");
}
}
string
test_unparser::c_varname (const string& e)
{
// XXX: safeify, uniquefy, given name
return e;
}
void
test_unparser::visit_block (block *s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
o->newline() << "{";
o->indent (1);
for (unsigned i=0; i<s->statements.size(); i++)
{
try
{
/* XXX: don't define an individual statement as an action
o->newline() << "c->actioncount ++;";
o->newline() << "if (c->actioncount > MAXACTION)";
o->newline(1) << "errorcount ++;";
o->indent(-1);
*/
o->newline() << "if (errorcount)";
o->newline(1) << "goto out;" << endl;
o->indent(-1);
s->statements[i]->visit (this);
o->newline();
}
catch (const semantic_error& e)
{
session->print_error (e);
}
}
o->newline(-1) << "}";
}
void
test_unparser::visit_null_statement (null_statement *s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
o->newline() << "/* null */;";
}
void
test_unparser::visit_expr_statement (expr_statement *s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
o->newline() << "(void) ";
s->value->visit (this);
o->line() << ";";
}
void
test_unparser::visit_if_statement (if_statement *s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
o->newline() << "if (";
o->indent (1);
s->condition->visit (this);
o->indent (-1);
o->line() << ") {";
o->indent (1);
s->thenblock->visit (this);
o->newline(-1) << "}";
if (s->elseblock)
{
o->newline() << "else {";
o->indent (1);
s->thenblock->visit (this);
o->newline(-1) << "}";
}
}
void
test_unparser::visit_for_loop (for_loop *s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
throw semantic_error ("not yet implemented", s->tok);
}
void
test_unparser::visit_return_statement (return_statement* s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
if (current_function == 0)
throw semantic_error ("cannot return from non-function", s->tok);
if (s->value->type != current_function->type)
throw semantic_error ("return type mismatch", current_function->tok,
"vs", s->tok);
o->newline() << "/* " << *s->tok << " */";
if (s->value->type == pe_long)
{
o->newline() << "c->locals[c->nesting]"
<< ".function_" << c_varname (current_function->name)
<< ".retvalue = ";
s->value->visit (this);
o->line() << ";";
}
else if (s->value->type == pe_string)
{
o->newline() << "strncpy (c->locals[c->nesting]"
<< ".function_" << c_varname (current_function->name)
<< ".retvalue = ";
s->value->visit (this);
o->line() << ", MAXSTRINGLEN);";
o->line() << ";";
}
else
throw semantic_error ("return type unsupported", s->tok);
o->newline() << "goto out;";
}
void
test_unparser::visit_delete_statement (delete_statement* s)
{
const token* t = s->tok;
o->newline() << "# " << t->location.line
<< " \"" << t->location.file << "\" " << endl;
throw semantic_error ("not yet implemented", s->tok);
}
void
test_unparser::visit_literal_string (literal_string* e)
{
o->line() << '"' << e->value << '"'; // XXX: escape special chars
}
void
test_unparser::visit_literal_number (literal_number* e)
{
o->line() << e->value;
}
void
test_unparser::visit_binary_expression (binary_expression* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_unary_expression (unary_expression* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_pre_crement (pre_crement* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_post_crement (post_crement* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_logical_or_expr (logical_or_expr* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_logical_and_expr (logical_and_expr* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_array_in (array_in* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_comparison (comparison* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_concatenation (concatenation* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_exponentiation (exponentiation* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_ternary_expression (ternary_expression* e)
{
throw semantic_error ("not yet implemented", e->tok);
}
struct test_unparser_assignment: public throwing_visitor
{
test_unparser* parent;
string op;
expression* rvalue;
test_unparser_assignment (test_unparser* p, const string& o, expression* e):
parent (p), op (o), rvalue (e) {}
// only symbols and arrayindex nodes are lvalues
void visit_symbol (symbol *e);
void visit_arrayindex (arrayindex *e);
};
void
test_unparser::visit_assignment (assignment* e)
{
if (e->type != e->left->type)
throw semantic_error ("type mismatch", e->tok,
"vs", e->left->tok);
if (e->right->type != e->left->type)
throw semantic_error ("type mismatch", e->right->tok,
"vs", e->left->tok);
if (e->op == "=" || e->op == "<<")
{
test_unparser_assignment* tav =
new test_unparser_assignment (this, e->op, e->right);
e->left->visit (tav);
}
else
throw semantic_error ("not yet implemented ", e->tok);
}
void
test_unparser::visit_symbol (symbol* e)
{
vardecl* r = e->referent;
if (r->index_types.size() != 0)
throw semantic_error ("invalid reference to array", e->tok);
// XXX: handle special macro symbols
// maybe the variable is a local
if (current_probe)
{
for (unsigned i=0; i<current_probe->locals.size(); i++)
{
vardecl* rr = current_probe->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
o->line() << "c->locals[c->nesting]"
<< ".probe_" << current_probenum
<< "." << c_varname (r->name);
return;
}
}
}
else if (current_function)
{
for (unsigned i=0; i<current_function->locals.size(); i++)
{
vardecl* rr = current_function->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
o->line() << "c->locals[c->nesting]"
<< ".function_"
<< c_varname (current_function->name)
<< "." << c_varname (r->name);
return;
}
}
}
// it better be a global
for (unsigned i=0; i<session->globals.size(); i++)
{
if (session->globals[i] == r)
{
// XXX: acquire read lock on global; copy value
// into local temporary
o->line() << "global_" << c_varname (r->name);
return;
}
}
throw semantic_error ("unresolved symbol", e->tok);
}
void
test_unparser_assignment::visit_symbol (symbol *e)
{
vardecl* r = e->referent;
translator_output* o = parent->o;
functiondecl* current_function = parent->current_function;
derived_probe* current_probe = parent->current_probe;
unsigned current_probenum = parent->current_probenum;
systemtap_session* session = parent->session;
if (op != "=")
throw semantic_error ("not yet implemented", e->tok);
if (r->index_types.size() != 0)
throw semantic_error ("invalid reference to array", e->tok);
// XXX: handle special macro symbols
unsigned tmpidx = ++ tmpidxgen;
// NB: because assignments are nestable expressions, we have
// to emit C constructs that are nestable expressions too.
// ... (A = B) ... ==>
// ... ({ tmp = B; store(A,tmp); tmp; }) ...
o->line() << "({ ";
o->indent(1);
// XXX: muse use context temporary pool instead
o->newline() << parent->c_typename (e->type)
<< " tmp" << tmpidx << ";";
if (e->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "tmp" << tmpidx;
o->line() << (e->type == pe_long ? " = " : ", ");
rvalue->visit (parent);
if (e->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
// XXX: strings may be passed safely via a char*, without
// a full copy in tmpNNN
// maybe the variable is a local
if (current_probe)
{
for (unsigned i=0; i<current_probe->locals.size(); i++)
{
vardecl* rr = current_probe->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
if (e->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "c->locals[c->nesting]"
<< ".probe_" << current_probenum
<< "." << parent->c_varname (r->name);
o->line() << (e->type == pe_long ? " = " : ", ")
<< "tmp" << tmpidx;
if (e->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
o->newline() << "tmp" << tmpidx << ";";
o->newline(-1) << "})";
return;
}
}
}
else if (current_function)
{
for (unsigned i=0; i<current_function->locals.size(); i++)
{
vardecl* rr = current_function->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
if (e->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "c->locals[c->nesting]"
<< ".function_"
<< parent->c_varname (current_function->name)
<< "." << parent->c_varname (r->name);
o->line() << (e->type == pe_long ? " = " : ", ")
<< "tmp" << tmpidx;
if (e->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
o->newline() << "tmp" << tmpidx << ";";
o->newline(-1) << "})";
return;
}
}
}
// it better be a global
for (unsigned i=0; i<session->globals.size(); i++)
{
if (session->globals[i] == r)
{
// XXX: acquire write lock on global
if (e->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "global_" << parent->c_varname (r->name);
o->line() << (e->type == pe_long ? " = " : ", ")
<< "tmp" << tmpidx;
if (e->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
o->newline() << "tmp" << tmpidx << ";";
o->newline(-1) << "})";
return;
}
}
throw semantic_error ("unresolved symbol", e->tok);
}
void
test_unparser::visit_arrayindex (arrayindex* e)
{
vardecl* r = e->referent;
if (r->index_types.size() == 0 ||
r->index_types.size() != e->indexes.size())
throw semantic_error ("invalid array reference", e->tok);
o->line() << "({";
o->indent(1);
o->newline() << "c->actioncount ++;";
o->newline() << "if (c->actioncount > MAXACTION)";
o->newline(1) << "errorcount ++;";
o->indent(-1);
o->newline() << "if (errorcount)";
o->newline(1) << "goto out;";
o->indent(-1);
// NB: because these expressions are nestable, emit this construct
// thusly:
// ({ tmp0=(idx0); ... tmpN=(idxN);
// fetch (array,idx0...N, &tmpresult);
// tmpresult; })
//
// we store all indices in temporary variables to avoid nasty
// reentrancy issues that pop up with nested expressions:
// e.g. a[a[c]=5] could deadlock
vector<unsigned> idxtmps;
for (unsigned i=0; i<r->index_types.size(); i++)
{
if (r->index_types[i] != e->indexes[i]->type)
throw semantic_error ("array index type mismatch", e->indexes[i]->tok);
unsigned tmpidx = ++ tmpidxgen;
idxtmps.push_back (tmpidx);
// XXX: muse use context temporary pool instead
o->newline() << c_typename (r->index_types[i]) << " tmp" << tmpidx << ";";
if (e->indexes[i]->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "tmp" << tmpidx;
o->line() << (e->indexes[i]->type == pe_long ? " = " : ", ");
e->indexes[i]->visit (this);
if (e->indexes[i]->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
}
o->newline() << "if (errorcount)";
o->newline(1) << "goto out;";
o->indent(-1);
o->newline() << c_typename (r->type) << " result;";
#if 0
// maybe the variable is a local
if (current_probe)
{
for (unsigned i=0; i<current_probe->locals.size(); i++)
{
vardecl* rr = current_probe->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
o->line() << "c->locals[c->nesting]"
<< ".probe_" << current_probenum
<< "." << c_varname (r->name);
return;
}
}
}
else if (current_function)
{
for (unsigned i=0; i<current_function->locals.size(); i++)
{
vardecl* rr = current_function->locals[i];
if (rr == r) // comparison of pointers is sufficient
{
o->line() << "c->locals[c->nesting]"
<< ".function_"
<< c_varname (current_function->name)
<< "." << c_varname (r->name);
return;
}
}
}
// it better be a global
for (unsigned i=0; i<session->globals.size(); i++)
{
if (session->globals[i] == r)
{
// XXX: acquire read lock on global; copy value
// into local temporary
o->line() << "global_" << c_varname (r->name);
return;
}
}
throw semantic_error ("unresolved symbol", e->tok);
#endif
o->newline() << "result;";
o->newline(-1) << "})";
}
void
test_unparser_assignment::visit_arrayindex (arrayindex *e)
{
throw semantic_error ("not yet implemented", e->tok);
}
void
test_unparser::visit_functioncall (functioncall* e)
{
functiondecl* r = e->referent;
if (r->formal_args.size() != e->args.size())
throw semantic_error ("invalid length argument list", e->tok);
o->line() << "({";
o->indent(1);
// NB: we store all actual arguments in temporary variables,
// to avoid colliding sharing of context variables with
// nested function calls: f(f(f(1)))
o->newline() << "/* compute actual arguments */";
for (unsigned i=0; i<e->args.size(); i++)
{
if (r->formal_args[i]->type != e->args[i]->type)
throw semantic_error ("function argument type mismatch",
e->args[i]->tok, "vs", r->formal_args[i]->tok);
// XXX: muse use context temporary pool instead
o->newline() << c_typename (r->formal_args[i]->type)
<< " tmp_" << r->formal_args[i]->name << ";";
if (e->args[i]->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "tmp_" << c_varname (r->formal_args[i]->name);
o->line() << (e->args[i]->type == pe_long ? " = " : ", ");
e->args[i]->visit (this);
if (e->args[i]->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
}
o->newline() << "if (c->nesting+2 >= MAXNESTING)";
o->newline(1) << "errorcount ++;";
o->newline(-1) << "c->actioncount ++;";
o->newline() << "if (c->actioncount > MAXACTION)";
o->newline(1) << "errorcount ++;";
o->newline(-1) << "if (errorcount)";
o->newline(1) << "goto out;";
o->indent(-1);
o->newline() << "/* initialize locals */";
for (unsigned i=0; i<r->locals.size(); i++)
{
if (r->locals[i]->index_types.size() > 0) // array?
throw semantic_error ("not yet implemented", r->tok);
else if (r->locals[i]->type == pe_long)
o->newline() << "c->locals[c->nesting+1]"
<< ".function_" << c_varname (r->name)
<< "." << c_varname (r->locals[i]->name)
<< " = 0;";
else if (r->locals[i]->type == pe_string)
o->newline() << "c->locals[c->nesting+1]"
<< ".function_" << c_varname (r->name)
<< "." << c_varname (r->locals[i]->name)
<< "[0] = '\\0';";
else
throw semantic_error ("unsupported local variable type",
r->locals[i]->tok);
}
o->newline() << "if (errorcount)";
o->newline(1) << "goto out;";
o->indent(-1);
o->newline() << "/* copy in stored actual arguments */";
for (unsigned i=0; i<e->args.size(); i++)
{
if (r->formal_args[i]->type != e->args[i]->type)
throw semantic_error ("function argument type mismatch",
e->args[i]->tok, "vs", r->formal_args[i]->tok);
if (e->args[i]->type == pe_string)
o->newline() << "strncpy (";
else
o->newline();
o->line() << "c->locals[c->nesting+1]"
<< ".function_" << c_varname (r->name)
<< "." << c_varname (r->formal_args[i]->name);
o->line() << (e->args[i]->type == pe_long ? " = " : ", ");
o->line() << "tmp_" << c_varname (r->formal_args[i]->name);
if (e->args[i]->type == pe_string)
o->line() << ", MAXSTRINGLEN)";
o->line() << ";";
}
// call function
o->newline() << "c->nesting ++;";
o->newline() << "function_" << c_varname (r->name) << " (c);";
o->newline() << "c->nesting --;";
// XXX: clean up any locals that need it (arrays)
// return result from retvalue slot
o->newline() << "c->locals[c->nesting+1]"
<< ".function_" << c_varname (r->name)
<< ".retvalue;";
o->newline(-1) << "})";
}
int
translate_pass (systemtap_session& s)
{
int rc = 0;
s.up = new test_unparser (& s);
try
{
s.op->newline() << "/* common header */";
s.up->emit_common_header ();
s.op->newline() << "/* globals */";
for (unsigned i=0; i<s.globals.size(); i++)
s.up->emit_global (s.globals[i]);
s.op->newline() << "/* function signatures */";
for (unsigned i=0; i<s.functions.size(); i++)
s.up->emit_functionsig (s.functions[i]);
s.op->newline() << "/* functions */";
for (unsigned i=0; i<s.functions.size(); i++)
s.up->emit_function (s.functions[i]);
s.op->newline() << "/* probes */";
for (unsigned i=0; i<s.probes.size(); i++)
s.up->emit_probe (s.probes[i], i);
s.op->newline() << "/* module init */";
s.up->emit_module_init ();
s.op->newline() << "/* module exit */";
s.up->emit_module_exit ();
s.op->newline();
}
catch (const semantic_error& e)
{
s.print_error (e);
}
delete s.up;
return rc + s.num_errors;
}
|