def float_bytes_to_real(bytes, signed): # XXX Currently does not make use of the signed parameter if len(bytes) not in (4, 8): raise SchemeException( "floating-point-bytes->real: byte string must have length 2, 4, or 8") try: if objectmodel.we_are_translated(): val = rarithmetic.r_int64(0) for i, v in enumerate(bytes): val += rarithmetic.r_int64(ord(v)) << (i * 8) return values.W_Flonum(longlong2float.longlong2float(val)) else: # use unsigned to avoid rlib bug val = rarithmetic.r_uint64(0) for i, v in enumerate(bytes): val += rarithmetic.r_uint64(ord(v)) << (i * 8) return values.W_Flonum(pycket_longlong2float(val)) except OverflowError, e: # Uncomment the check below to run Pycket on the # interpreter with compiled (zo) files # (fasl makes a call that blows the longlong2float on rpython) # if val == 18442240474082181120L: # return values.W_Flonum.NEGINF raise SchemeException("RPython overflow : %s" % e)
def test_str_of_longlong(self): def f(i): return str(i) res = self.interpret(f, [r_int64(0)]) assert self.ll_to_string(res) == '0' res = self.interpret(f, [r_int64(413974738222117)]) assert self.ll_to_string(res) == '413974738222117'
def test_float_conversion_implicit(self): def f(ii): return 1.0 + ii res = self.interpret(f, [r_int64(100000000)]) assert type(res) is float assert res == 100000001. res = self.interpret(f, [r_int64(1234567890123456789)]) assert type(res) is float assert self.float_eq(res, 1.2345678901234568e+18)
def integer_bytes_to_integer(bstr, signed): # XXX Currently does not make use of the signed parameter bytes = bstr.as_bytes_list() if len(bytes) not in (4, 8): raise SchemeException( "floating-point-bytes->real: byte string must have length 2, 4, or 8" ) val = rarithmetic.r_int64(0) for i, v in enumerate(bytes): val += rarithmetic.r_int64(ord(v)) << (i * 8) return values.W_Flonum(longlong2float.longlong2float(val))
def __init__(self): self.check_frequency = -1 # NB. use of r_int64 to be extremely far on the safe side: # this is increasing by one after each loop or bridge is # compiled, and it must not overflow. If the backend implements # complete freeing in cpu.free_loop_and_bridges(), then it may # be possible to get arbitrary many of them just by waiting long # enough. But in this day and age, you'd still never have the # patience of waiting for a slowly-increasing 64-bit number to # overflow :-) # According to my estimates it's about 5e9 years given 1000 loops # per second self.current_generation = r_int64(1) self.next_check = r_int64(-1) self.alive_loops = {}
def rffi_example(): int_number = r_int32(-10) print c_abs(int_number) long_number = r_int64(-10) print c_labs(long_number) longlong_number = r_longlong(-10) print c_llabs(longlong_number)
def set_max_age(self, max_age, check_frequency=0): if max_age <= 0: self.next_check = r_int64(-1) else: self.max_age = max_age if check_frequency <= 0: check_frequency = int(math.sqrt(max_age)) self.check_frequency = check_frequency self.next_check = self.current_generation + 1
def time_now(self): """ Answer the UTC microseconds since the Smalltalk epoch. The value is derived from the Posix epoch with a constant offset corresponding to elapsed microseconds between the two epochs according to RFC 868 """ import time from rpython.rlib.rarithmetic import r_int64 secs_to_usecs = 1000 * 1000 return r_int64(time.time() * secs_to_usecs) + constants.SQUEAK_EPOCH_DELTA_MICROSECONDS
def test_hash(self): def f(x): return objectmodel.compute_hash(x) res = self.interpret(f, [123456789]) assert res == 123456789 res = self.interpret(f, [r_int64(123456789012345678)]) if sys.maxint == 2147483647: # check the way we compute such a hash so far assert res == -1506741426 + 9 * 28744523 else: assert res == 123456789012345678
def wrap_int(self, val): if isinstance(val, r_int64) and not is_valid_int(val): if val > 0 and val <= r_int64(constants.U_MAXINT): return self.wrap_positive_wordsize_int(intmask(val)) else: raise WrappingError elif isinstance(val, r_uint) or isinstance(val, r_uint32): return self.wrap_positive_wordsize_int(intmask(val)) elif not is_valid_int(val): raise WrappingError # we don't do tagging return model.W_SmallInteger(intmask(val))
def event_time_to_microseconds(interp, ev_time): """ The microsecond-based time primitives are relative to a roll-over (we use startup time). The millisecond-based ones are based on the Squeak-Epoch (1901). This function converts the former to the latter by: scaling up, adding image startup timestamp, and finally adding the Epoch constant. """ secs_to_usecs = 1000 * 1000 return r_int64(ev_time * 1000 + interp.startup_time * secs_to_usecs) + \ constants.SQUEAK_EPOCH_DELTA_MICROSECONDS
def unwrap_int64(self, space): if self.size() > constants.BYTES_PER_MACHINE_LONGLONG: raise error.UnwrappingError("Too large to convert bytes to word") elif (self.size() == constants.BYTES_PER_MACHINE_LONGLONG and ord(self.getchar(constants.BYTES_PER_MACHINE_LONGLONG - 1)) >= 0x80): # Sign-bit is set, this will overflow raise error.UnwrappingError("Too large to convert bytes to word") word = r_int64(0) for i in range(self.size()): try: word += r_int64(ord(self.getchar(i))) << 8 * i except OverflowError: # never raised after translation :( raise error.UnwrappingError( "Too large to convert bytes to word") if self.getclass(space).is_same_object(space.w_LargePositiveInteger): return word elif self.getclass(space).is_same_object(space.w_LargeNegativeInteger): return -word else: raise error.UnwrappingError
def unwrap_int64(self, space): if constants.IS_64BIT: ret = intmask(self.value) if self.is_positive(space): if ret < 0: raise error.UnwrappingError else: if ret > 0: raise error.UnwrappingError else: ret = r_int64(self.value) if self.is_positive(space): return ret else: return -ret
def test_isinstance_vs_int_types(self): class FakeSpace(object): def wrap(self, x): if x is None: return [None] if isinstance(x, str): return x if isinstance(x, r_int64): return int(x) return "XXX" wrap._annspecialcase_ = 'specialize:argtype(0)' space = FakeSpace() def wrap(x): return space.wrap(x) res = self.interpret(wrap, [r_int64(0)]) assert res == 0
class JitCellToken(AbstractDescr): """Used for rop.JUMP, giving the target of the jump. This is different from TreeLoop: the TreeLoop class contains the whole loop, including 'operations', and goes away after the loop was compiled; but the LoopDescr remains alive and points to the generated assembler. """ FORCE_BRIDGE_SEGMENTING = 1 # stored in retraced_count target_tokens = None failed_states = None retraced_count = 0 invalidated = False outermost_jitdriver_sd = None # and more data specified by the backend when the loop is compiled number = -1 generation = r_int64(0) # one purpose of LoopToken is to keep alive the CompiledLoopToken # returned by the backend. When the LoopToken goes away, the # CompiledLoopToken has its __del__ called, which frees the assembler # memory and the ResumeGuards. compiled_loop_token = None def __init__(self): # For memory management of assembled loops self._keepalive_jitcell_tokens = {} # set of other JitCellToken def record_jump_to(self, jitcell_token): assert isinstance(jitcell_token, JitCellToken) self._keepalive_jitcell_tokens[jitcell_token] = None def __repr__(self): return '<Loop %d, gen=%d>' % (self.number, self.generation) def repr_of_descr(self): return '<Loop%d>' % self.number def dump(self): self.compiled_loop_token.cpu.dump_loop_token(self) def get_retraced_count(self): return self.retraced_count >> 1 def set_retraced_count(self, value): self.retraced_count = (value << 1) | (self.retraced_count & 1)
def wrap_longlong(self, val): if not we_are_translated(): "Tests only" if isinstance(val, long) and not isinstance(val, r_int64): return self.wrap_long_untranslated(val) if not is_valid_int(val): if isinstance(val, r_ulonglong): return self.wrap_ulonglong(val) elif isinstance(val, r_int64): if val > 0: if constants.IS_64BIT: if not val <= r_longlonglong(constants.U_MAXINT): # on 64bit, U_MAXINT must be wrapped in an unsigned longlonglong return self.wrap_ulonglong(val) else: if not val <= r_int64(constants.U_MAXINT): return self.wrap_ulonglong(val) elif val < 0: return self.wrap_nlonglong(val) # handles the rest and raises if necessary return self.wrap_int(val)
def test_signal_at_utc_microseconds(): start = space.unwrap_int64(prim(UTC_MICROSECOND_CLOCK, [0])) future = start + r_int64(400 * 1000) sema = space.w_Semaphore.as_class_get_shadow(space).new() prim(SIGNAL_AT_UTC_MICROSECONDS, [space.w_nil, sema, future]) assert space.w_timerSemaphore() is sema
def unwrap_int64(self, space): return r_int64(self.value)
def decode_timeval_ns(t): return (r_int64(rffi.getintfield(t, 'c_tv_sec')) * 10**9 + r_int64(rffi.getintfield(t, 'c_tv_usec')) * 10**3)
assert annmodel.SomeInteger(knowntype=r_int64).contains(s_longlong) return annmodel.SomeFloat() def specialize_call(self, hop): [v_longlong] = hop.inputargs(lltype.SignedLongLong) hop.exception_cannot_occur() return hop.genop("convert_longlong_bytes_to_float", [v_longlong], resulttype=lltype.Float) # ____________________________________________________________ # For encoding integers inside nonstandard NaN bit patterns. # ff ff ff fe xx xx xx xx (signed 32-bit int) nan_high_word_int32 = -2 # -2 == (int)0xfffffffe nan_encoded_zero = r_int64(nan_high_word_int32 << 32) def encode_int32_into_longlong_nan(value): return (nan_encoded_zero + rffi.cast(rffi.LONGLONG, rffi.cast(rffi.UINT, value))) def decode_int32_from_longlong_nan(value): return rffi.cast(lltype.Signed, rffi.cast(rffi.INT, value)) def is_int32_from_longlong_nan(value): return intmask(value >> 32) == nan_high_word_int32
def compute_result_annotation(self, s_longlong): assert annmodel.SomeInteger(knowntype=r_int64).contains(s_longlong) return annmodel.SomeFloat() def specialize_call(self, hop): [v_longlong] = hop.inputargs(lltype.SignedLongLong) hop.exception_cannot_occur() return hop.genop("convert_longlong_bytes_to_float", [v_longlong], resulttype=lltype.Float) # ____________________________________________________________ # For encoding integers inside nonstandard NaN bit patterns. # ff ff ff fe xx xx xx xx (signed 32-bit int) nan_high_word_int32 = -2 # -2 == (int)0xfffffffe nan_encoded_zero = r_int64(nan_high_word_int32 << 32) def encode_int32_into_longlong_nan(value): return (nan_encoded_zero + rffi.cast(rffi.LONGLONG, rffi.cast(rffi.UINT, value))) def decode_int32_from_longlong_nan(value): return rffi.cast(lltype.Signed, rffi.cast(rffi.INT, value)) def is_int32_from_longlong_nan(value): return intmask(value >> 32) == nan_high_word_int32 CAN_ALWAYS_ENCODE_INT32 = (sys.maxint == 2147483647) def can_encode_int32(value): if CAN_ALWAYS_ENCODE_INT32:
def test_primitive_utc_microseconds_clock(): start = space.unwrap_int64(prim(UTC_MICROSECOND_CLOCK, [0])) time.sleep(0.3) stop = space.unwrap_int64(prim(UTC_MICROSECOND_CLOCK, [0])) assert start + r_int64(250 * 1000) <= stop
def test_downcast_int(self): def f(i): return int(i) res = self.interpret(f, [r_int64(0)]) assert res == 0
def g(n): if n > 0: return f(r_int64(0)) else: return f(0)
FORM_HEIGHT = 2 FORM_DEPTH = 3 # ___________________________________________________________________________ # Miscellaneous constants COMPILED_METHOD_FULL_FRAME_SIZE = 56 COMPILED_METHOD_SMALL_FRAME_SIZE = 16 LITERAL_START = 1 # index of the first literal after the method header BYTES_PER_WORD = 4 WORDS_IN_FLOAT = 2 # Fixed number of word-slots in a Squeak Float object INTERP_PROXY_MAJOR = 1 INTERP_PROXY_MINOR = 13 # The Delta between Squeak Epoch (Jan 1st 1901) and POSIX Epoch (Jan 1st 1970) SQUEAK_EPOCH_DELTA_MICROSECONDS = r_int64(2177452800000000L) # ___________________________________________________________________________ # Special objects indices SO_NIL = 0 SO_FALSE = 1 SO_TRUE = 2 SO_SCHEDULERASSOCIATIONPOINTER = 3 SO_BITMAP_CLASS = 4 SO_SMALLINTEGER_CLASS = 5 SO_STRING_CLASS = 6 SO_ARRAY_CLASS = 7 SO_SMALLTALK = 8 # Deprecated SO_FLOAT_CLASS = 9 SO_METHODCONTEXT_CLASS = 10