| 1 |
(* label.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2001 Bell Labs, Lucent Technologies |
| 4 |
*) |
| 5 |
|
| 6 |
structure Label :> LABEL = |
| 7 |
struct |
| 8 |
|
| 9 |
datatype label_kind = GLOB of string | LOCAL of string | ANON |
| 10 |
|
| 11 |
type label = { |
| 12 |
id : word, |
| 13 |
addr : int ref, |
| 14 |
kind : label_kind |
| 15 |
} |
| 16 |
|
| 17 |
local |
| 18 |
val cnt = ref 0w0 |
| 19 |
in |
| 20 |
fun reset () = cnt := 0w0 |
| 21 |
fun mkLab k = let val id = !cnt in cnt := id+0w1; {id=id, addr=ref ~1, kind=k} end |
| 22 |
end (* local *) |
| 23 |
|
| 24 |
(* make a global label *) |
| 25 |
fun global name = mkLab(GLOB name) |
| 26 |
|
| 27 |
(* make a label generator; note that if the prefix string is "", then |
| 28 |
* the standard prefix "L" will be used. |
| 29 |
*) |
| 30 |
fun label "" = label "L" |
| 31 |
| label prefix = let |
| 32 |
val kind = LOCAL prefix |
| 33 |
in |
| 34 |
fn () => mkLab kind |
| 35 |
end |
| 36 |
|
| 37 |
(* make an anonymous label *) |
| 38 |
fun anon () = mkLab ANON |
| 39 |
|
| 40 |
(* label equality, comparisons, and hashing *) |
| 41 |
fun same (l1 : label, l2 : label) = (#id l1 = #id l2) |
| 42 |
fun compare (l1 : label, l2 : label) = Word.compare(#id l1, #id l2) |
| 43 |
fun hash (l : label) = #id l |
| 44 |
|
| 45 |
(* label addresses *) |
| 46 |
exception GlobalLabel |
| 47 |
fun setAddr ({id, addr, kind=GLOB _}, _) = raise GlobalLabel |
| 48 |
| setAddr ({id, addr, kind}, a) = addr := a |
| 49 |
|
| 50 |
fun addrOf {id, addr, kind=GLOB _} = raise GlobalLabel |
| 51 |
| addrOf {id, addr, kind} = !addr |
| 52 |
|
| 53 |
(* return a string representation of the label; this function is meant for |
| 54 |
* debugging; use the fmt function for assembly output. |
| 55 |
*) |
| 56 |
fun toString {id, addr, kind=GLOB name} = name |
| 57 |
| toString {id, addr, kind=LOCAL prefix} = prefix ^ Word.toString id |
| 58 |
| toString {id, addr, kind=ANON} = ".L" ^ Word.toString id |
| 59 |
|
| 60 |
(* format a label for assembly output. The gPrefix argument is the target |
| 61 |
* ABI's prefix for global symbols (e.g., "_" or "") and the aPrefix is |
| 62 |
* the target assembler's prefix for anonymous labels. Local labels are |
| 63 |
* emitted using their specified prefxix. |
| 64 |
*) |
| 65 |
fun fmt {gPrefix, aPrefix} = let |
| 66 |
fun toStr {id, addr, kind=GLOB name} = gPrefix ^ name |
| 67 |
| toStr {id, addr, kind=LOCAL prefix} = prefix ^ Word.toString id |
| 68 |
| toStr {id, addr, kind=ANON} = aPrefix ^ Word.toString id |
| 69 |
in |
| 70 |
toStr |
| 71 |
end |
| 72 |
|
| 73 |
end |