PageRenderTime 419ms CodeModel.GetById 111ms app.highlight 179ms RepoModel.GetById 123ms app.codeStats 0ms

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