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