Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/codegen/clang.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/codegen/clang.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3810 - (view) (download)
Original Path: branches/vis15/src/compiler/clang/clang.sml

1 : jhr 3768 (* clang.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3769 * An tree representation of programs in a C-like language (e.g., C, C++, CUDA,
9 :     * or OpenCL).
10 : jhr 3768 *)
11 :    
12 :     structure CLang =
13 :     struct
14 :    
15 :     type var = string
16 :     type attr = string (* e.g., "static", "kernel", etc ... *)
17 :    
18 :     datatype ty
19 :     = T_Num of RawTypes.ty
20 :     | T_Ptr of ty
21 : jhr 3769 | T_RestrictPtr of ty (* pointer type with "restrict" annotation *)
22 : jhr 3768 | T_Array of ty * int option
23 :     | T_Named of string
24 : jhr 3769 | T_Template of string * ty list
25 : jhr 3768 | T_Qual of attr * ty (* qualified type *)
26 :    
27 :     datatype typed_var = V of ty * var
28 :    
29 :     val voidTy = T_Named "void"
30 :     val voidPtr = T_Ptr voidTy
31 :     val charTy = T_Named "char"
32 :     val boolTy = T_Named "bool"
33 :     val charPtr = T_Ptr charTy
34 :     val charArrayPtr = T_Ptr charPtr
35 :     val intTy = T_Named "int"
36 :     val int8 = T_Num(RawTypes.RT_Int8)
37 :     val uint8 = T_Num(RawTypes.RT_UInt8)
38 :     val int32 = T_Num(RawTypes.RT_Int32)
39 :     val uint32 = T_Num(RawTypes.RT_UInt32)
40 :     val int64 = T_Num(RawTypes.RT_Int64)
41 :     val float = T_Num(RawTypes.RT_Float)
42 :     val double = T_Num(RawTypes.RT_Double)
43 :    
44 :     datatype decl
45 :     = D_Pragma of string list
46 :     | D_Comment of string list
47 :     (* verbatim text (e.g., preprocessor directives) *)
48 :     | D_Verbatim of string list
49 :     (* global variable declaration *)
50 :     | D_Var of attr list * ty * var * initializer option
51 :     (* function prototype *)
52 :     | D_Proto of attr list * ty * string * param list
53 :     (* function definition *)
54 :     | D_Func of attr list * ty * string * param list * stm
55 :     (* struct type declaration; if the third argument is SOME name, then a
56 :     * typedef is generated.
57 :     *)
58 :     | D_StructDef of string option * (ty * string) list * string option
59 : jhr 3810 (* derived struct declaration (C++) *)
60 :     | D_DerivedStructDef of string * (ty * string) list
61 : jhr 3768
62 :     and initializer
63 :     = I_Exp of exp
64 :     | I_Exps of initializer list
65 : jhr 3769 | I_Struct of (string * initializer) list (* C99 labeled struct initializer *)
66 :     | I_Array of (int * initializer) list (* C99 labeled array initializer *)
67 :     | I_Cons of ty * exp list
68 : jhr 3768
69 :     and param = PARAM of attr list * ty * var
70 :    
71 :     and stm
72 :     = S_Block of stm list (* "{" stms "}" *)
73 :     | S_Comment of string list
74 :     | S_Verbatim of string list
75 :     | S_Decl of attr list * ty * var * initializer option
76 :     (* ty var [ '=' exp ]';' *)
77 :     | S_Exp of exp (* exp ';' *)
78 :     | S_If of exp * stm * stm (* 'if' exp stm 'else' stm *)
79 :     | S_While of exp * stm (* 'while' exp stm *)
80 :     | S_DoWhile of stm * exp (* 'do' stm 'while' exp *)
81 :     | S_For of (ty * var * exp) list * exp * exp list * stm
82 :     (* 'for' '(' inits ';' exp ';' incrs ')' stm *)
83 : jhr 3769 | S_KernCall of string * exp list * exp list
84 :     (* f "<<<" ... ">>>" "(" ... ")" [CUDA] *)
85 : jhr 3768 | S_Return of exp option (* 'return' [ exp ] ';' *)
86 :     | S_Break (* 'break' ';' *)
87 :     | S_Continue (* 'continue' ';' *)
88 :    
89 :     and exp
90 :     = E_Grp of exp (* "(" e ")" *)
91 :     | E_AssignOp of exp * assignop * exp (* lvalue op= e *)
92 :     | E_Cond of exp * exp * exp (* e "?" e ":" e *)
93 :     | E_BinOp of exp * binop * exp (* e op e *)
94 :     | E_UnOp of unop * exp (* op e *)
95 :     | E_PostOp of exp * postfix (* e op *)
96 : jhr 3769 | E_Apply of exp * exp list (* f "(" ... ")" *)
97 :     | E_Cons of ty * exp list (* ty "(" ... ")" [C++,CUDA]*)
98 :     | E_New of ty * exp list (* "new" ty "(" ... ")" [C++,CUDA]*)
99 : jhr 3768 | E_Subscript of exp * exp (* e "[" e "]" *)
100 :     | E_Select of exp * string (* e "." f *)
101 :     | E_Indirect of exp * string (* e "->" f *)
102 :     | E_Cast of ty * exp (* "(" ty ")" e *)
103 : jhr 3769 | E_Vec of ty * exp list (* vector-expression; syntax depends on target [C,OpenCL] *)
104 : jhr 3768 | E_Var of var
105 : jhr 3769 | E_Int of IntLit.t * ty
106 :     | E_Flt of RealLit.t * ty
107 : jhr 3768 | E_Bool of bool
108 :     | E_Str of string
109 :     | E_Char of char
110 :     | E_Sizeof of ty (* "sizeof(" ty ")" *)
111 :    
112 :     (* assignment operators *)
113 :     and assignop
114 :     = $= | += | *= | /= | %= | <<= | >>= | &= | ^= | |=
115 :    
116 :     (* binary operators in increasing order of precedence *)
117 :     and binop
118 :     = #||
119 :     | #&&
120 :     | #|
121 :     | #^
122 :     | #&
123 :     | #== | #!=
124 :     | #< | #<= | #>= | #>
125 :     | #<< | #>>
126 :     | #+ | #-
127 :     | #* | #/ | #%
128 :    
129 :     and unop = %- | %! | %& | %* | %~ | %++ | %--
130 :    
131 :     and postfix = ^++ | ^--
132 :    
133 :     (* smart constructors that add E_Grp wrappers based on operator precedence *)
134 :     local
135 :     val commaP = 0
136 :     val assignP = 1
137 :     val condP = 2
138 :     val lorP = 3
139 :     val landP = 4
140 :     val borP = 5
141 :     val bxorP = 6
142 :     val bandP = 7
143 :     val eqP = 8
144 :     val relP = 9
145 :     val shiftP = 10
146 :     val addP = 11
147 :     val mulP = 12
148 :     val castP = 13
149 :     val unaryP = 14
150 :     val preP = 15
151 :     val compundP = 16 (* compound literal *)
152 :     val postP = 17
153 :     val callP = 18
154 :     val subP = 19
155 :     val atomP = 20
156 :     fun precOfBinop rator = (case rator
157 :     of #|| => lorP
158 :     | #&& => landP
159 :     | #| => borP
160 :     | #^ => bxorP
161 :     | #& => bandP
162 :     | #== => eqP | #!= => eqP
163 :     | #< => relP | #<= => relP | #>= => relP | #> => relP
164 :     | #<< => shiftP | #>> => shiftP
165 :     | #+ => addP | #- => addP
166 :     | #* => mulP | #/ => mulP | #% => mulP
167 :     (* end case *))
168 :     fun prec (E_Grp _) = atomP
169 :     | prec (E_AssignOp _) = assignP
170 :     | prec (E_Cond _) = condP
171 :     | prec (E_BinOp(_, rator, _)) = precOfBinop rator
172 :     | prec (E_UnOp _) = preP
173 :     | prec (E_PostOp _) = postP
174 :     | prec (E_Apply _) = callP
175 : jhr 3769 | prec (E_Cons _) = callP (* check this *)
176 :     | prec (E_New _) = callP (* check this *)
177 : jhr 3768 | prec (E_Subscript _) = postP
178 :     | prec (E_Select _) = postP
179 :     | prec (E_Indirect _) = postP
180 :     | prec (E_Cast _) = castP
181 :     | prec (E_Vec _) = castP
182 :     | prec (E_Var _) = atomP
183 :     | prec (E_Int _) = atomP
184 :     | prec (E_Flt _) = atomP
185 :     | prec (E_Bool _) = atomP
186 :     | prec (E_Str _) = atomP
187 :     | prec (E_Char _) = atomP
188 :     | prec (E_Sizeof _) = callP
189 :     in
190 :     fun mkGrp e = if (prec e < atomP) then E_Grp e else e
191 :     fun mkAssignOp (e1, rator, e2) = let
192 :     val e1' = if prec e1 < unaryP then E_Grp e1 else e1
193 :     val e2' = if prec e2 < assignP then E_Grp e2 else e2
194 :     in
195 :     E_AssignOp(e1', rator, e2')
196 :     end
197 :     (* note that we over-parenthesize here, but it makes nested conditionals easeier to read *)
198 :     fun mkCond (e1, e2, e3) = E_Cond(
199 :     if prec e1 <= condP then E_Grp e1 else e1,
200 :     if prec e2 <= condP then E_Grp e2 else e2,
201 :     if prec e3 < condP then E_Grp e3 else e3)
202 :     (* Note that all C binary operators are left associative. *)
203 :     fun mkBinOp (e1, #-, e2 as E_UnOp(%-, _)) = let
204 :     val e1' = if prec e1 < addP then E_Grp e1 else e1
205 :     val e2' = E_Grp e2
206 :     in
207 :     E_BinOp(e1', #-, e2')
208 :     end
209 :     | mkBinOp (e1, rator, e2) = let
210 :     val p = precOfBinop rator
211 :     val e1' = if prec e1 < p then E_Grp e1 else e1
212 :     val e2' = if prec e2 <= p then E_Grp e2 else e2
213 :     in
214 :     E_BinOp(e1', rator, e2')
215 :     end
216 :     fun mkUnOp (%-, e as E_UnOp(%-, _)) = E_UnOp(%-, E_Grp e)
217 :     | mkUnOp (%-, e as E_UnOp(%--, _)) = E_UnOp(%-, E_Grp e)
218 :     | mkUnOp (%--, e as E_UnOp(%-, _)) = E_UnOp(%--, E_Grp e)
219 :     | mkUnOp (%--, e as E_UnOp(%--, _)) = E_UnOp(%--, E_Grp e)
220 :     | mkUnOp (%&, E_UnOp(%*, e)) = e
221 :     | mkUnOp (%*, E_UnOp(%&, e)) = e
222 :     | mkUnOp (rator, e) = if prec e < unaryP
223 :     then E_UnOp(rator, E_Grp e)
224 :     else E_UnOp(rator, e)
225 :     fun mkPostOp (e, rator) = if prec e < postP
226 :     then E_PostOp(E_Grp e, rator)
227 :     else E_PostOp(e, rator)
228 :     fun mkApply (f, args) = E_Apply(E_Var f, args)
229 : jhr 3769 (* FIXME: check precedence *)
230 : jhr 3768 fun mkApplyExp (e, args) = E_Apply(e, args)
231 : jhr 3769 val mkCons = E_Cons
232 :     val mkNew = E_New
233 : jhr 3768 fun mkSubscript(e1, e2) = if prec e1 < postP
234 :     then E_Subscript(E_Grp e1, e2)
235 :     else E_Subscript(e1, e2)
236 :     fun mkSelect (e, f) = if prec e < postP
237 :     then E_Select(E_Grp e, f)
238 :     else E_Select(e, f)
239 :     fun mkIndirect (e, f) = if prec e < postP
240 :     then E_Indirect(E_Grp e, f)
241 :     else E_Indirect(e, f)
242 :     fun mkCast (ty, e) = if prec e < castP
243 :     then E_Cast(ty, E_Grp e)
244 :     else E_Cast(ty, e)
245 :     val mkVec = E_Vec
246 :     val mkVar = E_Var
247 :     fun mkIntTy (n, ty) = if n < 0 then E_UnOp(%-, E_Int(~n, ty)) else E_Int(n, ty)
248 :     fun mkInt n = mkIntTy(n, intTy)
249 : jhr 3769 fun mkFlt (f, ty) = if RealLit.isNeg f
250 :     then E_UnOp(%-, E_Flt(RealLit.negate f, ty))
251 : jhr 3768 else E_Flt(f, ty)
252 :     val mkBool = E_Bool
253 :     val mkStr = E_Str
254 :     val mkChar = E_Char
255 :     val mkSizeof = E_Sizeof
256 :     fun mkAddrOf x = mkUnOp(%&, x)
257 :     end (* local *)
258 :    
259 :     val skip = S_Block[]
260 :    
261 :     local
262 :     fun paren (e as E_Grp _) = e
263 :     | paren e = E_Grp e
264 :     in
265 :     val mkComment = S_Comment
266 :     fun mkBlock [stm] = stm
267 :     | mkBlock stms = S_Block stms
268 :     fun unBlock (S_Block stms) = stms
269 :     | unBlock stm = [stm]
270 :     fun prependStm (stm, blk) = mkBlock(stm :: unBlock blk)
271 :     fun appendStm (blk, stm) = mkBlock(unBlock blk @ [stm])
272 :     fun concatBlocks blocks = mkBlock(List.concat(List.map unBlock blocks))
273 :     fun mkDecl (ty, x, init) = S_Decl([], ty, x, init)
274 :     fun mkDeclInit (ty, x, init) = S_Decl([], ty, x, SOME(I_Exp init))
275 :     val mkAttrDecl = S_Decl
276 :     val mkExpStm = S_Exp
277 :     fun mkAssign (e1, e2) = S_Exp(mkAssignOp(e1, $=, e2))
278 :     fun mkAssign' (e1, rator, e2) = S_Exp(mkAssignOp(e1, rator, e2))
279 :     fun mkIfThenElse (e, b1, b2) = S_If(paren e, b1, b2)
280 :     fun mkIfThen (e, b) = mkIfThenElse (e, b, skip)
281 :     val mkFor = S_For
282 :     fun mkWhile (e, b) = S_While(paren e, b)
283 :     fun mkDoWhile (b, e) = S_DoWhile(b, paren e)
284 :     fun mkCall (f, args) = S_Exp(mkApply(f, args))
285 :     fun mkCallExp (f, args) = S_Exp(mkApplyExp(f, args))
286 : jhr 3769 val mkKernCall = S_KernCall
287 : jhr 3768 val mkReturn = S_Return
288 :     val mkBreak = S_Break
289 :     val mkContinue = S_Continue
290 :     end (* local *)
291 :    
292 :     (* utility functions *)
293 :    
294 :     fun varToString x = x
295 :    
296 :     fun assignopToString rator = (case rator
297 :     of $= => "="
298 :     | += => "+="
299 :     | *= => "*="
300 :     | /= => "/="
301 :     | %= => "%="
302 :     | <<= => "<<="
303 :     | >>= => ">>="
304 :     | &= => "&="
305 :     | ^= => "^="
306 :     | |= => "|="
307 :     (* end case *))
308 :    
309 :     fun binopToString rator = (case rator
310 :     of #|| => "||"
311 :     | #&& => "&&"
312 :     | #== => "=="
313 :     | #| => "|"
314 :     | #^ => "^"
315 :     | #& => "&"
316 :     | #!= => "!="
317 :     | #< => "<"
318 :     | #<= => "<="
319 :     | #>= => ">="
320 :     | #> => ">"
321 :     | #<< => "<<"
322 :     | #>> => ">>"
323 :     | #+ => "+"
324 :     | #- => "-"
325 :     | #* => "*"
326 :     | #/ => "/"
327 :     | #% => "%"
328 :     (* end case *))
329 :    
330 :     fun unopToString rator = (case rator
331 :     of %- => "-"
332 :     | %! => "!"
333 :     | %& => "&"
334 :     | %* => "*"
335 :     | %~ => "~"
336 :     | %++ => "++"
337 :     | %-- => "--"
338 :     (* end case *))
339 :    
340 :     fun postopToString rator = (case rator
341 :     of ^++ => "++"
342 :     | ^-- => "--"
343 :     (* end case *))
344 :    
345 :     (* generate verbatim text from a template string by substituting for placeholders
346 :     * Placeholders have the syntax @<id>@ and are replaced with the string associated
347 :     * with <id> in the list of substitutions. If <id> is empty, then no substitution
348 :     * is applied, instead the "@@" is replaced by "@".
349 :     *)
350 :     local
351 : jhr 3770 fun verbatim sl subs = List.map (StringSubst.expand subs) sl
352 : jhr 3768 in
353 :     fun verbatimDcl sl subs = D_Verbatim(verbatim sl subs)
354 :     fun verbatimStm sl subs = S_Verbatim(verbatim sl subs)
355 :     end (* local *)
356 :    
357 :     (* for debugging (not syntactically correct!) *)
358 :     fun expToString e = let
359 :     fun e2s (e, l) = (case e
360 :     of E_Grp e => "(" :: e2s(e, ")"::l)
361 :     | E_AssignOp(e1, rator, e2) => e2s(e1, assignopToString rator :: e2s(e2, l))
362 :     | E_Cond(e1, e2, e3) => "(" :: e2s(e1, "?" :: e2s(e2, ":" :: e2s (e3, ")" :: l)))
363 :     | E_BinOp(e1, rator, e2) => e2s(e1, binopToString rator :: e2s(e2, l))
364 :     | E_UnOp(rator, e) => unopToString rator :: e2s(e, l)
365 :     | E_PostOp(e, rator) => e2s(e, postopToString rator :: l)
366 :     | E_Apply(e, es) => e2s(e, "(" :: args2s(es, ")"::l))
367 : jhr 3769 | E_Cons(ty, es) => e2s(e, "ty(" :: args2s(es, ")"::l))
368 :     | E_New(ty, es) => e2s(e, "new ty (" :: args2s(es, ")"::l))
369 : jhr 3768 | E_Subscript(e1, e2) => e2s(e1, "[" :: e2s(e2, "]"::l))
370 :     | E_Select(e, f) => e2s(e, "." :: f :: l)
371 :     | E_Indirect(e, f) => e2s(e, "->" :: f :: l)
372 :     | E_Cast(ty, e) => "(ty)" :: e2s(e, l) (* FIXME: need tyToString *)
373 :     | E_Vec(ty, args) => "(vec)(" :: args2s(args, ")"::l) (* FIXME: need tyToString *)
374 :     | E_Var x => x::l
375 : jhr 3769 | E_Int(n, _) => IntLit.toString n :: l
376 :     | E_Flt(f, _) => RealLit.toString f :: l
377 : jhr 3768 | E_Bool b => Bool.toString b :: l
378 :     | E_Str s => "\"" :: String.toCString s :: "\"" :: l
379 :     | E_Char c => "'" :: Char.toCString c :: "'" :: l
380 :     | E_Sizeof ty => "sizeof(ty)" :: l
381 :     (* end case *))
382 :     and args2s ([], l) = l
383 :     | args2s ([e], l) = e2s(e, l)
384 :     | args2s (e::es, l) = e2s(e, ","::args2s(es, l))
385 :     in
386 :     String.concat(e2s(e, []))
387 :     end
388 :    
389 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0