Skip to content

Commit af83d8d

Browse files
committed
rewrite smtlib pass to use SExpr class
1 parent 6b3d9c6 commit af83d8d

File tree

3 files changed

+409
-389
lines changed

3 files changed

+409
-389
lines changed

backends/functional/cxx.cc

+19-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
#include "kernel/yosys.h"
2121
#include "kernel/functionalir.h"
22+
#include <ctype.h>
2223

2324
USING_YOSYS_NAMESPACE
2425
PRIVATE_NAMESPACE_BEGIN
2526

26-
const char illegal_characters[] = "!\"#%&'()*+,-./:;<=>?@[]\\^`{|}~ ";
2727
const char *reserved_keywords[] = {
2828
"alignas","alignof","and","and_eq","asm","atomic_cancel","atomic_commit",
2929
"atomic_noexcept","auto","bitand","bitor","bool","break","case",
@@ -42,6 +42,16 @@ const char *reserved_keywords[] = {
4242
nullptr
4343
};
4444

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+
4555
struct CxxType {
4656
FunctionalIR::Sort sort;
4757
CxxType(FunctionalIR::Sort sort) : sort(sort) {}
@@ -61,30 +71,30 @@ using CxxWriter = FunctionalTools::Writer;
6171
struct CxxStruct {
6272
std::string name;
6373
dict<IdString, CxxType> types;
64-
FunctionalTools::Scope scope;
74+
CxxScope<IdString> scope;
6575
CxxStruct(std::string name)
66-
: name(name), scope(illegal_characters, reserved_keywords) {
76+
: name(name) {
6777
scope.reserve("fn");
6878
scope.reserve("visit");
6979
}
7080
void insert(IdString name, CxxType type) {
71-
scope(name);
81+
scope(name, name);
7282
types.insert({name, type});
7383
}
7484
void print(CxxWriter &f) {
7585
f.print("\tstruct {} {{\n", name);
7686
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));
7888
}
7989
f.print("\n\t\ttemplate <typename T> void visit(T &&fn) {{\n");
8090
for (auto p : types) {
81-
f.print("\t\t\tfn(\"{}\", {});\n", RTLIL::unescape_id(p.first), scope(p.first));
91+
f.print("\t\t\tfn(\"{}\", {});\n", RTLIL::unescape_id(p.first), scope(p.first, p.first));
8292
}
8393
f.print("\t\t}}\n");
8494
f.print("\t}};\n\n");
8595
};
8696
std::string operator[](IdString field) {
87-
return scope(field);
97+
return scope(field, field);
8898
}
8999
};
90100

@@ -165,7 +175,7 @@ struct CxxModule {
165175
output_struct.insert(name, sort);
166176
for (auto [name, sort] : ir.state())
167177
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);
169179
}
170180
void write_header(CxxWriter &f) {
171181
f.print("#include \"sim.h\"\n\n");
@@ -180,7 +190,7 @@ struct CxxModule {
180190
}
181191
void write_eval_def(CxxWriter &f) {
182192
f.print("void {0}::eval({0}::Inputs const &input, {0}::Outputs &output, {0}::State const &current_state, {0}::State &next_state)\n{{\n", module_name);
183-
FunctionalTools::Scope locals(illegal_characters, reserved_keywords);
193+
CxxScope<int> locals;
184194
locals.reserve("input");
185195
locals.reserve("output");
186196
locals.reserve("current_state");

0 commit comments

Comments
 (0)