Example #1
0
def test_random_class_knowledge(boxes_known_classes):
    cls = FakeClass()
    for box, known_class in boxes_known_classes:
        if known_class:
            box.set_forwarded(InstancePtrInfo(known_class=cls))
    optimizer = FakeOptimizer()

    refboxes = [
        box for (box, _) in boxes_known_classes
        if isinstance(box, InputArgRef)
    ]

    numb_state = NumberingState(1)
    numb_state.append_int(1)  # size of resume block
    liveboxes = [box for (box, _) in boxes_known_classes]

    serialize_optimizer_knowledge(optimizer, numb_state, liveboxes, {}, None)

    assert len(numb_state.create_numbering().code) == 4 + math.ceil(
        len(refboxes) / 6.0)

    dct = {box: cls for box, known_class in boxes_known_classes if known_class}
    after_optimizer = FakeOptimizer(cpu=FakeCPU(dct))
    deserialize_optimizer_knowledge(after_optimizer,
                                    FakeStorage(numb_state.create_numbering()),
                                    liveboxes, liveboxes)
    for box, known_class in boxes_known_classes:
        assert (box in after_optimizer.constant_classes) == known_class
Example #2
0
def test_known_classes():
    cls = FakeClass()
    box1 = InputArgRef()
    box1.set_forwarded(InstancePtrInfo(known_class=cls))
    box2 = InputArgRef()
    box3 = InputArgRef()
    optimizer = FakeOptimizer()

    numb_state = NumberingState(4)
    numb_state.append_int(1)  # size of resume block
    liveboxes = [InputArgInt(), box2, box1, box3]

    serialize_optimizer_knowledge(optimizer, numb_state, liveboxes, {}, None)

    assert unpack_numbering(
        numb_state.create_numbering()) == [1, 0b010000, 0, 0, 0]

    rbox1 = InputArgRef()
    rbox2 = InputArgRef()
    rbox3 = InputArgRef()
    after_optimizer = FakeOptimizer(cpu=FakeCPU({rbox1: cls}))
    deserialize_optimizer_knowledge(after_optimizer,
                                    FakeStorage(numb_state.create_numbering()),
                                    [InputArgInt(), rbox2, rbox1, rbox3],
                                    liveboxes)
    assert box1 in after_optimizer.constant_classes
    assert box2 not in after_optimizer.constant_classes
    assert box3 not in after_optimizer.constant_classes