userspace/cparser.h - ktap

Data types defined

Functions defined

Macros defined

Source code

  1. #ifndef __KTAP_CPARSER_H__
  2. #define __KTAP_CPARSER_H__

  3. /*
  4. * Copyright (c) 2011 James R. McKaskill
  5. *
  6. * This software is licensed under the stock MIT license:
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * ----------------------------------------------------------------------------
  27. */

  28. /*
  29. * Adapted from luaffi commit: abc638c9341025580099dcf77795c4b320ba0e63
  30. *
  31. * Copyright (c) 2013 Yicheng Qin, Qingping Hou
  32. */

  33. #ifdef CONFIG_KTAP_FFI

  34. #include <assert.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <stdint.h>
  38. #include <string.h>
  39. #include <stdbool.h>

  40. #include "../include/ktap_ffi.h"

  41. #define PTR_ALIGN_MASK (sizeof(void*) - 1)
  42. #define FUNCTION_ALIGN_MASK (sizeof(void (*)()) - 1)
  43. #define DEFAULT_ALIGN_MASK 7

  44. struct parser {
  45.     int line;
  46.     const char *next;
  47.     const char *prev;
  48.     unsigned align_mask;
  49. };

  50. enum {
  51.     C_CALL,
  52.     STD_CALL,
  53.     FAST_CALL,
  54. };


  55. #define MAX_TYPE_NAME_LEN CSYM_NAME_MAX_LEN

  56. enum {
  57.     /* 0 - 4 */
  58.     INVALID_TYPE,
  59.     VOID_TYPE,
  60.     BOOL_TYPE,
  61.     INT8_TYPE,
  62.     INT16_TYPE,
  63.     /* 5 - 9 */
  64.     INT32_TYPE,
  65.     INT64_TYPE,
  66.     INTPTR_TYPE,
  67.     ENUM_TYPE,
  68.     UNION_TYPE,
  69.     /* 10 - 12 */
  70.     STRUCT_TYPE,
  71.     FUNCTION_TYPE,
  72.     FUNCTION_PTR_TYPE,
  73. };


  74. #define IS_CHAR_UNSIGNED (((char) -1) > 0)

  75. #define POINTER_BITS 2
  76. #define POINTER_MAX ((1 << POINTER_BITS) - 1)

  77. #define ALIGNOF(S) ((int) ((char*) &S.v - (char*) &S - 1))


  78. /* Note: if adding a new member that is associated with a struct/union
  79. * definition then it needs to be copied over in ctype.c:set_defined for when
  80. * we create types based off of the declaration alone.
  81. *
  82. * Since this is used as a header for every ctype and cdata, and we create a
  83. * ton of them on the stack, we try and minimise its size.
  84. */
  85. struct cp_ctype {
  86.     size_t base_size; /* size of the base type in bytes */
  87.     int ffi_base_cs_id;
  88.     int ffi_cs_id; /* index for csymbol from ktap vm */
  89.     union {
  90.         /* valid if is_bitfield */
  91.         struct {
  92.             /* size of bitfield in bits */
  93.             unsigned bit_size : 7;
  94.             /* offset within the current byte between 0-63 */
  95.             unsigned bit_offset : 6;
  96.         };
  97.         /* Valid if is_array */
  98.         size_t array_size;
  99.         /* Valid for is_variable_struct or is_variable_array. If
  100.          * variable_size_known (only used for is_variable_struct)
  101.          * then this is the total increment otherwise this is the
  102.          * per element increment.
  103.          */
  104.         size_t variable_increment;
  105.     };
  106.     size_t offset;
  107.     /* as (align bytes - 1) eg 7 gives 8 byte alignment */
  108.     unsigned align_mask : 4;
  109.     /* number of dereferences to get to the base type
  110.      * including +1 for arrays */
  111.     unsigned pointers : POINTER_BITS;
  112.     /* const pointer mask, LSB is current pointer, +1 for the whether
  113.      * the base type is const */
  114.     unsigned const_mask : POINTER_MAX + 1;
  115.     unsigned type : 5; /* value given by type enum above */
  116.     unsigned is_reference : 1;
  117.     unsigned is_array : 1;
  118.     unsigned is_defined : 1;
  119.     unsigned is_null : 1;
  120.     unsigned has_member_name : 1;
  121.     unsigned calling_convention : 2;
  122.     unsigned has_var_arg : 1;
  123.     /* set for variable array types where we don't know
  124.      * the variable size yet */
  125.     unsigned is_variable_array : 1;
  126.     unsigned is_variable_struct : 1;
  127.     /* used for variable structs after we know the variable size */
  128.     unsigned variable_size_known : 1;
  129.     unsigned is_bitfield : 1;
  130.     unsigned has_bitfield : 1;
  131.     unsigned is_jitted : 1;
  132.     unsigned is_packed : 1;
  133.     unsigned is_unsigned : 1;
  134. };

  135. #define ALIGNED_DEFAULT (__alignof__(void* __attribute__((aligned))) - 1)

  136. csymbol *cp_id_to_csym(int id);
  137. #define ct_ffi_cs(ct) (cp_id_to_csym((ct)->ffi_cs_id))

  138. size_t ctype_size(const struct cp_ctype* ct);
  139. int ctype_stack_top();
  140. int cp_ctype_init();
  141. int cp_ctype_free();
  142. struct cp_ctype *ctype_lookup_type(char *name);
  143. void cp_ctype_dump_stack();
  144. void cp_error(const char *err_msg_fmt, ...);
  145. struct cp_ctype *cp_ctype_reg_type(char *name, struct cp_ctype *ct);

  146. void cp_update_csym_in_ctype(struct cp_ctype *ct);
  147. void cp_push_ctype_with_name(struct cp_ctype *ct, const char *name, int nlen);
  148. void cp_push_ctype(struct cp_ctype *ct);
  149. void cp_set_defined(struct cp_ctype *ct);

  150. int cp_symbol_build_func(struct cp_ctype *type, const char *fname, int fn_size);
  151. int cp_symbol_build_record(const char *stname, int type, int start_top);
  152. int cp_symbol_build_fake_record(const char *stname, int type);
  153. int cp_symbol_build_pointer(struct cp_ctype *ct);

  154. int ffi_parse_cdef(const char *s);
  155. void ffi_parse_new(const char *s, struct cp_ctype *ct);
  156. int ffi_lookup_csymbol_id_by_name(const char *s);

  157. void ffi_cparser_init(void);
  158. void ffi_cparser_free(void);


  159. static inline csymbol *cp_csymf_ret(csymbol_func *csf)
  160. {
  161.     return cp_id_to_csym(csf->ret_id);
  162. }

  163. static inline csymbol *cp_csymf_arg(csymbol_func *csf, int idx)
  164. {
  165.     return cp_id_to_csym(csf->arg_ids[idx]);
  166. }


  167. #else
  168. static inline void ffi_cparser_init(void)
  169. {
  170.     return;
  171. }
  172. static inline void ffi_cparser_free(void)
  173. {
  174.     return;
  175. }
  176. #endif /* CONFIG_KTAP_FFI */


  177. #endif /* __KTAP_CPARSER_H__ */