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

SCM Repository

[diderot] Diff of /branches/pure-cfg/src/compiler/c-target/c-target.sml
ViewVC logotype

Diff of /branches/pure-cfg/src/compiler/c-target/c-target.sml

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

revision 547, Thu Feb 17 14:57:07 2011 UTC revision 548, Thu Feb 17 22:41:53 2011 UTC
# Line 18  Line 18 
18        | T_Real                  (* default real type *)        | T_Real                  (* default real type *)
19        | T_Vec of int        | T_Vec of int
20        | T_IVec of int        | T_IVec of int
21        | T_Image of int          (* n-dimensional image data *)        | T_Image of int * RawTypes.ty    (* n-dimensional image data *)
22        | T_Data of CL.ty         (* pointer to image data *)        | T_Ptr of RawTypes.ty            (* pointer type *)
23    
24    (* string representation of types (for debugging *)    (* string representation of types (for debugging *)
25      fun tyToString ty = (case ty      fun tyToString ty = (case ty
# Line 29  Line 29 
29              | T_Real => "T_Real"              | T_Real => "T_Real"
30              | T_Vec n => concat["T_Vec(", Int.toString n, ")"]              | T_Vec n => concat["T_Vec(", Int.toString n, ")"]
31              | T_IVec n => concat["T_IVec(", Int.toString n, ")"]              | T_IVec n => concat["T_IVec(", Int.toString n, ")"]
32              | T_Image n => concat["T_Image(", Int.toString n, ")"]              | T_Image(n, ty) => concat[
33              | T_Data ty => "T_Data"                    "T_Image", Int.toString n, "D(", RawTypes.toString ty, ")"
34                    ]
35                | T_Ptr ty => concat["T_Ptr(", RawTypes.toString ty, ")"]
36            (* end case *))            (* end case *))
37    
38      datatype strand = Strand of {      datatype strand = Strand of {
# Line 92  Line 94 
94        | ivecTy n = if (n < 1) orelse (!gVectorWid < n)        | ivecTy n = if (n < 1) orelse (!gVectorWid < n)
95            then raise Size            then raise Size
96            else T_IVec n            else T_IVec n
97      fun imageTy dim = T_Image dim      fun imageTy (ImageInfo.ImgInfo{ty=([], rTy), dim, ...}) = T_Image(dim, rTy)
98        fun imageDataTy (ImageInfo.ImgInfo{ty=([], rTy), ...}) = T_Ptr rTy
99      val stringTy = T_String      val stringTy = T_String
100    
101      val statusTy = CL.T_Named "Status_t"      val statusTy = CL.T_Named "Status_t"
# Line 104  Line 107 
107        | cvtTy T_Real = !gRealTy        | cvtTy T_Real = !gRealTy
108        | cvtTy (T_Vec n) = CLang.T_Named(concat["Diderot_vec", Int.toString n, "D_t"])        | cvtTy (T_Vec n) = CLang.T_Named(concat["Diderot_vec", Int.toString n, "D_t"])
109        | cvtTy (T_IVec n) = raise Fail "FIXME: T_IVec"        | cvtTy (T_IVec n) = raise Fail "FIXME: T_IVec"
110        | cvtTy (T_Image n) = CLang.T_Named(concat["Diderot_image", Int.toString n, "D_t"])        | cvtTy (T_Image(n, _)) = CLang.T_Named(concat["Diderot_image", Int.toString n, "D_t"])
111        | cvtTy (T_Data ty) = ty        | cvtTy (T_Ptr ty) = CL.T_Ptr(CL.T_Num ty)
112    
113      (* report invalid arguments *)
114        fun invalid (name, []) = raise Fail("invaild "^name)
115          | invalid (name, args) = let
116              fun arg2s (e, ty) = concat["(", CL.expToString e, " : ", tyToString ty, ")"]
117              val args = String.concatWith ", " (List.map arg2s args)
118              in
119                raise Fail(concat["invalid arguments to ", name, ": ", args])
120              end
121    
122    (* helper functions for checking the types of arguments *)    (* helper functions for checking the types of arguments *)
123      fun scalarTy T_Int = true      fun scalarTy T_Int = true
124        | scalarTy T_Real = true        | scalarTy T_Real = true
125        | scalarTy _ = false        | scalarTy _ = false
126      fun numTy T_Bool = false      fun numTy T_Int = true
127        | numTy (T_Image _) = false        | numTy T_Real = true
128        | numTy (T_Data _) = false        | numTy (T_Vec _) = true
129        | numTy _ = true        | numTy (T_IVec _) = true
130          | numTy _ = false
131    
132      fun newProgram () = (      fun newProgram () = (
133            initTargetSpec();            initTargetSpec();
# Line 158  Line 171 
171          fun stringLit s = (CL.mkStr s, stringTy)          fun stringLit s = (CL.mkStr s, stringTy)
172          fun boolLit b = (CL.mkBool b, boolTy)          fun boolLit b = (CL.mkBool b, boolTy)
173    
       (* vector construction *)  
         fun vector _ = raise Fail "FIXME: Expr.vector"  
   
174        (* select from a vector *)        (* select from a vector *)
175          fun select (i, (e, T_Vec n)) =          fun select (i, (e, T_Vec n)) =
176                if (i < 0) orelse (n <= i)                if (i < 0) orelse (n <= i)
# Line 170  Line 180 
180                if (i < 0) orelse (n <= i)                if (i < 0) orelse (n <= i)
181                  then raise Subscript                  then raise Subscript
182                  else (CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i, CL.int32)), T_Int)                  else (CL.mkSubscript(e, CL.mkInt(IntInf.fromInt i, CL.int32)), T_Int)
183            | select _ = raise Fail "invalid argument to select"            | select (_, x) = invalid("select", [x])
184    
185        (* vector (and scalar) arithmetic *)        (* vector (and scalar) arithmetic *)
186          local          local
# Line 178  Line 188 
188            fun binop rator ((e1, ty1), (e2, ty2)) =            fun binop rator ((e1, ty1), (e2, ty2)) =
189                  if checkTys (ty1, ty2)                  if checkTys (ty1, ty2)
190                    then (CL.mkBinOp(e1, rator, e2), ty1)                    then (CL.mkBinOp(e1, rator, e2), ty1)
191                    else raise Fail(concat[                    else invalid (
192                        "invalid arguments to binary operator \"",                      concat["binary operator \"", CL.binopToString rator, "\""],
193                        CL.binopToString rator, "\""                      [(e1, ty1), (e2, ty2)])
                     ])  
194          in          in
195          val add = binop CL.#+          fun add ((e1, ty as T_Ptr _), (e2, T_Int)) = (CL.mkBinOp(e1, CL.#+, e2), ty)
196          val sub = binop CL.#-            | add args = binop CL.#+ args
197            fun sub ((e1, ty as T_Ptr _), (e2, T_Int)) = (CL.mkBinOp(e1, CL.#-, e2), ty)
198              | sub args = binop CL.#- args
199        (* NOTE: multiplication and division are also used for scaling *)        (* NOTE: multiplication and division are also used for scaling *)
200          fun mul ((e1, T_Real), (e2, T_Vec n)) =          fun mul ((e1, T_Real), (e2, T_Vec n)) =
201                (CL.E_Apply("Diderot_scale"^vectorSuffix n, [e1, e2]), T_Vec n)                (CL.E_Apply("Diderot_scale"^vectorSuffix n, [e1, e2]), T_Vec n)
# Line 225  Line 236 
236            fun cmpop rator ((e1, ty1), (e2, ty2)) =            fun cmpop rator ((e1, ty1), (e2, ty2)) =
237                  if checkTys (ty1, ty2)                  if checkTys (ty1, ty2)
238                    then (CL.mkBinOp(e1, rator, e2), T_Bool)                    then (CL.mkBinOp(e1, rator, e2), T_Bool)
239                    else raise Fail "invalid arguments to compare operator"                    else invalid (
240                        concat["compare operator \"", CL.binopToString rator, "\""],
241                        [(e1, ty1), (e2, ty2)])
242          in          in
243          val lt = cmpop CL.#<          val lt = cmpop CL.#<
244          val lte = cmpop CL.#<=          val lte = cmpop CL.#<=
# Line 280  Line 293 
293    
294        (* conversions *)        (* conversions *)
295          fun toReal (e, T_Int) = (CL.mkCast(!gRealTy, e), T_Real)          fun toReal (e, T_Int) = (CL.mkCast(!gRealTy, e), T_Real)
296            | toReal _ = raise Fail "invalid argument for toReal"            | toReal e = invalid ("toReal", [e])
297    
298          fun truncToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(trunc e)), T_Int)          fun truncToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(trunc e)), T_Int)
299            | truncToInt _ = raise Fail "invalid argument for truncToInt"            | truncToInt e = invalid ("truncToInt", [e])
300          fun roundToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(round e)), T_Int)          fun roundToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(round e)), T_Int)
301            | roundToInt _ = raise Fail "invalid argument for roundToInt"            | roundToInt e = invalid ("roundToInt", [e])
302          fun ceilToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(floor e)), T_Int)          fun ceilToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(floor e)), T_Int)
303            | ceilToInt _ = raise Fail "invalid argument for ceilToInt"            | ceilToInt e = invalid ("ceilToInt", [e])
304          fun floorToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(ceil e)), T_Int)          fun floorToInt (e as (_, T_Real)) = (CL.mkCast(!gIntTy, #1(ceil e)), T_Int)
305            | floorToInt _ = raise Fail "invalid argument for floorToInt"            | floorToInt e = invalid ("floorToInt", [e])
306    
307        (* runtime system hooks *)        (* runtime system hooks *)
308          fun imageAddr (e, T_Image d) = let          fun imageAddr (e, T_Image(_, rTy)) = let
309                val cTy = CL.T_Ptr(!gRealTy)                val cTy = CL.T_Ptr(!gRealTy)
310                in                in
311                  (CL.mkCast(cTy, CL.mkIndirect(e, "data")), T_Data cTy)                  (CL.mkCast(cTy, CL.mkIndirect(e, "data")), T_Ptr rTy)
312                  end
313              | imageAddr a = invalid("imageAddr", [a])
314            fun getImgData (e, T_Ptr rTy) = let
315                  val realTy as CL.T_Num rTy' = !gRealTy
316                  val e = CL.E_UnOp(CL.%*, e)
317                  in
318                    if (rTy' = rTy)
319                      then (e, T_Real)
320                      else (CL.E_Cast(realTy, e), T_Real)
321                  end
322              | getImgData a = invalid("getImgData", [a])
323            fun posToImgSpace ((img, T_Image(d, _)), (pos, T_Vec n)) = let
324                  val fnName = concat["Diderot_ToImageSpace", Int.toString d, "D"]
325                  val e = CL.mkApply(fnName, [img, pos])
326                  in
327                    (e, T_Vec n)
328                end                end
329            | imageAddr _ = raise Fail "invalid argument to imageAddr"            | posToImgSpace (a, b) = invalid("posToImgSpace", [a, b])
330          fun inside (d, (pos, T_Vec n), (img, T_Image d'), s) = let          fun inside ((pos, T_Vec n), (img, T_Image(d, _)), s) = let
331                val fnName = concat["Diderot_Inside", Int.toString d, "D"]                val fnName = concat["Diderot_Inside", Int.toString d, "D"]
332                val e = CL.mkApply(fnName,                val e = CL.mkApply(fnName,
333                      [pos, img, CL.mkInt(IntInf.fromInt n, CL.int32)])                      [pos, img, CL.mkInt(IntInf.fromInt n, CL.int32)])
334                in                in
335                  (e, T_Bool)                  (e, T_Bool)
336                end                end
337              | inside (a, b, _) = invalid("inside", [a, b])
338    
339        end (* Expr *)        end (* Expr *)
340    
# Line 322  Line 352 
352          fun ifthenelse ((e, T_Bool), s1, s2) = CL.mkIfThenElse(e, s1, s2)          fun ifthenelse ((e, T_Bool), s1, s2) = CL.mkIfThenElse(e, s1, s2)
353        (* special Diderot forms *)        (* special Diderot forms *)
354          fun cons (lhs, args) = comment ["**** cons ****"] (* FIXME *)          fun cons (lhs, args) = comment ["**** cons ****"] (* FIXME *)
355            fun getImgData (lhs, n, e) = comment ["**** getImgData ****"] (* FIXME *)
356          fun loadImage (lhs : var, dim, name : exp) = let          fun loadImage (lhs : var, dim, name : exp) = let
357                val sts = "sts"                val sts = "sts"
358                val imgTy = CL.T_Named(concat["Diderot_image", Int.toString dim, "D_t"])                val imgTy = CL.T_Named(concat["Diderot_image", Int.toString dim, "D_t"])

Legend:
Removed from v.547  
changed lines
  Added in v.548

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