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

SCM Repository

[diderot] Diff of /branches/charisee/src/compiler/codegen/clang.sml
ViewVC logotype

Diff of /branches/charisee/src/compiler/codegen/clang.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1368, Wed Jun 22 20:58:28 2011 UTC revision 1766, Sat Mar 24 10:09:29 2012 UTC
# Line 19  Line 19 
19        | T_Ptr of ty        | T_Ptr of ty
20        | T_Array of ty * int option        | T_Array of ty * int option
21        | T_Named of string        | T_Named of string
22          | T_Qual of attr * ty     (* qualified type *)
23    
24      datatype typed_var = V of ty * var      datatype typed_var = V of ty * var
25    
# Line 28  Line 29 
29      val charPtr = T_Ptr(charTy)      val charPtr = T_Ptr(charTy)
30      val charArrayPtr = T_Ptr(charPtr)      val charArrayPtr = T_Ptr(charPtr)
31      val intTy = T_Named "int"      val intTy = T_Named "int"
32        val int8 = T_Num(RawTypes.RT_Int8)
33        val uint8 = T_Num(RawTypes.RT_UInt8)
34      val int32 = T_Num(RawTypes.RT_Int32)      val int32 = T_Num(RawTypes.RT_Int32)
35      val uint32 = T_Num(RawTypes.RT_UInt32)      val uint32 = T_Num(RawTypes.RT_UInt32)
36      val int64 = T_Num(RawTypes.RT_Int64)      val int64 = T_Num(RawTypes.RT_Int64)
# Line 40  Line 43 
43        | D_Verbatim of string list        | D_Verbatim of string list
44      (* global variable declaration *)      (* global variable declaration *)
45        | D_Var of attr list * ty * var * initializer option        | D_Var of attr list * ty * var * initializer option
46        (* function prototype *)
47          | D_Proto of attr list * ty * string * param list
48      (* function definition *)      (* function definition *)
49        | D_Func of attr list * ty * string * param list * stm        | D_Func of attr list * ty * string * param list * stm
50      (* typedef of struct type *)      (* struct type declaration; if the second argument is SOME name, then a
51        | D_StructDef of (ty * string) list * string       * typedef is generated.
52         *)
53          | D_StructDef of string option * (ty * string) list * string option
54    
55      and initializer      and initializer
56        = I_Exp of exp        = I_Exp of exp
# Line 60  Line 67 
67        | S_Exp of exp                    (* exp ';' *)        | S_Exp of exp                    (* exp ';' *)
68        | S_If of exp * stm * stm         (* 'if' exp stm 'else' stm *)        | S_If of exp * stm * stm         (* 'if' exp stm 'else' stm *)
69        | S_While of exp * stm            (* 'while' exp stm *)        | S_While of exp * stm            (* 'while' exp stm *)
70          | S_DoWhile of stm * exp          (* 'do' stm 'while' exp *)
71        | S_For of (ty * var * exp) list * exp * exp list * stm        | S_For of (ty * var * exp) list * exp * exp list * stm
72                                          (* 'for' '(' inits ';' exp ';' incrs ')' stm *)                                          (* 'for' '(' inits ';' exp ';' incrs ')' stm *)
73        | S_Call of string * exp list     (* func '(' args ')' *)        | S_Call of string * exp list     (* func '(' args ')' *)
# Line 70  Line 78 
78      and exp      and exp
79        = E_Grp of exp                    (* "(" e ")" *)        = E_Grp of exp                    (* "(" e ")" *)
80        | E_AssignOp of exp * assignop * exp (* lvalue op= e *)        | E_AssignOp of exp * assignop * exp (* lvalue op= e *)
81          | E_Cond of exp * exp * exp       (* e "?" e ":" e *)
82        | E_BinOp of exp * binop * exp    (* e op e *)        | E_BinOp of exp * binop * exp    (* e op e *)
83        | E_UnOp of unop * exp            (* op e *)        | E_UnOp of unop * exp            (* op e *)
84        | E_PostOp of exp * postfix       (* e op *)        | E_PostOp of exp * postfix       (* e op *)
# Line 93  Line 102 
102      and binop      and binop
103        = #||        = #||
104        | #&&        | #&&
105          | #|
106          | #^
107          | #&
108        | #== | #!=        | #== | #!=
109        | #< | #<= | #>= | #>        | #< | #<= | #>= | #>
110        | #<< | #>>        | #<< | #>>
# Line 111  Line 123 
123        val lorP          = 3        val lorP          = 3
124        val landP         = 4        val landP         = 4
125        val borP          = 5        val borP          = 5
126        val bandP         = 6        val bxorP         = 6
127        val eqP           = 7        val bandP         = 7
128        val relP          = 8        val eqP           = 8
129        val shiftP        = 9        val relP          = 9
130        val addP          = 10        val shiftP        = 10
131        val mulP          = 11        val addP          = 11
132        val unaryP        = 12        val mulP          = 12
133        val preP          = 13        val castP         = 13
134        val compundP      = 14    (* compound literal *)        val unaryP        = 14
135        val postP         = 15        val preP          = 15
136        val callP         = 16        val compundP      = 16    (* compound literal *)
137        val subP          = 17        val postP         = 17
138        val atomP         = 18        val callP         = 18
139          val subP          = 19
140          val atomP         = 20
141        fun precOfBinop rator = (case rator        fun precOfBinop rator = (case rator
142               of #|| => lorP               of #|| => lorP
143                | #&& => landP                | #&& => landP
144                  | #| => borP
145                  | #^ => bxorP
146                  | #& => bandP
147                | #== => eqP | #!= => eqP                | #== => eqP | #!= => eqP
148                | #< => relP | #<= => relP | #>= => relP | #> => relP                | #< => relP | #<= => relP | #>= => relP | #> => relP
149                | #<< => shiftP | #>> => shiftP                | #<< => shiftP | #>> => shiftP
# Line 135  Line 152 
152              (* end case *))              (* end case *))
153        fun prec (E_Grp _) = atomP        fun prec (E_Grp _) = atomP
154          | prec (E_AssignOp _) = assignP          | prec (E_AssignOp _) = assignP
155            | prec (E_Cond _) = condP
156          | prec (E_BinOp(_, rator, _)) = precOfBinop rator          | prec (E_BinOp(_, rator, _)) = precOfBinop rator
157          | prec (E_UnOp _) = preP          | prec (E_UnOp _) = preP
158          | prec (E_PostOp _) = postP          | prec (E_PostOp _) = postP
# Line 142  Line 160 
160          | prec (E_Subscript _) = postP          | prec (E_Subscript _) = postP
161          | prec (E_Select _) = postP          | prec (E_Select _) = postP
162          | prec (E_Indirect _) = postP          | prec (E_Indirect _) = postP
163          | prec (E_Cast _) = unaryP          | prec (E_Cast _) = castP
164          | prec (E_Var _) = atomP          | prec (E_Var _) = atomP
165          | prec (E_Int _) = atomP          | prec (E_Int _) = atomP
166          | prec (E_Flt _) = atomP          | prec (E_Flt _) = atomP
# Line 157  Line 175 
175            in            in
176              E_AssignOp(e1', rator, e2')              E_AssignOp(e1', rator, e2')
177            end            end
178      (* note that we over-parenthesize here, but it makes nested conditionals easeier to read *)
179        fun mkCond (e1, e2, e3) = E_Cond(
180              if prec e1 <= condP then E_Grp e1 else e1,
181              if prec e2 <= condP then E_Grp e2 else e2,
182              if prec e3 < condP then E_Grp e3 else e3)
183    (* Note that all C binary operators are left associative. *)    (* Note that all C binary operators are left associative. *)
184      fun mkBinOp (e1, #-, e2 as E_UnOp(%-, _)) = let      fun mkBinOp (e1, #-, e2 as E_UnOp(%-, _)) = let
185            val e1' = if prec e1 < addP then E_Grp e1 else e1            val e1' = if prec e1 < addP then E_Grp e1 else e1
# Line 191  Line 214 
214      fun mkIndirect (e, f) = if prec e < postP      fun mkIndirect (e, f) = if prec e < postP
215            then E_Indirect(E_Grp e, f)            then E_Indirect(E_Grp e, f)
216            else E_Indirect(e, f)            else E_Indirect(e, f)
217      fun mkCast (ty, e) = E_Cast(ty, e)      fun mkCast (ty, e) = if prec e < castP
218              then E_Cast(ty, E_Grp e)
219              else E_Cast(ty, e)
220      val mkVar = E_Var      val mkVar = E_Var
221      fun mkIntTy (n, ty) = if n < 0 then E_UnOp(%-, E_Int(~n, ty)) else E_Int(n, ty)      fun mkIntTy (n, ty) = if n < 0 then E_UnOp(%-, E_Int(~n, ty)) else E_Int(n, ty)
222      fun mkInt n = mkIntTy(n, intTy)      fun mkInt n = mkIntTy(n, intTy)
# Line 213  Line 238 
238      fun mkBlock [stm] = stm      fun mkBlock [stm] = stm
239        | mkBlock stms = S_Block stms        | mkBlock stms = S_Block stms
240      fun mkDecl (ty, x, init) = S_Decl([], ty, x, init)      fun mkDecl (ty, x, init) = S_Decl([], ty, x, init)
241        fun mkDeclInit (ty, x, init) = S_Decl([], ty, x, SOME(I_Exp init))
242      val mkAttrDecl = S_Decl      val mkAttrDecl = S_Decl
243      val mkExpStm = S_Exp      val mkExpStm = S_Exp
244      fun mkAssign (e1, e2) = S_Exp(mkAssignOp(e1, $=, e2))      fun mkAssign (e1, e2) = S_Exp(mkAssignOp(e1, $=, e2))
# Line 220  Line 246 
246      fun mkIfThen (e, b) = mkIfThenElse (e, b, skip)      fun mkIfThen (e, b) = mkIfThenElse (e, b, skip)
247      val mkFor = S_For      val mkFor = S_For
248      fun mkWhile (e, b) = S_While(paren e, b)      fun mkWhile (e, b) = S_While(paren e, b)
249        fun mkDoWhile (b, e) = S_DoWhile(b, paren e)
250      val mkCall = S_Call      val mkCall = S_Call
251      val mkReturn = S_Return      val mkReturn = S_Return
252      val mkBreak = S_Break      val mkBreak = S_Break
# Line 247  Line 274 
274             of #|| => "||"             of #|| => "||"
275              | #&& => "&&"              | #&& => "&&"
276              | #== => "=="              | #== => "=="
277                | #| => "|"
278                | #^ => "^"
279                | #& => "&"
280              | #!= => "!="              | #!= => "!="
281              | #< => "<"              | #< => "<"
282              | #<= => "<="              | #<= => "<="
# Line 276  Line 306 
306              | ^-- => "--"              | ^-- => "--"
307            (* end case *))            (* end case *))
308    
309      (* generate verbatim text from a template string by substituting for placeholders
310       * Placeholders have the syntax @<id>@ and are replaced with the string associated
311       * with <id> in the list of substitutions.  If <id> is empty, then no substitution
312       * is applied, instead the "@@" is replaced by "@".
313       *)
314        local
315          structure SS = Substring
316        in
317        fun verbatim sl subs = let
318              fun scan (start, ss, n, frags) = (case SS.getc ss
319                     of SOME(#"@", rest) => let
320                          val frags = SS.slice(start, 0, SOME n) :: frags
321                          val (expansion, rest) = scanPlaceholder rest
322                          in
323                            scan (rest, rest, 0, expansion::frags)
324                          end
325                      | SOME(_, rest) => scan (start, rest, n+1, frags)
326                      | NONE => SS.concat(List.rev(start::frags))
327                    (* end case *))
328              and scanPlaceholder start = let
329                    fun scan (ss, n) = (case SS.getc ss
330                           of NONE => raise Fail "incomplete placeholder"
331                            | SOME(#"@", rest) => (SS.string(SS.slice(start, 0, SOME n)), rest)
332                            | SOME(_, rest) => scan (rest, n+1)
333                          (* end case *))
334                    val (placeholder, rest) = scan (start, 0)
335                    in
336                      if (placeholder = "")
337                        then (SS.full "@", rest)
338                        else (case List.find (fn (s, _) => (s = placeholder)) subs
339                           of SOME(_, expansion) => (SS.full expansion, rest)
340                            | NONE => raise Fail(concat["unknown placeholder @", placeholder, "@"])
341                          (* end case *))
342                    end
343              fun expand s = let
344                    val ss = SS.full s
345                    in
346                      scan (ss, ss, 0, [])
347                    end
348              in
349                D_Verbatim(List.map expand sl)
350              end
351        end (* local *)
352    
353    (* for debugging (not syntactically correct!) *)    (* for debugging (not syntactically correct!) *)
354      fun expToString e = let      fun expToString e = let
355            fun e2s (e, l) = (case e            fun e2s (e, l) = (case e
356                 of E_Grp e => "(" :: e2s(e, ")"::l)                 of E_Grp e => "(" :: e2s(e, ")"::l)
357                  | E_AssignOp(e1, rator, e2) => e2s(e1, assignopToString rator :: e2s(e2, l))                  | E_AssignOp(e1, rator, e2) => e2s(e1, assignopToString rator :: e2s(e2, l))
358                    | E_Cond(e1, e2, e3) => "(" :: e2s(e1, "?" :: e2s(e2, ":" :: e2s (e3, ")" :: l)))
359                  | E_BinOp(e1, rator, e2) => e2s(e1, binopToString rator :: e2s(e2, l))                  | E_BinOp(e1, rator, e2) => e2s(e1, binopToString rator :: e2s(e2, l))
360                  | E_UnOp(rator, e) => unopToString rator :: e2s(e, l)                  | E_UnOp(rator, e) => unopToString rator :: e2s(e, l)
361                  | E_PostOp(e, rator) => e2s(e, postopToString rator :: l)                  | E_PostOp(e, rator) => e2s(e, postopToString rator :: l)

Legend:
Removed from v.1368  
changed lines
  Added in v.1766

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