Chris@82: (* Chris@82: * Copyright (c) 1997-1999 Massachusetts Institute of Technology Chris@82: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@82: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@82: * Chris@82: * This program is free software; you can redistribute it and/or modify Chris@82: * it under the terms of the GNU General Public License as published by Chris@82: * the Free Software Foundation; either version 2 of the License, or Chris@82: * (at your option) any later version. Chris@82: * Chris@82: * This program is distributed in the hope that it will be useful, Chris@82: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@82: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@82: * GNU General Public License for more details. Chris@82: * Chris@82: * You should have received a copy of the GNU General Public License Chris@82: * along with this program; if not, write to the Free Software Chris@82: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@82: * Chris@82: *) Chris@82: Chris@82: open Expr Chris@82: open List Chris@82: open Printf Chris@82: open Variable Chris@82: open Annotate Chris@82: open Simdmagic Chris@82: open C Chris@82: Chris@82: let realtype = "V" Chris@82: let realtypep = realtype ^ " *" Chris@82: let constrealtype = "const " ^ realtype Chris@82: let constrealtypep = constrealtype ^ " *" Chris@82: let alignment_mod = 2 Chris@82: Chris@82: (* Chris@82: * SIMD C AST unparser Chris@82: *) Chris@82: let foldr_string_concat l = fold_right (^) l "" Chris@82: Chris@82: let rec unparse_by_twiddle nam tw src = Chris@82: sprintf "%s(&(%s),%s)" nam (Variable.unparse tw) (unparse_expr src) Chris@82: Chris@82: and unparse_store dst = function Chris@82: | Times (NaN MULTI_A, x) -> Chris@82: sprintf "STM%d(&(%s),%s,%s,&(%s));\n" Chris@82: !Simdmagic.store_multiple Chris@82: (Variable.unparse dst) (unparse_expr x) Chris@82: (Variable.vstride_of_locative dst) Chris@82: (Variable.unparse_for_alignment alignment_mod dst) Chris@82: | Times (NaN MULTI_B, Plus stuff) -> Chris@82: sprintf "STN%d(&(%s)%s,%s);\n" Chris@82: !Simdmagic.store_multiple Chris@82: (Variable.unparse dst) Chris@82: (List.fold_right (fun x a -> "," ^ (unparse_expr x) ^ a) stuff "") Chris@82: (Variable.vstride_of_locative dst) Chris@82: | src_expr -> Chris@82: sprintf "ST(&(%s),%s,%s,&(%s));\n" Chris@82: (Variable.unparse dst) (unparse_expr src_expr) Chris@82: (Variable.vstride_of_locative dst) Chris@82: (Variable.unparse_for_alignment alignment_mod dst) Chris@82: Chris@82: and unparse_expr = Chris@82: let rec unparse_plus = function Chris@82: | [a] -> unparse_expr a Chris@82: Chris@82: | (Uminus (Times (NaN I, b))) :: c :: d -> op2 "VFNMSI" [b] (c :: d) Chris@82: | c :: (Uminus (Times (NaN I, b))) :: d -> op2 "VFNMSI" [b] (c :: d) Chris@82: | (Uminus (Times (NaN CONJ, b))) :: c :: d -> op2 "VFNMSCONJ" [b] (c :: d) Chris@82: | c :: (Uminus (Times (NaN CONJ, b))) :: d -> op2 "VFNMSCONJ" [b] (c :: d) Chris@82: | (Times (NaN I, b)) :: c :: d -> op2 "VFMAI" [b] (c :: d) Chris@82: | c :: (Times (NaN I, b)) :: d -> op2 "VFMAI" [b] (c :: d) Chris@82: | (Times (NaN CONJ, b)) :: (Uminus c) :: d -> op2 "VFMSCONJ" [b] (c :: d) Chris@82: | (Uminus c) :: (Times (NaN CONJ, b)) :: d -> op2 "VFMSCONJ" [b] (c :: d) Chris@82: | (Times (NaN CONJ, b)) :: c :: d -> op2 "VFMACONJ" [b] (c :: d) Chris@82: | c :: (Times (NaN CONJ, b)) :: d -> op2 "VFMACONJ" [b] (c :: d) Chris@82: | (Times (NaN _, b)) :: (Uminus c) :: d -> failwith "VFMS NaN" Chris@82: | (Uminus c) :: (Times (NaN _, b)) :: d -> failwith "VFMS NaN" Chris@82: Chris@82: | (Uminus (Times (a, b))) :: c :: d -> op3 "VFNMS" a b (c :: d) Chris@82: | c :: (Uminus (Times (a, b))) :: d -> op3 "VFNMS" a b (c :: d) Chris@82: | (Times (a, b)) :: (Uminus c) :: d -> op3 "VFMS" a b (c :: negate d) Chris@82: | (Uminus c) :: (Times (a, b)) :: d -> op3 "VFMS" a b (c :: negate d) Chris@82: | (Times (a, b)) :: c :: d -> op3 "VFMA" a b (c :: d) Chris@82: | c :: (Times (a, b)) :: d -> op3 "VFMA" a b (c :: d) Chris@82: Chris@82: | (Uminus a :: b) -> op2 "VSUB" b [a] Chris@82: | (b :: Uminus a :: c) -> op2 "VSUB" (b :: c) [a] Chris@82: | (a :: b) -> op2 "VADD" [a] b Chris@82: | [] -> failwith "unparse_plus" Chris@82: and op3 nam a b c = Chris@82: nam ^ "(" ^ (unparse_expr a) ^ ", " ^ (unparse_expr b) ^ ", " ^ Chris@82: (unparse_plus c) ^ ")" Chris@82: and op2 nam a b = Chris@82: nam ^ "(" ^ (unparse_plus a) ^ ", " ^ (unparse_plus b) ^ ")" Chris@82: and op1 nam a = Chris@82: nam ^ "(" ^ (unparse_expr a) ^ ")" Chris@82: and negate = function Chris@82: | [] -> [] Chris@82: | (Uminus x) :: y -> x :: negate y Chris@82: | x :: y -> (Uminus x) :: negate y Chris@82: Chris@82: in function Chris@82: | CTimes(Load tw, src) Chris@82: when Variable.is_constant tw && !Magic.generate_bytw -> Chris@82: unparse_by_twiddle "BYTW" tw src Chris@82: | CTimesJ(Load tw, src) Chris@82: when Variable.is_constant tw && !Magic.generate_bytw -> Chris@82: unparse_by_twiddle "BYTWJ" tw src Chris@82: | Load v when is_locative(v) -> Chris@82: sprintf "LD(&(%s), %s, &(%s))" (Variable.unparse v) Chris@82: (Variable.vstride_of_locative v) Chris@82: (Variable.unparse_for_alignment alignment_mod v) Chris@82: | Load v when is_constant(v) -> sprintf "LDW(&(%s))" (Variable.unparse v) Chris@82: | Load v -> Variable.unparse v Chris@82: | Num n -> sprintf "LDK(%s)" (Number.to_konst n) Chris@82: | NaN n -> failwith "NaN in unparse_expr" Chris@82: | Plus [] -> "0.0 /* bug */" Chris@82: | Plus [a] -> " /* bug */ " ^ (unparse_expr a) Chris@82: | Plus a -> unparse_plus a Chris@82: | Times(NaN I,b) -> op1 "VBYI" b Chris@82: | Times(NaN CONJ,b) -> op1 "VCONJ" b Chris@82: | Times(a,b) -> Chris@82: sprintf "VMUL(%s, %s)" (unparse_expr a) (unparse_expr b) Chris@82: | CTimes(a,Times(NaN I, b)) -> Chris@82: sprintf "VZMULI(%s, %s)" (unparse_expr a) (unparse_expr b) Chris@82: | CTimes(a,b) -> Chris@82: sprintf "VZMUL(%s, %s)" (unparse_expr a) (unparse_expr b) Chris@82: | CTimesJ(a,Times(NaN I, b)) -> Chris@82: sprintf "VZMULIJ(%s, %s)" (unparse_expr a) (unparse_expr b) Chris@82: | CTimesJ(a,b) -> Chris@82: sprintf "VZMULJ(%s, %s)" (unparse_expr a) (unparse_expr b) Chris@82: | Uminus a when !Magic.vneg -> op1 "VNEG" a Chris@82: | Uminus a -> failwith "SIMD Uminus" Chris@82: | _ -> failwith "unparse_expr" Chris@82: Chris@82: and unparse_decl x = C.unparse_decl x Chris@82: Chris@82: and unparse_ast ast = Chris@82: let rec unparse_assignment = function Chris@82: | Assign (v, x) when Variable.is_locative v -> Chris@82: unparse_store v x Chris@82: | Assign (v, x) -> Chris@82: (Variable.unparse v) ^ " = " ^ (unparse_expr x) ^ ";\n" Chris@82: Chris@82: and unparse_annotated force_bracket = Chris@82: let rec unparse_code = function Chris@82: | ADone -> "" Chris@82: | AInstr i -> unparse_assignment i Chris@82: | ASeq (a, b) -> Chris@82: (unparse_annotated false a) ^ (unparse_annotated false b) Chris@82: and declare_variables l = Chris@82: let rec uvar = function Chris@82: [] -> failwith "uvar" Chris@82: | [v] -> (Variable.unparse v) ^ ";\n" Chris@82: | a :: b -> (Variable.unparse a) ^ ", " ^ (uvar b) Chris@82: in let rec vvar l = Chris@82: let s = if !Magic.compact then 15 else 1 in Chris@82: if (List.length l <= s) then Chris@82: match l with Chris@82: [] -> "" Chris@82: | _ -> realtype ^ " " ^ (uvar l) Chris@82: else Chris@82: (vvar (Util.take s l)) ^ (vvar (Util.drop s l)) Chris@82: in vvar (List.filter Variable.is_temporary l) Chris@82: in function Chris@82: Annotate (_, _, decl, _, code) -> Chris@82: if (not force_bracket) && (Util.null decl) then Chris@82: unparse_code code Chris@82: else "{\n" ^ Chris@82: (declare_variables decl) ^ Chris@82: (unparse_code code) ^ Chris@82: "}\n" Chris@82: Chris@82: in match ast with Chris@82: | Asch a -> (unparse_annotated true a) Chris@82: | Return x -> "return " ^ unparse_ast x ^ ";" Chris@82: | Simd_leavefun -> "VLEAVE();" Chris@82: | For (a, b, c, d) -> Chris@82: "for (" ^ Chris@82: unparse_ast a ^ "; " ^ unparse_ast b ^ "; " ^ unparse_ast c Chris@82: ^ ")" ^ unparse_ast d Chris@82: | If (a, d) -> Chris@82: "if (" ^ Chris@82: unparse_ast a Chris@82: ^ ")" ^ unparse_ast d Chris@82: | Block (d, s) -> Chris@82: if (s == []) then "" Chris@82: else Chris@82: "{\n" ^ Chris@82: foldr_string_concat (map unparse_decl d) ^ Chris@82: foldr_string_concat (map unparse_ast s) ^ Chris@82: "}\n" Chris@82: | x -> C.unparse_ast x Chris@82: Chris@82: and unparse_function = function Chris@82: Fcn (typ, name, args, body) -> Chris@82: let rec unparse_args = function Chris@82: [Decl (a, b)] -> a ^ " " ^ b Chris@82: | (Decl (a, b)) :: s -> a ^ " " ^ b ^ ", " Chris@82: ^ unparse_args s Chris@82: | [] -> "" Chris@82: | _ -> failwith "unparse_function" Chris@82: in Chris@82: (typ ^ " " ^ name ^ "(" ^ unparse_args args ^ ")\n" ^ Chris@82: unparse_ast body) Chris@82: Chris@82: let extract_constants f = Chris@82: let constlist = flatten (map expr_to_constants (C.ast_to_expr_list f)) Chris@82: in map Chris@82: (fun n -> Chris@82: Tdecl Chris@82: ("DVK(" ^ (Number.to_konst n) ^ ", " ^ (Number.to_string n) ^ Chris@82: ");\n")) Chris@82: (unique_constants constlist)