cannam@167: (* cannam@167: * Copyright (c) 1997-1999 Massachusetts Institute of Technology cannam@167: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@167: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@167: * cannam@167: * This program is free software; you can redistribute it and/or modify cannam@167: * it under the terms of the GNU General Public License as published by cannam@167: * the Free Software Foundation; either version 2 of the License, or cannam@167: * (at your option) any later version. cannam@167: * cannam@167: * This program is distributed in the hope that it will be useful, cannam@167: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@167: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@167: * GNU General Public License for more details. cannam@167: * cannam@167: * You should have received a copy of the GNU General Public License cannam@167: * along with this program; if not, write to the Free Software cannam@167: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@167: * cannam@167: *) cannam@167: cannam@167: type variable = cannam@167: (* temporary variables generated automatically *) cannam@167: | Temporary of int cannam@167: (* memory locations, e.g., array elements *) cannam@167: | Locative of (Unique.unique * Unique.unique * cannam@167: (int -> string) * int * string) cannam@167: (* constant values, e.g., twiddle factors *) cannam@167: | Constant of (Unique.unique * string) cannam@167: cannam@167: let hash v = Hashtbl.hash v cannam@167: cannam@167: let same a b = (a == b) cannam@167: cannam@167: let is_constant = function cannam@167: | Constant _ -> true cannam@167: | _ -> false cannam@167: cannam@167: let is_temporary = function cannam@167: | Temporary _ -> true cannam@167: | _ -> false cannam@167: cannam@167: let is_locative = function cannam@167: | Locative _ -> true cannam@167: | _ -> false cannam@167: cannam@167: let same_location a b = cannam@167: match (a, b) with cannam@167: | (Locative (location_a, _, _, _, _), Locative (location_b, _, _, _, _)) -> cannam@167: Unique.same location_a location_b cannam@167: | _ -> false cannam@167: cannam@167: let same_class a b = cannam@167: match (a, b) with cannam@167: | (Locative (_, class_a, _, _, _), Locative (_, class_b, _, _, _)) -> cannam@167: Unique.same class_a class_b cannam@167: | (Constant (class_a, _), Constant (class_b, _)) -> cannam@167: Unique.same class_a class_b cannam@167: | _ -> false cannam@167: cannam@167: let make_temporary = cannam@167: let tmp_count = ref 0 cannam@167: in fun () -> begin cannam@167: tmp_count := !tmp_count + 1; cannam@167: Temporary !tmp_count cannam@167: end cannam@167: cannam@167: let make_constant class_token name = cannam@167: Constant (class_token, name) cannam@167: cannam@167: let make_locative location_token class_token name i vs = cannam@167: Locative (location_token, class_token, name, i, vs) cannam@167: cannam@167: let vstride_of_locative = function cannam@167: | Locative (_, _, _, _, vs) -> vs cannam@167: | _ -> failwith "vstride_of_locative" cannam@167: cannam@167: (* special naming conventions for variables *) cannam@167: let rec base62_of_int k = cannam@167: let x = k mod 62 cannam@167: and y = k / 62 in cannam@167: let c = cannam@167: if x < 10 then cannam@167: Char.chr (x + Char.code '0') cannam@167: else if x < 36 then cannam@167: Char.chr (x + Char.code 'a' - 10) cannam@167: else cannam@167: Char.chr (x + Char.code 'A' - 36) cannam@167: in cannam@167: let s = String.make 1 c in cannam@167: let r = if y == 0 then "" else base62_of_int y in cannam@167: r ^ s cannam@167: cannam@167: let varname_of_int k = cannam@167: if !Magic.compact then cannam@167: base62_of_int k cannam@167: else cannam@167: string_of_int k cannam@167: cannam@167: let unparse = function cannam@167: | Temporary k -> "T" ^ (varname_of_int k) cannam@167: | Constant (_, name) -> name cannam@167: | Locative (_, _, name, i, _) -> name i cannam@167: cannam@167: let unparse_for_alignment m = function cannam@167: | Locative (_, _, name, i, _) -> name (i mod m) cannam@167: | _ -> failwith "unparse_for_alignment" cannam@167: