//-------------------------------------------------------------------------------------------------- // // @ CopyRight Roberti & Parau Enterprises, Inc. 2021-2023 // // This work is licensed under the Creative Commons Attribution-NoDerivatives 4.0 International License. // To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/4.0/ // or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. // //-------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------ // // DVASM driver class - one per input file in a multi-threading environment // //------------------------------------------------------------------------------ package framework; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import java.util.ArrayList; import java.util.concurrent.locks.*; import java.lang.Runnable; public class DVDriver implements Runnable { public DVDriver(LinkedList inputFiles, ArrayList macroDirectories, LinkedList outputFiles, LinkedList listFiles, String archDirectory, String archExtDirectory, DVMacros macros, ReentrantLock fileListLock) { this.inputFiles= inputFiles; this.outputFiles= outputFiles; this.macroDirectories= macroDirectories; this.archDirectory= archDirectory; this.archExtDirectory= archExtDirectory; this.listFiles= listFiles; this.macros= macros; this.fileListLock= fileListLock; } public void run() { while(true) { String inputFile; String outputFile; String listFile; DVParseInput parseInput; DVStatements statements; boolean errFlag= false; // // Get next file set to process under lock // this.fileListLock.lock(); if (this.inputFiles.isEmpty()) { fileListLock.unlock(); return; } inputFile= this.inputFiles.pop(); outputFile= this.outputFiles.pop(); listFile= this.listFiles.pop(); this.fileListLock.unlock(); // // Parse input // long timeStart= System.currentTimeMillis(); parseInput= null; try { parseInput= new DVParseInput(inputFile, this.macroDirectories, this.archDirectory, this.archExtDirectory, this.macros); } catch(IOException e) { System.err.println("\n*** Unable to read input file [" + inputFile + "] - Error: " + e.getMessage()); } if (parseInput == null) continue; statements= parseInput.parse(); statements.logMsg(String.format("Parse elapsed time is: %d milliseconds\n", System.currentTimeMillis()-timeStart)); timeStart= System.currentTimeMillis(); if (statements.errors > 0) statements.logMsg("Input parsing completed with errors"); else if (statements.firstPhaseWarnings > 0) statements.logMsg("Input parsing completed with warnings"); else statements.logMsg("Input parsing completed succesfully"); // // Process after parsing // if (statements.errors == 0) { statements.symbols.setDependencies(); if (statements.errors > 0) statements.logMsg("Dependencies preprocessing failed due to error(s)"); else { statements.logMsg("Dependencies preprocessing completed successfully"); statements.setSecondPhase(); for (int i= 0; i < 256; i++) { statements.symbols.evaluate(); if (statements.errors > 0) { statements.logMsg("Dependencies resolution failed due to error(s)"); break; } statements.logMsg("Dependencies resolution completed successfully"); statements.genCode(); if (statements.errors > 0) { statements.logMsg("Code generation failed due to error(s)"); break; } if (statements.warnings > 0) statements.logMsg("Code generation completed with warnings"); else statements.logMsg("Code generation completed successfully"); if (! statements.changedLengthFlag) break; statements.resetSecondPhase(); } // // Write ELF binary to file // if (statements.errors == 0) { byte[] elfBinary= statements.generateElfBinary(); try { FileOutputStream elfOutput= new FileOutputStream(new File(outputFile)); elfOutput.write(elfBinary); elfOutput.close(); } catch(IOException e) { statements.logMsg("Error writing ELF file [" + outputFile + "] - Error: " + e.toString()); } } } statements.logMsg(String.format("Code generation elapsed time is: %d milliseconds\n", System.currentTimeMillis()-timeStart)); } // // Write out HTML listing // try { FileWriter outFile= new FileWriter(listFile); outFile.write((new DVHtmlListing()).getListing(statements)); outFile.close(); } catch(IOException e) { statements.logErr("Error while writing listing file [" + listFile + "] - Error: " + e.toString()); } System.err.format(" DVASM execution completed with [%3d] warnings and [%3d] errors for input file [%s]\n", statements.warnings, statements.errors, inputFile); } } // // Input/output files // private LinkedList inputFiles; private ArrayList macroDirectories; private LinkedList outputFiles; private LinkedList listFiles; private String archDirectory; private String archExtDirectory; private DVMacros macros; private Lock fileListLock; }