1 pnkfelix 1.1.2.1  // CodeGeneratorGenerator.java, created Thu Jun 24 20:08:33 1999 by pnkfelix
  2 pnkfelix 1.1.2.1  // Copyright (C) 1999 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.Tools.PatMat;
  5 pnkfelix 1.1.2.1  
  6 pnkfelix 1.1.2.3  import java.io.PrintWriter;
  7 pnkfelix 1.1.2.1  
  8 pnkfelix 1.1.2.1  /**
  9 pnkfelix 1.1.2.1   * <code>CodeGeneratorGenerator</code> generates a java program to
 10 pnkfelix 1.1.2.1   * convert <code>Tree</code>s into <code>Instr</code> sequences.  
 11 pnkfelix 1.1.2.1   *
 12 pnkfelix 1.1.2.1   * A Code Generator Generator (CGG) takes a set of instruction pattern
 13 pnkfelix 1.1.2.4   * tiles and constructs a java source file for a utility class
 14 pnkfelix 1.1.2.4   * containing a method <code>codegen(TreeCode)</code> which will
 15 pnkfelix 1.1.2.4   * generate an <code>HCode</code> in <code>Instr</code> form from a
 16 pnkfelix 1.1.2.4   * <code>TreeCode</code>.  In short, a CGG generates a Code Generator.
 17 pnkfelix 1.1.2.1   * 
 18 cananian 1.1.2.17  * @see harpoon.Backend.Generic.CodeGen
 19 pnkfelix 1.1.2.1   * @author  Felix S. Klock II <pnkfelix@mit.edu>
 20 cananian 1.5       * @version $Id: CodeGeneratorGenerator.java,v 1.5 2004/02/08 01:59:54 cananian Exp $ */
 21 pnkfelix 1.1.2.1  public abstract class CodeGeneratorGenerator {
 22 pnkfelix 1.1.2.1  
 23 pnkfelix 1.1.2.6      private static final String TREE_TreeCode = "harpoon.IR.Tree.TreeCode";
 24 pnkfelix 1.1.2.11     private static final String TREE_Code = "harpoon.IR.Tree.Code";
 25 pnkfelix 1.1.2.16     private static final String TREE_Data = "harpoon.IR.Tree.Data";
 26 pnkfelix 1.1.2.11     private static final String GENERIC_Code = "harpoon.Backend.Generic.Code";
 27 pnkfelix 1.1.2.13     private static final String ASSEM_Instr = "harpoon.IR.Assem.Instr";
 28 pnkfelix 1.1.2.14     private static final String ASSEM_InstrFactory = 
 29 pnkfelix 1.1.2.14         "harpoon.IR.Assem.InstrFactory";
 30 pnkfelix 1.1.2.22     private static final String UTIL_List = "java.util.List";
 31 pnkfelix 1.1.2.6  
 32 pnkfelix 1.1.2.1      /** The machine specification that the CodeGenerators outputted by
 33 pnkfelix 1.1.2.1          <code>this</code> will target.  
 34 pnkfelix 1.1.2.1      */
 35 pnkfelix 1.1.2.1      protected Spec spec;
 36 pnkfelix 1.1.2.1      
 37 pnkfelix 1.1.2.1      /** Names the class that <code>this</code> outputs. */
 38 pnkfelix 1.1.2.1      private String className;
 39 pnkfelix 1.1.2.1  
 40 pnkfelix 1.1.2.1      /** Creates a <code>CodeGeneratorGenerator</code>. 
 41 pnkfelix 1.1.2.1          <BR> <B>requires:</B> <OL>
 42 pnkfelix 1.1.2.1               <LI> <code>s</code> follows the standard template for
 43 pnkfelix 1.1.2.1                    defining a machine specification. 
 44 pnkfelix 1.1.2.1               <LI> if <code>s</code> contains Java statements that rely
 45 pnkfelix 1.1.2.1                    on knowledge about the class to be produced (such as
 46 pnkfelix 1.1.2.1                    a Constructor implementation) then the class named
 47 pnkfelix 1.1.2.1                    must match the <code>className</code> parameter.
 48 pnkfelix 1.1.2.1               </OL>
 49 pnkfelix 1.1.2.1          <BR> <B>effects:</B> Creates a new
 50 pnkfelix 1.1.2.1               <code>CodeGeneratorGenerator</code> and associates the
 51 pnkfelix 1.1.2.1               machine specification <code>s</code> with the newly
 52 pnkfelix 1.1.2.1               constructed <code>CodeGeneratorGenerator</code>.
 53 pnkfelix 1.1.2.3          @see <A HREF="doc-files/instr-selection-tool.html">Standard Specification Template</A>
 54 pnkfelix 1.1.2.1          @param s <code>Spec</code> defining the machine specification
 55 pnkfelix 1.1.2.1                   that the CodeGenerator output by <code>this</code>
 56 pnkfelix 1.1.2.1                   will target.
 57 pnkfelix 1.1.2.1          @param className Formal name of the class that
 58 pnkfelix 1.1.2.1                           <code>this</code> outputs. 
 59 pnkfelix 1.1.2.1      */
 60 pnkfelix 1.1.2.1      public CodeGeneratorGenerator(Spec s, String className) {
 61 pnkfelix 1.1.2.1          this.spec = s;
 62 pnkfelix 1.1.2.1          this.className = className;
 63 pnkfelix 1.1.2.1      }
 64 pnkfelix 1.1.2.1      
 65 pnkfelix 1.1.2.21     /** Returns the fully qualified name of the superclass of the
 66 pnkfelix 1.1.2.21         generated <code>CodeGen</code>.
 67 pnkfelix 1.1.2.21         The superclass should be a subclass of
 68 pnkfelix 1.1.2.21         <code>Generic.CodeGen</code> (or <code>Generic.CodeGen</code>
 69 pnkfelix 1.1.2.21         itself.  
 70 pnkfelix 1.1.2.21     */
 71 pnkfelix 1.1.2.21     protected abstract String producedClassType();
 72 pnkfelix 1.1.2.21 
 73 pnkfelix 1.1.2.1      /** Writes the Code Generator to <code>out</code>.
 74 pnkfelix 1.1.2.1          <BR> <B>modifies:</B> <code>out</code>
 75 pnkfelix 1.1.2.1          <BR> <B>effects:</B> 
 76 pnkfelix 1.1.2.1               Generates Java source for the machine specification
 77 pnkfelix 1.1.2.2               associated with <code>this</code>.  
 78 pnkfelix 1.1.2.3               Then writes the Java source code out to <code>out</code>,
 79 pnkfelix 1.1.2.3               and flushes <code>out</code> after it finishes outputting
 80 pnkfelix 1.1.2.3               the source code.
 81 pnkfelix 1.1.2.6               <BR>All of the output file is parameterized by
 82 pnkfelix 1.1.2.5               <code>this.spec</code>, with the exception of  
 83 pnkfelix 1.1.2.5               <OL> 
 84 pnkfelix 1.1.2.16              <LI>Class name.
 85 pnkfelix 1.1.2.7                   This is already defined by <code>this.className</code>.
 86 pnkfelix 1.1.2.16              <LI>Class signature.
 87 pnkfelix 1.1.2.7                   This is already hardcoded as 
 88 pnkfelix 1.1.2.11                  <code>public class <u>this.className</u> extends
 89 pnkfelix 1.1.2.11                  harpoon.Backend.Generic.CodeGen</code>. 
 90 pnkfelix 1.1.2.16              <LI>Code-gen method signature.  This is already hardcoded as 
 91 pnkfelix 1.1.2.16                  <code>public final Instr gen(harpoon.IR.Tree.Code tree,
 92 pnkfelix 1.1.2.16                        harpoon.IR.Assem.InstrFactory inf)</code>. 
 93 pnkfelix 1.1.2.16              <LI>Data-gen method signature.  This is already hardcoded as 
 94 pnkfelix 1.1.2.16                  <code>public final Instr gen(harpoon.IR.Tree.Data tree,
 95 pnkfelix 1.1.2.16                        harpoon.IR.Assem.InstrFactory inf)</code>. 
 96 pnkfelix 1.1.2.5               </OL>
 97 pnkfelix 1.1.2.1          @param out Target output device for the Java source code.
 98 pnkfelix 1.1.2.1      */
 99 pnkfelix 1.1.2.3      public void outputJavaFile(PrintWriter out) {
100 pnkfelix 1.1.2.3          out.println(spec.global_stms);
101 pnkfelix 1.1.2.11         out.println("public class " + className + 
102 pnkfelix 1.1.2.21                     " extends "+producedClassType()+" { ");
103 pnkfelix 1.1.2.3          out.println(spec.class_stms);
104 pnkfelix 1.1.2.3          
105 pnkfelix 1.1.2.16         out.println("\t/** Generates assembly code from a <code>" + TREE_Code + "</code>.");
106 pnkfelix 1.1.2.3          out.println("\t    <BR> <B>modifies:</B> <code>this</code>");
107 pnkfelix 1.1.2.3          out.println("\t    <BR> <B>effects:</B>");
108 pnkfelix 1.1.2.3          out.println("\t         Scans <code>tree</code> to find a tiling of ");
109 pnkfelix 1.1.2.3          out.println("\t         Instruction Patterns, calling auxillary methods");
110 pnkfelix 1.1.2.22         out.println("\t         and data structures as defined in the .spec file.");
111 pnkfelix 1.1.2.22         out.println("\t         Generates an associated <code>Derivation</code>");
112 pnkfelix 1.1.2.22         out.println("\t         object as the second element of the returned");
113 pnkfelix 1.1.2.22         out.println("\t         <code>List</code>.");
114 pnkfelix 1.1.2.3          out.println("\t    @param tree Set of abstract <code>Tree</code> instructions ");
115 pnkfelix 1.1.2.3          out.println("\t                that form the body of the procedure being compiled.");
116 pnkfelix 1.1.2.3          out.println("\t*/");
117 pnkfelix 1.1.2.25         out.println("\tpublic final "+UTIL_List+" cgg_genCode(final " + 
118 pnkfelix 1.1.2.18                     TREE_Code +" code, final " + ASSEM_InstrFactory +
119 pnkfelix 1.1.2.14                     " inf) {"); // method start
120 pnkfelix 1.1.2.12 
121 cananian 1.1.2.24         out.println("\t_methodPrologue_(inf);");
122 pnkfelix 1.1.2.12         out.println(spec.method_prologue_stms);
123 pnkfelix 1.1.2.1  
124 pnkfelix 1.1.2.20         outputSelectionMethod(out, false);
125 pnkfelix 1.1.2.12 
126 pnkfelix 1.1.2.12         out.println(spec.method_epilogue_stms);
127 pnkfelix 1.1.2.1  
128 pnkfelix 1.1.2.22         out.println(returnCodeStatements());
129 pnkfelix 1.1.2.22 
130 pnkfelix 1.1.2.16         out.println("\t}"); // end Code-gen method
131 pnkfelix 1.1.2.16 
132 pnkfelix 1.1.2.16         out.println("\t/** Generates assembly code from a <code>" + TREE_Data + "</code>.");
133 pnkfelix 1.1.2.16         out.println("\t    <BR> <B>modifies:</B> <code>this</code>");
134 pnkfelix 1.1.2.16         out.println("\t    <BR> <B>effects:</B>");
135 pnkfelix 1.1.2.16         out.println("\t         Scans <code>tree</code> to define a layout of ");
136 pnkfelix 1.1.2.16         out.println("\t         Instructions, calling auxillary methods");
137 pnkfelix 1.1.2.16         out.println("\t         and data structures as defined in the .Spec file");
138 pnkfelix 1.1.2.16         out.println("\t    @param tree Set of abstract <code>Tree</code> instructions ");
139 pnkfelix 1.1.2.16         out.println("\t                that form the body of the data structure being compiled.");
140 pnkfelix 1.1.2.16         out.println("\t*/");
141 pnkfelix 1.1.2.25         out.println("\tpublic final "+ASSEM_Instr+" cgg_genData(" + 
142 pnkfelix 1.1.2.18                     TREE_Data +" code, final " + ASSEM_InstrFactory + 
143 pnkfelix 1.1.2.16                     " inf) {"); // method start
144 pnkfelix 1.1.2.16 
145 cananian 1.1.2.24         out.println("\t_methodPrologue_(inf);");
146 pnkfelix 1.1.2.16         out.println(spec.method_prologue_stms);
147 pnkfelix 1.1.2.16 
148 pnkfelix 1.1.2.20         outputSelectionMethod(out, true);
149 pnkfelix 1.1.2.16 
150 pnkfelix 1.1.2.16         out.println(spec.method_epilogue_stms);
151 pnkfelix 1.1.2.16 
152 pnkfelix 1.1.2.22         out.println(returnDataStatements());
153 pnkfelix 1.1.2.22 
154 pnkfelix 1.1.2.3          out.println("\t}"); // end method
155 pnkfelix 1.1.2.16 
156 pnkfelix 1.1.2.3          out.println("}"); // end class
157 pnkfelix 1.1.2.1          out.flush();
158 pnkfelix 1.1.2.22     }
159 pnkfelix 1.1.2.22 
160 pnkfelix 1.1.2.22     protected String returnCodeStatements() {
161 pnkfelix 1.1.2.22         return 
162 cananian 1.3.2.1              "\tassert first != null : \""+
163 cananian 1.3.2.1              "Should always generate some instrs\";\n"+
164 cananian 1.5                  "\treturn net.cscott.jutil.Default.pair(first, getDerivation());";
165 pnkfelix 1.1.2.22     }
166 pnkfelix 1.1.2.22 
167 pnkfelix 1.1.2.22     protected String returnDataStatements() {
168 pnkfelix 1.1.2.22         return 
169 cananian 1.3.2.1              "\tassert first != null : \""+
170 cananian 1.3.2.1              "Should always generate some instrs\";\n"+
171 pnkfelix 1.1.2.22             "\treturn first;";
172 pnkfelix 1.1.2.1      }
173 pnkfelix 1.1.2.1  
174 pnkfelix 1.1.2.1      /** Writes the Instruction Selection Method to <code>out</code>.
175 pnkfelix 1.1.2.18 
176 pnkfelix 1.1.2.1          <BR> <B>modifies:</B> <code>out</code>
177 pnkfelix 1.1.2.18 
178 pnkfelix 1.1.2.1          <BR> <B>effects:</B>
179 pnkfelix 1.1.2.4               <BR> Generates Java source for the instruction selection
180 pnkfelix 1.1.2.3               method, not including method signature or surrounding
181 pnkfelix 1.1.2.4               braces.
182 pnkfelix 1.1.2.9               <BR>Outputs generated source to <code>out</code>.
183 pnkfelix 1.1.2.9  
184 pnkfelix 1.1.2.9          <BR> <B>Subclass Implementation Notes:</B>
185 pnkfelix 1.1.2.9               <BR> Generated method has one parameter available to be
186 pnkfelix 1.1.2.19              referenced: <code>code</code>, a <code>Tree.Code</code>
187 pnkfelix 1.1.2.3               that represents the input set of <code>Tree</code>
188 pnkfelix 1.1.2.19              <code>HCodeElement</code>s.  From this the code may
189 pnkfelix 1.1.2.19              access the <code>Frame</code>,
190 pnkfelix 1.1.2.19              <code>TreeDerivation</code>, and
191 pnkfelix 1.1.2.19              <code>IR.Tree.Tree</code> objects necessary to create
192 pnkfelix 1.1.2.19              assembly code <code>Instr</code>s using the actions
193 pnkfelix 1.1.2.19              provided by a spec file.
194 pnkfelix 1.1.2.19 
195 pnkfelix 1.1.2.6               <BR>Generated method must, for each pattern in
196 pnkfelix 1.1.2.6               <code>this.spec</code>, define variables for the action
197 pnkfelix 1.1.2.6               statements in the pattern to refer to.  These variables
198 pnkfelix 1.1.2.6               are:<OL>
199 pnkfelix 1.1.2.8               <LI>The <code>Spec.ExpId</code> objects defined in the
200 pnkfelix 1.1.2.19                  pattern, including the result_id and %extra Temp
201 pnkfelix 1.1.2.19                  objects. 
202 pnkfelix 1.1.2.6               <LI>The <code>HCodeElement</code> <code>ROOT</code>.  
203 pnkfelix 1.1.2.10                  This should be defined as the
204 pnkfelix 1.1.2.10                  <code>IR.Tree.Tree</code> element being analyzed.  To
205 pnkfelix 1.1.2.10                  reference specific fields or methods of the
206 pnkfelix 1.1.2.10                  <code>IR.Tree.Tree</code> element being analyzed, one
207 pnkfelix 1.1.2.10                  should cast <code>ROOT</code> to the appropriate
208 pnkfelix 1.1.2.10                  subclass of <code>IR.Tree.Tree</code> (though this
209 pnkfelix 1.1.2.10                  requirement may be dropped in the future).
210 pnkfelix 1.1.2.6               </OL>
211 pnkfelix 1.1.2.18 
212 pnkfelix 1.1.2.3               <BR>Generated method finds a tiling for
213 pnkfelix 1.1.2.3               <code>tree</code>, using the information in
214 pnkfelix 1.1.2.3               <code>this.spec</code> as a <code>Spec.Rule</code> tile
215 pnkfelix 1.1.2.3               source, and then runs <code>Spec.Rule.action_str</code>
216 pnkfelix 1.1.2.3               for each matching tile.
217 pnkfelix 1.1.2.18 
218 pnkfelix 1.1.2.3               <BR>Awards bonus points to subclasses that implement this
219 pnkfelix 1.1.2.3               method to output documentation for the generated method
220 pnkfelix 1.1.2.3               body. 
221 pnkfelix 1.1.2.20         @param out Target output device for the Java source code.  
222 pnkfelix 1.1.2.20         @param isData indicates if we're pattern matching code or data tables
223 pnkfelix 1.1.2.20     */
224 pnkfelix 1.1.2.20     public abstract void outputSelectionMethod(PrintWriter out, boolean isData);
225 pnkfelix 1.1.2.1  }
226 pnkfelix 1.1.2.1  
227 pnkfelix 1.1.2.1  
228 cananian 1.2