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: type variable = Chris@82: (* temporary variables generated automatically *) Chris@82: | Temporary of int Chris@82: (* memory locations, e.g., array elements *) Chris@82: | Locative of (Unique.unique * Unique.unique * Chris@82: (int -> string) * int * string) Chris@82: (* constant values, e.g., twiddle factors *) Chris@82: | Constant of (Unique.unique * string) Chris@82: Chris@82: let hash v = Hashtbl.hash v Chris@82: Chris@82: let same a b = (a == b) Chris@82: Chris@82: let is_constant = function Chris@82: | Constant _ -> true Chris@82: | _ -> false Chris@82: Chris@82: let is_temporary = function Chris@82: | Temporary _ -> true Chris@82: | _ -> false Chris@82: Chris@82: let is_locative = function Chris@82: | Locative _ -> true Chris@82: | _ -> false Chris@82: Chris@82: let same_location a b = Chris@82: match (a, b) with Chris@82: | (Locative (location_a, _, _, _, _), Locative (location_b, _, _, _, _)) -> Chris@82: Unique.same location_a location_b Chris@82: | _ -> false Chris@82: Chris@82: let same_class a b = Chris@82: match (a, b) with Chris@82: | (Locative (_, class_a, _, _, _), Locative (_, class_b, _, _, _)) -> Chris@82: Unique.same class_a class_b Chris@82: | (Constant (class_a, _), Constant (class_b, _)) -> Chris@82: Unique.same class_a class_b Chris@82: | _ -> false Chris@82: Chris@82: let make_temporary = Chris@82: let tmp_count = ref 0 Chris@82: in fun () -> begin Chris@82: tmp_count := !tmp_count + 1; Chris@82: Temporary !tmp_count Chris@82: end Chris@82: Chris@82: let make_constant class_token name = Chris@82: Constant (class_token, name) Chris@82: Chris@82: let make_locative location_token class_token name i vs = Chris@82: Locative (location_token, class_token, name, i, vs) Chris@82: Chris@82: let vstride_of_locative = function Chris@82: | Locative (_, _, _, _, vs) -> vs Chris@82: | _ -> failwith "vstride_of_locative" Chris@82: Chris@82: (* special naming conventions for variables *) Chris@82: let rec base62_of_int k = Chris@82: let x = k mod 62 Chris@82: and y = k / 62 in Chris@82: let c = Chris@82: if x < 10 then Chris@82: Char.chr (x + Char.code '0') Chris@82: else if x < 36 then Chris@82: Char.chr (x + Char.code 'a' - 10) Chris@82: else Chris@82: Char.chr (x + Char.code 'A' - 36) Chris@82: in Chris@82: let s = String.make 1 c in Chris@82: let r = if y == 0 then "" else base62_of_int y in Chris@82: r ^ s Chris@82: Chris@82: let varname_of_int k = Chris@82: if !Magic.compact then Chris@82: base62_of_int k Chris@82: else Chris@82: string_of_int k Chris@82: Chris@82: let unparse = function Chris@82: | Temporary k -> "T" ^ (varname_of_int k) Chris@82: | Constant (_, name) -> name Chris@82: | Locative (_, _, name, i, _) -> name i Chris@82: Chris@82: let unparse_for_alignment m = function Chris@82: | Locative (_, _, name, i, _) -> name (i mod m) Chris@82: | _ -> failwith "unparse_for_alignment" Chris@82: