-
Notifications
You must be signed in to change notification settings - Fork 0
/
extern.cpp
90 lines (79 loc) · 3.33 KB
/
extern.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
/*
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 "extern.h"
namespace BMV2 {
Util::JsonArray*
Extern::addExternAttributes(const IR::Declaration_Instance* di,
const IR::ExternBlock* block) {
auto attributes = new Util::JsonArray();
auto paramIt = block->getConstructorParameters()->parameters.begin();
for (auto arg : *di->arguments) {
auto name = arg->toString();
if (arg->is<IR::Constant>()) {
auto cVal = arg->to<IR::Constant>();
if (arg->type->is<IR::Type_Bits>()) {
json->add_extern_attribute(name, "hexstr",
stringRepr(cVal->value), attributes);
} else {
BUG("%1%: unhandled constant constructor param", cVal->toString());
}
} else if (arg->is<IR::Declaration_ID>()) {
auto declId = arg->to<IR::Declaration_ID>();
json->add_extern_attribute(name, "string", declId->toString(), attributes);
} else if (arg->type->is<IR::Type_Enum>()) {
json->add_extern_attribute(name, "string", arg->toString(), attributes);
} else {
BUG("%1%: unknown constructor param type", arg->type);
}
++paramIt;
}
return attributes;
}
/// generate extern_instances from instance declarations.
bool Extern::preorder(const IR::Declaration_Instance* decl) {
LOG1("ExternConv Visiting ..." << dbp(decl));
// Declaration_Instance -> P4Control -> ControlBlock
auto grandparent = getContext()->parent->node;
if (grandparent->is<IR::ControlBlock>()) {
auto block = grandparent->to<IR::ControlBlock>()->getValue(decl);
CHECK_NULL(block);
if (block->is<IR::ExternBlock>()) {
auto externBlock = block->to<IR::ExternBlock>();
auto name = decl->name;
auto type = "";
if (decl->type->is<IR::Type_Specialized>())
type = decl->type->to<IR::Type_Specialized>()->baseType->toString();
else if (decl->type->is<IR::Type_Name>())
type = decl->type->to<IR::Type_Name>()->path->name.toString();
else
P4C_UNIMPLEMENTED("extern support for %1%", decl);
auto attributes = addExternAttributes(decl, externBlock);
json->add_extern(name, type, attributes);
} else {
BUG("%1% Unsupported block type for extern generation.", block->toString());
}
}
return false;
}
/// Custom visitor to enable traversal on other blocks
bool Extern::preorder(const IR::PackageBlock *block) {
if (backend->target != Target::PORTABLE)
return false;
for (auto it : block->constantValue) {
if (it.second->is<IR::Block>()) {
visit(it.second->getNode());
}
}
return false;
}
} // namespace BMV2