1 cananian 1.1.2.2 // RealtimeAllocationStrategy.java, created Fri Mar 23 10:31:56 2001 by wbeebee 2 wbeebee 1.1.2.1 // Copyright (C) 2001 Wes Beebee <wbeebee@mit.edu> 3 wbeebee 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 wbeebee 1.1.2.1 package harpoon.Analysis.Realtime; 5 wbeebee 1.1.2.1 6 wbeebee 1.1.2.1 import harpoon.Analysis.Maps.AllocationInformation.AllocationProperties; 7 wbeebee 1.1.2.4 import harpoon.Analysis.Realtime.Realtime; 8 wbeebee 1.1.2.1 9 wbeebee 1.1.2.1 import harpoon.Backend.Generic.Frame; 10 wbeebee 1.1.2.1 import harpoon.Backend.Runtime1.MallocAllocationStrategy; 11 wbeebee 1.1.2.1 12 wbeebee 1.1.2.1 import harpoon.ClassFile.HCodeElement; 13 wbeebee 1.1.2.1 14 wbeebee 1.1.2.4 import harpoon.IR.Tree.ALIGN; 15 wbeebee 1.1.2.4 import harpoon.IR.Tree.CONST; 16 wbeebee 1.1.2.4 import harpoon.IR.Tree.DATUM; 17 wbeebee 1.1.2.1 import harpoon.IR.Tree.DerivationGenerator; 18 wbeebee 1.1.2.4 import harpoon.IR.Tree.ESEQ; 19 wbeebee 1.1.2.1 import harpoon.IR.Tree.Exp; 20 wbeebee 1.1.2.1 import harpoon.IR.Tree.ExpList; 21 wbeebee 1.1.2.4 import harpoon.IR.Tree.JUMP; 22 wbeebee 1.1.2.4 import harpoon.IR.Tree.LABEL; 23 wbeebee 1.1.2.4 import harpoon.IR.Tree.NAME; 24 wbeebee 1.1.2.4 import harpoon.IR.Tree.SEGMENT; 25 wbeebee 1.1.2.4 import harpoon.IR.Tree.Stm; 26 wbeebee 1.1.2.1 import harpoon.IR.Tree.TreeFactory; 27 wbeebee 1.1.2.4 28 wbeebee 1.1.2.4 import harpoon.Temp.Label; 29 wbeebee 1.1.2.1 import harpoon.Temp.Temp; 30 wbeebee 1.1.2.1 31 wbeebee 1.1.2.4 import java.util.ArrayList; 32 wbeebee 1.1.2.4 import java.util.Enumeration; 33 wbeebee 1.1.2.4 import java.util.Hashtable; 34 wbeebee 1.1.2.4 import java.util.List; 35 wbeebee 1.1.2.4 36 wbeebee 1.1.2.1 /** 37 wbeebee 1.1.2.4 * <code>RealtimeAllocationStrategy</code> makes all memory allocations to go through 38 wbeebee 1.1.2.4 * <code>RTJ_malloc</code>. If DEBUG_REF is turned on, then it calls 39 wbeebee 1.1.2.4 * <code>RTJ_malloc_ref(size, fileNumber, "fileName")</code>. The const char* "fileName" 40 wbeebee 1.1.2.4 * must be emitted in a separate area, so emitStrings must be called later in the 41 wbeebee 1.1.2.4 * file to emit that data. 42 cananian 1.1.2.2 * @author Wes Beebee <wbeebee@mit.edu> 43 wbeebee 1.1.2.1 */ 44 wbeebee 1.1.2.1 45 wbeebee 1.1.2.1 public class RealtimeAllocationStrategy extends MallocAllocationStrategy { 46 wbeebee 1.1.2.4 47 wbeebee 1.1.2.4 private static Hashtable string2label = new Hashtable(); 48 wbeebee 1.1.2.4 49 wbeebee 1.1.2.1 /** Creates a <code>RealtimeAllocationStrategy</code>. 50 wbeebee 1.1.2.1 */ 51 wbeebee 1.1.2.1 public RealtimeAllocationStrategy(Frame f) { super (f, "RTJ_malloc"); } 52 wbeebee 1.1.2.1 53 wbeebee 1.1.2.4 /** Produces the Tree code corresponding to a memory allocation. 54 wbeebee 1.1.2.4 */ 55 wbeebee 1.1.2.4 public Exp memAlloc(TreeFactory tf, HCodeElement src, 56 wbeebee 1.1.2.1 DerivationGenerator dg, 57 wbeebee 1.1.2.1 AllocationProperties ap, 58 wbeebee 1.1.2.1 Exp length) { 59 wbeebee 1.1.2.4 if (Realtime.DEBUG_REF) { 60 wbeebee 1.1.2.6 return 61 wbeebee 1.1.2.6 buildAllocCall(tf, src, dg, ap, "RTJ_malloc_ref", length, 62 wbeebee 1.1.2.6 new ExpList(new CONST(tf, src, src.getLineNumber()), 63 wbeebee 1.1.2.6 new ExpList(new NAME(tf, src, 64 wbeebee 1.1.2.6 stringLabel(src.getSourceFile())), 65 wbeebee 1.1.2.6 null))); 66 wbeebee 1.1.2.4 } 67 wbeebee 1.1.2.4 return buildAllocCall(tf, src, dg, ap, "RTJ_malloc", length, null); 68 wbeebee 1.1.2.4 } 69 wbeebee 1.1.2.5 70 wbeebee 1.1.2.6 /** Return a label corresponding to the string s. 71 wbeebee 1.1.2.6 * Available only if DEBUG_REF is turned on. 72 wbeebee 1.1.2.5 */ 73 wbeebee 1.1.2.6 public static Label stringLabel(String s) { 74 wbeebee 1.1.2.6 Label label = null; 75 wbeebee 1.1.2.5 if (Realtime.DEBUG_REF) { 76 wbeebee 1.1.2.6 label = (Label)string2label.get(s); 77 wbeebee 1.1.2.6 if (label == null) { 78 wbeebee 1.1.2.6 string2label.put(s, (label=new Label())); 79 wbeebee 1.1.2.5 } 80 wbeebee 1.1.2.5 } 81 wbeebee 1.1.2.6 return label; 82 wbeebee 1.1.2.5 } 83 wbeebee 1.1.2.5 84 wbeebee 1.1.2.4 85 wbeebee 1.1.2.4 /** Emit all of the const char*'s used so far in the file and flush the list. 86 wbeebee 1.1.2.4 */ 87 wbeebee 1.1.2.4 public static Stm emitStrings(TreeFactory tf, HCodeElement src) { 88 wbeebee 1.1.2.4 List stmList = new ArrayList(); 89 wbeebee 1.3 stmList.add(new SEGMENT(tf, src, SEGMENT.TEXT)); 90 wbeebee 1.1.2.4 91 wbeebee 1.1.2.4 Enumeration keys = string2label.keys(); 92 wbeebee 1.1.2.4 while (keys.hasMoreElements()) { 93 wbeebee 1.1.2.4 String s = (String)keys.nextElement(); 94 wbeebee 1.1.2.4 stmList.add(new LABEL(tf, src, (Label)string2label.get(s), true)); 95 wbeebee 1.1.2.4 for (int i=0; i<s.length(); i++) 96 wbeebee 1.1.2.4 stmList.add(new DATUM(tf, src, new CONST(tf, src, 8, false, 97 wbeebee 1.1.2.4 (int) s.charAt(i)))); 98 wbeebee 1.1.2.4 // null terminate 99 wbeebee 1.1.2.4 stmList.add(new DATUM(tf, src, new CONST(tf, src, 8, false, 0))); 100 wbeebee 1.1.2.4 // align to proper word boundary 101 wbeebee 1.1.2.4 stmList.add(new ALIGN(tf, src, 8)); 102 wbeebee 1.1.2.4 } 103 wbeebee 1.1.2.4 string2label.clear(); 104 wbeebee 1.1.2.4 return Stm.toStm(stmList); 105 wbeebee 1.1.2.1 } 106 cananian 1.2 }