1 cananian 1.1.2.1 // MallocAllocationStrategy.java, created Wed Oct 13 13:01:18 1999 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1999 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.Runtime1;
 5 cananian 1.1.2.1 
 6 cananian 1.1.2.4 import harpoon.Analysis.Maps.AllocationInformation.AllocationProperties;
 7 cananian 1.1.2.3 import harpoon.Backend.Generic.Frame;
 8 cananian 1.1.2.2 import harpoon.ClassFile.HClass;
 9 cananian 1.1.2.1 import harpoon.ClassFile.HCodeElement;
10 cananian 1.1.2.2 import harpoon.IR.Tree.DerivationGenerator;
11 cananian 1.1.2.1 import harpoon.IR.Tree.Exp;
12 cananian 1.1.2.1 import harpoon.IR.Tree.ExpList;
13 cananian 1.1.2.1 import harpoon.IR.Tree.TreeFactory;
14 cananian 1.1.2.1 import harpoon.IR.Tree.ESEQ;
15 cananian 1.1.2.1 import harpoon.IR.Tree.NAME;
16 cananian 1.1.2.1 import harpoon.IR.Tree.NATIVECALL;
17 cananian 1.1.2.1 import harpoon.IR.Tree.TEMP;
18 cananian 1.1.2.1 import harpoon.IR.Tree.Type;
19 cananian 1.1.2.1 import harpoon.Temp.Label;
20 cananian 1.1.2.1 import harpoon.Temp.Temp;
21 cananian 1.1.2.1 /**
22 cananian 1.1.2.1  * <code>MallocAllocationStrategy</code> calls a C function with the
23 cananian 1.1.2.1  * same prototype as <code>malloc()</code> to do the allocation.
24 cananian 1.1.2.1  * 
25 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
26 cananian 1.2      * @version $Id: MallocAllocationStrategy.java,v 1.2 2002/02/25 21:02:20 cananian Exp $
27 cananian 1.1.2.1  */
28 cananian 1.1.2.1 public class MallocAllocationStrategy extends AllocationStrategy {
29 cananian 1.1.2.7     protected final Frame frame;
30 cananian 1.1.2.3     final String funcname;
31 cananian 1.1.2.3 
32 cananian 1.1.2.1     /** Creates a <code>MallocAllocationStrategy</code>.
33 cananian 1.1.2.3      * @param funcname The name of the C function to call to do the allocation.
34 cananian 1.1.2.1      */
35 cananian 1.1.2.3     public MallocAllocationStrategy(Frame f, String funcname) {
36 cananian 1.1.2.3         this.frame = f;
37 cananian 1.1.2.3         this.funcname = funcname;
38 cananian 1.1.2.1     }
39 cananian 1.1.2.2     public Exp memAlloc(TreeFactory tf, HCodeElement source,
40 cananian 1.1.2.2                         DerivationGenerator dg,
41 cananian 1.1.2.4                         AllocationProperties ap,
42 cananian 1.1.2.2                         Exp length) {
43 cananian 1.1.2.4         return buildAllocCall(tf, source, dg, ap, funcname, length, null);
44 cananian 1.1.2.4     }
45 cananian 1.1.2.4     protected Exp buildAllocCall(TreeFactory tf, HCodeElement source,
46 cananian 1.1.2.4                                  DerivationGenerator dg,
47 cananian 1.1.2.4                                  AllocationProperties ap,
48 cananian 1.1.2.4                                  String funcname, Exp length, ExpList addlArgs)
49 cananian 1.1.2.4     {
50 cananian 1.1.2.8         Label func = new Label(frame.getRuntime().getNameMap()
51 cananian 1.1.2.3                                .c_function_name(funcname));
52 cananian 1.1.2.1         Temp Tret = new Temp(tf.tempFactory(), "ma");
53 cananian 1.1.2.1         return new ESEQ
54 cananian 1.1.2.1             (tf, source,
55 cananian 1.1.2.1              new NATIVECALL
56 cananian 1.1.2.1              (tf, source,
57 cananian 1.1.2.2               (TEMP)
58 cananian 1.1.2.2               DECLARE(dg, HClass.Void/*not an obj yet, just memory*/, Tret,
59 cananian 1.1.2.2               new TEMP(tf, source, Type.POINTER, Tret)),
60 cananian 1.1.2.2               (NAME)
61 cananian 1.1.2.2               DECLARE(dg, HClass.Void/*some random c function*/,
62 cananian 1.1.2.2               new NAME(tf, source, func)),
63 cananian 1.1.2.4               new ExpList(length, addlArgs)),
64 cananian 1.1.2.2              DECLARE(dg, HClass.Void/*not an obj yet, just memory*/, Tret,
65 cananian 1.1.2.2              new TEMP(tf, source, Type.POINTER, Tret)));
66 cananian 1.1.2.2     }
67 cananian 1.1.2.7     protected static Exp DECLARE(DerivationGenerator dg, HClass hc, Exp exp) {
68 cananian 1.1.2.2         if (dg!=null) dg.putType(exp, hc);
69 cananian 1.1.2.2         return exp;
70 cananian 1.1.2.2     }
71 cananian 1.1.2.7     protected static Exp DECLARE(DerivationGenerator dg, HClass hc, Temp t,
72 cananian 1.1.2.2                                Exp exp) {
73 cananian 1.1.2.2         if (dg!=null) dg.putTypeAndTemp(exp, hc, t);
74 cananian 1.1.2.2         return exp;
75 cananian 1.1.2.1     }
76 cananian 1.2     }