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