-
Notifications
You must be signed in to change notification settings - Fork 64
/
Gnuifier.class.st
208 lines (174 loc) · 6.83 KB
/
Gnuifier.class.st
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
"
My instances automate the translation of a Squeak interpreter for use with GCC. In the specified FileDirectory, I copy 'interp.c' to 'interp.c.old'; translate a gnuified interpreter back into 'interp.c'; and save a working copy of sqGnu.h.
To gnuify an interpreter, try something like one of the following:
(Gnuifier on:
((FileDirectory default
directoryNamed: 'src')
directoryNamed: 'vm') pathName) gnuify
(Gnuifier on:
'powercow X:Users:werdna:Desktop:squeak:Squeak3.2a-4599 Folder:src:vm') gnuify
Structure:
directory FileDirectory -- specifying where I should do my gnuification
I can attempt to undo my damage with #deGnuify.
"
Class {
#name : #Gnuifier,
#superclass : #Object,
#instVars : [
'directory',
'interpreterFilename',
'shouldCleanupOldFile'
],
#category : #'Slang-PostProcessing'
}
{ #category : #'as yet unclassified' }
Gnuifier class >> on: aFilePathStringOrDirectory [
^self new setDirectory: aFilePathStringOrDirectory asFileReference
]
{ #category : #'as yet unclassified' }
Gnuifier >> copyFrom: inFileStream to: outFileStream [
"convert interp.c to use GNU features"
| inData |
Cursor read showWhile:
[inData := inFileStream upToEnd withSqueakLineEndings].
Cursor write showWhile:
[outFileStream nextPutAll: inData].
outFileStream close
]
{ #category : #'as yet unclassified' }
Gnuifier >> doCleanupOldFile [
self doCleanupOldFile: true
]
{ #category : #'as yet unclassified' }
Gnuifier >> doCleanupOldFile: aBoolean [
shouldCleanupOldFile := aBoolean
]
{ #category : #'as yet unclassified' }
Gnuifier >> gnuify [
"This Gnuifier produces a file compatible with gcc 3.x and gcc 4.x.
We label the gnuified VM so one can identify the valid gcc version.
The script run on Wndows, platforms/win32/misc/gnuify, produces
something compatible with gcc 2.95 (gack)."
(directory asFileReference / ('gcc3x-', interpreterFilename))
ensureDelete.
self
gnuifyFrom: (directory asFileReference / interpreterFilename)
to: (VMMaker forceNewFileNamed: directory / ('gcc3x-', interpreterFilename)).
self shouldCleanupOldFile
ifTrue: [ (directory / interpreterFilename) ensureDelete ]
]
{ #category : #'as yet unclassified' }
Gnuifier >> gnuifyFrom: inputFile to: outFileStream [
"convert interp.c to use GNU features"
| inData beforeInterpret inInterpret inInterpretVars beforePrimitiveResponse inPrimitiveResponse |
inData := inputFile contents withInternalLineEndings.
"print a header"
outFileStream
nextPutAll: '/* This file has been post-processed for GNU C */';
cr;
cr;
cr.
beforeInterpret := true. "whether we are before the beginning of interpret()"
inInterpret := false. "whether we are in the middle of interpret"
inInterpretVars := false. "whether we are in the variables of interpret"
beforePrimitiveResponse := true. "whether we are before the beginning of primitiveResponse()"
inPrimitiveResponse := false. "whether we are inside of primitiveResponse"
UIManager default
displayProgress: 'Gnuifying'
from: 1
to: (inData occurrencesOf: Character cr)
during: [ :bar |
| lineNumber |
lineNumber := 0.
inData linesDo: [ :inLine |
| outLine extraOutLine |
bar value: (lineNumber := lineNumber + 1).
outLine := inLine. "print out one line for each input line; by default, print out the line that was input, but some rules modify it"
extraOutLine := nil. "occasionally print a second output line..."
beforeInterpret
ifTrue: [
inLine = '#include "sq.h"' ifTrue: [
outLine := '#include "sqGnu.h"' ].
(inLine beginsWith: 'interpret(void)') ifTrue: [ "reached the beginning of interpret"
beforeInterpret := false.
inInterpret := true.
inInterpretVars := true ] ]
ifFalse: [
inInterpretVars
ifTrue: [
(inLine findString: 'register struct foo * foo = &fum;') > 0
ifTrue: [
outLine := 'register struct foo * foo FOO_REG = &fum;' ].
(inLine findString: ' local_instructionPointer;') > 0 ifTrue: [
outLine := ' register char* local_instructionPointer IP_REG;' ].
(inLine findString: ' local_framePointer;') > 0 ifTrue: [
outLine := ' register char* local_framePointer FP_REG;' ].
(inLine findString: ' local_stackPointer;') > 0 ifTrue: [
outLine := ' register char* local_stackPointer SP_REG;' ].
(inLine findString: ' currentBytecode;') > 0 ifTrue: [
outLine := ' register sqInt currentBytecode CB_REG;' ].
inLine isEmpty ifTrue: [ "reached end of variables"
inInterpretVars := false.
outLine := ' JUMP_TABLE;'.
extraOutLine := '\#if MULTIPLEBYTECODESETS\ if(!asserta((sizeof(jumpTable)/sizeof(jumpTable[0])) >= 512))\ error("bytecode jumpTable too small");\#endif\'
withCRs ] ]
ifFalse: [
inInterpret
ifTrue: [ "working inside interpret(); translate the switch statement"
(inLine beginsWith: ' case ') ifTrue: [
| tokens |
tokens := inLine findTokens: ' :'.
outLine := ' CASE(' , tokens second , ')'.
tokens size > 2 ifTrue: [
(tokens allButFirst: 2) do: [ :token |
outLine := outLine , ' ' , token ] ] ].
inLine = ' break;' ifTrue: [ outLine := ' BREAK;' ].
inLine = '}' ifTrue: [ "all finished with interpret()"
inInterpret := false ] ]
ifFalse: [
beforePrimitiveResponse
ifTrue: [
(inLine beginsWith: 'primitiveResponse(') ifTrue: [ "into primitiveResponse we go"
beforePrimitiveResponse := false.
inPrimitiveResponse := true.
extraOutLine := ' PRIM_TABLE;' ] ]
ifFalse: [
inPrimitiveResponse ifTrue: [
1halt.
inLine = ' switch (primitiveIndex) {' ifTrue: [
extraOutLine := outLine.
outLine := ' PRIM_DISPATCH;' ].
inLine = ' switch (GIV(primitiveIndex)) {' ifTrue: [
extraOutLine := outLine.
outLine := ' PRIM_DISPATCH;' ].
(inLine beginsWith: ' case ') ifTrue: [
| caseLabel |
caseLabel := (inLine findTokens: ' :') second.
outLine := ' CASE(' , caseLabel , ')' ].
inLine = '}' ifTrue: [ inPrimitiveResponse := false ] ] ] ] ] ].
outFileStream
nextPutAll: outLine;
cr.
extraOutLine ifNotNil: [
outFileStream
nextPutAll: extraOutLine;
cr ] ] ].
outFileStream close
]
{ #category : #'as yet unclassified' }
Gnuifier >> initialize [
super initialize.
shouldCleanupOldFile := false
]
{ #category : #'as yet unclassified' }
Gnuifier >> interpreterFilename: aString [
interpreterFilename := aString
]
{ #category : #'as yet unclassified' }
Gnuifier >> setDirectory: aFileDirectory [
directory := aFileDirectory
]
{ #category : #'as yet unclassified' }
Gnuifier >> shouldCleanupOldFile [
^ shouldCleanupOldFile
]