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 3812 - (view) (download)

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

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