1 pnkfelix 1.1.2.1 // MaxMunchCG.java, created Fri Feb 11 01:26:47 2000 by pnkfelix 2 cananian 1.1.2.15 // Copyright (C) 2000 Felix S. Klock II <pnkfelix@mit.edu> 3 pnkfelix 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 pnkfelix 1.1.2.1 package harpoon.Backend.Generic; 5 pnkfelix 1.1.2.1 6 pnkfelix 1.1.2.2 import harpoon.ClassFile.HClass; 7 pnkfelix 1.1.2.4 import harpoon.ClassFile.HCodeElement; 8 pnkfelix 1.1.2.2 import harpoon.IR.Assem.Instr; 9 cananian 1.1.2.9 import harpoon.IR.Tree.TEMP; 10 pnkfelix 1.1.2.2 import harpoon.Temp.Temp; 11 pnkfelix 1.1.2.10 import harpoon.Temp.TempMap; 12 cananian 1.1.2.9 import harpoon.Temp.TempFactory; 13 pnkfelix 1.1.2.2 import harpoon.Analysis.Maps.Derivation; 14 pnkfelix 1.1.2.12 import harpoon.Analysis.Maps.TypeMap.TypeNotKnownException; 15 pnkfelix 1.1.2.2 16 pnkfelix 1.1.2.4 import harpoon.Util.Util; 17 cananian 1.5 import net.cscott.jutil.Default; 18 pnkfelix 1.1.2.4 19 pnkfelix 1.1.2.3 import java.util.Map; 20 pnkfelix 1.1.2.3 import java.util.HashMap; 21 pnkfelix 1.1.2.3 22 pnkfelix 1.1.2.1 /** 23 pnkfelix 1.1.2.1 * <code>MaxMunchCG</code> is a <code>MaximalMunchCGG</code> specific 24 pnkfelix 1.1.2.1 * extension of <code>CodeGen</code>. Its purpose is to incorporate 25 pnkfelix 1.1.2.1 * functionality common to all target architectures but specific to 26 pnkfelix 1.1.2.1 * the particular code generation strategy employed by the CGG. Other 27 pnkfelix 1.1.2.1 * <code>CodeGeneratorGenerator</code> implementations should add 28 pnkfelix 1.1.2.1 * their own extensions of <code>CodeGen</code>. 29 pnkfelix 1.1.2.1 * 30 cananian 1.1.2.15 * @author Felix S. Klock II <pnkfelix@mit.edu> 31 cananian 1.6 * @version $Id: MaxMunchCG.java,v 1.6 2004/02/08 03:20:52 cananian Exp $ */ 32 pnkfelix 1.1.2.1 public abstract class MaxMunchCG extends CodeGen { 33 pnkfelix 1.1.2.1 34 pnkfelix 1.1.2.1 /** Creates a <code>MaxMunchCG</code>. */ 35 pnkfelix 1.1.2.1 public MaxMunchCG(Frame frame) { 36 pnkfelix 1.1.2.1 super(frame); 37 pnkfelix 1.1.2.1 } 38 pnkfelix 1.1.2.2 39 pnkfelix 1.1.2.2 // first = null OR first instr passed to emit(Instr) 40 pnkfelix 1.1.2.2 protected Instr first; 41 pnkfelix 1.1.2.2 42 pnkfelix 1.1.2.2 // last = null OR last instr passed to emit(Instr) 43 pnkfelix 1.1.2.2 protected Instr last; 44 pnkfelix 1.1.2.2 45 pnkfelix 1.1.2.2 /** Emits <code>i</code> as the next instruction in the 46 pnkfelix 1.1.2.2 instruction stream. 47 pnkfelix 1.1.2.2 */ 48 pnkfelix 1.1.2.14 protected final Instr cgg_backendEmit(Instr i) { 49 pnkfelix 1.1.2.2 debug( "Emitting "+i.toString() ); 50 pnkfelix 1.1.2.2 if (first == null) { 51 pnkfelix 1.1.2.2 first = i; 52 pnkfelix 1.1.2.2 } 53 pnkfelix 1.1.2.2 // its correct that last==null the first time this is called 54 pnkfelix 1.1.2.2 i.layout(last, null); 55 pnkfelix 1.1.2.2 last = i; 56 pnkfelix 1.1.2.4 57 cananian 1.6 for (Object tO : i.defC()) { 58 cananian 1.6 Temp t = (Temp) tO; 59 pnkfelix 1.1.2.4 TypeAndDerivation td = 60 pnkfelix 1.1.2.5 (TypeAndDerivation) tempToType.get(t); 61 cananian 1.3.2.1 assert td != null : "Uh oh forgot to declare "+t+" before "+i; 62 cananian 1.1.2.8 ti2td.put(Default.pair(i, t), td); 63 pnkfelix 1.1.2.4 } 64 pnkfelix 1.1.2.4 65 pnkfelix 1.1.2.2 return i; 66 pnkfelix 1.1.2.4 } 67 pnkfelix 1.1.2.4 68 cananian 1.1.2.9 /** tempmap from tree temps to instr temps */ 69 cananian 1.1.2.9 protected Temp makeTemp(TEMP t, TempFactory tf) { 70 cananian 1.1.2.9 Temp treeTemp = t.temp; 71 cananian 1.3.2.1 assert !frame.getRegFileInfo().isRegister(treeTemp); 72 cananian 1.1.2.9 Temp instrTemp = (Temp) tempmap.get(treeTemp); 73 cananian 1.1.2.9 if (instrTemp==null) { 74 cananian 1.1.2.9 instrTemp = frame.getTempBuilder().makeTemp(t, tf); 75 cananian 1.1.2.9 tempmap.put(treeTemp, instrTemp); 76 pnkfelix 1.1.2.11 } 77 cananian 1.3.2.1 assert instrTemp.tempFactory()==tf; 78 cananian 1.1.2.9 return instrTemp; 79 cananian 1.1.2.9 } 80 cananian 1.1.2.9 private Map tempmap = new HashMap(); 81 cananian 1.1.2.9 82 pnkfelix 1.1.2.10 83 pnkfelix 1.1.2.13 protected Derivation getDerivation() { 84 cananian 1.1.2.6 final Map ti2td = this.ti2td; // keep own copy of this map. 85 pnkfelix 1.1.2.4 return new Derivation() { 86 pnkfelix 1.1.2.4 public Derivation.DList derivation(HCodeElement hce, Temp t) 87 pnkfelix 1.1.2.4 throws TypeNotKnownException { 88 cananian 1.1.2.8 TypeAndDerivation tad = 89 cananian 1.1.2.8 (TypeAndDerivation) ti2td.get( Default.pair(hce, t) ); 90 cananian 1.1.2.8 if (tad==null) throw new TypeNotKnownException(hce, t); 91 pnkfelix 1.1.2.10 92 pnkfelix 1.1.2.10 return Derivation.DList.rename(tad.dlist, new TempMap() { 93 pnkfelix 1.1.2.10 public Temp tempMap(Temp t1) { 94 pnkfelix 1.1.2.10 Temp tr = (Temp) tempmap.get(t1); 95 cananian 1.3.2.1 assert tr != null; 96 pnkfelix 1.1.2.10 return tr; 97 pnkfelix 1.1.2.10 } 98 pnkfelix 1.1.2.10 }); 99 pnkfelix 1.1.2.4 } 100 pnkfelix 1.1.2.4 101 pnkfelix 1.1.2.4 public HClass typeMap(HCodeElement hce, Temp t) 102 pnkfelix 1.1.2.4 throws TypeNotKnownException { 103 cananian 1.1.2.8 TypeAndDerivation tad = 104 cananian 1.1.2.8 (TypeAndDerivation) ti2td.get( Default.pair(hce, t) ); 105 cananian 1.1.2.8 if (tad==null) throw new TypeNotKnownException(hce, t); 106 cananian 1.1.2.8 return tad.type; 107 pnkfelix 1.1.2.4 } 108 pnkfelix 1.1.2.4 }; 109 pnkfelix 1.1.2.2 } 110 pnkfelix 1.1.2.2 111 pnkfelix 1.1.2.3 // tXi -> TypeAndDerivation 112 pnkfelix 1.1.2.3 private Map ti2td = new HashMap(); 113 pnkfelix 1.1.2.3 114 pnkfelix 1.1.2.5 private Map tempToType = new HashMap(); 115 pnkfelix 1.1.2.3 116 pnkfelix 1.1.2.5 public void declare(Temp t, HClass clz) { 117 pnkfelix 1.1.2.5 // System.out.println(t + " " + clz); 118 pnkfelix 1.1.2.5 tempToType.put(t, new TypeAndDerivation(clz)); 119 pnkfelix 1.1.2.3 } 120 pnkfelix 1.1.2.5 121 pnkfelix 1.1.2.5 public void declare(Temp t, Derivation.DList dl) { 122 pnkfelix 1.1.2.5 // System.out.println(t + " " + dl); 123 pnkfelix 1.1.2.5 tempToType.put(t, new TypeAndDerivation(dl)); 124 pnkfelix 1.1.2.5 } 125 cananian 1.1.2.6 126 cananian 1.1.2.6 protected void clearDecl() { tempToType.clear(); } 127 cananian 1.1.2.6 128 cananian 1.1.2.7 protected void _methodPrologue_(harpoon.IR.Assem.InstrFactory inf) { 129 cananian 1.1.2.6 // initialize state variables each time gen() is called 130 cananian 1.1.2.6 first = null; last = null; 131 cananian 1.1.2.6 ti2td = new HashMap(); // reset derivation information. 132 cananian 1.1.2.9 tempmap.clear(); 133 cananian 1.1.2.6 } 134 pnkfelix 1.1.2.5 135 pnkfelix 1.1.2.3 // union type for Derivation.DList and HClass 136 pnkfelix 1.1.2.3 static class TypeAndDerivation { 137 pnkfelix 1.1.2.3 Derivation.DList dlist; 138 pnkfelix 1.1.2.3 HClass type; 139 pnkfelix 1.1.2.3 TypeAndDerivation(HClass hc) { 140 pnkfelix 1.1.2.3 type = hc; 141 pnkfelix 1.1.2.3 } 142 pnkfelix 1.1.2.3 TypeAndDerivation(Derivation.DList dl) { 143 pnkfelix 1.1.2.3 dlist = dl; 144 pnkfelix 1.1.2.2 } 145 pnkfelix 1.1.2.2 } 146 pnkfelix 1.1.2.2 147 pnkfelix 1.1.2.1 148 cananian 1.2 }