📄 wcetanalyser.java
字号:
dotout.write(sb.toString()); dotout.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } //global = mem; return ""; }}/** * It has a HashMap of WCETBasicBlocks. The class have methods that are called * from the WCETAnalyzers controlFlowGraph method. It creates the the directed * graph of wcbbs. */class WCETMethodBlock { StringBuffer lsobj; // list of BBs that are loopcontrollers ArrayList loopcontrollers = new ArrayList(); int wcet = -1; // wcet count final int mid; // a unique id across the app // event id that is incremented each time the method is invoked // in a global context int E = 0; // Basic Blocks TreeMap bbs; JavaClass jc; Method methodbcel; MethodGen mg; ControlFlowGraph cfg; ConstantPoolGen cpg; String tostr; String signature; String name; String cname; WCETAnalyser wca; String lpf = null; String dotf = null; String[] codeLines = null; // directed graph of the basic blocks int dg[][]; // method size in 32 bit words public int n = -1; public int wcetlp; static HashMap wcetvars; public WCETBasicBlock S; public WCETBasicBlock T; public boolean leaf = true; // if no invokes out from anly bb // create a bb covering the whole method // from here on we split it when necessary public void init(InstructionHandle stih, InstructionHandle endih) { WCETBasicBlock wcbb = new WCETBasicBlock(stih, endih, this, WCETBasicBlock.BNODE); S.sucbb = wcbb; bbs.put(new Integer(wcbb.getStart()), wcbb); } /** * Instanciated from from <code>SetClassInfo</code>. */ public WCETMethodBlock(Method method, JavaClass jc, WCETAnalyser wca) {//System.out.println("WCMB CONSTR putting: "+jc.getClassName()+"."+method.getName());//if(method.getName().equals("printLn"))// System.out.println("HELLO"); wca.mtowcmb.put(method,this); this.wca = wca; mid = wca.idtmp++; bbs = new TreeMap(); this.methodbcel = method; name = methodbcel.getName(); cname = jc.getClassName(); this.jc = jc;//System.out.println("sourcefilename: "+ jc.getSourceFileName()); //method length in words if (!method.isAbstract()) { n = (method.getCode().getCode().length + 3) / 4; //LImethod.getLineNumberTable() String methodId = method.getName() + method.getSignature(); String classId = jc.getClassName(); String srcFile = jc.getSourceFileName(); String filePath = (String)wca.javaFilePathMap.get(classId); if(filePath==null){ System.out.println("Did not find file:"+srcFile+" class:"+ classId+" package:"+jc.getPackageName()); System.exit(-1); } if(codeLines == null){ try { codeLines = new String[0]; BufferedReader in = new BufferedReader(new FileReader(filePath)); String str; ArrayList al = new ArrayList(); int line =0; while ((str = in.readLine()) != null) { line++; if(str.trim().startsWith("for") || str.trim().startsWith("while")){ if(str.indexOf("@WCA")==-1){ System.out.println("Error: no WCA annotation on line "+line+" in "+filePath+"."+srcFile+" \""+str+"\""); System.out.println("Default annotation inserted: \"//@WCA loop=1\""); str += "// @WCA loop=1"; } } al.add(str); } codeLines = (String[])al.toArray(new String[0]); in.close(); } catch (IOException e) { } } } else { n = -1; } } /** * Control flow analysis for one nonabstract-method. */ public void controlFlowGraph() { cpg = new ConstantPoolGen(jc.getConstantPool()); // Some methods overridden (see bottom of this file) InstConstraintVisitor icv = new AnInstConstraintVisitor(); icv.setConstantPoolGen(cpg); ExecutionVisitor ev = new ExecutionVisitor(); ev.setConstantPoolGen(cpg); mg = new MethodGen(methodbcel, jc.getClassName(), cpg); // String tostr = mg.toString();//String signature = mg.getSignature();//String name = mg.getName();//String cname = mg.getClassName(); icv.setMethodGen(mg); if (!(mg.isAbstract() || mg.isNative())) { // IF mg HAS CODE mg.getInstructionList().setPositions(true); S = new WCETBasicBlock(this,WCETBasicBlock.SNODE); bbs.put(new Integer(Integer.MIN_VALUE), S); T = new WCETBasicBlock(this,WCETBasicBlock.TNODE); // pass 0: Create basic blocks InstructionHandle ih = mg.getInstructionList().getStart(); // wcet startup: create the first full covering bb InstructionHandle ihend = mg.getInstructionList().getEnd(); init(ih, ihend); do { // create new bb (a)for branch target and (b) for sucessor Instruction ins = ih.getInstruction(); if(ih.getInstruction() instanceof InvokeInstruction && (((InvokeInstruction)ih.getInstruction()).getClassName(getCpg())).indexOf("Native")==-1){//System.out.println("classname:"+((InvokeInstruction)ih.getInstruction()).getClassName(getCpg()));//System.out.println("wca.nativeClass:"+wca.nativeClass); createBasicBlock(ih); createBasicBlock(ih.getNext()); } else if (ih.getInstruction() instanceof BranchInstruction) { InstructionHandle ihtar = ((BranchInstruction) ih.getInstruction()) .getTarget(); InstructionHandle ihnext = ih.getNext(); createBasicBlock(ihtar); if (ihnext != null) { createBasicBlock(ihnext); } } } while ((ih = ih.getNext()) != null); // Pass 1: Set the id of each block int bid = 0; // it is sorted on the (final) start pos of each block for (Iterator iter = getBbs().keySet().iterator(); iter.hasNext();) { WCETBasicBlock wbb = (WCETBasicBlock) getBbs().get( (Integer) iter.next()); wbb.calculateWcet(); wbb.setBid(bid); bid++; if(wbb.nodetype != WCETBasicBlock.SNODE && wbb.nodetype != WCETBasicBlock.TNODE){ ih = wbb.getEndih(); WCETBasicBlock wbbthis = getCoveringBB(ih); if(ih.getInstruction() instanceof BranchInstruction) { // target InstructionHandle ihtar = ((BranchInstruction) ih.getInstruction()) .getTarget(); WCETBasicBlock wbbtar = getCoveringBB(ihtar); // target wbb wbbthis.setTarbb(wbbtar); // targeter in target wbbtar.addTargeter(wbbthis); // next when the instruction is an if // TODO: What about TABLESWITCH and LOOKUPSWITCH if (ih.getInstruction() instanceof IfInstruction) { InstructionHandle ihnext = ih.getNext(); if (ihnext != null) { WCETBasicBlock wbbnxt = getCoveringBB(ihnext); // nextwbb wbbthis.setSucbb(wbbnxt); } } } else if(ih.getInstruction() instanceof ReturnInstruction){ // TODO: set T node here if(T==null) System.out.println("T=null"); if(wbbthis==null) System.out.println("wbbthis=null"); wbbthis.sucbb = T; T.addTargeter(wbbthis); } else { // set the successor InstructionHandle ihnext = ih.getNext(); if (ihnext != null) { WCETBasicBlock wbbnxt = getCoveringBB(ihnext); // nextwbb wbbthis.setSucbb(wbbnxt); } } } } bbs.put(new Integer(Integer.MAX_VALUE), T); T.bid = bid; TreeMap newbbs = new TreeMap(); for (Iterator iter = getBbs().keySet().iterator(); iter.hasNext();) { WCETBasicBlock wbb = (WCETBasicBlock) getBbs().get( (Integer) iter.next()); newbbs.put(new Integer(wbb.bid),wbb);//System.out.println("CFG putting "+wbb.bid+" in newbbs. Nodetype:"+wbb.nodetype); } bbs = newbbs; //bbs.put(new Integer(T.bid),T); } } public void link (){ // set up the loop controllers // WCMB: Arraylist of <WCBB> loopcontrollers // WCBB: ArrayList of <ArrayList> of loopchains // loopchain: ArrayList <WCBB> of WCBB in chain for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); WCETBasicBlock wcbb = (WCETBasicBlock) bbs.get(keyInt); if(wcbb.loopcontroller){ wcbb.createLoopChains(); loopcontrollers.add(wcbb); } } for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); WCETBasicBlock wcbb = (WCETBasicBlock) bbs.get(keyInt); // hook the called method to the outgoing node if(wcbb.nodetype == WCETBasicBlock.INODE){//if(wca.getMethod(wcbb.bbinvo)==null){// System.out.println("wca.getMethod(wcbb.bbinvo) == null"); //}//else// System.out.println(wca.getMethod(wcbb.bbinvo).getName());//if(wca.getWCMB(wca.getMethod(wcbb.bbinvo))==null){// System.out.println("wca.getWCMB(wca.getMethod(wcbb.bbinvo)) == null");//} wcbb.invowcmb = wca.getWCMB(wca.getMethod(wcbb.bbinvo)); if(wcbb.invowcmb==null){ //check super class(es) String bbinvotmp = wcbb.bbinvo; bbinvotmp = jc.getSuperclassName()+wcbb.bbinvo.substring(jc.getClassName().length()); wcbb.invowcmb = wca.getWCMB(wca.getMethod(bbinvotmp)); if(wcbb.invowcmb == null && jc.isAbstract()){ // check for implementations of the abstact method int classhits = 0; String mname = wcbb.bbinvo.substring(jc.getClassName().length()+1); for (int i=0;i<wca.jca.length;i++){ String newbbinvo = wca.jca[i].getClassName()+"."+mname; if(wca.getWCMB(wca.getMethod(newbbinvo)) != null){ wcbb.invowcmb = wca.getWCMB(wca.getMethod(newbbinvo)); System.out.println("implementatin of abstract method "+wcbb.bbinvo+" found in "+newbbinvo +" with wcet="+wcbb.invowcmb.wcetlp); classhits++; } } if(classhits>1){ System.out.println("WARNING: multiple implementations of "+wcbb.bbinvo +" : "+jc.getClassName()+"."+name+":"+wcbb.getIDS()+" is not supported yet"); System.exit(-1); } } if(wcbb.invowcmb!=null) wcbb.bbinvo = bbinvotmp; else{ System.out.println("Could not resolve "+wcbb.bbinvo+" for linking in "+jc.getClassName()+"."+name+":"+wcbb.getIDS()); System.out.println("jc abstract:"+jc.isAbstract()); } } leaf = false; // backtrack/* ArrayList wcbbs = new ArrayList(); wcbbs.add(wcbb); WCETBasicBlock lcwcbb = null; while(wcbbs.size()>0){ WCETBasicBlock curwcbb = (WCETBasicBlock)wcbbs.get(0); WCETBasicBlock[] tarbb = curwcbb.getInBBSArray(); for (int i=0;i<tarbb.length;i++){ if(tarbb[i].loopcontroller){ lcwcbb = tarbb[i]; wcbbs.clear(); break; } if(tarbb[i].nodetype != WCETBasicBlock.SNODE || tarbb[i].nodetype != WCETBasicBlock.TNODE){ wcbbs.add(tarbb[i]); } } }*/ } }/*//TODO: discuss with ms // if there are any path that leads back to the INODE that has only one loopcontroller for (Iterator iter = bbs.keySet().iterator(); iter.hasNext();) { Integer keyInt = (Integer) iter.next(); WCETBasicBlock wcbb = (WCETBasicBlock) bbs.get(keyInt); if(wcbb.nodetype == WCETBasicBlock.INODE && wca.global){ if(wcbb.invowcmb.leaf){ System.out.println("invowcmb from "+cname+"."+name+"("+wcbb.getIDS()+")"+":"+wcbb.invowcmb.cname+"."+wcbb.invowcmb.name+" is a leaf"); // we have a candidate // dismiss if there is an invoblock in an inner loop ArrayList okl = new ArrayList(); // ok ArrayList pl = new ArrayList(); // pending ArrayList link = new ArrayList(); link.add(wcbb); pl.add(link); while(pl.size()>0){ link = (ArrayList)pl.get(0);System.out.println("\na:"+ WU.printChain(link)); WCETBasicBlock wcbbinvo = (WCETBasicBlock)link.get(0); WCETBasicBlock wcbblast = (WCETBasicBlock)link.get(link.size()-1); //first check the chain if(wcbbnext == wcbbinvo){ System.out.println("removing pl(0). pll.size:"+pl.size()); okl.add(pl.remove(0)); // the max 1 candidate chain saved System.out.println("removed pl(0). pll.size:"+pl.size()); if(pl.size()>0) System.out.println("pl(0):"+WU.printChain((ArrayList)pl.get(0))); } else if(link.contains(wcbbnext)){ pl.remove(0); } else link.add(wcbbnext); }}} //then advance it if(wcbblast.sucbb != null && wcbblast.tarbb != null){ ArrayList linkclone = (ArrayList)link.clone(); pl.add(linkclone); } WCETBasicBlock wcbbnext = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -