32 |
|
|
33 |
datatype program = Prog of { |
datatype program = Prog of { |
34 |
globals : CL.decl list ref, |
globals : CL.decl list ref, |
35 |
|
topDecls : CL.decl list ref, |
36 |
strands : strand list ref |
strands : strand list ref |
37 |
} |
} |
38 |
|
|
44 |
val gRealTy = ref CL.float |
val gRealTy = ref CL.float |
45 |
|
|
46 |
(* initialize globals based on target precision *) |
(* initialize globals based on target precision *) |
47 |
fun initGlobals () = if !Controls.doublePrecision |
fun initTargetSpec () = if !Controls.doublePrecision |
48 |
then ( |
then ( |
49 |
gVectorWid := 2; |
gVectorWid := 2; |
50 |
gIntTy := CL.int64; |
gIntTy := CL.int64; |
70 |
then raise Size |
then raise Size |
71 |
else T_IVec n |
else T_IVec n |
72 |
fun imageTy dim = T_Image dim |
fun imageTy dim = T_Image dim |
73 |
|
val stringTy = T_Data CL.charPtr |
74 |
|
|
75 |
(* convert target types to CLang types *) |
(* convert target types to CLang types *) |
76 |
fun cvtTy T_Bool = CLang.T_Named "bool" |
fun cvtTy T_Bool = CLang.T_Named "bool" |
91 |
| numTy _ = true |
| numTy _ = true |
92 |
|
|
93 |
fun newProgram () = ( |
fun newProgram () = ( |
94 |
initGlobals(); |
initTargetSpec(); |
95 |
Prog{ |
Prog{ |
96 |
globals = ref [], |
globals = ref [], |
97 |
|
topDecls = ref [], |
98 |
strands = ref [] |
strands = ref [] |
99 |
}) |
}) |
100 |
|
|
101 |
|
fun globalInit (Prog{topDecls, ...}, init) = let |
102 |
|
val initFn = CL.D_Func([], CL.voidTy, "Diderot_InitGlobals", [], init) |
103 |
|
in |
104 |
|
topDecls := initFn :: !topDecls |
105 |
|
end |
106 |
|
|
107 |
fun defineStrand (p, strandId) = raise Fail "FIXME: unimplemented" |
fun defineStrand (p, strandId) = raise Fail "FIXME: unimplemented" |
108 |
|
|
109 |
structure Var = |
structure Var = |
127 |
(* literals *) |
(* literals *) |
128 |
fun intLit n = (CL.mkInt(n, !gIntTy), intTy) |
fun intLit n = (CL.mkInt(n, !gIntTy), intTy) |
129 |
fun floatLit f = (CL.mkFlt(f, !gRealTy), realTy) |
fun floatLit f = (CL.mkFlt(f, !gRealTy), realTy) |
130 |
fun stringLit s = (CL.mkStr s, T_Data CL.charPtr) |
fun stringLit s = (CL.mkStr s, stringTy) |
131 |
fun boolLit b = (CL.mkBool b, boolTy) |
fun boolLit b = (CL.mkBool b, boolTy) |
132 |
|
|
133 |
(* vector construction *) |
(* vector construction *) |
276 |
fun ifthenelse ((e, T_Bool), s1, s2) = CL.mkIfThenElse(e, s1, s2) |
fun ifthenelse ((e, T_Bool), s1, s2) = CL.mkIfThenElse(e, s1, s2) |
277 |
fun die () = comment ["**** die ****"] (* FIXME *) |
fun die () = comment ["**** die ****"] (* FIXME *) |
278 |
fun stabilize () = comment ["**** stabilize ****"] (* FIXME *) |
fun stabilize () = comment ["**** stabilize ****"] (* FIXME *) |
279 |
|
(* |
280 |
|
| IL.S_Cons(lhs, args) => [] (* FIXME *) |
281 |
|
| IL.S_LoadImage(lhs, dim, name) => let |
282 |
|
val stsTy = CL.Named "Status_t" |
283 |
|
val sts = "sts" |
284 |
|
val imgTy = CL.Named(concat["Diderot_image", Int.toString dim, "D_t"]) |
285 |
|
val loadFn = concat["Diderot_LoadImage", Int.toString dim, "D"] |
286 |
|
in [ |
287 |
|
CL.S_Decl( |
288 |
|
stsTy, sts, |
289 |
|
SOME(CL.E_Apply(loadFn, [name, CL.mkUnOp(CL.%&, lhs)]))) |
290 |
|
] end |
291 |
|
| IL.S_Input(lhs, name, optDflt) => let |
292 |
|
val inputFn = (case TreeIL.Var.ty lhs |
293 |
|
of Ty.StringTy => "Diderot_InputString" |
294 |
|
| Ty.Ty.VecTy 1 => "Diderot_InputReal" |
295 |
|
| Ty.Ty.VecTy 3 => "Diderot_InputVec3" |
296 |
|
| _ => raise Fail "unsupported input type" |
297 |
|
(* end case *)) |
298 |
|
val (initCode, hasDflt) = (case optDflt |
299 |
|
of SOME e => ([CL.S_Assign(lhs, e)], true) |
300 |
|
| NONE => ([], false) |
301 |
|
(* end case *)) |
302 |
|
val code = [ |
303 |
|
CL.S_Decl( |
304 |
|
stsTy, sts, |
305 |
|
SOME(CL.E_Apply(inputFn, [ |
306 |
|
name, CL.mkUnOp(CL.%&, lhs), CL.mkBool hasDflt |
307 |
|
]))) |
308 |
|
] |
309 |
|
in |
310 |
|
initCode @ code |
311 |
|
end |
312 |
|
*) |
313 |
end |
end |
314 |
|
|
315 |
fun generate (baseName, Prog{globals, strands}) = let |
fun generate (baseName, Prog{globals, topDecls, strands}) = let |
316 |
val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"} |
val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"} |
317 |
val outS = TextIO.openOut fileName |
val outS = TextIO.openOut fileName |
318 |
val ppStrm = PrintAsC.new outS |
val ppStrm = PrintAsC.new outS |
319 |
|
fun ppDecl dcl = PrintAsC.output(ppStrm, dcl) |
320 |
in |
in |
321 |
List.app (fn dcl => PrintAsC.output(ppStrm, dcl)) (List.rev (!globals)); |
List.app ppDecl (List.rev (!globals)); |
322 |
|
List.app ppDecl (List.rev (!topDecls)); |
323 |
(* what about the strands, etc? *) |
(* what about the strands, etc? *) |
324 |
PrintAsC.close ppStrm; |
PrintAsC.close ppStrm; |
325 |
TextIO.closeOut outS |
TextIO.closeOut outS |