codegen-mangle.sml 3.71 KB
Newer Older
mb0's avatar
mb0 committed
1
2
3
4
5
6
7
8
9
10

structure Mangle = struct
   structure Map = StringMap
   structure VI = VarInfo
   structure FI = FieldInfo
   structure CI = ConInfo

   val variables = SymbolTables.varTable
   val fields = SymbolTables.fieldTable
   val constructors = SymbolTables.conTable
mb0's avatar
mb0 committed
11

mb0's avatar
mb0 committed
12
13
14
   val names = ref Map.empty : string Map.map ref
   val revnames = ref Map.empty : string Map.map ref
   val stamp = ref 0
mb0's avatar
mb0 committed
15
16
17
18
   fun reset () =
      (names := Map.empty
      ;revnames := Map.empty
      ;stamp := 0)
mb0's avatar
mb0 committed
19
20
21
22
23

   fun getStringOfPrim sym = Atom.toString (VI.getAtom(!variables, sym))

   fun getString sym =
      let
Axel Simon's avatar
Axel Simon committed
24
         val s = VI.getInternalString (!variables, sym)
mb0's avatar
mb0 committed
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
      in
         if String.isPrefix "%" s then getStringOfPrim sym
         else s
      end

   fun getStringOfField sym = Atom.toString(FI.getAtom(!fields, sym))
   fun getStringOfTag sym = Atom.toString(CI.getAtom(!constructors, sym))

   fun insert (plain, mangled) =
      (names :=
         Map.insert
            (!names,
             plain,
             mangled)
      ;revnames :=
         Map.insert
            (!revnames,
             mangled,
             plain))

   fun reverseFind mangled = Map.find (!revnames, mangled)

   fun next () = 
      let
         val s = !stamp
      in
         s before stamp := s + 1
      end

   fun resolveCollision n =
      let
         val s = n ^ Int.toString (next())
      in
         s
      end

   fun mangleName s =
      let
         fun tf c =
            case c of
               #"%" => "__"
             | #"#" => "_"
             | #"<" => "_lt_"
             | #">" => "_gt_"
             | #"=" => "_eq_"
             | #"!" => "_ex_"
             | #"*" => "_star_"
mb0's avatar
Up.    
mb0 committed
72
             | #"-" => "_"
mb0's avatar
mb0 committed
73
             | #"+" => "_plus_"
mb0's avatar
Up.    
mb0 committed
74
             | #"^" => "_hat_"
mb0's avatar
mb0 committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
             | #"/" => "_slash_"
             | #"?" => "_q_"
             | _ => String.str c
      in
         String.translate tf s
      end

   fun mangle f sym = 
      let
         val mangled = f sym
      in
         case reverseFind mangled of
            NONE =>
               (insert (sym, mangled)
               ;mangled)
          | SOME _ =>
               (insert (sym, resolveCollision mangled)
               ;mangled)
      end

   fun mangleExport sym =
      let
         val mangled = "__" ^ mangleName sym ^ "__"
      in
         mangled
      end

   fun mangleField sym =
      let
         val mangled = "___" ^ mangleName sym 
      in
         mangled
      end

   fun mangleTag sym =
      let
         val mangled = "__" ^ mangleName sym
      in
         mangled
      end

   fun applyField sym =
      let
         val sym = getStringOfField sym
      in
         case Map.find (!names, sym) of
            SOME csym => csym
          | NONE => mangle mangleField sym
      end

   fun applyTag sym =
      let
         val sym = getStringOfTag sym
      in
         case Map.find (!names, sym) of
            SOME csym => csym
          | NONE => mangle mangleTag sym
      end

   fun apply sym =
      let
         val sym = getString sym
      in
         case Map.find (!names, sym) of
            SOME csym => csym
          | NONE => mangle mangleName sym
      end

   fun applyExport sym =
      let
         val sym' = getString sym
         val mangled = mangleExport (getStringOfPrim sym)
      in
         case Map.find (!names, sym') of
            SOME csym => csym
          | NONE =>
               (case reverseFind mangled of
                  NONE =>
                     (insert (sym', mangled)
                     ;mangled)
                | SOME _ =>
                     (insert (sym', resolveCollision mangled)
                     ;mangled))
      end

   (*
   val apply = fn sym =>
      (Pretty.prettyTo (TextIO.stdOut, CPS.PP.var sym);print"\n";apply sym)
   *)
end