Exemplo n.º 1
0
 def wrapped(interp, argument_count_m1):
     argument_count = argument_count_m1 + 1 # to account for the rcvr
     frame = interp.w_active_context
     assert argument_count == len_unwrap_spec
     if len(frame.stack) < len_unwrap_spec:
         raise PrimitiveFailedError()
     args = ()
     for i, spec in unrolling_unwrap_spec:
         index = -len_unwrap_spec + i
         w_arg = frame.stack[index]
         if spec is int:
             args += (utility.unwrap_int(w_arg), )
         elif spec is index1_0:
             args += (utility.unwrap_int(w_arg)-1, )
         elif spec is float:
             args += (utility.unwrap_float(w_arg), )
         elif spec is object:
             args += (w_arg, )
         elif spec is str:
             args += (w_arg.as_string(), )
         elif spec is char:
             args += (unwrap_char(w_arg), )
         else:
             raise NotImplementedError(
                 "unknown unwrap_spec %s" % (spec, ))
     w_result = func(interp, *args)
     frame.pop_n(len_unwrap_spec)   # only if no exception occurs!
     if not no_result:
         assert w_result is not None
         interp.w_active_context.push(w_result)
Exemplo n.º 2
0
 def wrapped(interp, argument_count_m1):
     argument_count = argument_count_m1 + 1  # to account for the rcvr
     frame = interp.w_active_context
     assert argument_count == len_unwrap_spec
     if len(frame.stack) < len_unwrap_spec:
         raise PrimitiveFailedError()
     args = ()
     for i, spec in unrolling_unwrap_spec:
         index = -len_unwrap_spec + i
         w_arg = frame.stack[index]
         if spec is int:
             args += (utility.unwrap_int(w_arg), )
         elif spec is index1_0:
             args += (utility.unwrap_int(w_arg) - 1, )
         elif spec is float:
             args += (utility.unwrap_float(w_arg), )
         elif spec is object:
             args += (w_arg, )
         elif spec is str:
             args += (w_arg.as_string(), )
         elif spec is char:
             args += (unwrap_char(w_arg), )
         else:
             raise NotImplementedError("unknown unwrap_spec %s" %
                                       (spec, ))
     w_result = func(interp, *args)
     frame.pop_n(len_unwrap_spec)  # only if no exception occurs!
     if not no_result:
         assert w_result is not None
         interp.w_active_context.push(w_result)
Exemplo n.º 3
0
def entry_point(argv):
    if len(argv) > 1:
        n = int(argv[1])
    else:
        n = 8
    interp.w_active_context.push(w_object)
    interp.w_active_context.push(wrap_int(n))
    result = interp.interpret()
    print unwrap_int(result)
    return 0
Exemplo n.º 4
0
 def literalatput0(self, index0, w_value):
     if index0 == 0:
         from pypy.lang.smalltalk import utility
         header = utility.unwrap_int(w_value)
         self.setheader(header)
     else:
         self.literals[index0 - 1] = w_value
Exemplo n.º 5
0
 def store(self, index, value):
     # THIS IS ALL UNTESTED CODE and we're a bit unhappy about it
     # because it crashd the translation N+4 times :-(
     from pypy.lang.smalltalk import utility
     if index == constants.BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX:
         self.argcnt = utility.unwrap_int(value)
     elif index == constants.BLKCTX_INITIAL_IP_INDEX:
         self.pc = utility.unwrap_int(value)
     elif index == constants.BLKCTX_HOME_INDEX:
         assert isinstance(value, W_MethodContext)
         self.w_home = value
     elif index >= constants.BLKCTX_TEMP_FRAME_START:
         stack_index = len(self.stack) - index - 1
         self.stack[stack_index] = value
     else:
         W_ContextPart.store(self, index, value)
Exemplo n.º 6
0
 def literalatput0(self, index0, w_value):
     if index0 == 0:
         from pypy.lang.smalltalk import utility
         header = utility.unwrap_int(w_value)
         self.setheader(header)
     else:
         self.literals[index0-1] = w_value
Exemplo n.º 7
0
def check_me():
    interp, w_object = build()
    interp.w_active_context.push(w_object)
    interp.w_active_context.push(wrap_int(8))
    result = interp.interpret()
    assert unwrap_int(result) == 34
    print "check_me() ok"
Exemplo n.º 8
0
 def store(self, index, value):
     # THIS IS ALL UNTESTED CODE and we're a bit unhappy about it
     # because it crashd the translation N+4 times :-(
     from pypy.lang.smalltalk import utility
     if index == constants.BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX:
         self.argcnt = utility.unwrap_int(value)
     elif index == constants.BLKCTX_INITIAL_IP_INDEX:
         self.pc = utility.unwrap_int(value)
     elif index == constants.BLKCTX_HOME_INDEX:
         assert isinstance(value, W_MethodContext)
         self.w_home = value
     elif index >= constants.BLKCTX_TEMP_FRAME_START:
         stack_index = len(self.stack) - index - 1
         self.stack[stack_index] = value
     else:
         W_ContextPart.store(self, index, value)
Exemplo n.º 9
0
 def atput0(self, index0, w_value):
     from pypy.lang.smalltalk import utility
     if index0 < self.getliteralsize():
         self.literalatput0(index0, w_value)
     else:
         # XXX use to-be-written unwrap_char
         index0 = index0 - self.getliteralsize()
         self.setchar(index0, chr(utility.unwrap_int(w_value)))
Exemplo n.º 10
0
 def atput0(self, index0, w_value):
     from pypy.lang.smalltalk import utility
     if index0 < self.getliteralsize():
         self.literalatput0(index0, w_value)
     else:
         # XXX use to-be-written unwrap_char
         index0 = index0 - self.getliteralsize()
         self.setchar(index0, chr(utility.unwrap_int(w_value)))
Exemplo n.º 11
0
 def test():
     interp = new_interpreter(sendLiteralSelectorBytecode(1 + 16))
     interp.w_active_context.w_method().literals = fakeliterals("foo", "sub")
     interp.w_active_context.push(wrap_int(50))
     interp.w_active_context.push(wrap_int(8))
     callerContext = interp.w_active_context
     interp.step()
     assert interp.w_active_context is callerContext
     assert len(interp.w_active_context.stack) == 1
     w_result = interp.w_active_context.pop()
     assert unwrap_int(w_result) == 42
Exemplo n.º 12
0
 def test():
     interp = new_interpreter(sendLiteralSelectorBytecode(1 + 16))
     interp.w_active_context.w_method().literals = fakeliterals(
         "foo", "sub")
     interp.w_active_context.push(wrap_int(50))
     interp.w_active_context.push(wrap_int(8))
     callerContext = interp.w_active_context
     interp.step()
     assert interp.w_active_context is callerContext
     assert len(interp.w_active_context.stack) == 1
     w_result = interp.w_active_context.pop()
     assert unwrap_int(w_result) == 42
Exemplo n.º 13
0
def test_fibWithArgument():
    bytecode = ''.join(map(chr, [ 16, 119, 178, 154, 118, 164, 11, 112, 16, 118, 177, 224, 112, 16, 119, 177, 224, 176, 124 ]))
    shadow = mockclass(0).as_class_get_shadow()
    method = model.W_CompiledMethod(len(bytecode))
    method.literalsize = 1
    method.bytes = bytecode
    method.argsize = 1
    method.literals = fakeliterals("fib:")
    shadow.installmethod("fib:", method)
    w_object = shadow.new()
    interp = new_interpreter(sendLiteralSelectorBytecode(16) + returnTopFromMethod)
    interp.w_active_context.w_method().literals = fakeliterals("fib:")
    interp.w_active_context.push(w_object)
    interp.w_active_context.push(wrap_int(8))
    result = interp.interpret()
    assert unwrap_int(result) == 34
Exemplo n.º 14
0
def test_fibWithArgument():
    bytecode = ''.join(
        map(chr, [
            16, 119, 178, 154, 118, 164, 11, 112, 16, 118, 177, 224, 112, 16,
            119, 177, 224, 176, 124
        ]))
    shadow = mockclass(0).as_class_get_shadow()
    method = model.W_CompiledMethod(len(bytecode))
    method.literalsize = 1
    method.bytes = bytecode
    method.argsize = 1
    method.literals = fakeliterals("fib:")
    shadow.installmethod("fib:", method)
    w_object = shadow.new()
    interp = new_interpreter(
        sendLiteralSelectorBytecode(16) + returnTopFromMethod)
    interp.w_active_context.w_method().literals = fakeliterals("fib:")
    interp.w_active_context.push(w_object)
    interp.w_active_context.push(wrap_int(8))
    result = interp.interpret()
    assert unwrap_int(result) == 34
Exemplo n.º 15
0
    def update_shadow(self):
        "Update the ClassShadow with data from the w_self class."
        from pypy.lang.smalltalk import objtable

        w_self = self.w_self
        # read and painfully decode the format
        classformat = utility.unwrap_int(
            w_self.fetch(constants.CLASS_FORMAT_INDEX))
        # The classformat in Squeak, as an integer value, is:
        #    <2 bits=instSize//64><5 bits=cClass><4 bits=instSpec>
        #                                    <6 bits=instSize\\64><1 bit=0>
        # In Slang the value is read directly as a boxed integer, so that
        # the code gets a "pointer" whose bits are set as above, but
        # shifted one bit to the left and with the lowest bit set to 1.

        # compute the instance size (really the size, not the number of bytes)
        instsize_lo = (classformat >> 1) & 0x3F
        instsize_hi = (classformat >> (9 + 1)) & 0xC0
        self.instance_size = (instsize_lo | instsize_hi) - 1  # subtract hdr
        # decode the instSpec
        format = (classformat >> 7) & 15
        self.instance_varsized = format >= 2
        if format < 4:
            self.instance_kind = POINTERS
        elif format == 4:
            self.instance_kind = WEAK_POINTERS
        elif format == 6:
            self.instance_kind = WORDS
            if self.instance_size != 0:
                raise ClassShadowError("can't have both words and a non-zero "
                                       "base instance size")
        elif 8 <= format <= 11:
            self.instance_kind = BYTES
            if self.instance_size != 0:
                raise ClassShadowError("can't have both bytes and a non-zero "
                                       "base instance size")
        elif 12 <= format <= 15:
            self.instance_kind = COMPILED_METHOD
        else:
            raise ClassShadowError("unknown format %d" % (format,))
        # read the name
        if w_self.size() > constants.CLASS_NAME_INDEX:
            w_name = w_self.fetch(constants.CLASS_NAME_INDEX)
            if isinstance(w_name, model.W_BytesObject):
                self.name = w_name.as_string()
        # read the methoddict
        w_methoddict = w_self.fetch(constants.CLASS_METHODDICT_INDEX)
        w_values = w_methoddict.fetch(constants.METHODDICT_VALUES_INDEX)
        size = w_methoddict.size() - constants.METHODDICT_NAMES_INDEX
        for i in range(size):
            w_selector = w_methoddict.fetch(constants.METHODDICT_NAMES_INDEX+i)
            if w_selector is not objtable.w_nil:
                if not isinstance(w_selector, model.W_BytesObject):
                    raise ClassShadowError("bogus selector in method dict")
                selector = w_selector.as_string()
                w_compiledmethod = w_values.fetch(i)
                if not isinstance(w_compiledmethod, model.W_CompiledMethod):
                    raise ClassShadowError("the methoddict must contain "
                                           "CompiledMethods only for now")
                self.methoddict[selector] = w_compiledmethod
        # for the rest, we need to reset invalid to False already so
        # that cycles in the superclass and/or metaclass chains don't
        # cause infinite recursion
        self.invalid = False
        # read s_superclass
        w_superclass = w_self.fetch(constants.CLASS_SUPERCLASS_INDEX)
        if w_superclass is objtable.w_nil:
            self.s_superclass = None
        else:
            self.s_superclass = w_superclass.as_class_get_shadow()
Exemplo n.º 16
0
    def update_shadow(self):
        "Update the ClassShadow with data from the w_self class."
        from pypy.lang.smalltalk import objtable

        w_self = self.w_self
        # read and painfully decode the format
        classformat = utility.unwrap_int(
            w_self.fetch(constants.CLASS_FORMAT_INDEX))
        # The classformat in Squeak, as an integer value, is:
        #    <2 bits=instSize//64><5 bits=cClass><4 bits=instSpec>
        #                                    <6 bits=instSize\\64><1 bit=0>
        # In Slang the value is read directly as a boxed integer, so that
        # the code gets a "pointer" whose bits are set as above, but
        # shifted one bit to the left and with the lowest bit set to 1.

        # compute the instance size (really the size, not the number of bytes)
        instsize_lo = (classformat >> 1) & 0x3F
        instsize_hi = (classformat >> (9 + 1)) & 0xC0
        self.instance_size = (instsize_lo | instsize_hi) - 1  # subtract hdr
        # decode the instSpec
        format = (classformat >> 7) & 15
        self.instance_varsized = format >= 2
        if format < 4:
            self.instance_kind = POINTERS
        elif format == 4:
            self.instance_kind = WEAK_POINTERS
        elif format == 6:
            self.instance_kind = WORDS
            if self.instance_size != 0:
                raise ClassShadowError("can't have both words and a non-zero "
                                       "base instance size")
        elif 8 <= format <= 11:
            self.instance_kind = BYTES
            if self.instance_size != 0:
                raise ClassShadowError("can't have both bytes and a non-zero "
                                       "base instance size")
        elif 12 <= format <= 15:
            self.instance_kind = COMPILED_METHOD
        else:
            raise ClassShadowError("unknown format %d" % (format, ))
        # read the name
        if w_self.size() > constants.CLASS_NAME_INDEX:
            w_name = w_self.fetch(constants.CLASS_NAME_INDEX)
            if isinstance(w_name, model.W_BytesObject):
                self.name = w_name.as_string()
        # read the methoddict
        w_methoddict = w_self.fetch(constants.CLASS_METHODDICT_INDEX)
        w_values = w_methoddict.fetch(constants.METHODDICT_VALUES_INDEX)
        size = w_methoddict.size() - constants.METHODDICT_NAMES_INDEX
        for i in range(size):
            w_selector = w_methoddict.fetch(constants.METHODDICT_NAMES_INDEX +
                                            i)
            if w_selector is not objtable.w_nil:
                if not isinstance(w_selector, model.W_BytesObject):
                    raise ClassShadowError("bogus selector in method dict")
                selector = w_selector.as_string()
                w_compiledmethod = w_values.fetch(i)
                if not isinstance(w_compiledmethod, model.W_CompiledMethod):
                    raise ClassShadowError("the methoddict must contain "
                                           "CompiledMethods only for now")
                self.methoddict[selector] = w_compiledmethod
        # for the rest, we need to reset invalid to False already so
        # that cycles in the superclass and/or metaclass chains don't
        # cause infinite recursion
        self.invalid = False
        # read s_superclass
        w_superclass = w_self.fetch(constants.CLASS_SUPERCLASS_INDEX)
        if w_superclass is objtable.w_nil:
            self.s_superclass = None
        else:
            self.s_superclass = w_superclass.as_class_get_shadow()
Exemplo n.º 17
0
 def atput0(self, index0, w_value):
     from pypy.lang.smalltalk import utility
     self.setword(index0, utility.unwrap_int(w_value))
Exemplo n.º 18
0
 def atput0(self, index0, w_value):
     from pypy.lang.smalltalk import utility
     self.setword(index0, utility.unwrap_int(w_value))