llvm_backend.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #if defined(LLVM_BACKEND_SUPPORT)
  2. #if defined(GB_SYSTEM_WINDOWS)
  3. #include "llvm-c/Core.h"
  4. #include "llvm-c/ExecutionEngine.h"
  5. #include "llvm-c/Target.h"
  6. #include "llvm-c/Analysis.h"
  7. #include "llvm-c/Object.h"
  8. #include "llvm-c/BitWriter.h"
  9. #include "llvm-c/DebugInfo.h"
  10. #include "llvm-c/Transforms/AggressiveInstCombine.h"
  11. #include "llvm-c/Transforms/InstCombine.h"
  12. #include "llvm-c/Transforms/IPO.h"
  13. #include "llvm-c/Transforms/PassManagerBuilder.h"
  14. #include "llvm-c/Transforms/Scalar.h"
  15. #include "llvm-c/Transforms/Utils.h"
  16. #include "llvm-c/Transforms/Vectorize.h"
  17. #else
  18. #include <llvm-c/Core.h>
  19. #include <llvm-c/ExecutionEngine.h>
  20. #include <llvm-c/Target.h>
  21. #include <llvm-c/Analysis.h>
  22. #include <llvm-c/Object.h>
  23. #include <llvm-c/BitWriter.h>
  24. #include <llvm-c/DebugInfo.h>
  25. #include <llvm-c/Transforms/AggressiveInstCombine.h>
  26. #include <llvm-c/Transforms/InstCombine.h>
  27. #include <llvm-c/Transforms/IPO.h>
  28. #include <llvm-c/Transforms/PassManagerBuilder.h>
  29. #include <llvm-c/Transforms/Scalar.h>
  30. #include <llvm-c/Transforms/Utils.h>
  31. #include <llvm-c/Transforms/Vectorize.h>
  32. #endif
  33. #endif
  34. struct lbProcedure;
  35. struct lbValue {
  36. LLVMValueRef value;
  37. Type *type;
  38. };
  39. enum lbAddrKind {
  40. lbAddr_Default,
  41. lbAddr_Map,
  42. lbAddr_Context,
  43. lbAddr_SoaVariable,
  44. lbAddr_RelativePointer,
  45. lbAddr_RelativeSlice,
  46. lbAddr_AtomOp_index_set,
  47. };
  48. struct lbAddr {
  49. lbAddrKind kind;
  50. lbValue addr;
  51. union {
  52. struct {
  53. lbValue key;
  54. Type *type;
  55. Type *result;
  56. } map;
  57. struct {
  58. Selection sel;
  59. } ctx;
  60. struct {
  61. lbValue index;
  62. Ast *index_expr;
  63. } soa;
  64. struct {
  65. lbValue index;
  66. Ast *node;
  67. } index_set;
  68. struct {
  69. bool deref;
  70. } relative;
  71. };
  72. };
  73. struct lbIncompleteDebugType {
  74. Type *type;
  75. LLVMMetadataRef metadata;
  76. };
  77. struct lbModule {
  78. LLVMModuleRef mod;
  79. LLVMContextRef ctx;
  80. u64 state_flags;
  81. CheckerInfo *info;
  82. gbMutex mutex;
  83. Map<LLVMTypeRef> types; // Key: Type *
  84. Map<Type *> llvm_types; // Key: LLVMTypeRef
  85. i32 internal_type_level;
  86. Map<lbValue> values; // Key: Entity *
  87. Map<lbAddr> soa_values; // Key: Entity *
  88. StringMap<lbValue> members;
  89. StringMap<lbProcedure *> procedures;
  90. Map<Entity *> procedure_values; // Key: LLVMValueRef
  91. StringMap<LLVMValueRef> const_strings;
  92. Map<lbProcedure *> anonymous_proc_lits; // Key: Ast *
  93. Map<struct lbFunctionType *> function_type_map; // Key: Type *
  94. Map<lbProcedure *> equal_procs; // Key: Type *
  95. Map<lbProcedure *> hasher_procs; // Key: Type *
  96. u32 global_array_index;
  97. u32 global_generated_index;
  98. u32 nested_type_name_guid;
  99. Array<lbProcedure *> procedures_to_generate;
  100. Array<String> foreign_library_paths;
  101. lbProcedure *curr_procedure;
  102. LLVMDIBuilderRef debug_builder;
  103. LLVMMetadataRef debug_compile_unit;
  104. Map<LLVMMetadataRef> debug_values; // Key: Pointer
  105. Array<lbIncompleteDebugType> debug_incomplete_types;
  106. };
  107. struct lbGenerator {
  108. lbModule module;
  109. CheckerInfo *info;
  110. Array<String> output_object_paths;
  111. String output_base;
  112. String output_name;
  113. };
  114. struct lbBlock {
  115. LLVMBasicBlockRef block;
  116. Scope *scope;
  117. isize scope_index;
  118. bool appended;
  119. Array<lbBlock *> preds;
  120. Array<lbBlock *> succs;
  121. };
  122. struct lbBranchBlocks {
  123. Ast *label;
  124. lbBlock *break_;
  125. lbBlock *continue_;
  126. };
  127. struct lbContextData {
  128. lbAddr ctx;
  129. isize scope_index;
  130. };
  131. enum lbParamPasskind {
  132. lbParamPass_Value, // Pass by value
  133. lbParamPass_Pointer, // Pass as a pointer rather than by value
  134. lbParamPass_Integer, // Pass as an integer of the same size
  135. lbParamPass_ConstRef, // Pass as a pointer but the value is immutable
  136. lbParamPass_BitCast, // Pass by value and bit cast to the correct type
  137. lbParamPass_Tuple, // Pass across multiple parameters (System V AMD64, up to 2)
  138. };
  139. enum lbDeferExitKind {
  140. lbDeferExit_Default,
  141. lbDeferExit_Return,
  142. lbDeferExit_Branch,
  143. };
  144. enum lbDeferKind {
  145. lbDefer_Node,
  146. lbDefer_Instr,
  147. lbDefer_Proc,
  148. };
  149. struct lbDefer {
  150. lbDeferKind kind;
  151. isize scope_index;
  152. isize context_stack_count;
  153. lbBlock * block;
  154. union {
  155. Ast *stmt;
  156. // NOTE(bill): 'instr' will be copied every time to create a new one
  157. lbValue instr;
  158. struct {
  159. lbValue deferred;
  160. Array<lbValue> result_as_args;
  161. } proc;
  162. };
  163. };
  164. struct lbTargetList {
  165. lbTargetList *prev;
  166. bool is_block;
  167. lbBlock * break_;
  168. lbBlock * continue_;
  169. lbBlock * fallthrough_;
  170. };
  171. enum lbProcedureFlag : u32 {
  172. lbProcedureFlag_WithoutMemcpyPass = 1<<0,
  173. };
  174. struct lbProcedure {
  175. u32 flags;
  176. lbProcedure *parent;
  177. Array<lbProcedure *> children;
  178. Entity * entity;
  179. lbModule * module;
  180. String name;
  181. Type * type;
  182. Ast * type_expr;
  183. Ast * body;
  184. u64 tags;
  185. ProcInlining inlining;
  186. bool is_foreign;
  187. bool is_export;
  188. bool is_entry_point;
  189. bool is_startup;
  190. lbFunctionType *abi_function_type;
  191. LLVMValueRef value;
  192. LLVMBuilderRef builder;
  193. bool is_done;
  194. lbAddr return_ptr;
  195. Array<lbValue> params;
  196. Array<lbDefer> defer_stmts;
  197. Array<lbBlock *> blocks;
  198. Array<lbBranchBlocks> branch_blocks;
  199. Scope * curr_scope;
  200. i32 scope_index;
  201. lbBlock * decl_block;
  202. lbBlock * entry_block;
  203. lbBlock * curr_block;
  204. lbTargetList * target_list;
  205. Ast *curr_stmt;
  206. Array<Scope *> scope_stack;
  207. Array<lbContextData> context_stack;
  208. LLVMMetadataRef debug_info;
  209. lbValue return_ptr_hint_value;
  210. Ast * return_ptr_hint_ast;
  211. bool return_ptr_hint_used;
  212. };
  213. bool lb_init_generator(lbGenerator *gen, Checker *c);
  214. void lb_generate_module(lbGenerator *gen);
  215. String lb_mangle_name(lbModule *m, Entity *e);
  216. String lb_get_entity_name(lbModule *m, Entity *e, String name = {});
  217. LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value=0);
  218. void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value);
  219. void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name);
  220. lbProcedure *lb_create_procedure(lbModule *module, Entity *entity);
  221. void lb_end_procedure(lbProcedure *p);
  222. LLVMTypeRef lb_type(lbModule *m, Type *type);
  223. lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append=false);
  224. lbValue lb_const_nil(lbModule *m, Type *type);
  225. lbValue lb_const_undef(lbModule *m, Type *type);
  226. lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local=true);
  227. lbValue lb_const_bool(lbModule *m, Type *type, bool value);
  228. lbValue lb_const_int(lbModule *m, Type *type, u64 value);
  229. lbAddr lb_addr(lbValue addr);
  230. Type *lb_addr_type(lbAddr const &addr);
  231. LLVMTypeRef lb_addr_lb_type(lbAddr const &addr);
  232. void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value);
  233. lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr);
  234. lbValue lb_emit_load(lbProcedure *p, lbValue v);
  235. void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value);
  236. void lb_build_stmt(lbProcedure *p, Ast *stmt);
  237. lbValue lb_build_expr(lbProcedure *p, Ast *expr);
  238. lbAddr lb_build_addr(lbProcedure *p, Ast *expr);
  239. void lb_build_stmt_list(lbProcedure *p, Array<Ast *> const &stmts);
  240. lbValue lb_build_gep(lbProcedure *p, lbValue const &value, i32 index) ;
  241. lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index);
  242. lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index);
  243. lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index);
  244. lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index);
  245. lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel);
  246. lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel);
  247. lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type);
  248. lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *platform_type);
  249. void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block);
  250. lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t);
  251. lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right);
  252. lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args, ProcInlining inlining = ProcInlining_none, bool use_return_ptr_hint = false);
  253. lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
  254. lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x);
  255. void lb_emit_jump(lbProcedure *p, lbBlock *target_block);
  256. void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block);
  257. void lb_start_block(lbProcedure *p, lbBlock *b);
  258. lbValue lb_build_call_expr(lbProcedure *p, Ast *expr);
  259. lbAddr lb_find_or_generate_context_ptr(lbProcedure *p);
  260. lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx);
  261. lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p);
  262. lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={});
  263. lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, i32 param_index=0);
  264. void lb_add_foreign_library_path(lbModule *m, Entity *e);
  265. lbValue lb_typeid(lbModule *m, Type *type);
  266. lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value);
  267. lbValue lb_address_from_load(lbProcedure *p, lbValue value);
  268. void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt);
  269. lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init);
  270. lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array<lbValue> const &args);
  271. lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index);
  272. lbValue lb_string_elem(lbProcedure *p, lbValue string);
  273. lbValue lb_string_len(lbProcedure *p, lbValue string);
  274. lbValue lb_cstring_len(lbProcedure *p, lbValue value);
  275. lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr);
  276. lbValue lb_slice_elem(lbProcedure *p, lbValue slice);
  277. lbValue lb_slice_len(lbProcedure *p, lbValue slice);
  278. lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da);
  279. lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da);
  280. lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da);
  281. lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da);
  282. lbValue lb_map_entries(lbProcedure *p, lbValue value);
  283. lbValue lb_map_entries_ptr(lbProcedure *p, lbValue value);
  284. lbValue lb_map_len(lbProcedure *p, lbValue value);
  285. lbValue lb_map_cap(lbProcedure *p, lbValue value);
  286. lbValue lb_soa_struct_len(lbProcedure *p, lbValue value);
  287. void lb_emit_increment(lbProcedure *p, lbValue addr);
  288. lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y);
  289. void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len);
  290. lbValue lb_type_info(lbModule *m, Type *type);
  291. lbValue lb_find_or_add_entity_string(lbModule *m, String const &str);
  292. lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr);
  293. bool lb_is_const(lbValue value);
  294. bool lb_is_const_or_global(lbValue value);
  295. bool lb_is_const_nil(lbValue value);
  296. String lb_get_const_string(lbModule *m, lbValue value);
  297. lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true);
  298. lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id);
  299. lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type);
  300. lbValue lb_gen_map_hash(lbProcedure *p, lbValue key, Type *key_type);
  301. void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, lbValue map_key, lbValue map_value, Ast *node);
  302. void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value);
  303. lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value);
  304. lbValue lb_emit_source_code_location(lbProcedure *p, String const &procedure, TokenPos const &pos);
  305. lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const &param_value, TokenPos const &pos);
  306. lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type);
  307. lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type);
  308. lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
  309. LLVMMetadataRef lb_debug_type(lbModule *m, Type *type);
  310. #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
  311. #define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info"
  312. #define LB_TYPE_INFO_DATA_NAME "__$type_info_data"
  313. #define LB_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
  314. #define LB_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
  315. #define LB_TYPE_INFO_OFFSETS_NAME "__$type_info_offsets_data"
  316. #define LB_TYPE_INFO_USINGS_NAME "__$type_info_usings_data"
  317. #define LB_TYPE_INFO_TAGS_NAME "__$type_info_tags_data"
  318. enum lbCallingConventionKind {
  319. lbCallingConvention_C = 0,
  320. lbCallingConvention_Fast = 8,
  321. lbCallingConvention_Cold = 9,
  322. lbCallingConvention_GHC = 10,
  323. lbCallingConvention_HiPE = 11,
  324. lbCallingConvention_WebKit_JS = 12,
  325. lbCallingConvention_AnyReg = 13,
  326. lbCallingConvention_PreserveMost = 14,
  327. lbCallingConvention_PreserveAll = 15,
  328. lbCallingConvention_Swift = 16,
  329. lbCallingConvention_CXX_FAST_TLS = 17,
  330. lbCallingConvention_FirstTargetCC = 64,
  331. lbCallingConvention_X86_StdCall = 64,
  332. lbCallingConvention_X86_FastCall = 65,
  333. lbCallingConvention_ARM_APCS = 66,
  334. lbCallingConvention_ARM_AAPCS = 67,
  335. lbCallingConvention_ARM_AAPCS_VFP = 68,
  336. lbCallingConvention_MSP430_INTR = 69,
  337. lbCallingConvention_X86_ThisCall = 70,
  338. lbCallingConvention_PTX_Kernel = 71,
  339. lbCallingConvention_PTX_Device = 72,
  340. lbCallingConvention_SPIR_FUNC = 75,
  341. lbCallingConvention_SPIR_KERNEL = 76,
  342. lbCallingConvention_Intel_OCL_BI = 77,
  343. lbCallingConvention_X86_64_SysV = 78,
  344. lbCallingConvention_Win64 = 79,
  345. lbCallingConvention_X86_VectorCall = 80,
  346. lbCallingConvention_HHVM = 81,
  347. lbCallingConvention_HHVM_C = 82,
  348. lbCallingConvention_X86_INTR = 83,
  349. lbCallingConvention_AVR_INTR = 84,
  350. lbCallingConvention_AVR_SIGNAL = 85,
  351. lbCallingConvention_AVR_BUILTIN = 86,
  352. lbCallingConvention_AMDGPU_VS = 87,
  353. lbCallingConvention_AMDGPU_GS = 88,
  354. lbCallingConvention_AMDGPU_PS = 89,
  355. lbCallingConvention_AMDGPU_CS = 90,
  356. lbCallingConvention_AMDGPU_KERNEL = 91,
  357. lbCallingConvention_X86_RegCall = 92,
  358. lbCallingConvention_AMDGPU_HS = 93,
  359. lbCallingConvention_MSP430_BUILTIN = 94,
  360. lbCallingConvention_AMDGPU_LS = 95,
  361. lbCallingConvention_AMDGPU_ES = 96,
  362. lbCallingConvention_AArch64_VectorCall = 97,
  363. lbCallingConvention_MaxID = 1023,
  364. };
  365. lbCallingConventionKind const lb_calling_convention_map[ProcCC_MAX] = {
  366. lbCallingConvention_C, // ProcCC_Invalid,
  367. lbCallingConvention_C, // ProcCC_Odin,
  368. lbCallingConvention_C, // ProcCC_Contextless,
  369. lbCallingConvention_C, // ProcCC_CDecl,
  370. lbCallingConvention_X86_StdCall, // ProcCC_StdCall,
  371. lbCallingConvention_X86_FastCall, // ProcCC_FastCall,
  372. lbCallingConvention_C, // ProcCC_None,
  373. lbCallingConvention_C, // ProcCC_InlineAsm,
  374. };
  375. enum : LLVMDWARFTypeEncoding {
  376. LLVMDWARFTypeEncoding_Address = 1,
  377. LLVMDWARFTypeEncoding_Boolean = 2,
  378. LLVMDWARFTypeEncoding_ComplexFloat = 3,
  379. LLVMDWARFTypeEncoding_Float = 4,
  380. LLVMDWARFTypeEncoding_Signed = 5,
  381. LLVMDWARFTypeEncoding_SignedChar = 6,
  382. LLVMDWARFTypeEncoding_Unsigned = 7,
  383. LLVMDWARFTypeEncoding_UnsignedChar = 8,
  384. LLVMDWARFTypeEncoding_ImaginaryFloat = 9,
  385. LLVMDWARFTypeEncoding_PackedDecimal = 10,
  386. LLVMDWARFTypeEncoding_NumericString = 11,
  387. LLVMDWARFTypeEncoding_Edited = 12,
  388. LLVMDWARFTypeEncoding_SignedFixed = 13,
  389. LLVMDWARFTypeEncoding_UnsignedFixed = 14,
  390. LLVMDWARFTypeEncoding_DecimalFloat = 15,
  391. LLVMDWARFTypeEncoding_Utf = 16,
  392. LLVMDWARFTypeEncoding_LoUser = 128,
  393. LLVMDWARFTypeEncoding_HiUser = 255
  394. };
  395. enum {
  396. DW_TAG_array_type = 1,
  397. DW_TAG_enumeration_type = 4,
  398. DW_TAG_structure_type = 19,
  399. DW_TAG_union_type = 23,
  400. DW_TAG_vector_type = 259,
  401. DW_TAG_subroutine_type = 21,
  402. DW_TAG_inheritance = 28,
  403. };
  404. enum : LLVMAttributeIndex {
  405. LLVMAttributeIndex_ReturnIndex = 0u,
  406. LLVMAttributeIndex_FunctionIndex = ~0u,
  407. LLVMAttributeIndex_FirstArgIndex = 1,
  408. };