19
19
20
20
#include " kernel/yosys.h"
21
21
#include " kernel/functionalir.h"
22
+ #include < ctype.h>
22
23
23
24
USING_YOSYS_NAMESPACE
24
25
PRIVATE_NAMESPACE_BEGIN
25
26
26
- const char illegal_characters[] = " !\" #%&'()*+,-./:;<=>?@[]\\ ^`{|}~ " ;
27
27
const char *reserved_keywords[] = {
28
28
" alignas" ," alignof" ," and" ," and_eq" ," asm" ," atomic_cancel" ," atomic_commit" ,
29
29
" atomic_noexcept" ," auto" ," bitand" ," bitor" ," bool" ," break" ," case" ,
@@ -42,6 +42,16 @@ const char *reserved_keywords[] = {
42
42
nullptr
43
43
};
44
44
45
+ template <typename Id> struct CxxScope : public FunctionalTools ::Scope<Id> {
46
+ CxxScope () {
47
+ for (const char **p = reserved_keywords; *p != nullptr ; p++)
48
+ this ->reserve (*p);
49
+ }
50
+ bool is_character_legal (char c) override {
51
+ return isascii (c) && (isalnum (c) || c == ' _' || c == ' $' );
52
+ }
53
+ };
54
+
45
55
struct CxxType {
46
56
FunctionalIR::Sort sort;
47
57
CxxType (FunctionalIR::Sort sort) : sort(sort) {}
@@ -61,30 +71,30 @@ using CxxWriter = FunctionalTools::Writer;
61
71
struct CxxStruct {
62
72
std::string name;
63
73
dict<IdString, CxxType> types;
64
- FunctionalTools::Scope scope;
74
+ CxxScope<IdString> scope;
65
75
CxxStruct (std::string name)
66
- : name(name), scope(illegal_characters, reserved_keywords) {
76
+ : name(name) {
67
77
scope.reserve (" fn" );
68
78
scope.reserve (" visit" );
69
79
}
70
80
void insert (IdString name, CxxType type) {
71
- scope (name);
81
+ scope (name, name );
72
82
types.insert ({name, type});
73
83
}
74
84
void print (CxxWriter &f) {
75
85
f.print (" \t struct {} {{\n " , name);
76
86
for (auto p : types) {
77
- f.print (" \t\t {} {};\n " , p.second .to_string (), scope (p.first ));
87
+ f.print (" \t\t {} {};\n " , p.second .to_string (), scope (p.first , p. first ));
78
88
}
79
89
f.print (" \n\t\t template <typename T> void visit(T &&fn) {{\n " );
80
90
for (auto p : types) {
81
- f.print (" \t\t\t fn(\" {}\" , {});\n " , RTLIL::unescape_id (p.first ), scope (p.first ));
91
+ f.print (" \t\t\t fn(\" {}\" , {});\n " , RTLIL::unescape_id (p.first ), scope (p.first , p. first ));
82
92
}
83
93
f.print (" \t\t }}\n " );
84
94
f.print (" \t }};\n\n " );
85
95
};
86
96
std::string operator [](IdString field) {
87
- return scope (field);
97
+ return scope (field, field );
88
98
}
89
99
};
90
100
@@ -165,7 +175,7 @@ struct CxxModule {
165
175
output_struct.insert (name, sort);
166
176
for (auto [name, sort] : ir.state ())
167
177
state_struct.insert (name, sort);
168
- module_name = FunctionalTools::Scope (illegal_characters, reserved_keywords) (module->name );
178
+ module_name = CxxScope< int >(). unique_name (module->name );
169
179
}
170
180
void write_header (CxxWriter &f) {
171
181
f.print (" #include \" sim.h\"\n\n " );
@@ -180,7 +190,7 @@ struct CxxModule {
180
190
}
181
191
void write_eval_def (CxxWriter &f) {
182
192
f.print (" void {0}::eval({0}::Inputs const &input, {0}::Outputs &output, {0}::State const ¤t_state, {0}::State &next_state)\n {{\n " , module_name);
183
- FunctionalTools::Scope locals (illegal_characters, reserved_keywords) ;
193
+ CxxScope< int > locals;
184
194
locals.reserve (" input" );
185
195
locals.reserve (" output" );
186
196
locals.reserve (" current_state" );
0 commit comments