-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfppProgram.cpp
283 lines (238 loc) · 9.05 KB
/
fppProgram.cpp
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
/*
Modifications copyright 2017 CESNET
Copyright 2013-present Barefoot Networks, 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://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 <chrono>
#include <ctime>
#include "ir/ir.h"
#include "fppProgram.h"
#include "fppType.h"
#include "fppParser.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/common/options.h"
#include <stdio.h>
namespace FPP {
bool FPPProgram::build() {
auto pack = toplevel->getMain();
if (pack->getConstructorParameters()->size() != 1) {
::error("Expected toplevel package %1% to have 1 parameter", pack->type);
return false;
}
auto pb = pack->getParameterValue(model.parser.parser.name)
->to<IR::ParserBlock>();
BUG_CHECK(pb != nullptr, "No parser block found");
parser = new FPPParser(this, pb, typeMap);
bool success = parser->build();
if (!success)
return success;
return true;
}
void FPPProgram::emitC(CodeBuilder* builder, cstring header) {
emitGeneratedComment(builder);
builder->appendFormat("#include \"%s\"", header);
builder->newline();
builder->target->emitIncludes(builder);
builder->newline();
builder->emitIndent();
builder->target->emitCodeSection(builder, functionName);
builder->emitIndent();
builder->target->emitMain(builder, functionName);
builder->blockStart();
//builder->appendLine("void *headers = NULL;");
builder->emitIndent();
builder->appendLine("packet_hdr_t *last_hdr = NULL;");
builder->emitIndent();
builder->appendLine("packet_hdr_t *hdr = NULL;");
//emitHeaderInstances(builder);
// builder->append(" = NULL");
//parser->headerType->emitInitializer(builder);
// builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("const uint8_t *%s = packet", packetStartVar);
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("const uint8_t *%s = packet + packet_len", packetEndVar);
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("uint64_t %s = 0", offsetVar);
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("enum fpp_errorCodes %s = ParserDefaultReject", errorVar);
builder->endOfStatement(true);
emitLocalVariables(builder);
builder->newline();
for (auto loc : parser->parserBlock->container->parserLocals)
{
auto ptr = dynamic_cast<const IR::Declaration_Variable *>(loc);
auto type = FPPTypeFactory::instance->create(ptr->type);
if (type == nullptr)
continue;
type->emitType(builder);
builder->appendFormat(" %s", loc->name.name.c_str());
builder->endOfStatement(true);
}
builder->newline();
for (auto loc : parser->parserBlock->container->parserLocals)
{
auto ptr = dynamic_cast<const IR::Declaration_Variable *>(loc);
auto type = FPPTypeFactory::instance->create(ptr->type);
if (type == nullptr)
continue;
builder->emitIndent();
builder->appendFormat("(void) %s", loc->name.name.c_str());
builder->endOfStatement(true);
}
builder->newline();
builder->emitIndent();
builder->append("*out = NULL");
builder->endOfStatement(true);
builder->newline();
builder->emitIndent();
builder->appendFormat("goto %s;", IR::ParserState::start.c_str());
builder->newline();
parser->emit(builder);
emitAcceptState(builder);
builder->emitIndent();
builder->append(endLabel); // TODO end of function/ return code
builder->appendLine(":");
builder->emitIndent();
builder->appendLine("return fpp_errorCode");
builder->appendLine(";");
builder->blockEnd(true); // end of function
builder->target->emitLicense(builder, license);
}
void FPPProgram::emitGeneratedComment(CodeBuilder* builder) {
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(now);
builder->append("/* Automatically generated by ");
builder->append(options.exe_name);
builder->append(" from ");
builder->append(options.file);
builder->append(" on ");
builder->append(std::ctime(&time));
builder->append(" */");
builder->newline();
}
void FPPProgram::emitH(CodeBuilder* builder, cstring) {
emitGeneratedComment(builder);
builder->appendLine("#ifndef _P4_GEN_HEADER_");
builder->appendLine("#define _P4_GEN_HEADER_");
builder->target->emitIncludes(builder);
builder->newline();
emitPreamble(builder);
emitTypes(builder);
builder->append("typedef struct packet_hdr_s ");
builder->blockStart();
builder->emitIndent();
builder->appendLine("enum fpp_headers type;");
builder->emitIndent();
builder->appendLine("void *hdr;");
builder->emitIndent();
builder->appendLine("struct packet_hdr_s *next;");
builder->blockEnd(true);
builder->append("packet_hdr_t");
builder->endOfStatement(true);
builder->newline();
builder->target->emitMain(builder, functionName);
builder->endOfStatement(true);
builder->appendLine("#endif");
}
void FPPProgram::emitTypes(CodeBuilder* builder) {
builder->append("enum fpp_headers ");
builder->blockStart();
builder->emitIndent();
builder->append("fpp_unknown_hdr");
for (auto d : program->objects) {
if (d->is<IR::Type>() && !d->is<IR::IContainer>() &&
!d->is<IR::Type_Extern>() && !d->is<IR::Type_Parser>() &&
!d->is<IR::Type_Control>() && !d->is<IR::Type_Typedef>() &&
!d->is<IR::Type_Error>()) {
auto type = FPPTypeFactory::instance->create(d->to<IR::Type>());
auto tmp = dynamic_cast<FPPStructType *>(type);
if (type == nullptr || tmp == nullptr)
continue;
builder->append(",");
builder->newline();
builder->emitIndent();
builder->appendFormat("fpp_%s", tmp->name);
}
}
builder->newline();
builder->blockEnd(true);
builder->endOfStatement(true);
builder->newline();
for (auto d : program->objects) {
if (d->is<IR::Type>() && !d->is<IR::IContainer>() &&
!d->is<IR::Type_Extern>() && !d->is<IR::Type_Parser>() &&
!d->is<IR::Type_Control>() && !d->is<IR::Type_Typedef>() &&
!d->is<IR::Type_Error>()) {
auto type = FPPTypeFactory::instance->create(d->to<IR::Type>());
if (type == nullptr)
continue;
type->emit(builder);
builder->newline();
}
}
}
namespace {
class ErrorCodesVisitor : public Inspector {
CodeBuilder* builder;
public:
explicit ErrorCodesVisitor(CodeBuilder* builder) : builder(builder) {}
bool preorder(const IR::Type_Error* errors) override {
for (auto m : *errors->getDeclarations()) {
builder->emitIndent();
builder->appendFormat("%s,\n", m->getName().name.c_str());
}
return false;
}
};
} // namespace
void FPPProgram::emitPreamble(CodeBuilder* builder) {
builder->emitIndent();
builder->appendFormat("enum %s ", errorEnum.c_str());
builder->blockStart();
ErrorCodesVisitor visitor(builder);
program->apply(visitor);
builder->emitIndent();
builder->appendLine("ParserDefaultReject,");
builder->emitIndent();
builder->appendLine("OutOfMemory");
builder->blockEnd(false);
builder->endOfStatement(true);
builder->newline();
builder->appendLine("#define FPP_MASK(t, w) ((((t)(1)) << (w)) - (t)1)");
builder->appendLine("#define BYTES(w) ((w) / 8)");
builder->appendLine("#define load_byte(ptr, bytes) (*(const uint8_t *)((const uint8_t *)(ptr) + bytes))");
builder->appendLine("#define load_half(ptr, bytes) (*(const uint16_t *)((const uint8_t *)(ptr) + bytes))");
builder->appendLine("#define load_word(ptr, bytes) (*(const uint32_t *)((const uint8_t *)(ptr) + bytes))");
builder->appendLine("#define load_dword(ptr, bytes) (*(const uint64_t *)((const uint8_t *)(ptr) + bytes))");
builder->newline();
}
void FPPProgram::emitLocalVariables(CodeBuilder* builder) {
}
void FPPProgram::emitHeaderInstances(CodeBuilder* builder) {
builder->emitIndent();
parser->headerType->declare(builder, parser->headers->name.name, true);
}
void FPPProgram::emitAcceptState(CodeBuilder* builder) {
builder->emitIndent();
builder->append(IR::ParserState::accept);
builder->append(":");
builder->newline();
builder->emitIndent();
builder->blockStart();
builder->emitIndent();
builder->appendLine("return NoError;");
builder->blockEnd(true);
}
} // namespace FPP