forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cc
225 lines (197 loc) · 7.85 KB
/
parser.cc
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
/*
* Copyright (c) 2015 PLUMgrid, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include "bcc_exception.h"
#include "parser.h"
#include "type_helper.h"
namespace ebpf {
namespace cc {
using std::find;
using std::move;
using std::string;
using std::unique_ptr;
bool Parser::variable_exists(VariableDeclStmtNode *decl) const {
if (scopes_->current_var()->lookup(decl->id_->name_, SCOPE_LOCAL) == NULL) {
return false;
}
return true;
}
VariableDeclStmtNode *Parser::variable_add(vector<int> *types, VariableDeclStmtNode *decl) {
if (variable_exists(decl)) {
fprintf(stderr, "redeclaration of variable %s", decl->id_->name_.c_str());
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
VariableDeclStmtNode *Parser::variable_add(vector<int> *types, VariableDeclStmtNode *decl, ExprNode *init_expr) {
AssignExprNode::Ptr assign(new AssignExprNode(decl->id_->copy(), ExprNode::Ptr(init_expr)));
decl->init_.push_back(move(assign));
if (variable_exists(decl)) {
fprintf(stderr, "redeclaration of variable %s", decl->id_->name_.c_str());
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
StructVariableDeclStmtNode *Parser::variable_add(StructVariableDeclStmtNode *decl, ExprNodeList *args, bool is_kv) {
if (is_kv) {
// annotate the init expressions with the declared id
for (auto arg = args->begin(); arg != args->end(); ++arg) {
// decorate with the name of this decl
auto n = static_cast<AssignExprNode *>(arg->get());
auto id = static_cast<IdentExprNode *>(n->lhs_.get());
id->prepend_dot(decl->id_->name_);
}
} else {
fprintf(stderr, "must use key = value syntax\n");
return NULL;
}
decl->init_ = move(*args);
delete args;
if (variable_exists(decl)) {
fprintf(stderr, "ccpg: warning: redeclaration of variable '%s'\n", decl->id_->name_.c_str());
return nullptr;
}
decl->scope_id_ = string("v") + std::to_string(scopes_->current_var()->id_) + string("_");
scopes_->current_var()->add(decl->id_->name_, decl);
return decl;
}
StmtNode *Parser::state_add(Scopes::StateScope *scope, IdentExprNode *id, BlockStmtNode *body) {
if (scopes_->current_state()->lookup(id->full_name(), SCOPE_LOCAL)) {
fprintf(stderr, "redeclaration of state %s\n", id->full_name().c_str());
// redeclaration
return NULL;
}
auto state = new StateDeclStmtNode(IdentExprNode::Ptr(id), BlockStmtNode::Ptr(body));
// add a reference to the lower scope
state->subs_[0].scope_ = scope;
// add me to the upper scope
scopes_->current_state()->add(state->id_->full_name(), state);
state->scope_id_ = string("s") + std::to_string(scopes_->current_state()->id_) + string("_");
return state;
}
StmtNode *Parser::state_add(Scopes::StateScope *scope, IdentExprNode *id1, IdentExprNode *id2, BlockStmtNode *body) {
auto state = scopes_->current_state()->lookup(id1->full_name(), SCOPE_LOCAL);
if (!state) {
state = new StateDeclStmtNode(IdentExprNode::Ptr(id1), IdentExprNode::Ptr(id2), BlockStmtNode::Ptr(body));
// add a reference to the lower scope
state->subs_[0].scope_ = scope;
// add me to the upper scope
scopes_->current_state()->add(state->id_->full_name(), state);
state->scope_id_ = string("s") + std::to_string(scopes_->current_state()->id_) + string("_");
return state;
} else {
if (state->find_sub(id2->name_) != state->subs_.end()) {
fprintf(stderr, "redeclaration of state %s, %s\n", id1->full_name().c_str(), id2->full_name().c_str());
return NULL;
}
state->subs_.push_back(StateDeclStmtNode::Sub(IdentExprNode::Ptr(id2), BlockStmtNode::Ptr(body),
ParserStateStmtNode::Ptr(), scope));
delete id1;
return new StateDeclStmtNode(); // stub
}
}
bool Parser::table_exists(TableDeclStmtNode *decl, bool search_local) {
if (scopes_->top_table()->lookup(decl->id_->name_, search_local) == NULL) {
return false;
}
return true;
}
StmtNode *Parser::table_add(IdentExprNode *type, IdentExprNodeList *templates,
IdentExprNode *id, string *size) {
auto table = new TableDeclStmtNode(IdentExprNode::Ptr(type),
move(*templates),
IdentExprNode::Ptr(id), size);
if (table_exists(table, true)) {
fprintf(stderr, "redeclaration of table %s\n", id->name_.c_str());
return table;
}
scopes_->top_table()->add(id->name_, table);
return table;
}
StmtNode * Parser::struct_add(IdentExprNode *type, FormalList *formals) {
auto struct_decl = new StructDeclStmtNode(IdentExprNode::Ptr(type), move(*formals));
if (scopes_->top_struct()->lookup(type->name_, SCOPE_LOCAL) != NULL) {
fprintf(stderr, "redeclaration of struct %s\n", type->name_.c_str());
return struct_decl;
}
auto pr_it = pragmas_.find("packed");
if (pr_it != pragmas_.end() && pr_it->second == "true")
struct_decl->packed_ = true;
int i = 0;
size_t offset = 0;
for (auto it = struct_decl->stmts_.begin(); it != struct_decl->stmts_.end(); ++it, ++i) {
FieldType ft = bits_to_enum((*it)->bit_width_);
offset = struct_decl->is_packed() ? offset : align_offset(offset, ft);
(*it)->slot_ = i;
(*it)->bit_offset_ = offset;
offset += (*it)->bit_width_;
}
struct_decl->bit_width_ = struct_decl->is_packed() ? offset : align_offset(offset, UINT32_T);
scopes_->top_struct()->add(type->name_, struct_decl);
return struct_decl;
}
StmtNode * Parser::result_add(int token, IdentExprNode *id, FormalList *formals, BlockStmtNode *body) {
StmtNode *stmt = NULL;
switch (token) {
case Tok::TMATCH:
stmt = new MatchDeclStmtNode(IdentExprNode::Ptr(id), move(*formals), BlockStmtNode::Ptr(body));
break;
case Tok::TMISS:
stmt = new MissDeclStmtNode(IdentExprNode::Ptr(id), move(*formals), BlockStmtNode::Ptr(body));
break;
case Tok::TFAILURE:
stmt = new FailureDeclStmtNode(IdentExprNode::Ptr(id), move(*formals), BlockStmtNode::Ptr(body));
break;
default:
{}
}
return stmt;
}
StmtNode * Parser::func_add(vector<int> *types, Scopes::StateScope *scope,
IdentExprNode *id, FormalList *formals, BlockStmtNode *body) {
auto decl = new FuncDeclStmtNode(IdentExprNode::Ptr(id), move(*formals), BlockStmtNode::Ptr(body));
if (scopes_->top_func()->lookup(decl->id_->name_, SCOPE_LOCAL)) {
fprintf(stderr, "redeclaration of func %s\n", id->name_.c_str());
return decl;
}
auto cur_scope = scopes_->current_var();
scopes_->set_current(scope);
for (auto it = formals->begin(); it != formals->end(); ++it)
if (!variable_add(nullptr, it->get())) {
delete decl;
return nullptr;
}
scopes_->set_current(cur_scope);
decl->scope_ = scope;
scopes_->top_func()->add(id->name_, decl);
return decl;
}
void Parser::set_loc(Node *n, const BisonParser::location_type &loc) const {
n->line_ = loc.begin.line;
n->column_ = loc.begin.column;
n->text_ = lexer.text(loc);
}
string Parser::pragma(const string &name) const {
auto it = pragmas_.find(name);
if (it == pragmas_.end()) return "main";
return it->second;
}
} // namespace cc
} // namespace ebpf