forked from chipsalliance/Surelog
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hellouhdm.cpp
228 lines (211 loc) · 8.13 KB
/
hellouhdm.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
/*
Copyright 2019 Alain Dargelas
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.
*/
/*
* File: hellouhdm.cpp
* Author: alain
*
* Created on April 13, 2020, 08:07 PM
*/
// Example of usage:
// cd tests/UnitElabBlock
// hellouhdm top.v -parse -mutestdout
#include <Surelog/API/Surelog.h>
#include <Surelog/CommandLine/CommandLineParser.h>
#include <Surelog/ErrorReporting/ErrorContainer.h>
#include <Surelog/SourceCompile/SymbolTable.h>
#include <functional>
#include <iostream>
// UHDM
#include <uhdm/ElaboratorListener.h>
#include <uhdm/VpiListener.h>
#include <uhdm/uhdm.h>
int main(int argc, const char** argv) {
// Read command line, compile a design, use -parse argument
uint32_t code = 0;
SURELOG::SymbolTable* symbolTable = new SURELOG::SymbolTable();
SURELOG::ErrorContainer* errors = new SURELOG::ErrorContainer(symbolTable);
SURELOG::CommandLineParser* clp =
new SURELOG::CommandLineParser(errors, symbolTable, false, false);
clp->noPython();
clp->setParse(true);
clp->setwritePpOutput(true);
clp->setCompile(true);
clp->setElaborate(true); // Request Surelog instance tree Elaboration
// clp->setElabUhdm(true); // Request UHDM Uniquification/Elaboration
bool success = clp->parseCommandLine(argc, argv);
errors->printMessages(clp->muteStdout());
vpiHandle the_design = 0;
SURELOG::scompiler* compiler = nullptr;
if (success && (!clp->help())) {
compiler = SURELOG::start_compiler(clp);
the_design = SURELOG::get_uhdm_design(compiler);
auto stats = errors->getErrorStats();
code = (!success) | stats.nbFatal | stats.nbSyntax | stats.nbError;
}
std::string result;
// If UHDM is not already elaborated/uniquified (uhdm db was saved by a
// different process pre-elaboration), then optionally elaborate it:
if (the_design && (!vpi_get(vpiElaborated, the_design))) {
std::cout << "UHDM Elaboration...\n";
UHDM::Serializer serializer;
UHDM::ElaboratorContext* elaboratorContext =
new UHDM::ElaboratorContext(&serializer, true);
elaboratorContext->m_elaborator.listenDesigns({the_design});
delete elaboratorContext;
}
// Browse the UHDM Data Model using the IEEE VPI API.
// See third_party/Verilog_Object_Model.pdf
// Either use the
// - C IEEE API, (See third_party/UHDM/tests/test_helper.h)
// - or C++ UHDM API (See third_party/UHDM/headers/*.h)
// - Listener design pattern (See third_party/UHDM/tests/test_listener.cpp)
// - Walker design pattern (See third_party/UHDM/src/vpi_visitor.cpp)
if (the_design) {
UHDM::design* udesign = nullptr;
if (vpi_get(vpiType, the_design) == vpiDesign) {
// C++ top handle from which the entire design can be traversed using the
// C++ API
udesign = UhdmDesignFromVpiHandle(the_design);
result.append("Design name (C++): ")
.append(udesign->VpiName())
.append("\n");
}
// Example demonstrating the classic VPI API traversal of the folded model
// of the design Flat non-elaborated module/interface/packages/classes list
// contains ports/nets/statements (No ranges or sizes here, see elaborated
// section below)
result +=
"Design name (VPI): " + std::string(vpi_get_str(vpiName, the_design)) +
"\n";
// Flat Module list:
result += "Module List:\n";
vpiHandle modItr = vpi_iterate(UHDM::uhdmallModules, the_design);
while (vpiHandle obj_h = vpi_scan(modItr)) {
if (vpi_get(vpiType, obj_h) != vpiModule) {
result += "ERROR: this is not a module\n";
}
std::string defName;
std::string objectName;
if (const char* s = vpi_get_str(vpiDefName, obj_h)) {
defName = s;
}
if (const char* s = vpi_get_str(vpiName, obj_h)) {
if (!defName.empty()) {
defName += " ";
}
objectName = std::string("(") + s + std::string(")");
}
// ...
// Iterate thru statements
// ...
result.append("+ module: ")
.append(defName)
.append(objectName)
.append(", file:")
.append(vpi_get_str(vpiFile, obj_h))
.append(", line:")
.append(std::to_string(vpi_get(vpiLineNo, obj_h)));
vpiHandle processItr = vpi_iterate(vpiProcess, obj_h);
while (vpiHandle sub_h = vpi_scan(processItr)) {
result += "\n \\_ process stmt, file:" +
std::string(vpi_get_str(vpiFile, sub_h)) +
", line:" + std::to_string(vpi_get(vpiLineNo, sub_h));
vpi_release_handle(sub_h);
}
vpiHandle assignItr = vpi_iterate(vpiContAssign, obj_h);
while (vpiHandle sub_h = vpi_scan(assignItr)) {
result += "\n \\_ assign stmt, file:" +
std::string(vpi_get_str(vpiFile, sub_h)) +
", line:" + std::to_string(vpi_get(vpiLineNo, sub_h));
}
vpi_release_handle(assignItr);
// ...
// Iterate thru ports
// ...
result += "\n";
vpi_release_handle(obj_h);
}
vpi_release_handle(modItr);
// Instance tree:
// Elaborated Instance tree
result += "Instance Tree:\n";
vpiHandle instItr = vpi_iterate(UHDM::uhdmtopModules, the_design);
while (vpiHandle obj_h = vpi_scan(instItr)) {
std::function<std::string(vpiHandle, std::string)> inst_visit =
[&inst_visit](vpiHandle obj_h, std::string margin) {
std::string res;
std::string defName;
std::string objectName;
if (const char* s = vpi_get_str(vpiDefName, obj_h)) {
defName = s;
}
if (const char* s = vpi_get_str(vpiName, obj_h)) {
if (!defName.empty()) {
defName += " ";
}
objectName = std::string("(") + s + std::string(")");
}
std::string f;
if (const char* s = vpi_get_str(vpiFile, obj_h)) {
f = s;
}
res.append(margin)
.append("+ module: ")
.append(defName)
.append(objectName)
.append(", file:")
.append(f)
.append(", line:")
.append(std::to_string(vpi_get(vpiLineNo, obj_h)))
.append("\n");
// Recursive tree traversal
margin = std::string(" ") + margin;
if (vpi_get(vpiType, obj_h) == vpiModule ||
vpi_get(vpiType, obj_h) == vpiGenScope) {
vpiHandle subItr = vpi_iterate(vpiModule, obj_h);
while (vpiHandle sub_h = vpi_scan(subItr)) {
res += inst_visit(sub_h, margin);
vpi_release_handle(sub_h);
}
vpi_release_handle(subItr);
}
if (vpi_get(vpiType, obj_h) == vpiModule ||
vpi_get(vpiType, obj_h) == vpiGenScope) {
vpiHandle subItr = vpi_iterate(vpiGenScopeArray, obj_h);
while (vpiHandle sub_h = vpi_scan(subItr)) {
res += inst_visit(sub_h, margin);
vpi_release_handle(sub_h);
}
vpi_release_handle(subItr);
}
if (vpi_get(vpiType, obj_h) == vpiGenScopeArray) {
vpiHandle subItr = vpi_iterate(vpiGenScope, obj_h);
while (vpiHandle sub_h = vpi_scan(subItr)) {
res += inst_visit(sub_h, margin);
vpi_release_handle(sub_h);
}
vpi_release_handle(subItr);
}
return res;
};
result.append(inst_visit(obj_h, ""));
}
}
std::cout << result << std::endl;
// Do not delete these objects until you are done with UHDM
SURELOG::shutdown_compiler(compiler);
delete clp;
delete symbolTable;
delete errors;
return code;
}