1 package net.mograsim.logic.model.verilog.model;
3 import java.util.HashSet;
5 import java.util.Objects;
7 import java.util.stream.Collectors;
9 import net.mograsim.logic.model.verilog.model.signals.IOPort;
10 import net.mograsim.logic.model.verilog.model.signals.Signal;
11 import net.mograsim.logic.model.verilog.model.statements.Statement;
13 public class VerilogComponentImplementation
15 private final VerilogComponentDeclaration declaration;
16 private final List<Statement> statements;
18 public VerilogComponentImplementation(VerilogComponentDeclaration declaration, List<Statement> statements)
20 this.declaration = Objects.requireNonNull(declaration);
21 this.statements = List.copyOf(statements);
28 Set<String> usedNames = declaration.getIOPorts().stream().map(IOPort::getName).collect(Collectors.toCollection(HashSet::new));
30 for (Statement statement : statements)
31 for (String definedName : statement.getDefinedNames())
32 if (!usedNames.add(definedName))
33 throw new IllegalArgumentException("Name occurs twice: " + definedName);
35 Set<Signal> allSignals = new HashSet<>();
36 allSignals.addAll(declaration.getIOPorts());
37 statements.stream().map(Statement::getDefinedSignals).forEach(allSignals::addAll);
39 // do two passes, a signal may be referenced before it is defined
40 for (Statement statement : statements)
41 if (!allSignals.containsAll(statement.getReferencedSignals()))
42 throw new IllegalArgumentException("Referenced an unknown signal: "
43 + statement.getReferencedSignals().stream().filter(s -> !allSignals.contains(s)).findAny().get());
46 public VerilogComponentDeclaration getDeclaration()
51 public List<Statement> getStatements()
56 public String toVerilogCode()
58 StringBuilder sb = new StringBuilder();
60 sb.append("module " + declaration.getID());
61 sb.append(declaration.getIOPorts().stream().map(IOPort::toDeclarationVerilogCode).collect(Collectors.joining(", ", "(", ")")));
64 for (Statement statement : statements)
65 sb.append(statement.toVerilogCode() + "\n");
66 if (!statements.isEmpty())
69 sb.append("endmodule\n");
75 public String toString()
77 return "Implementation[" + declaration.getID() + "]";
85 result = prime * result + ((declaration == null) ? 0 : declaration.hashCode());
86 result = prime * result + ((statements == null) ? 0 : statements.hashCode());
91 public boolean equals(Object obj)
97 if (getClass() != obj.getClass())
99 VerilogComponentImplementation other = (VerilogComponentImplementation) obj;
100 if (declaration == null)
102 if (other.declaration != null)
104 } else if (!declaration.equals(other.declaration))
106 if (statements == null)
108 if (other.statements != null)
110 } else if (!statements.equals(other.statements))