Source

lang-smalltalk / spyvm / squeakimage.py

  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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import py
import os
import sys
from spyvm import constants
from spyvm import model
from spyvm.tool.bitmanipulation import splitter

from rpython.rlib import objectmodel

def chrs2int(b):
    assert len(b) == 4
    first = ord(b[0]) # big endian
    if first & 0x80 != 0:
        first = first - 0x100
    return (first << 24 | ord(b[1]) << 16 | ord(b[2]) << 8 | ord(b[3]))

def swapped_chrs2int(b):
    assert len(b) == 4
    first = ord(b[3]) # little endian
    if first & 0x80 != 0:
        first = first - 0x100
    return (first << 24 | ord(b[2]) << 16 | ord(b[1]) << 8 | ord(b[0]))

def chrs2long(b):
    assert len(b) == 8
    first = ord(b[0]) # big endian
    if first & 0x80 != 0:
        first = first - 0x100
    return (      first << 56 | ord(b[1]) << 48 | ord(b[2]) << 40 | ord(b[3]) << 32
            | ord(b[4]) << 24 | ord(b[5]) << 16 | ord(b[6]) <<  8 | ord(b[7])      )

def swapped_chrs2long(b):
    assert len(b) == 8
    first = ord(b[7]) # little endian
    if first & 0x80 != 0:
        first = first - 0x100
    return (      first << 56 | ord(b[6]) << 48 | ord(b[5]) << 40 | ord(b[4]) << 32
            | ord(b[3]) << 24 | ord(b[2]) << 16 | ord(b[1]) <<  8 | ord(b[0])      )


# ____________________________________________________________
#
# Reads an image file and creates all model objects

class Stream(object):
    """ Simple input stream """
    def __init__(self, inputfile=None, data=None):
        if inputfile is None and data is None:
            raise RuntimeError("need to supply either inputfile or data")

        if inputfile:
            try:
                self.data = inputfile.read()
            finally:
                inputfile.close()
        else:
            self.data = data
        self.reset()

    def peek(self):
        if self.pos >= len(self.data):
            raise IndexError
        data_peek = self.data[self.pos:self.pos + self.word_size]
        if self.use_long_read:
            if self.swap:
                return swapped_chrs2long(data_peek)
            else:
                return chrs2long(data_peek)
        else:
            if self.swap:
                return swapped_chrs2int(data_peek)
            else:
                return chrs2int(data_peek)
    

    def next(self):
        integer = self.peek()
        self.pos += self.word_size
        self.count += self.word_size
        return integer

    def reset(self):
        self.swap = False
        self.pos = 0
        self.count = 0
        self.be_32bit()

    def reset_count(self):
        self.count = 0

    def skipbytes(self, jump):
        assert jump > 0
        assert (self.pos + jump) <= len(self.data)
        self.pos += jump
        self.count += jump

    def skipwords(self, jump):
        self.skipbytes(jump * self.word_size)
        assert (self.pos + jump) <= len(self.data)
        self.pos += jump
        self.count += jump


    def length(self):
        return len(self.data)

    def close(self):
        pass # already closed

    def be_64bit(self):
        self.word_size = 8
        self.use_long_read = True

    def be_32bit(self):
        self.word_size = 4
        self.use_long_read = False


class CorruptImageError(Exception):
    pass

class UnsupportedImageError(Exception):
    pass

# ____________________________________________________________

class ImageVersion(object):

    def __init__(self, magic, is_big_endian, is_64bit, has_closures, has_floats_reversed):
        self.magic = magic
        self.is_big_endian = is_big_endian
        self.is_64bit = is_64bit
        self.has_closures = has_closures
        self.has_floats_reversed = has_floats_reversed

image_versions = {
    0x00001966:         ImageVersion(6502,  True,  False, False, False),
    0x66190000:         ImageVersion(6502,  False, False, False, False),
    0x00001968:         ImageVersion(6504,  True,  False, True,  False),
    0x68190000:         ImageVersion(6504,  False, False, True,  False),
    0x00001969:         ImageVersion(6505,  True,  False, True,  True ),
    0x69190000:         ImageVersion(6505,  False, False, True,  True ),
    0x00000000000109A0: ImageVersion(68000, True,  True,  False, False),
}

if sys.maxint == 2 ** 63 - 1:
    image_versions.update({
   -0x5ff6ff0000000000:
    # signed version of 0xA009010000000000: 
                        ImageVersion(68000, False, True,  False, False),
    0x00000000000109A2: ImageVersion(68002, True,  True,  True,  False),
   -0x5df6ff0000000000:
    # signed version of 0xA209010000000000: 
			ImageVersion(68002, False, True,  True,  False),
    0x00000000000109A3: ImageVersion(68003, True,  True,  True,  True ),
   -0x5cf6ff0000000000:
    # signed version of 0xA309010000000000: 
			ImageVersion(68003, False, True,  True,  True ),
})

    
def version(magic):
    ver = image_versions.get(magic, None)
    if ver is None:
        raise CorruptImageError
    # if ver.is_64bit or ver.has_floats_reversed:
    #     raise UnsupportedImageError
    return ver

possible_image_offset = 512

def version_from_stream(stream):
    # 32 bit
    try:
        return version(stream.peek())
    except CorruptImageError as e:
        if stream.length() > possible_image_offset + 4:
            stream.skipbytes(possible_image_offset)
            try:
                return version(stream.peek())
            except CorruptImageError:
                pass # raise original error
        # 64 bit
        stream.reset()
        stream.be_64bit()
        try:
            v = version(stream.peek())
            assert v.is_64bit
            return v
        except CorruptImageError as e:
            if stream.length() > possible_image_offset + 4:
                stream.skipbytes(possible_image_offset)
                try:
                    v = version(stream.peek())
                    assert v.is_64bit
                    return v
                except CorruptImageError:
                    pass # raise original error
        raise

    
    
def reader_for_image(space, stream):
    ver = version_from_stream(stream)
    if not ver.is_big_endian:
        stream.swap = True
    return ImageReader(space, stream, ver)

class ImageReader(object):
    
    def __init__(self, space, stream, version):
        self.space = space
        self.stream = stream
        self.version = version
        # dictionary mapping old address to chunk object
        self.chunks = {}
        self.chunklist = []
        # cache wrapper integers
        self.intcache = {}

    def initialize(self):
        # XXX should be called something like read_full_image
        self.read_header()
        self.read_body()
        self.init_compactclassesarray()
        # until here, the chunks are generated
        self.init_g_objects()
        self.init_w_objects()
        self.fillin_w_objects()
        self.synchronize_shadows()

    def read_version(self):
        # 1 word version
        magic = self.stream.next()
        assert self.version.magic == magic

    def read_header(self):
        self.read_version()
        #------
        # 1 word headersize
        headersize = self.stream.next()
        # 1 word size of the full image
        self.endofmemory = self.stream.next() # endofmemory = bodysize
        # 1 word old base address
        self.oldbaseaddress = self.stream.next()
        # 1 word pointer to special objects array
        self.specialobjectspointer = self.stream.next()
        # 1 word last used hash
        lasthash = self.stream.next()
        savedwindowssize = self.stream.next()
        print "savedwindowssize", savedwindowssize
        fullscreenflag = self.stream.next()
        extravmmemory = self.stream.next()
        self.stream.skipbytes(headersize - self.stream.pos)

    def read_body(self):
        import sys
        self.stream.reset_count()
        while self.stream.count < self.endofmemory:
            chunk, pos = self.read_object()
            if len(self.chunklist) % 1000 == 0: os.write(2,'#')
            self.chunklist.append(chunk)
            self.chunks[pos + self.oldbaseaddress] = chunk
        self.stream.close()
        self.swap = self.stream.swap #save for later
        self.stream = None
        return self.chunklist # return for testing

    def init_g_objects(self):
        for chunk in self.chunks.itervalues():
            chunk.as_g_object(self) # initialized g_object

    def init_w_objects(self):
        self.assign_prebuilt_constants()
        for chunk in self.chunks.itervalues():
            chunk.g_object.init_w_object()

    def assign_prebuilt_constants(self):
        # assign w_objects for objects that are already in objtable
        for name, so_index in constants.objects_in_special_object_table.items():
            w_object = self.space.objtable["w_" + name]
            if self.special_object(so_index).w_object is None:
                self.special_object(so_index).w_object = w_object
            else:
                if self.special_object(0).w_object is not self.space.w_nil:
                   raise Warning('Object found in multiple places in the special objects array')
        # assign w_objects for objects that are already in classtable
        for name, so_index in constants.classes_in_special_object_table.items():
            w_object = self.space.classtable["w_" + name]
            if self.special_object(so_index).w_object is None:
                self.special_object(so_index).w_object = w_object
            else:
                if self.special_object(0).w_object is not self.space.w_nil:
                   raise Warning('Object found in multiple places in the special objects array')

    def special_object(self, index):
        special = self.chunks[self.specialobjectspointer].g_object.pointers
        return special[index]

    def fillin_w_objects(self):
        for chunk in self.chunks.itervalues():
            chunk.g_object.w_object.fillin(self.space, chunk.g_object)

    def synchronize_shadows(self):
        for chunk in self.chunks.itervalues():
            casted = chunk.g_object.w_object
            if isinstance(casted, model.W_PointersObject) and casted.has_shadow():
                casted._shadow.update()

    def init_compactclassesarray(self):
        """ from the blue book (CompiledMethod Symbol Array PseudoContext LargePositiveInteger nil MethodDictionary Association Point Rectangle nil TranslatedMethod BlockContext MethodContext nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil ) """
        special = self.chunks[self.specialobjectspointer]
        assert special.size > 24 #at least
        assert special.format == 2
        # squeak-specific: compact classes array
        chunk = self.chunks[special.data[COMPACT_CLASSES_ARRAY]]
        assert len(chunk.data) == 31
        assert chunk.format == 2
        self.compactclasses = [self.chunks[pointer] for pointer in chunk.data]

    def read_object(self):
        kind = self.stream.peek() & 3 # 2 bits
        if kind == 0: # 00 bits
            chunk, pos = self.read_3wordobjectheader()
        elif kind == 1: # 01 bits
            chunk, pos = self.read_2wordobjectheader()
        elif kind == 3: # 11 bits
            chunk, pos = self.read_1wordobjectheader()
        else: # 10 bits
            raise CorruptImageError("Unused block not allowed in image")
        size = chunk.size
        chunk.data = [self.stream.next()
                     for _ in range(size - 1)] #size-1, excluding header
        return chunk, pos

    def read_1wordobjectheader(self):
        kind, size, format, classid, idhash = (
            splitter[2,6,4,5,12](self.stream.next()))
        assert kind == 3
        return ImageChunk(self.space, size, format, classid, idhash), self.stream.count - 4

    def read_2wordobjectheader(self):
        assert self.stream.peek() & 3 == 1 #kind
        classid = self.stream.next() - 01 # remove headertype to get pointer
        kind, size, format, _, idhash = splitter[2,6,4,5,12](self.stream.next())
        assert kind == 1
        return ImageChunk(self.space, size, format, classid, idhash), self.stream.count - 4

    def read_3wordobjectheader(self):
        kind, size = splitter[2,30](self.stream.next())
        assert kind == 0
        assert splitter[2](self.stream.peek())[0] == 0 #kind
        classid = self.stream.next() - 00 # remove headertype to get pointer
        kind, _, format, _, idhash = splitter[2,6,4,5,12](self.stream.next())
        assert kind == 0
        return ImageChunk(self.space, size, format, classid, idhash), self.stream.count - 4


# ____________________________________________________________

class SqueakImage(object):

    def from_reader(self, space, reader):
        from spyvm import constants
        self.special_objects = [g_object.w_object for g_object in
                                reader.chunks[reader.specialobjectspointer]
                                .g_object.pointers]

        for name, idx in constants.objects_in_special_object_table.items():
            space.objtable["w_" + name] = self.special_objects[idx]

        self.w_asSymbol = self.find_symbol(space, reader, "asSymbol")
        self.w_simulateCopyBits = self.find_symbol(space, reader, "simulateCopyBits")

    def find_symbol(self, space, reader, symbol):
        w_dnu = self.special(constants.SO_DOES_NOT_UNDERSTAND)
        assert isinstance(w_dnu, model.W_BytesObject)
        assert w_dnu.as_string() == "doesNotUnderstand:"
        w_Symbol = w_dnu.getclass(space)
        w_obj = None
        # bit annoying that we have to hunt through the image :-(
        for chunk in reader.chunklist:
            w_obj = chunk.g_object.w_object
            if not isinstance(w_obj, model.W_BytesObject):
                continue
            if not w_obj.getclass(space).is_same_object(w_Symbol):
                continue
            if w_obj.as_string() == symbol:
                break
        assert w_obj is not None
        return w_obj

    def special(self, index):
        return self.special_objects[index]

# from the squeak source code:
# in squeak, the compact classes array can be found at this position
# in the special objects array
COMPACT_CLASSES_ARRAY = 28

# ____________________________________________________________

class GenericObject(object):
    """ Intermediate representation of squeak objects. To establish all
        pointers as object references, ImageReader creates instances of
        GenericObject from the image chunks, and uses them as starting
        point for the actual create of spyvm.model classes.
        """

    def __init__(self, space):
        self.space = space
        self.reader = None

    def isinitialized(self):
        return self.reader is not None

    def initialize_int(self, value, reader):
        self.reader = reader
        self.value = value
        self.size = -1
        if value in reader.intcache:
            w_int = reader.intcache[value]
        else:
            w_int = self.space.wrap_int(value)
            reader.intcache[value] = w_int
        self.w_object = w_int

    def initialize(self, chunk, reader):
        self.reader = reader
        self.size = chunk.size
        self.hash12 = chunk.hash12
        self.format = chunk.format
        self.chunk = chunk # for bytes, words and compiledmethod
        self.init_class()
        self.init_data() # for pointers
        self.w_object = None

    def init_class(self):
        if self.chunk.iscompact():
            self.g_class = self.reader.compactclasses[self.chunk.classid
                - 1].g_object # Smalltalk is 1-based indexed
        else:
            self.g_class = self.reader.chunks[self.chunk.classid].g_object

    def init_data(self):
        if self.ispointers():
            self.pointers = self.decode_pointers()
            assert None not in self.pointers
        elif self.iscompiledmethod():
            header = self.chunk.data[0] >> 1 # untag tagged int
            _, literalsize, _, _, _ = constants.decode_compiled_method_header(header)
            self.pointers = self.decode_pointers(literalsize + 1) # adjust +1 for the header

    def decode_pointers(self, end=-1):
        if end == -1:
            end = len(self.chunk.data)
        pointers = []
        for i in range(end):
            pointer = self.chunk.data[i]
            if (pointer & 1) == 1:
                small_int = GenericObject(self.space)
                small_int.initialize_int(pointer >> 1, self.reader)
                pointers.append(small_int)
            else:
                pointers.append(self.reader.chunks[pointer].g_object)
        return pointers

    def isbytes(self):
        return 8 <= self.format <= 11

    def is32bitlargepositiveinteger(self):
        return (self.format == 8 and
                self.space.w_LargePositiveInteger.is_same_object(self.g_class.w_object) and
                len(self.get_bytes()) <= 4)

    def iswords(self):
        return self.format == 6

    def isfloat(self):
        return self.iswords() and self.space.w_Float.is_same_object(self.g_class.w_object)

    def ispointers(self):
        return self.format < 5 #TODO, what about compiled methods?

    def iscompiledmethod(self):
        return 12 <= self.format <= 15

    def init_w_object(self):
        """ 0      no fields
            1      fixed fields only (all containing pointers)
            2      indexable fields only (all containing pointers)
            3      both fixed and indexable fields (all containing pointers)
            4      both fixed and indexable weak fields (all containing pointers).

            5      unused
            6      indexable word fields only (no pointers)
            7      indexable long (64-bit) fields (only in 64-bit images)

         8-11      indexable byte fields only (no pointers) (low 2 bits are low 2 bits of size)
        12-15     compiled methods:
                       # of literal oops specified in method header,
                       followed by indexable bytes (same interpretation of low 2 bits as above)
        """
        if self.w_object is None:
            # the instantiate call circumvents the constructors
            # and makes empty objects
            if self.ispointers():
                # XXX self.format == 4 is weak
                self.w_object = objectmodel.instantiate(model.W_PointersObject)
            elif self.format == 5:
                raise CorruptImageError("Unknown format 5")
            elif self.isfloat():
                self.w_object = objectmodel.instantiate(model.W_Float)
            elif self.is32bitlargepositiveinteger():
                self.w_object = objectmodel.instantiate(model.W_LargePositiveInteger1Word)
            elif self.iswords():
                self.w_object = objectmodel.instantiate(model.W_WordsObject)
            elif self.format == 7:
                raise CorruptImageError("Unknown format 7, no 64-bit support yet :-)")
            elif self.isbytes():
                self.w_object = objectmodel.instantiate(model.W_BytesObject)
            elif self.iscompiledmethod():
                self.w_object = objectmodel.instantiate(model.W_CompiledMethod)
            else:
                assert 0, "not reachable"
        return self.w_object

    def get_bytes(self):
        bytes = []
        if self.reader.swap:
            for each in self.chunk.data:
                bytes.append(chr((each >> 0) & 0xff))
                bytes.append(chr((each >> 8) & 0xff))
                bytes.append(chr((each >> 16) & 0xff))
                bytes.append(chr((each >> 24) & 0xff))
        else:
            for each in self.chunk.data:
                bytes.append(chr((each >> 24) & 0xff))
                bytes.append(chr((each >> 16) & 0xff))
                bytes.append(chr((each >> 8) & 0xff))
                bytes.append(chr((each >> 0) & 0xff))
        stop = len(bytes) - (self.format & 3)
        assert stop >= 0
        return bytes[:stop] # omit odd bytes

    def get_ruints(self, required_len=-1):
        from rpython.rlib.rarithmetic import r_uint
        words = [r_uint(x) for x in self.chunk.data]
        if required_len != -1 and len(words) != required_len:
            raise CorruptImageError("Expected %d words, got %d" % (required_len, len(words)))
        return words

    def get_pointers(self):
        assert self.pointers is not None
        return [g_object.w_object for g_object in self.pointers]

    def get_class(self):
        w_class = self.g_class.w_object
        assert isinstance(w_class, model.W_PointersObject)
        return w_class

    def get_hash(self):
        return self.chunk.hash12


class ImageChunk(object):
    """ A chunk knows the information from the header, but the body of the
    object is not decoded yet."""
    def __init__(self, space, size, format, classid, hash12):
        self.size = size
        self.format = format
        self.classid = classid
        self.hash12 = hash12
        # list of integers forming the body of the object
        self.data = None
        self.g_object = GenericObject(space)

    def __eq__(self, other):
        "(for testing)"
        return (self.__class__ is other.__class__ and
                self.format == other.format and
                self.classid == other.classid and
                self.hash12 == other.hash12 and
                self.data == other.data)

    def __ne__(self, other):
        "(for testing)"
        return not self == other

    def as_g_object(self, reader):
        if not self.g_object.isinitialized():
            self.g_object.initialize(self, reader)
        return self.g_object

    def iscompact(self):
        return 0 < self.classid < 32