SCM Repository
View of /trunk/src/compiler/simplify/eval.sml
Parent Directory
|
Revision Log
Revision 269 -
(download)
(annotate)
Wed Aug 11 04:42:30 2010 UTC (10 years, 5 months ago) by jhr
File size: 7814 byte(s)
Wed Aug 11 04:42:30 2010 UTC (10 years, 5 months ago) by jhr
File size: 7814 byte(s)
Make the "lift" phase be more agressive about evaluating
(* eval.sml * * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) * All rights reserved. * * Evaluation of "static" expressions. *) structure Eval : sig datatype value = BV of bool | SV of string | IV of IntInf.int | TV of (int list * real list) (* tensors *) | FV of FieldDef.field_def | Img of ImageInfo.info | KV of Kernel.kernel val evalStatics : Var.Set.set * Simple.block -> value Var.Map.map end = struct structure Ty = Types structure BV = BasisVars structure S = Simple structure VMap = Var.Map structure VSet = Var.Set structure VTbl = Var.Tbl datatype value = BV of bool | SV of string | IV of IntInf.int | TV of (int list * real list) (* tensors *) | FV of FieldDef.field_def | Img of ImageInfo.info | KV of Kernel.kernel fun RV r = TV([], [r]) fun toString (BV b) = Bool.toString b | toString (IV i) = IntInf.toString i | toString (SV s) = concat["\"", String.toString s, "\""] | toString (TV(s, v)) = "tensor" | toString (FV fld) = FieldDef.toString fld | toString (Img info) = ImageInfo.toString info | toString (KV h) = Kernel.toString h val tbl : (value list -> value) VTbl.hash_table = let val tbl = VTbl.mkTable (128, Fail "Eval table") fun intBinOp rator [IV a, IV b] = IV(rator(a, b)) fun tensorBinOp rator [TV(s1, v1), TV(s2, v2)] = TV(s1, ListPair.mapEq rator (v1, v2)) fun realBinOp rator [TV([], [a]), TV([], [b])] = RV(rator(a, b)) fun realUnOp rator [TV([], [a])] = RV(rator a) fun intCmp rator [IV a, IV b] = BV(rator(a, b)) fun realCmp rator [TV([], [a]), TV([], [b])] = BV(rator(a, b)) fun boolCmp rator [BV a, BV b] = BV(rator(a, b)) fun stringCmp rator [SV a, SV b] = BV(rator(a, b)) fun kernel h [] = KV h in List.app (VTbl.insert tbl) [ (BV.add_ii, intBinOp (op +)), (BV.add_tt, tensorBinOp (op +)), (BV.sub_ii, intBinOp (op -)), (BV.sub_tt, tensorBinOp (op -)), (BV.mul_ii, intBinOp (op * )), (BV.mul_rr, realBinOp (op * )), (* (BV.mul_rt, tensorOp Op.Scale), (BV.mul_tr, fn (y, sv, [t, r]) => tensorOp Op.Scale (y, sv, [r, t])), *) (BV.div_ii, intBinOp IntInf.quot), (BV.div_rr, realBinOp (op /)), (* (BV.div_tr, tensorOp Op.InvScale), *) (BV.lt_ii, intCmp (op <)), (BV.lt_rr, realCmp (op <)), (BV.lte_ii, intCmp (op <=)), (BV.lte_rr, realCmp (op <=)), (BV.gte_ii, intCmp (op >=)), (BV.gte_rr, realCmp (op >=)), (BV.gt_ii, intCmp (op >)), (BV.gt_rr, realCmp (op >)), (BV.equ_bb, boolCmp (op =)), (BV.equ_ii, intCmp (op =)), (BV.equ_ss, stringCmp (op =)), (BV.equ_rr, realCmp Real.==), (BV.neq_bb, boolCmp (op <>)), (BV.neq_ii, intCmp (op <>)), (BV.neq_ss, stringCmp (op <>)), (BV.neq_rr, realCmp Real.!=), (BV.neg_i, fn [IV i] => IV(~i)), (* (BV.neg_t, tensorOp Op.Neg), *) (BV.neg_f, fn [FV fld] => FV(FieldDef.neg fld)), (* (BV.op_at, fn (y, _, xs) => assign(y, Op.Probe, xs)), *) (BV.op_D, fn [FV fld] => FV(FieldDef.diff fld)), (* (BV.op_norm, tensorOp Op.Norm), *) (BV.op_not, fn [BV b] => BV(not b)), (* (BV.op_subscript, fn (y, [SK, NK], xs) => ??), (BV.fn_CL, fn (y, _, xs) => assign(y, Op.CL, xs)), *) (BV.op_convolve, fn [Img info, KV h] => FV(FieldDef.CONV(0, info, h))), (BV.fn_convolve, fn [KV h, Img info] => FV(FieldDef.CONV(0, info, h))), (BV.fn_cos, realUnOp Math.cos), (* (BV.fn_dot, vectorOp Op.Dot), (BV.fn_inside, fn (y, _, xs) => assign(y, Op.Inside, xs)), *) (BV.fn_max, realBinOp Real.min), (BV.fn_min, realBinOp Real.max), (BV.fn_modulate, tensorBinOp (op * )), (BV.fn_pow, realBinOp Real.Math.pow), (* (BV.fn_principleEvec, vectorOp Op.PrincipleEvec), *) (BV.fn_sin, realUnOp Math.sin), (BV.kn_bspln3, kernel Kernel.bspln3), (BV.kn_bspln5, kernel Kernel.bspln5), (BV.kn_ctmr, kernel Kernel.ctmr), (BV.kn_tent, kernel Kernel.tent), (BV.i2r, fn [IV i] => RV(real(IntInf.toInt i))) ]; tbl end fun loadImage ([Ty.DIM dim, Ty.SHAPE shp], [SV filename]) = let val Ty.DimConst d = TypeUtil.resolveDim dim val Ty.Shape dd = TypeUtil.resolveShape shp val info as ImageInfo.ImgInfo{dim, ...} = ImageInfo.getInfo filename in (* check that the expected dimension and actual dimension match *) if (d <> dim) then raise Fail(concat["image file \"", filename, "\" has wrong dimension"]) else (); (* check that the expected shape and actual shape match *) (* FIXME *) Img info end fun evalVar env x = (case VMap.find (env, x) of SOME v => v | NONE => raise Fail("undefined variable " ^ Var.uniqueNameOf x) (* end case *)) fun apply (env, f, mvs, xs) = if List.all (fn x => VMap.inDomain(env, x)) xs then (* try *)( if Var.same(f, BV.fn_load) then SOME(loadImage(mvs, List.map (evalVar env) xs)) else (case VTbl.find tbl f of SOME evalFn => SOME(evalFn (List.map (evalVar env) xs)) | NONE => NONE (* end case *)) ) handle ex => ( TextIO.output (TextIO.stdErr, concat [ Var.uniqueNameOf f, "(", String.concatWith "," (List.map Var.uniqueNameOf xs), ") fails with exception ", exnName ex, "\n" ]); raise ex) else NONE fun evalExp (env, e) = (case e of S.E_Var x => VMap.find (env, x) | S.E_Lit(Literal.Int i) => SOME(IV i) | S.E_Lit(Literal.Float f) => SOME(RV(FloatLit.toReal f)) | S.E_Lit(Literal.String s) => SOME(SV s) | S.E_Lit(Literal.Bool b) => SOME(BV b) | S.E_Tuple _ => raise Fail "E_Tuple" | S.E_Apply(f, mvs, xs, _) => apply(env, f, mvs, xs) | S.E_Cons xs => raise Fail "TODO: E_Cons" | S.E_Input(ty, name, optDefault) => raise Fail "impossible" | S.E_Field fld => SOME(FV fld) | S.E_LoadImage info => SOME(Img info) (* end case *)) fun evalStatics (statics, blk) = let fun evalBlock (env, S.Block stms) = let exception Done of value VMap.map fun evalStm (stm, env) = (case stm of S.S_Assign(x, S.E_Input(ty, name, optDefault)) => if VSet.member(statics, x) then let val optDefault = Option.map (evalVar env) optDefault val input = (case ty of Ty.T_Bool => Inputs.getInput(name, (Option.map BV) o Bool.fromString, optDefault) | Ty.T_Int => Inputs.getInput(name, (Option.map IV) o IntInf.fromString, optDefault) | Ty.T_String => Inputs.getInput(name, fn s => SOME(SV s), optDefault) | Ty.T_Tensor(Ty.Shape[]) => Inputs.getInput(name, (Option.map RV) o Real.fromString, optDefault) | Ty.T_Tensor shp => raise Fail "TODO: general tensor inputs" | _ => raise Fail(concat[ "input ", name, " has invalid type ", TypeUtil.toString ty ]) (* end case *)) in case input of SOME v => VMap.insert(env, x, v) | NONE => raise Fail("error getting required input " ^ name) (* end case *) end else env | S.S_Assign(x, e) => (case evalExp(env, e) of SOME v => (print(concat[Var.uniqueNameOf x, " = ", toString v, "\n"]); VMap.insert(env, x, v) ) | NONE => env (* end case *)) | S.S_IfThenElse(x, b1, b2) => (case VMap.find(env, x) of SOME(BV true) => evalBlock(env, b1) | SOME(BV false) => evalBlock(env, b2) | SOME _ => raise Fail "type error" | NONE => raise (Done env) (* end case *)) | S.S_New _ => raise Fail "unexpected new actor" | S.S_Die => raise Fail "unexpected die" | S.S_Stabilize => raise Fail "unexpected stabilize" (* end case *)) in (List.foldl evalStm env stms) handle Done env => env end in evalBlock (VMap.empty, blk) end end
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |