Ast.ml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. (*
  2. * Copyright (C) 2011-2018 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_count : attr_value option;
  62. }
  63. let empty_ptr_size = {
  64. ps_size = None;
  65. ps_count = None;
  66. }
  67. (* Pointers have several special attributes. *)
  68. type ptr_attr = {
  69. pa_direction : ptr_direction;
  70. pa_size : ptr_size;
  71. pa_isptr : bool; (* If a foreign type is a pointer type *)
  72. pa_isary : bool; (* If a foreign type is an array *)
  73. pa_isstr : bool;
  74. pa_iswstr : bool;
  75. pa_rdonly : bool; (* If the pointer is 'const' qualified *)
  76. pa_chkptr : bool; (* Whether to generate code to check pointer *)
  77. }
  78. (* parameter type *)
  79. type parameter_type =
  80. | PTVal of atype (* Passed by value *)
  81. | PTPtr of atype * ptr_attr (* Passed by address *)
  82. type call_conv = CC_CDECL | CC_STDCALL | CC_FASTCALL | CC_NONE
  83. let get_call_conv_str (cc: call_conv) =
  84. match cc with
  85. CC_CDECL -> "CDECL"
  86. | CC_STDCALL -> "STDCALL"
  87. | CC_FASTCALL -> "FASTCALL"
  88. | CC_NONE -> "NOCONVENTION"
  89. (* function attribute - only for untrusted functions *)
  90. type func_attr = {
  91. fa_dllimport : bool; (* use 'dllimport'? *)
  92. fa_convention: call_conv; (* the calling convention *)
  93. }
  94. (* A declarator can be an identifier or an identifier with array form.
  95. * For a simlpe identifier, the `array_dims' is an empty list `[]'.
  96. * A dimension with size -1 means that user explicitly declared `ary[]'.
  97. *)
  98. type declarator = {
  99. identifier : string;
  100. array_dims : int list;
  101. }
  102. let is_array (declr: declarator) = declr.array_dims <> []
  103. (* Parameter declaration. *)
  104. type pdecl = parameter_type * declarator
  105. (* Structure member declaration *)
  106. type mdecl = atype * declarator
  107. (* Definition of a struct or union *)
  108. type struct_def = {
  109. sname : string; (* structure name. *)
  110. mlist : mdecl list; (* structure members. *)
  111. }
  112. (* Definition of a enum *)
  113. type enum_val = EnumValNone | EnumVal of attr_value
  114. type enum_ele = string * enum_val
  115. type enum_def = {
  116. enname: string; (* enum name - "" for anonymous enum *)
  117. enbody: enum_ele list; (* elements of enum *)
  118. }
  119. (* Composite type - the form for struct/union definition are the same. *)
  120. type composite_type =
  121. StructDef of struct_def
  122. | UnionDef of struct_def
  123. | EnumDef of enum_def
  124. (* Function declaration. *)
  125. type func_decl = {
  126. fname : string; (* function name. *)
  127. rtype : atype; (* return type. *)
  128. plist : pdecl list; (* parameter list. *)
  129. }
  130. (* The untrusted functions might have a string list that specifying
  131. * trusted ECALLs possibly to be made. While the trusted functions
  132. * have a bool tag to identify whether it is private or not (private
  133. * means it can only be accessed by an OCALL).
  134. *)
  135. type trusted_func = {
  136. tf_fdecl : func_decl;
  137. tf_is_priv : bool;
  138. }
  139. type untrusted_func = {
  140. uf_fdecl : func_decl;
  141. uf_fattr : func_attr;
  142. uf_allow_list : string list;
  143. uf_propagate_errno : bool;
  144. }
  145. type enclave_func =
  146. | Trusted of trusted_func
  147. | Untrusted of untrusted_func
  148. (* Module import declaration. *)
  149. type import_decl = {
  150. mname : string; (* from which to import functions. *)
  151. flist : string list; (* a list of functions to be imported. *)
  152. }
  153. (* All valid expressions. *)
  154. type expr =
  155. | Interface of enclave_func list
  156. | Composite of composite_type
  157. | Importing of import_decl
  158. | Include of string
  159. (* The definition of an Enclave *)
  160. type enclave = {
  161. ename : string; (* enclave name. *)
  162. eexpr : expr list; (* expressions inside enclave. *)
  163. }
  164. (* -------------------------------------------------------------------
  165. * Some utility function to manupulate types defined in AST.
  166. * -------------------------------------------------------------------
  167. *)
  168. (* Get the string representation of a type. *)
  169. let rec get_tystr (ty: atype) =
  170. match ty with
  171. | Char sn ->
  172. (match sn with
  173. Signed -> "char"
  174. | Unsigned -> "unsigned char")
  175. | Long sn ->
  176. (match sn with
  177. Signed -> "long"
  178. | Unsigned -> "unsigned long")
  179. | LLong sn ->
  180. (match sn with
  181. Signed -> "long long"
  182. | Unsigned -> "unsigned long long")
  183. | Int ia ->
  184. Printf.sprintf "%s%sint"
  185. (if ia.ia_signedness = Unsigned then "unsigned " else "")
  186. (match ia.ia_shortness with
  187. IShort -> "short "
  188. | ILong -> "long "
  189. | INone -> "")
  190. | Float -> "float"
  191. | Double -> "double"
  192. | LDouble -> "long double"
  193. | Int8 -> "int8_t"
  194. | Int16 -> "int16_t"
  195. | Int32 -> "int32_t"
  196. | Int64 -> "int64_t"
  197. | UInt8 -> "uint8_t"
  198. | UInt16 -> "uint16_t"
  199. | UInt32 -> "uint32_t"
  200. | UInt64 -> "uint64_t"
  201. | Void -> "void"
  202. | SizeT -> "size_t"
  203. | WChar -> "wchar_t"
  204. | Struct id -> "struct " ^ id
  205. | Union id -> "union " ^ id
  206. | Enum id -> "enum " ^ id
  207. | Foreign s -> s
  208. | Ptr ty -> get_tystr(ty) ^ "*"
  209. (* Get the plain `atype' from a `parameter_type'. *)
  210. let get_param_atype (pt: parameter_type) =
  211. match pt with
  212. | PTVal t -> t
  213. | PTPtr (t, _) -> t
  214. (* Convert attr_value to string *)
  215. let attr_value_to_string (attr: attr_value) =
  216. match attr with
  217. ANumber n -> Printf.sprintf "%d" n
  218. | AString s -> s