1 kkz      1.1.2.1  // EventDrivenCode.java, created Fri Nov 12 14:40:55 1999 by kkz
  2 cananian 1.1.2.12 // Copyright (C) 1999 Karen K. Zee <kkz@alum.mit.edu>
  3 kkz      1.1.2.1  // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 kkz      1.1.2.1  package harpoon.Analysis.EventDriven;
  5 kkz      1.1.2.1  
  6 kkz      1.1.2.1  import harpoon.ClassFile.HClass;
  7 kkz      1.1.2.1  import harpoon.ClassFile.HCode;
  8 cananian 1.1.2.11 import harpoon.ClassFile.HCodeAndMaps;
  9 kkz      1.1.2.1  import harpoon.ClassFile.HMethod;
 10 bdemsky  1.1.2.6  import harpoon.ClassFile.Loader;
 11 bdemsky  1.1.2.6  import harpoon.ClassFile.Linker;
 12 kkz      1.1.2.1  import harpoon.IR.Quads.CALL;
 13 kkz      1.1.2.1  import harpoon.IR.Quads.Code;
 14 kkz      1.1.2.1  import harpoon.IR.Quads.FOOTER;
 15 kkz      1.1.2.1  import harpoon.IR.Quads.HEADER;
 16 kkz      1.1.2.1  import harpoon.IR.Quads.METHOD;
 17 bdemsky  1.1.2.2  import harpoon.IR.Quads.THROW;
 18 kkz      1.1.2.1  import harpoon.IR.Quads.Quad;
 19 kkz      1.1.2.1  import harpoon.IR.Quads.RETURN;
 20 kkz      1.1.2.1  import harpoon.Temp.Temp;
 21 bdemsky  1.1.2.2  import harpoon.Temp.TempFactory;
 22 kkz      1.1.2.1  
 23 kkz      1.1.2.1  /**
 24 kkz      1.1.2.1   * <code>EventDrivenCode</code>
 25 bdemsky  1.1.2.6   *
 26 cananian 1.1.2.12  * @author Karen K. Zee <kkz@alum.mit.edu>
 27 cananian 1.3       * @version $Id: EventDrivenCode.java,v 1.3 2003/03/11 18:22:21 cananian Exp $
 28 kkz      1.1.2.1   */
 29 bdemsky  1.1.2.7  public class EventDrivenCode extends harpoon.IR.Quads.QuadSSI {
 30 kkz      1.1.2.1      /** Creates a <code>EventDrivenCode</code>. */
 31 bdemsky  1.1.2.6      public EventDrivenCode(HMethod parent, HMethod newmain, Temp[] params,
 32 bdemsky  1.1.2.6                             Linker linker) {
 33 kkz      1.1.2.1          super(parent, null);
 34 bdemsky  1.1.2.6          this.quads = buildCode(newmain, params, linker);
 35 kkz      1.1.2.1      }
 36 kkz      1.1.2.1  
 37 kkz      1.1.2.1      private EventDrivenCode(HMethod parent) {
 38 kkz      1.1.2.1          super(parent, null);
 39 kkz      1.1.2.1      }
 40 kkz      1.1.2.1  
 41 kkz      1.1.2.1      /** Clone this code representation.  The clone has its own copy of
 42 kkz      1.1.2.1       *  the quad graph. 
 43 kkz      1.1.2.1       */
 44 cananian 1.3          public HCodeAndMaps<Quad> clone(HMethod newMethod) {
 45 cananian 1.1.2.11         return cloneHelper(new EventDrivenCode(newMethod));
 46 kkz      1.1.2.1      }
 47 kkz      1.1.2.1  
 48 kkz      1.1.2.1      /**
 49 kkz      1.1.2.1       * Return the name of this code view.
 50 kkz      1.1.2.1       * @return the name of the <code>parent</code>'s code view.
 51 kkz      1.1.2.1       */
 52 kkz      1.1.2.1      public String getName() {
 53 bdemsky  1.1.2.7          return harpoon.IR.Quads.QuadSSI.codename;
 54 kkz      1.1.2.1      }
 55 kkz      1.1.2.1  
 56 bdemsky  1.1.2.6      private Quad buildCode(HMethod newmain, Temp[] params, Linker linker) {
 57 bdemsky  1.1.2.8          TempFactory tf=qf.tempFactory();
 58 kkz      1.1.2.1          System.out.println("Entering EventDrivenCode.buildCode()");
 59 bdemsky  1.1.2.7          HEADER h = new HEADER(qf, null);
 60 bdemsky  1.1.2.7          FOOTER f = new FOOTER(qf, null, 4);
 61 kkz      1.1.2.1          Quad.addEdge(h, 0, f, 0);
 62 bdemsky  1.1.2.8          Temp[] params2=new Temp[params.length];
 63 bdemsky  1.1.2.8          for (int i=0;i<params.length;i++)
 64 bdemsky  1.1.2.8              params2[i]=new Temp(tf,params[i].name());
 65 bdemsky  1.1.2.8          METHOD m = new METHOD(qf, null, params2, 1);
 66 kkz      1.1.2.1          Quad.addEdge(h, 1, m, 0);
 67 kkz      1.1.2.1  
 68 kkz      1.1.2.1          if (newmain == null) System.out.println("newmain is null");
 69 kkz      1.1.2.1  
 70 kkz      1.1.2.1          // call to new main method (mainAsync)
 71 bdemsky  1.1.2.2          Temp t=new Temp(tf);
 72 bdemsky  1.1.2.2          Temp exc=new Temp(tf);
 73 bdemsky  1.1.2.2  
 74 bdemsky  1.1.2.8          CALL c1 = new CALL(qf, null, newmain, params2, t, exc, true,
 75 kkz      1.1.2.1                             false, new Temp[0]);
 76 bdemsky  1.1.2.7          THROW throwq=new THROW(qf, null, exc);
 77 bdemsky  1.1.2.2          Quad.addEdge(c1, 1, throwq,0);
 78 kkz      1.1.2.1          Quad.addEdge(m, 0, c1, 0);
 79 kkz      1.1.2.1  
 80 kkz      1.1.2.1          // call to scheduler
 81 kkz      1.1.2.1          HMethod schloop = null;
 82 kkz      1.1.2.1          try {
 83 kkz      1.1.2.1              final HClass sch = 
 84 bdemsky  1.1.2.6                  linker.forName("harpoon.Analysis.ContBuilder.Scheduler");
 85 kkz      1.1.2.1              schloop = sch.getDeclaredMethod("loop", new HClass[0]);
 86 kkz      1.1.2.1          } catch (Exception e) {
 87 kkz      1.1.2.1              System.err.println
 88 kkz      1.1.2.1                  ("Cannot find harpoon.Analysis.EventDriven.Scheduler");
 89 kkz      1.1.2.1          }
 90 bdemsky  1.1.2.7          
 91 kkz      1.1.2.1  
 92 bdemsky  1.1.2.2          Temp exc2=new Temp(tf);
 93 bdemsky  1.1.2.7          CALL c2 = new CALL(qf, null, schloop, new Temp[0], null, exc2, 
 94 kkz      1.1.2.1                             true, false, new Temp[0]);
 95 bdemsky  1.1.2.7          THROW throwq2=new THROW(qf, null, exc2);
 96 kkz      1.1.2.1          Quad.addEdge(c1, 0, c2, 0);
 97 bdemsky  1.1.2.4          Quad.addEdge(c2, 1, throwq2,0);
 98 kkz      1.1.2.1  
 99 bdemsky  1.1.2.3  
100 bdemsky  1.1.2.7          RETURN r = new RETURN(qf, null, null);
101 bdemsky  1.1.2.3          Quad.addEdge(c2,0,r,0);
102 kkz      1.1.2.1          Quad.addEdge(r, 0, f, 1);
103 bdemsky  1.1.2.2          Quad.addEdge(throwq,0,f,2);
104 bdemsky  1.1.2.2          Quad.addEdge(throwq2,0, f,3);
105 kkz      1.1.2.1          System.out.println("Leaving EventDrivenCode.buildCode()");
106 kkz      1.1.2.1          return h;
107 kkz      1.1.2.1      }    
108 cananian 1.2      }