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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2116 - (view) (download)

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

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