Math2mat
|
00001 /****************************************************************************** 00002 * StructTreatment 00003 ****************************************************************************** 00004 * Auteur : Trolliet Gregory 00005 * Date : 4 mars 2009 00006 * Description : 00007 ******************************************************************************/ 00008 package m2m.backend.structure; 00009 00010 import org.w3c.dom.*; 00011 00012 import m2m.backend.buildingblocks.BuildingBlock; 00013 import m2m.backend.buildingblocks.BuildingBlock.NumType; 00014 00015 import java.util.Vector; 00016 import java.io.ByteArrayInputStream; 00017 import java.io.File; 00018 import java.io.FileWriter; 00019 import java.io.FileNotFoundException; 00020 import java.io.IOException; 00021 import java.io.InputStream; 00022 import java.io.FileInputStream; 00023 import java.util.Calendar; 00024 import java.util.Iterator; 00025 import java.util.StringTokenizer; 00026 import java.util.HashMap; 00027 import java.util.ArrayList; 00028 00029 00030 00031 import m2m.backend.octaveparser.*; 00032 import m2m.backend.project.ProjectRef; 00033 import m2m.backend.utils.FileUtils; 00034 import m2m.backend.utils.XMLUtils; 00035 import m2m.backend.vhdl.*; 00036 00044 public class StructTreatment extends ProjectRef 00045 { 00049 private int tmpCpt; 00053 private int opCpt; 00057 private Function top; 00061 private Vector<String> negList; 00065 private HashMap<String, String> pragmaMap; 00069 private HashMap<String, SimpleVariable> varMap; 00073 public static final String TMP_NAME = "m2mtmp"; 00077 private int nbMonitor = 0; 00081 private long parseTime; 00082 00086 private String sourceMd5; 00087 00091 private String sourceCode; 00092 00096 public StructTreatment() { 00097 this.tmpCpt = 0; 00098 this.opCpt = 0; 00099 this.top = null; 00100 this.negList = new Vector<String>(); 00101 this.pragmaMap = new HashMap<String, String>(); 00102 this.varMap = new HashMap<String, SimpleVariable>(); 00103 this.nbMonitor=0; 00104 } 00105 00106 00111 @SuppressWarnings("unchecked") 00112 public StructTreatment copy() { 00113 StructTreatment clone=new StructTreatment(); 00114 clone.tmpCpt=this.tmpCpt; 00115 clone.opCpt=this.opCpt; 00116 clone.top=new Function(); 00117 clone.top=(Function)this.top.copy(null); 00118 clone.negList = (Vector<String>)this.negList.clone(); 00119 clone.pragmaMap = (HashMap<String, String>)this.pragmaMap.clone(); 00120 clone.varMap = (HashMap<String, SimpleVariable>)this.varMap.clone(); 00121 clone.nbMonitor = this.nbMonitor; 00122 return clone; 00123 } 00124 00125 public void modifyNumType(NumType type) { 00126 top.modifyNumType(type); 00127 } 00128 00134 public long getParseTime() { 00135 return parseTime; 00136 } 00137 00142 public void setTop(Function top) { 00143 this.top = top; 00144 } 00145 00150 public Function getTop() { 00151 return this.top; 00152 } 00153 00158 public Vector<Element> getInput() { 00159 return ((Function) this.top).getInput(); 00160 } 00161 00166 public Vector<Element> getOutput() { 00167 return ((Function) this.top).getOutput(); 00168 } 00169 00174 public String getName() { 00175 return top.getName(); 00176 } 00177 00183 public void parse(File file) throws ParsingException 00184 { 00185 InputStream in = null; 00186 try { 00187 in = new FileInputStream(file.getPath()); 00188 } catch (FileNotFoundException e) { 00189 System.out.println("Le fichier n'existe pas!"); 00190 e.printStackTrace(); 00191 System.exit(0); 00192 } catch (SecurityException e) { 00193 e.printStackTrace(); 00194 } 00195 00196 OctaveLexer lexer = new OctaveLexer(in); 00197 OctaveParser parser = new OctaveParser(lexer); 00198 // Create the top entity of the structure 00199 this.top = new Function(); 00200 parser.setTop(this.top); 00201 parser.setTreat(this); 00202 try { 00203 parser.entry(); 00204 if (!parser.isSuccessful()) 00205 throw new ParsingException("Parsing error"); 00206 this.treeToVector(); 00207 this.postParse(top); 00208 Calendar cal = Calendar.getInstance(); 00209 parseTime = cal.getTime().getTime(); 00210 // long timeCurrent = cal.getTime().getTime() / 100000; 00211 } catch (Exception e) { 00212 throw new ParsingException(e); 00213 } 00214 sourceMd5=FileUtils.getMd5(file.getAbsolutePath()); 00215 00216 sourceCode = FileUtils.readFileAsString(file.getAbsolutePath()); 00217 } 00223 public void parseNoFlat(File file) throws ParsingException 00224 { 00225 InputStream in = null; 00226 try { 00227 in = new FileInputStream(file.getPath()); 00228 } catch (FileNotFoundException e) { 00229 System.out.println("Le fichier n'existe pas!"); 00230 e.printStackTrace(); 00231 System.exit(0); 00232 } catch (SecurityException e) { 00233 e.printStackTrace(); 00234 } 00235 00236 OctaveLexer lexer = new OctaveLexer(in); 00237 OctaveParser parser = new OctaveParser(lexer); 00238 // Create the top entity of the structure 00239 this.top = new Function(); 00240 parser.setTop(this.top); 00241 parser.setTreat(this); 00242 try { 00243 parser.entry(); 00244 if (!parser.isSuccessful()) 00245 throw new ParsingException("Parsing error"); 00246 //OptimizeOperators opt=new OptimizeOperators(this); 00247 //opt.optimise(); 00248 this.postParse(top); 00249 Calendar cal = Calendar.getInstance(); 00250 parseTime = cal.getTime().getTime(); 00251 // long timeCurrent = cal.getTime().getTime() / 100000; 00252 } catch (Exception e) { 00253 throw new ParsingException(e); 00254 } 00255 sourceMd5=FileUtils.getMd5(file.getAbsolutePath()); 00256 00257 sourceCode = FileUtils.readFileAsString(file.getAbsolutePath()); 00258 } 00259 00260 public String getSourceMd5() { 00261 return sourceMd5; 00262 } 00263 00264 public String getSourceCode() { 00265 return sourceCode; 00266 } 00267 00273 public void parse(String code) throws ParsingException 00274 { 00275 00276 InputStream in = null; 00277 in = new ByteArrayInputStream(code.getBytes()); 00278 00279 OctaveLexer lexer = new OctaveLexer(in); 00280 OctaveParser parser = new OctaveParser(lexer); 00281 // Create the top entity of the structure 00282 this.top = new Function(); 00283 parser.setTop(this.top); 00284 parser.setTreat(this); 00285 try { 00286 parser.entry(); 00287 if (!parser.isSuccessful()) 00288 throw new ParsingException("Parsing error"); 00289 // OptimizeOperators opt=new OptimizeOperators(this); 00290 // opt.optimise(); 00291 this.treeToVector(); 00292 this.postParse(top); 00293 Calendar cal = Calendar.getInstance(); 00294 parseTime = cal.getTime().getTime(); 00295 } catch (Exception e) { 00296 throw new ParsingException(e); 00297 } 00298 sourceMd5=FileUtils.getStringMd5(code); 00299 sourceCode=code; 00300 } 00301 00309 public Vector<Element> createOp( 00310 Vector<Element> a, Vector<Element> b, Operation op) { 00311 Vector<Element> vec = new Vector<Element>(); 00312 // Add all the elements of a and remove the last. That's the one to 00313 // treat. 00314 vec.addAll(a); 00315 vec.remove(vec.lastIndexOf(a.lastElement())); 00316 if (a.lastElement() instanceof Negation) { 00317 vec.add(a.lastElement()); 00318 op.addInput(((Negation) a.lastElement()).getOutputAt(0)); 00319 } else { 00320 op.addInput(a.lastElement()); 00321 } 00322 // Same operation that with a 00323 vec.addAll(b); 00324 vec.remove(vec.lastIndexOf(b.lastElement())); 00325 if (b.lastElement() instanceof Negation) { 00326 vec.add(b.lastElement()); 00327 op.addInput(((Negation) b.lastElement()).getOutputAt(0)); 00328 } else { 00329 op.addInput(b.lastElement()); 00330 } 00331 op.setName(op.getName() + this.getNextOpCpt()); 00332 vec.add(op); 00333 if (op instanceof DotDivision || op instanceof DotMultiplication) { 00334 for (Element e : op.getInput()) { 00335 if (e instanceof Variable) { 00336 this.addPragma(getTrueName(e.getName()), "vector"); 00337 } 00338 } 00339 } 00340 return vec; 00341 } 00342 00349 public Vector<Element> createNot(Vector<Element> a, Not not) { 00350 Vector<Element> vec = new Vector<Element>(); 00351 vec.addAll(a); 00352 vec.remove(vec.lastIndexOf(a.lastElement())); 00353 if (a.lastElement() instanceof Negation) { 00354 vec.add(a.lastElement()); 00355 not.addInput(((Operation) a.lastElement()).getOutputAt(0)); 00356 } else { 00357 not.addInput(a.lastElement()); 00358 } 00359 vec.add(not); 00360 return vec; 00361 } 00362 00367 public int getNextOpCpt() { 00368 return this.opCpt++; 00369 } 00370 00382 public void treeToVector() { 00383 this.treeToVector(top); 00384 } 00385 00390 public void treeToVector(Function parent) { 00391 parent.setBody(this.convertBody(parent.getBody(), parent)); 00392 if (parent instanceof LoopWhile) { 00393 ((LoopWhile) parent).setCond(this.convertBody(((LoopWhile) parent).getCond(), parent)); 00394 } 00395 } 00396 00402 private Vector<Element> convertBody(Vector<Element> vec, Element parent) { 00403 Vector<Element> newel = new Vector<Element>(); 00404 if (vec == null || vec.isEmpty()) { 00405 return vec; 00406 } 00407 for (Element e : vec) { 00408 if (e instanceof Operation) { 00409 newel.addAll(this.convertOperation((Operation) e, parent)); 00410 } 00411 else if (e instanceof Switch) { 00412 Switch sw = (Switch) parent; 00413 for (Case c : sw.getCases()) { 00414 c.setBody(this.convertBody(c.getBody(), parent)); 00415 } 00416 } 00417 else if (e instanceof IfThenElse) { 00418 newel.add(e); 00419 IfThenElse ite = (IfThenElse) e; 00420 ite.setBodyTrue(this.convertBody(ite.getBodyTrue(), ite)); 00421 ite.setBodyFalse(this.convertBody(ite.getBodyFalse(), ite)); 00422 ite.setCond(this.convertBody(ite.getCond(), ite)); 00423 for(Element element : ite.internalVars) { 00424 if(!((Function)parent).internalVars.contains(element)) 00425 ((Function)parent).internalVars.add(element); 00426 } 00427 } 00428 else if(e instanceof Function ) { 00429 newel.add(e); 00430 treeToVector((Function) e); 00431 } 00432 } 00433 return newel; 00434 } 00435 00441 private Vector<Element> convertOperation(Operation op, Element parent) { 00442 Vector<Element> vec = new Vector<Element>(); 00443 Vector<Element> input; 00444 SimpleVariable tmp; 00445 input = op.getInput(); 00446 op.setInput(new Vector<Element>()); 00447 for (Element e : input) { 00448 if (e instanceof Operation) { 00449 vec.addAll(this.convertOperation((Operation) e, parent)); 00450 // if operation need to be separate, create a temporary value 00451 tmp = new SimpleVariable(TMP_NAME + "_m2m_" + tmpCpt++); 00452 ((Operation) e).addOutput(tmp); 00453 ((Function)parent).addInternalVar(tmp); 00454 op.addInput(tmp); 00455 } else if (e instanceof Variable) { 00456 op.addInput(e); 00457 } 00458 } 00459 vec.add(op); 00460 return vec; 00461 } 00462 00469 public Vector<Element> createAff(Variable var, Vector<Element> expr) { 00470 Vector<Element> vec = new Vector<Element>(); 00471 Operation result = new Assignment(); 00472 if (expr.lastElement() instanceof Variable) { 00473 // Add all element of expr to vec except the last one 00474 vec.addAll(expr); 00475 vec.removeElementAt(vec.lastIndexOf(expr.lastElement())); 00476 // If that's a SimpleVariable, juste add to the assignment 00477 result.addInput((Variable) expr.lastElement()); 00478 result.setName("aff" + this.opCpt++); 00479 var.setType(((Variable) expr.lastElement()).getType()); 00480 if (var instanceof SimpleVariable) { 00481 ((SimpleVariable) var).setSize(((Variable) expr.lastElement()).getSize()); 00482 } 00483 if (((Variable) expr.lastElement()).getType().equals("const")) { 00484 ((SimpleVariable) var).setVal((((Variable) expr.lastElement()).getVal())); 00485 } 00486 } else if (expr.lastElement() instanceof Negation) { 00487 // Add all elements of expr to vec 00488 vec.addAll(expr); 00489 result.addInput(((Negation) expr.lastElement()).getOutputAt(0)); 00490 result.setName("aff" + this.opCpt++); 00491 var.setType(((Variable) ((Negation) expr.lastElement()).getOutputAt(0)).getType()); 00492 } else if (expr.lastElement() instanceof Operation) { 00493 vec.addAll(expr); 00494 vec.removeElementAt(vec.lastIndexOf(expr.lastElement())); 00495 result = ((Operation) expr.lastElement()); 00496 //var.setType(((Variable)((Operation)expr.lastElement()).getOutputAt(0)).getType()); 00497 } 00498 result.addOutput(var); 00499 vec.add(result); 00500 return vec; 00501 } 00502 00508 public SimpleVariable createInOut(String name) { 00509 SimpleVariable var = null; 00510 if (this.varMap.containsKey(name)) { 00511 var = this.varMap.get(name); 00512 } else { 00513 var = new SimpleVariable(name); 00514 this.varMap.put(name, var); 00515 } 00516 if (this.pragmaMap.containsKey(getTrueName(name))) { 00517 ((SimpleVariable) var).setType(this.pragmaMap.get(getTrueName(name))); 00518 } 00519 return var; 00520 } 00521 00535 public Vector<Element> createVar(String name, ArrayList<Double> val, 00536 String type,int size, boolean minus) { 00537 //int size = val.size(); 00538 SimpleVariable var = null; 00539 Vector<Element> vec = new Vector<Element>(); 00540 if (name.isEmpty()) { 00541 // If it as no name, it don't exist so we simply create 00542 vec.add(new SimpleVariable(val)); 00543 } else if (minus && !negList.contains(name)) { 00544 Negation neg = null; 00545 // If it's negative and never declared before 00546 neg = new Negation(); 00547 var = (SimpleVariable) this.createVar(getTrueName(name) + "_m2m_n", 00548 0.0, "", false).lastElement(); 00549 if (this.pragmaMap.containsKey(name)) { 00550 var.setType(this.pragmaMap.get(name)); 00551 } 00552 neg.addOutput(var); 00553 neg.addInput(this.createVar(name, 0.0, "", false).lastElement()); 00554 neg.setName("neg" + this.opCpt++); 00555 this.negList.add(name); 00556 vec.add(neg); 00557 vec.add(var); 00558 } else if (minus) { 00559 // If negative and already declared 00560 vec.add(this.createVar(name + "_m2m_n", 0.0, "", false).lastElement()); 00561 } else { 00562 // If normal variable 00563 if (this.varMap.containsKey(name)) { 00564 var = this.varMap.get(name); 00565 } else { 00566 var = new SimpleVariable(name, val, type); 00567 var.setSize(size); 00568 this.top.addInternalVar(var); 00569 this.varMap.put(name, var); 00570 } 00571 if (this.pragmaMap.containsKey(getTrueName(name))) { 00572 ((SimpleVariable) var).setType(this.pragmaMap.get(getTrueName(name))); 00573 } 00574 vec.add(var); 00575 } 00576 return vec; 00577 } 00578 00585 public static String getTrueName(String name) { 00586 StringTokenizer st = new StringTokenizer(name, "_"); 00587 String trueName = new String(); 00588 String tmp = new String(); 00589 if (st.hasMoreTokens()) { 00590 trueName = st.nextToken(); 00591 } 00592 while (st.hasMoreTokens()) { 00593 tmp = st.nextToken(); 00594 if (tmp.equals("m2m")) { 00595 return trueName; 00596 } 00597 trueName += "_" + tmp; 00598 } 00599 return trueName; 00600 } 00601 00607 public Vector<Element> createSimpleVar(SimpleVariable var) { 00608 return this.createVar(var.getName(), var.getValues(), var.getType(), var.getSize(), false); 00609 } 00610 00617 public Vector<Element> createVar(Variable var) { 00618 Vector<Element> vec = null; 00619 if (var instanceof VectorVariable) { 00620 vec = this.createVar(((VectorVariable) var).getVar()); 00621 } else if (var instanceof SimpleVariable) { 00622 vec = this.createSimpleVar((SimpleVariable) var); 00623 } 00624 return vec; 00625 } 00626 00635 public Vector<Element> createVar(String name, double val, 00636 String type, boolean minus) { 00637 ArrayList<Double> values = new ArrayList<Double>(); 00638 values.add(val); 00639 return this.createVar(name, values, type, 1, minus); 00640 } 00641 00647 public Vector<Element> createVar(String name) { 00648 ArrayList<Double> values = new ArrayList<Double>(); 00649 //values.add(0.0); 00650 return this.createVar(name, values, "", 0, false); 00651 } 00652 00657 public void count(Element el) { 00658 el.setName(el.getName() + this.opCpt++); 00659 } 00660 00669 public void createInOutAff(Function top, HashMap<String, ReadWriteIndex> map) { 00670 Assignment aff; 00671 String name; 00672 int cpt = 0; 00673 if (top == null) { 00674 top = this.top; 00675 } 00676 // Input aff 00677 for (Element var : top.getInput()) { 00678 name = var.getName(); 00679 if (this.varMap.containsKey(name + "_m2m_0")) { 00680 aff = new Assignment(); 00681 aff.setName("aff_final_" + cpt++); 00682 aff.addInput(var); 00683 aff.addOutput(this.varMap.get(name + "_m2m_0")); 00684 top.addBodyToTop(aff); 00685 } else { 00686 aff = new Assignment(); 00687 aff.setName("aff_final_" + cpt++); 00688 Variable tmp = (Variable) this.createVar(name + "_m2m_0", 0.0, "", false).lastElement(); 00689 aff.addInput(var); 00690 aff.addOutput(tmp); 00691 top.addBodyToTop(aff); 00692 } 00693 } 00694 // Output aff 00695 for (Element var : top.getOutput()) { 00696 name = var.getName(); 00697 if(map.containsKey(name)) 00698 if (this.varMap.containsKey(name + "_m2m_" + ((ReadWriteIndex)map.get(name)).getReadIndex())) { 00699 aff = new Assignment(); 00700 aff.setName("aff_final_" + cpt++); 00701 aff.addInput(this.varMap.get(name + "_m2m_" +((ReadWriteIndex)map.get(name)).getReadIndex())); 00702 aff.addOutput(var); 00703 top.addBody(aff); 00704 } 00705 } 00706 } 00707 00713 public void addPragma(String pragma) { 00714 StringTokenizer st = new StringTokenizer(pragma, " :\t"); 00715 String name = null; 00716 String type = null; 00717 if (st.countTokens() == 3 & st.nextToken().equals("%m2m")) { 00718 name = st.nextToken(); 00719 type = st.nextToken(); 00720 //System.out.println(name+"\t"+type); 00721 this.pragmaMap.put(name, type); 00722 this.changeType(this.top, name, type); 00723 //System.out.println("Nombre d'occurences de la variable modifiée :" 00724 // + this.changeType(this.top, name, type)); 00725 } else { 00726 System.out.println("#Le pragma ne respecte pas le standard!"); 00727 } 00728 } 00729 00736 public void addPragma(String name, String type) { 00737 this.pragmaMap.put(name, type); 00738 this.changeType(this.top, name, type); 00739 System.out.println("Nombre d'occurences de la variable modifiée :" + this.changeType(this.top, name, type)); 00740 } 00741 00749 private int changeType(Element top, String name, String type) { 00750 int nb = 0; 00751 if (top instanceof Function) { 00752 if (top instanceof LoopFor) { 00753 nb += this.changeType(((LoopFor) top).getStart(), name, type); 00754 nb += this.changeType(((LoopFor) top).getIncr(), name, type); 00755 nb += this.changeType(((LoopFor) top).getEnd(), name, type); 00756 } 00757 for (Element el : ((Function) top).getInput()) { 00758 nb += this.changeType(el, name, type); 00759 } 00760 for (Element el : ((Function) top).getOutput()) { 00761 nb += this.changeType(el, name, type); 00762 } 00763 for (Element el : ((Function) top).getInternalVars()) { 00764 nb += this.changeType(el, name, type); 00765 } 00766 for (Element el : ((Function) top).getBody()) { 00767 nb += this.changeType(el, name, type); 00768 } 00769 } else if (top instanceof Operation) { 00770 for (Element el : ((Operation) top).getInput()) { 00771 nb += this.changeType(el, name, type); 00772 } 00773 for (Element el : ((Operation) top).getOutput()) { 00774 nb += this.changeType(el, name, type); 00775 } 00776 } else if (top instanceof SimpleVariable) { 00777 if (getTrueName(top.getName()).equals(name)) { 00778 ((SimpleVariable) top).setType(type); 00779 return 1; 00780 } 00781 } else if (top instanceof VectorVariable) { 00782 if (getTrueName(top.getName()).equals(name)) { 00783 ((SimpleVariable) ((VectorVariable) top).getVar()).setType(type); 00784 return 1; 00785 } 00786 } 00787 return nb; 00788 } 00789 00796 public void changeVar(Element top, Variable varin, Variable varout) { 00797 if (top instanceof Function) { 00798 for (Element el : ((Function) top).getBody()) { 00799 this.changeVar(el, varin, varout); 00800 } 00801 while (((Function) top).getInput().indexOf(varin) != -1) { 00802 ((Function) top).getInput().set(((Function) top).getInput().indexOf(varin), varout); 00803 } 00804 while (((Function) top).getOutput().indexOf(varin) != -1) { 00805 ((Function) top).getOutput().set(((Function) top).getOutput().indexOf(varin), varout); 00806 } 00807 if (top instanceof IfThenElse) { 00808 for (Element el : ((IfThenElse) top).getCond()) { 00809 this.changeVar(el, varin, varout); 00810 } 00811 for (Element el : ((IfThenElse) top).getBodyTrue()) { 00812 this.changeVar(el, varin, varout); 00813 } 00814 for (Element el : ((IfThenElse) top).getBodyFalse()) { 00815 this.changeVar(el, varin, varout); 00816 } 00817 } 00818 } else if (top instanceof Operation) { 00819 while (((Operation) top).getInput().indexOf(varin) != -1) { 00820 ((Operation) top).getInput().set(((Operation) top).getInput().indexOf(varin), varout); 00821 } 00822 while (((Operation) top).getOutput().indexOf(varin) != -1) { 00823 ((Operation) top).getOutput().set(((Operation) top).getOutput().indexOf(varin), varout); 00824 } 00825 } 00826 } 00827 00828 public void findInBody(Element top, HashMap<String, ReadWriteIndex> cpt) { 00829 HashMap<String, Integer> list = new HashMap<String, Integer>(); 00830 if (top instanceof LoopFor) { 00831 for (Element el : ((LoopFor) top).getBody()) { 00832 if (el instanceof Operation) { 00833 for (Element in : ((Operation) el).getInput()) { 00834 String name = getTrueName(in.getName()); 00835 if (in instanceof Variable 00836 & !((Variable) in).getType().equals("const") 00837 & !((Variable) in).getType().equals("iter") 00838 & !((LoopFor) top).getInput().contains(in) 00839 & !name.equalsIgnoreCase(TMP_NAME)) { 00840 list.put(name, ((ReadWriteIndex)cpt.get(name)).getReadIndex()); 00841 } 00842 } 00843 } else if (el instanceof Function) { 00844 for (Element in : ((Function) el).getInput()) { 00845 String name = getTrueName(in.getName()); 00846 if (in instanceof Variable 00847 & !((LoopFor) top).getInput().contains(in) 00848 & !name.equalsIgnoreCase(TMP_NAME) 00849 & !((Variable) in).getType().equals("iter")) { 00850 list.put(name, ((ReadWriteIndex)cpt.get(name)).getReadIndex()); 00851 //((LoopFor) top).addInput(in); 00852 } 00853 } 00854 } 00855 } 00856 } 00857 00858 for (String s : list.keySet()) 00859 { 00860 Element e = this.createVar(s + "_m2m_" + ((ReadWriteIndex)cpt.get(s)).getReadIndex()).lastElement(); 00861 if(!((LoopFor) top).getInput().contains(e) && ((Variable) e).getType().equals("iter")) 00862 ((LoopFor) top).addInput(e); 00863 } 00864 } 00865 00871 public boolean isVariable(String name) { 00872 for (String var : this.varMap.keySet()) { 00873 if (getTrueName(var).equals(name)) { 00874 return true; 00875 } 00876 } 00877 return false; 00878 } 00879 00884 public Vector<Variable> monitoredVar() { 00885 Vector<Variable> vec = new Vector<Variable>(); 00886 for (SimpleVariable var : varMap.values()) { 00887 if (var.monitor) { 00888 vec.add(var); 00889 } 00890 } 00891 return vec; 00892 } 00893 00900 public void setBlock(Element top, String opName, BuildingBlock block) { 00901 if (top instanceof Function) { 00902 for (Element el : ((Function) top).getBody()) { 00903 this.setBlock(el, opName, block); 00904 } 00905 } else if (top instanceof Operation) { 00906 if (((Operation) top).getOpName().equals(opName)) { 00907 ((Operation) top).setBlock(block); 00908 } 00909 } 00910 } 00911 00918 public boolean writeVHDL(String vhdlPath) { 00919 File file = new File(vhdlPath+this.top.getName()+".vhd"); 00920 FileWriter fw = null; 00921 try { 00922 // Intialization of the cost of all SimpleVariable 00923 setCost(this.top); 00924 00925 fw = new FileWriter(file); 00926 String sVhdl = new String(); 00927 00928 VHDLCreator vhdl = new VHDLCreator(this.top, project); 00929 vhdl.wrapTop(vhdlPath); 00930 sVhdl = vhdl.createVHDL(file.getName()); 00931 00932 fw.write(sVhdl); 00933 fw.close(); 00934 00935 } catch (FileNotFoundException e) { 00936 e.printStackTrace(); 00937 return false; 00938 } catch (IOException e) { 00939 e.printStackTrace(); 00940 return false; 00941 } catch (VHDLException e) { 00942 System.out.println("#Unable to create VHDL file! "+e.getMessage()); 00943 return false; 00944 } 00945 System.out.println("VHDL file written!"); 00946 return true; 00947 } 00948 00952 public void print() { 00953 System.out.println(this.top.toString(0)); 00954 } 00955 00962 public boolean writeSchematic(String outFile) { 00963 File file = new File(outFile); 00964 FileWriter fw; 00965 try { 00966 fw = new FileWriter(file); 00967 String str = this.createSchematic(); 00968 fw.write(str); 00969 fw.close(); 00970 } catch (FileNotFoundException e) { 00971 e.printStackTrace(); 00972 return false; 00973 } catch (IOException e) { 00974 e.printStackTrace(); 00975 return false; 00976 } 00977 System.out.println("Schematic file written!"); 00978 return true; 00979 } 00980 00986 private String createSchematic() { 00987 String sSchema = new String(); 00988 sSchema += "digraph G {\n"; 00989 sSchema += top.toSchematic(1, "", GraphParams.defaultShape, ""); 00990 sSchema += "\n}"; 00991 return sSchema; 00992 } 00993 00997 public void writeOctaveFile() { 00998 this.writeOctaveFile(null); 00999 } 01000 01005 public boolean writeOctaveFile(String outFile) { 01006 File file; 01007 if (outFile == null) { 01008 file = new File("test/" + this.top.getName() + ".m"); 01009 System.out.println(file.getName()); 01010 } else { 01011 file = new File(outFile); 01012 } 01013 FileWriter fw; 01014 try { 01015 fw = new FileWriter(file); 01016 String str = this.createOctave(); 01017 fw.write(str); 01018 fw.close(); 01019 } catch (FileNotFoundException e) { 01020 e.printStackTrace(); 01021 return false; 01022 } catch (IOException e) { 01023 e.printStackTrace(); 01024 return false; 01025 } 01026 System.out.println("Octave file written!"); 01027 return true; 01028 } 01029 01034 private String createOctave() { 01035 String sOctave = new String(); 01036 sOctave += this.top.toOctave(0); 01037 return sOctave; 01038 } 01039 01040 01044 public void clean(Vector<Element> body, Vector<Element> varVector) 01045 { 01046 Vector<Function> functionVector = new Vector<Function>(); 01047 Element element; 01048 Vector<Element> elementList = new Vector<Element>(); 01049 Iterator<Element> iterElement; 01050 Element tempElement; 01051 01052 /* Get all children functions and read simple variable */ 01053 elementList.addAll(body); 01054 iterElement = elementList.iterator(); 01055 while (iterElement.hasNext()) 01056 { 01057 element = iterElement.next(); 01058 01059 if (element instanceof Operation) { 01060 varVector.add(((Operation)element).getInputAt(0)); 01061 if(((Operation)element).getInput().size() > 1) { 01062 tempElement = ((Operation)element).getInputAt(1); 01063 if(tempElement instanceof Operation) 01064 elementList.add(tempElement); 01065 else 01066 varVector.add(tempElement); 01067 } 01068 } 01069 else if (element instanceof Function) { 01070 varVector.addAll(((Function)element).getInput()); 01071 functionVector.add((Function)element); 01072 } 01073 01074 01075 elementList.remove(elementList.indexOf(element)); 01076 iterElement = elementList.iterator(); 01077 } 01078 01079 /* Clean all children function */ 01080 Vector<Element> tmpVarVector = new Vector<Element>(); 01081 for(Function function : functionVector) 01082 { 01083 for(Element variable : function.outputVars) 01084 { 01085 if(!varVector.contains(variable)) 01086 tmpVarVector.add(variable); 01087 } 01088 function.outputVars.removeAll(tmpVarVector); 01089 if(function instanceof IfThenElse) 01090 { 01091 clean(((IfThenElse)function).getBodyTrue() , varVector); 01092 clean(((IfThenElse)function).getBodyFalse() , varVector); 01093 } 01094 else 01095 clean(function.body, varVector); 01096 } 01097 } 01098 01105 public void updateConditionsFor(HashMap<String, ReadWriteIndex> tmpVar, HashMap<String, ReadWriteIndex> cptVar, LoopFor loopFor) 01106 { 01107 /* Update the end simple variable if it's used into the loop */ 01108 String name = getTrueName(loopFor.getEnd().getName()); 01109 if(cptVar.get(name) != null && ((ReadWriteIndex)tmpVar.get(name)).getReadIndex() != ((ReadWriteIndex)cptVar.get(name)).getReadIndex()-1) 01110 loopFor.setEnd((SimpleVariable)createVar(name +"_m2m_"+(((ReadWriteIndex)cptVar.get(name)).getReadIndex())).lastElement()); 01111 01112 /* Update the increment simple variable if it's used into the loop */ 01113 name = getTrueName(loopFor.getIncr().getName()); 01114 if(cptVar.get(name) != null && ((ReadWriteIndex)tmpVar.get(name)).getReadIndex() != ((ReadWriteIndex)cptVar.get(name)).getReadIndex()-1) 01115 loopFor.setIncr((SimpleVariable)createVar(name +"_m2m_"+(((ReadWriteIndex)cptVar.get(name)).getReadIndex())).lastElement()); 01116 } 01117 01118 01125 public void updateConditionsWhile(HashMap<String, ReadWriteIndex> tmpVar, HashMap<String, ReadWriteIndex> cptVar, LoopWhile loopWhile) 01126 { 01127 String name; 01128 Element e; 01129 01130 /* Go throught all operation of the condition */ 01131 for(Element op : loopWhile.getCond()) 01132 { 01133 /* Udate the first input of the operation */ 01134 for(int i = 0; i < ((Operation)op).getInput().size(); i++) 01135 { 01136 e = ((Operation)op).getInputAt(i); 01137 name = getTrueName(e.getName()); 01138 if(cptVar.get(name) != null && !name.equals(TMP_NAME) && ((ReadWriteIndex)tmpVar.get(name)).getReadIndex() != ((ReadWriteIndex)cptVar.get(name)).getReadIndex()-1) 01139 ((Operation)op).setInputAt(i, ((SimpleVariable)createVar(name +"_m2m_"+(((ReadWriteIndex)cptVar.get(name)).getReadIndex())).lastElement())); 01140 } 01141 } 01142 } 01143 01144 01149 private void setCost(Function func) 01150 { 01151 /* Initialize the cost of input varaible */ 01152 for(Element e : func.getInput()) 01153 ((SimpleVariable)e).setCost(0); 01154 01155 // Set set the cost of body elements 01156 func.setMaxCost(setCost(func.getBody())); 01157 } 01158 01163 private int setCost(Vector<Element> elements) 01164 { 01165 int maxCost = 0; 01166 int tempMaxCost = 0; 01167 01168 for(Element e : elements) { 01169 if(e instanceof Operation) 01170 tempMaxCost = setCost((Operation)e); 01171 else if(e instanceof IfThenElse) 01172 tempMaxCost = setCost((IfThenElse)e); 01173 else if (e instanceof LoopFor) { 01174 LoopFor loop = (LoopFor)e; 01175 tempMaxCost = setCost(((LoopFor)e).getBody()); 01176 ((SimpleVariable)loop.getIterOperation().getOutputAt(0)).setCost(loop.getIterOperation().getBlock().latencyTime()+1); 01177 loop.setMaxCost(tempMaxCost); 01178 loop.setRelativeMaxCost(); 01179 } 01180 01181 maxCost = maxCost < tempMaxCost ? tempMaxCost : maxCost; 01182 } 01183 01184 return maxCost; 01185 } 01186 01192 private int setCost(Operation op) 01193 { 01194 int maxCost = 0; 01195 int tmpCost = 0; 01196 Vector<SimpleVariable> inputs = new Vector<SimpleVariable>(); 01197 Vector<Vector<Element>> variablesDependencies = new Vector<Vector<Element>>(); 01198 Vector<Element> resultDependencies = new Vector<Element>(); 01199 01200 /* Initialisation of inputs */ 01201 for(int i = 0; i < op.getInput().size(); i++) 01202 inputs.add((SimpleVariable) op.getInputAt(i)); 01203 if(op instanceof Multiplexer) 01204 inputs.add((SimpleVariable)((Multiplexer)op).getSel()); 01205 01206 /* Get the maximum input cost */ 01207 for(int i = 0; i < inputs.size(); i++) { 01208 SimpleVariable sv = inputs.elementAt(i); 01209 variablesDependencies.add(sv.getVariableDependencies()); 01210 variablesDependencies.elementAt(i).add(sv); 01211 01212 if(op.getBlock() == null) { 01213 maxCost = sv.getCost(); 01214 break; 01215 } else { 01216 tmpCost = sv.getCost() + op.getBlock().latencyTime() + 1; 01217 maxCost = tmpCost > maxCost ? tmpCost : maxCost; 01218 } 01219 } 01220 01221 /* Create result variable dependencies */ 01222 for(int i = 0; i < variablesDependencies.size(); i++) { 01223 for(int j = 0; j < variablesDependencies.elementAt(i).size(); j++) { 01224 if(!resultDependencies.contains(variablesDependencies.elementAt(i).elementAt(j))) 01225 resultDependencies.add(variablesDependencies.elementAt(i).elementAt(j)); 01226 } 01227 } 01228 01229 /* Set the maximum latency cost of outputs */ 01230 for(Element e : op.getOutput()) { 01231 ((SimpleVariable)e).setCost(maxCost); 01232 ((SimpleVariable)e).getVariableDependencies().addAll(resultDependencies); 01233 } 01234 01235 return maxCost; 01236 } 01237 01238 01243 private int setCost(IfThenElse ite) 01244 { 01245 int maxCost = 0; 01246 int tempMaxCost = 0; 01247 01248 tempMaxCost = setCost(ite.getCond()); 01249 maxCost = maxCost < tempMaxCost ? tempMaxCost : maxCost; 01250 01251 tempMaxCost = setCost(ite.getBodyTrue()); 01252 maxCost = maxCost < tempMaxCost ? tempMaxCost : maxCost; 01253 01254 tempMaxCost = setCost(ite.getBodyFalse()); 01255 maxCost = maxCost < tempMaxCost ? tempMaxCost : maxCost; 01256 01257 return maxCost; 01258 } 01259 01260 01265 public boolean getMonitor() { 01266 return (nbMonitor>0); 01267 } 01268 01269 01274 public void modifyMonitor(boolean monitor) { 01275 if (monitor) 01276 nbMonitor++; 01277 else 01278 nbMonitor--; 01279 } 01280 01281 public boolean isEmpty() { 01282 if (top==null) 01283 return true; 01284 if (top.name.isEmpty()) 01285 return true; 01286 return false; 01287 } 01288 01289 public boolean fromXml(org.w3c.dom.Element el) { 01290 01291 this.sourceMd5 = XMLUtils.getTextValue(el, "sourceMd5",""); 01292 this.sourceCode = XMLUtils.getTextValue(el,"SourceCode",""); 01293 this.top=new Function(); 01294 01295 01296 NodeList nl = el.getElementsByTagName("Function"); 01297 if(nl != null && nl.getLength() > 0) { 01298 org.w3c.dom.Element e = (org.w3c.dom.Element)nl.item(0); 01299 if (e!=null) { 01300 this.top.fromXml(e); 01301 } 01302 else 01303 this.top=null; 01304 } 01305 01306 // this.setSimulationCalcPrecision(XMLUtils.getIntValue(el,"Precision",0)); 01307 // this.setNumberOfSamples(XMLUtils.getIntValue(el,"NbSamples",5000)); 01308 // this.setInputFrequency(XMLUtils.getFloatValue(el,"InputFrequency",(float)100.0)); 01309 // this.setOutputFrequency(XMLUtils.getFloatValue(el,"OutputFrequency",(float)100.0)); 01310 // this.setSystemFrequency(XMLUtils.getFloatValue(el,"SystemFrequency",(float)100.0)); 01311 return true; 01312 } 01313 01314 public String getXmlTagName() { 01315 return "Struct"; 01316 } 01317 01318 public org.w3c.dom.Element toXml(Document dom) { 01319 org.w3c.dom.Element el; 01320 el=dom.createElement(getXmlTagName()); 01321 el.appendChild(XMLUtils.createTextElement(dom,"sourceMd5",sourceMd5)); 01322 el.appendChild(XMLUtils.createTextElement(dom, "SourceCode",sourceCode)); 01323 if (top!=null) 01324 el.appendChild(top.toXml(dom,false)); 01325 /*int monindex=0; 01326 for(m2m.backend.structure.Element e : top.inputVars) { 01327 if (e.getMonitor()) { 01328 el.appendChild(XMLUtils.createTextElement(dom,"Monitor"+monindex,e.getName())); 01329 monindex++; 01330 } 01331 } 01332 for(m2m.backend.structure.Element e : top.outputVars) { 01333 if (e.getMonitor()) { 01334 el.appendChild(XMLUtils.createTextElement(dom,"Monitor"+monindex,e.getName())); 01335 monindex++; 01336 } 01337 } 01338 for(m2m.backend.structure.Element e : top.internalVars) { 01339 if (e.getMonitor()) { 01340 el.appendChild(XMLUtils.createTextElement(dom,"Monitor"+monindex,e.getName())); 01341 monindex++; 01342 } 01343 } 01344 */ 01345 return el; 01346 } 01347 01348 01353 public Vector<Element> postParse(Function function) 01354 { 01355 Vector<Element> elements = new Vector<Element>(); 01356 Vector<Element> removeElements = new Vector<Element>(); 01357 01358 /* Add inputs variables to internals variables */ 01359 if(function instanceof LoopFor) { 01360 for(Element e : function.getInput()) 01361 function.addInternalVar(e); 01362 } 01363 01364 elements = postParse(function.getBody()); 01365 01366 /* Remove all unused element in the function level */ 01367 for(int i = 0; i < function.getInternalVars().size(); i++) { 01368 if(!elements.contains(function.getInternalVars().elementAt(i)) && !function.inputVars.contains(function.getInternalVars().elementAt(i))) 01369 removeElements.add(function.getInternalVars().elementAt(i)); 01370 } 01371 01372 function.getInternalVars().removeAll(removeElements); 01373 01374 return elements; 01375 } 01376 01377 01383 public Vector<Element> postParse(Vector<Element> vector) 01384 { 01385 Vector<Element> elements = new Vector<Element>(); 01386 01387 for(Element e : vector) { 01388 if(e instanceof Operation) { 01389 for(Element input : ((Operation)e).getInput()) { 01390 if(!elements.contains(input)) 01391 elements.add(input); 01392 } 01393 for(Element output : ((Operation)e).getOutput()) { 01394 if(!elements.contains(output)) 01395 elements.add(output); 01396 } 01397 } 01398 else if (e instanceof IfThenElse) { 01399 elements.addAll(postParse(((IfThenElse)e).getBodyFalse())); 01400 elements.addAll(postParse(((IfThenElse)e).getBodyTrue())); 01401 elements.addAll(postParse(((IfThenElse)e).getCond())); 01402 } 01403 else if(e instanceof Function){ 01404 postParse((Function)e); 01405 } 01406 } 01407 01408 return elements; 01409 } 01410 }