SCM Repository
View of /branches/pure-cfg/src/compiler/codegen/codegen-fn.sml
Parent Directory
|
Revision Log
Revision 533 -
(download)
(annotate)
Mon Feb 14 22:56:45 2011 UTC (11 years, 4 months ago) by jhr
File size: 5585 byte(s)
Mon Feb 14 22:56:45 2011 UTC (11 years, 4 months ago) by jhr
File size: 5585 byte(s)
Working on code generation
(* codegen-fn.sml * * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) * All rights reserved. * * Generic support for translating LowIL code to the target representation. We * assume that the LowIL has first been run through the splitting pass to match * the target's vector widths. *) functor CodeGenFn (T : TARGET) : sig val generate : string * LowIL.program -> unit end = struct structure IL = TreeIL structure Ty = IL.Ty structure Op = IL.Op structure V = IL.Var (* convert LowIL types to T types *) fun cvtTy ty = (case ty of Ty.BoolTy => T.boolTy | Ty.StringTy => T.stringTy | Ty.IVecTy 1 => T.intTy | Ty.IVecTy n => T.ivecTy n (* FIXME: what about vector splits? *) | Ty.VecTy 1 => T.realTy | Ty.VecTy n => T.vecTy n (* FIXME: what about vector splits? *) | Ty.AddrTy => T.boolTy (* FIXME *) | Ty.ImageTy => T.boolTy (* FIXME *) (* end case *)) fun lookup (env, x) = (case V.Map.find (env, x) of SOME x' => x' | NONE => raise Fail "lookup" (* end case *)) fun trExp (env, e) = (case e of IL.E_Var x => (case V.kind x of IL.VK_Global => T.Expr.global(lookup(env, x)) | IL.VK_State strand => raise Fail "FIXME: state var" | IL.VK_Local => T.Expr.var(lookup(env, x)) (* end case *)) | IL.E_Lit(Literal.Int n) => T.Expr.intLit n | IL.E_Lit(Literal.Bool b) => T.Expr.boolLit b | IL.E_Lit(Literal.Float f) => T.Expr.floatLit f | IL.E_Lit(Literal.String s) => T.Expr.stringLit s | IL.E_Op(rator, args) => (case (rator, trExps(env, args)) of (Op.Add ty, [a, b]) => T.Expr.add(a, b) | (Op.Sub ty, [a, b]) => T.Expr.sub(a, b) | (Op.Mul ty, [a, b]) => T.Expr.mul(a, b) | (Op.Div ty, [a, b]) => T.Expr.divide(a, b) | (Op.Neg ty, [a]) => T.Expr.neg a | (Op.LT ty, [a, b]) => T.Expr.lt(a, b) | (Op.LTE ty, [a, b]) => T.Expr.lte(a, b) | (Op.EQ ty, [a, b]) => T.Expr.equ(a, b) | (Op.NEQ ty, [a, b]) => T.Expr.neq(a, b) | (Op.GT ty, [a, b]) => T.Expr.gt(a, b) | (Op.GTE ty, [a, b]) => T.Expr.gte(a, b) | (Op.Not, [a]) => T.Expr.not a | (Op.Max, [a, b]) => T.Expr.max(a, b) | (Op.Min, [a, b]) => T.Expr.min(a, b) | (Op.Sin, [a]) => T.Expr.sin a | (Op.Cos, [a]) => T.Expr.cos a | (Op.Pow, [a, b]) => T.Expr.pow(a, b) | (Op.Dot d, [a, b]) => T.Expr.dot(a, b) | (Op.Cross, [a, b]) => T.Expr.cross(a, b) | (Op.Select(ty, i), [a]) => T.Expr.select(i, a) | (Op.Norm d, [a]) => T.Expr.length a | (Op.Scale d, [a, b]) => T.Expr.mul(a, b) | (Op.InvScale d, [a, b]) => T.Expr.divide(a, b) | (Op.CL, _) => raise Fail "CL unimplemented" | (Op.PrincipleEvec ty, _) => raise Fail "PrincipleEvec unimplemented" (* | (Op.Subscript ty, *) | (Op.Floor d, [a]) => T.Expr.floor a | (Op.IntToReal, [a]) => T.Expr.toReal a | (Op.TruncToInt d, [a]) => T.Expr.truncToInt a | (Op.RoundToInt d, [a]) => T.Expr.roundToInt a | (Op.CeilToInt d, [a]) => T.Expr.ceilToInt a | (Op.FloorToInt d, [a]) => T.Expr.floorToInt a | (Op.ImageAddress, [a]) => T.Expr.imageAddr a | (Op.LoadVoxels(rTy, n), [a]) => T.Expr.intLit 0 (* FIXME *) | (Op.PosToImgSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *) | (Op.GradToWorldSpace d, [v, x]) => T.Expr.intLit 0 (* FIXME *) | (Op.LoadImage info, [a]) => raise Fail "impossible" | (Op.Inside d, [v, x]) => raise Fail "impossible" | (Op.Input(ty, name), []) => raise Fail "impossible" | (Op.InputWithDefault(ty, name), [a]) => T.Expr.intLit 0 (* FIXME *) | _ => raise Fail(concat[ "incorrect number of arguments for ", Op.toString rator ]) (* end case *)) (* end case *)) and trExps (env, exps) = List.map (fn exp => trExp(env, exp)) exps fun trStmt (env, stm) = (case stm of IL.S_Comment text => [T.Stmt.comment text] (* FIXME: special case for when x is a strand-state variable *) | IL.S_Assign(x, exp) => (case V.kind x of IL.VK_Global => [T.Stmt.assign(lookup(env, x), trExp(env, exp))] | IL.VK_State strand => raise Fail "FIXME: state var" | IL.VK_Local => [T.Stmt.assign(lookup(env, x), trExp(env, exp))] (* end case *)) | IL.S_Cons(lhs, args) => [T.Stmt.cons(lookup(env, lhs), trExps(env, args))] | IL.S_LoadImage(lhs, dim, name) => T.Stmt.loadImage (lookup(env, lhs), dim, trExp(env, name)) | IL.S_Input(lhs, name, optDflt) => T.Stmt.input(lookup(env, lhs), name, Option.map (fn e => trExp(env, e)) optDflt) | IL.S_IfThen(cond, thenBlk) => [T.Stmt.ifthen(trExp(env, cond), trBlock(env, thenBlk))] | IL.S_IfThenElse(cond, thenBlk, elseBlk) => [T.Stmt.ifthenelse(trExp(env, cond), trBlock(env, thenBlk), trBlock(env, elseBlk))] | IL.S_Die => [T.Stmt.die()] | IL.S_Stabilize => [T.Stmt.stabilize()] (* end case *)) and trBlock (env, IL.Block{locals, body}) = ( (* what about the locals?? *) T.Stmt.block(List.foldr (fn (stm, stms) => trStmt(env, stm)@stms) [] body)) fun generate (fileStem, srcProg) = let val TreeIL.Program{globals, globalInit, strands} = LowToTree.translate srcProg val prog = T.newProgram () (* define the globals and initialize the environment *) val env = let fun gvar (x, env) = V.Map.insert(env, x, T.Var.global(prog, cvtTy(V.ty x), V.name x)) in List.foldl gvar V.Map.empty globals end in (* global initialization *) T.globalInit (prog, trBlock (env, globalInit)); (* output the program *) T.generate (fileStem, prog) end end
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |