Commit a0aa59b5 authored by Peter's avatar Peter
Browse files

fixed

1. single file multiple module
parent db0babb1
Loading
Loading
Loading
Loading
Loading
+9 −62
Changes for src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java: 9 added lines, 62 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import hk.quantr.javalib.CommonLib;
import hk.quantr.verilogcompiler.antlr.VerilogLexer;
import hk.quantr.verilogcompiler.antlr.VerilogParser;
import hk.quantr.verilogcompiler.antlr.VerilogPreParser;
import hk.quantr.verilogcompiler.listener.AllModuleListener;
import hk.quantr.verilogcompiler.listener.SingleModuleListener;
import hk.quantr.verilogcompiler.listener.VerilogMacroDefinitionListener;
import hk.quantr.verilogcompiler.listener.preprocess.VerilogPreProcessorListener;
@@ -18,7 +17,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -27,7 +25,6 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.tree.ParseTree;
@@ -49,7 +46,7 @@ public class VerilogHelper {
//		return listener;
//	}
	public static VerilogMacroDefinitionListener scanMacroDefinitions(File preferFile, File folder) throws FileNotFoundException, IOException {
		logger.info(" --- scanMacroDefinitions " + preferFile + ", " + folder.getAbsolutePath());
//		logger.info(" --- scanMacroDefinitions " + preferFile + ", " + folder.getAbsolutePath());
		// scan all verilog for macro definitions
		logger.info(" --- listing folder " + folder.getAbsolutePath());
		Collection<File> temp = FileUtils.listFiles(folder, new String[]{"v"}, true);
@@ -140,6 +137,14 @@ public class VerilogHelper {
//							}
						} else {
							System.err.println("ERROR 2: " + macroText);
							// dump verilogPreListener.macroParameters
							for (String key : verilogPreListener.macroParameters.keySet()) {
								System.err.println("Macro: " + key + " Parameters: " + verilogPreListener.macroParameters.get(key));
							}
							// dump callingParameters
							for (int x = 0; x < callingParameters.length; x++) {
								System.err.println("Calling Parameter " + x + ": " + callingParameters[x]);
							}
							System.exit(124);
						}
					} else {
@@ -190,64 +195,6 @@ public class VerilogHelper {
		return CommonLib.objectToJson(outputs);
	}

//	public static hk.quantr.verilogcompiler.structure.Module parseTopModule(File folder, File topModuleFile) {
////		File topModuleFile = new File(folder.getAbsolutePath() + "/" + topModuleFile.getAbsolutePath());
//
//		Collection<File> files = FileUtils.listFiles(folder, new String[]{"v"}, true);
//		HashMap<String, hk.quantr.verilogcompiler.structure.Module> modules = new HashMap<>();
//		for (File file : files) {
//			try {
//				logger.log(Level.INFO, "parseTopModule(), processing {0}", file.getAbsolutePath());
//				String content = IOUtils.toString(new FileInputStream(file), "utf-8");
//				ParseStructure ps = new ParseStructure();
//				String processedContent = VerilogHelper.preProcess(file, null, null);
//				String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, file, processedContent, VerilogHelper.scanMacroDefinitions(file, folder), ps);
//				VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent));
//				CommonTokenStream tokenStream = new CommonTokenStream(lexer);
//				SingleModuleListener listener = new SingleModuleListener();
//				VerilogParser parser = new VerilogParser(tokenStream);
////				parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
//				parser.addParseListener(listener);
//				VerilogParser.Source_textContext context = parser.source_text();
//
//				if (listener.moduleName == null) {
//					continue;
//				}
//
//				modules.put(listener.moduleName, new hk.quantr.verilogcompiler.structure.Module(listener.moduleName, listener.inputs, listener.outputs));
//			} catch (IOException | RecognitionException ex) {
//				ex.printStackTrace();
//			}
//		}
//		logger.info("----");
//
//		// parse top module
//		try {
//			String content = IOUtils.toString(new FileInputStream(topModuleFile), "utf-8");
//			ParseStructure ps = new ParseStructure();
//			String processedContent = VerilogHelper.preProcess(topModuleFile, null, null);
//			String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, topModuleFile, processedContent, VerilogHelper.scanMacroDefinitions(topModuleFile, folder), ps);
//			VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent));
//			CommonTokenStream tokenStream = new CommonTokenStream(lexer);
//			AllModuleListener listener = new AllModuleListener(modules);
//			VerilogParser parser = new VerilogParser(tokenStream);
//			parser.addParseListener(listener);
//			VerilogParser.Source_textContext context = parser.source_text();
//
//			hk.quantr.verilogcompiler.structure.Module topModule = new hk.quantr.verilogcompiler.structure.Module();
//			topModule.name = listener.moduleName;
//			topModule.modules = listener.modules;
//			topModule.inputs = listener.inputs;
//			topModule.outputs = listener.outputs;
//			topModule.wires = listener.wires;
//
////			logger.info(topModule);
//			return topModule;
//		} catch (IOException ex) {
//			ex.printStackTrace();
//		}
//		return null;
//	}
	public static hk.quantr.verilogcompiler.structure.Module fileToModule(File file) throws IOException {
		String content = IOUtils.toString(new FileInputStream(file), "utf-8");
		ParseStructure ps = new ParseStructure();
+299 −193

File changed.

Preview size limit exceeded, changes collapsed.

+336 −213

File changed.

Preview size limit exceeded, changes collapsed.

+186 −27
Changes for src/main/java/hk/quantr/verilogcompiler/antlr/VerilogPreParser.g4: 186 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging

parser grammar VerilogPreParser;

options {
@@ -36,8 +39,8 @@ compiler_directive
    : begin_keywords_directive
    | celldefine_directive
    | default_nettype_directive
	| endcelldefine_directive
    | end_keywords_directive
    | endcelldefine_directive
    | ifdef_directive
    | ifndef_directive
    | include_directive
@@ -52,29 +55,185 @@ compiler_directive
    | undef_directive
    ;

begin_keywords_directive : GA BEGIN_KEYWORDS_DIRECTIVE DIRECTIVE_TEXT ;
celldefine_directive : GA CELLDEFINE_DIRECTIVE ;
default_nettype_directive : GA DEFAULT_NETTYPE_DIRECTIVE DIRECTIVE_TEXT ;
endcelldefine_directive : GA ENDCELLDEFINE_DIRECTIVE ;
end_keywords_directive : GA END_KEYWORDS_DIRECTIVE ;
ifdef_directive : GA IFDEF_DIRECTIVE text_macro_identifier ifdef_group_of_lines elsif_directive* else_directive? endif_directive ;
ifndef_directive : GA IFNDEF_DIRECTIVE text_macro_identifier ifndef_group_of_lines elsif_directive* else_directive? endif_directive ;
include_directive : GA INCLUDE_DIRECTIVE DIRECTIVE_TEXT ;
line_directive : GA LINE_DIRECTIVE DIRECTIVE_TEXT ;
nounconnected_drive_directive : GA NOUNCONNECTED_DRIVE_DIRECTIVE ;
pragma_directive : GA PRAGMA_DIRECTIVE DIRECTIVE_TEXT ;
resetall_directive : GA RESETALL_DIRECTIVE ;
text_macro_definition : GA DEFINE_DIRECTIVE text_macro_identifier macro_text ;
text_macro_usage : GA MACRO_USAGE ;
timescale_directive : GA TIMESCALE_DIRECTIVE  DIRECTIVE_TEXT ;
unconnected_drive_directive : GA UNCONNECTED_DRIVE_DIRECTIVE DIRECTIVE_TEXT ;
undef_directive : GA UNDEF_DIRECTIVE text_macro_identifier ;
elsif_directive : GA ELSIF_DIRECTIVE text_macro_identifier elsif_group_of_lines ;
else_directive : GA ELSE_DIRECTIVE else_group_of_lines ;
endif_directive : GA ENDIF_DIRECTIVE ;
text_macro_identifier : DIRECTIVE_IDENTIFIER ;
ifdef_group_of_lines : ( SOURCE_TEXT | compiler_directive )* ;
ifndef_group_of_lines : ( SOURCE_TEXT | compiler_directive )* ;
elsif_group_of_lines : ( SOURCE_TEXT | compiler_directive )* ;
else_group_of_lines : ( SOURCE_TEXT | compiler_directive )* ;
macro_text : ( MACRO_TEXT | MACRO_ESC_NEWLINE )* ;
 No newline at end of file
begin_keywords_directive
    : GA BEGIN_KEYWORDS_DIRECTIVE DQ version_specifier DQ
    ;

celldefine_directive
    : GA CELLDEFINE_DIRECTIVE
    ;

default_nettype_directive
    : GA DEFAULT_NETTYPE_DIRECTIVE default_nettype_value
    ;

default_nettype_value
    : DEFAULT_NETTYPE_VALUE
    ;

else_directive
    : GA ELSE_DIRECTIVE group_of_lines
    ;

elsif_directive
    : GA ELSIF_DIRECTIVE macro_identifier group_of_lines
    ;

end_keywords_directive
    : GA END_KEYWORDS_DIRECTIVE
    ;

endcelldefine_directive
    : GA ENDCELLDEFINE_DIRECTIVE
    ;

endif_directive
    : GA ENDIF_DIRECTIVE
    ;

filename
    : FILENAME
    ;

group_of_lines
    : (source_text_ | compiler_directive)*
    ;

identifier
    : SIMPLE_IDENTIFIER
    ;

ifdef_directive
    : GA IFDEF_DIRECTIVE macro_identifier group_of_lines elsif_directive* else_directive? endif_directive
    ;

ifndef_directive
    : GA IFNDEF_DIRECTIVE macro_identifier group_of_lines elsif_directive* else_directive? endif_directive
    ;

include_directive
    : GA INCLUDE_DIRECTIVE DQ filename DQ
    ;

level
    : UNSIGNED_NUMBER
    ;

line_directive
    : GA LINE_DIRECTIVE number DQ filename DQ level
    ;

macro_delimiter
    : MACRO_DELIMITER
    ;

macro_esc_newline
    : MACRO_ESC_NEWLINE
    ;

macro_esc_quote
    : MACRO_ESC_QUOTE
    ;

macro_identifier
    : MACRO_IDENTIFIER
    ;

macro_name
    : MACRO_NAME
    ;

macro_quote
    : MACRO_QUOTE
    ;

macro_text
    : (macro_text_ | macro_delimiter | macro_esc_newline | macro_esc_quote | macro_quote | string_)*
    ;

macro_text_
    : MACRO_TEXT
    ;

macro_usage
    : MACRO_USAGE
    ;

nounconnected_drive_directive
    : GA NOUNCONNECTED_DRIVE_DIRECTIVE
    ;

number
    : UNSIGNED_NUMBER
    ;

pragma_directive
    : GA PRAGMA_DIRECTIVE pragma_name (pragma_expression ( CO pragma_expression)*)?
    ;

pragma_expression
    : (pragma_keyword EQ)? pragma_value
    ;

pragma_keyword
    : SIMPLE_IDENTIFIER
    ;

pragma_name
    : SIMPLE_IDENTIFIER
    ;

pragma_value
    : LP pragma_expression (CO pragma_expression)* RP
    | number
    | string_
    | identifier
    ;

resetall_directive
    : GA RESETALL_DIRECTIVE
    ;

source_text_
    : SOURCE_TEXT
    ;

string_
    : STRING
    ;

text_macro_definition
    : GA DEFINE_DIRECTIVE macro_name macro_text
    ;

text_macro_usage
    : GA macro_usage
    ;

time_precision
    : TIME_VALUE TIME_UNIT
    ;

time_unit
    : TIME_VALUE TIME_UNIT
    ;

timescale_directive
    : GA TIMESCALE_DIRECTIVE time_unit SL time_precision
    ;

unconnected_drive_directive
    : GA UNCONNECTED_DRIVE_DIRECTIVE unconnected_drive_value
    ;

unconnected_drive_value
    : UNCONNECTED_DRIVE_VALUE
    ;

undef_directive
    : GA UNDEF_DIRECTIVE macro_identifier
    ;

version_specifier
    : VERSION_SPECIFIER
    ;
 No newline at end of file
+77 −5
Changes for src/main/java/hk/quantr/verilogcompiler/listener/AllModuleListener.java: 77 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -4,14 +4,22 @@ import hk.quantr.verilogcompiler.antlr.VerilogParser;
import hk.quantr.verilogcompiler.antlr.VerilogParser.Module_instanceContext;
import hk.quantr.verilogcompiler.antlr.VerilogParser.Named_port_connectionContext;
import hk.quantr.verilogcompiler.antlr.VerilogParser.Port_identifierContext;
import hk.quantr.verilogcompiler.antlr.VerilogParser.Var_port_idContext;
import hk.quantr.verilogcompiler.antlr.VerilogParserBaseListener;
import hk.quantr.verilogcompiler.structure.Port;
import hk.quantr.verilogcompiler.structure.Wire;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.io.FileUtils;

/**
 *
@@ -25,10 +33,64 @@ public class AllModuleListener extends VerilogParserBaseListener {
	public LinkedHashMap<String, hk.quantr.verilogcompiler.structure.Module> modules = new LinkedHashMap<>();

	private HashMap<String, hk.quantr.verilogcompiler.structure.Module> supportingModule;
	private static HashMap<String, hk.quantr.verilogcompiler.structure.Module> predefinedModules = null;
	public String moduleName;

	public AllModuleListener(HashMap<String, hk.quantr.verilogcompiler.structure.Module> modules) {
		this.supportingModule = modules;
		loadPredefinedModules();
	}

	private static synchronized void loadPredefinedModules() {
		if (predefinedModules != null) {
			return;
		}

		predefinedModules = new HashMap<>();

		try {
			// Try to find predefinedModules.json in current directory or common locations
			File jsonFile = new File("predefinedModules.json");

			if (jsonFile.exists()) {
				String jsonContent = FileUtils.readFileToString(jsonFile, "utf-8");
				Gson gson = new Gson();
				Type listType = new TypeToken<List<Map<String, Object>>>() {
				}.getType();
				List<Map<String, Object>> moduleList = gson.fromJson(jsonContent, listType);

				for (Map<String, Object> moduleData : moduleList) {
					String name = (String) moduleData.get("name");
					List<String> inputNames = (List<String>) moduleData.get("inputs");
					List<String> outputNames = (List<String>) moduleData.get("outputs");

					ArrayList<Port> inputs = new ArrayList<>();
					ArrayList<Port> outputs = new ArrayList<>();

					if (inputNames != null) {
						for (String inputName : inputNames) {
							inputs.add(new Port("input", null, null, inputName));
						}
					}

					if (outputNames != null) {
						for (String outputName : outputNames) {
							outputs.add(new Port("output", null, null, outputName));
						}
					}

					hk.quantr.verilogcompiler.structure.Module module
							= new hk.quantr.verilogcompiler.structure.Module(name, inputs, outputs);
					predefinedModules.put(name, module);
					System.out.println("Loaded predefined module: " + name);
				}
			} else {
				System.out.println("predefinedModules.json not found, no predefined modules loaded");
			}
		} catch (IOException ex) {
			System.err.println("Error loading predefinedModules.json: " + ex.getMessage());
			ex.printStackTrace();
		}
	}

	@Override
@@ -54,9 +116,9 @@ public class AllModuleListener extends VerilogParserBaseListener {
			}
		}
		if (ctx.list_of_variable_port_identifiers() != null) {
			List<Port_identifierContext> list = ctx.list_of_variable_port_identifiers().port_identifier();
			for (Port_identifierContext portCtx : list) {
				outputs.add(new Port("output", type, range, portCtx.getText()));
			List<Var_port_idContext> list = ctx.list_of_variable_port_identifiers().var_port_id();
			for (Var_port_idContext varPort : list) {
				outputs.add(new Port("output", type, range, varPort.port_identifier().getText()));
			}
		}
	}
@@ -74,7 +136,7 @@ public class AllModuleListener extends VerilogParserBaseListener {
		String wireName = ctx.list_of_net_identifiers() == null ? "null" : /*CommonLib.randomString(6, true, false, false) + "_" + */ ctx.list_of_net_identifiers().getText();
//		System.out.println("wireName=" + wireName);
		String wireType = ctx.range_() == null ? "null" : ctx.range_().getText();
		System.out.println(wireName);
//		System.out.println(wireName);
		wires.put(wireName, new Wire(wireName, wireType));
//		} catch (OptionErrorException ex) {
//			Logger.getLogger(AllModuleListener.class.getName()).log(Level.SEVERE, null, ex);
@@ -84,10 +146,18 @@ public class AllModuleListener extends VerilogParserBaseListener {
	@Override
	public void exitModule_instantiation(VerilogParser.Module_instantiationContext ctx) {
		moduleName = ctx.module_identifier().getText();
		System.out.println("exitModule_instantiation=" + moduleName);
		try {
			for (Module_instanceContext module_instance : ctx.module_instance()) {
				System.out.println("moduleName=" + moduleName);
				hk.quantr.verilogcompiler.structure.Module module = supportingModule.get(moduleName);
				if (module == null) {
					// Try to get from predefined modules
					module = predefinedModules.get(moduleName);
					if (module == null) {
						System.err.println("Warning: Module '" + moduleName + "' not found in supporting modules or predefinedModules.json");
						continue;
					}
				}
				modules.put(module.name, module);

//				System.out.println("++" + ctx.module_identifier().getText() + ",\t" + module_instance.name_of_instance().getText() + ",\t" + module_instance.list_of_port_connections().getText());
@@ -113,4 +183,6 @@ public class AllModuleListener extends VerilogParserBaseListener {
			System.exit(1);
		}
	}
	
	
}
Loading