Debian Codegraph @

lib/mlrisc-lib/MLRISC/mltree/machine-int.sml

raw
1 (*
2 * How to evaluate constants for various widths.
3 *
4 * Internally, we represent machine_int as a signed integer.
5 * So when we do bit or unsigned operations we have to convert to
6 * the unsigned representation first.
7 *
8 * Note: this implementation requires andb, orb, xorb etc in IntInf.
9 * You have to upgrade to the latest version of smlnj-lib if this
10 * fails to compile.
11 *)
12 local
13
14 val maxSz = 65
15
16 in
17
18 structure MachineInt : MACHINE_INT =
19 struct
20
21 structure I = IntInf
22 structure S = String
23 type machine_int = I.int
24 type sz = int
25
26 datatype div_rounding_mode = DIV_TO_ZERO | DIV_TO_NEGINF
27
28 val itow = Word.fromInt
29
30 (* Parse hex or binary, but not octal, that's for wussies *)
31 val hexToInt = StringCvt.scanString (I.scan StringCvt.HEX)
32 val binToInt = StringCvt.scanString (I.scan StringCvt.BIN)
33
34 (* Precompute some tables for faster arithmetic *)
35 local
36 val pow2table = Array.tabulate(maxSz,fn n => I.<<(1,itow n)) (* 2^n *)
37 val masktable = Array.tabulate(maxSz,
38 fn n => I.-(I.<<(1,itow n),1)) (* 2^n-1 *)
39 val maxtable = Array.tabulate(maxSz+1,
40 fn 0 => 0
41 | n => I.-(I.<<(1,itow(n-1)),1)) (* 2^{n-1}-1 *)
42 val mintable = Array.tabulate(maxSz+1,
43 fn 0 => 0
44 | n => I.~(I.<<(1,itow(n-1)))) (* -2^{n-1} *)
45 in
46
47 fun pow2 i = if i < maxSz then Array.sub(pow2table, i)
48 else I.<<(1,itow i)
49 fun maskOf sz = if sz < maxSz then Array.sub(masktable, sz)
50 else I.-(I.<<(1,itow sz),1)
51 fun maxOfSize sz = if sz < maxSz then Array.sub(maxtable, sz)
52 else I.-(I.<<(1,itow(sz-1)),1)
53 fun minOfSize sz = if sz < maxSz then Array.sub(mintable, sz)
54 else I.~(I.<<(1,itow(sz-1)))
55 end
56
57 (* queries *)
58 fun isNeg(i) = I.sign i < 0
59 fun isPos(i) = I.sign i > 0
60 fun isZero(i) = I.sign i = 0
61 fun isNonNeg(i) = I.sign i >= 0
62 fun isNonPos(i) = I.sign i <= 0
63 fun isEven(i) = isZero(I.rem(i,2))
64 fun isOdd(i) = not(isEven(i))
65
66 (* to unsigned representation *)
67 fun unsigned(sz, i) = if isNeg i then I.+(i, pow2 sz) else i
68
69 (* to signed representation *)
70 fun signed(sz, i) = if I.>(i, maxOfSize sz) then I.-(i, pow2 sz) else i
71
72 (* Narrow to the representation of a given type *)
73 fun narrow(sz, i) = signed(sz, I.andb(i, maskOf sz))
74
75 (* Recognize 0x and 0b prefix and do the right thing *)
76 fun fromString(sz, s) =
77 let val n = S.size s
78 fun conv(i,negate) =
79 if n >= 2+i andalso S.sub(s, i) = #"0" then
80 (case S.sub(s, i+1) of
81 #"x" => (hexToInt (S.substring(s,2+i,n-2-i)), negate)
82 | #"b" => (binToInt (S.substring(s,2+i,n-2-i)), negate)
83 | _ => (I.fromString s, false)
84 )
85 else (I.fromString s, false)
86 val (result, negate) =
87 if s = "" then (NONE, false)
88 else if S.sub(s, 0) = #"~" then conv(1, true)
89 else conv(0, false)
90 in case (result, negate) of
91 (SOME n, true) => SOME(narrow(sz, I.~ n))
92 | (SOME n, false) => SOME(narrow(sz, n))
93 | (NONE, _) => NONE
94 end
95
96 (* Convert types into IntInf without losing precision. *)
97 structure Cvt =
98 struct
99 structure W = Word
100 structure W32 = Word32
101 val wtoi = W.toIntX
102 val w32toi = W32.toIntX
103 val fromInt = I.fromInt
104 val fromInt32 = Int32.toLarge
105 val fromInt64 = Int64.toLarge
106 fun fromWord w = I.fromLarge(Word.toLargeInt w)
107 (*
108 fun fromWord32 w = I.+(I.<<(I.fromInt(w32toi(W32.>>(w,0w16))),0w16),
109 I.fromInt(w32toi(W32.andb(w,0wxffff))))
110 *)
111 fun fromWord32 w = I.fromLarge(Word32.toLargeInt w)
112 fun fromWord64 w = I.fromLarge(Word64.toLargeInt w)
113 end
114 (* machine_int <-> other types *)
115 fun fromInt(sz,i) = narrow(sz,Cvt.fromInt i)
116 fun fromInt32(sz,i) = narrow(sz,Cvt.fromInt32 i)
117 fun fromInt64(sz,i) = narrow(sz,Cvt.fromInt64 i)
118 fun fromWord(sz,w) = narrow(sz,Cvt.fromWord w)
119 fun fromWord32(sz,w) = narrow(sz,Cvt.fromWord32 w)
120 fun fromWord64(sz,w) = narrow(sz,Cvt.fromWord64 w)
121 fun toString(sz,i) = I.toString i
122 val toHex = I.fmt StringCvt.HEX
123 val toBin = I.fmt StringCvt.BIN
124 fun toHexString(sz, i) = "0x"^toHex(unsigned(sz, i))
125 fun toBinString(sz, i) = "0b"^toBin(unsigned(sz, i))
126 fun toInt(sz, i) = I.toInt(narrow(sz, i))
127 fun toInt32(sz, i) = Int32.fromLarge(narrow(sz, i))
128 fun toInt64(sz, i) = Int64.fromLarge(narrow(sz, i))
129 fun toWord(sz, i) = Word.fromLargeInt(I.toLarge(unsigned(sz, i)))
130 fun toWord32(sz, i) = Word32.fromLargeInt(I.toLarge(unsigned(sz, i)))
131 (*
132 let val i = unsigned(sz, i)
133 val lo = I.andb(i,0xffff)
134 val hi = I.~>>(i,0w16)
135 fun tow32 i = Word32.fromLargeInt(I.toLarge i)
136 in tow32 lo + Word32.<<(tow32 hi, 0w16) end
137 *)
138 fun toWord64(sz, i) = Word64.fromLargeInt(I.toLarge(unsigned(sz, i)))
139
140 fun hash i = Word.fromInt(I.toInt(I.andb(i,0x1fffffff)))
141
142 fun isInRange(sz, i) = I.<=(minOfSize sz,i) andalso I.<=(i,maxOfSize sz)
143
144 fun signedBinOp f (sz,i,j) = narrow(sz, f(i, j))
145
146 fun signedUnaryOp f (sz,i) = narrow(sz, f i)
147
148 fun unsignedBinOp f (sz,i,j) = narrow(sz, f(unsigned(sz,i), unsigned(sz,j)))
149
150 fun trappingUnaryOp f (sz,i) =
151 let val x = f i
152 in if isInRange(sz, x) then x else raise Overflow
153 end
154
155 fun trappingBinOp f (sz,i,j) =
156 let val x = f(i,j)
157 in if isInRange(sz, x) then x else raise Overflow
158 end
159
160 (* two's complement operators *)
161 val NEG = signedUnaryOp I.~
162 val ABS = signedUnaryOp I.abs
163 val ADD = signedBinOp I.+
164 val SUB = signedBinOp I.-
165 val MULS = signedBinOp I.*
166 fun DIVS (DIV_TO_ZERO, ty, x, y) = signedBinOp I.quot (ty, x, y)
167 | DIVS (DIV_TO_NEGINF, ty, x, y) = signedBinOp I.div (ty, x, y)
168 fun REMS (DIV_TO_ZERO, ty, x, y) = signedBinOp I.rem (ty, x, y)
169 | REMS (DIV_TO_NEGINF, ty, x, y) = signedBinOp I.mod (ty, x, y)
170 val MULU = unsignedBinOp I.*
171 val DIVU = unsignedBinOp I.div
172 (*
173 val QUOTU = unsignedBinOp I.quot
174 *)
175 val REMU = unsignedBinOp I.rem
176
177 val NEGT = trappingUnaryOp I.~
178 val ABST = trappingUnaryOp I.abs
179 val ADDT = trappingBinOp I.+
180 val SUBT = trappingBinOp I.-
181 val MULT = trappingBinOp I.*
182 fun DIVT (DIV_TO_ZERO, ty, x, y) = trappingBinOp I.quot (ty, x, y)
183 | DIVT (DIV_TO_NEGINF, ty, x, y) = trappingBinOp I.div (ty, x, y)
184
185 fun NOTB(sz,x) = narrow(sz,I.notb x)
186 fun ANDB(sz,x,y) = narrow(sz,I.andb(x,y))
187 fun ORB(sz,x,y) = narrow(sz,I.orb(x,y))
188 fun XORB(sz,x,y) = narrow(sz,I.xorb(x,y))
189 fun EQVB(sz,x,y) = narrow(sz,I.xorb(I.notb x,y))
190 fun Sll(sz,x,y) = narrow(sz,I.<<(x, y))
191 fun Srl(sz,x,y) = narrow(sz,I.~>>(unsigned(sz, x), y))
192 fun Sra(sz,x,y) = narrow(sz,I.~>>(x, y))
193 fun SLL(sz,x,y) = Sll(sz,x,toWord(sz, y))
194 fun SRL(sz,x,y) = Srl(sz,x,toWord(sz, y))
195 fun SRA(sz,x,y) = Sra(sz,x,toWord(sz, y))
196
197 fun BITSLICE(sz,sl,x) =
198 let fun slice([],n) = n
199 | slice((from,to)::sl,n) =
200 slice(sl, ORB(sz, narrow(to-from+1,
201 Srl(sz, x, Word.fromInt from)), n))
202 in slice(sl, 0)
203 end
204
205 fun bitOf(sz, i, b) =
206 toWord(1, narrow(1, Srl(sz, i, Word.fromInt b)))
207 fun byteOf(sz, i, b) =
208 toWord(8, narrow(8, Srl(sz, i, Word.fromInt(b*8))))
209 fun halfOf(sz, i, h) =
210 toWord(16, narrow(16, Srl(sz, i, Word.fromInt(h*16))))
211 fun wordOf(sz, i, w) =
212 toWord32(32, narrow(32, Srl(sz, i, Word.fromInt(w*32))))
213
214 (* type promotion *)
215 fun SX(toSz,fromSz,i) = narrow(toSz, narrow(fromSz, i))
216 fun ZX(toSz,fromSz,i) = narrow(toSz, unsigned(fromSz, narrow(fromSz, i)))
217
218 (* comparisions *)
219 fun EQ(sz,i:I.int,j) = i = j
220 fun NE(sz,i:I.int,j) = i <> j
221 fun GT(sz,i:I.int,j) = i > j
222 fun GE(sz,i:I.int,j) = i >= j
223 fun LT(sz,i:I.int,j) = i < j
224 fun LE(sz,i:I.int,j) = i <= j
225 fun LTU(sz,i,j) = unsigned(sz, i) < unsigned(sz, j)
226 fun GTU(sz,i,j) = unsigned(sz, i) > unsigned(sz, j)
227 fun LEU(sz,i,j) = unsigned(sz, i) <= unsigned(sz, j)
228 fun GEU(sz,i,j) = unsigned(sz, i) >= unsigned(sz, j)
229 (*
230 * Split an integer "i" of size "sz" into words of size "wordSize"
231 *)
232 fun split{sz, wordSize, i} =
233 let fun loop(sz, i, ws) =
234 if sz <= 0 then rev ws
235 else
236 let val w = narrow(wordSize, i)
237 val i = IntInf.~>>(i, Word.fromInt wordSize)
238 in loop(sz - wordSize, i, w::ws)
239 end
240 in loop(sz, unsigned(sz, i), [])
241 end
242
243 end
244
245 end