File: STSymTable.st

package info (click to toggle)
gnu-smalltalk 2.1.8-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,484 kB
  • ctags: 8,274
  • sloc: ansic: 53,661; sh: 17,366; perl: 4,319; awk: 1,319; yacc: 1,023; makefile: 1,010; sed: 258; lex: 249
file content (349 lines) | stat: -rw-r--r-- 7,772 bytes parent folder | download
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
74
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"======================================================================
|
|   Smalltalk in Smalltalk compiler symbol table
|
|
 ======================================================================"


"======================================================================
|
| Copyright 1995, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
| Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
|
 ======================================================================"


Object subclass: #STLiteralsTable
       instanceVariableNames: 'map array'
       classVariableNames: 'UseUndeclared'
       poolDictionaries: ''
       category: 'System-Compiler'
! 

!STLiteralsTable class methodsFor: 'instance creation'!

new: aSize
    ^self new initialize: aSize
! !

!STLiteralsTable methodsFor: 'accessing'!

addLiteral: anObject
    "Answers the index of the given literal.  If the literal is already 
     present in the literals, returns the index of that one."

    ^map at: anObject ifAbsentPut: [ | newArray |
	"Grow the array when full"
	array size = map size ifTrue: [ 
	    (newArray := Array new: map size * 2)	
		replaceFrom: 1 to: map size with: array startingAt: 1.

	    array become: newArray
	].
	array at: map size + 1 put: anObject.
	map size
    ].
!

literals
    ^array
!

trim
    array become: (array copyFrom: 1 to: map size).
! !

!STLiteralsTable methodsFor: 'private'!

initialize: aSize
    map := Dictionary new: aSize.
    array := Array new: aSize.
! !



Object subclass: #STVariable
       instanceVariableNames: 'id scope canStore'
       classVariableNames: ''
       poolDictionaries: ''
       category: 'System-Compiler'
! 


!STVariable class methodsFor: 'instance creation'!

id: id scope: scope canStore: canStore
    ^self new
	id: id
	scope: scope
	canStore: canStore
! !

!STVariable methodsFor: 'accessing'!

canStore
    ^canStore
!

id
    ^id
!

id: anObject scope: scopeIndex canStore: aBoolean
    id := anObject.
    scope := scopeIndex.
    canStore := aBoolean
!

scope
    ^scope
! !



Object subclass: #STSymbolTable
       instanceVariableNames: 'variables tempCount litTable pools instVars environment scopes'
       classVariableNames: 'UseUndeclared'
       poolDictionaries: ''
       category: 'System-Compiler'
! 

!STSymbolTable class methodsFor: 'accessing'!

initialize
    UseUndeclared := 0
!

insideFilein
    ^UseUndeclared > 0
!

nowInsideFileIn
    UseUndeclared := UseUndeclared + 1
!

nowOutsideFileIn
    UseUndeclared := UseUndeclared - 1
!


!STSymbolTable class methodsFor: 'instance creation'!

new
    ^super new init
! !

!STSymbolTable methodsFor: 'declaring'!

addPool: poolDictionary
    pools addAll: poolDictionary withAllSuperspaces.
!

declareEnvironment: aBehavior

    | i |
    environment := aBehavior.

    i := 0.
    aBehavior allInstVarNames do: [ :iv |
	instVars at: iv asSymbol put: (i := i + 1).
    ].
    self declareGlobals
!

declareGlobals
    | behavior |
    behavior := environment.

    "Find a suitable Class object from the given behavior"
    behavior isMetaclass ifTrue: [ behavior := behavior instanceClass ].

    [ behavior isClass ] whileFalse: [
	behavior := behavior superclass.
	behavior isNil ifTrue: [ ^self ]
    ].

    behavior withAllSuperclassesDo: [ :class |
	self addPool: behavior environment.
	class classPool isEmpty ifFalse: [
	    pools add: class classPool
	]
    ].

    behavior withAllSuperclassesDo: [ :class || dicts |
	dicts := behavior sharedPoolDictionaries.
	dicts isNil ifFalse: [
	    dicts do: [ :sp | self addPool: sp ]
	]
    ].
!

declareTemporary: tempName canStore: canStore for: stCompiler
    | ok symbol |
    symbol := tempName asSymbol.
    ok := variables includesKey: symbol.
    variables at: symbol put: (STVariable
	id: tempCount
	scope: scopes size
	canStore: canStore
    ).

    ok ifFalse: [
	stCompiler compileWarning: 'duplicate variable name ', tempName
    ].
    tempCount := tempCount + 1.
    ^tempCount - 1
!

scopeEnter
    scopes add: tempCount.
    tempCount := 0.
!

scopeLeave
    "Answer whether we are in a `clean' scope (no return from method, no
     references to variable in an outer scope)."
    tempCount := scopes removeLast.
!

undeclareTemporary: tempName
    variables removeKey: tempName asSymbol ifAbsent: [ ].
!

addLiteral: aLiteral
    "Answers the index of the given literal.  If the literal is already 
     present in the litTable, returns the index of that one."

    ^litTable addLiteral: aLiteral
! !

!STSymbolTable methodsFor: 'accessing'!

canStore: aName
    variables at: aName asSymbol ifPresent: [ :var |
	^var canStore
    ].
    ^true
!

numTemps
    ^tempCount
!

isTemporary: aName
    ^variables includesKey: aName asSymbol
!

isReceiver: aName
    ^instVars includesKey: aName asSymbol
!

outerScopes: aName
    | value |
    value := variables at: aName asSymbol.
    ^scopes size - value scope.
!

invalidScopeResolution: stCompiler
    ^stCompiler compileError: 'invalid scope resolution'
!

bindingOf: namesArray for: stCompiler
    | assoc |
    assoc := self lookupPoolsFor: (namesArray at: 1) asSymbol.

    assoc isNil ifTrue: [ ^nil ].

    "Ok, proceed with the remaining names (if any)."
    namesArray from: 2 to: namesArray size keysAndValuesDo: [ :i :each |
	assoc := assoc value 
	    associationAt: each asSymbol
	    ifAbsent: [
		| symbol |
		i < namesArray size ifTrue: [ self invalidScopeResolution: stCompiler ].

		"Last item, add to Undeclared"
		self class insideFilein ifFalse: [ ^nil ].
		(each at: 1) isUppercase ifFalse: [ ^nil ].

		symbol := each asSymbol.
		^Undeclared associationAt: symbol ifAbsent: [
		    Undeclared add: symbol -> nil
		].
	    ].
    ].
    ^assoc
!

lookupPoolsFor: symbol
    pools do: [ :pool |
        | assoc |
        assoc := pool associationAt: symbol ifAbsent: [ nil ].
	assoc isNil ifFalse: [ ^assoc ].
    ].
    ^nil
!

lookupName: aName for: stCompiler
    "Answers a value for the name"
    | symbol value assoc index |
    index := aName indexOf: $. .
    symbol := index = 0
	ifTrue: [ aName asSymbol ]
	ifFalse: [ (aName copyFrom: 1 to: index - 1) asSymbol ].

    value := variables at: symbol ifAbsent: [ nil ].
    value isNil ifFalse: [ 
        index = 0 ifFalse: [ self invalidScopeResolution: stCompiler ].
        ^value id ].

    value := instVars at: symbol ifAbsent: [ nil ].
    value isNil ifFalse: [
        index = 0 ifFalse: [ self invalidScopeResolution: stCompiler ].
        ^value - 1 ].

    assoc := index = 0
	ifTrue: [ self lookupPoolsFor: symbol ]
	ifFalse: [ self bindingOf: (aName substrings: $.) for: stCompiler ].

    assoc isNil ifFalse: [ ^self addLiteral: assoc ].
    ^assoc
!

finish
    litTable trim
!

literals
    ^litTable literals
! !


!STSymbolTable methodsFor: 'private'!

init
    variables := Dictionary new: 5.
    litTable := STLiteralsTable new: 13.
    instVars := Dictionary new: 7.
    scopes := OrderedCollection new: 5.
    pools := IdentitySet new: 7.
    tempCount := 0.
! !

STSymbolTable initialize!