Loading aaaa.v +753 −139 File changed.Preview size limit exceeded, changes collapsed. Show changes nbactions.xml +3 −3 Changes for nbactions.xml: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ <properties> <exec.vmArgs></exec.vmArgs> <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> <exec.mainClass>hk.quantr.verilogcompiler.VerilogCompiler</exec.mainClass> <exec.executable>java</exec.executable> </properties> Loading @@ -29,7 +29,7 @@ <properties> <exec.vmArgs>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</exec.vmArgs> <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> <exec.mainClass>hk.quantr.verilogcompiler.VerilogCompiler</exec.mainClass> <exec.executable>java</exec.executable> <jpda.listen>true</jpda.listen> Loading @@ -49,7 +49,7 @@ <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.mainClass>${packageClassName}</exec.mainClass> <exec.executable>java</exec.executable> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> </properties> </action> <action> Loading src/main/java/hk/quantr/verilogcompiler/VerilogCompiler.java +3 −2 Changes for src/main/java/hk/quantr/verilogcompiler/VerilogCompiler.java: 3 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ package hk.quantr.verilogcompiler; import hk.quantr.javalib.CommonLib; import hk.quantr.javalib.PropertyUtil; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; Loading Loading @@ -152,8 +153,8 @@ public class VerilogCompiler { String temp = cmd.getOptionValue("m"); File file = new File(temp); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), ps); System.out.println(sourceContent); } } Loading src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java +76 −94 Changes for src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java: 76 added lines, 94 removed lines. Original line number Diff line number Diff line Loading @@ -10,16 +10,14 @@ import hk.quantr.verilogcompiler.listener.VerilogMacroDefinitionListener; import hk.quantr.verilogcompiler.listener.preprocess.VerilogPreProcessorListener; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.MacroListing.Type; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; Loading Loading @@ -66,11 +64,11 @@ public class VerilogHelper { return verilogPreListener; } public static String getContentReplacedWithMacro(String content, VerilogMacroDefinitionListener verilogPreListener, ArrayList<MacroListing> macroListing) throws FileNotFoundException, IOException { public static String getContentReplacedWithMacro(int parseNo, String content, VerilogMacroDefinitionListener verilogPreListener, ParseStructure ps) throws FileNotFoundException, IOException { String lines[] = content.split("\r?\n"); if (macroListing.size() == 0) { if (ps.macroListing.isEmpty()) { for (int x = 0; x < lines.length; x++) { macroListing.add(new MacroListing(x, lines[x], Type.ORIGINAL, null)); ps.macroListing.add(new MacroListing(x, lines[x], Type.ORIGINAL)); } } Loading @@ -79,6 +77,7 @@ public class VerilogHelper { tokenStream.fill(); TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); VerilogMacroDefinitionListener preListener = getPreListener(content); CommonLib.printWithLineNumber(content); for (Token token : preListener.macroUsageTokens) { String macroText = token.getText(); String macroName = macroText.replaceAll("\\(.*", ""); Loading @@ -97,29 +96,25 @@ public class VerilogHelper { String callingParamter = callingParameters[x]; macroBody = macroBody.replaceAll("\\b" + parameter + "\\b", callingParamter); } // int macroLineNo = macroListing.get(currentLineNo).secondLineNo; // for (String temp : macroBody.split("\r?\n")) { // macroListing.add(new MacroListing(token.getLine() - 1, macroLineNo + r, temp, Type.MACRO, uuid)); // r++; // } // add to macroListing int currentLineNo = 0; for (int z = macroListing.size() - 1; z >= 0; z--) { if (macroListing.get(z).lineNo == token.getLine() - 1) { currentLineNo = z; break; } } int lineNo = token.getLine() - 1; MacroListing parentMacro = ps.get(lineNo, parseNo); // macroListing.add(z + 1, new MacroListing(token.getLine() - 1, macroBody, Type.MACRO)); int r = 0; String uuid = UUID.randomUUID().toString(); int macroLineNo = macroListing.get(currentLineNo).secondLineNo; for (String temp : macroBody.split("\r?\n")) { macroListing.add(new MacroListing(token.getLine() - 1, macroLineNo + r, temp, Type.MACRO, uuid)); r++; if (parentMacro.lineNo == 194) { System.out.println(""); } for (int q = currentLineNo + 1; q < macroListing.size(); q++) { if (macroListing.get(q).type == Type.ORIGINAL) { macroListing.get(q).secondLineNo += r - 1; if (parentMacro == null) { System.err.println("ERROR"); System.out.println(ps); System.exit(125); } for (String temp : macroBody.split("\r?\n")) { parentMacro.children.add(new MacroListing(parentMacro.lineNo, temp, Type.MACRO)); } // end add to macroListing Loading Loading @@ -150,46 +145,49 @@ public class VerilogHelper { String result = rewriter.getText(); if (!result.equals(content)) { result = getContentReplacedWithMacro(result, verilogPreListener, macroListing); System.out.println(ps); System.out.println("+".repeat(100)); CommonLib.printWithLineNumber(result); System.out.println("+".repeat(100)); result = getContentReplacedWithMacro(parseNo + 1, result, verilogPreListener, ps); } return result; } public static String getContentReplacedWithMacroWithoutReplaceFunction(String content, VerilogMacroDefinitionListener verilogPreListener) throws FileNotFoundException, IOException { VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(content)); CommonTokenStream tokenStream = new CommonTokenStream(lexer, VerilogLexer.DIRECTIVES); tokenStream.fill(); TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); VerilogMacroDefinitionListener preListener = getPreListener(content); for (Token token : preListener.macroUsageTokens) { String macroText = token.getText(); String macroName = macroText.replaceAll("\\(.*", ""); if (preListener.macroUsageTokens.contains(token) && verilogPreListener.macroDefinitions.get(macroName) != null) { String macroBody = verilogPreListener.macroDefinitions.get(macroName); // logger.info("\treplacing " + macroName); // rewriter.replace(token, macroBody); // rewriter.delete(token.getTokenIndex() - 1); // if (verilogPreListener.macroUsageTypes.get(macroName)) { // rewriter.delete(token.getTokenIndex() + 1); // remove last ';' // public static String getContentReplacedWithMacroWithoutReplaceFunction(int parseNo, String content, VerilogMacroDefinitionListener verilogPreListener) throws FileNotFoundException, IOException { // VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(content)); // CommonTokenStream tokenStream = new CommonTokenStream(lexer, VerilogLexer.DIRECTIVES); // tokenStream.fill(); // TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); // VerilogMacroDefinitionListener preListener = getPreListener(content); // for (Token token : preListener.macroUsageTokens) { // String macroText = token.getText(); // String macroName = macroText.replaceAll("\\(.*", ""); // if (preListener.macroUsageTokens.contains(token) && verilogPreListener.macroDefinitions.get(macroName) != null) { // String macroBody = verilogPreListener.macroDefinitions.get(macroName); //// logger.info("\treplacing " + macroName); //// rewriter.replace(token, macroBody); //// rewriter.delete(token.getTokenIndex() - 1); //// if (verilogPreListener.macroUsageTypes.get(macroName)) { //// rewriter.delete(token.getTokenIndex() + 1); // remove last ';' //// } // // if (verilogPreListener.macroParameters.get(macroName) != null) { // for (String parameter : verilogPreListener.macroParameters.get(macroName)) { // rewriter.insertAfter(token, ";\n$display(" + parameter + ")"); // } // } // //// logger.info(token + " \t = \t" + verilogPreListener.macroDefinitions.get(macroName)); // } // } // String result = rewriter.getText(); // if (!result.equals(content)) { // ArrayList<MacroListing> macroListing = new ArrayList<>(); // result = getContentReplacedWithMacro(parseNo + 1, result, verilogPreListener); // } // return result; // } if (verilogPreListener.macroParameters.get(macroName) != null) { for (String parameter : verilogPreListener.macroParameters.get(macroName)) { rewriter.insertAfter(token, ";\n$display(" + parameter + ")"); } } // logger.info(token + " \t = \t" + verilogPreListener.macroDefinitions.get(macroName)); } } String result = rewriter.getText(); if (!result.equals(content)) { ArrayList<MacroListing> macroListing = new ArrayList<>(); result = getContentReplacedWithMacro(result, verilogPreListener, macroListing); } return result; } public static String verilogToJson(File file) throws FileNotFoundException, IOException { if (!(file.isFile() && file.exists())) { logger.log(Level.SEVERE, "verilog file/folder " + file.getAbsolutePath() + " not exist"); Loading @@ -211,8 +209,8 @@ public class VerilogHelper { } public static hk.quantr.verilogcompiler.structure.Module parseVerilogForOutput(String content, File folder) throws IOException { ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); SingleModuleListener listener = new SingleModuleListener(); Loading @@ -235,8 +233,8 @@ public class VerilogHelper { try { logger.info("parseTopModule(), processing " + file.getAbsolutePath()); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); SingleModuleListener listener = new SingleModuleListener(); Loading @@ -259,8 +257,8 @@ public class VerilogHelper { // parse top module try { String content = IOUtils.toString(new FileInputStream(topModuleFile), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); AllModuleListener listener = new AllModuleListener(modules); Loading @@ -284,9 +282,11 @@ public class VerilogHelper { } public static String preProcess(File file) throws FileNotFoundException, FileNotFoundException, IOException { ParseStructure ps = new ParseStructure(); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), macroListing); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), ps); System.out.println(" final ".repeat(30)); System.out.println(ps); FileUtils.writeStringToFile(new File("aaaa.v"), sourceContent, "utf-8"); CommonLib.printWithLineNumber(sourceContent); System.out.println("+".repeat(100)); Loading @@ -294,40 +294,22 @@ public class VerilogHelper { CommonTokenStream tokenStream = new CommonTokenStream(lexer); VerilogParser parser = new VerilogParser(tokenStream); ParseTree tree = parser.source_text(); Collections.sort(macroListing, (MacroListing o1, MacroListing o2) -> { if (o1.lineNo == o2.lineNo) { return Integer.valueOf(o1.secondLineNo).compareTo(Integer.valueOf(o2.secondLineNo)); } return Integer.valueOf(o1.lineNo).compareTo(Integer.valueOf(o2.lineNo)); }); for (MacroListing m : macroListing) { System.out.println(m); } System.out.println("-".repeat(100)); VerilogPreProcessorListener listener = new VerilogPreProcessorListener(tokenStream, sourceContent, macroListing); VerilogPreProcessorListener listener = new VerilogPreProcessorListener(tokenStream, sourceContent, ps); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(listener, tree); StringBuilder sb = new StringBuilder(); Collections.sort(macroListing, (MacroListing o1, MacroListing o2) -> { if (o1.lineNo == o2.lineNo) { return Integer.valueOf(o1.secondLineNo).compareTo(Integer.valueOf(o2.secondLineNo)); } return Integer.valueOf(o1.lineNo).compareTo(Integer.valueOf(o2.lineNo)); }); for (MacroListing m : macroListing) { System.out.println(m); if (m.type == Type.ORIGINAL || m.type == Type.PROFILING) { sb.append(m.line); sb.append("\n"); } } // for (MacroListing m : macroListing) { // System.out.println(m); // if (m.type == Type.ORIGINAL || m.type == Type.PROFILING) { // sb.append(m.line); // sb.append("\n"); // } // } // return listener.rewriter.getText(); return sb.toString(); } Loading src/main/java/hk/quantr/verilogcompiler/listener/preprocess/VerilogPreProcessorListener.java +9 −9 Changes for src/main/java/hk/quantr/verilogcompiler/listener/preprocess/VerilogPreProcessorListener.java: 9 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ package hk.quantr.verilogcompiler.listener.preprocess; import hk.quantr.verilogcompiler.antlr.VerilogParser; import hk.quantr.verilogcompiler.antlr.VerilogParserBaseListener; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.MacroListing.Type; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.util.ArrayList; import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.TokenStreamRewriter; Loading @@ -19,12 +19,12 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { public TokenStreamRewriter rewriter; String sourceContent; ArrayList<MacroListing> macroListing; ParseStructure ps; public VerilogPreProcessorListener(TokenStream tokens, String sourceContent, ArrayList<MacroListing> macroListing) { public VerilogPreProcessorListener(TokenStream tokens, String sourceContent, ParseStructure ps) { rewriter = new TokenStreamRewriter(tokens); this.sourceContent = sourceContent; this.macroListing = macroListing; this.ps = ps; } @Override Loading @@ -36,13 +36,13 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { int lineNo = ctx.stop.getLine() - 1; String prefix = ""; /* String prefix = ""; for (MacroListing temp : macroListing) { if (temp.secondLineNo == lineNo) { prefix = temp.line.replaceAll("[^\t]", " "); break; } } }*/ // find macro uuid // String macroUUID = macroListing.get(lineNo).uuid; Loading @@ -59,7 +59,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { // } // // // find macro uuid int macroLastLineNo = -1; /*int macroLastLineNo = -1; int macroLastSecondLineNo = -1; for (int z = macroListing.size() - 1; z >= 0; z--) { if (macroListing.get(z).secondLineNo == lineNo) { Loading @@ -70,7 +70,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { } if (macroLastLineNo == -1) { System.out.println("shit"); } }*/ // end find macro uuid // find original Loading @@ -82,7 +82,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { // temp--; // } // end find original macroListing.add(new MacroListing(macroLastLineNo, macroLastSecondLineNo, prefix + "$display(" + ctx.variable_lvalue().getText() + ");", MacroListing.Type.PROFILING, null)); // macroListing.add(new MacroListing(macroLastLineNo, macroLastSecondLineNo, prefix + "$display(" + ctx.variable_lvalue().getText() + ");", MacroListing.Type.PROFILING, null)); // // add to macroListing // for (int z = macroListing.size() - 1; z >= 0; z--) { Loading Loading
nbactions.xml +3 −3 Changes for nbactions.xml: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ <properties> <exec.vmArgs></exec.vmArgs> <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> <exec.mainClass>hk.quantr.verilogcompiler.VerilogCompiler</exec.mainClass> <exec.executable>java</exec.executable> </properties> Loading @@ -29,7 +29,7 @@ <properties> <exec.vmArgs>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</exec.vmArgs> <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> <exec.mainClass>hk.quantr.verilogcompiler.VerilogCompiler</exec.mainClass> <exec.executable>java</exec.executable> <jpda.listen>true</jpda.listen> Loading @@ -49,7 +49,7 @@ <exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args> <exec.mainClass>${packageClassName}</exec.mainClass> <exec.executable>java</exec.executable> <exec.appArgs>-t ../quantr-i/src/ex.v -o ../quantr-i/src-output</exec.appArgs> <exec.appArgs>-t ../quantr-i/src/id.v -o ../quantr-i/src-output</exec.appArgs> </properties> </action> <action> Loading
src/main/java/hk/quantr/verilogcompiler/VerilogCompiler.java +3 −2 Changes for src/main/java/hk/quantr/verilogcompiler/VerilogCompiler.java: 3 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ package hk.quantr.verilogcompiler; import hk.quantr.javalib.CommonLib; import hk.quantr.javalib.PropertyUtil; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; Loading Loading @@ -152,8 +153,8 @@ public class VerilogCompiler { String temp = cmd.getOptionValue("m"); File file = new File(temp); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), ps); System.out.println(sourceContent); } } Loading
src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java +76 −94 Changes for src/main/java/hk/quantr/verilogcompiler/VerilogHelper.java: 76 added lines, 94 removed lines. Original line number Diff line number Diff line Loading @@ -10,16 +10,14 @@ import hk.quantr.verilogcompiler.listener.VerilogMacroDefinitionListener; import hk.quantr.verilogcompiler.listener.preprocess.VerilogPreProcessorListener; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.MacroListing.Type; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; Loading Loading @@ -66,11 +64,11 @@ public class VerilogHelper { return verilogPreListener; } public static String getContentReplacedWithMacro(String content, VerilogMacroDefinitionListener verilogPreListener, ArrayList<MacroListing> macroListing) throws FileNotFoundException, IOException { public static String getContentReplacedWithMacro(int parseNo, String content, VerilogMacroDefinitionListener verilogPreListener, ParseStructure ps) throws FileNotFoundException, IOException { String lines[] = content.split("\r?\n"); if (macroListing.size() == 0) { if (ps.macroListing.isEmpty()) { for (int x = 0; x < lines.length; x++) { macroListing.add(new MacroListing(x, lines[x], Type.ORIGINAL, null)); ps.macroListing.add(new MacroListing(x, lines[x], Type.ORIGINAL)); } } Loading @@ -79,6 +77,7 @@ public class VerilogHelper { tokenStream.fill(); TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); VerilogMacroDefinitionListener preListener = getPreListener(content); CommonLib.printWithLineNumber(content); for (Token token : preListener.macroUsageTokens) { String macroText = token.getText(); String macroName = macroText.replaceAll("\\(.*", ""); Loading @@ -97,29 +96,25 @@ public class VerilogHelper { String callingParamter = callingParameters[x]; macroBody = macroBody.replaceAll("\\b" + parameter + "\\b", callingParamter); } // int macroLineNo = macroListing.get(currentLineNo).secondLineNo; // for (String temp : macroBody.split("\r?\n")) { // macroListing.add(new MacroListing(token.getLine() - 1, macroLineNo + r, temp, Type.MACRO, uuid)); // r++; // } // add to macroListing int currentLineNo = 0; for (int z = macroListing.size() - 1; z >= 0; z--) { if (macroListing.get(z).lineNo == token.getLine() - 1) { currentLineNo = z; break; } } int lineNo = token.getLine() - 1; MacroListing parentMacro = ps.get(lineNo, parseNo); // macroListing.add(z + 1, new MacroListing(token.getLine() - 1, macroBody, Type.MACRO)); int r = 0; String uuid = UUID.randomUUID().toString(); int macroLineNo = macroListing.get(currentLineNo).secondLineNo; for (String temp : macroBody.split("\r?\n")) { macroListing.add(new MacroListing(token.getLine() - 1, macroLineNo + r, temp, Type.MACRO, uuid)); r++; if (parentMacro.lineNo == 194) { System.out.println(""); } for (int q = currentLineNo + 1; q < macroListing.size(); q++) { if (macroListing.get(q).type == Type.ORIGINAL) { macroListing.get(q).secondLineNo += r - 1; if (parentMacro == null) { System.err.println("ERROR"); System.out.println(ps); System.exit(125); } for (String temp : macroBody.split("\r?\n")) { parentMacro.children.add(new MacroListing(parentMacro.lineNo, temp, Type.MACRO)); } // end add to macroListing Loading Loading @@ -150,46 +145,49 @@ public class VerilogHelper { String result = rewriter.getText(); if (!result.equals(content)) { result = getContentReplacedWithMacro(result, verilogPreListener, macroListing); System.out.println(ps); System.out.println("+".repeat(100)); CommonLib.printWithLineNumber(result); System.out.println("+".repeat(100)); result = getContentReplacedWithMacro(parseNo + 1, result, verilogPreListener, ps); } return result; } public static String getContentReplacedWithMacroWithoutReplaceFunction(String content, VerilogMacroDefinitionListener verilogPreListener) throws FileNotFoundException, IOException { VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(content)); CommonTokenStream tokenStream = new CommonTokenStream(lexer, VerilogLexer.DIRECTIVES); tokenStream.fill(); TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); VerilogMacroDefinitionListener preListener = getPreListener(content); for (Token token : preListener.macroUsageTokens) { String macroText = token.getText(); String macroName = macroText.replaceAll("\\(.*", ""); if (preListener.macroUsageTokens.contains(token) && verilogPreListener.macroDefinitions.get(macroName) != null) { String macroBody = verilogPreListener.macroDefinitions.get(macroName); // logger.info("\treplacing " + macroName); // rewriter.replace(token, macroBody); // rewriter.delete(token.getTokenIndex() - 1); // if (verilogPreListener.macroUsageTypes.get(macroName)) { // rewriter.delete(token.getTokenIndex() + 1); // remove last ';' // public static String getContentReplacedWithMacroWithoutReplaceFunction(int parseNo, String content, VerilogMacroDefinitionListener verilogPreListener) throws FileNotFoundException, IOException { // VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(content)); // CommonTokenStream tokenStream = new CommonTokenStream(lexer, VerilogLexer.DIRECTIVES); // tokenStream.fill(); // TokenStreamRewriter rewriter = new TokenStreamRewriter(tokenStream); // VerilogMacroDefinitionListener preListener = getPreListener(content); // for (Token token : preListener.macroUsageTokens) { // String macroText = token.getText(); // String macroName = macroText.replaceAll("\\(.*", ""); // if (preListener.macroUsageTokens.contains(token) && verilogPreListener.macroDefinitions.get(macroName) != null) { // String macroBody = verilogPreListener.macroDefinitions.get(macroName); //// logger.info("\treplacing " + macroName); //// rewriter.replace(token, macroBody); //// rewriter.delete(token.getTokenIndex() - 1); //// if (verilogPreListener.macroUsageTypes.get(macroName)) { //// rewriter.delete(token.getTokenIndex() + 1); // remove last ';' //// } // // if (verilogPreListener.macroParameters.get(macroName) != null) { // for (String parameter : verilogPreListener.macroParameters.get(macroName)) { // rewriter.insertAfter(token, ";\n$display(" + parameter + ")"); // } // } // //// logger.info(token + " \t = \t" + verilogPreListener.macroDefinitions.get(macroName)); // } // } // String result = rewriter.getText(); // if (!result.equals(content)) { // ArrayList<MacroListing> macroListing = new ArrayList<>(); // result = getContentReplacedWithMacro(parseNo + 1, result, verilogPreListener); // } // return result; // } if (verilogPreListener.macroParameters.get(macroName) != null) { for (String parameter : verilogPreListener.macroParameters.get(macroName)) { rewriter.insertAfter(token, ";\n$display(" + parameter + ")"); } } // logger.info(token + " \t = \t" + verilogPreListener.macroDefinitions.get(macroName)); } } String result = rewriter.getText(); if (!result.equals(content)) { ArrayList<MacroListing> macroListing = new ArrayList<>(); result = getContentReplacedWithMacro(result, verilogPreListener, macroListing); } return result; } public static String verilogToJson(File file) throws FileNotFoundException, IOException { if (!(file.isFile() && file.exists())) { logger.log(Level.SEVERE, "verilog file/folder " + file.getAbsolutePath() + " not exist"); Loading @@ -211,8 +209,8 @@ public class VerilogHelper { } public static hk.quantr.verilogcompiler.structure.Module parseVerilogForOutput(String content, File folder) throws IOException { ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); SingleModuleListener listener = new SingleModuleListener(); Loading @@ -235,8 +233,8 @@ public class VerilogHelper { try { logger.info("parseTopModule(), processing " + file.getAbsolutePath()); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); SingleModuleListener listener = new SingleModuleListener(); Loading @@ -259,8 +257,8 @@ public class VerilogHelper { // parse top module try { String content = IOUtils.toString(new FileInputStream(topModuleFile), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(folder), macroListing); ParseStructure ps = new ParseStructure(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(folder), ps); VerilogLexer lexer = new VerilogLexer(CharStreams.fromString(sourceContent)); CommonTokenStream tokenStream = new CommonTokenStream(lexer); AllModuleListener listener = new AllModuleListener(modules); Loading @@ -284,9 +282,11 @@ public class VerilogHelper { } public static String preProcess(File file) throws FileNotFoundException, FileNotFoundException, IOException { ParseStructure ps = new ParseStructure(); String content = IOUtils.toString(new FileInputStream(file), "utf-8"); ArrayList<MacroListing> macroListing = new ArrayList<>(); String sourceContent = VerilogHelper.getContentReplacedWithMacro(content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), macroListing); String sourceContent = VerilogHelper.getContentReplacedWithMacro(0, content, VerilogHelper.scanMacroDefinitions(file.getParentFile()), ps); System.out.println(" final ".repeat(30)); System.out.println(ps); FileUtils.writeStringToFile(new File("aaaa.v"), sourceContent, "utf-8"); CommonLib.printWithLineNumber(sourceContent); System.out.println("+".repeat(100)); Loading @@ -294,40 +294,22 @@ public class VerilogHelper { CommonTokenStream tokenStream = new CommonTokenStream(lexer); VerilogParser parser = new VerilogParser(tokenStream); ParseTree tree = parser.source_text(); Collections.sort(macroListing, (MacroListing o1, MacroListing o2) -> { if (o1.lineNo == o2.lineNo) { return Integer.valueOf(o1.secondLineNo).compareTo(Integer.valueOf(o2.secondLineNo)); } return Integer.valueOf(o1.lineNo).compareTo(Integer.valueOf(o2.lineNo)); }); for (MacroListing m : macroListing) { System.out.println(m); } System.out.println("-".repeat(100)); VerilogPreProcessorListener listener = new VerilogPreProcessorListener(tokenStream, sourceContent, macroListing); VerilogPreProcessorListener listener = new VerilogPreProcessorListener(tokenStream, sourceContent, ps); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(listener, tree); StringBuilder sb = new StringBuilder(); Collections.sort(macroListing, (MacroListing o1, MacroListing o2) -> { if (o1.lineNo == o2.lineNo) { return Integer.valueOf(o1.secondLineNo).compareTo(Integer.valueOf(o2.secondLineNo)); } return Integer.valueOf(o1.lineNo).compareTo(Integer.valueOf(o2.lineNo)); }); for (MacroListing m : macroListing) { System.out.println(m); if (m.type == Type.ORIGINAL || m.type == Type.PROFILING) { sb.append(m.line); sb.append("\n"); } } // for (MacroListing m : macroListing) { // System.out.println(m); // if (m.type == Type.ORIGINAL || m.type == Type.PROFILING) { // sb.append(m.line); // sb.append("\n"); // } // } // return listener.rewriter.getText(); return sb.toString(); } Loading
src/main/java/hk/quantr/verilogcompiler/listener/preprocess/VerilogPreProcessorListener.java +9 −9 Changes for src/main/java/hk/quantr/verilogcompiler/listener/preprocess/VerilogPreProcessorListener.java: 9 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ package hk.quantr.verilogcompiler.listener.preprocess; import hk.quantr.verilogcompiler.antlr.VerilogParser; import hk.quantr.verilogcompiler.antlr.VerilogParserBaseListener; import hk.quantr.verilogcompiler.macro.MacroListing; import hk.quantr.verilogcompiler.macro.MacroListing.Type; import hk.quantr.verilogcompiler.macro.ParseStructure; import java.util.ArrayList; import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.TokenStreamRewriter; Loading @@ -19,12 +19,12 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { public TokenStreamRewriter rewriter; String sourceContent; ArrayList<MacroListing> macroListing; ParseStructure ps; public VerilogPreProcessorListener(TokenStream tokens, String sourceContent, ArrayList<MacroListing> macroListing) { public VerilogPreProcessorListener(TokenStream tokens, String sourceContent, ParseStructure ps) { rewriter = new TokenStreamRewriter(tokens); this.sourceContent = sourceContent; this.macroListing = macroListing; this.ps = ps; } @Override Loading @@ -36,13 +36,13 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { int lineNo = ctx.stop.getLine() - 1; String prefix = ""; /* String prefix = ""; for (MacroListing temp : macroListing) { if (temp.secondLineNo == lineNo) { prefix = temp.line.replaceAll("[^\t]", " "); break; } } }*/ // find macro uuid // String macroUUID = macroListing.get(lineNo).uuid; Loading @@ -59,7 +59,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { // } // // // find macro uuid int macroLastLineNo = -1; /*int macroLastLineNo = -1; int macroLastSecondLineNo = -1; for (int z = macroListing.size() - 1; z >= 0; z--) { if (macroListing.get(z).secondLineNo == lineNo) { Loading @@ -70,7 +70,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { } if (macroLastLineNo == -1) { System.out.println("shit"); } }*/ // end find macro uuid // find original Loading @@ -82,7 +82,7 @@ public class VerilogPreProcessorListener extends VerilogParserBaseListener { // temp--; // } // end find original macroListing.add(new MacroListing(macroLastLineNo, macroLastSecondLineNo, prefix + "$display(" + ctx.variable_lvalue().getText() + ");", MacroListing.Type.PROFILING, null)); // macroListing.add(new MacroListing(macroLastLineNo, macroLastSecondLineNo, prefix + "$display(" + ctx.variable_lvalue().getText() + ");", MacroListing.Type.PROFILING, null)); // // add to macroListing // for (int z = macroListing.size() - 1; z >= 0; z--) { Loading