1 wbeebee  1.1.2.12 // Frame.java, created Wed Jun 28 22:25:27 2000 by cananian
  2 cananian 1.1.2.1  // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.1  // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.2.1  package harpoon.Backend.PreciseC;
  5 cananian 1.1.2.1  
  6 cananian 1.1.2.1  import harpoon.Analysis.ClassHierarchy;
  7 cananian 1.1.2.5  import harpoon.Analysis.CallGraph;
  8 wbeebee  1.1.2.9  import harpoon.Analysis.Realtime.Realtime;
  9 wbeebee  1.1.2.9  import harpoon.Analysis.Realtime.RealtimeRuntime;
 10 salcianu 1.6      import harpoon.Analysis.MemOpt.PreallocOpt;
 11 cananian 1.1.2.1  import harpoon.Backend.Generic.GCInfo;
 12 cananian 1.1.2.2  import harpoon.Backend.Generic.LocationFactory;
 13 cananian 1.1.2.1  import harpoon.Backend.Analysis.BasicGCInfo;
 14 cananian 1.1.2.2  import harpoon.ClassFile.HClass;
 15 cananian 1.1.2.1  import harpoon.ClassFile.HCodeElement;
 16 cananian 1.1.2.1  import harpoon.ClassFile.HCodeFactory;
 17 cananian 1.1.2.2  import harpoon.ClassFile.HData;
 18 cananian 1.1.2.2  import harpoon.ClassFile.HDataElement;
 19 cananian 1.1.2.1  import harpoon.ClassFile.HMethod;
 20 cananian 1.1.2.1  import harpoon.ClassFile.Linker;
 21 cananian 1.1.2.2  import harpoon.IR.Tree.ALIGN;
 22 cananian 1.1.2.2  import harpoon.IR.Tree.DATUM;
 23 cananian 1.1.2.2  import harpoon.IR.Tree.Data;
 24 cananian 1.1.2.2  import harpoon.IR.Tree.Exp;
 25 cananian 1.1.2.2  import harpoon.IR.Tree.LABEL;
 26 cananian 1.1.2.2  import harpoon.IR.Tree.MEM;
 27 cananian 1.1.2.2  import harpoon.IR.Tree.NAME;
 28 cananian 1.1.2.2  import harpoon.IR.Tree.SEGMENT;
 29 cananian 1.1.2.2  import harpoon.IR.Tree.Stm;
 30 cananian 1.1.2.2  import harpoon.IR.Tree.TreeFactory;
 31 cananian 1.1.2.2  import harpoon.Temp.Label;
 32 cananian 1.1.2.2  import harpoon.Temp.Temp;
 33 cananian 1.1.2.2  import harpoon.Temp.TempFactory;
 34 cananian 1.10     import net.cscott.jutil.Default;
 35 cananian 1.1.2.1  import harpoon.Util.Util;
 36 cananian 1.1.2.1  
 37 salcianu 1.7      import harpoon.Backend.Runtime1.AllocationStrategy;
 38 salcianu 1.7      import harpoon.Backend.Runtime1.AllocationStrategyFactory;
 39 salcianu 1.7      
 40 cananian 1.1.2.2  import java.util.ArrayList;
 41 cananian 1.1.2.2  import java.util.Iterator;
 42 cananian 1.1.2.2  import java.util.List;
 43 cananian 1.1.2.2  import java.util.Map;
 44 cananian 1.1.2.2  import java.util.Set;
 45 cananian 1.1.2.1  /**
 46 cananian 1.1.2.1   * <code>Frame</code> contains the machine/runtime information necessary
 47 cananian 1.1.2.1   * to compile for the preciseC backend.
 48 cananian 1.1.2.1   * 
 49 cananian 1.1.2.1   * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 50 cananian 1.11      * @version $Id: Frame.java,v 1.11 2004/02/08 03:20:54 cananian Exp $
 51 cananian 1.1.2.1   */
 52 cananian 1.1.2.1  public class Frame extends harpoon.Backend.Generic.Frame {
 53 cananian 1.1.2.1      private final harpoon.Backend.Generic.Runtime   runtime;
 54 cananian 1.1.2.1      private final Linker linker;
 55 cananian 1.1.2.1      private final static boolean pointersAreLong =
 56 cananian 1.1.2.1          System.getProperty("harpoon.frame.pointers", "short")
 57 cananian 1.1.2.1          .equalsIgnoreCase("long");
 58 cananian 1.1.2.1      private final static boolean is_elf = true;
 59 cananian 1.1.2.1  
 60 salcianu 1.7          public Frame(HMethod main) {
 61 salcianu 1.7              this(main, null);
 62 salcianu 1.7          }
 63 salcianu 1.7      
 64 salcianu 1.8          /** Creates a <code>Frame</code>.
 65 salcianu 1.8      
 66 salcianu 1.8              @param main main method of the program
 67 salcianu 1.8      
 68 salcianu 1.8              @param asFact Factory providing the appropriate allocation
 69 salcianu 1.8              strategy.  If <code>null</code>, we use the default,
 70 salcianu 1.8              &quot;malloc&quot; based allocation strategy. */
 71 salcianu 1.7          public Frame(HMethod main, AllocationStrategyFactory asFact) {
 72 cananian 1.1.2.1          super();
 73 cananian 1.1.2.1          linker = main.getDeclaringClass().getLinker();
 74 salcianu 1.8      
 75 salcianu 1.8              // get an appropriate allocation strategy
 76 salcianu 1.8              AllocationStrategy as = null;
 77 salcianu 1.8              if(asFact != null)
 78 salcianu 1.8                  as = asFact.getAllocationStrategy(this);
 79 salcianu 1.8              else {
 80 salcianu 1.8                  System.out.print("Allocation strategy: malloc (default)");
 81 salcianu 1.8                  // default, "malloc" allocation strategy
 82 salcianu 1.8                  as = new harpoon.Backend.Runtime1.MallocAllocationStrategy
 83 salcianu 1.8                      (this,
 84 salcianu 1.8                       System.getProperty("harpoon.alloc.func", "malloc"));
 85 salcianu 1.8              }
 86 cananian 1.3.2.2  
 87 salcianu 1.9              runtime = getRuntime(as, main);
 88 salcianu 1.9          }
 89 salcianu 1.9      
 90 salcianu 1.9      
 91 salcianu 1.9          private harpoon.Backend.Generic.Runtime getRuntime
 92 salcianu 1.9              (AllocationStrategy as, HMethod main) {
 93 salcianu 1.9              if(Realtime.REALTIME_JAVA)
 94 salcianu 1.9                  return new RealtimeRuntime(this, as, main, !is_elf);
 95 salcianu 1.9              
 96 salcianu 1.9              if(System.getProperty("harpoon.runtime","1").equals("1"))
 97 salcianu 1.9                  return 
 98 salcianu 1.9                      new harpoon.Backend.Runtime1.Runtime(this, as, main, !is_elf);
 99 salcianu 1.9              
100 salcianu 1.9              try {
101 cananian 1.3.2.2              Class c;
102 cananian 1.3.2.2              try {
103 cananian 1.3.2.2                  // try abbreviated name first
104 salcianu 1.9                      c = Class.forName("harpoon.Backend.Runtime" + 
105 salcianu 1.9                                        System.getProperty("harpoon.runtime","1") + 
106 salcianu 1.9                                        ".Runtime");
107 salcianu 1.9                      System.out.println("Using runtime " +
108 salcianu 1.9                                         System.getProperty("harpoon.runtime","1"));
109 cananian 1.3.2.2              } catch (ClassNotFoundException e) {
110 cananian 1.3.2.2                  // try full name
111 cananian 1.3.2.2                  c = Class.forName(System.getProperty("harpoon.runtime"));
112 salcianu 1.9                      System.out.println("Using runtime " +
113 salcianu 1.9                                         System.getProperty("harpoon.runtime"));
114 cananian 1.3.2.2              }
115 cananian 1.3.2.2              java.lang.reflect.Constructor cc = c.getConstructor(new Class[] {
116 cananian 1.3.2.2                  Class.forName("harpoon.Backend.Generic.Frame"),
117 cananian 1.3.2.2                  Class.forName("harpoon.Backend.Runtime1.AllocationStrategy"),
118 cananian 1.3.2.2                  Class.forName("harpoon.ClassFile.HMethod"),
119 cananian 1.3.2.2                  Boolean.TYPE });
120 salcianu 1.9                  return 
121 salcianu 1.9                      (harpoon.Backend.Generic.Runtime)
122 cananian 1.3.2.2                  cc.newInstance(new Object[] { this, as, main,
123 salcianu 1.9                                                        new Boolean(!is_elf) });
124 cananian 1.3.2.2          } catch (Throwable t) {
125 salcianu 1.9                  throw new RuntimeException("Can't use specified runtime: " + t);
126 cananian 1.3.2.2          }
127 cananian 1.1.2.1      }
128 salcianu 1.9      
129 cananian 1.1.2.1      public Linker getLinker() { return linker; }
130 cananian 1.1.2.1      public boolean pointersAreLong() { return pointersAreLong; }
131 cananian 1.1.2.1      public harpoon.Backend.Generic.CodeGen getCodeGen() { return null; }
132 cananian 1.1.2.1      public harpoon.Backend.Generic.Runtime getRuntime() { return runtime; }
133 cananian 1.1.2.2      public harpoon.Backend.Generic.RegFileInfo getRegFileInfo() {
134 cananian 1.1.2.2          return regfileinfo;
135 cananian 1.1.2.2      }
136 cananian 1.1.2.1      public harpoon.Backend.Generic.LocationFactory getLocationFactory() {
137 cananian 1.1.2.2          return locationfactory;
138 cananian 1.1.2.1      }
139 cananian 1.1.2.1      public harpoon.Backend.Generic.InstrBuilder getInstrBuilder(){return null;}
140 cananian 1.1.2.1      public harpoon.Backend.Generic.TempBuilder getTempBuilder(){ return null; }
141 cananian 1.1.2.1      public harpoon.Backend.Generic.GCInfo getGCInfo() { return null; }
142 cananian 1.1.2.1      public HCodeFactory getCodeFactory(HCodeFactory hcf) { return null; }
143 cananian 1.1.2.2  
144 cananian 1.1.2.2      private harpoon.Backend.Generic.RegFileInfo regfileinfo =
145 cananian 1.1.2.2          new harpoon.Backend.Generic.RegFileInfo() {
146 cananian 1.1.2.2              public Set calleeSave() { return null; }
147 cananian 1.1.2.2              public Set callerSave() { return null; }
148 cananian 1.1.2.2              public Set liveOnExit() { return null; }
149 cananian 1.1.2.2              public Temp[] getAllRegisters() { return null; }
150 cananian 1.1.2.2              public Temp[] getGeneralRegisters() { return null; }
151 pnkfelix 1.1.2.4              public boolean isRegister(Temp t) { return false; }
152 cananian 1.1.2.2              public Iterator suggestRegAssignment(Temp t, Map regfile) {
153 cananian 1.1.2.2                  return null;
154 cananian 1.1.2.2              }
155 cananian 1.1.2.2          };
156 cananian 1.1.2.2  
157 cananian 1.1.2.2      // simple location factory that allocates global variables for each loc.
158 cananian 1.1.2.2      final List globals = new ArrayList();
159 cananian 1.1.2.2      private LocationFactory locationfactory = new LocationFactory() {
160 cananian 1.1.2.2          public LocationFactory.Location allocateLocation(final int type) {
161 cananian 1.1.2.2              final Label l = new Label();
162 cananian 1.1.2.2              globals.add(Default.pair(l, new Integer(type)));
163 cananian 1.1.2.2              return new Location() {
164 cananian 1.1.2.2                  public Exp makeAccessor(TreeFactory tf, HCodeElement source) {
165 cananian 1.1.2.2                      return new MEM(tf, source, type, new NAME(tf, source, l));
166 cananian 1.1.2.2                  }
167 cananian 1.1.2.2              };
168 cananian 1.1.2.2          }
169 cananian 1.1.2.2          public HData makeLocationData(final harpoon.Backend.Generic.Frame f) {
170 cananian 1.3.2.1              assert f==Frame.this;
171 cananian 1.1.2.2              return new harpoon.IR.Tree.Data("location-data", f) {
172 cananian 1.1.2.2                  public HClass getHClass() { return null; }
173 cananian 1.1.2.2                  final HDataElement root;
174 cananian 1.1.2.2                  {   // initialize root:
175 cananian 1.1.2.2                      List stmlist = new ArrayList();
176 cananian 1.1.2.2                      stmlist.add(new SEGMENT(tf, null, SEGMENT.ZERO_DATA));
177 cananian 1.1.2.2                                  
178 cananian 1.11                         for (Object pairO : globals) {
179 cananian 1.11                             List pair = (List) pairO;
180 cananian 1.1.2.2                          Label l = (Label) pair.get(0);
181 cananian 1.1.2.2                          int  ty = ((Integer)pair.get(1)).intValue();
182 cananian 1.1.2.2  
183 cananian 1.1.2.2                          stmlist.add(new ALIGN(tf,null,8));
184 cananian 1.1.2.2                          stmlist.add(new LABEL(tf,null,l,true));
185 cananian 1.1.2.2                          stmlist.add(new DATUM(tf,null,ty));
186 cananian 1.1.2.2                      }
187 cananian 1.1.2.2                      this.root = (HDataElement) Stm.toStm(stmlist);
188 cananian 1.1.2.2                  }
189 cananian 1.1.2.2                  public HDataElement getRootElement() { return root; }
190 cananian 1.1.2.2              };
191 cananian 1.1.2.2          }
192 cananian 1.1.2.2      };
193 cananian 1.2      }