+2
−2
| Original line number | Diff line number | Diff line |
|---|---|---|
package hk.quantr.verilogcompiler.listener;
|
||
import hk.quantr.verilogcompiler.antlr.VerilogParser;
|
||
import hk.quantr.verilogcompiler.antlr.VerilogParserBaseListener;
|
||
import hk.quantr.verilogcompiler.structure.Module;
|
||
import hk.quantr.verilogcompiler.structure.Port;
|
||
import hk.quantr.verilogcompiler.structure.Wire;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.logging.Logger;
|
||
/**
|
||
* Listener to extract wire declarations and their connections from Verilog modules
|
||
*
|
||
* @author Peter <[email protected]>
|
||
*/
|
||
public class WireListener extends VerilogParserBaseListener {
|
||
// Map of module name to list of wires in that module
|
||
public HashMap<String, ArrayList<Wire>> moduleWires = new HashMap<>();
|
||
|
||
// Map of wire name to Wire object for connection tracking
|
||
private HashMap<String, Wire> wiresByName = new HashMap<>();
|
||
|
||
// Available module definitions for port lookup
|
||
private HashMap<String, Module> availableModules;
|
||
|
||
private String currentModuleName = null;
|
||
private ArrayList<Wire> currentWires = new ArrayList<>();
|
||
private ArrayList<Port> currentModuleInputs = new ArrayList<>();
|
||
private ArrayList<Port> currentModuleOutputs = new ArrayList<>();
|
||
private static Logger logger = Logger.getLogger(WireListener.class.getName());
|
||
|
||
public WireListener(HashMap<String, Module> availableModules) {
|
||
this.availableModules = availableModules != null ? availableModules : new HashMap<>();
|
||
}
|
||
@Override
|
||
public void enterModule_declaration(VerilogParser.Module_declarationContext ctx) {
|
||
// Reset for new module
|
||
currentWires = new ArrayList<>();
|
||
wiresByName = new HashMap<>();
|
||
currentModuleInputs = new ArrayList<>();
|
||
currentModuleOutputs = new ArrayList<>();
|
||
currentModuleName = null;
|
||
}
|
||
@Override
|
||
public void exitModule_identifier(VerilogParser.Module_identifierContext ctx) {
|
||
// Capture module name when we encounter the module identifier
|
||
if (currentModuleName == null) {
|
||
currentModuleName = ctx.getText();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void exitInput_declaration(VerilogParser.Input_declarationContext ctx) {
|
||
// Capture input ports of the current module
|
||
String type = ctx.net_type() == null ? null : ctx.net_type().getText();
|
||
String range = ctx.range_() == null ? null : ctx.range_().getText();
|
||
|
||
List<VerilogParser.Port_identifierContext> list = ctx.list_of_port_identifiers().port_identifier();
|
||
for (VerilogParser.Port_identifierContext portCtx : list) {
|
||
Port port = new Port("input", type, range, portCtx.getText());
|
||
currentModuleInputs.add(port);
|
||
logger.info("Found input port in module " + currentModuleName + ": " + portCtx.getText());
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void exitOutput_declaration(VerilogParser.Output_declarationContext ctx) {
|
||
// Capture output ports of the current module
|
||
String type = ctx.net_type() == null ? null : ctx.net_type().getText();
|
||
String range = ctx.range_() == null ? null : ctx.range_().getText();
|
||
|
||
if (ctx.list_of_port_identifiers() != null) {
|
||
List<VerilogParser.Port_identifierContext> list = ctx.list_of_port_identifiers().port_identifier();
|
||
for (VerilogParser.Port_identifierContext portCtx : list) {
|
||
Port port = new Port("output", type, range, portCtx.getText());
|
||
currentModuleOutputs.add(port);
|
||
logger.info("Found output port in module " + currentModuleName + ": " + portCtx.getText());
|
||
}
|
||
}
|
||
if (ctx.list_of_variable_port_identifiers() != null) {
|
||
List<VerilogParser.Var_port_idContext> list = ctx.list_of_variable_port_identifiers().var_port_id();
|
||
for (VerilogParser.Var_port_idContext varPort : list) {
|
||
Port port = new Port("output", type, range, varPort.port_identifier().getText());
|
||
currentModuleOutputs.add(port);
|
||
logger.info("Found output port in module " + currentModuleName + ": " + varPort.port_identifier().getText());
|
||
}
|
||
}
|
||
}
|
||
@Override
|
||
public void exitNet_declaration(VerilogParser.Net_declarationContext ctx) {
|
||
// Parse wire declarations
|
||
String netType = ctx.net_type() != null ? ctx.net_type().getText() : "wire";
|
||
String range = ctx.range_() != null ? ctx.range_().getText() : null;
|
||
|
||
// Handle list_of_net_identifiers (wire declarations without assignments)
|
||
if (ctx.list_of_net_identifiers() != null) {
|
||
List<VerilogParser.Net_idContext> netIds = ctx.list_of_net_identifiers().net_id();
|
||
for (VerilogParser.Net_idContext netId : netIds) {
|
||
String wireName = netId.net_identifier().getText();
|
||
Wire wire = new Wire(wireName, range);
|
||
currentWires.add(wire);
|
||
wiresByName.put(wireName, wire);
|
||
logger.info("Found wire in module " + currentModuleName + ": " + wireName +
|
||
(range != null ? " " + range : ""));
|
||
}
|
||
}
|
||
|
||
// Handle list_of_net_decl_assignments (wire declarations with assignments)
|
||
if (ctx.list_of_net_decl_assignments() != null) {
|
||
List<VerilogParser.Net_decl_assignmentContext> netDecls =
|
||
ctx.list_of_net_decl_assignments().net_decl_assignment();
|
||
for (VerilogParser.Net_decl_assignmentContext netDecl : netDecls) {
|
||
String wireName = netDecl.net_identifier().getText();
|
||
Wire wire = new Wire(wireName, range);
|
||
currentWires.add(wire);
|
||
wiresByName.put(wireName, wire);
|
||
logger.info("Found wire (with assignment) in module " + currentModuleName +
|
||
": " + wireName + (range != null ? " " + range : ""));
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void exitModule_instantiation(VerilogParser.Module_instantiationContext ctx) {
|
||
String instantiatedModuleName = ctx.module_identifier().getText();
|
||
logger.info("Found module instantiation in " + currentModuleName + ": " + instantiatedModuleName);
|
||
|
||
try {
|
||
// Look up the module definition
|
||
Module moduleDefinition = availableModules.get(instantiatedModuleName);
|
||
if (moduleDefinition == null) {
|
||
logger.info("Warning: Module definition '" + instantiatedModuleName + "' not found");
|
||
return;
|
||
}
|
||
|
||
// Process each instance of this module
|
||
for (VerilogParser.Module_instanceContext moduleInstance : ctx.module_instance()) {
|
||
String instanceName = moduleInstance.name_of_module_instance().getText();
|
||
|
||
// Process named port connections
|
||
if (moduleInstance.list_of_port_connections() != null &&
|
||
moduleInstance.list_of_port_connections().named_port_connection() != null) {
|
||
|
||
for (VerilogParser.Named_port_connectionContext namedPort :
|
||
moduleInstance.list_of_port_connections().named_port_connection()) {
|
||
|
||
String portName = namedPort.port_identifier().getText();
|
||
|
||
// Get the expression (usually the wire name)
|
||
if (namedPort.expression() != null) {
|
||
String signalName = namedPort.expression().getText();
|
||
|
||
// Find or create the wire/signal
|
||
Wire wire = wiresByName.get(signalName);
|
||
boolean isNewImplicitWire = false;
|
||
Port topModulePort = null;
|
||
|
||
if (wire == null) {
|
||
// Check if it's an input/output port of the current module
|
||
Port modulePort = findPortInModule(signalName);
|
||
if (modulePort != null) {
|
||
// Create an implicit wire for this module port
|
||
wire = new Wire(signalName, modulePort.range);
|
||
wiresByName.put(signalName, wire);
|
||
currentWires.add(wire);
|
||
isNewImplicitWire = true;
|
||
topModulePort = modulePort;
|
||
logger.info(" Created implicit wire for module port: " + signalName);
|
||
} else {
|
||
// Create an implicit wire for undeclared signal
|
||
wire = new Wire(signalName, null);
|
||
wiresByName.put(signalName, wire);
|
||
currentWires.add(wire);
|
||
logger.info(" Created implicit wire for undeclared signal: " + signalName);
|
||
}
|
||
}
|
||
|
||
// If this is a new implicit wire from a top module port, add the top module port connection
|
||
if (isNewImplicitWire && topModulePort != null) {
|
||
// Create a pseudo-module to represent the current (parent) module
|
||
Module currentModuleObj = availableModules.get(currentModuleName);
|
||
if (currentModuleObj == null) {
|
||
// Create a minimal module representation for the current module
|
||
currentModuleObj = new Module(currentModuleName, currentModuleInputs, currentModuleOutputs);
|
||
}
|
||
|
||
// Create a copy of the port with module info
|
||
Port topPortCopy = new Port(topModulePort.direction, topModulePort.type, topModulePort.range, topModulePort.name);
|
||
topPortCopy.module = currentModuleObj;
|
||
|
||
// Add the top module port connection
|
||
wire.ports.add(topPortCopy);
|
||
wire.modules.add(currentModuleObj);
|
||
|
||
logger.info(" Added top module port connection for: " + signalName + " (" + topModulePort.direction + ")");
|
||
}
|
||
|
||
// Find the port in the instantiated module definition
|
||
Port port = moduleDefinition.getPort(portName);
|
||
if (port != null) {
|
||
// Create a copy of the port with module info
|
||
Port portCopy = new Port(port.direction, port.type, port.range, port.name);
|
||
portCopy.module = moduleDefinition;
|
||
|
||
// Add connection to wire
|
||
wire.ports.add(portCopy);
|
||
wire.modules.add(moduleDefinition);
|
||
|
||
logger.info(" Connected signal '" + signalName + "' to " +
|
||
instantiatedModuleName + "." + portName + " (" + port.direction + ")");
|
||
} else {
|
||
logger.info(" Warning: Port '" + portName + "' not found in module '" +
|
||
instantiatedModuleName + "'");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception ex) {
|
||
System.err.println("Error processing module instantiation: " + ex.getMessage());
|
||
ex.printStackTrace();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Find a port in the current module's input/output ports
|
||
*/
|
||
private Port findPortInModule(String portName) {
|
||
for (Port port : currentModuleInputs) {
|
||
if (port.name.equals(portName)) {
|
||
return port;
|
||
}
|
||
}
|
||
for (Port port : currentModuleOutputs) {
|
||
if (port.name.equals(portName)) {
|
||
return port;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
@Override
|
||
public void exitModule_declaration(VerilogParser.Module_declarationContext ctx) {
|
||
// Store wires for this module
|
||
if (currentModuleName != null) {
|
||
if (!moduleWires.containsKey(currentModuleName)) {
|
||
moduleWires.put(currentModuleName, new ArrayList<>());
|
||
}
|
||
moduleWires.get(currentModuleName).addAll(currentWires);
|
||
logger.info("Module " + currentModuleName + " has " + currentWires.size() + " wires");
|
||
}
|
||
}
|
||
} |
Loading