/JIT/PyTypeBuilder.h

http://unladen-swallow.googlecode.com/ · C Header · 663 lines · 542 code · 66 blank · 55 comment · 2 complexity · bdbe954ba155be74c1b2eeb64afdd33e MD5 · raw file

  1. // -*- C++ -*-
  2. #ifndef UTIL_PYTYPEBUILDER_H
  3. #define UTIL_PYTYPEBUILDER_H
  4. #ifndef __cplusplus
  5. #error This header expects to be included only in C++ source
  6. #endif
  7. #include "Python.h"
  8. #include "code.h"
  9. #include "frameobject.h"
  10. #include "longintrepr.h"
  11. #include "JIT/global_llvm_data.h"
  12. #include "llvm/Module.h"
  13. #include "llvm/Support/IRBuilder.h"
  14. #include "llvm/Support/TypeBuilder.h"
  15. struct PyExcInfo;
  16. // llvm::TypeBuilder requires a boolean parameter specifying whether
  17. // the type needs to be cross-compilable. In Python, we don't need
  18. // anything to be cross-compilable (the VM is by definition running on
  19. // the platform it's generating code for), so we define PyTypeBuilder
  20. // to hard-code that parameter to false.
  21. template<typename T>
  22. class PyTypeBuilder : public llvm::TypeBuilder<T, false> {};
  23. // This function uses the JIT compiler's TargetData object to convert
  24. // from a byte offset inside a type to a GEP index referring to the
  25. // field of the type. This should be called like
  26. // _PyTypeBuilder_GetFieldIndexFromOffset(
  27. // PyTypeBuilder<PySomethingType>::get(context),
  28. // offsetof(PySomethingType, field));
  29. // It will only work if PySomethingType is a POD type.
  30. unsigned int
  31. _PyTypeBuilder_GetFieldIndexFromOffset(
  32. const llvm::StructType *type, size_t offset);
  33. // Enter the LLVM namespace in order to add specializations of
  34. // llvm::TypeBuilder.
  35. namespace llvm {
  36. // Defines a static member function FIELD_NAME(ir_builder, ptr) to
  37. // access TYPE::FIELD_NAME inside ptr. GetElementPtr instructions
  38. // require the index of the field within the type, but padding makes
  39. // it hard to predict that index from the list of fields in the type.
  40. // Because the compiler building this file knows the byte offset of
  41. // the field, we can use llvm::TargetData to compute the index. This
  42. // has the extra benefit that it's more resilient to changes in the
  43. // set or order of fields in a type.
  44. #define DEFINE_FIELD(TYPE, FIELD_NAME) \
  45. static unsigned FIELD_NAME##_index(LLVMContext &context) { \
  46. static const unsigned int index = \
  47. _PyTypeBuilder_GetFieldIndexFromOffset( \
  48. PyTypeBuilder<TYPE>::get(context), \
  49. offsetof(TYPE, FIELD_NAME)); \
  50. return index; \
  51. } \
  52. template<bool preserveNames, typename Folder> \
  53. static Value *FIELD_NAME(IRBuilder<preserveNames, Folder> &builder, \
  54. Value *ptr) { \
  55. assert(ptr->getType() == PyTypeBuilder<TYPE*>::get(ptr->getContext()) \
  56. && "*ptr must be of type " #TYPE); \
  57. return builder.CreateStructGEP( \
  58. ptr, FIELD_NAME##_index(ptr->getContext()), #FIELD_NAME); \
  59. }
  60. #ifdef Py_TRACE_REFS
  61. #define DEFINE_OBJECT_HEAD_FIELDS(TYPE) \
  62. DEFINE_FIELD(TYPE, _ob_next) \
  63. DEFINE_FIELD(TYPE, _ob_prev) \
  64. DEFINE_FIELD(TYPE, ob_refcnt) \
  65. DEFINE_FIELD(TYPE, ob_type)
  66. #else
  67. #define DEFINE_OBJECT_HEAD_FIELDS(TYPE) \
  68. DEFINE_FIELD(TYPE, ob_refcnt) \
  69. DEFINE_FIELD(TYPE, ob_type)
  70. #endif
  71. template<> class TypeBuilder<PyObject, false> {
  72. public:
  73. static const StructType *get(llvm::LLVMContext &context) {
  74. return cast<StructType>(
  75. PyGlobalLlvmData::Get()->module()->getTypeByName(
  76. // Clang's name for the PyObject struct.
  77. "struct._object"));
  78. }
  79. DEFINE_OBJECT_HEAD_FIELDS(PyObject)
  80. };
  81. template<> class TypeBuilder<PyVarObject, false> {
  82. public:
  83. static const StructType *get(llvm::LLVMContext &context) {
  84. return cast<StructType>(
  85. PyGlobalLlvmData::Get()->module()->getTypeByName(
  86. // Clang's name for the PyVarObject struct.
  87. "struct.PyVarObject"));
  88. }
  89. DEFINE_OBJECT_HEAD_FIELDS(PyObject)
  90. DEFINE_FIELD(PyVarObject, ob_size)
  91. };
  92. template<> class TypeBuilder<PyStringObject, false> {
  93. public:
  94. static const StructType *get(llvm::LLVMContext &context) {
  95. return cast<StructType>(
  96. PyGlobalLlvmData::Get()->module()->getTypeByName(
  97. // Clang's name for the PyStringObject struct.
  98. "struct.PyStringObject"));
  99. }
  100. DEFINE_OBJECT_HEAD_FIELDS(PyStringObject)
  101. DEFINE_FIELD(PyStringObject, ob_size)
  102. DEFINE_FIELD(PyStringObject, ob_shash)
  103. DEFINE_FIELD(PyStringObject, ob_sstate)
  104. DEFINE_FIELD(PyStringObject, ob_sval)
  105. };
  106. template<> class TypeBuilder<PyUnicodeObject, false> {
  107. public:
  108. static const StructType *get(llvm::LLVMContext &context) {
  109. return cast<StructType>(
  110. PyGlobalLlvmData::Get()->module()->getTypeByName(
  111. // Clang's name for the PyUnicodeObject struct.
  112. "struct.PyUnicodeObject"));
  113. }
  114. DEFINE_OBJECT_HEAD_FIELDS(PyUnicodeObject)
  115. DEFINE_FIELD(PyUnicodeObject, length)
  116. DEFINE_FIELD(PyUnicodeObject, str)
  117. DEFINE_FIELD(PyUnicodeObject, hash)
  118. DEFINE_FIELD(PyUnicodeObject, defenc)
  119. };
  120. template<> class TypeBuilder<PyIntObject, false> {
  121. public:
  122. static const StructType *get(llvm::LLVMContext &context) {
  123. return cast<StructType>(
  124. PyGlobalLlvmData::Get()->module()->getTypeByName(
  125. // Clang's name for the PyIntObject struct.
  126. "struct.PyIntObject"));
  127. }
  128. DEFINE_OBJECT_HEAD_FIELDS(PyIntObject)
  129. DEFINE_FIELD(PyIntObject, ob_ival)
  130. };
  131. template<> class TypeBuilder<PyLongObject, false> {
  132. public:
  133. static const StructType *get(llvm::LLVMContext &context) {
  134. return cast<StructType>(
  135. PyGlobalLlvmData::Get()->module()->getTypeByName(
  136. // Clang's name for the PyLongObject struct.
  137. "struct._longobject"));
  138. }
  139. DEFINE_OBJECT_HEAD_FIELDS(PyLongObject)
  140. DEFINE_FIELD(PyLongObject, ob_digit)
  141. };
  142. template<> class TypeBuilder<PyFloatObject, false> {
  143. public:
  144. static const StructType *get(llvm::LLVMContext &context) {
  145. return cast<StructType>(
  146. PyGlobalLlvmData::Get()->module()->getTypeByName(
  147. // Clang's name for the PyFloatObject struct.
  148. "struct.PyFloatObject"));
  149. }
  150. DEFINE_OBJECT_HEAD_FIELDS(PyFloatObject)
  151. DEFINE_FIELD(PyFloatObject, ob_fval)
  152. };
  153. template<> class TypeBuilder<PyComplexObject, false> {
  154. public:
  155. static const StructType *get(llvm::LLVMContext &context) {
  156. return cast<StructType>(
  157. PyGlobalLlvmData::Get()->module()->getTypeByName(
  158. // Clang's name for the PyFloatObject struct.
  159. "struct.PyComplexObject"));
  160. }
  161. DEFINE_OBJECT_HEAD_FIELDS(PyComplexObject)
  162. DEFINE_FIELD(PyComplexObject, cval)
  163. };
  164. template<> class TypeBuilder<PyTupleObject, false> {
  165. public:
  166. static const StructType *get(llvm::LLVMContext &context) {
  167. return cast<StructType>(
  168. PyGlobalLlvmData::Get()->module()->getTypeByName(
  169. // Clang's name for the PyTupleObject struct.
  170. "struct.PyTupleObject"));
  171. }
  172. DEFINE_OBJECT_HEAD_FIELDS(PyTupleObject)
  173. DEFINE_FIELD(PyTupleObject, ob_size)
  174. DEFINE_FIELD(PyTupleObject, ob_item)
  175. };
  176. template<> class TypeBuilder<PyListObject, false> {
  177. public:
  178. static const StructType *get(llvm::LLVMContext &context) {
  179. return cast<StructType>(
  180. PyGlobalLlvmData::Get()->module()->getTypeByName(
  181. // Clang's name for the PyListObject struct.
  182. "struct.PyListObject"));
  183. }
  184. DEFINE_OBJECT_HEAD_FIELDS(PyListObject)
  185. DEFINE_FIELD(PyListObject, ob_size)
  186. DEFINE_FIELD(PyListObject, ob_item)
  187. DEFINE_FIELD(PyListObject, allocated)
  188. };
  189. template<> class TypeBuilder<PyTypeObject, false> {
  190. public:
  191. static const StructType *get(llvm::LLVMContext &context) {
  192. return cast<StructType>(
  193. PyGlobalLlvmData::Get()->module()->getTypeByName(
  194. // Clang's name for the PyTypeObject struct.
  195. "struct._typeobject"));
  196. }
  197. DEFINE_OBJECT_HEAD_FIELDS(PyTypeObject)
  198. DEFINE_FIELD(PyTypeObject, ob_size)
  199. DEFINE_FIELD(PyTypeObject, tp_name)
  200. DEFINE_FIELD(PyTypeObject, tp_basicsize)
  201. DEFINE_FIELD(PyTypeObject, tp_itemsize)
  202. DEFINE_FIELD(PyTypeObject, tp_dealloc)
  203. DEFINE_FIELD(PyTypeObject, tp_print)
  204. DEFINE_FIELD(PyTypeObject, tp_getattr)
  205. DEFINE_FIELD(PyTypeObject, tp_setattr)
  206. DEFINE_FIELD(PyTypeObject, tp_compare)
  207. DEFINE_FIELD(PyTypeObject, tp_repr)
  208. DEFINE_FIELD(PyTypeObject, tp_as_number)
  209. DEFINE_FIELD(PyTypeObject, tp_as_sequence)
  210. DEFINE_FIELD(PyTypeObject, tp_as_mapping)
  211. DEFINE_FIELD(PyTypeObject, tp_hash)
  212. DEFINE_FIELD(PyTypeObject, tp_call)
  213. DEFINE_FIELD(PyTypeObject, tp_str)
  214. DEFINE_FIELD(PyTypeObject, tp_getattro)
  215. DEFINE_FIELD(PyTypeObject, tp_setattro)
  216. DEFINE_FIELD(PyTypeObject, tp_as_buffer)
  217. DEFINE_FIELD(PyTypeObject, tp_flags)
  218. DEFINE_FIELD(PyTypeObject, tp_doc)
  219. DEFINE_FIELD(PyTypeObject, tp_traverse)
  220. DEFINE_FIELD(PyTypeObject, tp_clear)
  221. DEFINE_FIELD(PyTypeObject, tp_richcompare)
  222. DEFINE_FIELD(PyTypeObject, tp_weaklistoffset)
  223. DEFINE_FIELD(PyTypeObject, tp_iter)
  224. DEFINE_FIELD(PyTypeObject, tp_iternext)
  225. DEFINE_FIELD(PyTypeObject, tp_methods)
  226. DEFINE_FIELD(PyTypeObject, tp_members)
  227. DEFINE_FIELD(PyTypeObject, tp_getset)
  228. DEFINE_FIELD(PyTypeObject, tp_base)
  229. DEFINE_FIELD(PyTypeObject, tp_dict)
  230. DEFINE_FIELD(PyTypeObject, tp_descr_get)
  231. DEFINE_FIELD(PyTypeObject, tp_descr_set)
  232. DEFINE_FIELD(PyTypeObject, tp_dictoffset)
  233. DEFINE_FIELD(PyTypeObject, tp_init)
  234. DEFINE_FIELD(PyTypeObject, tp_alloc)
  235. DEFINE_FIELD(PyTypeObject, tp_new)
  236. DEFINE_FIELD(PyTypeObject, tp_free)
  237. DEFINE_FIELD(PyTypeObject, tp_is_gc)
  238. DEFINE_FIELD(PyTypeObject, tp_bases)
  239. DEFINE_FIELD(PyTypeObject, tp_mro)
  240. DEFINE_FIELD(PyTypeObject, tp_cache)
  241. DEFINE_FIELD(PyTypeObject, tp_subclasses)
  242. DEFINE_FIELD(PyTypeObject, tp_weaklist)
  243. DEFINE_FIELD(PyTypeObject, tp_del)
  244. DEFINE_FIELD(PyTypeObject, tp_version_tag)
  245. #ifdef COUNT_ALLOCS
  246. DEFINE_FIELD(PyTypeObject, tp_allocs)
  247. DEFINE_FIELD(PyTypeObject, tp_frees)
  248. DEFINE_FIELD(PyTypeObject, tp_maxalloc)
  249. DEFINE_FIELD(PyTypeObject, tp_prev)
  250. DEFINE_FIELD(PyTypeObject, tp_next)
  251. #endif
  252. };
  253. template<> class TypeBuilder<PyNumberMethods, false> {
  254. public:
  255. static const StructType *get(llvm::LLVMContext &context) {
  256. return cast<StructType>(
  257. PyGlobalLlvmData::Get()->module()->getTypeByName(
  258. // Clang's name for the PyTypeObject struct.
  259. "struct.PyNumberMethods"));
  260. }
  261. DEFINE_FIELD(PyNumberMethods, nb_add)
  262. DEFINE_FIELD(PyNumberMethods, nb_subtract)
  263. DEFINE_FIELD(PyNumberMethods, nb_multiply)
  264. DEFINE_FIELD(PyNumberMethods, nb_divide)
  265. DEFINE_FIELD(PyNumberMethods, nb_remainder)
  266. DEFINE_FIELD(PyNumberMethods, nb_divmod)
  267. DEFINE_FIELD(PyNumberMethods, nb_power)
  268. DEFINE_FIELD(PyNumberMethods, nb_negative)
  269. DEFINE_FIELD(PyNumberMethods, nb_positive)
  270. DEFINE_FIELD(PyNumberMethods, nb_absolute)
  271. DEFINE_FIELD(PyNumberMethods, nb_nonzero)
  272. DEFINE_FIELD(PyNumberMethods, nb_invert)
  273. DEFINE_FIELD(PyNumberMethods, nb_lshift)
  274. DEFINE_FIELD(PyNumberMethods, nb_rshift)
  275. DEFINE_FIELD(PyNumberMethods, nb_and)
  276. DEFINE_FIELD(PyNumberMethods, nb_xor)
  277. DEFINE_FIELD(PyNumberMethods, nb_or)
  278. DEFINE_FIELD(PyNumberMethods, nb_coerce)
  279. DEFINE_FIELD(PyNumberMethods, nb_int)
  280. DEFINE_FIELD(PyNumberMethods, nb_long)
  281. DEFINE_FIELD(PyNumberMethods, nb_float)
  282. DEFINE_FIELD(PyNumberMethods, nb_oct)
  283. DEFINE_FIELD(PyNumberMethods, nb_hex)
  284. DEFINE_FIELD(PyNumberMethods, nb_inplace_add)
  285. DEFINE_FIELD(PyNumberMethods, nb_inplace_subtract)
  286. DEFINE_FIELD(PyNumberMethods, nb_inplace_multiply)
  287. DEFINE_FIELD(PyNumberMethods, nb_inplace_divide)
  288. DEFINE_FIELD(PyNumberMethods, nb_inplace_remainder)
  289. DEFINE_FIELD(PyNumberMethods, nb_inplace_power)
  290. DEFINE_FIELD(PyNumberMethods, nb_inplace_lshift)
  291. DEFINE_FIELD(PyNumberMethods, nb_inplace_rshift)
  292. DEFINE_FIELD(PyNumberMethods, nb_inplace_and)
  293. DEFINE_FIELD(PyNumberMethods, nb_inplace_xor)
  294. DEFINE_FIELD(PyNumberMethods, nb_inplace_or)
  295. DEFINE_FIELD(PyNumberMethods, nb_floor_divide)
  296. DEFINE_FIELD(PyNumberMethods, nb_true_divide)
  297. DEFINE_FIELD(PyNumberMethods, nb_inplace_floor_divide)
  298. DEFINE_FIELD(PyNumberMethods, nb_inplace_true_divide)
  299. DEFINE_FIELD(PyNumberMethods, nb_index)
  300. };
  301. template<> class TypeBuilder<PySequenceMethods, false> {
  302. public:
  303. static const StructType *get(llvm::LLVMContext &context) {
  304. return cast<StructType>(
  305. PyGlobalLlvmData::Get()->module()->getTypeByName(
  306. // Clang's name for the PyTypeObject struct.
  307. "struct.PySequenceMethods"));
  308. }
  309. DEFINE_FIELD(PySequenceMethods, sq_length)
  310. DEFINE_FIELD(PySequenceMethods, sq_concat)
  311. DEFINE_FIELD(PySequenceMethods, sq_repeat)
  312. DEFINE_FIELD(PySequenceMethods, sq_item)
  313. DEFINE_FIELD(PySequenceMethods, sq_slice)
  314. DEFINE_FIELD(PySequenceMethods, sq_ass_item)
  315. DEFINE_FIELD(PySequenceMethods, sq_ass_slice)
  316. DEFINE_FIELD(PySequenceMethods, sq_contains)
  317. DEFINE_FIELD(PySequenceMethods, sq_inplace_concat)
  318. DEFINE_FIELD(PySequenceMethods, sq_inplace_repeat)
  319. };
  320. template<> class TypeBuilder<PyMappingMethods, false> {
  321. public:
  322. static const StructType *get(llvm::LLVMContext &context) {
  323. return cast<StructType>(
  324. PyGlobalLlvmData::Get()->module()->getTypeByName(
  325. // Clang's name for the PyTypeObject struct.
  326. "struct.PyMappingMethods"));
  327. }
  328. // No fields yet because we haven't needed them.
  329. };
  330. template<> class TypeBuilder<PyBufferProcs, false> {
  331. public:
  332. static const StructType *get(llvm::LLVMContext &context) {
  333. return cast<StructType>(
  334. PyGlobalLlvmData::Get()->module()->getTypeByName(
  335. // Clang's name for the PyTypeObject struct.
  336. "struct.PyBufferProcs"));
  337. }
  338. // No fields yet because we haven't needed them.
  339. };
  340. template<> class TypeBuilder<PyCodeObject, false> {
  341. public:
  342. static const StructType *get(llvm::LLVMContext &context) {
  343. return cast<StructType>(
  344. PyGlobalLlvmData::Get()->module()->getTypeByName(
  345. // Clang's name for the PyCodeObject struct.
  346. "struct.PyCodeObject"));
  347. }
  348. DEFINE_OBJECT_HEAD_FIELDS(PyCodeObject)
  349. DEFINE_FIELD(PyCodeObject, co_argcount)
  350. DEFINE_FIELD(PyCodeObject, co_nlocals)
  351. DEFINE_FIELD(PyCodeObject, co_stacksize)
  352. DEFINE_FIELD(PyCodeObject, co_flags)
  353. DEFINE_FIELD(PyCodeObject, co_code)
  354. DEFINE_FIELD(PyCodeObject, co_consts)
  355. DEFINE_FIELD(PyCodeObject, co_names)
  356. DEFINE_FIELD(PyCodeObject, co_varnames)
  357. DEFINE_FIELD(PyCodeObject, co_freevars)
  358. DEFINE_FIELD(PyCodeObject, co_cellvars)
  359. DEFINE_FIELD(PyCodeObject, co_filename)
  360. DEFINE_FIELD(PyCodeObject, co_name)
  361. DEFINE_FIELD(PyCodeObject, co_firstlineno)
  362. DEFINE_FIELD(PyCodeObject, co_lnotab)
  363. DEFINE_FIELD(PyCodeObject, co_zombieframe)
  364. DEFINE_FIELD(PyCodeObject, co_llvm_function)
  365. DEFINE_FIELD(PyCodeObject, co_native_function)
  366. DEFINE_FIELD(PyCodeObject, co_use_jit)
  367. DEFINE_FIELD(PyCodeObject, co_optimization)
  368. DEFINE_FIELD(PyCodeObject, co_fatalbailcount)
  369. DEFINE_FIELD(PyCodeObject, co_hotness)
  370. DEFINE_FIELD(PyCodeObject, co_watching)
  371. };
  372. template<> class TypeBuilder<PyFunctionObject, false> {
  373. public:
  374. static const StructType *get(llvm::LLVMContext &context) {
  375. return cast<StructType>(
  376. PyGlobalLlvmData::Get()->module()->getTypeByName(
  377. // Clang's name for the PyFunctionObject struct.
  378. "struct.PyFunctionObject"));
  379. }
  380. DEFINE_OBJECT_HEAD_FIELDS(PyFunctionObject)
  381. DEFINE_FIELD(PyFunctionObject, func_code)
  382. DEFINE_FIELD(PyFunctionObject, func_globals)
  383. DEFINE_FIELD(PyFunctionObject, func_defaults)
  384. DEFINE_FIELD(PyFunctionObject, func_closure)
  385. DEFINE_FIELD(PyFunctionObject, func_doc)
  386. DEFINE_FIELD(PyFunctionObject, func_name)
  387. DEFINE_FIELD(PyFunctionObject, func_dict)
  388. DEFINE_FIELD(PyFunctionObject, func_weakreflist)
  389. DEFINE_FIELD(PyFunctionObject, func_module)
  390. };
  391. template<> class TypeBuilder<PyMethodObject, false> {
  392. public:
  393. static const StructType *get(llvm::LLVMContext &context) {
  394. return cast<StructType>(
  395. PyGlobalLlvmData::Get()->module()->getTypeByName(
  396. // Clang's name for the PyFunctionObject struct.
  397. "struct.PyMethodObject"));
  398. }
  399. DEFINE_OBJECT_HEAD_FIELDS(PyMethodObject)
  400. DEFINE_FIELD(PyMethodObject, im_func)
  401. DEFINE_FIELD(PyMethodObject, im_self)
  402. DEFINE_FIELD(PyMethodObject, im_class)
  403. DEFINE_FIELD(PyMethodObject, im_weakreflist)
  404. };
  405. template<> class TypeBuilder<PyTryBlock, false> {
  406. public:
  407. static const StructType *get(llvm::LLVMContext &context) {
  408. return cast<StructType>(
  409. PyGlobalLlvmData::Get()->module()->getTypeByName(
  410. // Clang's name for the PyTryBlock struct.
  411. "struct.PyTryBlock"));
  412. }
  413. DEFINE_FIELD(PyTryBlock, b_type)
  414. DEFINE_FIELD(PyTryBlock, b_handler)
  415. DEFINE_FIELD(PyTryBlock, b_level)
  416. };
  417. template<> class TypeBuilder<PyFrameObject, false> {
  418. public:
  419. static const StructType *get(llvm::LLVMContext &context) {
  420. return cast<StructType>(
  421. PyGlobalLlvmData::Get()->module()->getTypeByName(
  422. // Clang's name for the PyFrameObject struct.
  423. "struct._frame"));
  424. }
  425. DEFINE_OBJECT_HEAD_FIELDS(PyFrameObject)
  426. DEFINE_FIELD(PyFrameObject, ob_size)
  427. DEFINE_FIELD(PyFrameObject, f_back)
  428. DEFINE_FIELD(PyFrameObject, f_code)
  429. DEFINE_FIELD(PyFrameObject, f_builtins)
  430. DEFINE_FIELD(PyFrameObject, f_globals)
  431. DEFINE_FIELD(PyFrameObject, f_locals)
  432. DEFINE_FIELD(PyFrameObject, f_valuestack)
  433. DEFINE_FIELD(PyFrameObject, f_stacktop)
  434. DEFINE_FIELD(PyFrameObject, f_trace)
  435. DEFINE_FIELD(PyFrameObject, f_exc_type)
  436. DEFINE_FIELD(PyFrameObject, f_exc_value)
  437. DEFINE_FIELD(PyFrameObject, f_exc_traceback)
  438. DEFINE_FIELD(PyFrameObject, f_tstate)
  439. DEFINE_FIELD(PyFrameObject, f_lasti)
  440. DEFINE_FIELD(PyFrameObject, f_use_jit)
  441. DEFINE_FIELD(PyFrameObject, f_lineno)
  442. DEFINE_FIELD(PyFrameObject, f_throwflag)
  443. DEFINE_FIELD(PyFrameObject, f_iblock)
  444. DEFINE_FIELD(PyFrameObject, f_bailed_from_llvm)
  445. DEFINE_FIELD(PyFrameObject, f_guard_type)
  446. DEFINE_FIELD(PyFrameObject, f_blockstack)
  447. DEFINE_FIELD(PyFrameObject, f_localsplus)
  448. };
  449. template<> class TypeBuilder<PyExcInfo, false> {
  450. public:
  451. static const StructType *get(llvm::LLVMContext &context) {
  452. return cast<StructType>(
  453. PyGlobalLlvmData::Get()->module()->getTypeByName(
  454. // Clang's name for the PyExcInfo struct
  455. // defined in llvm_inline_functions.c.
  456. "struct.PyExcInfo"));
  457. }
  458. // We use an enum here because PyExcInfo isn't defined in a header.
  459. enum Fields {
  460. FIELD_EXC,
  461. FIELD_VAL,
  462. FIELD_TB,
  463. };
  464. };
  465. template<> class TypeBuilder<PyThreadState, false> {
  466. public:
  467. static const StructType *get(llvm::LLVMContext &context) {
  468. return cast<StructType>(
  469. PyGlobalLlvmData::Get()->module()->getTypeByName(
  470. // Clang's name for the PyThreadState struct.
  471. "struct._ts"));
  472. }
  473. DEFINE_FIELD(PyThreadState, next)
  474. DEFINE_FIELD(PyThreadState, interp)
  475. DEFINE_FIELD(PyThreadState, frame)
  476. DEFINE_FIELD(PyThreadState, recursion_depth)
  477. DEFINE_FIELD(PyThreadState, tracing)
  478. DEFINE_FIELD(PyThreadState, use_tracing)
  479. DEFINE_FIELD(PyThreadState, c_profilefunc)
  480. DEFINE_FIELD(PyThreadState, c_tracefunc)
  481. DEFINE_FIELD(PyThreadState, c_profileobj)
  482. DEFINE_FIELD(PyThreadState, c_traceobj)
  483. DEFINE_FIELD(PyThreadState, curexc_type)
  484. DEFINE_FIELD(PyThreadState, curexc_value)
  485. DEFINE_FIELD(PyThreadState, curexc_traceback)
  486. DEFINE_FIELD(PyThreadState, exc_type)
  487. DEFINE_FIELD(PyThreadState, exc_value)
  488. DEFINE_FIELD(PyThreadState, exc_traceback)
  489. DEFINE_FIELD(PyThreadState, dict)
  490. DEFINE_FIELD(PyThreadState, tick_counter)
  491. DEFINE_FIELD(PyThreadState, gilstate_counter)
  492. DEFINE_FIELD(PyThreadState, async_exc)
  493. DEFINE_FIELD(PyThreadState, thread_id)
  494. };
  495. template<> class TypeBuilder<PyCFunctionObject, false> {
  496. public:
  497. static const StructType *get(llvm::LLVMContext &context) {
  498. static const StructType *const result =
  499. cast<StructType>(PyGlobalLlvmData::Get()->module()->getTypeByName(
  500. // Clang's name for the PyCFunctionObject
  501. // struct.
  502. "struct.PyCFunctionObject"));
  503. return result;
  504. }
  505. DEFINE_OBJECT_HEAD_FIELDS(PyCFunctionObject)
  506. DEFINE_FIELD(PyCFunctionObject, m_ml)
  507. DEFINE_FIELD(PyCFunctionObject, m_self)
  508. DEFINE_FIELD(PyCFunctionObject, m_module)
  509. };
  510. template<> class TypeBuilder<PyMethodDescrObject, false> {
  511. public:
  512. static const StructType *get(llvm::LLVMContext &context) {
  513. static const StructType *const result =
  514. cast<StructType>(PyGlobalLlvmData::Get()->module()->getTypeByName(
  515. // Clang's name for the PyMethodDescrObject struct.
  516. "struct.PyMethodDescrObject"));
  517. return result;
  518. }
  519. DEFINE_OBJECT_HEAD_FIELDS(PyMethodDescrObject)
  520. DEFINE_FIELD(PyMethodDescrObject, d_type)
  521. DEFINE_FIELD(PyMethodDescrObject, d_name)
  522. DEFINE_FIELD(PyMethodDescrObject, d_method)
  523. };
  524. template<> class TypeBuilder<PyMethodDef, false> {
  525. public:
  526. static const StructType *get(llvm::LLVMContext &context) {
  527. static const StructType *const result =
  528. cast<StructType>(PyGlobalLlvmData::Get()->module()->getTypeByName(
  529. // Clang's name for the PyMethodDef struct.
  530. "struct.PyMethodDef"));
  531. return result;
  532. }
  533. DEFINE_FIELD(PyMethodDef, ml_name)
  534. DEFINE_FIELD(PyMethodDef, ml_meth)
  535. DEFINE_FIELD(PyMethodDef, ml_flags)
  536. DEFINE_FIELD(PyMethodDef, ml_doc)
  537. };
  538. // We happen to have two functions with these types, so we must define type
  539. // builder specializations for them.
  540. template<typename R, typename A1, typename A2, typename A3, typename A4,
  541. typename A5, typename A6, typename A7, bool cross>
  542. class TypeBuilder<R(A1, A2, A3, A4, A5, A6, A7), cross> {
  543. public:
  544. static const FunctionType *get(llvm::LLVMContext &Context) {
  545. std::vector<const Type*> params;
  546. params.reserve(7);
  547. params.push_back(TypeBuilder<A1, cross>::get(Context));
  548. params.push_back(TypeBuilder<A2, cross>::get(Context));
  549. params.push_back(TypeBuilder<A3, cross>::get(Context));
  550. params.push_back(TypeBuilder<A4, cross>::get(Context));
  551. params.push_back(TypeBuilder<A5, cross>::get(Context));
  552. params.push_back(TypeBuilder<A6, cross>::get(Context));
  553. params.push_back(TypeBuilder<A7, cross>::get(Context));
  554. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  555. params, false);
  556. }
  557. };
  558. template<typename R, typename A1, typename A2, typename A3, typename A4,
  559. typename A5, typename A6, typename A7, typename A8, bool cross>
  560. class TypeBuilder<R(A1, A2, A3, A4, A5, A6, A7, A8), cross> {
  561. public:
  562. static const FunctionType *get(llvm::LLVMContext &Context) {
  563. std::vector<const Type*> params;
  564. params.reserve(8);
  565. params.push_back(TypeBuilder<A1, cross>::get(Context));
  566. params.push_back(TypeBuilder<A2, cross>::get(Context));
  567. params.push_back(TypeBuilder<A3, cross>::get(Context));
  568. params.push_back(TypeBuilder<A4, cross>::get(Context));
  569. params.push_back(TypeBuilder<A5, cross>::get(Context));
  570. params.push_back(TypeBuilder<A6, cross>::get(Context));
  571. params.push_back(TypeBuilder<A7, cross>::get(Context));
  572. params.push_back(TypeBuilder<A8, cross>::get(Context));
  573. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  574. params, false);
  575. }
  576. };
  577. #undef DEFINE_OBJECT_HEAD_FIELDS
  578. #undef DEFINE_FIELD
  579. } // namespace llvm
  580. namespace py {
  581. typedef PyTypeBuilder<PyObject> ObjectTy;
  582. typedef PyTypeBuilder<PyVarObject> VarObjectTy;
  583. typedef PyTypeBuilder<PyStringObject> StringTy;
  584. typedef PyTypeBuilder<PyIntObject> IntTy;
  585. typedef PyTypeBuilder<PyTupleObject> TupleTy;
  586. typedef PyTypeBuilder<PyListObject> ListTy;
  587. typedef PyTypeBuilder<PyTypeObject> TypeTy;
  588. typedef PyTypeBuilder<PyCodeObject> CodeTy;
  589. typedef PyTypeBuilder<PyFunctionObject> FunctionTy;
  590. typedef PyTypeBuilder<PyMethodObject> MethodTy;
  591. typedef PyTypeBuilder<PyFrameObject> FrameTy;
  592. typedef PyTypeBuilder<PyThreadState> ThreadStateTy;
  593. typedef PyTypeBuilder<PyCFunctionObject> CFunctionTy;
  594. typedef PyTypeBuilder<PyMethodDescrObject> MethodDescrTy;
  595. typedef PyTypeBuilder<PyMethodDef> MethodDefTy;
  596. } // namespace py
  597. #endif // UTIL_PYTYPEBUILDER_H