PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/pypy/rpython/llinterp.py

https://bitbucket.org/quangquach/pypy
Python | 1404 lines | 1392 code | 9 blank | 3 comment | 13 complexity | 2c81a4f21ac3c8527c7f791e073cce91 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. from pypy.objspace.flow.model import FunctionGraph, Constant, Variable, c_last_exception
  2. from pypy.rlib.rarithmetic import intmask, r_uint, ovfcheck, r_longlong, r_longlonglong
  3. from pypy.rlib.rarithmetic import r_ulonglong, is_valid_int
  4. from pypy.rpython.lltypesystem import lltype, llmemory, lloperation, llheap
  5. from pypy.rpython.lltypesystem import rclass
  6. from pypy.rpython.ootypesystem import ootype
  7. from pypy.rlib.objectmodel import ComputedIntSymbolic, CDefinedIntSymbolic
  8. from pypy.rlib.objectmodel import Symbolic
  9. from pypy.rlib import rstackovf
  10. import sys, os
  11. import math
  12. import py
  13. import traceback, cStringIO
  14. log = py.log.Producer('llinterp')
  15. class LLException(Exception):
  16. def __init__(self, *args):
  17. "NOT_RPYTHON"
  18. Exception.__init__(self, *args)
  19. def __str__(self):
  20. etype = self.args[0]
  21. #evalue = self.args[1]
  22. if len(self.args) > 2:
  23. f = cStringIO.StringIO()
  24. original_type, original_value, original_tb = self.args[2]
  25. traceback.print_exception(original_type, original_value, original_tb,
  26. file=f)
  27. extra = '\n' + f.getvalue().rstrip('\n')
  28. extra = extra.replace('\n', '\n | ') + '\n `------'
  29. else:
  30. extra = ''
  31. return '<LLException %r%s>' % (type_name(etype), extra)
  32. class LLFatalError(Exception):
  33. def __str__(self):
  34. return ': '.join([str(x) for x in self.args])
  35. def type_name(etype):
  36. if isinstance(lltype.typeOf(etype), lltype.Ptr):
  37. return ''.join(etype.name).rstrip('\x00')
  38. else:
  39. # ootype!
  40. return etype._INSTANCE._name.split(".")[-1]
  41. class LLInterpreter(object):
  42. """ low level interpreter working with concrete values. """
  43. current_interpreter = None
  44. def __init__(self, typer, tracing=True, exc_data_ptr=None):
  45. self.bindings = {}
  46. self.typer = typer
  47. # 'heap' is module or object that provides malloc, etc for lltype ops
  48. self.heap = llheap
  49. self.exc_data_ptr = exc_data_ptr
  50. self.frame_stack = []
  51. self.tracer = None
  52. self.frame_class = LLFrame
  53. if tracing:
  54. self.tracer = Tracer()
  55. def eval_graph(self, graph, args=(), recursive=False):
  56. llframe = self.frame_class(graph, args, self)
  57. if self.tracer and not recursive:
  58. global tracer1
  59. tracer1 = self.tracer
  60. self.tracer.start()
  61. retval = None
  62. self.traceback_frames = []
  63. old_frame_stack = self.frame_stack[:]
  64. prev_interpreter = LLInterpreter.current_interpreter
  65. LLInterpreter.current_interpreter = self
  66. try:
  67. try:
  68. retval = llframe.eval()
  69. except LLException, e:
  70. log.error("LLEXCEPTION: %s" % (e, ))
  71. self.print_traceback()
  72. if self.tracer:
  73. self.tracer.dump('LLException: %s\n' % (e,))
  74. raise
  75. except Exception, e:
  76. if getattr(e, '_go_through_llinterp_uncaught_', False):
  77. raise
  78. log.error("AN ERROR OCCURED: %s" % (e, ))
  79. self.print_traceback()
  80. if self.tracer:
  81. line = str(e)
  82. if line:
  83. line = ': ' + line
  84. line = '* %s' % (e.__class__.__name__,) + line
  85. self.tracer.dump(line + '\n')
  86. raise
  87. finally:
  88. LLInterpreter.current_interpreter = prev_interpreter
  89. assert old_frame_stack == self.frame_stack
  90. if self.tracer:
  91. if retval is not None:
  92. self.tracer.dump(' ---> %r\n' % (retval,))
  93. if not recursive:
  94. self.tracer.stop()
  95. return retval
  96. def print_traceback(self):
  97. frames = self.traceback_frames
  98. frames.reverse()
  99. self.traceback_frames = []
  100. lines = []
  101. for frame in frames:
  102. logline = frame.graph.name + "()"
  103. if frame.curr_block is None:
  104. logline += " <not running yet>"
  105. lines.append(logline)
  106. continue
  107. try:
  108. logline += " " + self.typer.annotator.annotated[frame.curr_block].func.__module__
  109. except (KeyError, AttributeError, TypeError):
  110. logline += " <unknown module>"
  111. lines.append(logline)
  112. for i, operation in enumerate(frame.curr_block.operations):
  113. if i == frame.curr_operation_index:
  114. logline = "E %s"
  115. else:
  116. logline = " %s"
  117. lines.append(logline % (operation, ))
  118. if self.tracer:
  119. self.tracer.dump('Traceback\n', bold=True)
  120. for line in lines:
  121. self.tracer.dump(line + '\n')
  122. for line in lines:
  123. log.traceback(line)
  124. def find_roots(self):
  125. """Return a list of the addresses of the roots."""
  126. #log.findroots("starting")
  127. roots = []
  128. for frame in self.frame_stack:
  129. #log.findroots("graph", frame.graph.name)
  130. frame.find_roots(roots)
  131. return roots
  132. def find_exception(self, exc):
  133. assert isinstance(exc, LLException)
  134. klass, inst = exc.args[0], exc.args[1]
  135. for cls in enumerate_exceptions_top_down():
  136. if hasattr(klass, 'name'): # lltype
  137. if "".join(klass.name).rstrip("\0") == cls.__name__:
  138. return cls
  139. else: # ootype
  140. if klass._INSTANCE._name.split('.')[-1] == cls.__name__:
  141. return cls
  142. raise ValueError("couldn't match exception, maybe it"
  143. " has RPython attributes like OSError?")
  144. def get_transformed_exc_data(self, graph):
  145. if hasattr(graph, 'exceptiontransformed'):
  146. return graph.exceptiontransformed
  147. if getattr(graph, 'rgenop', False):
  148. return self.exc_data_ptr
  149. return None
  150. def _store_exception(self, exc):
  151. raise PleaseOverwriteStoreException("You just invoked ll2ctypes callback without overwriting _store_exception on llinterpreter")
  152. class PleaseOverwriteStoreException(Exception):
  153. pass
  154. def checkptr(ptr):
  155. assert isinstance(lltype.typeOf(ptr), lltype.Ptr)
  156. def checkadr(addr):
  157. assert lltype.typeOf(addr) is llmemory.Address
  158. def is_inst(inst):
  159. return isinstance(lltype.typeOf(inst), (ootype.Instance, ootype.BuiltinType, ootype.StaticMethod))
  160. def checkinst(inst):
  161. assert is_inst(inst)
  162. class LLFrame(object):
  163. def __init__(self, graph, args, llinterpreter):
  164. assert not graph or isinstance(graph, FunctionGraph)
  165. self.graph = graph
  166. self.args = args
  167. self.llinterpreter = llinterpreter
  168. self.heap = llinterpreter.heap
  169. self.bindings = {}
  170. self.curr_block = None
  171. self.curr_operation_index = 0
  172. self.alloca_objects = []
  173. def newsubframe(self, graph, args):
  174. return self.__class__(graph, args, self.llinterpreter)
  175. # _______________________________________________________
  176. # variable setters/getters helpers
  177. def clear(self):
  178. self.bindings.clear()
  179. def fillvars(self, block, values):
  180. vars = block.inputargs
  181. assert len(vars) == len(values), (
  182. "block %s received %d args, expected %d" % (
  183. block, len(values), len(vars)))
  184. for var, val in zip(vars, values):
  185. self.setvar(var, val)
  186. def setvar(self, var, val):
  187. if var.concretetype is not lltype.Void:
  188. try:
  189. val = lltype.enforce(var.concretetype, val)
  190. except TypeError:
  191. assert False, "type error: input value of type:\n\n\t%r\n\n===> variable of type:\n\n\t%r\n" % (lltype.typeOf(val), var.concretetype)
  192. assert isinstance(var, Variable)
  193. self.bindings[var] = val
  194. def setifvar(self, var, val):
  195. if isinstance(var, Variable):
  196. self.setvar(var, val)
  197. def getval(self, varorconst):
  198. try:
  199. val = varorconst.value
  200. except AttributeError:
  201. val = self.bindings[varorconst]
  202. if isinstance(val, ComputedIntSymbolic):
  203. val = val.compute_fn()
  204. if varorconst.concretetype is not lltype.Void:
  205. try:
  206. val = lltype.enforce(varorconst.concretetype, val)
  207. except TypeError:
  208. assert False, "type error: %r val from %r var/const" % (lltype.typeOf(val), varorconst.concretetype)
  209. return val
  210. def getval_or_subop(self, varorsubop):
  211. from pypy.translator.oosupport.treebuilder import SubOperation
  212. if isinstance(varorsubop, SubOperation):
  213. self.eval_operation(varorsubop.op)
  214. resultval = self.getval(varorsubop.op.result)
  215. del self.bindings[varorsubop.op.result] # XXX hack
  216. return resultval
  217. else:
  218. return self.getval(varorsubop)
  219. # _______________________________________________________
  220. # other helpers
  221. def getoperationhandler(self, opname):
  222. ophandler = getattr(self, 'op_' + opname, None)
  223. if ophandler is None:
  224. # try to import the operation from opimpl.py
  225. ophandler = lloperation.LL_OPERATIONS[opname].fold
  226. setattr(self.__class__, 'op_' + opname, staticmethod(ophandler))
  227. return ophandler
  228. # _______________________________________________________
  229. # evaling functions
  230. def eval(self):
  231. graph = self.graph
  232. tracer = self.llinterpreter.tracer
  233. if tracer:
  234. tracer.enter(graph)
  235. self.llinterpreter.frame_stack.append(self)
  236. try:
  237. try:
  238. nextblock = graph.startblock
  239. args = self.args
  240. while 1:
  241. self.clear()
  242. self.fillvars(nextblock, args)
  243. nextblock, args = self.eval_block(nextblock)
  244. if nextblock is None:
  245. for obj in self.alloca_objects:
  246. obj._obj._free()
  247. return args
  248. except Exception:
  249. self.llinterpreter.traceback_frames.append(self)
  250. raise
  251. finally:
  252. leavingframe = self.llinterpreter.frame_stack.pop()
  253. assert leavingframe is self
  254. if tracer:
  255. tracer.leave()
  256. def eval_block(self, block):
  257. """ return (nextblock, values) tuple. If nextblock
  258. is None, values is the concrete return value.
  259. """
  260. self.curr_block = block
  261. catch_exception = block.exitswitch == c_last_exception
  262. e = None
  263. try:
  264. for i, op in enumerate(block.operations):
  265. self.curr_operation_index = i
  266. self.eval_operation(op)
  267. except LLException, e:
  268. if not (catch_exception and op is block.operations[-1]):
  269. raise
  270. except RuntimeError, e:
  271. rstackovf.check_stack_overflow()
  272. # xxx fish fish fish for proper etype and evalue to use
  273. rtyper = self.llinterpreter.typer
  274. bk = rtyper.annotator.bookkeeper
  275. classdef = bk.getuniqueclassdef(rstackovf._StackOverflow)
  276. exdata = rtyper.getexceptiondata()
  277. evalue = exdata.get_standard_ll_exc_instance(rtyper, classdef)
  278. etype = exdata.fn_type_of_exc_inst(evalue)
  279. e = LLException(etype, evalue)
  280. if not (catch_exception and op is block.operations[-1]):
  281. raise e
  282. # determine nextblock and/or return value
  283. if len(block.exits) == 0:
  284. # return block
  285. tracer = self.llinterpreter.tracer
  286. if len(block.inputargs) == 2:
  287. # exception
  288. if tracer:
  289. tracer.dump('raise')
  290. etypevar, evaluevar = block.getvariables()
  291. etype = self.getval(etypevar)
  292. evalue = self.getval(evaluevar)
  293. # watch out, these are _ptr's
  294. raise LLException(etype, evalue)
  295. resultvar, = block.getvariables()
  296. result = self.getval(resultvar)
  297. exc_data = self.llinterpreter.get_transformed_exc_data(self.graph)
  298. if exc_data:
  299. # re-raise the exception set by this graph, if any
  300. etype = exc_data.exc_type
  301. if etype:
  302. evalue = exc_data.exc_value
  303. if tracer:
  304. tracer.dump('raise')
  305. exc_data.exc_type = lltype.typeOf(etype )._defl()
  306. exc_data.exc_value = lltype.typeOf(evalue)._defl()
  307. from pypy.translator import exceptiontransform
  308. T = resultvar.concretetype
  309. errvalue = exceptiontransform.error_value(T)
  310. # check that the exc-transformed graph returns the error
  311. # value when it returns with an exception set
  312. assert result == errvalue
  313. raise LLException(etype, evalue)
  314. if tracer:
  315. tracer.dump('return')
  316. return None, result
  317. elif block.exitswitch is None:
  318. # single-exit block
  319. assert len(block.exits) == 1
  320. link = block.exits[0]
  321. elif catch_exception:
  322. link = block.exits[0]
  323. if e:
  324. exdata = self.llinterpreter.typer.getexceptiondata()
  325. cls = e.args[0]
  326. inst = e.args[1]
  327. for link in block.exits[1:]:
  328. assert issubclass(link.exitcase, py.builtin.BaseException)
  329. if self.op_direct_call(exdata.fn_exception_match,
  330. cls, link.llexitcase):
  331. self.setifvar(link.last_exception, cls)
  332. self.setifvar(link.last_exc_value, inst)
  333. break
  334. else:
  335. # no handler found, pass on
  336. raise e
  337. else:
  338. llexitvalue = self.getval(block.exitswitch)
  339. if block.exits[-1].exitcase == "default":
  340. defaultexit = block.exits[-1]
  341. nondefaultexits = block.exits[:-1]
  342. assert defaultexit.llexitcase is None
  343. else:
  344. defaultexit = None
  345. nondefaultexits = block.exits
  346. for link in nondefaultexits:
  347. if link.llexitcase == llexitvalue:
  348. break # found -- the result is in 'link'
  349. else:
  350. if defaultexit is None:
  351. raise ValueError("exit case %r not found in the exit links "
  352. "of %r" % (llexitvalue, block))
  353. else:
  354. link = defaultexit
  355. return link.target, [self.getval(x) for x in link.args]
  356. def eval_operation(self, operation):
  357. tracer = self.llinterpreter.tracer
  358. if tracer:
  359. tracer.dump(str(operation))
  360. ophandler = self.getoperationhandler(operation.opname)
  361. # XXX slighly unnice but an important safety check
  362. if operation.opname == 'direct_call':
  363. assert isinstance(operation.args[0], Constant)
  364. elif operation.opname == 'indirect_call':
  365. assert isinstance(operation.args[0], Variable)
  366. if getattr(ophandler, 'specialform', False):
  367. retval = ophandler(*operation.args)
  368. else:
  369. vals = [self.getval_or_subop(x) for x in operation.args]
  370. if getattr(ophandler, 'need_result_type', False):
  371. vals.insert(0, operation.result.concretetype)
  372. try:
  373. retval = ophandler(*vals)
  374. except LLException, e:
  375. # safety check check that the operation is allowed to raise that
  376. # exception
  377. if operation.opname in lloperation.LL_OPERATIONS:
  378. canraise = lloperation.LL_OPERATIONS[operation.opname].canraise
  379. if Exception not in canraise:
  380. exc = self.llinterpreter.find_exception(e)
  381. for canraiseexc in canraise:
  382. if issubclass(exc, canraiseexc):
  383. break
  384. else:
  385. raise TypeError("the operation %s is not expected to raise %s" % (operation, exc))
  386. # for exception-transformed graphs, store the LLException
  387. # into the exc_data used by this graph
  388. exc_data = self.llinterpreter.get_transformed_exc_data(
  389. self.graph)
  390. if exc_data:
  391. etype = e.args[0]
  392. evalue = e.args[1]
  393. exc_data.exc_type = etype
  394. exc_data.exc_value = evalue
  395. from pypy.translator import exceptiontransform
  396. retval = exceptiontransform.error_value(
  397. operation.result.concretetype)
  398. else:
  399. raise
  400. self.setvar(operation.result, retval)
  401. if tracer:
  402. if retval is None:
  403. tracer.dump('\n')
  404. else:
  405. tracer.dump(' ---> %r\n' % (retval,))
  406. def make_llexception(self, exc=None):
  407. if exc is None:
  408. original = sys.exc_info()
  409. exc = original[1]
  410. # it makes no sense to convert some exception classes that
  411. # just mean something buggy crashed
  412. if isinstance(exc, (AssertionError, AttributeError,
  413. TypeError, NameError,
  414. KeyboardInterrupt, SystemExit,
  415. ImportError, SyntaxError)):
  416. raise original[0], original[1], original[2] # re-raise it
  417. # for testing the JIT (see ContinueRunningNormally) we need
  418. # to let some exceptions introduced by the JIT go through
  419. # the llinterpreter uncaught
  420. if getattr(exc, '_go_through_llinterp_uncaught_', False):
  421. raise original[0], original[1], original[2] # re-raise it
  422. extraargs = (original,)
  423. else:
  424. extraargs = ()
  425. typer = self.llinterpreter.typer
  426. exdata = typer.getexceptiondata()
  427. if isinstance(exc, OSError):
  428. self.op_direct_call(exdata.fn_raise_OSError, exc.errno)
  429. assert False, "op_direct_call above should have raised"
  430. else:
  431. evalue = exdata.get_standard_ll_exc_instance_by_class(exc.__class__)
  432. etype = self.op_direct_call(exdata.fn_type_of_exc_inst, evalue)
  433. raise LLException(etype, evalue, *extraargs)
  434. def invoke_callable_with_pyexceptions(self, fptr, *args):
  435. obj = self.llinterpreter.typer.type_system.deref(fptr)
  436. try:
  437. return obj._callable(*args)
  438. except LLException, e:
  439. raise
  440. except Exception, e:
  441. if getattr(e, '_go_through_llinterp_uncaught_', False):
  442. raise
  443. if getattr(obj, '_debugexc', False):
  444. log.ERROR('The llinterpreter got an '
  445. 'unexpected exception when calling')
  446. log.ERROR('the external function %r:' % (fptr,))
  447. log.ERROR('%s: %s' % (e.__class__.__name__, e))
  448. if self.llinterpreter.tracer:
  449. self.llinterpreter.tracer.flush()
  450. import sys
  451. from pypy.translator.tool.pdbplus import PdbPlusShow
  452. PdbPlusShow(None).post_mortem(sys.exc_info()[2])
  453. self.make_llexception()
  454. def find_roots(self, roots):
  455. #log.findroots(self.curr_block.inputargs)
  456. vars = []
  457. for v in self.curr_block.inputargs:
  458. if isinstance(v, Variable):
  459. vars.append(v)
  460. for op in self.curr_block.operations[:self.curr_operation_index]:
  461. vars.append(op.result)
  462. for v in vars:
  463. TYPE = v.concretetype
  464. if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'gc':
  465. roots.append(_address_of_local_var(self, v))
  466. # __________________________________________________________
  467. # misc LL operation implementations
  468. def op_debug_view(self, *ll_objects):
  469. from pypy.translator.tool.lltracker import track
  470. track(*ll_objects)
  471. def op_debug_assert(self, x, msg):
  472. assert x, msg
  473. def op_debug_fatalerror(self, ll_msg, ll_exc=None):
  474. msg = ''.join(ll_msg.chars)
  475. if ll_exc is None:
  476. raise LLFatalError(msg)
  477. else:
  478. ll_exc_type = lltype.cast_pointer(rclass.OBJECTPTR, ll_exc).typeptr
  479. raise LLFatalError(msg, LLException(ll_exc_type, ll_exc))
  480. def op_debug_llinterpcall(self, pythonfunction, *args_ll):
  481. try:
  482. return pythonfunction(*args_ll)
  483. except:
  484. self.make_llexception()
  485. def op_debug_start_traceback(self, *args):
  486. pass # xxx write debugging code here?
  487. def op_debug_reraise_traceback(self, *args):
  488. pass # xxx write debugging code here?
  489. def op_debug_record_traceback(self, *args):
  490. pass # xxx write debugging code here?
  491. def op_debug_print_traceback(self, *args):
  492. pass # xxx write debugging code here?
  493. def op_debug_catch_exception(self, *args):
  494. pass # xxx write debugging code here?
  495. def op_jit_marker(self, *args):
  496. pass
  497. def op_jit_record_known_class(self, *args):
  498. pass
  499. def op_get_exception_addr(self, *args):
  500. raise NotImplementedError
  501. def op_get_exc_value_addr(self, *args):
  502. raise NotImplementedError
  503. def op_instrument_count(self, ll_tag, ll_label):
  504. pass # xxx for now
  505. def op_keepalive(self, value):
  506. pass
  507. def op_hint(self, x, hints):
  508. return x
  509. def op_decode_arg(self, fname, i, name, vargs, vkwds):
  510. raise NotImplementedError("decode_arg")
  511. def op_decode_arg_def(self, fname, i, name, vargs, vkwds, default):
  512. raise NotImplementedError("decode_arg_def")
  513. def op_check_no_more_arg(self, fname, n, vargs):
  514. raise NotImplementedError("check_no_more_arg")
  515. def op_getslice(self, vargs, start, stop_should_be_None):
  516. raise NotImplementedError("getslice") # only for argument parsing
  517. def op_check_self_nonzero(self, fname, vself):
  518. raise NotImplementedError("check_self_nonzero")
  519. def op_setfield(self, obj, fieldname, fieldvalue):
  520. # obj should be pointer
  521. FIELDTYPE = getattr(lltype.typeOf(obj).TO, fieldname)
  522. if FIELDTYPE is not lltype.Void:
  523. self.heap.setfield(obj, fieldname, fieldvalue)
  524. def op_bare_setfield(self, obj, fieldname, fieldvalue):
  525. # obj should be pointer
  526. FIELDTYPE = getattr(lltype.typeOf(obj).TO, fieldname)
  527. if FIELDTYPE is not lltype.Void:
  528. setattr(obj, fieldname, fieldvalue)
  529. def op_getinteriorfield(self, obj, *offsets):
  530. checkptr(obj)
  531. ob = obj
  532. for o in offsets:
  533. if isinstance(o, str):
  534. ob = getattr(ob, o)
  535. else:
  536. ob = ob[o]
  537. assert not isinstance(ob, lltype._interior_ptr)
  538. return ob
  539. def getinneraddr(self, obj, *offsets):
  540. TYPE = lltype.typeOf(obj).TO
  541. addr = llmemory.cast_ptr_to_adr(obj)
  542. for o in offsets:
  543. if isinstance(o, str):
  544. addr += llmemory.offsetof(TYPE, o)
  545. TYPE = getattr(TYPE, o)
  546. else:
  547. addr += llmemory.itemoffsetof(TYPE, o)
  548. TYPE = TYPE.OF
  549. return addr, TYPE
  550. def op_setinteriorfield(self, obj, *fieldnamesval):
  551. offsets, fieldvalue = fieldnamesval[:-1], fieldnamesval[-1]
  552. inneraddr, FIELD = self.getinneraddr(obj, *offsets)
  553. if FIELD is not lltype.Void:
  554. self.heap.setinterior(obj, inneraddr, FIELD, fieldvalue, offsets)
  555. def op_bare_setinteriorfield(self, obj, *fieldnamesval):
  556. offsets, fieldvalue = fieldnamesval[:-1], fieldnamesval[-1]
  557. inneraddr, FIELD = self.getinneraddr(obj, *offsets)
  558. if FIELD is not lltype.Void:
  559. llheap.setinterior(obj, inneraddr, FIELD, fieldvalue)
  560. def op_getarrayitem(self, array, index):
  561. return array[index]
  562. def op_setarrayitem(self, array, index, item):
  563. # array should be a pointer
  564. ITEMTYPE = lltype.typeOf(array).TO.OF
  565. if ITEMTYPE is not lltype.Void:
  566. self.heap.setarrayitem(array, index, item)
  567. def op_bare_setarrayitem(self, array, index, item):
  568. # array should be a pointer
  569. ITEMTYPE = lltype.typeOf(array).TO.OF
  570. if ITEMTYPE is not lltype.Void:
  571. array[index] = item
  572. def perform_call(self, f, ARGS, args):
  573. fobj = self.llinterpreter.typer.type_system.deref(f)
  574. has_callable = getattr(fobj, '_callable', None) is not None
  575. if hasattr(fobj, 'graph'):
  576. graph = fobj.graph
  577. else:
  578. assert has_callable, "don't know how to execute %r" % f
  579. return self.invoke_callable_with_pyexceptions(f, *args)
  580. args_v = graph.getargs()
  581. if len(ARGS) != len(args_v):
  582. raise TypeError("graph with %d args called with wrong func ptr type: %r" %(len(args_v), ARGS))
  583. for T, v in zip(ARGS, args_v):
  584. if not lltype.isCompatibleType(T, v.concretetype):
  585. raise TypeError("graph with %r args called with wrong func ptr type: %r" %
  586. (tuple([v.concretetype for v in args_v]), ARGS))
  587. frame = self.newsubframe(graph, args)
  588. return frame.eval()
  589. def op_direct_call(self, f, *args):
  590. FTYPE = self.llinterpreter.typer.type_system.derefType(lltype.typeOf(f))
  591. return self.perform_call(f, FTYPE.ARGS, args)
  592. def op_indirect_call(self, f, *args):
  593. graphs = args[-1]
  594. args = args[:-1]
  595. if graphs is not None:
  596. obj = self.llinterpreter.typer.type_system.deref(f)
  597. if hasattr(obj, 'graph'):
  598. assert obj.graph in graphs
  599. else:
  600. pass
  601. #log.warn("op_indirect_call with graphs=None:", f)
  602. return self.op_direct_call(f, *args)
  603. def op_malloc(self, obj, flags):
  604. flavor = flags['flavor']
  605. zero = flags.get('zero', False)
  606. track_allocation = flags.get('track_allocation', True)
  607. if flavor == "stack":
  608. result = self.heap.malloc(obj, zero=zero, flavor='raw')
  609. self.alloca_objects.append(result)
  610. return result
  611. ptr = self.heap.malloc(obj, zero=zero, flavor=flavor,
  612. track_allocation=track_allocation)
  613. return ptr
  614. def op_malloc_varsize(self, obj, flags, size):
  615. flavor = flags['flavor']
  616. zero = flags.get('zero', False)
  617. track_allocation = flags.get('track_allocation', True)
  618. assert flavor in ('gc', 'raw')
  619. try:
  620. ptr = self.heap.malloc(obj, size, zero=zero, flavor=flavor,
  621. track_allocation=track_allocation)
  622. return ptr
  623. except MemoryError:
  624. self.make_llexception()
  625. def op_malloc_nonmovable(self, TYPE, flags):
  626. flavor = flags['flavor']
  627. assert flavor == 'gc'
  628. zero = flags.get('zero', False)
  629. return self.heap.malloc_nonmovable(TYPE, zero=zero)
  630. def op_malloc_nonmovable_varsize(self, TYPE, flags, size):
  631. flavor = flags['flavor']
  632. assert flavor == 'gc'
  633. zero = flags.get('zero', False)
  634. return self.heap.malloc_nonmovable(TYPE, size, zero=zero)
  635. def op_free(self, obj, flags):
  636. assert flags['flavor'] == 'raw'
  637. track_allocation = flags.get('track_allocation', True)
  638. self.heap.free(obj, flavor='raw', track_allocation=track_allocation)
  639. def op_gc_add_memory_pressure(self, size):
  640. self.heap.add_memory_pressure(size)
  641. def op_shrink_array(self, obj, smallersize):
  642. return self.heap.shrink_array(obj, smallersize)
  643. def op_zero_gc_pointers_inside(self, obj):
  644. raise NotImplementedError("zero_gc_pointers_inside")
  645. def op_gc_writebarrier_before_copy(self, source, dest,
  646. source_start, dest_start, length):
  647. if hasattr(self.heap, 'writebarrier_before_copy'):
  648. return self.heap.writebarrier_before_copy(source, dest,
  649. source_start, dest_start,
  650. length)
  651. else:
  652. return True
  653. def op_getfield(self, obj, field):
  654. checkptr(obj)
  655. # check the difference between op_getfield and op_getsubstruct:
  656. assert not isinstance(getattr(lltype.typeOf(obj).TO, field),
  657. lltype.ContainerType)
  658. return getattr(obj, field)
  659. def op_force_cast(self, RESTYPE, obj):
  660. from pypy.rpython.lltypesystem import ll2ctypes
  661. return ll2ctypes.force_cast(RESTYPE, obj)
  662. op_force_cast.need_result_type = True
  663. def op_cast_int_to_ptr(self, RESTYPE, int1):
  664. return lltype.cast_int_to_ptr(RESTYPE, int1)
  665. op_cast_int_to_ptr.need_result_type = True
  666. def op_cast_ptr_to_int(self, ptr1):
  667. checkptr(ptr1)
  668. return lltype.cast_ptr_to_int(ptr1)
  669. def op_cast_opaque_ptr(self, RESTYPE, obj):
  670. checkptr(obj)
  671. return lltype.cast_opaque_ptr(RESTYPE, obj)
  672. op_cast_opaque_ptr.need_result_type = True
  673. def op_cast_ptr_to_adr(self, ptr):
  674. checkptr(ptr)
  675. return llmemory.cast_ptr_to_adr(ptr)
  676. def op_cast_adr_to_int(self, adr, mode):
  677. checkadr(adr)
  678. return llmemory.cast_adr_to_int(adr, mode)
  679. def op_convert_float_bytes_to_longlong(self, f):
  680. from pypy.rlib import longlong2float
  681. return longlong2float.float2longlong(f)
  682. def op_weakref_create(self, v_obj):
  683. def objgetter(): # special support for gcwrapper.py
  684. return self.getval(v_obj)
  685. return self.heap.weakref_create_getlazy(objgetter)
  686. op_weakref_create.specialform = True
  687. def op_weakref_deref(self, PTRTYPE, obj):
  688. return self.heap.weakref_deref(PTRTYPE, obj)
  689. op_weakref_deref.need_result_type = True
  690. def op_cast_ptr_to_weakrefptr(self, obj):
  691. return llmemory.cast_ptr_to_weakrefptr(obj)
  692. def op_cast_weakrefptr_to_ptr(self, PTRTYPE, obj):
  693. return llmemory.cast_weakrefptr_to_ptr(PTRTYPE, obj)
  694. op_cast_weakrefptr_to_ptr.need_result_type = True
  695. def op_gc__collect(self, *gen):
  696. self.heap.collect(*gen)
  697. def op_gc_heap_stats(self):
  698. raise NotImplementedError
  699. def op_gc_obtain_free_space(self, size):
  700. raise NotImplementedError
  701. def op_gc_can_move(self, ptr):
  702. addr = llmemory.cast_ptr_to_adr(ptr)
  703. return self.heap.can_move(addr)
  704. def op_gc_thread_prepare(self):
  705. self.heap.thread_prepare()
  706. def op_gc_thread_run(self):
  707. self.heap.thread_run()
  708. def op_gc_thread_start(self):
  709. self.heap.thread_start()
  710. def op_gc_thread_die(self):
  711. self.heap.thread_die()
  712. def op_gc_thread_before_fork(self):
  713. raise NotImplementedError
  714. def op_gc_thread_after_fork(self):
  715. raise NotImplementedError
  716. def op_gc_free(self, addr):
  717. # what can you do?
  718. pass
  719. #raise NotImplementedError("gc_free")
  720. def op_gc_fetch_exception(self):
  721. raise NotImplementedError("gc_fetch_exception")
  722. def op_gc_restore_exception(self, exc):
  723. raise NotImplementedError("gc_restore_exception")
  724. def op_gc_adr_of_nursery_top(self):
  725. raise NotImplementedError
  726. def op_gc_adr_of_nursery_free(self):
  727. raise NotImplementedError
  728. def op_gc_adr_of_root_stack_base(self):
  729. raise NotImplementedError
  730. def op_gc_adr_of_root_stack_top(self):
  731. raise NotImplementedError
  732. def op_gc_call_rtti_destructor(self, rtti, addr):
  733. if hasattr(rtti._obj, 'destructor_funcptr'):
  734. d = rtti._obj.destructor_funcptr
  735. obptr = addr.ref()
  736. return self.op_direct_call(d, obptr)
  737. def op_gc_deallocate(self, TYPE, addr):
  738. raise NotImplementedError("gc_deallocate")
  739. def op_gc_reload_possibly_moved(self, v_newaddr, v_ptr):
  740. assert v_newaddr.concretetype is llmemory.Address
  741. assert isinstance(v_ptr.concretetype, lltype.Ptr)
  742. assert v_ptr.concretetype.TO._gckind == 'gc'
  743. newaddr = self.getval(v_newaddr)
  744. p = llmemory.cast_adr_to_ptr(newaddr, v_ptr.concretetype)
  745. if isinstance(v_ptr, Constant):
  746. assert v_ptr.value == p
  747. else:
  748. self.setvar(v_ptr, p)
  749. op_gc_reload_possibly_moved.specialform = True
  750. def op_gc_identityhash(self, obj):
  751. return lltype.identityhash(obj)
  752. def op_gc_id(self, ptr):
  753. PTR = lltype.typeOf(ptr)
  754. if isinstance(PTR, lltype.Ptr):
  755. return self.heap.gc_id(ptr)
  756. elif isinstance(PTR, ootype.OOType):
  757. return ootype.identityhash(ptr) # XXX imprecise
  758. raise NotImplementedError("gc_id on %r" % (PTR,))
  759. def op_gc_set_max_heap_size(self, maxsize):
  760. raise NotImplementedError("gc_set_max_heap_size")
  761. def op_gc_asmgcroot_static(self, index):
  762. raise NotImplementedError("gc_asmgcroot_static")
  763. def op_gc_stack_bottom(self):
  764. pass # marker for trackgcroot.py
  765. def op_gc_shadowstackref_new(self): # stacklet+shadowstack
  766. raise NotImplementedError("gc_shadowstackref_new")
  767. def op_gc_shadowstackref_context(self):
  768. raise NotImplementedError("gc_shadowstackref_context")
  769. def op_gc_shadowstackref_destroy(self):
  770. raise NotImplementedError("gc_shadowstackref_destroy")
  771. def op_gc_save_current_state_away(self):
  772. raise NotImplementedError("gc_save_current_state_away")
  773. def op_gc_forget_current_state(self):
  774. raise NotImplementedError("gc_forget_current_state")
  775. def op_gc_restore_state_from(self):
  776. raise NotImplementedError("gc_restore_state_from")
  777. def op_gc_start_fresh_new_state(self):
  778. raise NotImplementedError("gc_start_fresh_new_state")
  779. def op_gc_get_type_info_group(self):
  780. raise NotImplementedError("gc_get_type_info_group")
  781. def op_gc_get_rpy_memory_usage(self):
  782. raise NotImplementedError("gc_get_rpy_memory_usage")
  783. def op_gc_get_rpy_roots(self):
  784. raise NotImplementedError("gc_get_rpy_roots")
  785. def op_gc_get_rpy_referents(self):
  786. raise NotImplementedError("gc_get_rpy_referents")
  787. def op_gc_is_rpy_instance(self):
  788. raise NotImplementedError("gc_is_rpy_instance")
  789. def op_gc_get_rpy_type_index(self):
  790. raise NotImplementedError("gc_get_rpy_type_index")
  791. def op_gc_dump_rpy_heap(self):
  792. raise NotImplementedError("gc_dump_rpy_heap")
  793. def op_gc_typeids_z(self):
  794. raise NotImplementedError("gc_typeids_z")
  795. def op_gc_gcflag_extra(self, subopnum, *args):
  796. return self.heap.gcflag_extra(subopnum, *args)
  797. def op_do_malloc_fixedsize_clear(self):
  798. raise NotImplementedError("do_malloc_fixedsize_clear")
  799. def op_do_malloc_varsize_clear(self):
  800. raise NotImplementedError("do_malloc_varsize_clear")
  801. def op_get_write_barrier_failing_case(self):
  802. raise NotImplementedError("get_write_barrier_failing_case")
  803. def op_get_write_barrier_from_array_failing_case(self):
  804. raise NotImplementedError("get_write_barrier_from_array_failing_case")
  805. def op_stack_current(self):
  806. return 0
  807. # __________________________________________________________
  808. # operations on addresses
  809. def op_raw_malloc(self, size):
  810. assert lltype.typeOf(size) == lltype.Signed
  811. return llmemory.raw_malloc(size)
  812. op_boehm_malloc = op_boehm_malloc_atomic = op_raw_malloc
  813. def op_boehm_register_finalizer(self, p, finalizer):
  814. pass
  815. def op_boehm_disappearing_link(self, link, obj):
  816. pass
  817. def op_raw_malloc_usage(self, size):
  818. assert lltype.typeOf(size) == lltype.Signed
  819. return llmemory.raw_malloc_usage(size)
  820. def op_raw_free(self, addr):
  821. checkadr(addr)
  822. llmemory.raw_free(addr)
  823. def op_raw_memclear(self, addr, size):
  824. checkadr(addr)
  825. llmemory.raw_memclear(addr, size)
  826. def op_raw_memcopy(self, fromaddr, toaddr, size):
  827. checkadr(fromaddr)
  828. checkadr(toaddr)
  829. llmemory.raw_memcopy(fromaddr, toaddr, size)
  830. op_raw_memmove = op_raw_memcopy # this is essentially the same here
  831. def op_raw_load(self, RESTYPE, addr, offset):
  832. checkadr(addr)
  833. if isinstance(offset, int):
  834. from pypy.rpython.lltypesystem import rffi
  835. ll_p = rffi.cast(rffi.CCHARP, addr)
  836. ll_p = rffi.cast(rffi.CArrayPtr(RESTYPE),
  837. rffi.ptradd(ll_p, offset))
  838. value = ll_p[0]
  839. else:
  840. assert offset.TYPE == RESTYPE
  841. value = getattr(addr, str(RESTYPE).lower())[offset.repeat]
  842. assert lltype.typeOf(value) == RESTYPE
  843. return value
  844. op_raw_load.need_result_type = True
  845. def op_raw_store(self, addr, offset, value):
  846. checkadr(addr)
  847. ARGTYPE = lltype.typeOf(value)
  848. if isinstance(offset, int):
  849. from pypy.rpython.lltypesystem import rffi
  850. ll_p = rffi.cast(rffi.CCHARP, addr)
  851. ll_p = rffi.cast(rffi.CArrayPtr(ARGTYPE),
  852. rffi.ptradd(ll_p, offset))
  853. ll_p[0] = value
  854. else:
  855. assert offset.TYPE == ARGTYPE
  856. getattr(addr, str(ARGTYPE).lower())[offset.repeat] = value
  857. def op_stack_malloc(self, size): # mmh
  858. raise NotImplementedError("backend only")
  859. def op_track_alloc_start(self, addr):
  860. # we don't do tracking at this level
  861. checkadr(addr)
  862. def op_track_alloc_stop(self, addr):
  863. checkadr(addr)
  864. # ____________________________________________________________
  865. # Overflow-detecting variants
  866. def op_int_neg_ovf(self, x):
  867. assert is_valid_int(x)
  868. try:
  869. return ovfcheck(-x)
  870. except OverflowError:
  871. self.make_llexception()
  872. def op_int_abs_ovf(self, x):
  873. assert is_valid_int(x)
  874. try:
  875. return ovfcheck(abs(x))
  876. except OverflowError:
  877. self.make_llexception()
  878. def op_int_lshift_ovf(self, x, y):
  879. assert is_valid_int(x)
  880. assert is_valid_int(y)
  881. try:
  882. return ovfcheck(x << y)
  883. except OverflowError:
  884. self.make_llexception()
  885. def _makefunc2(fn, operator, xtype, ytype=None):
  886. import sys
  887. d = sys._getframe(1).f_locals
  888. if ytype is None:
  889. ytype = xtype
  890. if '_ovf' in fn:
  891. checkfn = 'ovfcheck'
  892. elif fn.startswith('op_int_'):
  893. checkfn = 'intmask'
  894. else:
  895. checkfn = ''
  896. if operator == '//':
  897. code = '''r = %(checkfn)s(x // y)
  898. if x^y < 0 and x%%y != 0:
  899. r += 1
  900. return r
  901. '''%locals()
  902. elif operator == '%':
  903. ## overflow check on % does not work with emulated int
  904. code = '''%(checkfn)s(x // y)
  905. r = x %% y
  906. if x^y < 0 and x%%y != 0:
  907. r -= y
  908. return r
  909. '''%locals()
  910. else:
  911. code = 'return %(checkfn)s(x %(operator)s y)'%locals()
  912. exec py.code.Source("""
  913. def %(fn)s(self, x, y):
  914. assert isinstance(x, %(xtype)s)
  915. assert isinstance(y, %(ytype)s)
  916. try:
  917. %(code)s
  918. except (OverflowError, ValueError, ZeroDivisionError):
  919. self.make_llexception()
  920. """ % locals()).compile() in globals(), d
  921. _makefunc2('op_int_add_ovf', '+', '(int, long, llmemory.AddressOffset)')
  922. _makefunc2('op_int_mul_ovf', '*', '(int, long, llmemory.AddressOffset)', '(int, long)')
  923. _makefunc2('op_int_sub_ovf', '-', '(int, long)')
  924. _makefunc2('op_int_floordiv_ovf', '//', '(int, long)') # XXX negative args
  925. _makefunc2('op_int_floordiv_zer', '//', '(int, long)') # can get off-by-one
  926. _makefunc2('op_int_floordiv_ovf_zer', '//', '(int, long)') # (see op_int_floordiv)
  927. _makefunc2('op_int_mod_ovf', '%', '(int, long)')
  928. _makefunc2('op_int_mod_zer', '%', '(int, long)')
  929. _makefunc2('op_int_mod_ovf_zer', '%', '(int, long)')
  930. _makefunc2('op_uint_floordiv_zer', '//', 'r_uint')
  931. _makefunc2('op_uint_mod_zer', '%', 'r_uint')
  932. _makefunc2('op_llong_floordiv_zer', '//', 'r_longlong')
  933. _makefunc2('op_llong_mod_zer', '%', 'r_longlong')
  934. _makefunc2('op_ullong_floordiv_zer', '//', 'r_ulonglong')
  935. _makefunc2('op_ullong_mod_zer', '%', 'r_ulonglong')
  936. _makefunc2('op_lllong_floordiv_zer', '//', 'r_longlonglong')
  937. _makefunc2('op_lllong_mod_zer', '%', 'r_longlonglong')
  938. def op_int_add_nonneg_ovf(self, x, y):
  939. if isinstance(y, int):
  940. assert y >= 0
  941. return self.op_int_add_ovf(x, y)
  942. def op_int_is_true(self, x):
  943. # special case
  944. if type(x) is CDefinedIntSymbolic:
  945. x = x.default
  946. # if type(x) is a subclass of Symbolic, bool(x) will usually raise
  947. # a TypeError -- unless __nonzero__ has been explicitly overridden.
  948. assert is_valid_int(x) or isinstance(x, Symbolic)
  949. return bool(x)
  950. # hack for jit.codegen.llgraph
  951. def op_check_and_clear_exc(self):
  952. exc_data = self.llinterpreter.get_transformed_exc_data(self.graph)
  953. assert exc_data
  954. etype = exc_data.exc_type
  955. evalue = exc_data.exc_value
  956. exc_data.exc_type = lltype.typeOf(etype )._defl()
  957. exc_data.exc_value = lltype.typeOf(evalue)._defl()
  958. return bool(etype)
  959. #Operation of ootype
  960. def op_new(self, INST):
  961. assert isinstance(INST, (ootype.Instance, ootype.BuiltinType))
  962. return ootype.new(INST)
  963. def op_oonewarray(self, ARRAY, length):
  964. assert isinstance(ARRAY, ootype.Array)
  965. assert is_valid_int(length)
  966. return ootype.oonewarray(ARRAY, length)
  967. def op_runtimenew(self, class_):
  968. return ootype.runtimenew(class_)
  969. def op_oonewcustomdict(self, DICT, eq_func, eq_obj, eq_method_name,
  970. hash_func, hash_obj, hash_method_name):
  971. eq_name, interp_eq = \
  972. wrap_callable(self.llinterpreter, eq_func, eq_obj, eq_method_name)
  973. EQ_FUNC = ootype.StaticMethod([DICT._KEYTYPE, DICT._KEYTYPE], ootype.Bool)
  974. sm_eq = ootype.static_meth(EQ_FUNC, eq_name, _callable=interp_eq)
  975. hash_name, interp_hash = \
  976. wrap_callable(self.llinterpreter, hash_func, hash_obj, hash_method_name)
  977. HASH_FUNC = ootype.StaticMethod([DICT._KEYTYPE], ootype.Signed)
  978. sm_hash = ootype.static_meth(HASH_FUNC, hash_name, _callable=interp_hash)
  979. # XXX: is it fine to have StaticMethod type for bound methods, too?
  980. return ootype.oonewcustomdict(DICT, sm_eq, sm_hash)
  981. def op_oosetfield(self, inst, name, value):
  982. checkinst(inst)
  983. assert isinstance(name, str)
  984. FIELDTYPE = lltype.typeOf(inst)._field_type(name)
  985. if FIELDTYPE is not lltype.Void:
  986. setattr(inst, name, value)
  987. def op_oogetfield(self, inst, name):
  988. checkinst(inst)
  989. assert isinstance(name, str)
  990. return getattr(inst, name)
  991. def op_oosend(self, message, inst, *args):
  992. checkinst(inst)
  993. assert isinstance(message, str)
  994. bm = getattr(inst, message)
  995. inst = bm.inst
  996. m = bm.meth
  997. args = m._checkargs(args, check_callable=False)
  998. if getattr(m, 'abstract', False):
  999. raise RuntimeError("calling abstract method %r" % (m,))
  1000. return self.perform_call(m, (lltype.typeOf(inst),)+lltype.typeOf(m).ARGS, [inst]+args)
  1001. def op_oostring(self, obj, base):
  1002. return ootype.oostring(obj, base)
  1003. def op_oounicode(self, obj, base):
  1004. try:
  1005. return ootype.oounicode(obj, base)
  1006. except UnicodeDecodeError:
  1007. self.make_llexception()
  1008. def op_ooparse_int(self, s, base):
  1009. try:
  1010. return ootype.ooparse_int(s, base)
  1011. except ValueError:
  1012. self.make_llexception()
  1013. def op_ooparse_float(self, s):
  1014. try:
  1015. return ootype.ooparse_float(s)
  1016. except ValueError:
  1017. self.make_llexception()
  1018. def op_oobox_int(self, i):
  1019. return ootype.oobox_int(i)
  1020. def op_oounbox_int(self, x):
  1021. return ootype.oounbox_int(x)
  1022. class Tracer(object):
  1023. Counter = 0
  1024. file = None
  1025. TRACE = int(os.getenv('PYPY_TRACE') or '0')
  1026. HEADER = """<html><head>
  1027. <script language=javascript type='text/javascript'>
  1028. function togglestate(n) {
  1029. var item = document.getElementById('div'+n)
  1030. if (item.style.display == 'none')
  1031. item.style.display = 'block';
  1032. else
  1033. item.style.display = 'none';
  1034. }
  1035. function toggleall(lst) {
  1036. for (var i = 0; i<lst.length; i++) {
  1037. togglestate(lst[i]);
  1038. }
  1039. }
  1040. </script>
  1041. </head>
  1042. <body><pre>
  1043. """
  1044. FOOTER = """</pre>
  1045. <script language=javascript type='text/javascript'>
  1046. toggleall(%r);
  1047. </script>
  1048. </body></html>"""
  1049. ENTER = ('''\n\t<a href="javascript:togglestate(%d)">%s</a>'''
  1050. '''\n<div id="div%d" style="display: %s">\t''')
  1051. LEAVE = '''\n</div>\t'''
  1052. def htmlquote(self, s, text_to_html={}):
  1053. # HTML quoting, lazily initialized
  1054. if not text_to_html:
  1055. import htmlentitydefs
  1056. for key, value in htmlentitydefs.entitydefs.items():
  1057. text_to_html[value] = '&' + key + ';'
  1058. return ''.join([text_to_html.get(c, c) for c in s])
  1059. def start(self):
  1060. # start of a dump file
  1061. if not self.TRACE:
  1062. return
  1063. from pypy.tool.udir import udir
  1064. n = Tracer.Counter
  1065. Tracer.Counter += 1
  1066. filename = 'llinterp_trace_%d.html' % n
  1067. self.file = udir.join(filename).open('w')
  1068. print >> self.file, self.HEADER
  1069. linkname = str(udir.join('llinterp_trace.html'))
  1070. try:
  1071. os.unlink(linkname)
  1072. except OSError:
  1073. pass
  1074. try:
  1075. os.symlink(filename, linkname)
  1076. except (AttributeError, OSError):
  1077. pass
  1078. self.count = 0
  1079. self.indentation = ''
  1080. self.depth = 0
  1081. self.latest_call_chain = []
  1082. def stop(self):
  1083. # end of a dump file
  1084. if self.file:
  1085. print >> self.file, self.FOOTER % (self.latest_call_chain[1:])
  1086. self.file.close()
  1087. self.file = None
  1088. def enter(self, graph):
  1089. # enter evaluation of a graph
  1090. if self.file:
  1091. del self.latest_call_chain[self.depth:]
  1092. self.depth += 1
  1093. self.latest_call_chain.append(self.count)
  1094. s = self.htmlquote(str(graph))
  1095. i = s.rfind(')')
  1096. s = s[:i+1] + '<b>' + s[i+1:] + '</b>'
  1097. if self.count == 0:
  1098. display = 'block'
  1099. else:
  1100. display = 'none'
  1101. text = self.ENTER % (self.count, s, self.count, display)
  1102. self.indentation += ' '
  1103. self.file.write(text.replace('\t', self.indentation))
  1104. self.count += 1
  1105. def leave(self):
  1106. # leave evaluation of a graph
  1107. if self.file:
  1108. self.indentation = self.indentation[:-4]
  1109. self.file.write(self.LEAVE.replace('\t', self.indentation))
  1110. self.depth -= 1
  1111. def dump(self, text, bold=False):
  1112. if self.file:
  1113. text = self.htmlquote(text)
  1114. if bold:
  1115. text = '<b>%s</b>' % (text,)
  1116. self.file.write(text.replace('\n', '\n'+self.indentation))
  1117. def flush(self):
  1118. if self.file:
  1119. self.file.flush()
  1120. def wrap_callable(llinterpreter, fn, obj, method_name):
  1121. if method_name is None:
  1122. # fn is a StaticMethod
  1123. if obj is not None:
  1124. self_arg = [obj]
  1125. else:
  1126. self_arg = []
  1127. func_graph = fn.graph
  1128. else:
  1129. # obj is an instance, we want to call 'method_name' on it
  1130. assert fn is None
  1131. self_arg = [obj]
  1132. func_graph = obj._TYPE._methods[method_name._str].graph
  1133. return wrap_graph(llinterpreter, func_graph, self_arg)
  1134. def wrap_graph(llinterpreter, graph, self_arg):
  1135. """
  1136. Returns a callable that inteprets the given func or method_name when called.
  1137. """
  1138. def interp_func(*args):
  1139. graph_args = self_arg + list(args)
  1140. return llinterpreter.eval_graph(graph, args=graph_args)
  1141. interp_func.graph = graph
  1142. interp_func.self_arg = self_arg
  1143. return graph.name, interp_func
  1144. def enumerate_exceptions_top_down():
  1145. import exceptions
  1146. result = []
  1147. seen = {}
  1148. def addcls(cls):
  1149. if (type(cls) is type(Exception) and
  1150. issubclass(cls, py.builtin.BaseException)):
  1151. if cls in seen:
  1152. return
  1153. for base in cls.__bases__: # bases first
  1154. addcls(base)
  1155. result.append(cls)
  1156. seen[cls] = True
  1157. for cls in exceptions.__dict__.values():
  1158. addcls(cls)
  1159. return result
  1160. class _address_of_local_var(object):
  1161. _TYPE = llmemory.Address
  1162. def __init__(self, frame

Large files files are truncated, but you can click here to view the full file