File: label.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (73 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(* label.sml
 *
 * COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies
 *)

structure Label :> LABEL =
  struct

    datatype label_kind = GLOB of string | LOCAL of string | ANON

    type label = {
	id : word,
	addr : int ref,
	kind : label_kind
      }

    local
      val cnt = ref 0w0
    in
    fun reset () = cnt := 0w0
    fun mkLab k = let val id = !cnt in cnt := id+0w1; {id=id, addr=ref ~1, kind=k} end
    end (* local *)

  (* make a global label *)
    fun global name = mkLab(GLOB name)

  (* make a label generator; note that if the prefix string is "", then
   * the standard prefix "L" will be used.
   *)
    fun label "" = label "L"
      | label prefix = let
	  val kind = LOCAL prefix
	  in
	    fn () => mkLab kind
	  end

  (* make an anonymous label *)
    fun anon () = mkLab ANON

  (* label equality, comparisons, and hashing *)
    fun same (l1 : label, l2 : label) = (#id l1 = #id l2)
    fun compare (l1 : label, l2 : label) = Word.compare(#id l1, #id l2)
    fun hash (l : label) = #id l

  (* label addresses *)
    exception GlobalLabel
    fun setAddr ({id, addr, kind=GLOB _}, _) = raise GlobalLabel
      | setAddr ({id, addr, kind}, a) = addr := a

    fun addrOf {id, addr, kind=GLOB _} = raise GlobalLabel
      | addrOf {id, addr, kind} = !addr

  (* return a string representation of the label; this function is meant for
   * debugging; use the fmt function for assembly output.
   *)
    fun toString {id, addr, kind=GLOB name} = name
      | toString {id, addr, kind=LOCAL prefix} = prefix ^ Word.toString id
      | toString {id, addr, kind=ANON} = ".L" ^ Word.toString id

  (* format a label for assembly output.  The gPrefix argument is the target
   * ABI's prefix for global symbols (e.g., "_" or "") and the aPrefix is
   * the target assembler's prefix for anonymous labels.  Local labels are
   * emitted using their specified prefxix.
   *)
    fun fmt {gPrefix, aPrefix} = let
	  fun toStr {id, addr, kind=GLOB name} = gPrefix ^ name
	    | toStr {id, addr, kind=LOCAL prefix} = prefix ^ Word.toString id
	    | toStr {id, addr, kind=ANON} = aPrefix ^ Word.toString id
	  in
	    toStr
	  end

  end