| 1 |
(* |
| 2 |
* A table for storing operands for a compilation unit. |
| 3 |
* We give each distinct operand a unique (negative) value number. |
| 4 |
*) |
| 5 |
functor OperandTable(Props : INSN_PROPERTIES) : OPERAND_TABLE = |
| 6 |
struct |
| 7 |
|
| 8 |
structure I = Props.I |
| 9 |
structure C = I.C |
| 10 |
structure IH = IntHashTable |
| 11 |
structure H = HashTable |
| 12 |
|
| 13 |
type valueNumber = C.cell |
| 14 |
|
| 15 |
datatype const = |
| 16 |
INT of int (* small integer operands *) |
| 17 |
| INTINF of IntInf.int (* large integer operands *) |
| 18 |
| OPERAND of I.operand (* other operand *) |
| 19 |
|
| 20 |
structure IntInfMap = |
| 21 |
RedBlackMapFn(type ord_key = IntInf.int |
| 22 |
val compare = IntInf.compare |
| 23 |
) |
| 24 |
|
| 25 |
datatype operandTable = |
| 26 |
TABLE of |
| 27 |
{ intTable : valueNumber IH.hash_table, |
| 28 |
miTable : valueNumber IntInfMap.map ref, |
| 29 |
opnTable : (I.operand,valueNumber) H.hash_table, |
| 30 |
nextValueNumber : int ref |
| 31 |
} |
| 32 |
|
| 33 |
datatype valueNumberMethods = |
| 34 |
VALUE_NUMBERING of |
| 35 |
{ int : int -> valueNumber, |
| 36 |
word : word -> valueNumber, |
| 37 |
int32 : Int32.int -> valueNumber, |
| 38 |
word32 : Word32.word -> valueNumber, |
| 39 |
intinf : IntInf.int -> valueNumber, |
| 40 |
operand : I.operand -> valueNumber |
| 41 |
} |
| 42 |
|
| 43 |
exception NoOperand |
| 44 |
exception NoConst |
| 45 |
exception NoInt |
| 46 |
exception NoIntInf |
| 47 |
|
| 48 |
val gp = C.cellkindDesc C.GP |
| 49 |
|
| 50 |
exception CONST of const |
| 51 |
|
| 52 |
fun mkConst(vn, const) = |
| 53 |
C.CELL{id=vn, an=ref [CONST const], col=ref C.PSEUDO, desc=gp} |
| 54 |
|
| 55 |
val bot = C.CELL{id= ~9999999, an=ref [], col=ref C.PSEUDO, desc=gp} |
| 56 |
val top = C.CELL{id= ~9999998, an=ref [], col=ref C.PSEUDO, desc=gp} |
| 57 |
val volatile = C.CELL{id= ~9999997, an=ref [], col=ref C.PSEUDO, desc=gp} |
| 58 |
|
| 59 |
fun create(nextValueNumber) = |
| 60 |
let |
| 61 |
val opnTable = H.mkTable(Props.hashOpn,Props.eqOpn) (32,NoOperand) |
| 62 |
val intTable = IH.mkTable (32, NoInt) |
| 63 |
val miTable = ref IntInfMap.empty |
| 64 |
|
| 65 |
fun newInt i = |
| 66 |
let val vn = !nextValueNumber (* value number *) |
| 67 |
val _ = nextValueNumber := vn - 1; |
| 68 |
val v = mkConst(vn, INT i) |
| 69 |
in IH.insert intTable (i, v) |
| 70 |
end |
| 71 |
|
| 72 |
fun init(n,0) = () |
| 73 |
| init(n,m) = (newInt n; init(n+1,m-1)) |
| 74 |
|
| 75 |
in init(0,2); |
| 76 |
TABLE{ intTable = intTable, |
| 77 |
miTable = miTable, |
| 78 |
opnTable = opnTable, |
| 79 |
nextValueNumber = nextValueNumber |
| 80 |
} |
| 81 |
end |
| 82 |
|
| 83 |
fun wordToIntInf w = IntInf.fromInt(Word.toIntX w) |
| 84 |
fun word32ToIntInf w = Word32.toLargeIntX w |
| 85 |
fun wordToInt w = Word.toIntX w |
| 86 |
fun word32ToInt w = Word32.toIntX w |
| 87 |
fun intInfToInt i = IntInf.toInt i |
| 88 |
fun intInfToInt32 i = Int32.fromLarge i |
| 89 |
fun intToIntInf i = IntInf.fromInt i |
| 90 |
fun intToInt32 i = Int32.fromInt i |
| 91 |
fun int32ToIntInf i = Int32.toLarge i |
| 92 |
fun int32ToInt i = Int32.toInt i |
| 93 |
|
| 94 |
(* Lookup the value number of a constant *) |
| 95 |
fun int(TABLE{intTable, ...}) = IH.lookup intTable |
| 96 |
|
| 97 |
fun word(TABLE{intTable, ...}) w = IH.lookup intTable (wordToInt w) |
| 98 |
|
| 99 |
fun word32(TABLE{intTable, miTable, ...}) w = |
| 100 |
IH.lookup intTable (word32ToInt w) handle Overflow => |
| 101 |
case IntInfMap.find(!miTable, word32ToIntInf w) of |
| 102 |
SOME v => v |
| 103 |
| NONE => raise NoIntInf |
| 104 |
|
| 105 |
fun int32(TABLE{intTable, miTable, ...}) w = |
| 106 |
IH.lookup intTable (int32ToInt w) handle Overflow => |
| 107 |
case IntInfMap.find(!miTable, int32ToIntInf w) of |
| 108 |
SOME v => v |
| 109 |
| NONE => raise NoIntInf |
| 110 |
|
| 111 |
fun intinf(TABLE{intTable, miTable, ...}) i = |
| 112 |
IH.lookup intTable (intInfToInt i) handle Overflow => |
| 113 |
case IntInfMap.find(!miTable,i) of |
| 114 |
SOME v => v |
| 115 |
| NONE => raise NoIntInf |
| 116 |
|
| 117 |
fun operand(TABLE{opnTable,...}) = H.lookup opnTable |
| 118 |
|
| 119 |
fun lookupValueNumbers tbl = |
| 120 |
VALUE_NUMBERING |
| 121 |
{ int = int tbl, |
| 122 |
word = word tbl, |
| 123 |
word32 = word32 tbl, |
| 124 |
int32 = int32 tbl, |
| 125 |
intinf = intinf tbl, |
| 126 |
operand = operand tbl |
| 127 |
} |
| 128 |
|
| 129 |
(* create new value numebers *) |
| 130 |
fun makeNewValueNumbers(TABLE{opnTable, |
| 131 |
nextValueNumber,intTable,miTable,...}) = |
| 132 |
let val findOpn = H.find opnTable |
| 133 |
val findInt = IH.find intTable |
| 134 |
val insertOpn = H.insert opnTable |
| 135 |
val insertInt = IH.insert intTable |
| 136 |
|
| 137 |
fun newConst(const) = |
| 138 |
let val vn = !nextValueNumber |
| 139 |
in nextValueNumber := vn - 1; |
| 140 |
mkConst(vn,const) |
| 141 |
end |
| 142 |
|
| 143 |
fun mkOpn opn = |
| 144 |
case findOpn opn of |
| 145 |
SOME v => v |
| 146 |
| NONE => let val v = newConst(OPERAND opn) |
| 147 |
in insertOpn(opn, v); v end |
| 148 |
fun mkInt i = |
| 149 |
case findInt i of |
| 150 |
SOME v => v |
| 151 |
| NONE => let val v = newConst(INT i) |
| 152 |
in insertInt(i, v); v end |
| 153 |
|
| 154 |
fun insertIntInf(i, v) = |
| 155 |
miTable := IntInfMap.insert(!miTable, i, v) |
| 156 |
|
| 157 |
fun mkIntInf' i = |
| 158 |
case IntInfMap.find(!miTable, i) of |
| 159 |
SOME v => v |
| 160 |
| NONE => let val v = newConst(INTINF i) |
| 161 |
in insertIntInf(i, v); v end |
| 162 |
|
| 163 |
fun mkIntInf i = mkInt(intInfToInt i) handle _ => mkIntInf' i |
| 164 |
|
| 165 |
fun mkWord w = mkInt(wordToInt w) |
| 166 |
|
| 167 |
fun mkInt32 i = mkInt(int32ToInt i) |
| 168 |
handle _ => mkIntInf'(int32ToIntInf i) |
| 169 |
|
| 170 |
fun mkWord32 w = mkInt(word32ToInt w) |
| 171 |
handle _ => mkIntInf'(word32ToIntInf w) |
| 172 |
in VALUE_NUMBERING |
| 173 |
{int=mkInt, |
| 174 |
word=mkWord, |
| 175 |
word32=mkWord32, |
| 176 |
int32=mkInt32, |
| 177 |
intinf=mkIntInf, |
| 178 |
operand=mkOpn |
| 179 |
} |
| 180 |
end |
| 181 |
|
| 182 |
(* value number -> const *) |
| 183 |
fun const(C.CELL{an, ...}) = |
| 184 |
let fun find(CONST c::_) = c |
| 185 |
| find(_::an) = find an |
| 186 |
| find [] = raise NoConst |
| 187 |
in find(!an) end |
| 188 |
|
| 189 |
end |