Ast.ml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. (*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. *)
  31. (* Available types. *)
  32. type signedness = Signed | Unsigned
  33. type shortness = IShort | ILong | INone
  34. type int_attr = {
  35. ia_signedness : signedness;
  36. ia_shortness : shortness;
  37. }
  38. type atype =
  39. | Char of signedness
  40. | Long of signedness
  41. | LLong of signedness
  42. | Int of int_attr
  43. | Float | Double | LDouble
  44. | Int8 | Int16 | Int32 | Int64
  45. | UInt8 | UInt16 | UInt32 | UInt64
  46. | Void | WChar | SizeT
  47. | Struct of string
  48. | Union of string
  49. | Enum of string
  50. | Foreign of string
  51. | Ptr of atype
  52. (* Pointer parameter direction *)
  53. type ptr_direction =
  54. | PtrIn | PtrOut | PtrInOut | PtrNoDirection
  55. (* It holds possible values for a given attribute. *)
  56. type attr_value =
  57. | AString of string
  58. | ANumber of int
  59. type ptr_size = {
  60. ps_size : attr_value option;
  61. ps_sizefunc : string option;
  62. ps_count : attr_value option;
  63. }
  64. let empty_ptr_size = {
  65. ps_size = None;
  66. ps_sizefunc = None;
  67. ps_count = None;
  68. }
  69. (* Pointers have several special attributes. *)
  70. type ptr_attr = {
  71. pa_direction : ptr_direction;
  72. pa_size : ptr_size;
  73. pa_isptr : bool; (* If a foreign type is a pointer type *)
  74. pa_isary : bool; (* If a foreign type is an array *)
  75. pa_isstr : bool;
  76. pa_iswstr : bool;
  77. pa_rdonly : bool; (* If the pointer is 'const' qualified *)
  78. pa_chkptr : bool; (* Whether to generate code to check pointer *)
  79. }
  80. (* parameter type *)
  81. type parameter_type =
  82. | PTVal of atype (* Passed by value *)
  83. | PTPtr of atype * ptr_attr (* Passed by address *)
  84. type call_conv = CC_CDECL | CC_STDCALL | CC_FASTCALL | CC_NONE
  85. let get_call_conv_str (cc: call_conv) =
  86. match cc with
  87. CC_CDECL -> "CDECL"
  88. | CC_STDCALL -> "STDCALL"
  89. | CC_FASTCALL -> "FASTCALL"
  90. | CC_NONE -> "NOCONVENTION"
  91. (* function attribute - only for untrusted functions *)
  92. type func_attr = {
  93. fa_dllimport : bool; (* use 'dllimport'? *)
  94. fa_convention: call_conv; (* the calling convention *)
  95. }
  96. (* A declarator can be an identifier or an identifier with array form.
  97. * For a simlpe identifier, the `array_dims' is an empty list `[]'.
  98. * A dimension with size -1 means that user explicitly declared `ary[]'.
  99. *)
  100. type declarator = {
  101. identifier : string;
  102. array_dims : int list;
  103. }
  104. let is_array (declr: declarator) = declr.array_dims <> []
  105. (* Parameter declaration. *)
  106. type pdecl = parameter_type * declarator
  107. (* Structure member declaration *)
  108. type mdecl = atype * declarator
  109. (* Definition of a struct or union *)
  110. type struct_def = {
  111. sname : string; (* structure name. *)
  112. mlist : mdecl list; (* structure members. *)
  113. }
  114. (* Definition of a enum *)
  115. type enum_val = EnumValNone | EnumVal of attr_value
  116. type enum_ele = string * enum_val
  117. type enum_def = {
  118. enname: string; (* enum name - "" for anonymous enum *)
  119. enbody: enum_ele list; (* elements of enum *)
  120. }
  121. (* Composite type - the form for struct/union definition are the same. *)
  122. type composite_type =
  123. StructDef of struct_def
  124. | UnionDef of struct_def
  125. | EnumDef of enum_def
  126. (* Function declaration. *)
  127. type func_decl = {
  128. fname : string; (* function name. *)
  129. rtype : atype; (* return type. *)
  130. plist : pdecl list; (* parameter list. *)
  131. }
  132. (* The untrusted functions might have a string list that specifying
  133. * trusted ECALLs possibly to be made. While the trusted functions
  134. * have a bool tag to identify whether it is private or not (private
  135. * means it can only be accessed by an OCALL).
  136. *)
  137. type trusted_func = {
  138. tf_fdecl : func_decl;
  139. tf_is_priv : bool;
  140. }
  141. type untrusted_func = {
  142. uf_fdecl : func_decl;
  143. uf_fattr : func_attr;
  144. uf_allow_list : string list;
  145. uf_propagate_errno : bool;
  146. }
  147. type enclave_func =
  148. | Trusted of trusted_func
  149. | Untrusted of untrusted_func
  150. (* Module import declaration. *)
  151. type import_decl = {
  152. mname : string; (* from which to import functions. *)
  153. flist : string list; (* a list of functions to be imported. *)
  154. }
  155. (* All valid expressions. *)
  156. type expr =
  157. | Interface of enclave_func list
  158. | Composite of composite_type
  159. | Importing of import_decl
  160. | Include of string
  161. (* The definition of an Enclave *)
  162. type enclave = {
  163. ename : string; (* enclave name. *)
  164. eexpr : expr list; (* expressions inside enclave. *)
  165. }
  166. (* -------------------------------------------------------------------
  167. * Some utility function to manupulate types defined in AST.
  168. * -------------------------------------------------------------------
  169. *)
  170. (* Get the string representation of a type. *)
  171. let rec get_tystr (ty: atype) =
  172. match ty with
  173. | Char sn ->
  174. (match sn with
  175. Signed -> "char"
  176. | Unsigned -> "unsigned char")
  177. | Long sn ->
  178. (match sn with
  179. Signed -> "long"
  180. | Unsigned -> "unsigned long")
  181. | LLong sn ->
  182. (match sn with
  183. Signed -> "long long"
  184. | Unsigned -> "unsigned long long")
  185. | Int ia ->
  186. Printf.sprintf "%s%sint"
  187. (if ia.ia_signedness = Unsigned then "unsigned " else "")
  188. (match ia.ia_shortness with
  189. IShort -> "short "
  190. | ILong -> "long "
  191. | INone -> "")
  192. | Float -> "float"
  193. | Double -> "double"
  194. | LDouble -> "long double"
  195. | Int8 -> "int8_t"
  196. | Int16 -> "int16_t"
  197. | Int32 -> "int32_t"
  198. | Int64 -> "int64_t"
  199. | UInt8 -> "uint8_t"
  200. | UInt16 -> "uint16_t"
  201. | UInt32 -> "uint32_t"
  202. | UInt64 -> "uint64_t"
  203. | Void -> "void"
  204. | SizeT -> "size_t"
  205. | WChar -> "wchar_t"
  206. | Struct id -> "struct " ^ id
  207. | Union id -> "union " ^ id
  208. | Enum id -> "enum " ^ id
  209. | Foreign s -> s
  210. | Ptr ty -> get_tystr(ty) ^ "*"
  211. (* Get the plain `atype' from a `parameter_type'. *)
  212. let get_param_atype (pt: parameter_type) =
  213. match pt with
  214. | PTVal t -> t
  215. | PTPtr (t, _) -> t
  216. (* Convert attr_value to string *)
  217. let attr_value_to_string (attr: attr_value) =
  218. match attr with
  219. ANumber n -> Printf.sprintf "%d" n
  220. | AString s -> s