-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctcode.m
494 lines (434 loc) · 11.7 KB
/
ctcode.m
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
const Template <- class Template
var s : String <- ""
var t : String <- ""
var finalString : String
var lastBrand : Character <- nil
var lastCount : Integer <- 0
var cs : CString
operation flush
if lastBrand !== nil and lastCount != 0 then
if lastCount < 0 then
s <- s || "%*" || lastBrand.asString
elseif lastCount = 1 then
s <- s || "%" || lastBrand.asString
else
s <- s || "%" || lastCount.asString || lastBrand.asString
end if
end if
lastCount <- 0
end flush
export operation isVector [whatBrand : Character]
self.flush
lastBrand <- whatBrand
lastCount <- ~1
t <- t || "data\0"
end isVector
export operation addOne [whatBrand : Character, name : String]
if whatBrand != 'Z' then
if lastBrand !== nil and lastBrand != whatBrand then self.flush end if
lastBrand <- whatBrand
lastCount <- lastCount + 1
end if
t <- t || name || "\0"
end addOne
export operation addLineNumbers[lineNumbers : String]
self.flush
t <- t || lineNumbers
end addLineNumbers
export function fetchIndex -> [r : Integer]
if cs == nil then
r <- nil
else
r <- cs.fetchIndex
end if
end fetchIndex
operation finalize
if finalString == nil then
const env : EnvironmentType <- Environment$env
const needDebugInfo <- env$generateDebugInfo and t != ""
self.flush
if lastBrand !== nil then
if needDebugInfo then
finalString <- s || "\0" || t
else
finalString <- s || "\0"
end if
elseif needDebugInfo then
finalString <- "\0" || t
end if
end if
end finalize
export operation getIndex [start : Integer, q : CPQueue]
if cs == nil then
self.finalize
if finalString !== nil then
cs <- CString.create
cs$s <- finalString
cs.getIndex[start, q]
end if
end if
end getIndex
export operation cpoint [file : OutStream]
if cs !== nil then
cs.cpoint[file]
end if
end cpoint
export operation getString -> [r : String]
self.finalize
r <- finalString
end getString
end Template
export Template
const aoove <- Array.of[OpVectorE]
const OPVectorCT <- immutable object OPVectorCT
const obj <- "-OB-"
const OPVectorCTOID <- 0x1819
const myind <- object x
field myind : Integer <- ~1
field myq : CPQueue
end x
export operation fetchIndex -> [r : Integer]
r <- myind$myind
end fetchIndex
export operation getIndex [start : Integer, q : CPQueue]
if myind$myq !== q then
myind$myq <- q
q.AddUpper[self]
myind$myind <- q.upperbound
end if
end getIndex
export operation CPoint [file : OutStream]
file.putString[obj]
OIDWriter.Out[OPVectorCTOID, file]
end CPoint
end OPVectorCT
const OpVector <- class OpVector
const obj <- "+OB-"
field d : aoove <- aoove.create[~10]
var myind : Integer <- ~1
export function asString -> [s : String]
s <- "opvector"
end asString
export operation addUpper[e : OpVectorE]
d.addUpper[e]
end addUpper
export operation setElement[i : Integer, e : OpVectorE]
d[i] <- e
end setElement
export function getElement[i : Integer] -> [r : OpVectorE]
r <- d[i]
end getElement
export function upperbound -> [r : Integer]
r <- d.upperbound
end upperbound
export operation getIndex [start : Integer, q : CPQueue]
if myind < 0 then
q.addUpper[self]
myind <- q.upperbound
OPVectorCT.getIndex[start, q]
for i : Integer <- 0 while i <= d.upperbound by i <- i + 1
begin
const dd <- d[i]
if dd !== nil then
dd.getIndex[start, q]
end if
end
end for
end if
end getIndex
export function fetchIndex -> [r : Integer]
r <- myind
end fetchIndex
export operation CPoint [file : OutStream]
var x : Integer
file.putString[obj]
file.writeint[4 + 4 + (d.upperbound - d.lowerbound + 1) * 4, 4]
file.writeInt[0x00000000 + OPVectorCT.fetchIndex, 4]
x <- d.upperbound + 1
file.writeInt[x, 4]
for i : Integer <- 0 while i <= d.upperbound by i <- i + 1
begin
const dd <- d[i]
if dd == nil then
x <- 0x80000000
else
x <- dd.fetchIndex
end if
end
file.writeInt[x, 4]
end for
end CPoint
export operation generateBuiltin -> [r : COpVector]
const voove <- Vector.of[COpVectorE]
var i : Integer <- 0
const limit : Integer <- d.upperbound
const myv <- voove.create[limit + 1]
var dd : OpVectorE
loop
exit when i > limit
dd <- d[i]
if dd !== nil then
myv[i] <- dd.generateBuiltin
end if
i <- i + 1
end loop
r <- COpVector.literal[myv]
end generateBuiltin
initially
d.addUpper[nil] % initially
d.addUpper[nil] % process
d.addUpper[nil] % recovery
end initially
end OpVector
export OpVector
const OPVectorECT <- immutable object OPVectorECT
const obj <- "-OB-"
const OPVectorECTOID <- 0x181a
const myind <- object x
field myind : Integer <- ~1
field myq : CPQueue
end x
export operation fetchIndex -> [r : Integer]
r <- myind$myind
end fetchIndex
export operation getIndex [start : Integer, q : CPQueue]
if myind$myq !== q then
myind$myq <- q
q.AddUpper[self]
myind$myind <- q.upperbound
end if
end getIndex
export operation CPoint [file : OutStream]
file.putString[obj]
OIDWriter.Out[OPVectorECTOID, file]
end CPoint
end OPVectorECT
const OpVectorE <- class OpVectorE [xxname : String]
const obj <- "+OB-"
field name : String <- xxname
field id : Integer
field nargs : Integer <- 0
field nress : Integer <- 0
field templ : Template
field code : String <- ""
const csname : CString <- CString.create
const cscode: CString <- CString.create
var myind : Integer <- ~1
export function asString -> [s : String]
s <- "opvectore"
end asString
export operation setOthers [x : aoa]
cscode$others <- x
end setOthers
export operation getIndex [start : Integer, q : CPQueue]
if myind < 0 then
q.addUpper[self]
myind <- q.upperbound
OPVectorECT.getIndex[start, q]
csname$s <- name
csname.getIndex[start, q]
if templ !== nil then
templ.getIndex[start, q]
end if
if code !== nil then
cscode$s <- code
cscode.getIndex[start, q]
end if
end if
end getIndex
export function fetchIndex -> [r : Integer]
r <- myind
end fetchIndex
export operation CPoint [file : OutStream]
var x : Integer
file.putString[obj]
file.writeInt[4 + 4 + 4 + 4 + 4 + 4 + 4, 4]
file.writeInt[0x00000000 + OPVectorECT.fetchIndex, 4]
file.writeInt[id, 4]
file.writeInt[nargs, 4]
file.writeInt[nress, 4]
x <- csname.fetchIndex
file.writeInt[x, 4]
if templ == nil then
x <- 0x80000000
else
x <- templ.fetchIndex
end if
file.writeInt[x, 4]
if code == nil then
x <- 0x80000000
else
x <- cscode.fetchIndex
end if
file.writeInt[x, 4]
end CPoint
export operation generateBuiltin -> [r : COpVectorE]
r <- COpVectorE.create[id, nargs, nress, name, templ.getString, code]
end generateBuiltin
end OpVectorE
export OpVectorE
const CTCodeCT <- immutable object CTCodeCT
const obj <- "-OB-"
const CTCodeCTOID <- 0x1818
const myind <- object x
field myind : Integer <- ~1
field myq : CPQueue
end x
export operation fetchIndex -> [r : Integer]
r <- myind$myind
end fetchIndex
export operation getIndex [start : Integer, q : CPQueue]
if myind$myq !== q then
myind$myq <- q
q.AddUpper[self]
myind$myind <- q.upperbound
end if
end getIndex
export operation CPoint [file : OutStream]
file.putString[obj]
OIDWriter.Out[CTCodeCTOID, file]
end CPoint
end CTCodeCT
const CreateOne <- class CreateOne [codeid : Integer, xxid : Integer]
const obj <- "*OB*"
const field id : Integer <- xxid
var myind : Integer <- ~1
export function fetchIndex -> [r : Integer]
r <- myind
end fetchIndex
export operation getIndex [start : Integer, q : CPQueue]
if myind < 0 then
q.AddUpper[self]
myind <- q.upperbound
end if
end getIndex
export operation CPoint [file : OutStream]
file.putString[obj]
OIDWriter.Out[codeid, file]
OIDWriter.Out[id, file]
end CPoint
end CreateOne
const CTCode <- class CTCode
const obj <- "+OB+"
var csname : CString <- CString.create
var csfilename : CString <- CString.create
field instanceSize : Integer
field instanceFlags : Integer
field opVec : OpVector <- OpVector.create
field name : String
field filename : String
field templ : Template
field mytype : CPAble
const field literals : aoip <- aoip.create[~8]
var cliterals : CInts
field id : Integer
var myind : Integer <- ~1
var creationThing : CreateOne
export function asString -> [s : String]
s <- "ct"
end asString
export operation getIndex [start : Integer, q : CPQueue]
if myind < 0 then
q.addUpper[self]
myind <- q.upperbound
CTCodeCT.getIndex[start, q]
opVec.getIndex[start, q]
if name !== nil then
csname$s <- name
csname.getIndex[start, q]
end if
if filename !== nil then
csfilename$s <- filename
csfilename.getIndex[start, q]
end if
if templ !== nil then
templ.getIndex[start, q]
end if
if mytype !== nil then
mytype.getIndex[start, q]
end if
if literals !== nil then
cliterals <- CInts.create[literals]
cliterals.getIndex[start, q]
end if
if name !== nil and name = "Compilation" then
creationThing <- CreateOne.create[id, 0]
creationThing.getIndex[start, q]
end if
end if
end getIndex
export function fetchIndex -> [r : Integer]
r <- myind
end fetchIndex
export operation CPoint [file : OutStream]
var x : Integer
file.putString[obj]
file.writeInt[16 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4, 4]
OIDWriter.Out[id, file]
file.writeInt[0x40000000 + CTCodeCT.fetchIndex, 4]
file.writeInt[instanceSize, 4]
file.writeInt[instanceFlags, 4]
x <- opVec.fetchIndex
file.writeInt[x, 4]
if name == nil then
x <- 0x80000000
else
x <- csname.fetchIndex
end if
file.writeInt[x, 4]
if filename == nil then
x <- 0x80000000
else
x <- csfilename.fetchIndex
end if
file.writeInt[x, 4]
if templ == nil then
x <- 0x80000000
else
x <- templ.fetchIndex
end if
file.writeInt[x, 4]
if mytype == nil then
x <- 0x80000000
else
x <- mytype.fetchIndex
end if
file.writeInt[x, 4]
% Write the literals
if cliterals == nil then
x <- 0x80000000
else
x <- cliterals.fetchIndex
end if
file.writeInt[x, 4]
end CPoint
export operation generateBuiltin -> [r : ConcreteType]
% Turn the literals into the real thing.
const lits <- VectorOfInt.create[(literals.upperbound + 1) * 4]
for i : Integer <- 0 while i <= literals.upperbound by i <- i + 1
lits[4 * i] <- 0 % The first two lines are the Node of the OID.
lits[4 * i + 1] <- (0).setBit[16, literals[i].second = 1]
lits[4 * i + 2] <- literals[i].first
% This is eventually overwritten with the pointer to the actual
% object when it is relocated.
lits[4 * i + 3] <- 0x80000000
end for
r <- ConcreteType.create[
instanceSize,
instanceFlags,
opVec.generateBuiltin,
name,
filename,
templ.getString,
nil,
LiteralList.literal[lits]]
primitive "INSTALLINOID" [] <- [id, r]
if mytype !== nil then
var typeid : Integer
typeid <- (view mytype as hasID)$id
primitive "RELOCATETYPE" [] <- [r, typeid]
end if
end generateBuiltin
end CTCode
export CTCode
export CreateOne