-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelaborator_main.cpp
More file actions
301 lines (251 loc) · 10.1 KB
/
Copy pathelaborator_main.cpp
File metadata and controls
301 lines (251 loc) · 10.1 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "SystemRDLLexer.h"
#include "SystemRDLParser.h"
#include "antlr4-runtime.h"
#include "cmdline_parser.h"
#include "elaborator.h"
#include "systemrdl_api.h"
#include "systemrdl_version.h"
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
using namespace antlr4;
using namespace systemrdl;
// Printer for elaborated model
class ElaboratedModelPrinter : public ElaboratedModelTraverser
{
public:
void print_model(ElaboratedAddrmap &root)
{
std::cout << "=== Elaborated SystemRDL Model ===" << std::endl;
traverse(root);
}
protected:
void pre_visit(ElaboratedNode &node) override
{
// Print indentation
for (int i = 0; i < depth_; i++) {
std::cout << " ";
}
// Print node information
std::string icon = "[REG]";
if (node.get_node_type() == "addrmap")
icon = "[MAP]";
else if (node.get_node_type() == "regfile")
icon = "[FILE]";
else if (node.get_node_type() == "reg")
icon = "[REG]";
else if (node.get_node_type() == "field")
icon = "[FIELD]";
else if (node.get_node_type() == "mem")
icon = "[MEM]";
std::cout << icon << " " << node.get_node_type() << ": " << node.inst_name;
if (node.absolute_address != 0 || node.get_node_type() == "addrmap") {
std::cout << " @ 0x" << std::hex << node.absolute_address << std::dec;
}
// For fields, show bit range
if (node.get_node_type() == "field") {
auto msb_prop = node.get_property("msb");
auto lsb_prop = node.get_property("lsb");
if (msb_prop && lsb_prop) {
std::cout << " [" << msb_prop->int_val << ":" << lsb_prop->int_val << "]";
}
}
if (node.size > 0) {
std::cout << " (size: " << node.size << " bytes)";
}
if (!node.array_dimensions.empty()) {
std::cout << " [array: ";
for (size_t i = 0; i < node.array_dimensions.size(); ++i) {
if (i > 0)
std::cout << "x";
std::cout << node.array_dimensions[i];
}
std::cout << "]";
}
std::cout << std::endl;
// Print properties
for (const auto &prop : node.properties) {
for (int i = 0; i <= depth_; i++) {
std::cout << " ";
}
std::cout << " " << prop.first << ": ";
switch (prop.second.type) {
case PropertyValue::STRING:
std::cout << "\"" << prop.second.string_val << "\"";
break;
case PropertyValue::INTEGER:
std::cout << prop.second.int_val;
break;
case PropertyValue::BOOLEAN:
std::cout << (prop.second.bool_val ? "true" : "false");
break;
default:
std::cout << "unknown";
}
std::cout << std::endl;
}
depth_++;
}
void post_visit(ElaboratedNode &node) override { depth_--; }
private:
int depth_ = 0;
};
// Helper function to generate default JSON filename
std::string get_default_ast_filename(const std::string &input_file, const std::string &suffix = "")
{
// Simple basename extraction
size_t last_slash = input_file.find_last_of("/\\");
size_t last_dot = input_file.find_last_of('.');
std::string basename;
if (last_slash != std::string::npos) {
basename = input_file.substr(last_slash + 1);
} else {
basename = input_file;
}
if (last_dot != std::string::npos && last_dot > last_slash) {
size_t trim_pos = last_dot - (last_slash != std::string::npos ? last_slash + 1 : 0);
basename.resize(trim_pos);
}
return basename + suffix + ".json";
}
int main(int argc, char *argv[])
{
// Setup command line parser
CmdLineParser cmdline("SystemRDL Elaborator - Parse and elaborate SystemRDL files");
cmdline.set_version(systemrdl::get_detailed_version());
cmdline.add_option_with_optional_value(
"a", "ast", "Enable AST JSON output, optionally specify filename");
cmdline.add_option_with_optional_value(
"j", "json", "Enable simplified JSON output, optionally specify filename");
cmdline.add_option("h", "help", "Show this help message");
if (!cmdline.parse(argc, argv)) {
return argc == 2
&& (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h"
|| std::string(argv[1]) == "--version" || std::string(argv[1]) == "-v")
? 0
: 1;
}
const auto &args = cmdline.get_positional_args();
if (args.empty()) {
std::cerr << "Error: No input file specified" << std::endl;
cmdline.print_help();
return 1;
}
std::string inputFile = args[0];
try {
// 1. Parsing phase
std::cout << "[PARSE] Parsing SystemRDL file: " << inputFile << std::endl;
std::ifstream stream(inputFile);
if (!stream.is_open()) {
std::cerr << "Error: Cannot open file " << inputFile << std::endl;
return 1;
}
ANTLRInputStream input(stream);
SystemRDLLexer lexer(&input);
CommonTokenStream tokens(&lexer);
SystemRDLParser parser(&tokens);
tree::ParseTree *tree = parser.root();
if (parser.getNumberOfSyntaxErrors() > 0) {
std::cerr << "Syntax errors found: " << parser.getNumberOfSyntaxErrors() << std::endl;
return 1;
}
std::cout << "[OK] Parsing successful!" << std::endl;
// 2. Elaboration phase
std::cout << "\n[ELAB] Starting elaboration..." << std::endl;
SystemRDLElaborator elaborator;
auto root_context = dynamic_cast<SystemRDLParser::RootContext *>(tree);
auto elaborated_model = elaborator.elaborate(root_context);
if (elaborator.has_errors()) {
std::cerr << "Elaboration errors:" << std::endl;
for (const auto &error : elaborator.get_errors()) {
std::cerr << " Line " << error.line << ":" << error.column << " - "
<< error.message << std::endl;
}
return 1;
}
if (!elaborated_model) {
std::cerr << "Failed to elaborate model" << std::endl;
return 1;
}
std::cout << "[OK] Elaboration successful!" << std::endl;
// 3. Print elaborated model
std::cout << "\n" << std::string(50, '=') << std::endl;
ElaboratedModelPrinter printer;
printer.print_model(*elaborated_model);
// 4. Generate address mapping
std::cout << "\n" << std::string(50, '=') << std::endl;
std::cout << "[ADDR] Address Map:" << std::endl;
std::cout << std::string(50, '=') << std::endl;
AddressMapGenerator addr_gen;
auto address_map = addr_gen.generate_address_map(*elaborated_model);
std::cout << std::left << std::setw(12) << "Address" << std::setw(8) << "Size"
<< std::setw(20) << "Name" << "Path" << std::endl;
std::cout << std::string(60, '-') << std::endl;
for (const auto &entry : address_map) {
printf(
"0x%08lx %-6lu %-18s %s\n",
entry.address,
entry.size,
entry.name.c_str(),
entry.path.c_str());
}
// 5. Generate AST JSON output if requested
if (cmdline.is_set("ast")) {
std::string output_file = cmdline.get_value("ast");
// If no filename provided, generate default
if (output_file.empty()) {
output_file = get_default_ast_filename(inputFile, "_ast_elaborated");
}
std::cout << "\nGenerating AST JSON output..." << std::endl;
// Use unified API for consistent JSON output
systemrdl::Result result = systemrdl::file::elaborate(inputFile);
if (result.ok()) {
std::ofstream outFile(output_file);
if (outFile.is_open()) {
outFile << result.value();
outFile.close();
std::cout << "AST JSON output written to: " << output_file << std::endl;
} else {
std::cerr << "Failed to write AST JSON output to: " << output_file << std::endl;
return 1;
}
} else {
std::cerr << "Failed to generate AST JSON: " << result.error() << std::endl;
return 1;
}
}
// 6. Generate simplified JSON output if requested
if (cmdline.is_set("json")) {
std::string output_file = cmdline.get_value("json");
// If no filename provided, generate default
if (output_file.empty()) {
output_file = get_default_ast_filename(inputFile, "_simplified");
}
std::cout << "\nGenerating simplified JSON output..." << std::endl;
// Use unified API for consistent JSON output
systemrdl::Result result = systemrdl::file::elaborate_simplified(inputFile);
if (result.ok()) {
std::ofstream outFile(output_file);
if (outFile.is_open()) {
outFile << result.value();
outFile.close();
std::cout << "Simplified JSON output written to: " << output_file << std::endl;
} else {
std::cerr << "Failed to write simplified JSON output to: " << output_file
<< std::endl;
return 1;
}
} else {
std::cerr << "Failed to generate simplified JSON: " << result.error() << std::endl;
return 1;
}
}
std::cout << "\nElaboration completed successfully!" << std::endl;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}