summaryrefslogtreecommitdiffstats
path: root/translate.cxx
blob: 52d370f8156ac7f77e045394ab3a126369479539 (plain)
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
// translation pass
// Copyright (C) 2005 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "config.h"
#include "staptree.h"
#include "elaborate.h"
#include "translate.h"
#include <iostream>
#include <set>
#include <sstream>
#include <string>

using namespace std;



// little utility function

template <typename T>
static string
stringify(T t)
{
  ostringstream s;
  s << t;
  return s.str ();
}


struct c_unparser: public unparser, public visitor
{
  systemtap_session* session;
  translator_output* o;

  derived_probe* current_probe;
  unsigned current_probenum;
  functiondecl* current_function;
  unsigned tmpvar_counter;
  unsigned label_counter;

  c_unparser (systemtap_session *ss):
    session (ss), o (ss->op), current_probe(0), current_function (0),
  tmpvar_counter (0), label_counter (0) {}
  ~c_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);

  // for use by looping constructs
  vector<string> loop_break_labels;
  vector<string> loop_continue_labels;

  string c_typename (exp_type e);
  string c_varname (const string& e);
  void c_assign (const string& lvalue, expression* rvalue, const string& msg);
  void c_assign (const string& lvalue, const string& rvalue, exp_type type,
                 const string& msg, const token* tok);

  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_foreach_loop (foreach_loop* s);
  void visit_return_statement (return_statement* s);
  void visit_delete_statement (delete_statement* s);
  void visit_next_statement (next_statement* s);
  void visit_break_statement (break_statement* s);
  void visit_continue_statement (continue_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_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);
};


// ------------------------------------------------------------------------


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;
}




// A shadow visitor, meant to generate temporary variable declarations
// for function or probe bodies.  Member functions should exactly match
// the corresponding c_unparser logic and traversal sequence,
// to ensure interlocking naming and declaration of temp variables.
struct c_tmpcounter: public traversing_visitor
{
  c_unparser* parent;
  unsigned tmpvar_counter;
  c_tmpcounter (c_unparser* p): parent (p), tmpvar_counter (0) {}

  // void visit_for_loop (for_loop* s);
  // void visit_foreach_loop (foreach_loop* s);
  // void visit_return_statement (return_statement* s);
  // void visit_delete_statement (delete_statement* s);
  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_ternary_expression (ternary_expression* e);
  void visit_assignment (assignment* e);
  void visit_arrayindex (arrayindex* e);
  void visit_functioncall (functioncall* e);
};


struct builtin_collector: public traversing_visitor
{
  set<string> called_builtins;
  void visit_functioncall (functioncall* e);
};

void
builtin_collector::visit_functioncall(functioncall* e)
{
  if (e->referent && !e->referent->body)
    called_builtins.insert(e->referent->name);
}

void
hookup_builtins(systemtap_session *session,
		translator_output *o)
{
  builtin_collector bc;
  for (unsigned i=0; i<session->functions.size(); i++)
    {
      functiondecl* fd = session->functions[i];
      if (fd->body)
	fd->body->visit(&bc);
    }
  for (unsigned i=0; i<session->probes.size(); i++)
    {
      derived_probe* dp = session->probes[i];
      dp->body->visit(&bc);
    }

  for (set<string>::const_iterator i = bc.called_builtins.begin();
       i != bc.called_builtins.end(); ++i)
    {
      o->newline() << "#define _BUILTIN_FUNCTION_" << *i << "_";
    }

  o->newline() << "#include \"builtin_functions.h\"";

  for (set<string>::const_iterator i = bc.called_builtins.begin();
       i != bc.called_builtins.end(); ++i)
    {
      o->newline() << "#undef _BUILTIN_FUNCTION_" << *i << "_";
    }
}

void
c_unparser::emit_common_header ()
{
  o->newline() << "#include <linux/string.h>";
  // XXX: tapsets.cxx should be able to add additional definitions

  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++)
    {
      derived_probe* dp = session->probes[i];
      o->newline() << "struct probe_" << i << "_locals {";
      o->indent(1);
      for (unsigned j=0; j<dp->locals.size(); j++)
        {
          vardecl* v = dp->locals[j];
          o->newline() << c_typename (v->type) << " " 
		       << c_varname (v->name) << ";";
        }
      c_tmpcounter ct (this);
      dp->body->visit (& ct);
      o->newline(-1) << "} probe_" << i << ";";
    }

  for (unsigned i=0; i<session->functions.size(); i++)
    {
      functiondecl* fd = session->functions[i];
      o->newline()
        << "struct function_" << c_varname (fd->name) << "_locals {";
      o->indent(1);
      for (unsigned j=0; j<fd->locals.size(); j++)
        {
          vardecl* v = fd->locals[j];
          o->newline() << c_typename (v->type) << " " 
  		       << c_varname (v->name) << ";";
        }
      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) << ";";
        }
      c_tmpcounter ct (this);
      if (fd->body)
	fd->body->visit (& ct);
      if (fd->type == pe_unknown)
	o->newline() << "/* no return value */";
      else
	{
	  o->newline() << c_typename (fd->type) << " __retvalue;";
	}
      o->newline(-1) << "} function_" << c_varname (fd->name) << ";";
    }
  o->newline(-1) << "} locals [MAXNESTING];";
  o->newline(-1) << "} contexts [MAXCONCURRENCY];" << endl;
  hookup_builtins(session, o);
}


void
c_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
c_unparser::emit_functionsig (functiondecl* v)
{
  o->newline() << "static void function_" << v->name
	       << " (struct context *c);";
}


void
c_unparser::emit_module_init ()
{
  o->newline() << "static int systemtap_module_init (void);";
  o->newline() << "int systemtap_module_init () {";
  o->newline(1) << "int anyrc = 0;";
  o->newline() << "int rc;";

  // XXX: yuck runtime
  o->newline() << "TRANSPORT_OPEN;";

  for (unsigned i=0; i<session->globals.size(); i++)
    {
      vardecl* v = session->globals[i];
      if (v->index_types.size() > 0) // array?
	throw semantic_error ("array init not yet implemented", v->tok);
      else if (v->type == pe_long)
	o->newline() << "global_" << c_varname (v->name)
		     << " = 0;";
      else if (v->type == pe_string)
	o->newline() << "global_" << c_varname (v->name)
		     << "[0] = '\\0';";
      else
	throw semantic_error ("unsupported global variable type",
			      v->tok);
    }

  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() << "out:";
  o->newline() << "return anyrc; /* if (anyrc) log badness */";
  o->newline(-1) << "}" << endl;
}


void
c_unparser::emit_module_exit ()
{
  // XXX: double-yuck runtime
  o->newline() << "void probe_exit () {";
  // need to reference these static functions for -Werror avoidance
  o->newline(1) << "if (0) next_fmt ((void *) 0, (void *) 0);";
  o->newline() << "if (0) _stp_dbug(\"\", 0, \"\");";
  o->newline(-1) << "}";
  //
  o->newline() << "static void systemtap_module_exit (void);";
  o->newline() << "void systemtap_module_exit () {";
  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() << "_stp_transport_close ();";
  // XXX: if anyrc, log badness
  o->newline(-1) << "}" << endl;
}


void
c_unparser::emit_function (functiondecl* v)
{
  o->newline() << "void function_" << c_varname (v->name)
            << " (struct context* c) {";
  o->indent(1);
  this->current_probe = 0;
  this->current_probenum = 0;
  this->current_function = v;
  this->tmpvar_counter = 0;

  o->newline()
    << "struct function_" << c_varname (v->name) << "_locals * "
    << " __restrict__ l =";
  o->newline(1)
    << "& c->locals[c->nesting].function_" << c_varname (v->name)
    << ";";
  o->newline(-1) << "(void) l;"; // make sure "l" is marked used

  // initialize locals
  for (unsigned i=0; i<v->locals.size(); i++)
    {
      if (v->locals[i]->index_types.size() > 0) // array?
	throw semantic_error ("array locals not supported", v->tok);
      else if (v->locals[i]->type == pe_long)
	o->newline() << "l->" << c_varname (v->locals[i]->name)
		     << " = 0;";
      else if (v->locals[i]->type == pe_string)
	o->newline() << "l->" << c_varname (v->locals[i]->name)
		     << "[0] = '\\0';";
      else
	throw semantic_error ("unsupported local variable type",
			      v->locals[i]->tok);
    }

  // initialize return value, if any
  if (v->type == pe_long)
    o->newline() << "l->__retvalue = 0;";
  else if (v->type == pe_string)
    o->newline() << "l->__retvalue[0] = '\\0';";

  v->body->visit (this);
  this->current_function = 0;

  o->newline(-1) << "out:";
  o->newline(1) << ";";

  o->newline(-1) << "}" << endl;
}


void
c_unparser::emit_probe (derived_probe* v, unsigned i)
{
  o->newline() << "static void probe_" << i << " (struct context *c);";
  o->newline() << "void probe_" << i << " (struct context *c) {";
  o->indent(1);

  // initialize frame pointer
  o->newline() << "struct probe_" << i << "_locals * __restrict__ l =";
  o->newline(1) << "& c->locals[c->nesting].probe_" << i << ";";
  o->newline(-1) << "(void) l;"; // make sure "l" is marked used

  // initialize locals
  for (unsigned j=0; j<v->locals.size(); j++)
    {
      if (v->locals[j]->index_types.size() > 0) // array?
	throw semantic_error ("array locals not supported", v->tok);
      else if (v->locals[j]->type == pe_long)
	o->newline() << "l->" << c_varname (v->locals[j]->name)
		     << " = 0;";
      else if (v->locals[j]->type == pe_string)
	o->newline() << "l->" << 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;
  this->tmpvar_counter = 0;
  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
c_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
c_unparser::c_varname (const string& e)
{
  // XXX: safeify, uniquefy, given name
  return e;
}


void
c_unparser::c_assign (const string& lvalue, expression* rvalue,
                      const string& msg)
{
  if (rvalue->type == pe_long)
    {
      o->newline() << lvalue << " = ";
      rvalue->visit (this);
      o->line() << ";";
    }
  else if (rvalue->type == pe_string)
    {
      o->newline() << "strncpy (" << lvalue << ", ";
      rvalue->visit (this);
      o->line() << ", MAXSTRINGLEN);";
    }
  else
    {
      string fullmsg = msg + " type unsupported";
      throw semantic_error (fullmsg, rvalue->tok);
    }
}


void
c_unparser::c_assign (const string& lvalue, const string& rvalue,
                      exp_type type, const string& msg, const token* tok)
{
  if (type == pe_long)
    {
      o->newline() << lvalue << " = " << rvalue << ";";
    }
  else if (type == pe_string)
    {
      o->newline() << "strncpy (" << lvalue << ", "
                   << rvalue << ", MAXSTRINGLEN);";
    }
  else
    {
      string fullmsg = msg + " type unsupported";
      throw semantic_error (fullmsg, tok);
    }
}



void
c_unparser::visit_block (block *s)
{
  o->newline() << "{";
  o->indent (1);
  o->newline() << "c->actioncount += " << s->statements.size() << ";";
  o->newline() << "if (c->actioncount > MAXACTION) errorcount ++;";

  for (unsigned i=0; i<s->statements.size(); i++)
    {
      try
        {
          // XXX: it's probably not necessary to check this so frequently
	  o->newline() << "if (errorcount) goto out;";
          s->statements[i]->visit (this);
	  o->newline();
        }
      catch (const semantic_error& e)
        {
          session->print_error (e);
        }
    }
  o->newline(-1) << "}";
}


void
c_unparser::visit_null_statement (null_statement *s)
{
  o->newline() << "/* null */;";
}


void
c_unparser::visit_expr_statement (expr_statement *s)
{
  o->newline() << "(void) ";
  s->value->visit (this);
  o->line() << ";";
}


void
c_unparser::visit_if_statement (if_statement *s)
{
  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
c_unparser::visit_for_loop (for_loop *s)
{
  s->init->visit (this);
  string ctr = stringify (label_counter++);
  string contlabel = "continue_" + ctr;
  string breaklabel = "break_" + ctr;

  o->newline() << contlabel << ":";

  o->newline() << "c->actioncount ++;";
  o->newline() << "if (c->actioncount > MAXACTION) errorcount ++;";
  o->newline() << "if (errorcount) goto out;";

  o->newline() << "if (! (";
  if (s->cond->type != pe_long)
    throw semantic_error ("expected numeric type", s->cond->tok);
  s->cond->visit (this);
  o->line() << ")) goto " << breaklabel << ";";

  loop_break_labels.push_back (breaklabel);
  loop_continue_labels.push_back (contlabel);
  s->block->visit (this);
  loop_break_labels.pop_back ();
  loop_continue_labels.pop_back ();

  s->incr->visit (this);
  o->newline() << "goto " << contlabel << ";";

  o->newline() << breaklabel << ": ; /* dummy statement */";
}


void
c_unparser::visit_foreach_loop (foreach_loop *s)
{
  throw semantic_error ("foreach loop not yet implemented", s->tok);
}


void
c_unparser::visit_return_statement (return_statement* s)
{
  if (current_function == 0)
    throw semantic_error ("cannot 'return' from probe", s->tok);

  if (s->value->type != current_function->type)
    throw semantic_error ("return type mismatch", current_function->tok,
                         "vs", s->tok);

  c_assign ("l->__retvalue", s->value, "return value");
  o->newline() << "goto out;";
}


void
c_unparser::visit_next_statement (next_statement* s)
{
  if (current_probe == 0)
    throw semantic_error ("cannot 'next' from function", s->tok);

  o->newline() << "goto out;";
}


void
c_unparser::visit_delete_statement (delete_statement* s)
{
  throw semantic_error ("delete statement not yet implemented", s->tok);
}


void
c_unparser::visit_break_statement (break_statement* s)
{
  if (loop_break_labels.size() == 0)
    throw semantic_error ("cannot 'break' outside loop", s->tok);

  string label = loop_break_labels[loop_break_labels.size()-1];
  o->newline() << "goto " << label << ";";
}


void
c_unparser::visit_continue_statement (continue_statement* s)
{
  if (loop_continue_labels.size() == 0)
    throw semantic_error ("cannot 'continue' outside loop", s->tok);

  string label = loop_continue_labels[loop_continue_labels.size()-1];
  o->newline() << "goto " << label << ";";
}



void
c_unparser::visit_literal_string (literal_string* e)
{
  o->line() << '"' << e->value << '"'; // XXX: escape special chars
}


void
c_unparser::visit_literal_number (literal_number* e)
{
  o->line() << e->value;
}



void
c_tmpcounter::visit_binary_expression (binary_expression* e)
{
  if (e->op == "/" || e->op == "%")
    {
      parent->o->newline()
        << parent->c_typename (pe_long)
        << " __tmp" << tmpvar_counter ++ << ";";
      parent->o->newline()
        << parent->c_typename (pe_long)
        << " __tmp" << tmpvar_counter ++ << ";";
    }

  e->left->visit (this);
  e->right->visit (this);
}


void
c_unparser::visit_binary_expression (binary_expression* e)
{
  if (e->type != pe_long ||
      e->left->type != pe_long ||
      e->right->type != pe_long)
    throw semantic_error ("expected numeric types", e->tok);
  
  if (e->op == "+" ||
      e->op == "-" ||
      e->op == "*" ||
      e->op == "&" ||
      e->op == "|" ||
      e->op == "^" ||
      e->op == "<<" ||
      e->op == ">>")
    {
      o->line() << "((";
      e->left->visit (this);
      o->line() << ") " << e->op << " (";
      e->right->visit (this);
      o->line() << "))";
    }
  else if (e->op == "/" ||
           e->op == "%")
    {
      // % and / need a division-by-zero check; and thus two temporaries
      // for proper evaluation order
      unsigned tmpidx1 = tmpvar_counter++;
      unsigned tmpidx2 = tmpvar_counter++;
      string tmp1 = "l->__tmp" + stringify (tmpidx1);
      string tmp2 = "l->__tmp" + stringify (tmpidx2);
      o->line() << "({";

      o->newline(1) << tmp1 << " = ";
      e->left->visit (this);
      o->line() << ";";

      o->newline() << tmp2 << " = ";
      e->right->visit (this);
      o->line() << ";";

      o->newline() << "if (" << tmp2 << " == 0) {";
      o->newline(1) << "errorcount ++;";
      o->newline() << tmp2 << " = 1;";
      o->newline(-1) << "}";

      o->newline() << tmp1 << " " << e->op << " " << tmp2 << ";";
      o->newline(-1) << "})";
    }
  else
    throw semantic_error ("operator not yet implemented", e->tok); 
}


void
c_unparser::visit_unary_expression (unary_expression* e)
{
  if (e->type != pe_long ||
      e->operand->type != pe_long)
    throw semantic_error ("expected numeric types", e->tok);

  o->line() << "(" << e->op << " (";
  e->operand->visit (this);
  o->line() << "))";
}

void
c_unparser::visit_logical_or_expr (logical_or_expr* e)
{
  if (e->type != pe_long ||
      e->left->type != pe_long ||
      e->right->type != pe_long)
    throw semantic_error ("expected numeric types", e->tok);

  o->line() << "((";
  e->left->visit (this);
  o->line() << ") " << e->op << " (";
  e->right->visit (this);
  o->line() << "))";
}


void
c_unparser::visit_logical_and_expr (logical_and_expr* e)
{
  if (e->type != pe_long ||
      e->left->type != pe_long ||
      e->right->type != pe_long)
    throw semantic_error ("expected numeric types", e->tok);

  o->line() << "((";
  e->left->visit (this);
  o->line() << ") " << e->op << " (";
  e->right->visit (this);
  o->line() << "))";
}


void
c_unparser::visit_array_in (array_in* e)
{
  throw semantic_error ("array-in expression not yet implemented", e->tok);
}


void
c_unparser::visit_comparison (comparison* e)
{
  o->line() << "(";

  if (e->left->type == pe_string)
    {
      if (e->left->type != pe_string ||
          e->right->type != pe_string)
        throw semantic_error ("expected string types", e->tok);

      o->line() << "strncmp (";
      e->left->visit (this);
      o->line() << ", ";
      e->right->visit (this);
      o->line() << ", MAXSTRINGLEN";
      o->line() << ") " << e->op << " 0";
    }
  else if (e->left->type == pe_long)
    {
      if (e->left->type != pe_long ||
          e->right->type != pe_long)
        throw semantic_error ("expected numeric types", e->tok);

      o->line() << "((";
      e->left->visit (this);
      o->line() << ") " << e->op << " (";
      e->right->visit (this);
      o->line() << "))";
    }
  else
    throw semantic_error ("unexpected type", e->left->tok);

  o->line() << ")";
}


void
c_tmpcounter::visit_concatenation (concatenation* e)
{
  parent->o->newline() << parent->c_typename (e->type)
                       << " __tmp" << tmpvar_counter ++ << ";";
  e->left->visit (this);
  e->right->visit (this);
}


void
c_unparser::visit_concatenation (concatenation* e)
{
  if (e->op != ".")
    throw semantic_error ("unexpected concatenation operator", e->tok);

  if (e->type != pe_string ||
      e->left->type != pe_string ||
      e->right->type != pe_string)
    throw semantic_error ("expected string types", e->tok);

  string tmpvar = "l->__tmp" + stringify (tmpvar_counter ++);
  
  o->line() << "({ ";
  o->indent(1);
  c_assign (tmpvar, e->left, "assignment");
  o->newline() << "strncat (" << tmpvar << ", ";
  e->right->visit (this);
  o->line() << ", MAXSTRINGLEN);";
  o->newline() << tmpvar << ";";
  o->newline(-1) << "})";
}


void
c_unparser::visit_ternary_expression (ternary_expression* e)
{
  if (e->cond->type != pe_long)
    throw semantic_error ("expected numeric condition", e->cond->tok);

  if (e->truevalue->type != e->falsevalue->type ||
      e->type != e->truevalue->type ||
      (e->truevalue->type != pe_long && e->truevalue->type != pe_string))
    throw semantic_error ("expected matching types", e->tok);

  o->line() << "((";
  e->cond->visit (this);
  o->line() << ") ? (";
  e->truevalue->visit (this);
  o->line() << ") : (";
  e->falsevalue->visit (this);
  o->line() << "))";
}


struct c_unparser_assignment: public throwing_visitor
{
  c_unparser* parent;
  string op;
  expression* rvalue;
  bool pre;
  c_unparser_assignment (c_unparser* p, const string& o, expression* e):
    throwing_visitor ("invalid lvalue type"),
    parent (p), op (o), rvalue (e), pre (false) {}
  c_unparser_assignment (c_unparser* p, const string& o, bool pp):
    throwing_visitor ("invalid lvalue type"),
    parent (p), op (o), rvalue (0), pre (pp) {}

  // only symbols and arrayindex nodes are possible lvalues
  void visit_symbol (symbol *e);
  void visit_arrayindex (arrayindex *e);
};


struct c_tmpcounter_assignment: public traversing_visitor
// leave throwing for illegal lvalues to the c_unparser_assignment instance
{
  c_tmpcounter* parent;
  const string& op;
  expression* rvalue;
  c_tmpcounter_assignment (c_tmpcounter* p, const string& o, expression* e):
    parent (p), op (o), rvalue (e) {}

  // only symbols and arrayindex nodes are possible lvalues
  void visit_symbol (symbol *e);
  void visit_arrayindex (arrayindex *e);
};



void
c_tmpcounter::visit_assignment (assignment *e)
{
  c_tmpcounter_assignment tav (this, e->op, e->right);
  e->left->visit (& tav);
}


void
c_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);

  c_unparser_assignment tav (this, e->op, e->right);
  e->left->visit (& tav);
}


void
c_tmpcounter::visit_pre_crement (pre_crement* e)
{
  c_tmpcounter_assignment tav (this, e->op, 0);
  e->operand->visit (& tav);
}

void
c_unparser::visit_pre_crement (pre_crement* e)
{
  if (e->type != pe_long ||
      e->type != e->operand->type)
    throw semantic_error ("expected numeric type", e->tok);

  c_unparser_assignment tav (this, e->op, true);
  e->operand->visit (& tav);
}


void
c_tmpcounter::visit_post_crement (post_crement* e)
{
  c_tmpcounter_assignment tav (this, e->op, 0);
  e->operand->visit (& tav);
}

void
c_unparser::visit_post_crement (post_crement* e)
{
  if (e->type != pe_long ||
      e->type != e->operand->type)
    throw semantic_error ("expected numeric type", e->tok);

  c_unparser_assignment tav (this, e->op, false);
  e->operand->visit (& tav);
}


void
c_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() << "l->" << c_varname (r->name);
	      return;
	    }
	}
    }
  else if (current_function)
    {
      // check locals
      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() << "l->" << c_varname (r->name);
	      return;
	    }
	}

      // check formal args
      for (unsigned i=0; i<current_function->formal_args.size(); i++)
	{
	  vardecl* rr = current_function->formal_args[i];
	  if (rr == r) // comparison of pointers is sufficient
	    {
	      o->line() << "l->" << 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);
}




// Assignment expansion is tricky.
//
// Because assignments are nestable expressions, we have
// to emit C constructs that are nestable expressions too.
// We have to evaluate the given expressions the proper number of times,
// including array indices.
// We have to lock the lvalue (if global) against concurrent modification,
// especially with modify-assignment operations (+=, ++).
// We have to check the rvalue (for division-by-zero checks).

// In the normal "pre=false" case, for (A op B) emit:
// ({ tmp = B; check(B); lock(A); res = A op tmp; A = res; unlock(A); res; })
// In the "pre=true" case, emit instead:
// ({ tmp = B; check(B); lock(A); res = A; A = res op tmp; unlock(A); res; })
//
// (op is the plain operator portion of a combined calculate/assignment:
// "+" for "+=", and so on.  It is in the "macop" variable below.)
//
// For array assignments, additional temporaries are used for each
// index, which are expanded before the "tmp=B" expression, in order
// to consistently order evaluation of lhs before rhs.
//

void
c_tmpcounter_assignment::visit_symbol (symbol *e)
{
  // tmp
  parent->parent->o->newline()
    << parent->parent->c_typename (e->type)
    << " __tmp" << parent->tmpvar_counter ++ << ";";
  // res
  parent->parent->o->newline()
    << parent->parent->c_typename (e->type)
    << " __tmp" << parent->tmpvar_counter ++ << ";";

  if (rvalue)
    rvalue->visit (parent);
}

void
c_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;
  systemtap_session* session = parent->session;

  if (r->index_types.size() != 0)
    throw semantic_error ("unexpected reference to array", e->tok);

  unsigned tmpidx = parent->tmpvar_counter ++;
  unsigned residx = parent->tmpvar_counter ++;
  string tmp_base = "l->__tmp";
  string tmpvar = tmp_base + stringify (tmpidx);
  string resvar = tmp_base + stringify (residx);
  o->line() << "({ ";
  o->indent(1);

  // part 1: "tmp = B"
  if (rvalue)
    parent->c_assign (tmpvar, rvalue, "assignment");
  else
    {
      if (op == "++" || op == "--")
        o->newline() << tmpvar << " = 1;";
      else
        // internal error
        throw semantic_error ("need rvalue for assignment", e->tok);
    }
  // OPT: literal rvalues could be used without a tmp* copy

  // part 2: "check (B)"
  if (op == "/=" || op == "%=") 
    {
      // need division-by-zero check
      o->newline() << "if (" << tmpvar << " == 0) {";
      o->newline(1) << "errorcount ++;";
      o->newline() << tmpvar << " = 1;";
      o->newline(-1) << "}";
    }

  // maybe the variable is a local
  string lvaluename;
  bool lock_global = false;

  if (current_probe)
    for (unsigned i=0; i<current_probe->locals.size(); i++)
      if (current_probe->locals[i] == r)
        lvaluename = "l->" + parent->c_varname (r->name);
  if (current_function)
    {
      for (unsigned i=0; i<current_function->locals.size(); i++)
        if (current_function->locals[i] == r)
          lvaluename = "l->" + parent->c_varname (r->name);
      
      for (unsigned i=0; i<current_function->formal_args.size(); i++)
        if (current_function->formal_args[i] == r)
          lvaluename = "l->" + parent->c_varname (r->name);
    }
  if (lvaluename == "")
    for (unsigned i=0; i<session->globals.size(); i++)
      if (session->globals[i] == r)
        {
          lvaluename = "global_" + parent->c_varname (r->name);
          lock_global = true;
        }

  if (lvaluename == "")
    throw semantic_error ("unresolved assignment to ", e->tok);

  // part 3: "lock(A)"
  if (lock_global)
    o->newline() << "/* XXX lock " << lvaluename << " */";

  // part 4/5
  if (e->type == pe_string)
    {
      if (pre)
        throw semantic_error ("pre assignment on strings not supported", 
                              e->tok);
      if (op == "=")
        {
          o->newline() << "strncpy (" << lvaluename
                       << ", " << tmpvar
                       << ", MAXSTRINGLEN);";
          // no need for second copy
          resvar = tmpvar;
        }
      else if (op == ".=")
        {
          // shortcut two-step construction of concatenated string in
          // empty resvar, then copy to tmpvar: instead concatenate
          // to lvalue directly, then copy back to resvar
          o->newline() << "strncat (" << lvaluename
                       << ", " << tmpvar
                       << ", MAXSTRINGLEN);";
          o->newline() << "strncpy (" << resvar
                       << ", " << lvaluename
                       << ", MAXSTRINGLEN);";
        }
      else
        throw semantic_error ("string assignment operator " +
                              op + " unsupported", e->tok);
    }
  else if (e->type == pe_long)
    {
      // a lot of operators come through this "gate":
      // - vanilla assignment "="
      // - stats aggregation "<<<"
      // - modify-accumulate "+=" and many friends
      // - pre/post-crement "++"/"--"

      // compute the modify portion of a modify-accumulate
      string macop;
      unsigned oplen = op.size();
      if (op == "=")
        macop = "* 0 +"; // clever (?) trick to select rvalue (tmp) only
      else if (oplen > 1 && op[oplen-1] == '=') // for +=, %=, <<=, etc...
        macop = op.substr(0, oplen-1);
      else if (op == "<<<")
        throw semantic_error ("stats aggregation not yet implemented", e->tok);
      else if (op == "++")
        macop = "+";
      else if (op == "--")
        macop = "-";
      else
        // internal error
        throw semantic_error ("unknown macop for assignment", e->tok);

      // part 4
      if (pre)
        o->newline() << resvar << " = " << lvaluename << ";";
      else
        o->newline() << resvar << " = "
                     << lvaluename << " " << macop << " " << tmpvar << ";";

      // part 5
      if (pre)
        o->newline() << lvaluename << " = "
                     << resvar << " " << macop << " " << tmpvar << ";";
      else
        o->newline() << lvaluename << " = " << resvar << ";";        
    }
  else
    throw semantic_error ("assignment type not yet implemented", e->tok);

  // part 6: "unlock(A)"
  if (lock_global)
    o->newline() << "/* XXX unlock " << lvaluename << " */";

  // part 7: "res"
  o->newline() << resvar << ";";
  o->newline(-1) << "})";
}


void
c_tmpcounter::visit_arrayindex (arrayindex *e)
{
  vardecl* r = e->referent;
  // one temporary per index dimension
  for (unsigned i=0; i<r->index_types.size(); i++)
    parent->o->newline()
      << parent->c_typename (r->index_types[i])
      << " __tmp" << tmpvar_counter ++ << ";";
  // now the result
  parent->o->newline()
    << parent->c_typename (r->type)
    << " __tmp" << tmpvar_counter ++ << ";";

  for (unsigned i=0; i<e->indexes.size(); i++)
    e->indexes[i]->visit (this);
}


void
c_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);

  throw semantic_error ("array read not supported", e->tok);

  o->line() << "({";
  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


  unsigned tmpidx_base = tmpvar_counter;
  tmpvar_counter += r->index_types.size() + 1 /* result */;
  string tmp_base = "l->__tmp";
  unsigned residx = tmpidx_base + r->index_types.size();

  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 = tmpidx_base + i;

    c_assign (tmp_base + stringify (tmpidx),
              e->indexes[i], "array index copy");
    }

  o->newline() << "if (errorcount) goto out;";

  o->newline() << tmp_base << residx << ";";
  o->newline(-1) << "})";
}


void
c_tmpcounter_assignment::visit_arrayindex (arrayindex *e)
{
}

void
c_unparser_assignment::visit_arrayindex (arrayindex *e)
{
  throw semantic_error ("array write not supported", e->tok);
}


void
c_tmpcounter::visit_functioncall (functioncall *e)
{
  functiondecl* r = e->referent;
  // one temporary per argument
  for (unsigned i=0; i<r->formal_args.size(); i++)
    parent->o->newline()
      << parent->c_typename (r->formal_args[i]->type)
      << " __tmp" << tmpvar_counter ++ << ";";

  for (unsigned i=0; i<e->args.size(); i++)
    e->args[i]->visit (this);
}


void
c_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)))

  // compute actual arguments
  unsigned tmpidx_base = tmpvar_counter;
  tmpvar_counter += r->formal_args.size();
  string tmp_base = "l->__tmp";

  for (unsigned i=0; i<e->args.size(); i++)
    {
      unsigned tmpidx = tmpidx_base + 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);

      c_assign (tmp_base + stringify(tmpidx),
                e->args[i], "function actual argument evaluation");
    }

  o->newline();
  o->newline() << "if (c->nesting+2 >= MAXNESTING) errorcount ++;";
  o->newline() << "c->actioncount ++;";
  o->newline() << "if (c->actioncount > MAXACTION) errorcount ++;";
  o->newline() << "if (errorcount) goto out;";
  o->newline();

  // copy in actual arguments
  for (unsigned i=0; i<e->args.size(); i++)
    {
      unsigned tmpidx = tmpidx_base + 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);

      c_assign ("c->locals[c->nesting+1].function_" +
		c_varname (r->name) + "." +
                c_varname (r->formal_args[i]->name),
                tmp_base + stringify (tmpidx),
                e->args[i]->type,
                "function actual argument copy",
                e->args[i]->tok);
    }

  // call function
  o->newline() << "c->nesting ++;";
  o->newline() << "function_" << c_varname (r->name) << " (c);";
  o->newline() << "c->nesting --;";
  
  // return result from retvalue slot
  
  if (r->type == pe_unknown)
    // If we passed typechecking, then nothing will use this return value
    o->newline() << "(void) 0;";
  else
    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.op = new translator_output (s.translated_source);
  c_unparser cup (& s);
  s.up = & cup;

  try
    {
      s.op->line() << "#define TEST_MODE " << (s.test_mode ? 1 : 0) << endl;
      s.op->newline() << "#include \"runtime.h\"";

      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++)
	if (s.functions[i]->body)
	  s.up->emit_functionsig (s.functions[i]);

      s.op->newline() << "/* functions */";
      for (unsigned i=0; i<s.functions.size(); i++)
	if (s.functions[i]->body)
	  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.up->emit_module_init ();
      s.up->emit_module_exit ();

      s.op->newline();
      s.op->newline() << "#if TEST_MODE";

      s.op->newline() << "/* test mode mainline */";
      s.op->newline() << "int main () {";
      s.op->newline(1) << "int rc = systemtap_module_init ();";
      s.op->newline() << "if (!rc) rc = systemtap_module_exit ();";
      s.op->newline() << "return rc;";
      s.op->newline(-1) << "}";

      s.op->newline() << "#else";

      s.op->newline();
      s.op->newline() << "static int __init _systemtap_module_init (void);";
      s.op->newline() << "int _systemtap_module_init () {";
      s.op->newline(1) << "return systemtap_module_init ();";
      s.op->newline(-1) << "}";

      s.op->newline();
      s.op->newline() << "static void __exit _systemtap_module_exit (void);";
      s.op->newline() << "void _systemtap_module_exit () {";
      s.op->newline(1) << "systemtap_module_exit ();";
      s.op->newline(-1) << "}";

      s.op->newline();
      s.op->newline() << "module_init (_systemtap_module_init);";
      s.op->newline() << "module_exit (_systemtap_module_exit);";
      s.op->newline() << "MODULE_DESCRIPTION(\"systemtap probe\");";
      s.op->newline() << "MODULE_LICENSE(\"GPL\");"; // XXX

      s.op->newline() << "#endif";

      s.op->line() << endl;
    }
  catch (const semantic_error& e)
    {
      s.print_error (e);
    }

  delete s.op;
  s.op = 0;
  s.up = 0;
  return rc + s.num_errors;
}