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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/cxx-util/tree-to-cxx.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/cxx-util/tree-to-cxx.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3810 - (view) (download)
Original Path: branches/vis15/src/compiler/c-util/tree-to-c.sml

1 : jhr 3768 (* tree-to-c.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 :     * Translate TreeIR to the C version of CLang.
9 :     *)
10 :    
11 :     structure PseudoVars =
12 :     struct
13 :     (* TreeIR "variables" that are used to get the names needed to access the
14 :     * global and strand state variables. These are just used as keys to lookup
15 :     * the C names in the environment, so their kind and type are irrelevant.
16 :     *)
17 :     local
18 : jhr 3810 fun new name = TreeIR.Var.new (name, TreeIR.Ty.IntTy)
19 : jhr 3768 in
20 :     val selfIn = new "$selfIn"
21 :     val selfOut = new "$selfOut"
22 :     val global = new "$global"
23 :     end (* local *)
24 :     end
25 :    
26 :     structure TreeToC : sig
27 :    
28 : jhr 3810 type env = CLang.typed_var TreeIR.Var.Map.map
29 : jhr 3768
30 :     val empty : env
31 :    
32 : jhr 3810 val trType : TreeTypes.t -> CLang.ty
33 : jhr 3768
34 : jhr 3810 val trBlock : env * TreeIR.block -> CLang.stm
35 : jhr 3768
36 : jhr 3810 val trFragment : env * TreeIR.block -> env * CLang.stm list
37 : jhr 3768
38 : jhr 3810 val trExp : env * TreeIR.exp -> CLang.exp
39 : jhr 3768
40 :     (* translate an expression to a variable form; return the variable (as an expresison)
41 :     * and the (optional) declaration.
42 :     *)
43 : jhr 3810 val expToVar : env * CLang.ty * string * TreeIR.exp -> CLang.exp * CLang.stm list
44 : jhr 3768
45 : jhr 3810 val trAssign : env * CLang.exp * TreeIR.exp -> CLang.stm list
46 : jhr 3768
47 :     end = struct
48 :    
49 :     structure CL = CLang
50 :     structure IR = TreeIR
51 :     structure Op = TreeOps
52 :     structure Ty = TreeTypes
53 :     structure V = IR.Var
54 :    
55 :     datatype var = datatype CL.typed_var
56 : jhr 3810 type env = CL.typed_var TreeIR.Var.Map.map
57 : jhr 3768
58 :     val empty = V.Map.empty
59 :    
60 :     fun lookup (env, x) = (case V.Map.find (env, x)
61 :     of SOME(V(_, x')) => x'
62 :     | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
63 :     (* end case *))
64 :    
65 :    
66 : jhr 3810 (* Translate a TreeIR operator application to a CLang expression *)
67 : jhr 3768 fun trOp (rator, args) = (case (rator, args)
68 :     | (Op.IAdd, [a, b]) => CL.mkBinOp(a, CL.#+, b)
69 :     | (Op.ISub, [a, b]) => CL.mkBinOp(a, CL.#-, b)
70 :     | (Op.IMul, [a, b]) => CL.mkBinOp(a, CL.#*, b)
71 :     | (Op.IDiv, [a, b]) => CL.mkBinOp(a, CL.#/, b)
72 :     | (Op.IMod, [a, b]) => CL.mkBinOp(a, CL.#%, b)
73 :     | (Op.INeg, [a]) => CL.mkUnOp(CL.%-, a)
74 :     | (Op.RAdd, [a, b]) => CL.mkBinOp(a, CL.#+, b)
75 :     | (Op.RSub, [a, b]) => CL.mkBinOp(a, CL.#-, b)
76 :     | (Op.RMul, [a, b]) => CL.mkBinOp(a, CL.#*, b)
77 :     | (Op.RDiv, [a, b]) => CL.mkBinOp(a, CL.#/, b)
78 :     | (Op.RNeg, [a]) => CL.mkUnOp(CL.%-, a)
79 :     | (Op.LT ty, [a, b]) => CL.mkBinOp(a, CL.#<, b)
80 :     | (Op.LTE ty, [a, b]) => CL.mkBinOp(a, CL.#<=, b)
81 :     | (Op.EQ ty, [a, b]) => CL.mkBinOp(a, CL.#==, b)
82 :     | (Op.NEQ ty, [a, b]) => CL.mkBinOp(a, CL.#!=, b)
83 :     | (Op.GTE ty, [a, b]) => CL.mkBinOp(a, CL.#>=, b)
84 :     | (Op.GT ty, [a, b]) => CL.mkBinOp(a, CL.#>, b)
85 :     | (Op.Not, [a]) => CL.mkUnOp(CL.%!, a)
86 :     | (Op.Abs ty, []) =>
87 :     | (Op.Max ty, []) =>
88 :     | (Op.Min ty, []) =>
89 :     | (Op.Clamp ty, []) =>
90 :     | (Op.Lerp ty, []) =>
91 :     | (Op.VAdd d, [a, b]) =>
92 :     | (Op.VSub d, [a, b]) =>
93 :     | (Op.VScale d, [a, b]) =>
94 :     | (Op.VMul d, [a, b]) =>
95 :     | (Op.VNeg d, [a]) =>
96 :     | (Op.VSum d, [a]) =>
97 :     | (Op.TensorIndex(ty * shape), []) =>
98 :     | (Op.EigenVecs2x2, []) =>
99 :     | (Op.EigenVecs3x3, []) =>
100 :     | (Op.EigenVals2x2, []) =>
101 :     | (Op.EigenVals3x3, []) =>
102 :     | (Op.Zero ty, []) =>
103 :     | (Op.Select(ty * int), []) =>
104 :     | (Op.Subscript ty, []) =>
105 :     | (Op.MkDynamic(ty * int), []) =>
106 :     | (Op.Append ty, []) =>
107 :     | (Op.Prepend ty, []) =>
108 :     | (Op.Concat ty, []) =>
109 :     | (Op.Range, []) =>
110 :     | (Op.Length ty, []) =>
111 :     | (Op.SphereQuery(ty * ty), []) =>
112 :     | (Op.Sqrt, []) =>
113 :     | (Op.Cos, []) =>
114 :     | (Op.ArcCos, []) =>
115 :     | (Op.Sine, []) =>
116 :     | (Op.ArcSin, []) =>
117 :     | (Op.Tan, []) =>
118 :     | (Op.ArcTan, []) =>
119 :     | (Op.Exp, []) =>
120 :     | (Op.Ceiling d, []) =>
121 :     | (Op.Floor d, []) =>
122 :     | (Op.Round d, []) =>
123 :     | (Op.Trunc d, []) =>
124 :     | (Op.IntToReal, []) =>
125 :     | (Op.RealToInt d, []) =>
126 :     (*
127 :     | R_All of ty
128 :     | R_Exists of ty
129 :     | R_Max of ty
130 :     | R_Min of ty
131 :     | R_Sum of ty
132 :     | R_Product of ty
133 :     | R_Mean of ty
134 :     | R_Variance of ty
135 :     *)
136 :     | (Op.Transform(ImageInfo.info * int), []) =>
137 :     | (Op.Translate(ImageInfo.info), []) =>
138 :     | (Op.BaseAddress(ImageInfo.info), []) =>
139 :     | (Op.ControlIndex(ImageInfo.info * idxctl * int), []) =>
140 :     | (Op.Inside(ImageInfo.info * int), []) =>
141 :     | (Op.ImageDim(ImageInfo.info * int), []) =>
142 :     | (Op.LoadSeq(ty * string), []) =>
143 :     | (Op.LoadImage(ty * string), []) =>
144 :     | (Op.Print(tys), []) =>
145 :     | (Op.MathFn f, args) => CL.mkApply(??, args)
146 :     (* end case *))
147 :    
148 :     end

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