Debian Codegraph @

gcc/m2/gm2-compiler/SymbolConversion.mod

raw
1 (* SymbolConversion.mod mapping between m2 symbols and gcc symbols.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE SymbolConversion ;
23
24 FROM NameKey IMPORT Name ;
25
26 FROM Indexing IMPORT Index, InitIndex, PutIndice, GetIndice, InBounds,
27 DebugIndex, InitIndexTuned, HighIndice ;
28
29 FROM SymbolTable IMPORT IsConst, PopValue, IsValueSolved, GetSymName,
30 GetType, SkipType, NulSym ;
31
32 FROM M2Error IMPORT InternalError ;
33 FROM M2ALU IMPORT PushTypeOfTree ;
34 FROM m2block IMPORT GetErrorNode, RememberConstant ;
35 FROM gcctypes IMPORT tree ;
36 FROM M2Printf IMPORT printf1 ;
37 FROM Storage IMPORT ALLOCATE ;
38 FROM SYSTEM IMPORT ADDRESS ;
39
40 CONST
41 USEPOISON = TRUE ;
42 GGCPOISON = 0A5A5A5A5H ; (* poisoned memory contains this code *)
43
44 TYPE
45 PtrToCardinal = POINTER TO CARDINAL ;
46
47 VAR
48 mod2gcc : Index ;
49 PoisonedSymbol: ADDRESS ;
50
51
52 (*
53 Mod2Gcc - given a modula-2 symbol, sym, return the gcc equivalent.
54 *)
55
56 PROCEDURE Mod2Gcc (sym: CARDINAL) : tree ;
57 VAR
58 n : Name ;
59 t : PtrToCardinal ;
60 tr: tree ;
61 BEGIN
62 IF USEPOISON
63 THEN
64 IF InBounds(mod2gcc, sym)
65 THEN
66 t := PtrToCardinal(GetIndice(mod2gcc, sym)) ;
67 IF (t#NIL) AND (t^=GGCPOISON)
68 THEN
69 InternalError ('gcc symbol has been poisoned')
70 END
71 END
72 END ;
73 IF InBounds(mod2gcc, sym)
74 THEN
75 tr := tree(GetIndice(mod2gcc, sym)) ;
76 IF tr=PoisonedSymbol
77 THEN
78 n := GetSymName(sym) ;
79 (* not poisoned by the garbage collector, but by the gm2 front end *)
80 printf1('the gm2 front end poisoned this symbol (%a)\n', n) ;
81 InternalError ('attempting to use a gcc symbol which is no longer in scope')
82 END ;
83 RETURN( tr )
84 ELSE
85 RETURN( NIL )
86 END
87 END Mod2Gcc ;
88
89
90 (*
91 Gcc2Mod - given a gcc tree return the modula-2 symbol.
92 *)
93
94 PROCEDURE Gcc2Mod (tree: tree) : CARDINAL ;
95 VAR
96 high, i: CARDINAL ;
97 BEGIN
98 i := 1 ;
99 high := HighIndice (mod2gcc) ;
100 WHILE i <= high DO
101 IF GetIndice (mod2gcc, i) = tree
102 THEN
103 RETURN i
104 END ;
105 INC (i)
106 END ;
107 RETURN NulSym
108 END Gcc2Mod ;
109
110
111 (*
112 AddModGcc - adds the tuple [ sym, gcc ] into the database.
113 *)
114
115 PROCEDURE AddModGcc (sym: CARDINAL; gcc: tree) ;
116 VAR
117 old: tree ;
118 t : PtrToCardinal ;
119 BEGIN
120 IF gcc=GetErrorNode()
121 THEN
122 InternalError ('error node generated during symbol conversion')
123 END ;
124
125 IF USEPOISON
126 THEN
127 t := PtrToCardinal(gcc) ;
128 IF (gcc#tree(NIL)) AND (t^=GGCPOISON)
129 THEN
130 InternalError ('gcc symbol has been poisoned')
131 END
132 END ;
133
134 old := Mod2Gcc(sym) ;
135 IF old=tree(NIL)
136 THEN
137 (* absent - add it *)
138 PutIndice(mod2gcc, sym, gcc) ;
139 IF GetIndice(mod2gcc, sym)#gcc
140 THEN
141 InternalError ('failed to add gcc <-> mod2 symbol')
142 END ;
143 gcc := RememberConstant(gcc)
144 ELSIF old=gcc
145 THEN
146 (* do nothing, as it is already stored *)
147 ELSIF old=GetErrorNode()
148 THEN
149 InternalError ('replacing a temporary symbol (currently unexpected)')
150 ELSE
151 InternalError ('should not be replacing a symbol')
152 END ;
153
154 IF IsConst(sym) AND (NOT IsValueSolved(sym))
155 THEN
156 PushTypeOfTree(sym, gcc) ;
157 PopValue(sym)
158 END
159 END AddModGcc ;
160
161
162 (*
163 RemoveMod2Gcc - removes the gcc symbol from the lookup table.
164 *)
165
166 PROCEDURE RemoveMod2Gcc (sym: CARDINAL) ;
167 BEGIN
168 PutIndice(mod2gcc, sym, NIL)
169 END RemoveMod2Gcc ;
170
171
172 (*
173 GccKnowsAbout - returns TRUE if gcc knows about the symbol, sym.
174 *)
175
176 PROCEDURE GccKnowsAbout (sym: CARDINAL) : BOOLEAN ;
177 BEGIN
178 RETURN( InBounds(mod2gcc, sym) AND (GetIndice(mod2gcc, sym)#NIL) )
179 END GccKnowsAbout ;
180
181
182 (*
183 AddTemporaryKnown - adds a temporary gcc symbol against the modula-2 sym.
184 *)
185
186 PROCEDURE AddTemporaryKnown (sym: CARDINAL) ;
187 BEGIN
188 (* we add the error node against symbol, sym. We expect it to be retacted later. *)
189 PutIndice (mod2gcc, sym, GetErrorNode ())
190 END AddTemporaryKnown ;
191
192
193 (*
194 RemoveTemporaryKnown - removes the temporary symbol.
195 *)
196
197 PROCEDURE RemoveTemporaryKnown (sym: CARDINAL) ;
198 BEGIN
199 IF Mod2Gcc(sym)=GetErrorNode()
200 THEN
201 PutIndice(mod2gcc, sym, NIL)
202 ELSE
203 InternalError ('attempting to remove a symbol which is not present in the tree')
204 END
205 END RemoveTemporaryKnown ;
206
207
208 (*
209 Mod2GccWithoutGCCPoison - given a modula-2 symbol, sym, return
210 the gcc equivalent, it does not check to see
211 whether the gcc symbol has been poisoned.
212 *)
213
214 PROCEDURE Mod2GccWithoutGCCPoison (sym: CARDINAL) : tree ;
215 VAR
216 n : Name ;
217 tr: tree ;
218 BEGIN
219 IF InBounds(mod2gcc, sym)
220 THEN
221 tr := tree(GetIndice(mod2gcc, sym)) ;
222 IF tr=PoisonedSymbol
223 THEN
224 n := GetSymName(sym) ;
225 (* not poisoned by the garbage collector, but by the gm2 front end. *)
226 printf1 ('the gm2 front end poisoned this symbol (%a)\n', n) ;
227 InternalError ('attempting to use a gcc symbol which is no longer in scope')
228 END ;
229 RETURN tr
230 ELSE
231 RETURN NIL
232 END
233 END Mod2GccWithoutGCCPoison ;
234
235
236 (*
237 Poison - poisons a symbol.
238 *)
239
240 PROCEDURE Poison (sym: WORD) ;
241 VAR
242 a: ADDRESS ;
243 BEGIN
244 IF NOT IsConst(sym)
245 THEN
246 a := Mod2GccWithoutGCCPoison(sym) ;
247 IF a#NIL
248 THEN
249 PutIndice(mod2gcc, sym, PoisonedSymbol)
250 END
251 END
252 END Poison ;
253
254
255 (*
256 Init - create both binary trees.
257 *)
258
259 PROCEDURE Init ;
260 BEGIN
261 mod2gcc := InitIndexTuned (1, 1024*1024 DIV 16, 16) ;
262 ALLOCATE (PoisonedSymbol, 1)
263 END Init ;
264
265
266 BEGIN
267 Init
268 END SymbolConversion.