manual.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "_cffi_include.h"
  2. #define AA (42)
  3. #define BB (&bb)
  4. static int bb = 16261;
  5. int foo42(int a, int *b)
  6. {
  7. return a - *b;
  8. }
  9. int foo64(int a)
  10. {
  11. return ~a;
  12. }
  13. struct foo_s {
  14. int a;
  15. };
  16. /************************************************************/
  17. static void *_cffi_types[] = {
  18. _CFFI_OP(_CFFI_OP_FUNCTION, 1),
  19. _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_INT),
  20. _CFFI_OP(_CFFI_OP_POINTER, 1),
  21. _CFFI_OP(_CFFI_OP_FUNCTION_END, 0),
  22. _CFFI_OP(_CFFI_OP_FUNCTION, 1),
  23. _CFFI_OP(_CFFI_OP_PRIMITIVE, _CFFI_PRIM_INT),
  24. _CFFI_OP(_CFFI_OP_FUNCTION_END, 0),
  25. _CFFI_OP(_CFFI_OP_STRUCT_UNION, 0),
  26. };
  27. #ifndef PYPY_VERSION
  28. static PyObject *
  29. _cffi_f_foo42(PyObject *self, PyObject *args)
  30. {
  31. int x0;
  32. int * x1;
  33. Py_ssize_t datasize;
  34. int result;
  35. PyObject *arg0;
  36. PyObject *arg1;
  37. if (!PyArg_ParseTuple(args, "OO:foo42", &arg0, &arg1))
  38. return NULL;
  39. x0 = _cffi_to_c_int(arg0, int);
  40. if (x0 == (int)-1 && PyErr_Occurred())
  41. return NULL;
  42. datasize = _cffi_prepare_pointer_call_argument(
  43. _cffi_types[1], arg1, (char **)&x1);
  44. if (datasize != 0) {
  45. if (datasize < 0)
  46. return NULL;
  47. x1 = alloca(datasize);
  48. memset((void *)x1, 0, datasize);
  49. if (_cffi_convert_array_from_object((char *)x1, _cffi_types[1], arg1) < 0)
  50. return NULL;
  51. }
  52. Py_BEGIN_ALLOW_THREADS
  53. _cffi_restore_errno();
  54. { result = foo42(x0, x1); }
  55. _cffi_save_errno();
  56. Py_END_ALLOW_THREADS
  57. return _cffi_from_c_int(result, int);
  58. }
  59. #else
  60. static int _cffi_f_foo42(int x0, int *x1)
  61. {
  62. return foo42(x0, x1);
  63. }
  64. #endif
  65. #ifndef PYPY_VERSION
  66. static PyObject *
  67. _cffi_f_foo64(PyObject *self, PyObject *arg0)
  68. {
  69. int x0;
  70. int result;
  71. x0 = _cffi_to_c_int(arg0, int);
  72. if (x0 == (int)-1 && PyErr_Occurred())
  73. return NULL;
  74. Py_BEGIN_ALLOW_THREADS
  75. _cffi_restore_errno();
  76. { result = foo64(x0); }
  77. _cffi_save_errno();
  78. Py_END_ALLOW_THREADS
  79. return _cffi_from_c_int(result, int);
  80. }
  81. #else
  82. static int _cffi_f_foo64(int x0)
  83. {
  84. return foo64(x0);
  85. }
  86. #endif
  87. static int _cffi_const_AA(unsigned long long *output)
  88. {
  89. *output = (unsigned long long)((AA) << 0); // integer
  90. return (AA) <= 0;
  91. }
  92. static void _cffi_const_BB(char *output)
  93. {
  94. *(int **)output = BB;
  95. }
  96. static const struct _cffi_global_s _cffi_globals[] = {
  97. { "AA", &_cffi_const_AA, _CFFI_OP(_CFFI_OP_CONSTANT_INT, 0) },
  98. { "BB", &_cffi_const_BB, _CFFI_OP(_CFFI_OP_CONSTANT, 2) },
  99. { "bb", &bb, _CFFI_OP(_CFFI_OP_GLOBAL_VAR, 1) },
  100. { "foo42", &_cffi_f_foo42, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_V, 0) },
  101. { "foo64", &_cffi_f_foo64, _CFFI_OP(_CFFI_OP_CPYTHON_BLTN_O, 4) },
  102. };
  103. struct _cffi_align_foo_s { char x; struct foo_s y; };
  104. static const struct _cffi_struct_union_s _cffi_struct_unions[] = {
  105. { "foo_s", 7, 0,
  106. sizeof(struct foo_s),
  107. offsetof(struct _cffi_align_foo_s, y),
  108. 1, 0 },
  109. };
  110. static const struct _cffi_field_s _cffi_fields[] = {
  111. { "a", offsetof(struct foo_s, a), sizeof(((struct foo_s *)0)->a),
  112. _CFFI_OP(_CFFI_OP_NOOP, 1) },
  113. };
  114. static const struct _cffi_type_context_s _cffi_type_context = {
  115. _cffi_types,
  116. _cffi_globals,
  117. _cffi_fields,
  118. _cffi_struct_unions,
  119. NULL,
  120. NULL,
  121. 5, /* num_globals */
  122. 1, /* num_struct_unions */
  123. 0,
  124. 0,
  125. NULL,
  126. 8, /* num_types */
  127. };
  128. #ifndef PYPY_VERSION
  129. PyMODINIT_FUNC
  130. initmanual(void)
  131. {
  132. _cffi_init("manual", 0x2601, &_cffi_type_context);
  133. }
  134. #else
  135. PyMODINIT_FUNC
  136. _cffi_pypyinit_manual(const void *p[])
  137. {
  138. p[0] = (const void *)0x2601;
  139. p[1] = &_cffi_type_context;
  140. }
  141. #endif