import py
import sys, random
from rpython.rlib.rarithmetic import r_uint, intmask
from rpython.jit.metainterp.executor import execute, wrap_constant
from rpython.jit.metainterp.executor import execute_varargs, _execute_arglist
from rpython.jit.metainterp.resoperation import rop, opname, opclasses,\
     InputArgInt, InputArgFloat, InputArgRef
from rpython.jit.metainterp.history import ConstInt, ConstPtr, ConstFloat
from rpython.jit.metainterp.history import AbstractDescr
from rpython.jit.metainterp import history
from rpython.jit.codewriter import longlong
from rpython.jit.backend.model import AbstractCPU
from rpython.rtyper.lltypesystem import  llmemory, rffi

class FakeDescr(AbstractDescr):
    pass

class FakeCallDescr(FakeDescr):
    def get_result_type(self):
        return history.FLOAT

class FakeFieldDescr(FakeDescr):
    def is_pointer_field(self):
        return False
    def is_float_field(self):
        return True

class FakeArrayDescr(FakeDescr):
    def is_array_of_pointers(self):
        return False
    def is_array_of_floats(self):
        return True

class FakeResultR:
    _TYPE = llmemory.GCREF
    def __init__(self, *args):
        self.fakeargs = args

class FakeMetaInterp:
    pass

class FakeCPU(AbstractCPU):
    supports_floats = True

    def bh_new(self, descr):
        return FakeResultR('new', descr)

    def bh_arraylen_gc(self, array, descr):
        assert not array
        assert isinstance(descr, FakeDescr)
        return 55

    def bh_setfield_gc_f(self, struct, newvalue, fielddescr):
        self.fakesetfield = (struct, newvalue, fielddescr)

    def bh_setarrayitem_gc_f(self, array, index, newvalue, arraydescr):
        self.fakesetarrayitem = (array, index, newvalue, arraydescr)

    def bh_call_f(self, func, args_i, args_r, args_f, calldescr):
        self.fakecalled = (func, args_i, args_r, args_f, calldescr)
        return longlong.getfloatstorage(42.5)

    def bh_strsetitem(self, string, index, newvalue):
        self.fakestrsetitem = (string, index, newvalue)


boxfloat = InputArgFloat.fromfloat
constfloat = ConstFloat.fromfloat


def test_execute():
    cpu = FakeCPU()
    descr = FakeDescr()
    box = execute(cpu, None, rop.INT_ADD, None, InputArgInt(40), ConstInt(2))
    assert box == 42
    box = execute(cpu, None, rop.NEW, descr)
    assert box.fakeargs == ('new', descr)

def test_execute_varargs():
    cpu = FakeCPU()
    descr = FakeCallDescr()
    argboxes = [InputArgInt(99999), InputArgInt(321), constfloat(2.25), ConstInt(123),
                InputArgRef(), boxfloat(5.5)]
    box = execute_varargs(cpu, FakeMetaInterp(), rop.CALL_F, argboxes, descr)
    assert longlong.getrealfloat(box) == 42.5
    assert cpu.fakecalled == (99999, [321, 123],
                              [ConstPtr.value],
                              [longlong.getfloatstorage(2.25),
                               longlong.getfloatstorage(5.5)],
                              descr)

def test_execute_nonspec():
    cpu = FakeCPU()
    descr = FakeDescr()
    # cases with a descr
    # arity == -1
    argboxes = [InputArgInt(321), ConstInt(123)]
    box = _execute_arglist(cpu, FakeMetaInterp(), rop.CALL_F,
                           argboxes, FakeCallDescr())
    assert longlong.getrealfloat(box) == 42.5
    # arity == 0
    box = _execute_arglist(cpu, None, rop.NEW, [], descr)
    assert box.fakeargs == ('new', descr)
    # arity == 1
    box1 = InputArgRef()
    box = _execute_arglist(cpu, None, rop.ARRAYLEN_GC, [box1], descr)
    assert box == 55
    # arity == 2
    box2 = boxfloat(222.2)
    fielddescr = FakeFieldDescr()
    _execute_arglist(cpu, None, rop.SETFIELD_GC, [box1, box2], fielddescr)
    assert cpu.fakesetfield == (box1.getref_base(), box2.getfloatstorage(),
                                fielddescr)
    # arity == 3
    box3 = InputArgInt(33)
    arraydescr = FakeArrayDescr()
    _execute_arglist(cpu, None, rop.SETARRAYITEM_GC, [box1, box3, box2],
                    arraydescr)
    assert cpu.fakesetarrayitem == (box1.getref_base(), box3.getint(),
                                    box2.getfloatstorage(), arraydescr)
    # cases without descr
    # arity == 1
    box = _execute_arglist(cpu, None, rop.INT_INVERT, [box3])
    assert box == ~33
    # arity == 2
    box = _execute_arglist(cpu, None, rop.INT_LSHIFT, [box3, InputArgInt(3)])
    assert box == 33 << 3
    # arity == 3
    _execute_arglist(cpu, None, rop.STRSETITEM, [box1, InputArgInt(3), box3])
    assert cpu.fakestrsetitem == (box1.getref_base(), 3, box3.getint())

# ints

def _int_binary_operations():
    minint = -sys.maxint-1
    # Test cases.  Note that for each operation there should be at least
    # one case in which the two input arguments are equal.
    for opnum, testcases in [
        (rop.INT_ADD, [(10, -2, 8),
                       (-60, -60, -120)]),
        (rop.INT_SUB, [(10, -2, 12),
                       (133, 133, 0)]),
        (rop.INT_MUL, [(-6, -3, 18),
                       (15, 15, 225)]),
        (rop.INT_AND, [(0xFF00, 0x0FF0, 0x0F00),
                       (-111, -111, -111)]),
        (rop.INT_OR, [(0xFF00, 0x0FF0, 0xFFF0),
                      (-111, -111, -111)]),
        (rop.INT_XOR, [(0xFF00, 0x0FF0, 0xF0F0),
                       (-111, -111, 0)]),
        (rop.INT_LSHIFT, [(10, 4, 10<<4),
                          (-5, 2, -20),
                          (-5, 0, -5),
                          (3, 3, 24)]),
        (rop.INT_RSHIFT, [(-17, 2, -5),
                          (19, 1, 9),
                          (3, 3, 0)]),