cannam@167
|
1 (*
|
cannam@167
|
2 * Copyright (c) 1997-1999 Massachusetts Institute of Technology
|
cannam@167
|
3 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
cannam@167
|
4 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
cannam@167
|
5 *
|
cannam@167
|
6 * This program is free software; you can redistribute it and/or modify
|
cannam@167
|
7 * it under the terms of the GNU General Public License as published by
|
cannam@167
|
8 * the Free Software Foundation; either version 2 of the License, or
|
cannam@167
|
9 * (at your option) any later version.
|
cannam@167
|
10 *
|
cannam@167
|
11 * This program is distributed in the hope that it will be useful,
|
cannam@167
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@167
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@167
|
14 * GNU General Public License for more details.
|
cannam@167
|
15 *
|
cannam@167
|
16 * You should have received a copy of the GNU General Public License
|
cannam@167
|
17 * along with this program; if not, write to the Free Software
|
cannam@167
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@167
|
19 *
|
cannam@167
|
20 *)
|
cannam@167
|
21
|
cannam@167
|
22 open Expr
|
cannam@167
|
23 open List
|
cannam@167
|
24 open Printf
|
cannam@167
|
25 open Variable
|
cannam@167
|
26 open Annotate
|
cannam@167
|
27 open Simdmagic
|
cannam@167
|
28 open C
|
cannam@167
|
29
|
cannam@167
|
30 let realtype = "V"
|
cannam@167
|
31 let realtypep = realtype ^ " *"
|
cannam@167
|
32 let constrealtype = "const " ^ realtype
|
cannam@167
|
33 let constrealtypep = constrealtype ^ " *"
|
cannam@167
|
34 let alignment_mod = 2
|
cannam@167
|
35
|
cannam@167
|
36 (*
|
cannam@167
|
37 * SIMD C AST unparser
|
cannam@167
|
38 *)
|
cannam@167
|
39 let foldr_string_concat l = fold_right (^) l ""
|
cannam@167
|
40
|
cannam@167
|
41 let rec unparse_by_twiddle nam tw src =
|
cannam@167
|
42 sprintf "%s(&(%s),%s)" nam (Variable.unparse tw) (unparse_expr src)
|
cannam@167
|
43
|
cannam@167
|
44 and unparse_store dst = function
|
cannam@167
|
45 | Times (NaN MULTI_A, x) ->
|
cannam@167
|
46 sprintf "STM%d(&(%s),%s,%s,&(%s));\n"
|
cannam@167
|
47 !Simdmagic.store_multiple
|
cannam@167
|
48 (Variable.unparse dst) (unparse_expr x)
|
cannam@167
|
49 (Variable.vstride_of_locative dst)
|
cannam@167
|
50 (Variable.unparse_for_alignment alignment_mod dst)
|
cannam@167
|
51 | Times (NaN MULTI_B, Plus stuff) ->
|
cannam@167
|
52 sprintf "STN%d(&(%s)%s,%s);\n"
|
cannam@167
|
53 !Simdmagic.store_multiple
|
cannam@167
|
54 (Variable.unparse dst)
|
cannam@167
|
55 (List.fold_right (fun x a -> "," ^ (unparse_expr x) ^ a) stuff "")
|
cannam@167
|
56 (Variable.vstride_of_locative dst)
|
cannam@167
|
57 | src_expr ->
|
cannam@167
|
58 sprintf "ST(&(%s),%s,%s,&(%s));\n"
|
cannam@167
|
59 (Variable.unparse dst) (unparse_expr src_expr)
|
cannam@167
|
60 (Variable.vstride_of_locative dst)
|
cannam@167
|
61 (Variable.unparse_for_alignment alignment_mod dst)
|
cannam@167
|
62
|
cannam@167
|
63 and unparse_expr =
|
cannam@167
|
64 let rec unparse_plus = function
|
cannam@167
|
65 | [a] -> unparse_expr a
|
cannam@167
|
66
|
cannam@167
|
67 | (Uminus (Times (NaN I, b))) :: c :: d -> op2 "VFNMSI" [b] (c :: d)
|
cannam@167
|
68 | c :: (Uminus (Times (NaN I, b))) :: d -> op2 "VFNMSI" [b] (c :: d)
|
cannam@167
|
69 | (Uminus (Times (NaN CONJ, b))) :: c :: d -> op2 "VFNMSCONJ" [b] (c :: d)
|
cannam@167
|
70 | c :: (Uminus (Times (NaN CONJ, b))) :: d -> op2 "VFNMSCONJ" [b] (c :: d)
|
cannam@167
|
71 | (Times (NaN I, b)) :: c :: d -> op2 "VFMAI" [b] (c :: d)
|
cannam@167
|
72 | c :: (Times (NaN I, b)) :: d -> op2 "VFMAI" [b] (c :: d)
|
cannam@167
|
73 | (Times (NaN CONJ, b)) :: (Uminus c) :: d -> op2 "VFMSCONJ" [b] (c :: d)
|
cannam@167
|
74 | (Uminus c) :: (Times (NaN CONJ, b)) :: d -> op2 "VFMSCONJ" [b] (c :: d)
|
cannam@167
|
75 | (Times (NaN CONJ, b)) :: c :: d -> op2 "VFMACONJ" [b] (c :: d)
|
cannam@167
|
76 | c :: (Times (NaN CONJ, b)) :: d -> op2 "VFMACONJ" [b] (c :: d)
|
cannam@167
|
77 | (Times (NaN _, b)) :: (Uminus c) :: d -> failwith "VFMS NaN"
|
cannam@167
|
78 | (Uminus c) :: (Times (NaN _, b)) :: d -> failwith "VFMS NaN"
|
cannam@167
|
79
|
cannam@167
|
80 | (Uminus (Times (a, b))) :: c :: d -> op3 "VFNMS" a b (c :: d)
|
cannam@167
|
81 | c :: (Uminus (Times (a, b))) :: d -> op3 "VFNMS" a b (c :: d)
|
cannam@167
|
82 | (Times (a, b)) :: (Uminus c) :: d -> op3 "VFMS" a b (c :: negate d)
|
cannam@167
|
83 | (Uminus c) :: (Times (a, b)) :: d -> op3 "VFMS" a b (c :: negate d)
|
cannam@167
|
84 | (Times (a, b)) :: c :: d -> op3 "VFMA" a b (c :: d)
|
cannam@167
|
85 | c :: (Times (a, b)) :: d -> op3 "VFMA" a b (c :: d)
|
cannam@167
|
86
|
cannam@167
|
87 | (Uminus a :: b) -> op2 "VSUB" b [a]
|
cannam@167
|
88 | (b :: Uminus a :: c) -> op2 "VSUB" (b :: c) [a]
|
cannam@167
|
89 | (a :: b) -> op2 "VADD" [a] b
|
cannam@167
|
90 | [] -> failwith "unparse_plus"
|
cannam@167
|
91 and op3 nam a b c =
|
cannam@167
|
92 nam ^ "(" ^ (unparse_expr a) ^ ", " ^ (unparse_expr b) ^ ", " ^
|
cannam@167
|
93 (unparse_plus c) ^ ")"
|
cannam@167
|
94 and op2 nam a b =
|
cannam@167
|
95 nam ^ "(" ^ (unparse_plus a) ^ ", " ^ (unparse_plus b) ^ ")"
|
cannam@167
|
96 and op1 nam a =
|
cannam@167
|
97 nam ^ "(" ^ (unparse_expr a) ^ ")"
|
cannam@167
|
98 and negate = function
|
cannam@167
|
99 | [] -> []
|
cannam@167
|
100 | (Uminus x) :: y -> x :: negate y
|
cannam@167
|
101 | x :: y -> (Uminus x) :: negate y
|
cannam@167
|
102
|
cannam@167
|
103 in function
|
cannam@167
|
104 | CTimes(Load tw, src)
|
cannam@167
|
105 when Variable.is_constant tw && !Magic.generate_bytw ->
|
cannam@167
|
106 unparse_by_twiddle "BYTW" tw src
|
cannam@167
|
107 | CTimesJ(Load tw, src)
|
cannam@167
|
108 when Variable.is_constant tw && !Magic.generate_bytw ->
|
cannam@167
|
109 unparse_by_twiddle "BYTWJ" tw src
|
cannam@167
|
110 | Load v when is_locative(v) ->
|
cannam@167
|
111 sprintf "LD(&(%s), %s, &(%s))" (Variable.unparse v)
|
cannam@167
|
112 (Variable.vstride_of_locative v)
|
cannam@167
|
113 (Variable.unparse_for_alignment alignment_mod v)
|
cannam@167
|
114 | Load v when is_constant(v) -> sprintf "LDW(&(%s))" (Variable.unparse v)
|
cannam@167
|
115 | Load v -> Variable.unparse v
|
cannam@167
|
116 | Num n -> sprintf "LDK(%s)" (Number.to_konst n)
|
cannam@167
|
117 | NaN n -> failwith "NaN in unparse_expr"
|
cannam@167
|
118 | Plus [] -> "0.0 /* bug */"
|
cannam@167
|
119 | Plus [a] -> " /* bug */ " ^ (unparse_expr a)
|
cannam@167
|
120 | Plus a -> unparse_plus a
|
cannam@167
|
121 | Times(NaN I,b) -> op1 "VBYI" b
|
cannam@167
|
122 | Times(NaN CONJ,b) -> op1 "VCONJ" b
|
cannam@167
|
123 | Times(a,b) ->
|
cannam@167
|
124 sprintf "VMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
|
cannam@167
|
125 | CTimes(a,Times(NaN I, b)) ->
|
cannam@167
|
126 sprintf "VZMULI(%s, %s)" (unparse_expr a) (unparse_expr b)
|
cannam@167
|
127 | CTimes(a,b) ->
|
cannam@167
|
128 sprintf "VZMUL(%s, %s)" (unparse_expr a) (unparse_expr b)
|
cannam@167
|
129 | CTimesJ(a,Times(NaN I, b)) ->
|
cannam@167
|
130 sprintf "VZMULIJ(%s, %s)" (unparse_expr a) (unparse_expr b)
|
cannam@167
|
131 | CTimesJ(a,b) ->
|
cannam@167
|
132 sprintf "VZMULJ(%s, %s)" (unparse_expr a) (unparse_expr b)
|
cannam@167
|
133 | Uminus a when !Magic.vneg -> op1 "VNEG" a
|
cannam@167
|
134 | Uminus a -> failwith "SIMD Uminus"
|
cannam@167
|
135 | _ -> failwith "unparse_expr"
|
cannam@167
|
136
|
cannam@167
|
137 and unparse_decl x = C.unparse_decl x
|
cannam@167
|
138
|
cannam@167
|
139 and unparse_ast ast =
|
cannam@167
|
140 let rec unparse_assignment = function
|
cannam@167
|
141 | Assign (v, x) when Variable.is_locative v ->
|
cannam@167
|
142 unparse_store v x
|
cannam@167
|
143 | Assign (v, x) ->
|
cannam@167
|
144 (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n"
|
cannam@167
|
145
|
cannam@167
|
146 and unparse_annotated force_bracket =
|
cannam@167
|
147 let rec unparse_code = function
|
cannam@167
|
148 | ADone -> ""
|
cannam@167
|
149 | AInstr i -> unparse_assignment i
|
cannam@167
|
150 | ASeq (a, b) ->
|
cannam@167
|
151 (unparse_annotated false a) ^ (unparse_annotated false b)
|
cannam@167
|
152 and declare_variables l =
|
cannam@167
|
153 let rec uvar = function
|
cannam@167
|
154 [] -> failwith "uvar"
|
cannam@167
|
155 | [v] -> (Variable.unparse v) ^ ";\n"
|
cannam@167
|
156 | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b)
|
cannam@167
|
157 in let rec vvar l =
|
cannam@167
|
158 let s = if !Magic.compact then 15 else 1 in
|
cannam@167
|
159 if (List.length l <= s) then
|
cannam@167
|
160 match l with
|
cannam@167
|
161 [] -> ""
|
cannam@167
|
162 | _ -> realtype ^ " " ^ (uvar l)
|
cannam@167
|
163 else
|
cannam@167
|
164 (vvar (Util.take s l)) ^ (vvar (Util.drop s l))
|
cannam@167
|
165 in vvar (List.filter Variable.is_temporary l)
|
cannam@167
|
166 in function
|
cannam@167
|
167 Annotate (_, _, decl, _, code) ->
|
cannam@167
|
168 if (not force_bracket) && (Util.null decl) then
|
cannam@167
|
169 unparse_code code
|
cannam@167
|
170 else "{\n" ^
|
cannam@167
|
171 (declare_variables decl) ^
|
cannam@167
|
172 (unparse_code code) ^
|
cannam@167
|
173 "}\n"
|
cannam@167
|
174
|
cannam@167
|
175 in match ast with
|
cannam@167
|
176 | Asch a -> (unparse_annotated true a)
|
cannam@167
|
177 | Return x -> "return " ^ unparse_ast x ^ ";"
|
cannam@167
|
178 | Simd_leavefun -> "VLEAVE();"
|
cannam@167
|
179 | For (a, b, c, d) ->
|
cannam@167
|
180 "for (" ^
|
cannam@167
|
181 unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c
|
cannam@167
|
182 ^ ")" ^ unparse_ast d
|
cannam@167
|
183 | If (a, d) ->
|
cannam@167
|
184 "if (" ^
|
cannam@167
|
185 unparse_ast a
|
cannam@167
|
186 ^ ")" ^ unparse_ast d
|
cannam@167
|
187 | Block (d, s) ->
|
cannam@167
|
188 if (s == []) then ""
|
cannam@167
|
189 else
|
cannam@167
|
190 "{\n" ^
|
cannam@167
|
191 foldr_string_concat (map unparse_decl d) ^
|
cannam@167
|
192 foldr_string_concat (map unparse_ast s) ^
|
cannam@167
|
193 "}\n"
|
cannam@167
|
194 | x -> C.unparse_ast x
|
cannam@167
|
195
|
cannam@167
|
196 and unparse_function = function
|
cannam@167
|
197 Fcn (typ, name, args, body) ->
|
cannam@167
|
198 let rec unparse_args = function
|
cannam@167
|
199 [Decl (a, b)] -> a ^ " " ^ b
|
cannam@167
|
200 | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", "
|
cannam@167
|
201 ^ unparse_args s
|
cannam@167
|
202 | [] -> ""
|
cannam@167
|
203 | _ -> failwith "unparse_function"
|
cannam@167
|
204 in
|
cannam@167
|
205 (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^
|
cannam@167
|
206 unparse_ast body)
|
cannam@167
|
207
|
cannam@167
|
208 let extract_constants f =
|
cannam@167
|
209 let constlist = flatten (map expr_to_constants (C.ast_to_expr_list f))
|
cannam@167
|
210 in map
|
cannam@167
|
211 (fun n ->
|
cannam@167
|
212 Tdecl
|
cannam@167
|
213 ("DVK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^
|
cannam@167
|
214 ");\n"))
|
cannam@167
|
215 (unique_constants constlist)
|