| 1 | from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 | from pypy.module._hpy_universal import llapi |
| 3 | from pypy.module._hpy_universal.apiset import APISet |
| 4 | |
| 5 | # =============== HPy-RPython BRIDGE =============== |
| 6 | # |
| 7 | # Semi-complicate (but hopefully not too magic!) machinery to make it possible |
| 8 | # to call RPython functions from C in a way which works both before and after |
| 9 | # translation: |
| 10 | # |
| 11 | # - during tests, the code runs on top of CPython, so we need ll2ctypes |
| 12 | # callbacks: in bridge.h, a set of macros turn e.g. a call to foo() into |
| 13 | # hpy_get_bridge()->foo() |
| 14 | # |
| 15 | # - after translations, we want a direct call to the generated C functions |
| 16 | |
| 17 | # NOTE: for bridge functions which take arguments of type "HPy", one more hack |
| 18 | # is needed. See the comment in bridge.h |
| 19 | |
| 20 | # Adding a new bridge function is annoying because it involves modifying few |
| 21 | # different places: in theory it could be automated, but as long as the number |
| 22 | # of bridge function remains manageable, it is better to avoid adding |
| 23 | # unnecessary magic. To add a new bridge function you need: |
| 24 | # |
| 25 | # 1. add the @BRIDGE.func decorator to your RPython function |
| 26 | # 2. add the corresponding field in _HPyBridge here |
| 27 | # 3. inside src/bridge.h: |
| 28 | # a) add the corresponding field in _HPyBridge |
| 29 | # b) write a macro for the RPYTHON_LL2CTYPES case |
| 30 | # c) write the function prototype for the non-RPYTHON_LL2CTYPES case |
| 31 | # d) if the func recevies HPy arguments, write a macro to convert them |
| 32 | |
| 33 | llapi.cts.parse_source(""" |
| 34 | typedef struct { |
| 35 | void * hpy_err_Occurred_rpy; |
| 36 | void * hpy_err_Clear; |
| 37 | } _HPyBridge; |
| 38 | """) |
| 39 | |
| 40 | _HPyBridge = llapi.cts.gettype('_HPyBridge') |
| 41 | hpy_get_bridge = rffi.llexternal('hpy_get_bridge', [], lltype.Ptr(_HPyBridge), |
| 42 | compilation_info=llapi.eci, _nowrapper=True) |
| 43 | |
| 44 | BRIDGE = APISet(llapi.cts, mode="universal", prefix='^hpy_', force_c_name=True) |