1 salcianu 1.1 // MZFCompilerStage.java, created Sat Apr 12 13:07:52 2003 by salcianu
  2 salcianu 1.1 // Copyright (C) 2003 Alexandru Salcianu <salcianu@MIT.EDU>
  3 salcianu 1.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 salcianu 1.1 package harpoon.Main;
  5 salcianu 1.1 
  6 salcianu 1.1 import harpoon.ClassFile.HClass;
  7 salcianu 1.1 import harpoon.ClassFile.HMethod;
  8 salcianu 1.1 import harpoon.Analysis.Quads.QuadClassHierarchy;
  9 salcianu 1.1 import harpoon.Util.Options.Option;
 10 salcianu 1.1 
 11 salcianu 1.1 import java.util.Iterator;
 12 salcianu 1.1 import java.util.List;
 13 salcianu 1.1 import java.util.Collections;
 14 salcianu 1.1 import java.util.Arrays;
 15 salcianu 1.1 
 16 salcianu 1.1 /**
 17 salcianu 1.1  * <code>MZFCompilerStage</code>
 18 salcianu 1.1  * 
 19 salcianu 1.1  * @author  Alexandru Salcianu <salcianu@MIT.EDU>
 20 salcianu 1.2  * @version $Id: MZFCompilerStage.java,v 1.2 2003/04/22 00:09:57 salcianu Exp $
 21 salcianu 1.1  */
 22 salcianu 1.1 public class MZFCompilerStage extends CompilerStageEZ {
 23 salcianu 1.1     
 24 salcianu 1.1     /** Creates a <code>MZFCompilerStage</code>. */
 25 salcianu 1.1     public MZFCompilerStage() { super("mzf"); }
 26 salcianu 1.1 
 27 salcianu 1.1     public List/*<Option>*/ getOptions() {
 28 salcianu 1.1         // no command line options: uses system properties instead
 29 salcianu 1.1         return Collections.EMPTY_LIST;
 30 salcianu 1.1     }
 31 salcianu 1.1 
 32 salcianu 1.1     // a bit bogus: real_action will do the real tests
 33 salcianu 1.2     public boolean enabled() { return true; }
 34 salcianu 1.1 
 35 salcianu 1.1     protected void real_action() {
 36 salcianu 1.1         /* counter factory must be set up before field reducer,
 37 salcianu 1.1          * or it will be optimized into nothingness. */
 38 salcianu 1.1         if (Boolean.getBoolean("size.counters") ||
 39 salcianu 1.1             Boolean.getBoolean("mzf.counters") ||
 40 salcianu 1.1             Boolean.getBoolean("harpoon.sizeopt.bitcounters")) {
 41 salcianu 1.1             hcf = harpoon.IR.Quads.QuadNoSSA.codeFactory(hcf);
 42 salcianu 1.1             hcf = harpoon.Analysis.Counters.CounterFactory
 43 salcianu 1.1                 .codeFactory(hcf, linker, mainM);
 44 salcianu 1.1             // recompute the hierarchy after transformation.
 45 salcianu 1.1             hcf = new harpoon.ClassFile.CachingCodeFactory(hcf);
 46 salcianu 1.1             classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
 47 salcianu 1.1         }
 48 salcianu 1.1         /*--- size optimizations ---*/
 49 salcianu 1.1         if (Boolean.getBoolean("mzf.compressor")) {
 50 salcianu 1.1             // if we're going to do mzf compression, make sure that
 51 salcianu 1.1             // the MZFExternalMap methods are in the root set and
 52 salcianu 1.1             // the class hierarchy (otherwise the FieldReducer
 53 salcianu 1.1             // will stub them out as uncallable).
 54 salcianu 1.1             HClass hcx = linker.forClass
 55 salcianu 1.1                 (harpoon.Runtime.MZFExternalMap.class);
 56 salcianu 1.1             roots.addAll(Arrays.asList(hcx.getDeclaredMethods()));
 57 salcianu 1.1             classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
 58 salcianu 1.1         }
 59 salcianu 1.1         if (Boolean.getBoolean("bitwidth")) {
 60 salcianu 1.1             hcf = harpoon.IR.Quads.QuadSSI.codeFactory(hcf);
 61 salcianu 1.1             hcf = new harpoon.ClassFile.CachingCodeFactory(hcf);
 62 salcianu 1.1             // read field roots
 63 salcianu 1.1             String resource = frame.getRuntime().resourcePath
 64 salcianu 1.1                 ("field-root.properties");
 65 salcianu 1.1             System.out.println("STARTING BITWIDTH ANALYSIS");
 66 salcianu 1.1             hcf = new harpoon.Analysis.SizeOpt.FieldReducer
 67 salcianu 1.1                 (hcf, frame, classHierarchy, roots, resource)
 68 salcianu 1.1                 .codeFactory();
 69 salcianu 1.1         }
 70 salcianu 1.1         if (Boolean.getBoolean("mzf.compressor") &&
 71 salcianu 1.1             System.getProperty("mzf.profile","").length()>0) {
 72 salcianu 1.1             hcf = harpoon.IR.Quads.QuadSSI.codeFactory(hcf);
 73 salcianu 1.1             if (!Boolean.getBoolean("bitwidth"))
 74 salcianu 1.1                 // SCCOptimize makes the SimpleConstMap used by
 75 salcianu 1.1                 // ConstructorClassifier more accurate.  However, if
 76 salcianu 1.1                 // we've used the FieldReducer, we're already
 77 salcianu 1.1                 // SCCOptimized, so no need to do it again.
 78 salcianu 1.1                 hcf = harpoon.Analysis.Quads.SCC.SCCOptimize
 79 salcianu 1.1                     .codeFactory(hcf);
 80 salcianu 1.1             hcf = new harpoon.ClassFile.CachingCodeFactory(hcf);
 81 salcianu 1.1             hcf = new harpoon.Analysis.SizeOpt.MZFCompressor
 82 salcianu 1.1                 (frame, hcf, classHierarchy,
 83 salcianu 1.1                  System.getProperty("mzf.profile")).codeFactory();
 84 salcianu 1.1             // START HACK: main still creates a String[], even after the
 85 salcianu 1.1             // Compressor has split String.  So re-add String[] to the
 86 salcianu 1.1             // root-set.
 87 salcianu 1.1             roots.add(linker.forDescriptor("[Ljava/lang/String;"));
 88 salcianu 1.1             // END HACK!
 89 salcianu 1.1             classHierarchy = new QuadClassHierarchy(linker, roots, hcf);
 90 salcianu 1.1         }
 91 salcianu 1.1         /* -- add counters to all allocations? -- */
 92 salcianu 1.1         if (Boolean.getBoolean("size.counters")) {
 93 salcianu 1.1             hcf = new harpoon.Analysis.SizeOpt.SizeCounters(hcf, frame)
 94 salcianu 1.1                 .codeFactory();
 95 salcianu 1.1             hcf = new harpoon.ClassFile.CachingCodeFactory(hcf);
 96 salcianu 1.1             // pull everything through the size counter factory
 97 salcianu 1.1             for (Iterator it=classHierarchy.callableMethods().iterator();
 98 salcianu 1.1                  it.hasNext(); )
 99 salcianu 1.1                 hcf.convert((HMethod)it.next());
100 salcianu 1.1         }
101 salcianu 1.1         /* -- find mostly-zero fields -- */
102 salcianu 1.1         if (Boolean.getBoolean("mzf.counters")) {
103 salcianu 1.1             hcf = new harpoon.Analysis.SizeOpt.MostlyZeroFinder
104 salcianu 1.1                 (hcf, classHierarchy, frame).codeFactory();
105 salcianu 1.1             hcf = new harpoon.ClassFile.CachingCodeFactory(hcf);
106 salcianu 1.1             // pull everything through the 'mostly zero finder', to make
107 salcianu 1.1             // sure that all relevant counter fields show up before
108 salcianu 1.1             // we start emitting code.
109 salcianu 1.1             for (Iterator it=classHierarchy.callableMethods().iterator();
110 salcianu 1.1                  it.hasNext(); )
111 salcianu 1.1                 hcf.convert((HMethod)it.next());
112 salcianu 1.1         }
113 salcianu 1.1     }
114 salcianu 1.1 }