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