| 1 | from rpython.jit.metainterp.support import ptr2int |
| 2 | |
| 3 | class Boxes(object): |
| 4 | pass |
| 5 | |
| 6 | def get_real_model(): |
| 7 | class LoopModel(object): |
| 8 | from rpython.jit.metainterp.history import TreeLoop, JitCellToken |
| 9 | from rpython.jit.metainterp.history import ConstInt, ConstPtr, ConstFloat |
| 10 | from rpython.jit.metainterp.history import BasicFailDescr, BasicFinalDescr, TargetToken |
| 11 | from rpython.jit.metainterp.opencoder import Trace |
| 12 | |
| 13 | from rpython.jit.metainterp.history import get_const_ptr_for_string |
| 14 | from rpython.jit.metainterp.history import get_const_ptr_for_unicode |
| 15 | get_const_ptr_for_string = staticmethod(get_const_ptr_for_string) |
| 16 | get_const_ptr_for_unicode = staticmethod(get_const_ptr_for_unicode) |
| 17 | |
| 18 | @staticmethod |
| 19 | def convert_to_floatstorage(arg): |
| 20 | from rpython.jit.codewriter import longlong |
| 21 | return longlong.getfloatstorage(float(arg)) |
| 22 | |
| 23 | @staticmethod |
| 24 | def ptr_to_int(obj): |
| 25 | return ptr2int(obj) |
| 26 | |
| 27 | return LoopModel |
| 28 | |
| 29 | def get_mock_model(): |
| 30 | class MockLoopModel(object): |
| 31 | class TreeLoop(object): |
| 32 | def __init__(self, name): |
| 33 | self.name = name |
| 34 | |
| 35 | class JitCellToken(object): |
| 36 | I_am_a_descr = True |
| 37 | |
| 38 | class TargetToken(object): |
| 39 | def __init__(self, jct): |
| 40 | pass |
| 41 | |
| 42 | class BasicFailDescr(object): |
| 43 | I_am_a_descr = True |
| 44 | final_descr = False |
| 45 | |
| 46 | class BasicFinalDescr(object): |
| 47 | I_am_a_descr = True |
| 48 | final_descr = True |
| 49 | |
| 50 | class Box(object): |
| 51 | _counter = 0 |
| 52 | type = 'b' |
| 53 | |
| 54 | def __init__(self, value=0): |
| 55 | self.value = value |
| 56 | |
| 57 | def __repr__(self): |
| 58 | result = str(self) |
| 59 | result += '(%s)' % self.value |
| 60 | return result |
| 61 | |
| 62 | def __str__(self): |
| 63 | if not hasattr(self, '_str'): |
| 64 | self._str = '%s%d' % (self.type, Box._counter) |
| 65 | Box._counter += 1 |
| 66 | return self._str |
| 67 | |
| 68 | class BoxInt(Box): |
| 69 | type = 'i' |
| 70 | |
| 71 | class BoxFloat(Box): |
| 72 | type = 'f' |
| 73 | |
| 74 | class BoxRef(Box): |
| 75 | type = 'p' |
| 76 | |
| 77 | class BoxVector(Box): |
| 78 | type = 'V' |
| 79 | |
| 80 | class Const(object): |
| 81 | bytesize = 8 |
| 82 | signed = True |
| 83 | def __init__(self, value=None): |
| 84 | self.value = value |
| 85 | |
| 86 | def _get_str(self): |
| 87 | return str(self.value) |
| 88 | |
| 89 | def is_constant(self): |
| 90 | return True |
| 91 | |
| 92 | class ConstInt(Const): |
| 93 | datatype = 'i' |
| 94 | pass |
| 95 | |
| 96 | class ConstPtr(Const): |
| 97 | datatype = 'r' |
| 98 | pass |
| 99 | |
| 100 | class ConstFloat(Const): |
| 101 | datatype = 'f' |
| 102 | signed = False |
| 103 | pass |
| 104 | |
| 105 | @classmethod |
| 106 | def get_const_ptr_for_string(cls, s): |
| 107 | return cls.ConstPtr(s) |
| 108 | |
| 109 | @classmethod |
| 110 | def get_const_ptr_for_unicode(cls, s): |
| 111 | return cls.ConstPtr(s) |
| 112 | |
| 113 | @staticmethod |
| 114 | def convert_to_floatstorage(arg): |
| 115 | return float(arg) |
| 116 | |
| 117 | @staticmethod |
| 118 | def ptr_to_int(obj): |
| 119 | return id(obj) |
| 120 | |
| 121 | return MockLoopModel |
| 122 | |
| 123 | |
| 124 | def get_model(use_mock): |
| 125 | if use_mock: |
| 126 | model = get_mock_model() |
| 127 | else: |
| 128 | model = get_real_model() |
| 129 | |
| 130 | class ExtendedTreeLoop(model.TreeLoop): |
| 131 | |
| 132 | def as_json(self): |
| 133 | return { |
| 134 | 'comment': self.comment, |
| 135 | 'name': self.name, |
| 136 | 'operations': [op.as_json() for op in self.operations], |
| 137 | 'inputargs': self.inputargs, |
| 138 | 'last_offset': self.last_offset |
| 139 | } |
| 140 | |
| 141 | def getboxes(self): |
| 142 | def opboxes(operations): |
| 143 | for op in operations: |
| 144 | yield op.result |
| 145 | for box in op.getarglist(): |
| 146 | yield box |
| 147 | def allboxes(): |
| 148 | for box in self.inputargs: |
| 149 | yield box |
| 150 | for box in opboxes(self.operations): |
| 151 | yield box |
| 152 | |
| 153 | boxes = Boxes() |
| 154 | for box in allboxes(): |
| 155 | if isinstance(box, model.Box): |
| 156 | name = str(box) |
| 157 | setattr(boxes, name, box) |
| 158 | return boxes |
| 159 | |
| 160 | def setvalues(self, **kwds): |
| 161 | boxes = self.getboxes() |
| 162 | for name, value in kwds.iteritems(): |
| 163 | getattr(boxes, name).value = value |
| 164 | |
| 165 | model.ExtendedTreeLoop = ExtendedTreeLoop |
| 166 | return model |