Debian Codegraph @

lib/mlrisc-lib/MLRISC/mltree/build-rtl.sml

raw
1 (*
2 * This takes a bunch of RTL and build a database that can be reused.
3 *)
4 structure BuildRTL : BUILD_RTL =
5 struct
6 structure RTL = MLTreeRTL
7 structure T = RTL.T
8
9 type ty = int
10
11 fun newOper name = ref{name=name,hash=0w0,attribs=0w0}
12
13 fun wordConst ty w = T.LI32(w)
14 fun intConst ty i = wordConst ty (Word32.fromInt i)
15
16 fun error msg = MLRiscErrorMsg.error("BuildRTL",msg)
17
18 fun fetch ty loc = T.REXT(ty,RTL.FETCH loc)
19
20 val wildCard = newOper "?"
21
22 fun op:= ty (x,y) = T.EXT(RTL.ASSIGN(x,y))
23 val noregion = T.LI 0
24 fun $ (k,ty) addr = RTL.CELL(k,ty,addr,noregion)
25 fun $$ (k,ty) (addr,region) = RTL.CELL(k,ty,addr,region)
26
27 fun aggb (t1,t2) cell = RTL.AGG(t2,RTL.BIG_ENDIAN,cell)
28 fun aggl (t1,t2) cell = RTL.AGG(t2,RTL.LITTLE_ENDIAN,cell)
29 fun idaggr t cell = RTL.AGG(t,RTL.LITTLE_ENDIAN,cell)
30 fun copy ty (dst,src) = T.COPY(ty,[],[])
31 val dummyTy = 32
32
33 fun ! (t,x,y) = T.REXT(t,RTL.ARG(x,y))
34
35 (* Integer operators *)
36 fun unary f ty x = f(ty,x)
37 fun binary f ty (x, y) = f(ty,x,y)
38 fun ternary f ty (x, y, z) = f(ty, x, y, z)
39
40 fun operand ty opn = opn
41 fun label ty label = label
42 fun immed ty imm = imm
43
44 datatype kind = GP | FP | CC
45
46 fun newOp name =
47 let val oper = newOper name
48 in fn xs => T.REXT(32,RTL.OP(oper,xs)) : RTL.exp
49 end
50
51 val newCond = newOp
52
53 fun sx (t1,t2) e = T.CVTI2I(t2,T.SIGN_EXTEND,t1,e)
54 fun zx (t1,t2) e = T.CVTI2I(t2,T.ZERO_EXTEND,t1,e)
55 fun ? ty = T.REXT(ty,RTL.OP(wildCard,[]))
56 fun forall t e = T.REXT(t,RTL.FORALL e)
57
58 fun bitslice t2 ranges e =
59 let val t1 = foldr (fn ((a,b),l) => b-a+1+l) 0 ranges
60 val r = map (fn (a,b) => {from=T.LI a,to=T.LI b}) ranges
61 in T.REXT(t1,RTL.SLICE(r,t2,e)) end
62
63 val not = T.NOT
64 val False = T.FALSE
65 val True = T.TRUE
66
67 val op + = binary T.ADD
68 val op - = binary T.SUB
69 val muls = binary T.MULS
70 val mulu = binary T.MULU
71 val divs = ternary T.DIVS
72 val divu = binary T.DIVU
73 val rems = ternary T.REMS
74 val remu = binary T.REMU
75 fun ~ ty x = (op - ty) (intConst ty 0,x)
76
77 val andb = binary T.ANDB
78 val orb = binary T.ORB
79 val xorb = binary T.XORB
80 val notb = unary T.NOTB
81 val << = binary T.SLL
82 val >> = binary T.SRL
83 val ~>> = binary T.SRA
84 fun eqvb ty (x,y) = notb ty (xorb ty (x,y))
85
86 (* Trapping operators *)
87 val addt = binary T.ADDT
88 val subt = binary T.SUBT
89 val mult = binary T.MULT
90 val divt = binary T.DIVT
91 val remt = binary T.REMT
92
93 fun cond ty (x,y,z) = T.COND(ty, x, y, z)
94
95 (* Integer comparisons *)
96 fun cmp cond ty (x,y) = T.CMP(ty,cond,x,y)
97
98 val == = cmp T.EQ
99 val op <> = cmp T.NE
100 val op > = cmp T.GT
101 val op < = cmp T.LT
102 val op <= = cmp T.LE
103 val op >= = cmp T.GE
104 val ltu = cmp T.LTU
105 val leu = cmp T.LEU
106 val gtu = cmp T.GTU
107 val geu = cmp T.GEU
108
109 (* Floating point operators *)
110 fun funary f =
111 let val oper = newOper f
112 in fn ty => fn x => T.REXT(ty,RTL.OP(oper,[x]))
113 end
114 fun fbinary f =
115 let val oper = newOper f
116 in fn ty => fn (x,y) => T.REXT(ty,RTL.OP(oper,[x, y]))
117 end
118
119 val fadd = fbinary "FADD"
120 val fsub = fbinary "FSUB"
121 val fmul = fbinary "FMUL"
122 val fdiv = fbinary "FDIV"
123 val fabs = funary "FABS"
124 val fneg = funary "FNEG"
125 val fsqrt = funary "FSQRT"
126
127 (* Floating point comparisons *)
128 fun fcmp fcond =
129 let val name = T.Basis.fcondToString fcond
130 val oper = newOper name
131 in fn ty => fn (x,y) =>
132 T.CMP(ty,T.NE,T.REXT(ty,RTL.OP(oper,[x,y])),T.LI 0)
133 end
134
135 val |?| = fcmp T.?
136 val |!<=>| = fcmp T.!<=>
137 val |==| = fcmp T.==
138 val |?=| = fcmp T.?=
139 val |!<>| = fcmp T.!<>
140 val |!?>=| = fcmp T.!?>=
141 val |<| = fcmp T.<
142 val |?<| = fcmp T.?<
143 val |!>=| = fcmp T.!>=
144 val |!?>| = fcmp T.!?>
145 val |<=| = fcmp T.<=
146 val |?<=| = fcmp T.?<=
147 val |!>| = fcmp T.!>
148 val |!?<=| = fcmp T.!?<=
149 val |>| = fcmp T.>
150 val |?>| = fcmp T.?>
151 val |!<=| = fcmp T.!<=
152 val |!?<| = fcmp T.!?<
153 val |>=| = fcmp T.>=
154 val |?>=| = fcmp T.?>=
155 val |!<| = fcmp T.!<
156 val |!?=| = fcmp T.!?=
157 val |<>| = fcmp T.<>
158 val |!=| = fcmp T.!=
159 val |!?| = fcmp T.!?
160 val |<=>| = fcmp T.<=>
161 val |?<>| = fcmp T.?<>
162
163 (* Action combinators *)
164 fun ||(a,b) = T.EXT(RTL.PAR(a,b))
165 val Nop = T.SEQ []
166 fun Jmp ty e = T.JMP([],e,[])
167 fun Call ty e = T.CALL{funct=e,targets=[],defs=[],uses=[],
168 cdefs=[],cuses=[],region= ~1}
169 val Ret = T.RET([],[])
170 fun If(x,y,z) = T.IF([],x,y,z)
171
172 fun map ty = List.map
173 end