llvmbase.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. Copyright (c) 2007-2008, 2013 by Jonas Maebe
  3. Contains the base types for LLVM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This Unit contains the base types for LLVM
  18. }
  19. unit llvmbase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. strings,globtype,
  24. cutils,cclasses,aasmbase,cpubase,cpuinfo,cgbase;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. tllvmop = (la_none,
  30. { terminator instructions }
  31. la_ret, la_br, la_switch, la_indirectbr,
  32. la_invoke, la_resume,
  33. la_unreachable,
  34. { binary operations }
  35. la_add, la_fadd, la_sub, la_fsub, la_mul, la_fmul,
  36. la_udiv,la_sdiv, la_fdiv, la_urem, la_srem, la_frem,
  37. { bitwise binary operations }
  38. la_shl, la_lshr, la_ashr, la_and, la_or, la_xor,
  39. { vector operations }
  40. la_extractelement, la_insertelement, la_shufflevector,
  41. { aggregate operations }
  42. la_extractvalue, la_insertvalue,
  43. { memory access and memory addressing operations }
  44. la_alloca,
  45. la_load, la_store,
  46. la_fence, la_cmpxchg, la_atomicrmw,
  47. la_getelementptr,
  48. { conversion operations }
  49. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  50. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  51. la_ptrtoint, la_inttoptr,
  52. la_bitcast,
  53. { other operations }
  54. la_icmp, la_fcmp,
  55. la_phi, la_select, la_call,
  56. la_va_arg, la_landingpad,
  57. la_blockaddress,
  58. { fpc pseudo opcodes }
  59. la_type, { type definition }
  60. la_catch, { catch clause of a landingpad }
  61. la_filter, { filter clause of a landingpad }
  62. la_cleanup, { cleanup clause of a landingpad (finally) }
  63. la_x_to_inttoptr, { have to convert something first to int before it can be converted to a pointer }
  64. la_ptrtoint_to_x, { have to convert a pointer first to int before it can be converted to something else }
  65. la_asmblock
  66. );
  67. tllvmvalueextension = (lve_none, lve_zeroext, lve_signext);
  68. const
  69. llvmterminatoropcodes = [la_ret, la_br, la_switch, la_indirectbr,
  70. la_invoke, la_resume,
  71. la_unreachable];
  72. llvmvalueextension2str: array[tllvmvalueextension] of TSymStr = ('',
  73. ' zeroext',' signext');
  74. type
  75. tllvmfpcmp = (
  76. lfc_invalid,
  77. lfc_false,
  78. lfc_oeq, lfc_ogt, lfc_oge, lfc_olt, lfc_ole, lfc_one, lfc_ord,
  79. lfc_ueq, lfc_ugt, lfc_uge, lfc_ult, lfc_ule, lfc_une, lfc_uno,
  80. lfc_true);
  81. {# This should define the array of instructions as string }
  82. llvmop2strtable=array[tllvmop] of string[14];
  83. const
  84. { = max(cpubase.max_operands,8) }
  85. max_operands = ((-ord(cpubase.max_operands<=8)) and 15) or ((-ord(cpubase.max_operands>8)) and cpubase.max_operands);
  86. function llvm_target_name: ansistring;
  87. function llvm_callingconvention_name(c: tproccalloption): ansistring;
  88. implementation
  89. uses
  90. globals,
  91. systems;
  92. {$j-}
  93. {$ifndef arm}
  94. const
  95. llvmsystemcpu: array[tsystemcpu] of ansistring =
  96. ('unknown',
  97. 'i386',
  98. 'm68k',
  99. 'alpha',
  100. 'powerpc',
  101. 'sparc',
  102. 'unknown',
  103. 'ia64',
  104. 'x86_64',
  105. 'mips',
  106. 'arm',
  107. 'powerpc64',
  108. 'avr',
  109. 'mipsel',
  110. 'unknown',
  111. 'unknown',
  112. 'aarch64',
  113. 'wasm32',
  114. 'sparc64',
  115. 'riscv32',
  116. 'riscv64'
  117. );
  118. {$endif}
  119. function llvm_target_name: ansistring;
  120. begin
  121. { architecture }
  122. {$ifdef arm}
  123. llvm_target_name:=lower(cputypestr[current_settings.cputype]);
  124. {$else arm}
  125. llvm_target_name:=llvmsystemcpu[target_info.cpu];
  126. {$endif}
  127. { vendor and/or OS }
  128. if target_info.system in systems_darwin then
  129. begin
  130. llvm_target_name:=llvm_target_name+'-apple';
  131. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  132. llvm_target_name:=llvm_target_name+'-macosx'+MacOSXVersionMin
  133. else
  134. llvm_target_name:=llvm_target_name+'-ios'+iPhoneOSVersionMin;
  135. end
  136. else if target_info.system in (systems_linux+systems_android) then
  137. llvm_target_name:=llvm_target_name+'-unknown-linux'
  138. else if target_info.system in systems_windows then
  139. begin
  140. { WinCE isn't supported (yet) by llvm, but if/when added this is
  141. presumably how they will differentiate it }
  142. if not(target_info.system in [system_i386_wince,system_arm_wince]) then
  143. llvm_target_name:=llvm_target_name+'-pc';
  144. llvm_target_name:=llvm_target_name+'-win32'
  145. end
  146. else if target_info.system in systems_freebsd then
  147. llvm_target_name:=llvm_target_name+'-freebsd'
  148. else if target_info.system in systems_openbsd then
  149. llvm_target_name:=llvm_target_name+'-openbsd'
  150. else if target_info.system in systems_netbsd then
  151. llvm_target_name:=llvm_target_name+'-netbsd'
  152. else if target_info.system in systems_aix then
  153. llvm_target_name:=llvm_target_name+'-ibm-aix'
  154. else if target_info.system in [system_i386_haiku] then
  155. llvm_target_name:=llvm_target_name+'-haiku'
  156. else if target_info.system in systems_embedded then
  157. llvm_target_name:=llvm_target_name+'-none'
  158. else
  159. llvm_target_name:=llvm_target_name+'-unknown';
  160. { environment/ABI }
  161. if target_info.system in systems_android then
  162. llvm_target_name:=llvm_target_name+'-android';
  163. {$if defined(FPC_ARMHF)}
  164. llvm_target_name:=llvm_target_name+'-gnueabihf';
  165. {$elseif defined(FPC_ARMEL)}
  166. if target_info.system in systems_embedded then
  167. llvm_target_name:=llvm_target_name+'-eabi'
  168. else if target_info.system=system_arm_android then
  169. { handled above already
  170. llvm_target_name:=llvm_target_name+'-android' }
  171. else
  172. llvm_target_name:=llvm_target_name+'-gnueabi';
  173. {$else}
  174. if target_info.system in systems_linux then
  175. llvm_target_name:=llvm_target_name+'-gnu';
  176. {$endif}
  177. end;
  178. function llvm_callingconvention_name(c: tproccalloption): ansistring;
  179. begin
  180. // TODO (unsupported by LLVM at this time):
  181. // * pocall_pascal
  182. // * pocall_oldfpccall
  183. // * pocall_syscall
  184. // * pocall_far16
  185. // * possibly pocall_softfloat
  186. case c of
  187. { to prevent errors if none of the defines below is active }
  188. pocall_none:
  189. result:='';
  190. {$ifdef i386}
  191. pocall_register:
  192. result:='x86_borlandregcallcc';
  193. pocall_stdcall:
  194. result:='x86_stdcallcc';
  195. {$endif i386}
  196. {$ifdef x86}
  197. pocall_interrupt:
  198. result:='x86_intrcc';
  199. pocall_sysv_abi_default,
  200. pocall_sysv_abi_cdecl:
  201. result:='x86_64_sysvcc';
  202. pocall_ms_abi_default,
  203. pocall_ms_abi_cdecl:
  204. result:='win64cc';
  205. pocall_vectorcall:
  206. result:='x86_vectorcallcc';
  207. pocall_internproc:
  208. result:=llvm_callingconvention_name(pocall_default);
  209. {$endif x86}
  210. {$ifdef avr}
  211. pocall_interrupt:
  212. result:='avr_intrcc';
  213. {$endif avr}
  214. {$if defined(arm) and not defined(FPC_ARMHF)}
  215. pocall_hardfloat:
  216. result:='arm_aapcs_vfpcc';
  217. {$endif arm and not FPC_ARMHF}
  218. else
  219. result:='';
  220. end;
  221. end;
  222. end.