/* SCCS - %W% - %G% - %U% */ /* * Copyright (c) 1997 USC Brain Project. email nsl@java.usc.edu. */ package npp.src.util; import java.util.*; import java.io.*; import npp.src.*; import pp.src.jbf.*; import java.lang.*; public class NslPreProcessor extends NPP{ public NslPreProcessor () { import_packages = new Vector(2,2); package_name = null; classes = new Vector(1,1); class_var_scope = cur_var_scope = new Vector(); var_scope = new Stack(); gencodes = new Vector(10,10); wholebuffer = null; init_gen_code_index = -1; mod_list = new Vector(1,1); init_class_path(); // Initialize class_path vector } // Package node must be a symbol /* public void addImportPackage(YYtoken node) { import_packages.addElement(importName); } */ public void setPackage(YYtoken node) { package_name = node.text; // System.err.println("PACKAGE "+package_name+" for "+modName); } public String getPackage() { // System.err.println("GETPACKAGE "+package_name+" for "+modName); return package_name; } public void startClass(ClassNode node) { pushScope(); classes.addElement(node); class_var_scope = cur_var_scope; cur_class = node; cur_method = null; cur_method_type = MethodNode.NONE; } public ClassNode getCurClass() { return cur_class; } public boolean endClass(YYtoken end_token) { FormalNode n; TempVar tv; NPPGenCode nppgc; // System.err.println("Finished with "+modName+" var found are"); // printVar(); /* Enumeration E = cur_var_scope.elements(); System.out.println("\n\n===VariableList\nType:\tName:\tNsl:\tNumeric:"); while(E.hasMoreElements()) { n = (FormalNode)(E.nextElement()); System.out.println(n.type.text+"\t"+n.text+"\t"+n.type.nslNumeric+"("+n.type.nslDim+")\t"+n.type.numeric+"("+n.type.dim+")"); } cur_class.listMakeInstDeclStmt(); */ /* Enumeration E = gencodes.elements(); while(E.hasMoreElements()) { nppgc = (NPPGenCode)(E.nextElement()); if (nppgc.expr!=null) { nppgc.expr.genCode(this); // if(nppgc.expr.processed) nppgc.text = nppgc.expr.code; } } */ cur_class.setEndToken(end_token); // Temporaries are no more instantiated in method initRun() // Now temporaries are instantiated in method inserted by npp /* if(init_gen_code_index!=-1) gencodes.setElementAt(cur_class.genInitStmt(), init_gen_code_index); */ addGenCode(cur_class.genDeclStmt()); // popScope(); init_gen_code_index = -1; cur_class = null; class_var_scope = null; return true; } /** Declare new constructor of the current class */ public void startConstructor() { //System.out.println("StartConstructor"); pushScope(); } public void startConstructor(ConstructorNode node) { pushScope(); cur_class.addConstructor(node); cur_constructor = node; } public void setCurConstructorParams(ParamsNode pn) { cur_constructor.setParams(pn); } /** Get the current constructor in parsing */ public ConstructorNode getCurConstructor() { return cur_constructor; } /** Declare the end of the current constructor */ public boolean endConstructor(YYtoken end_token) { //System.out.println("End Constructor: "+cur_constructor); cur_constructor.setEndToken(end_token); /* place holder for future */ cur_constructor = null; popScope(); return true; } /** Declare new method of the current class */ public void startMethod() { //System.out.println("StartMethod"); pushScope(); } public void startMethod(MethodNode node) { pushScope(); cur_method_type = cur_class.addMethod(node); // System.out.println("Starting method "+node+" type"+cur_method_type); cur_method = node; } public void setCurMethodParams(ParamsNode pn) { cur_method.setParams(pn); } /** Get the current method in parsing */ public MethodNode getCurMethod() { return cur_method; } /** Declare the end of the current method */ public boolean endMethod(YYtoken end_token) { //System.out.println("End Method: "+cur_method); cur_method.setEndToken(end_token); // addGenCode(cur_method.genDeclStmt()); if(cur_method_type == MethodNode.INITRUN) // init_gen_code_index = addGenCode("/* Init Method Code Gen */\n", init_gen_code_index = addGenCode("", end_token.lineno, end_token.charno)-1; /* place holder for future */ cur_method = null; cur_method_type = MethodNode.NONE; // listGenCode(); popScope(); return true; } /** Declare new data member into the class. */ public void addLocalVar(FormalNode n) { //System.out.println("Add variable "+n.text); cur_var_scope.addElement(n); } public void addLocalVar(ParamsNode p) { FormalNode n; Enumeration E = (p.list).elements(); while(E.hasMoreElements()) { n = (FormalNode)(E.nextElement()); // System.out.println(n.type.text+"\t"+n.text+"\t"+n.type.nslNumeric+"("+n.type.nslDim+")\t"+n.type.numeric+"("+n.type.dim+")"); //System.out.println("Add variable "+n.text); cur_var_scope.addElement(n); } } public YYtoken resolveVar(YYtoken t) { return resolveVar(new ExprNode(t)); } public ExprNode resolveVar(ExprNode t) { Stack tmp_stack; Vector scope; Enumeration E = cur_var_scope.elements(); FormalNode ttmp; // do not check complex qualified symbols if (t.tokentype == YYtokentypes.nt_qualifiedSymbol) return t; // scan the current scope while (E.hasMoreElements()) { ttmp = (FormalNode)E.nextElement(); if (ttmp.equals(t)) { t.type = ttmp.type;//new TypeNode(ttmp.type); return t; } } // scan outer scope tmp_stack = new Stack(); while (!var_scope.empty()) { E = ((Vector)var_scope.peek()).elements(); while (E.hasMoreElements()) { ttmp = (FormalNode)E.nextElement(); if (ttmp.text.equals(t.text)) { restoreScope(tmp_stack); t.type=ttmp.type; //new TypeNode(ttmp.type); return t; } } // not found in this scope tmp_stack.push(var_scope.pop()); } // not found any scope restoreScope(tmp_stack); return t; } private void restoreScope(Stack tmp) { try { while(!tmp.empty()) { var_scope.push(tmp.pop()); } } catch (EmptyStackException e) { System.err.println("System internal error: NslPreProcessor"); throw e; } } public void pushScope() { if(cur_var_scope!=null) var_scope.push(cur_var_scope); cur_var_scope = new Vector(10,10); // System.out.println("Push ........................................"); } public void popScope() { try { cur_var_scope.removeAllElements(); cur_var_scope = (Vector) var_scope.pop(); // System.out.println("Pull ........................................"); } catch(EmptyStackException e) { System.err.println("NslPreProcessor.java: pop empty scope at popScope()"); throw e; } } public int addGenCode(NPPGenCode gc) { NPPGenCode old; int index; if (gc!=null) { if(gencodes.size() > 0) { old = (NPPGenCode)gencodes.lastElement(); index = gencodes.size(); while((old.start_lineno>gc.start_lineno || (old.start_lineno == gc.start_lineno && old.start_charno > gc.start_charno)) && index > 0) { index --; old = (NPPGenCode)gencodes.elementAt(index); } if(indexgc.start_lineno || (old.start_lineno == gc.start_lineno && old.start_charno > gc.start_charno))) gencodes.insertElementAt(gc, index); else gencodes.insertElementAt(gc, index+1); } else gencodes.addElement(gc); } //System.out.println(gc); return gencodes.size(); } public int addGenCode(ExprNode expr, YYtoken token) { if(!expr.processed) { //# System.out.println("/* Code ["+expr.code+"] is not processed.*/"); return gencodes.size(); } return addGenCode(new NPPGenCode(expr.code, expr.lineno, expr.charno,//-expr.text.length()+1, token.lineno, token.charno-1));//-token.text.length())); /* gencodes.addElement(new NPPGenCode(expr, expr.lineno, expr.charno-expr.text.length()+1, token.lineno, token.charno-token.text.length())); */ } /* ERH: Keyword replacement*/ public int addGenCode(String subs,YYtoken token) { if (!nocomments) { System.out.println("/* REPLACING:["+token.text+"]"); System.out.println("starts at:"+(token.charno-token.text.length()+1)+ " end at:"+token.charno+" */"); } return addGenCode(new NPPGenCode(subs, token.lineno, token.charno-token.text.length()+1, token.lineno, token.charno)); } public int addGenCode(String s,int slineno, int scharno, int elineno, int echarno) { if (!nocomments) { System.out.println("/* inserting:["+s+"]"); } return addGenCode(new NPPGenCode(s, slineno, scharno, elineno, echarno)); } public int addGenCode(String place_holder, int lineno, int charno) { if(place_holder == null) return gencodes.size(); /* gencodes.addElement(new NPPGenCode(place_holder, lineno, charno, lineno, -1)); return gencodes.size(); */ return addGenCode(new NPPGenCode(place_holder, lineno, charno, lineno, -1)); } public void listGenCode() { Enumeration E = gencodes.elements(); NPPGenCode gc; System.out.println("\n\nCode Generation"); while (E.hasMoreElements()) { gc = (NPPGenCode)E.nextElement(); System.out.println(gc); } } public void genCode() { try { fout = new FileOutputStream(modName+".java"); pout = new PrintStream(fout); } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); System.exit(0); } Enumeration GC = gencodes.elements(); Enumeration WB = wholebuffer.elements(); NPPGenCode gc = null; String wb = null; /* while (WB.hasMoreElements()) { wb = ((StringBuffer)WB.nextElement()).toString(); System.out.println(wb); } System.out.println("====================================="); WB = wholebuffer.elements(); */ StringBuffer outbuf = new StringBuffer(wholebuffer.size()*80); int curline = -1; int curchar = -1; int gcline = -1; int gcchar= -1; int gcendline =-1; int gcendchar =-1; if (GC.hasMoreElements()) { gc = (NPPGenCode)GC.nextElement(); gcline = gc.start_lineno; gcchar = gc.start_charno; gcendline = gc.end_lineno; gcendchar = gc.end_charno; } else gcline=-1; while (WB.hasMoreElements()) { wb = ((StringBuffer)WB.nextElement()).toString(); curline++; curchar=0; while (gcline==curline) { /* print upto the beginning of changed code*/ if(curchar["+ gcendline+","+gcendchar+"]"); /* print rest of the line */ if(curline < gcendline) { append_com(outbuf,"$"+wb.substring(curchar+1)); wb = ((StringBuffer)WB.nextElement()).toString(); curline++; curchar=0; } /* print lines up to the end of the changed code */ while(curline < gcendline) { append_com(outbuf,"%"+wb); wb = ((StringBuffer)WB.nextElement()).toString(); curline++; curchar=0; } /* print rest of the lines */ if(curchar<=gcendchar) { /* ERH: equality added 7/297 */ if(wb.length() < gcendchar+1) append_com(outbuf,"#"+wb.substring(curchar)); else append_com(outbuf,"@"+wb.substring(curchar, gcendchar+1)); curchar = gcendchar+1; } /*enclose it with comment */ append_com(outbuf,"*/\n"); } /* print the new code */ outbuf.append(gc.text); /* get new set of gencode */ if(GC.hasMoreElements()) { gc = (NPPGenCode)GC.nextElement(); gcline = gc.start_lineno; gcchar = gc.start_charno; gcendline = gc.end_lineno; gcendchar = gc.end_charno; } else { gcline = -1; } } /* print lines normally */ if(curchar= 0) { cur_dir = fname.substring(0,index); } else cur_dir = System.getProperty("user.dir"); //System.err.println("Curr Dir is "+ cur_dir); } public String findfile_indir(String fname, String dirname) { if (!dirname.endsWith(System.getProperty("file.separator"))) dirname = dirname+System.getProperty("file.separator"); // System.err.println("IN DIR "+dirname); File srch_dir = new File(dirname); // do exception handling here. if (srch_dir != null && srch_dir.exists()) { String[] files = srch_dir.list(); int findex = files.length; while(--findex>-1) { if(fname.equals(files[findex])) return dirname+files[findex]; } } return null; } public String findFile(String fname, String extn) { File srch_dir; String retstr, module; retstr = findfile_indir(fname+extn, cur_dir); if (retstr != null) { // System.err.println("findFile: Found In Cur Dir "+retstr); if (cur_dir.equals(System.getProperty("user.dir"))) return retstr.substring(retstr.lastIndexOf(System.getProperty("file.separator"))+1); else return retstr; } Enumeration E = class_path.elements(); while(E.hasMoreElements()) { String cpath=(String)E.nextElement(); Enumeration Emod = mod_list.elements(); while(Emod.hasMoreElements()) { module = (String)Emod.nextElement(); if (!module.endsWith("*")) { if (module.endsWith(fname)) { retstr = cpath+System.getProperty("file.separator")+module+extn; if(new File(retstr).exists()) { // System.err.println("findFile: Found "+retstr); return retstr; } } } else { module = module.substring(0,module.lastIndexOf("*")); retstr = findfile_indir(fname+extn, cpath+System.getProperty("file.separator")+module); if (retstr != null) { // System.err.println("findFile: Found "+retstr); return retstr; } } } } return null; } boolean parseTemp = false; public void parseClass(String fName, boolean addVar) { FileInputStream fin_s, fin_i; FileOutputStream fout_i; String classFileName, srcFileName, interFileName, tmpFileName; boolean var_add = addVar; YYtoken yylval; String pName = null; if (verbose) { System.err.println("Processing "+fName+" in dir "+cur_dir); } classFileName=findFile(fName, ".class"); if (classFileName==null) { srcFileName=findFile(fName, ".mod"); } else { srcFileName=classFileName.substring(0,classFileName.lastIndexOf(".class"))+".mod"; if (!new File(srcFileName).exists()) srcFileName=null; } // System.err.println(srcFileName+" "+fName+" "+classFileName); NslPreProcessor b_npp = new NslPreProcessor(); if (srcFileName != null) b_npp.setModName(srcFileName.substring(0,srcFileName.lastIndexOf(".mod"))); else if (classFileName != null) b_npp.setModName(srcFileName.substring(0,srcFileName.lastIndexOf(".mod"))); else { System.err.println(fName+".mod Not Found "); return; } // System.err.println("Src "+srcFileName+" and Class "+classFileName); // Create an Intermediate and Java File // if srcFile modified after classFile if (srcFileName!=null) { if (classFileName==null || (new File(srcFileName).lastModified() > new File(classFileName).lastModified())) { try { fin_s = new FileInputStream(srcFileName); interFileName=srcFileName.substring(0,srcFileName.lastIndexOf(".mod"))+".nlx"; fout_i = new FileOutputStream(interFileName); File nlxf = new File(interFileName); } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); return; } lx.src.util.NslPrePreProcessor s_nppp = new lx.src.util.NslPrePreProcessor(); lx.src.YYlex s_yyl = new lx.src.YYlex(fin_s,System.out); s_yyl.init(s_nppp); lx.src.YYparse s_yyp = new lx.src.YYparse(s_yyl,System.err); if(!lexonly) { s_yyp.init(s_nppp);//new NslPreProcessor()); } else s_yyp = null; if(debuglex) { System.out.println("lex debug turned on"); if(llevel > 0) s_yyl.setdebug(llevel); else s_yyl.setdebug(true); } if(s_yyp != null && debugparse) { System.out.println("parse debug turned on"); if(plevel > 0) s_yyp.setdebug(plevel); else s_yyp.setdebug(true); } try { if(lexonly) { do { yylval = s_yyl.yylex(); } while (yylval != null); // if(debuglex)System.out.println("lex succeeded"); return; } else { if (verbose) { System.err.println("Parsing "+srcFileName); } System.setOut(new PrintStream(fout_i)); s_yyp.yyparse(); System.setOut(System.out); // s_npp.genCode(); // if(debugparse)System.out.println("parse succeeded"); } } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); System.err.println("*** CAN NOT PARSE MOD FILE "+srcFileName+" ! *** \n"); System.exit(0); } // Create a Java File try { fin_s.close(); fout_i.close(); fin_i = new FileInputStream(interFileName); b_yyl = new YYlex(fin_i,System.out); b_yyl.init(b_npp); } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); return; } if(!lexonly) { b_yyp = new YYparse(b_yyl,System.err); b_yyp.init(b_npp);//new NslPreProcessor()); } else b_yyp = null; if(debuglex) { System.out.println("lex debug turned on"); if(llevel > 0) yyl.setdebug(llevel); else b_yyl.setdebug(true); } if(b_yyp != null && debugparse) { System.out.println("parse debug turned on"); if(plevel > 0) b_yyp.setdebug(plevel); else b_yyp.setdebug(true); } try { if(lexonly) { do { yylval = b_yyl.yylex(); } while (yylval != null); if(debuglex)System.out.println("lex succeeded"); return; } else { if (verbose) { System.err.println("Parsing "+interFileName); } b_yyp.yyparse(); if (verbose) { System.err.println("Generating code from "+interFileName); } b_npp.genCode(); if(debugparse)System.out.println("parse succeeded"); pName = b_npp.getPackage(); // Gets the package name of this module // to be used for javap } } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); System.err.println("*** CAN NOT PARSE NLX FILE "+interFileName +" ! *** \n"); System.exit(0); } System.err.println("Finished parsing. Creating java file"); //nlxf.delete(); String jFile=srcFileName.substring(0,srcFileName.lastIndexOf(".mod"))+".java"; classFileName=srcFileName.substring(0,srcFileName.lastIndexOf(".mod"))+".class"; parseTemp = false; // No more Required to parse Profiler generated File // Compile the Java File String classpath = System.getProperty("java.class.path"); String dir = ""; int endIndex = jFile.lastIndexOf(System.getProperty("file.separator")); if (endIndex !=-1) dir = jFile.substring(0,endIndex); if (!dir.equals(System.getProperty("user.dir")) && !dir.equals("")) classpath = dir+":"+classpath; /* try { Process process = (Runtime.getRuntime()).exec(new String[] {"javac", "-classpath", classpath, jFile}); process.waitFor(); if (process.exitValue() != 0) { InputStream errStr = process.getErrorStream(); byte[] errbyte = new byte[errStr.available()]; errStr.read(errbyte); System.err.write(errbyte, 0, errbyte.length); System.err.println("NotCompiled "+classFileName); errStr.close(); System.exit(0); } else if (verbose) { System.err.println("Compiled "+classFileName); } } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); System.err.println("*** CAN NOT CREATE CLASS FILE ! *** \n"); System.exit(0); } */ if (var_add) { addBaseVar(b_npp); var_add = false; } } } // end-if (srcFile!=null) // Create a latest tmp profile file if required tmpFileName=classFileName.substring(0,classFileName.lastIndexOf(".class"))+".tmp"; File tmpf = new File(tmpFileName); if ((tmpf==null) || (tmpf.lastModified() < new File(classFileName).lastModified())) { try { if (tmpf!=null) tmpf.delete(); if (verbose) { System.err.println("Make a tmp file "+tmpFileName); } BufferedInputStream ipStr = null; FileOutputStream tmpout = new FileOutputStream(tmpFileName); tmpout.write(new byte[] {(byte)'/', (byte)'/', (byte)'/'}); tmpout.flush(); byte [] ipbyte = null; int avail = 0; if (pName != null) pName = pName+"."+fName; else pName = fName; // System.err.println("Profiling "+pName); Process process = (Runtime.getRuntime()).exec(new String[] {"javap", "-private", pName}); ipStr = new BufferedInputStream (process.getInputStream()); if(System.getProperty("os.name").indexOf("Windows") == -1) process.waitFor(); else { if (avail>0) { ipbyte = new byte[avail]; ipStr.read(ipbyte, 0, avail); tmpout.write(ipbyte, 0, avail); } } while (true) { try { if (process.exitValue() !=0) { InputStream errStr = process.getErrorStream(); byte[] errbyte = new byte[errStr.available()]; errStr.read(errbyte); System.err.write(errbyte, 0, errbyte.length); tmpout.close(); errStr.close(); ipStr.close(); (new File(tmpFileName)).delete(); return; } else { // System.err.println("Process Exited"); avail = ipStr.available(); if (avail > 0) { ipbyte = new byte[avail]; ipStr.read(ipbyte, 0, avail); tmpout.write(ipbyte, 0, avail); } ipStr.close(); tmpout.close(); break; } } catch (IllegalThreadStateException e) { avail = ipStr.available(); if (avail > 0) { ipbyte = new byte[avail]; ipStr.read(ipbyte, 0, avail); tmpout.write(ipbyte, 0, avail); } } } // while(true) } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); return; } } if (parseTemp) { try { FileInputStream tmpin = new FileInputStream(tmpFileName); patch.src.YYlex t_yyl = new patch.src.YYlex(tmpin, System.out); t_yyl.init(b_npp); patch.src.YYparse t_yyp = new patch.src.YYparse(t_yyl, System.err); t_yyp.init(b_npp); if (verbose) { System.err.println("Parsing "+tmpFileName); } t_yyp.yyparse(); } catch (Exception e) { System.err.println("!!! exception detected:" + e.getMessage()); e.printStackTrace(); System.err.println("*** CAN NOT PARSE TMP FILE "+tmpFileName+" ! *** \n"); System.exit(0); } if (var_add) { addBaseVar(b_npp); var_add = false; } if (debugparse) System.out.println("parsing tmp file succeeded"); } } public void addBaseVar(NslPreProcessor from) { Stack tmp = from.getVarStack(); while(!tmp.empty()) { var_scope.push(tmp.pop()); } } public Stack getVarStack() { Stack tmp_scope = var_scope; if(cur_var_scope!=null) tmp_scope.push(cur_var_scope); return tmp_scope; } public void printVar() { Stack tmp_stack; Vector scope; Enumeration E = cur_var_scope.elements(); FormalNode ttmp; // scan the current scope while (E.hasMoreElements()) { ttmp = (FormalNode)E.nextElement(); System.err.println(ttmp.code0); // System.err.println(ttmp.type.text); } // scan outer scope tmp_stack = new Stack(); while (!var_scope.empty()) { E = ((Vector)var_scope.peek()).elements(); while (E.hasMoreElements()) { ttmp = (FormalNode)E.nextElement(); System.err.println(ttmp.code0); // System.err.println(ttmp.type.text); } // not found in this scope tmp_stack.push(var_scope.pop()); } // not found any scope restoreScope(tmp_stack); } public void init_class_path() { String classpath = System.getProperty("java.class.path"); class_path = new Vector(); // System.err.println(classpath); int index; String cpath_sep; if(System.getProperty("os.name").indexOf("Windows") == -1) cpath_sep = ":"; else cpath_sep = ";"; while((index = classpath.indexOf(cpath_sep)) > 0) { class_path.addElement(classpath.substring(0,index)); classpath = classpath.substring(index+1); // System.err.println("CLASSPATH: "+classpath); } /* Enumeration E = class_path.elements(); while(E.hasMoreElements()) { System.err.println("PATH "+(String)E.nextElement()); } */ } public void addModList(String srchpath) { if (verbose) { System.err.println("Adding module: "+srchpath); } if (srchpath.indexOf("examples")!=-1) { srchpath = srchpath.replace('.', (System.getProperty("file.separator")).charAt(0)); mod_list.addElement(srchpath); } } public static final int MAX_TEMP_NUMBER = ClassNode.MAX_TEMP_NUMBER; public static final int MIN_TEMP_NUMBER = ClassNode.MIN_TEMP_NUMBER; public static final String TEMP_NUMBER_PREFIX = ClassNode.TEMP_NUMBER_PREFIX; public static final String TEMP_ACCESS_FLAG = ClassNode.TEMP_ACCESS_FLAG; int cur_tmp_number; Vector tmpVarList; Vector import_packages; String package_name; Vector classes; ClassNode cur_class; int cur_method_type; MethodNode cur_method; int init_gen_code_index; ConstructorNode cur_constructor; Vector cur_var_scope; String cur_dir; Vector mod_list; Vector class_var_scope; Vector class_path; Stack var_scope; Vector gencodes; Vector wholebuffer; // thr private YYlex b_yyl; private YYparse b_yyp; private NslPreProcessor b_npp; public String modName; private static FileOutputStream fout; private static PrintStream pout; // static public boolean nocomments; }