Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/FakeSMLAst/ast-util.sml

raw
1 functor MDLAstUtil(Ast : MDL_AST) : MDL_AST_UTIL =
2 struct
3
4 structure Ast = Ast
5 open Ast
6
7 fun ID id = IDexp(IDENT([],id))
8 fun APP(f,e) = APPexp(ID f,e)
9 fun BINOPexp(f,x,y) = APP(f,TUPLEexp[x,y])
10 fun PLUS(a,LITexp(INTlit 0)) = a
11 | PLUS(a,LITexp(WORDlit 0w0)) = a
12 | PLUS(a,LITexp(WORD32lit 0w0)) = a
13 | PLUS(LITexp(INTlit 0),a) = a
14 | PLUS(LITexp(WORDlit 0w0),a) = a
15 | PLUS(LITexp(WORD32lit 0w0),a) = a
16 | PLUS(a,b) = BINOPexp("+",a,b)
17 fun MINUS(a,LITexp(INTlit 0)) = a
18 | MINUS(a,LITexp(WORDlit 0w0)) = a
19 | MINUS(a,LITexp(WORD32lit 0w0)) = a
20 | MINUS(a,b) = BINOPexp("-",a,b)
21 fun ANDB(a,b) = BINOPexp("&&",a,b)
22 fun ORB(a,b) = BINOPexp("||",a,b)
23 fun SLL(a,LITexp(WORDlit 0w0)) = a
24 | SLL(a,LITexp(WORD32lit 0w0)) = a
25 | SLL(a,LITexp(INTlit 0)) = a
26 | SLL(a,b) = BINOPexp("<<",a,b)
27 fun SLR(a,LITexp(WORDlit 0w0)) = a
28 | SLR(a,LITexp(WORD32lit 0w0)) = a
29 | SLR(a,LITexp(INTlit 0)) = a
30 | SLR(a,b) = BINOPexp(">>",a,b)
31 fun SAR(a,LITexp(WORDlit 0w0)) = a
32 | SAR(a,LITexp(WORD32lit 0w0)) = a
33 | SAR(a,LITexp(INTlit 0)) = a
34 | SAR(a,b) = BINOPexp("~>>",a,b)
35
36 fun BOOLexp x = LITexp(BOOLlit x)
37 fun STRINGexp s = LITexp(STRINGlit s)
38 fun INTexp x = LITexp(INTlit x)
39 fun INT32exp x = LITexp(INT32lit x)
40 fun INTINFexp x = LITexp(INTINFlit x)
41 fun CHARexp x = LITexp(CHARlit x)
42 fun WORDexp x = LITexp(WORDlit x)
43 fun WORD32exp x = LITexp(WORD32lit x)
44
45 fun BOOLpat x = LITpat(BOOLlit x)
46 fun STRINGpat s = LITpat(STRINGlit s)
47 fun INTpat x = LITpat(INTlit x)
48 fun INT32pat x = LITpat(INT32lit x)
49 fun INTINFpat x = LITpat(INTINFlit x)
50 fun CHARpat x = LITpat(CHARlit x)
51 fun WORDpat x = LITpat(WORDlit x)
52 fun WORD32pat x = LITpat(WORD32lit x)
53
54 val UNIT = TUPLEexp []
55 val TRUE = BOOLexp true
56 val FALSE = BOOLexp false
57 fun ANDALSO(LITexp(BOOLlit true),x) = x
58 | ANDALSO(LITexp(BOOLlit false),x) = FALSE
59 | ANDALSO(x,LITexp(BOOLlit true)) = x
60 | ANDALSO(x,LITexp(BOOLlit false)) = FALSE
61 | ANDALSO(x,y) = BINOPexp("andalso",x,y)
62 fun ORELSE(LITexp(BOOLlit true),x) = TRUE
63 | ORELSE(LITexp(BOOLlit false),x) = x
64 | ORELSE(x,LITexp(BOOLlit true)) = TRUE
65 | ORELSE(x,LITexp(BOOLlit false)) = x
66 | ORELSE(x,y) = BINOPexp("orelse",x,y)
67 val NILexp = LISTexp([],NONE)
68
69
70 val UNITty = IDty(IDENT([],"unit"))
71 val BOOLty = IDty(IDENT([],"bool"))
72 val INTty = IDty(IDENT([],"int"))
73 val REGISTERty = IDty(IDENT([],"CellsBasis.cell"))
74 val REGISTERLISTty = APPty(IDENT([],"list"),[REGISTERty])
75 val INTLISTty = APPty(IDENT([],"list"),[INTty])
76 val STRINGty = IDty(IDENT([],"string"))
77 val WORD32ty = IDty(IDENT(["Word32"],"word"))
78 val WORDty = IDty(IDENT(["Word"],"word"))
79 val LABELty = IDty(IDENT(["Label"],"label"))
80 val LABEXPty = IDty(IDENT(["LabelExp"],"labexp"))
81 val CONSTty = IDty(IDENT(["Constant"],"const"))
82 val CELLKINDty = IDty(IDENT([],"CellsBasis.cellkind"))
83 val CELLSETty = IDty(IDENT([],"cellset"))
84
85 fun DATATYPE(id,args,cbs) =
86 DATATYPEbind{id=id,tyvars=args,mc=NONE,asm=false,field=NONE,cbs=cbs}
87 fun CONS(id,arg) = CONSbind{id=id,ty=arg,mc=NONE,asm=NONE,rtl=NONE,
88 nop=FLAGoff,nullified=FLAGoff,
89 delayslot=NONE,
90 delaycand=NONE,sdi=NONE,latency=NONE,
91 pipeline=NONE, loc=SourceMapping.dummyLoc}
92 fun VAL(id,e) = VALdecl[VALbind(case id of "_" => WILDpat | _ => IDpat id,e)]
93 fun FUN'(id,p,e) = FUNbind(id,[CLAUSE([p],NONE,e)])
94 fun FUN(id,p,e) = FUNdecl [FUN'(id,p,e)]
95 fun LET([],e) = e
96 | LET(d,e) = LETexp(d,[e])
97
98
99 fun ERROR text = CLAUSE([WILDpat],NONE,APP("error",STRINGexp text))
100 fun ERRORfun name =
101 $["fun error msg = MLRiscErrorMsg.error(\""^name^"\",msg)"]
102 fun DUMMYfun name =
103 $["fun "^name^" _ = error \""^name^"\""]
104
105
106 fun BITSLICE(e,ranges) =
107 let val temp = ID "temp"
108 fun gen(tmp, [], pos, e) = e
109 | gen(tmp, (a,b)::slices, pos, e) =
110 let val width = b - a + 1
111 val mask = Word32.<<(0w1, Word.fromInt width) - 0w1
112 val field = SLL(tmp, WORD32exp(Word32.fromInt a))
113 val field = ANDB(field, WORD32exp mask)
114 in gen(tmp, slices, pos+width,
115 PLUS(SLL(field, WORD32exp(Word32.fromInt pos)),e))
116 end
117 fun emit(tmp) = gen(tmp, rev ranges, 0, WORD32exp 0w0)
118 in case ranges of
119 [_] => emit e
120 | _ => LETexp([VALdecl[VALbind(IDpat "temp",e)]], [emit(ID "temp")])
121 end
122
123 (* Add an entry *)
124 fun cons(x,LISTexp(a,b)) = LISTexp(x::a,b)
125 | cons(x,y) = LISTexp([x],SOME y)
126
127 (* Append an entry *)
128 fun append(x,LISTexp([],NONE)) = x
129 | append(x,y) = APP("@",TUPLEexp[x,y])
130
131 fun compareLiteral(x,y) =
132 let fun kind(INTlit _) = 0
133 | kind(BOOLlit _) = 1
134 | kind(STRINGlit _) = 2
135 | kind(CHARlit _) = 3
136 | kind(WORDlit _) = 4
137 | kind(WORD32lit _) = 5
138 | kind(INTINFlit _) = 6
139 | kind(REALlit _) = 7
140 | kind(INT32lit _) = 8
141 in case (x, y) of
142 (INTlit x,INTlit y) => Int.compare(x,y)
143 | (INT32lit x,INT32lit y) => Int32.compare(x,y)
144 | (BOOLlit x,BOOLlit y) => if x = y then EQUAL
145 else if x = false then LESS else GREATER
146 | (STRINGlit x,STRINGlit y) => String.compare(x,y)
147 | (CHARlit x,CHARlit y) => Char.compare(x,y)
148 | (WORDlit x,WORDlit y) => Word.compare(x,y)
149 | (WORD32lit x,WORD32lit y) => Word32.compare(x,y)
150 | (INTINFlit x,INTINFlit y) => IntInf.compare(x,y)
151 | (REALlit x,REALlit y) => String.compare(x,y)
152 | (x, y) => Int.compare(kind x,kind y)
153 end
154 end