blob: 09cd75761b4377c2fe701528883445f0f9172f8e (
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
|
// semantic analysis pass, beginnings of elaboration
// Copyright 2005 Red Hat Inc.
// GPL
#include "staptree.h"
#include "parse.h"
#include <iostream>
int
semantic_pass_1 (vector<stapfile*>& files)
{
int rc = 0;
// link up symbols to their declarations
for (unsigned i=0; i<files.size(); i++)
{
stapfile* f = files[i];
// ... on functions
for (unsigned j=0; j<f->functions.size(); j++)
{
functiondecl* fn = f->functions[j];
symresolution_info ri (fn->locals, f->globals, f->functions, fn);
fn->body->resolve_symbols (ri);
if (ri.num_unresolved)
rc ++;
}
// ... and on probes
for (unsigned j=0; j<f->probes.size(); j++)
{
probe* pn = f->probes[j];
symresolution_info ri (pn->locals, f->globals, f->functions);
pn->body->resolve_symbols (ri);
if (ri.num_unresolved)
rc ++;
}
}
return rc;
}
int
semantic_pass_2 (vector<stapfile*>& files)
{
int rc = 0;
// next pass: type inference
unsigned iterations = 0;
typeresolution_info ti;
ti.assert_resolvability = false;
while (1)
{
iterations ++;
// cerr << "Type resolution, iteration " << iterations << endl;
ti.num_newly_resolved = 0;
ti.num_still_unresolved = 0;
for (unsigned i=0; i<files.size(); i++)
{
stapfile* f = files[i];
for (unsigned j=0; j<f->functions.size(); j++)
{
functiondecl* fn = f->functions[j];
ti.current_function = fn;
fn->body->resolve_types (ti);
if (fn->type == pe_unknown)
ti.unresolved (fn->tok);
}
for (unsigned j=0; j<f->probes.size(); j++)
{
probe* pn = f->probes[j];
ti.current_function = 0;
pn->body->resolve_types (ti);
}
for (unsigned j=0; j<f->globals.size(); j++)
{
vardecl* gd = f->globals[j];
if (gd->type == pe_unknown)
ti.unresolved (gd->tok);
}
}
if (ti.num_newly_resolved == 0) // converged
if (ti.num_still_unresolved == 0)
break; // successfully
else if (! ti.assert_resolvability)
ti.assert_resolvability = true; // last pass, with error msgs
else
{ // unsuccessful conclusion
rc ++;
break;
}
}
return rc;
}
int
main (int argc, char *argv [])
{
int rc = 0;
vector<stapfile*> files;
if (argc == 1)
{
stapfile* f = parser::parse (cin);
if (f)
files.push_back (f);
else
rc ++;
}
else for (int i = 1; i < argc; i ++)
{
stapfile* f = parser::parse (argv[i]);
if (f)
files.push_back (f);
else
rc ++;
}
rc += semantic_pass_1 (files);
if (rc == 0)
rc += semantic_pass_2 (files);
for (unsigned i=0; i<files.size(); i++)
{
stapfile* f = files[i];
for (unsigned j=0; j<f->functions.size(); j++)
{
functiondecl* fn = f->functions[j];
cerr << "Function ";
fn->printsig (cerr);
cerr << endl << "locals:" << endl;
for (unsigned k=0; k<fn->locals.size(); k++)
{
vardecl* fa = fn->locals[k];
cerr << "\t";
fa->printsig (cerr);
cerr << endl;
}
cerr << endl;
}
for (unsigned j=0; j<f->probes.size(); j++)
{
probe* pn = f->probes[j];
cerr << "Probe ";
pn->printsig (cerr);
cerr << "locals:" << endl;
for (unsigned k=0; k<pn->locals.size(); k++)
{
vardecl* fa = pn->locals[k];
cerr << "\t";
fa->printsig (cerr);
cerr << endl;
}
cerr << endl;
}
cerr << "globals:" << endl;
for (unsigned k=0; k<f->globals.size(); k++)
{
vardecl* fa = f->globals[k];
cerr << "\t";
fa->printsig (cerr);
cerr << endl;
}
cerr << endl;
}
return rc;
}
|