| 1 |
from rpython.jit.backend.detect_cpu import getcpuclass |
| 2 |
from rpython.jit.tool.oparser import parse |
| 3 |
from rpython.jit.metainterp.history import JitCellToken, NoStats |
| 4 |
from rpython.jit.metainterp.history import BasicFinalDescr, BasicFailDescr |
| 5 |
from rpython.jit.metainterp.gc import get_description |
| 6 |
from rpython.jit.metainterp.optimize import SpeculativeError |
| 7 |
from rpython.annotator.listdef import s_list_of_strings |
| 8 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 9 |
from rpython.rtyper.rclass import getclassrepr, getinstancerepr |
| 10 |
from rpython.translator.unsimplify import call_initial_function |
| 11 |
from rpython.translator.translator import TranslationContext |
| 12 |
from rpython.translator.c import genc |
| 13 |
|
| 14 |
|
| 15 |
def run_guards_translated(gcremovetypeptr): |
| 16 |
class A(object): |
| 17 |
pass |
| 18 |
class B(A): |
| 19 |
pass |
| 20 |
class C(B): |
| 21 |
pass |
| 22 |
def main(argv): |
| 23 |
A(); B().foo = len(argv); C() |
| 24 |
return 0 |
| 25 |
|
| 26 |
t = TranslationContext() |
| 27 |
t.config.translation.gc = "minimark" |
| 28 |
t.config.translation.gcremovetypeptr = gcremovetypeptr |
| 29 |
ann = t.buildannotator() |
| 30 |
ann.build_types(main, [s_list_of_strings], main_entry_point=True) |
| 31 |
rtyper = t.buildrtyper() |
| 32 |
rtyper.specialize() |
| 33 |
|
| 34 |
classdef = ann.bookkeeper.getuniqueclassdef(B) |
| 35 |
rclass = getclassrepr(rtyper, classdef) |
| 36 |
rinstance = getinstancerepr(rtyper, classdef) |
| 37 |
LLB = rinstance.lowleveltype.TO |
| 38 |
ptr_vtable_B = rclass.getvtable() |
| 39 |
adr_vtable_B = llmemory.cast_ptr_to_adr(ptr_vtable_B) |
| 40 |
vtable_B = llmemory.cast_adr_to_int(adr_vtable_B, mode="symbolic") |
| 41 |
|
| 42 |
CPU = getcpuclass() |
| 43 |
cpu = CPU(rtyper, NoStats(), |
| 44 |
translate_support_code=True, |
| 45 |
gcdescr=get_description(t.config)) |
| 46 |
execute_token = cpu.make_execute_token(llmemory.GCREF) |
| 47 |
finaldescr = BasicFinalDescr() |
| 48 |
faildescr = BasicFailDescr() |
| 49 |
|
| 50 |
descr_B = cpu.sizeof(LLB, ptr_vtable_B) |
| 51 |
typeid_B = descr_B.get_type_id() |
| 52 |
fielddescr_B = cpu.fielddescrof(LLB, 'inst_foo') |
| 53 |
|
| 54 |
LLD = lltype.GcStruct('D', ('dd', lltype.Signed)) |
| 55 |
descr_D = cpu.sizeof(LLD) |
| 56 |
fielddescr_D = cpu.fielddescrof(LLD, 'dd') |
| 57 |
|
| 58 |
ARRAY = lltype.GcArray(lltype.Signed) |
| 59 |
arraydescr = cpu.arraydescrof(ARRAY) |
| 60 |
|
| 61 |
loop1 = parse(""" |
| 62 |
[p0] |
| 63 |
guard_class(p0, ConstInt(vtable_B), descr=faildescr) [] |
| 64 |
finish(descr=finaldescr) |
| 65 |
""", namespace={'finaldescr': finaldescr, |
| 66 |
'faildescr': faildescr, |
| 67 |
'vtable_B': vtable_B}) |
| 68 |
|
| 69 |
loop1a = parse(""" |
| 70 |
[p0] |
| 71 |
guard_nonnull_class(p0, ConstInt(vtable_B), descr=faildescr) [] |
| 72 |
finish(descr=finaldescr) |
| 73 |
""", namespace={'finaldescr': finaldescr, |
| 74 |
'faildescr': faildescr, |
| 75 |
'vtable_B': vtable_B}) |
| 76 |
|
| 77 |
loop2 = parse(""" |
| 78 |
[p0] |
| 79 |
guard_gc_type(p0, ConstInt(typeid_B), descr=faildescr) [] |
| 80 |
finish(descr=finaldescr) |
| 81 |
""", namespace={'finaldescr': finaldescr, |
| 82 |
'faildescr': faildescr, |
| 83 |
'typeid_B': typeid_B}) |
| 84 |
|
| 85 |
loop3 = parse(""" |
| 86 |
[p0] |
| 87 |
guard_is_object(p0, descr=faildescr) [] |
| 88 |
finish(descr=finaldescr) |
| 89 |
""", namespace={'finaldescr': finaldescr, |
| 90 |
'faildescr': faildescr}) |
| 91 |
|
| 92 |
loop4 = parse(""" |
| 93 |
[p0] |
| 94 |
guard_subclass(p0, ConstInt(vtable_B), descr=faildescr) [] |
| 95 |
finish(descr=finaldescr) |
| 96 |
""", namespace={'finaldescr': finaldescr, |
| 97 |
'faildescr': faildescr, |
| 98 |
'vtable_B': vtable_B}) |
| 99 |
|
| 100 |
def g(): |
| 101 |
cpu.setup_once() |
| 102 |
token1 = JitCellToken() |
| 103 |
token1a = JitCellToken() |
| 104 |
token2 = JitCellToken() |
| 105 |
token3 = JitCellToken() |
| 106 |
token4 = JitCellToken() |
| 107 |
cpu.compile_loop(loop1.inputargs, loop1.operations, token1) |
| 108 |
cpu.compile_loop(loop1a.inputargs, loop1a.operations, token1a) |
| 109 |
cpu.compile_loop(loop2.inputargs, loop2.operations, token2) |
| 110 |
cpu.compile_loop(loop3.inputargs, loop3.operations, token3) |
| 111 |
cpu.compile_loop(loop4.inputargs, loop4.operations, token4) |
| 112 |
|
| 113 |
for token, p0 in [ |
| 114 |
(token1, rffi.cast(llmemory.GCREF, A())), |
| 115 |
(token1, rffi.cast(llmemory.GCREF, B())), |
| 116 |
(token1, rffi.cast(llmemory.GCREF, C())), |
| 117 |
|
| 118 |
(token1a, rffi.cast(llmemory.GCREF, A())), |
| 119 |
(token1a, lltype.nullptr(llmemory.GCREF.TO)), |
| 120 |
(token1a, rffi.cast(llmemory.GCREF, B())), |
| 121 |
(token1a, rffi.cast(llmemory.GCREF, C())), |
| 122 |
|
| 123 |
(token2, rffi.cast(llmemory.GCREF, A())), |
| 124 |
(token2, rffi.cast(llmemory.GCREF, B())), |
| 125 |
(token2, rffi.cast(llmemory.GCREF, C())), |
| 126 |
(token2, rffi.cast(llmemory.GCREF, [42, 43])), |
| 127 |
|
| 128 |
(token3, rffi.cast(llmemory.GCREF, A())), |
| 129 |
(token3, rffi.cast(llmemory.GCREF, B())), |
| 130 |
(token3, rffi.cast(llmemory.GCREF, [44, 45])), |
| 131 |
|
| 132 |
(token4, rffi.cast(llmemory.GCREF, A())), |
| 133 |
(token4, rffi.cast(llmemory.GCREF, B())), |
| 134 |
(token4, rffi.cast(llmemory.GCREF, C())), |
| 135 |
]: |
| 136 |
frame = execute_token(token, p0) |
| 137 |
descr = cpu.get_latest_descr(frame) |
| 138 |
if descr is finaldescr: |
| 139 |
print 'match' |
| 140 |
elif descr is faildescr: |
| 141 |
print 'fail' |
| 142 |
else: |
| 143 |
print '???' |
| 144 |
|
| 145 |
if token is token2: |
| 146 |
print int(cpu.get_actual_typeid(p0) == typeid_B) |
| 147 |
if token is token3: |
| 148 |
print int(cpu.check_is_object(p0)) |
| 149 |
|
| 150 |
for p0 in [lltype.nullptr(llmemory.GCREF.TO), |
| 151 |
rffi.cast(llmemory.GCREF, A()), |
| 152 |
rffi.cast(llmemory.GCREF, B()), |
| 153 |
rffi.cast(llmemory.GCREF, C()), |
| 154 |
rffi.cast(llmemory.GCREF, lltype.malloc(LLD)), |
| 155 |
rffi.cast(llmemory.GCREF, lltype.malloc(ARRAY, 5)), |
| 156 |
rffi.cast(llmemory.GCREF, "foobar"), |
| 157 |
rffi.cast(llmemory.GCREF, u"foobaz")]: |
| 158 |
results = ['B', 'D', 'A', 'S', 'U'] |
| 159 |
try: |
| 160 |
cpu.protect_speculative_field(p0, fielddescr_B) |
| 161 |
except SpeculativeError: |
| 162 |
results[0] = '-' |
| 163 |
try: |
| 164 |
cpu.protect_speculative_field(p0, fielddescr_D) |
| 165 |
except SpeculativeError: |
| 166 |
results[1] = '-' |
| 167 |
try: |
| 168 |
cpu.protect_speculative_array(p0, arraydescr) |
| 169 |
except SpeculativeError: |
| 170 |
results[2] = '-' |
| 171 |
try: |
| 172 |
cpu.protect_speculative_string(p0) |
| 173 |
except SpeculativeError: |
| 174 |
results[3] = '-' |
| 175 |
try: |
| 176 |
cpu.protect_speculative_unicode(p0) |
| 177 |
except SpeculativeError: |
| 178 |
results[4] = '-' |
| 179 |
print ''.join(results) |
| 180 |
|
| 181 |
|
| 182 |
call_initial_function(t, g) |
| 183 |
|
| 184 |
cbuilder = genc.CStandaloneBuilder(t, main, t.config) |
| 185 |
cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES) |
| 186 |
cbuilder.compile() |
| 187 |
|
| 188 |
data = cbuilder.cmdexec('') |
| 189 |
assert data == ('fail\n' |
| 190 |
'match\n' |
| 191 |
'fail\n' |
| 192 |
|
| 193 |
'fail\n' |
| 194 |
'fail\n' |
| 195 |
'match\n' |
| 196 |
'fail\n' |
| 197 |
|
| 198 |
'fail\n' '0\n' |
| 199 |
'match\n' '1\n' |
| 200 |
'fail\n' '0\n' |
| 201 |
'fail\n' '0\n' |
| 202 |
|
| 203 |
'match\n' '1\n' |
| 204 |
'match\n' '1\n' |
| 205 |
'fail\n' '0\n' |
| 206 |
|
| 207 |
'fail\n' |
| 208 |
'match\n' |
| 209 |
'match\n' |
| 210 |
|
| 211 |
'-----\n' |
| 212 |
'-----\n' |
| 213 |
'B----\n' |
| 214 |
'B----\n' |
| 215 |
'-D---\n' |
| 216 |
'--A--\n' |
| 217 |
'---S-\n' |
| 218 |
'----U\n' |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
def test_guards_translated_with_gctypeptr(): |
| 223 |
run_guards_translated(gcremovetypeptr=False) |
| 224 |
|
| 225 |
def test_guards_translated_without_gctypeptr(): |
| 226 |
run_guards_translated(gcremovetypeptr=True) |