1 salcianu 1.1 // PreallocAllocationStrategy.java, created Thu Nov 28 20:01:25 2002 by salcianu
 2 salcianu 1.1 // Copyright (C) 2000 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.Analysis.MemOpt;
 5 salcianu 1.1 
 6 salcianu 1.7 import harpoon.Backend.Runtime1.BDWAllocationStrategy;
 7 salcianu 1.1 import harpoon.Backend.Generic.Frame;
 8 salcianu 1.1 import harpoon.ClassFile.HClass;
 9 salcianu 1.1 import harpoon.ClassFile.HCodeElement;
10 salcianu 1.6 import harpoon.Analysis.Maps.AllocationInformation.AllocationProperties;
11 salcianu 1.1 import harpoon.IR.Tree.DerivationGenerator;
12 salcianu 1.1 import harpoon.IR.Tree.Exp;
13 salcianu 1.1 import harpoon.IR.Tree.TreeFactory;
14 salcianu 1.1 import harpoon.IR.Tree.NAME;
15 salcianu 1.1 import harpoon.IR.Tree.MEM;
16 salcianu 1.1 import harpoon.IR.Tree.Type;
17 salcianu 1.3 import harpoon.IR.Quads.Quad;
18 salcianu 1.6 import harpoon.Temp.Label;
19 salcianu 1.6 import harpoon.Temp.Temp;
20 salcianu 1.4 import harpoon.Util.Util;
21 salcianu 1.1 
22 salcianu 1.5 import java.util.Map;
23 salcianu 1.5 
24 salcianu 1.1 /** <code>PreallocAllocationStrategy</code> is the allocation strategy
25 salcianu 1.1     for the Static Memory Preallocation Optimization (via Ovy's
26 salcianu 1.1     Incompatibility Analysis).  When asked to generate code that
27 salcianu 1.1     allocates memory for an allocation site, it behaves as follows:
28 salcianu 1.1 
29 salcianu 1.1     <ul>
30 salcianu 1.1 
31 salcianu 1.1     <li> If the <code>AllocationProperties</code> object for that
32 salcianu 1.1     allocation site indicates that the memory space has been
33 salcianu 1.1     preallocated, then generate code that simply reads the static
34 salcianu 1.6     variable that points to the pre-allocated chunk of memory.
35 salcianu 1.1 
36 salcianu 1.1     <li> Otherwise, use the standard allocation from
37 salcianu 1.7     <code>BDWAllocationStrategy</code> (yes, the preallocation
38 salcianu 1.7     optimization currently works only with BDW).
39 salcianu 1.1 
40 salcianu 1.1     </ul>
41 salcianu 1.1  
42 salcianu 1.1     @author  Alexandru Salcianu <salcianu@MIT.EDU>
43 salcianu 1.7     @version $Id: PreallocAllocationStrategy.java,v 1.7 2003/03/07 20:57:20 salcianu Exp $ */
44 salcianu 1.7 public class PreallocAllocationStrategy extends BDWAllocationStrategy {
45 salcianu 1.1     
46 salcianu 1.1     /** Creates a <code>PreallocAllocationStrategy</code>. */
47 salcianu 1.6     public PreallocAllocationStrategy(Frame f) { 
48 salcianu 1.7         super(f);
49 salcianu 1.1     }
50 salcianu 1.1 
51 salcianu 1.1     public Exp memAlloc(TreeFactory tf, HCodeElement source,
52 salcianu 1.1                         DerivationGenerator dg,
53 salcianu 1.1                         AllocationProperties ap,
54 salcianu 1.1                         Exp length) {
55 salcianu 1.5 
56 salcianu 1.6         Label label = ap.getLabelOfPtrToMemoryChunk();
57 salcianu 1.5 
58 salcianu 1.6         if(label != null) {
59 salcianu 1.5             Exp pointer_expr =
60 salcianu 1.6                 new MEM(tf, source,
61 salcianu 1.6                         Type.POINTER, new NAME(tf, source, label));
62 salcianu 1.5             dg.putType(pointer_expr, HClass.Void);
63 salcianu 1.6 
64 salcianu 1.5             return pointer_expr;
65 salcianu 1.1         }
66 salcianu 1.3         else
67 salcianu 1.6             return super.memAlloc(tf, source, dg, ap, length);
68 salcianu 1.1     }
69 salcianu 1.1 }