/src/wrappers/llvm/library/values/llvm_function.e

http://github.com/tybor/Liberty · Specman e · 250 lines · 132 code · 36 blank · 82 comment · 3 complexity · ed1ca8758b09133fb29b51e81728f06a MD5 · raw file

  1. class LLVM_FUNCTION
  2. -- A function in LLVM
  3. -- The Function class represents a single procedure in LLVM. It is actually
  4. -- one of the more complex classes in the LLVM heirarchy because it must
  5. -- keep track of a large amount of data. The Function class keeps track of
  6. -- a list of BasicBlocks, a list of formal Arguments, and a SymbolTable.
  7. -- The list of BasicBlocks is the most commonly used part of Function
  8. -- objects. The list imposes an implicit ordering of the blocks in the
  9. -- function, which indicate how the code will be layed out by the backend.
  10. -- Additionally, the first BasicBlock is the implicit entry node for the
  11. -- Function. It is not legal in LLVM to explicitly branch to this initial
  12. -- block. There are no implicit exit nodes, and in fact there may be
  13. -- multiple exit nodes from a single Function. If the BasicBlock list is
  14. -- empty, this indicates that the Function is actually a function
  15. -- declaration: the actual body of the function hasn't been linked in yet.
  16. -- In addition to a list of BasicBlocks, the Function class also keeps
  17. -- track of the list of formal Arguments that the function receives. This
  18. -- container manages the lifetime of the Argument nodes, just like the
  19. -- BasicBlock list does for the BasicBlocks.
  20. -- The SymbolTable is a very rarely used LLVM feature that is only used
  21. -- when you have to look up a value by name. Aside from that, the
  22. -- SymbolTable is used internally to make sure that there are not conflicts
  23. -- between the names of Instructions, BasicBlocks, or Arguments in the
  24. -- function body.
  25. -- Note that Function is a GlobalValue and therefore also a Constant. The
  26. -- value of the function is its address (after linking) which is guaranteed
  27. -- to be constant.
  28. -- Eiffel specific note: variadic functions - functions accepting a
  29. -- variable number of arguments - are not supported.
  30. inherit
  31. LLVM_GLOBAL_VALUE
  32. ITERABLE[LLVM_VALUE]
  33. -- to provide iteration over parameters
  34. rename
  35. count as parameters_count,
  36. is_empty as is_parameterless
  37. new_iterator as new_parameter_iterator,
  38. do_all as do_all_parameters
  39. for_all as for_all_parameters
  40. exists as exists_parameter
  41. undefine is_equal, copy, out_in_tagged_out_memory, fill_tagged_out_memory
  42. end
  43. insert LLVM_VALUE_WRAPPER_FACTORY
  44. create {ANY} make
  45. create {WRAPPER, WRAPPER_HANDLER} from_external_pointer
  46. feature {} -- Creation
  47. make (a_return_type: LLVM_TYPE; some_parameters: WRAPPER_COLLECTION[LLVM_TYPE])
  48. local some_wrappers: WRAPPER_COLLECTION[LLVM_TYPE]
  49. do
  50. from_external_pointer
  51. (llvmfunction_type
  52. (null_or(a_return_type),
  53. null_or_array(some_parameters),
  54. some_parameters.count.to_natural_32,
  55. 0 -- i.e. False, non-variadic
  56. ))
  57. end
  58. feature {ANY} -- Calling convention
  59. calling_convention: LLVMCALL_CONV_ENUM
  60. do
  61. Result.change_value(llvmget_function_call_conv(handle).to_integer_32)
  62. end
  63. set_calling_convention (a_calling_convention: LLVMCALL_CONV_ENUM)
  64. -- Set calling convention
  65. do
  66. llvmset_function_call_conv(handle, a_calling_convention.value.to_natural_32)
  67. end
  68. feature {ANY} -- Iterating over blocks
  69. -- `first', `last' from LLVM_FUNCTION and `next', `previous' from
  70. -- LLVM_BASIC_BLOCK allow to iterate over blocks. A proper ITERATOR shall
  71. -- be provided (TODO).
  72. -- Note: a feature like "blocks: COLLECTION[LLVM_BASIC_BLOCK]" is not
  73. -- offered even if there are functions like "llvmcount_basic_blocks" and
  74. -- "llvmget_basic_blocks" because its content may be invalidated adding
  75. -- some blocks; there is not efficient way to monitor changes standing my
  76. -- insufficient current knowledge of LLVM. Paolo 2009-10-20.
  77. first: LLVM_BASIC_BLOCK
  78. -- The first block
  79. local p: POINTER
  80. do
  81. p := llvmget_first_basic_block(handle)
  82. if p.is_not_null then
  83. create Result.from_external_pointer(p)
  84. end
  85. end
  86. last: LLVM_BASIC_BLOCK
  87. -- The last block
  88. local p: POINTER
  89. do
  90. p:=llvmget_last_basic_block(handle)
  91. if p.is_not_null then
  92. create Result.from_external_pointer(p)
  93. end
  94. end
  95. basic_blocks_count: NATURAL_32
  96. do
  97. Result := llvmcount_basic_blocks(handle)
  98. end
  99. -- TODO: basic_blocks: COLLECTION[LLVM_BASIC_BLOCK] may be not efficiently
  100. -- implemented, so "void LLVMGetBasicBlocks(LLVMValueRef Fn,
  101. -- LLVMBasicBlockRef *BasicBlocks);" is not wrapped. It would require that
  102. -- the given array is stable, while it is mutable since instructions may be
  103. -- added anytime. To implement this feature we shall directly access C++
  104. -- API.
  105. new_block_iterator: ITERATOR_OVER_BASIC_BLOCKS
  106. -- A new iterator over blocks composing the function.
  107. do
  108. create Result.from_function(Current)
  109. ensure Result/=Void
  110. end
  111. entry_basic_block: LLVM_BASIC_BLOCK
  112. -- Entry block of function
  113. local p: POINTER
  114. do
  115. p := llvmget_entry_basic_block(handle)
  116. if p.is_not_null then
  117. create Result.from_external_pointer(p)
  118. end
  119. end
  120. feature {ANY} -- Parameters
  121. parameters_count: INTEGER
  122. -- The number of parameters. TODO: should be NATURAL
  123. do
  124. Result:=llvmcount_params(handle).to_integer_32
  125. end
  126. is_parameterless: BOOLEAN do Result:=(parameters_count=0) end
  127. -- Note: "void LLVMGetParams(LLVMValueRef Fn,
  128. -- LLVMValueRef *Params);" will not be wrapped because
  129. -- the C interface doe not allow an efficient
  130. -- implementation see the note about basic blocks.
  131. is_valid_parameter_index (an_index: INTEGER): BOOLEAN
  132. do
  133. Result := an_index.in_range(0, parameters_count)
  134. end
  135. parameter (an_index: INTEGER): LLVM_VALUE
  136. -- The parameter at `an_index'
  137. -- TODO: `an_index' should be a NATURAL_32 as in C it's an unsigned.
  138. require is_valid_parameter_index(an_index)
  139. local p: POINTER
  140. do
  141. p := llvmget_param(handle,an_index.to_natural_32)
  142. check
  143. p.is_not_null
  144. end
  145. Result:=value_wrapper(p)
  146. ensure Result/=Void
  147. end
  148. new_parameter_iterator: ITERATOR_OVER_FUNCTION_PARAMETERS
  149. -- A newly allocated iterator over Current function's parameters.
  150. do
  151. create Result.from_function(Current)
  152. end
  153. -- LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn);
  154. -- LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn);
  155. -- LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg);
  156. -- LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg);
  157. -- void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA);
  158. -- void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA);
  159. -- void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align);
  160. feature {ANY} -- Deleting
  161. delete
  162. -- Delete Current function.
  163. do
  164. llvmdelete_function(handle)
  165. handle := default_pointer
  166. ensure
  167. unusable: is_deleted
  168. end
  169. is_deleted: BOOLEAN
  170. -- Has Current function already been deleted?
  171. do
  172. Result:=handle.is_null
  173. end
  174. feature {ANY} -- Miscellaneous
  175. intrinsic_id: NATURAL_32
  176. do
  177. Result:=llvmget_intrinsic_id(handle)
  178. end
  179. gc: FIXED_STRING
  180. -- wraps LLVMGetGC. TODO: provide a better name and description
  181. do
  182. create Result.from_external(llvmget_gc(handle))
  183. ensure Result/=Void
  184. end
  185. set_gc (a_name: ABSTRACT_STRING)
  186. -- wraps LLVMSetGC. TODO: provide better name and description
  187. do
  188. llvmset_gc(handle,a_name.to_external)
  189. ensure set: a_name.is_equal(gc)
  190. end
  191. add_attribute (an_attribute: LLVMATTRIBUTE_ENUM)
  192. do
  193. llvmadd_function_attr(handle,an_attribute.value)
  194. end
  195. remove_attribute (an_attribute: LLVMATTRIBUTE_ENUM)
  196. do
  197. llvmremove_function_attr(handle,an_attribute.value)
  198. end
  199. end -- class LLVM_FUNCTION
  200. -- Copyright (C) 2009-2017: Paolo Redaelli
  201. -- This file is part of LLVM wrappers for Liberty Eiffel.
  202. --
  203. -- This library is free software: you can redistribute it and/or modify
  204. -- it under the terms of the GNU Lesser General Public License as published by
  205. -- the Free Software Foundation, version 3 of the License.
  206. --
  207. -- Liberty Eiffel is distributed in the hope that it will be useful,
  208. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  209. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  210. -- GNU General Public License for more details.
  211. --
  212. -- You should have received a copy of the GNU General Public License
  213. -- along with Liberty Eiffel. If not, see <http://www.gnu.org/licenses/>.
  214. --