Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/MDL/mlrisc-types.sml

raw
1 (*
2 * MLRISC specific things are abstracted out here in this module.
3 *)
4
5 functor MLRiscTypes
6 (structure Comp : MDL_COMPILE
7 structure RTL : MLTREE_RTL
8 ) : MLRISC_TYPES =
9 struct
10
11 structure Ast = Comp.Ast
12 structure AstPP = Comp.AstPP
13 structure TU = Comp.TypeUtils
14 structure RTL = RTL
15 structure T = RTL.T
16 structure RW = Comp.Rewriter
17 structure C = CellsBasis
18
19 open Ast Comp.Util Comp.Error
20
21 val t2s = PP.text o AstPP.ty
22 val e2s = PP.text o AstPP.exp
23 val SKIP = RW.noRewrite
24
25 (* does this type has special meaning in an instruction representation? *)
26 fun isSpecialRepType t =
27 let fun isSpecial t =
28 case TU.deref t of
29 CELLty _ => true (* cell types are special *)
30 | IDty(IDENT(_,"int")) => true
31 | IDty(IDENT([],"operand")) => true
32 | IDty(IDENT(_,"cellset")) => true
33 | _ => false
34
35 val found = ref false
36
37 fun ty _ t = (if isSpecial t then found := true else (); t)
38 val _ = #ty(RW.rewrite{ty=ty,exp=SKIP,decl=SKIP,sexp=SKIP,pat=SKIP}) t
39 in !found
40 end
41
42 (* return the real representation type of an rtl argument *)
43 fun representationOf(rtlName, arg, loc, ty) =
44 let fun err() =
45 (errorPos(loc,
46 "'"^arg^"' in rtl "^rtlName^" has an illegal type "^t2s ty);
47 (0, "bits")
48 )
49 in case TU.deref ty of
50 IDty(IDENT([],"operand")) => (0, "operand")
51 | IDty(IDENT([],"label")) => (0, "label")
52 | IDty(IDENT([],"region")) => (0, "region")
53 | APPty(IDENT([],"operand"), [INTVARty n]) => (n, "operand")
54 | APPty(IDENT([],"bits"), [INTVARty n]) => (n, "bits")
55 | ty => err()
56 end
57
58 fun representationOf(rtlName, arg, loc, ty) =
59 let fun err() =
60 (errorPos(loc,
61 "'"^arg^"' in rtl "^rtlName^" has an illegal type "^t2s ty);
62 (0, "bits")
63 )
64 in case TU.deref ty of
65 IDty(IDENT([],"operand")) => (0, "operand")
66 | IDty(IDENT(_,"label")) => (0, "label")
67 | IDty(IDENT([],"region")) => (0, "region")
68 | APPty(IDENT([],"operand"), [INTVARty n]) => (n, "operand")
69 | APPty(IDENT([],"bits"), [INTVARty n]) => (n, "cell")
70 | ty => err()
71 end
72
73 (* Given the actual represention of an rtl argument,
74 * insert coercion if possible.
75 *)
76 fun insertRepCoercion(exp, ty) =
77 (case (exp, TU.deref ty) of
78 (T.ARG(_,k,_), IDty(IDENT([],"int"))) => k := T.REP "int"
79 | (T.ARG(_,k,_), IDty(IDENT(_,"label"))) => k := T.REP "label"
80 | (T.ARG(_,k,_), IDty(IDENT([],"operand"))) => k := T.REP "operand"
81 | (T.$(_,_,T.ARG(_,k,_)), CELLty _) => k := T.REP "cell"
82 | _ => ()
83 )
84
85 fun ofCellKind(T.$(_,k,_),CELLdecl{id, ...}) =
86 (case C.cellkindToString k of
87 "CELLSET" => true
88 | k => k = id
89 )
90 | ofCellKind(T.ARG _,CELLdecl{id, ...}) = false
91 | ofCellKind(_, _) = false
92
93
94 (*
95 * A database of all special types
96 *)
97 datatype howto =
98 HOWTO of
99 { rep : string, (* name of representation *)
100 isSSAValue : bool, (* is it a value in SSA form? *)
101 mlType : Ast.ty, (* type in ML *)
102 isConst : bool, (* if so, is it always a constant? *)
103 isMultiValued : (* if a value can it take more than one *)
104 Comp.md -> bool
105 }
106
107 val howtos = ref [] : howto list ref
108
109 fun findRep r =
110 case List.find (fn HOWTO{rep, ...} => rep = r) (!howtos) of
111 SOME(HOWTO howto) => howto
112 | NONE => fail("bug: representation "^r^" not known")
113
114 (*---------------------------------------------------------------------
115 *
116 * Code generation magic
117 *
118 *---------------------------------------------------------------------*)
119 fun isConst(T.REP rep) = #isConst(findRep rep)
120
121 (*---------------------------------------------------------------------
122 *
123 * Okay, now specify all the types that we have to handle.
124 *
125 *---------------------------------------------------------------------*)
126 fun no _ = false
127 fun yes _ = true
128 fun bug _ = fail("unimplemented")
129
130 val _ = howtos :=
131 [HOWTO{rep = "label",
132 isSSAValue = false,
133 mlType = IDty(IDENT(["Label"],"label")),
134 isConst = true,
135 isMultiValued = no
136 },
137
138 HOWTO{rep = "int",
139 isSSAValue = true,
140 mlType = IDty(IDENT([],"int")),
141 isConst = true,
142 isMultiValued = no
143 },
144
145 HOWTO{rep = "operand",
146 isSSAValue = true,
147 mlType = IDty(IDENT(["I"],"operand")),
148 isConst = false,
149 isMultiValued = yes
150 },
151
152 HOWTO{rep = "cellset",
153 isSSAValue = true,
154 mlType = IDty(IDENT(["C"],"cellset")),
155 isConst = false,
156 isMultiValued = yes
157 }
158 ]
159
160 (*---------------------------------------------------------------------
161 *
162 * Generate an expression for performing the appropriate conversion
163 *
164 *---------------------------------------------------------------------*)
165 datatype conv = IGNORE | CONV of string | MULTI of string
166
167 structure DescMap = RedBlackMapFn(type ord_key = string
168 val compare = String.compare)
169 fun getOpnd desc =
170 let val tbl =
171 foldr (fn ((rep,conv), tbl) =>
172 DescMap.insert(tbl, rep, conv)) DescMap.empty desc
173
174 fun mkConvFun(rep,conv) =
175 "fun get_"^rep^"(x,L) = "^
176 (case conv of
177 IGNORE => "L"
178 | CONV f => f^"::L"
179 | MULTI f => f^"@L"
180 )
181 fun mkConvFun0(rep,conv) =
182 "fun get_"^rep^"'(x) = "^
183 (case conv of
184 IGNORE => "[]"
185 | CONV f => "["^f^"]"
186 | MULTI f => f
187 )
188 val decl = $(map mkConvFun desc @ map mkConvFun0 desc)
189
190 fun apply(rep, this, rest) = APP("get_"^rep,TUPLEexp[this,rest])
191
192 fun getIt(rep, this, rest) =
193 case (DescMap.find(tbl, rep), rest) of
194 (NONE, _) => fail("getOpnd: "^rep^" is not defined")
195 | (SOME IGNORE, _) => rest
196 | (SOME(CONV _), _) => apply(rep, this, rest)
197 | (SOME(MULTI conv), LISTexp([],NONE)) => APP("get_"^rep^"'", this)
198 | (SOME(MULTI _), rest) => apply(rep, this, rest)
199
200 fun get(this,T.$(_,k,_),rest) =
201 if C.cellkindToString k = "CELLSET" then
202 getIt("cellset",this,rest)
203 else
204 getIt("cell",this,rest)
205 | get(this,T.ARG(_,ref(T.REP rep),_),rest) = getIt(rep,this,rest)
206 | get(_, e, _) = fail("MLRiscTypes.get: "^RTL.Util.rexpToString e)
207
208 in { decl= decl,
209 get = get
210 }
211 end
212
213 end