Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Hanlon committed Mar 15, 2023
1 parent 13e91a5 commit d49d29e
Showing 1 changed file with 62 additions and 39 deletions.
101 changes: 62 additions & 39 deletions tools/netlist/netlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct VariableMemberAccess : public VariableSelectorBase {
/// of a variable symbol, with zero or more selectors applied.
class NetlistNode : public Node<NetlistNode, NetlistEdge> {
public:
NetlistNode() = default;
NetlistNode() : ID(++nextID) {};
void addElementSelect(const ConstantValue &index) {
selectors.emplace_back(std::make_unique<VariableElementSelect>(index));
}
Expand All @@ -93,13 +93,20 @@ class NetlistNode : public Node<NetlistNode, NetlistEdge> {
void addMemberAccess(string_view name) {
selectors.emplace_back(std::make_unique<VariableMemberAccess>(name));
}
void setName(string_view name) { name = name; }
string_view getName() { return name; }
private:
void setName(string_view newName) { name = newName; }
string_view getName() const { return name; }

public:
size_t ID;
string_view name;

private:
static size_t nextID;
std::vector<std::unique_ptr<VariableSelectorBase>> selectors;
};

size_t NetlistNode::nextID = 0;

class NetlistEdge : public DirectedEdge<NetlistNode, NetlistEdge> {
public:
NetlistEdge(NetlistNode &targetNode) : DirectedEdge(targetNode) {}
Expand Down Expand Up @@ -323,50 +330,52 @@ void printJson(Compilation& compilation, const std::string& fileName,
const std::vector<std::string>& scopes) {
JsonWriter writer;
writer.setPrettyPrint(true);

ASTSerializer serializer(compilation, writer);
if (scopes.empty()) {
serializer.serialize(compilation.getRoot());
}
else {
for (auto& scopeName : scopes) {
auto sym = compilation.getRoot().lookupName(scopeName);
if (sym)
serializer.serialize(*sym);
serializer.serialize(compilation.getRoot());
} else {
for (auto& scopeName : scopes) {
auto sym = compilation.getRoot().lookupName(scopeName);
if (sym) {
serializer.serialize(*sym);
}
}
}

writeToFile(fileName, writer.view());
}

void printDOT(const Netlist &netlist, const std::string &fileName) {
slang::FormatBuffer buffer;
buffer.append("digraph {\n");
for (auto &node : netlist) {
buffer.format(" N{} [label=\"{}\",shape=circle]\n", node->ID, node->getName());
}
for (auto &node : netlist) {
for (auto &edge : node->getEdges()) {
buffer.format(" {} -> {}", node->getName(), edge->getTargetNode());
buffer.format(" N{} -> N{}\n", node->getName(), edge->getTargetNode().getName());
}
}
buffer.append("}\n");
writeToFile(fileName, buffer.str());
writeToFile(fileName, string_view(buffer.data()));
}

template<typename Stream, typename String>
void writeToFile(Stream& os, string_view fileName, String contents) {
os.write(contents.data(), contents.size());
os.flush();
if (!os)
throw std::runtime_error(fmt::format("Unable to write AST to '{}'", fileName));
os.write(contents.data(), contents.size());
os.flush();
if (!os) {
throw std::runtime_error(fmt::format("Unable to write AST to '{}'", fileName));
}
}

void writeToFile(string_view fileName, string_view contents) {
if (fileName == "-") {
writeToFile(std::cout, "stdout", contents);
}
else {
std::ofstream file{std::string(fileName)};
writeToFile(file, fileName, contents);
}
std::cout<<"filename "<<fileName<<"\n";
if (fileName == "-") {
writeToFile(std::cout, "stdout", contents);
} else {
std::ofstream file{std::string(fileName)};
writeToFile(file, fileName, contents);
}
}

int main(int argc, char** argv) {
Expand Down Expand Up @@ -417,23 +426,37 @@ int main(int argc, char** argv) {
return 2;
}

bool ok = driver.parseAllSources();
try {

auto compilation = driver.createCompilation();
ok &= driver.reportCompilation(*compilation, quiet == true);
bool ok = driver.parseAllSources();

if (!ok) {
return ok;
}
auto compilation = driver.createCompilation();
ok &= driver.reportCompilation(*compilation, quiet == true);

if (astJsonFile) {
printJson(*compilation, *astJsonFile, astJsonScopes);
return 0;
}
if (!ok) {
return ok;
}

Netlist netlist;
UnrollVisitor visitor(*compilation, netlist);
compilation->getRoot().visit(visitor);
if (astJsonFile) {
printJson(*compilation, *astJsonFile, astJsonScopes);
return 0;
}

// Create the netlist by traversing the AST.
Netlist netlist;
UnrollVisitor visitor(*compilation, netlist);
compilation->getRoot().visit(visitor);
std::cout << "Netlist has " << netlist.size() << " nodes\n";

if (netlistDotFile) {
printDOT(netlist, *netlistDotFile);
return 0;
}

} catch (const std::exception& e) {
OS::printE(fmt::format("{}\n", e.what()));
return 3;
}

return 0;
}

0 comments on commit d49d29e

Please sign in to comment.