llvm_backend.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #if defined(GB_SYSTEM_WINDOWS)
  2. #include <llvm-c/Config/llvm-config.h>
  3. #else
  4. #include <llvm/Config/llvm-config.h>
  5. #endif
  6. #include <llvm-c/Core.h>
  7. #include <llvm-c/ExecutionEngine.h>
  8. #include <llvm-c/Target.h>
  9. #include <llvm-c/Analysis.h>
  10. #include <llvm-c/Object.h>
  11. #include <llvm-c/BitWriter.h>
  12. #include <llvm-c/DebugInfo.h>
  13. #if LLVM_VERSION_MAJOR >= 17
  14. #include <llvm-c/Transforms/PassBuilder.h>
  15. #else
  16. #include <llvm-c/Transforms/AggressiveInstCombine.h>
  17. #include <llvm-c/Transforms/InstCombine.h>
  18. #include <llvm-c/Transforms/IPO.h>
  19. #include <llvm-c/Transforms/PassManagerBuilder.h>
  20. #include <llvm-c/Transforms/Scalar.h>
  21. #include <llvm-c/Transforms/Utils.h>
  22. #include <llvm-c/Transforms/Vectorize.h>
  23. #endif
  24. #if LLVM_VERSION_MAJOR < 11
  25. #error "LLVM Version 11 is the minimum required"
  26. #elif LLVM_VERSION_MAJOR == 12 && !(LLVM_VERSION_MINOR > 0 || LLVM_VERSION_PATCH > 0)
  27. #error "If LLVM Version 12.x.y is wanted, at least LLVM 12.0.1 is required"
  28. #endif
  29. #if LLVM_VERSION_MAJOR > 12 || (LLVM_VERSION_MAJOR == 12 && LLVM_VERSION_MINOR >= 0 && LLVM_VERSION_PATCH > 0)
  30. #define ODIN_LLVM_MINIMUM_VERSION_12 1
  31. #else
  32. #define ODIN_LLVM_MINIMUM_VERSION_12 0
  33. #endif
  34. #if LLVM_VERSION_MAJOR > 13 || (LLVM_VERSION_MAJOR == 13 && LLVM_VERSION_MINOR >= 0 && LLVM_VERSION_PATCH > 0)
  35. #define ODIN_LLVM_MINIMUM_VERSION_13 1
  36. #else
  37. #define ODIN_LLVM_MINIMUM_VERSION_13 0
  38. #endif
  39. #if LLVM_VERSION_MAJOR > 14 || (LLVM_VERSION_MAJOR == 14 && LLVM_VERSION_MINOR >= 0 && LLVM_VERSION_PATCH > 0)
  40. #define ODIN_LLVM_MINIMUM_VERSION_14 1
  41. #else
  42. #define ODIN_LLVM_MINIMUM_VERSION_14 0
  43. #endif
  44. #if LLVM_VERSION_MAJOR == 15 || LLVM_VERSION_MAJOR == 16
  45. #error "LLVM versions 15 and 16 are not supported"
  46. #endif
  47. #if LLVM_VERSION_MAJOR >= 17
  48. #define LB_USE_NEW_PASS_SYSTEM 1
  49. #else
  50. #define LB_USE_NEW_PASS_SYSTEM 0
  51. #endif
  52. gb_internal bool lb_use_new_pass_system(void) {
  53. return LB_USE_NEW_PASS_SYSTEM;
  54. }
  55. struct lbProcedure;
  56. struct lbValue {
  57. LLVMValueRef value;
  58. Type *type;
  59. };
  60. enum lbAddrKind {
  61. lbAddr_Default,
  62. lbAddr_Map,
  63. lbAddr_Context,
  64. lbAddr_SoaVariable,
  65. lbAddr_RelativePointer,
  66. lbAddr_Swizzle,
  67. lbAddr_SwizzleLarge,
  68. lbAddr_BitField,
  69. };
  70. struct lbAddr {
  71. lbAddrKind kind;
  72. lbValue addr;
  73. union {
  74. struct {
  75. lbValue key;
  76. Type *type;
  77. Type *result;
  78. } map;
  79. struct {
  80. Selection sel;
  81. } ctx;
  82. struct {
  83. lbValue index;
  84. Ast *index_expr;
  85. } soa;
  86. struct {
  87. lbValue index;
  88. Ast *node;
  89. } index_set;
  90. struct {
  91. bool deref;
  92. } relative;
  93. struct {
  94. Type *type;
  95. u8 count; // 2, 3, or 4 components
  96. u8 indices[4];
  97. } swizzle;
  98. struct {
  99. Type *type;
  100. Slice<i32> indices;
  101. } swizzle_large;
  102. struct {
  103. Type *type;
  104. i64 bit_offset;
  105. i64 bit_size;
  106. } bitfield;
  107. };
  108. };
  109. struct lbIncompleteDebugType {
  110. Type *type;
  111. LLVMMetadataRef metadata;
  112. };
  113. typedef Slice<i32> lbStructFieldRemapping;
  114. enum lbFunctionPassManagerKind {
  115. lbFunctionPassManager_default,
  116. lbFunctionPassManager_default_without_memcpy,
  117. lbFunctionPassManager_none,
  118. lbFunctionPassManager_minimal,
  119. lbFunctionPassManager_size,
  120. lbFunctionPassManager_speed,
  121. lbFunctionPassManager_aggressive,
  122. lbFunctionPassManager_COUNT
  123. };
  124. struct lbModule {
  125. LLVMModuleRef mod;
  126. LLVMContextRef ctx;
  127. struct lbGenerator *gen;
  128. LLVMTargetMachineRef target_machine;
  129. CheckerInfo *info;
  130. AstPackage *pkg; // possibly associated
  131. AstFile *file; // possibly associated
  132. PtrMap<Type *, LLVMTypeRef> types; // mutex: types_mutex
  133. PtrMap<void *, lbStructFieldRemapping> struct_field_remapping; // Key: LLVMTypeRef or Type *, mutex: types_mutex
  134. PtrMap<Type *, LLVMTypeRef> func_raw_types; // mutex: func_raw_types_mutex
  135. RecursiveMutex types_mutex;
  136. RecursiveMutex func_raw_types_mutex;
  137. i32 internal_type_level;
  138. RwMutex values_mutex;
  139. PtrMap<Entity *, lbValue> values;
  140. PtrMap<Entity *, lbAddr> soa_values;
  141. StringMap<lbValue> members;
  142. StringMap<lbProcedure *> procedures;
  143. PtrMap<LLVMValueRef, Entity *> procedure_values;
  144. Array<lbProcedure *> missing_procedures_to_check;
  145. StringMap<LLVMValueRef> const_strings;
  146. PtrMap<Type *, struct lbFunctionType *> function_type_map;
  147. PtrMap<Type *, lbProcedure *> equal_procs;
  148. PtrMap<Type *, lbProcedure *> hasher_procs;
  149. PtrMap<Type *, lbProcedure *> map_get_procs;
  150. PtrMap<Type *, lbProcedure *> map_set_procs;
  151. std::atomic<u32> nested_type_name_guid;
  152. Array<lbProcedure *> procedures_to_generate;
  153. Array<Entity *> global_procedures_to_create;
  154. Array<Entity *> global_types_to_create;
  155. lbProcedure *curr_procedure;
  156. LLVMBuilderRef const_dummy_builder;
  157. LLVMDIBuilderRef debug_builder;
  158. LLVMMetadataRef debug_compile_unit;
  159. RecursiveMutex debug_values_mutex;
  160. PtrMap<void *, LLVMMetadataRef> debug_values;
  161. StringMap<lbAddr> objc_classes;
  162. StringMap<lbAddr> objc_selectors;
  163. PtrMap<Type *, lbAddr> map_cell_info_map; // address of runtime.Map_Info
  164. PtrMap<Type *, lbAddr> map_info_map; // address of runtime.Map_Cell_Info
  165. PtrMap<Ast *, lbAddr> exact_value_compound_literal_addr_map; // Key: Ast_CompoundLit
  166. LLVMPassManagerRef function_pass_managers[lbFunctionPassManager_COUNT];
  167. };
  168. struct lbGenerator : LinkerData {
  169. CheckerInfo *info;
  170. PtrMap<void *, lbModule *> modules; // key is `AstPackage *` (`void *` is used for future use)
  171. PtrMap<LLVMContextRef, lbModule *> modules_through_ctx;
  172. lbModule default_module;
  173. RecursiveMutex anonymous_proc_lits_mutex;
  174. PtrMap<Ast *, lbProcedure *> anonymous_proc_lits;
  175. std::atomic<u32> global_array_index;
  176. std::atomic<u32> global_generated_index;
  177. isize used_module_count;
  178. lbProcedure *startup_runtime;
  179. lbProcedure *cleanup_runtime;
  180. lbProcedure *objc_names;
  181. };
  182. struct lbBlock {
  183. LLVMBasicBlockRef block;
  184. Scope *scope;
  185. isize scope_index;
  186. bool appended;
  187. Array<lbBlock *> preds;
  188. Array<lbBlock *> succs;
  189. };
  190. struct lbBranchBlocks {
  191. Ast *label;
  192. lbBlock *break_;
  193. lbBlock *continue_;
  194. };
  195. struct lbContextData {
  196. lbAddr ctx;
  197. isize scope_index;
  198. isize uses;
  199. };
  200. enum lbParamPasskind {
  201. lbParamPass_Value, // Pass by value
  202. lbParamPass_Pointer, // Pass as a pointer rather than by value
  203. lbParamPass_Integer, // Pass as an integer of the same size
  204. lbParamPass_ConstRef, // Pass as a pointer but the value is immutable
  205. lbParamPass_BitCast, // Pass by value and bit cast to the correct type
  206. lbParamPass_Tuple, // Pass across multiple parameters (System V AMD64, up to 2)
  207. };
  208. enum lbDeferExitKind {
  209. lbDeferExit_Default,
  210. lbDeferExit_Return,
  211. lbDeferExit_Branch,
  212. };
  213. enum lbDeferKind {
  214. lbDefer_Node,
  215. lbDefer_Proc,
  216. };
  217. struct lbDefer {
  218. lbDeferKind kind;
  219. isize scope_index;
  220. isize context_stack_count;
  221. lbBlock * block;
  222. union {
  223. Ast *stmt;
  224. struct {
  225. lbValue deferred;
  226. Array<lbValue> result_as_args;
  227. } proc;
  228. };
  229. };
  230. struct lbTargetList {
  231. lbTargetList *prev;
  232. bool is_block;
  233. lbBlock * break_;
  234. lbBlock * continue_;
  235. lbBlock * fallthrough_;
  236. };
  237. struct lbTupleFix {
  238. Slice<lbValue> values;
  239. };
  240. enum lbProcedureFlag : u32 {
  241. lbProcedureFlag_WithoutMemcpyPass = 1<<0,
  242. lbProcedureFlag_DebugAllocaCopy = 1<<1,
  243. };
  244. struct lbProcedure {
  245. u32 flags;
  246. u16 state_flags;
  247. lbProcedure *parent;
  248. Array<lbProcedure *> children;
  249. Entity * entity;
  250. lbModule * module;
  251. String name;
  252. Type * type;
  253. Ast * type_expr;
  254. Ast * body;
  255. u64 tags;
  256. ProcInlining inlining;
  257. bool is_foreign;
  258. bool is_export;
  259. bool is_entry_point;
  260. bool is_startup;
  261. lbFunctionType *abi_function_type;
  262. LLVMValueRef value;
  263. LLVMBuilderRef builder;
  264. bool is_done;
  265. lbAddr return_ptr;
  266. Array<lbDefer> defer_stmts;
  267. Array<lbBlock *> blocks;
  268. Array<lbBranchBlocks> branch_blocks;
  269. Scope * curr_scope;
  270. i32 scope_index;
  271. lbBlock * decl_block;
  272. lbBlock * entry_block;
  273. lbBlock * curr_block;
  274. lbTargetList * target_list;
  275. PtrMap<Entity *, lbValue> direct_parameters;
  276. bool in_multi_assignment;
  277. Array<LLVMValueRef> raw_input_parameters;
  278. LLVMValueRef temp_callee_return_struct_memory;
  279. Ast *curr_stmt;
  280. Array<Scope *> scope_stack;
  281. Array<lbContextData> context_stack;
  282. LLVMMetadataRef debug_info;
  283. PtrMap<Ast *, lbValue> selector_values;
  284. PtrMap<Ast *, lbAddr> selector_addr;
  285. PtrMap<LLVMValueRef, lbTupleFix> tuple_fix_map;
  286. };
  287. #ifndef ABI_PKG_NAME_SEPARATOR
  288. #define ABI_PKG_NAME_SEPARATOR "."
  289. #endif
  290. #if !ODIN_LLVM_MINIMUM_VERSION_14
  291. #define LLVMConstGEP2(Ty__, ConstantVal__, ConstantIndices__, NumIndices__) LLVMConstGEP(ConstantVal__, ConstantIndices__, NumIndices__)
  292. #define LLVMConstInBoundsGEP2(Ty__, ConstantVal__, ConstantIndices__, NumIndices__) LLVMConstInBoundsGEP(ConstantVal__, ConstantIndices__, NumIndices__)
  293. #define LLVMBuildPtrDiff2(Builder__, Ty__, LHS__, RHS__, Name__) LLVMBuildPtrDiff(Builder__, LHS__, RHS__, Name__)
  294. #endif
  295. gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c);
  296. gb_internal String lb_mangle_name(lbModule *m, Entity *e);
  297. gb_internal String lb_get_entity_name(lbModule *m, Entity *e, String name = {});
  298. gb_internal LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value=0);
  299. gb_internal LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char const *name, LLVMTypeRef type);
  300. gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value);
  301. gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name);
  302. gb_internal lbProcedure *lb_create_procedure(lbModule *module, Entity *entity, bool ignore_body=false);
  303. gb_internal void lb_end_procedure(lbProcedure *p);
  304. gb_internal LLVMTypeRef lb_type(lbModule *m, Type *type);
  305. gb_internal LLVMTypeRef llvm_get_element_type(LLVMTypeRef type);
  306. gb_internal lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append=false);
  307. gb_internal lbValue lb_const_nil(lbModule *m, Type *type);
  308. gb_internal lbValue lb_const_undef(lbModule *m, Type *type);
  309. gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local=true);
  310. gb_internal lbValue lb_const_bool(lbModule *m, Type *type, bool value);
  311. gb_internal lbValue lb_const_int(lbModule *m, Type *type, u64 value);
  312. gb_internal lbAddr lb_addr(lbValue addr);
  313. gb_internal Type *lb_addr_type(lbAddr const &addr);
  314. gb_internal LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val);
  315. gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value);
  316. gb_internal lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr);
  317. gb_internal lbValue lb_emit_load(lbProcedure *p, lbValue v);
  318. gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value);
  319. gb_internal void lb_build_stmt(lbProcedure *p, Ast *stmt);
  320. gb_internal lbValue lb_build_expr(lbProcedure *p, Ast *expr);
  321. gb_internal lbAddr lb_build_addr(lbProcedure *p, Ast *expr);
  322. gb_internal void lb_build_stmt_list(lbProcedure *p, Array<Ast *> const &stmts);
  323. gb_internal lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index);
  324. gb_internal lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index);
  325. gb_internal lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index);
  326. gb_internal lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index);
  327. gb_internal lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index);
  328. gb_internal lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index);
  329. gb_internal lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index);
  330. gb_internal lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index);
  331. gb_internal lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel);
  332. gb_internal lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel);
  333. gb_internal lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column);
  334. gb_internal lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column);
  335. gb_internal lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column);
  336. gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type);
  337. gb_internal lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type);
  338. gb_internal void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block);
  339. gb_internal lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t);
  340. gb_internal lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right);
  341. gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args, ProcInlining inlining = ProcInlining_none);
  342. gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
  343. gb_internal lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x);
  344. gb_internal void lb_emit_jump(lbProcedure *p, lbBlock *target_block);
  345. gb_internal void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block);
  346. gb_internal void lb_start_block(lbProcedure *p, lbBlock *b);
  347. gb_internal lbValue lb_build_call_expr(lbProcedure *p, Ast *expr);
  348. gb_internal lbAddr lb_find_or_generate_context_ptr(lbProcedure *p);
  349. gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx);
  350. gb_internal lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p);
  351. gb_internal lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr);
  352. gb_internal lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, bool force_no_init=false);
  353. gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e);
  354. gb_internal lbValue lb_typeid(lbModule *m, Type *type);
  355. gb_internal lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value);
  356. gb_internal lbValue lb_address_from_load(lbProcedure *p, lbValue value);
  357. gb_internal void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt);
  358. gb_internal lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init);
  359. gb_internal lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array<lbValue> const &args);
  360. gb_internal lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index);
  361. gb_internal lbValue lb_const_ptr_offset(lbModule *m, lbValue ptr, lbValue index);
  362. gb_internal lbValue lb_string_elem(lbProcedure *p, lbValue string);
  363. gb_internal lbValue lb_string_len(lbProcedure *p, lbValue string);
  364. gb_internal lbValue lb_cstring_len(lbProcedure *p, lbValue value);
  365. gb_internal lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr);
  366. gb_internal lbValue lb_slice_elem(lbProcedure *p, lbValue slice);
  367. gb_internal lbValue lb_slice_len(lbProcedure *p, lbValue slice);
  368. gb_internal lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da);
  369. gb_internal lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da);
  370. gb_internal lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da);
  371. gb_internal lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da);
  372. gb_internal lbValue lb_map_len(lbProcedure *p, lbValue value);
  373. gb_internal lbValue lb_map_cap(lbProcedure *p, lbValue value);
  374. gb_internal lbValue lb_soa_struct_len(lbProcedure *p, lbValue value);
  375. gb_internal void lb_emit_increment(lbProcedure *p, lbValue addr);
  376. gb_internal lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y);
  377. gb_internal lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t);
  378. gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len);
  379. gb_internal lbValue lb_type_info(lbProcedure *p, Type *type);
  380. gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str);
  381. gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr);
  382. gb_internal bool lb_is_const(lbValue value);
  383. gb_internal bool lb_is_const_or_global(lbValue value);
  384. gb_internal bool lb_is_const_nil(lbValue value);
  385. gb_internal String lb_get_const_string(lbModule *m, lbValue value);
  386. gb_internal lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true);
  387. gb_internal lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id);
  388. gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue const &map_ptr, lbValue key, lbValue *key_ptr_);
  389. gb_internal lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type);
  390. gb_internal lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type);
  391. gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key);
  392. gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node);
  393. gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos);
  394. gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e);
  395. gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e);
  396. gb_internal void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value, bool is_default_case);
  397. gb_internal lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value);
  398. gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos);
  399. gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure, TokenPos const &pos);
  400. gb_internal lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const &param_value, TokenPos const &pos);
  401. gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type);
  402. gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type);
  403. gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t);
  404. gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type);
  405. gb_internal lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type);
  406. gb_internal lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type);
  407. gb_internal lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type);
  408. gb_internal lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type);
  409. gb_internal lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type);
  410. gb_internal lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x);
  411. gb_internal void lb_mem_zero_addr(lbProcedure *p, LLVMValueRef ptr, Type *type);
  412. gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e);
  413. gb_internal lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type);
  414. gb_internal lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block);
  415. gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_);
  416. gb_internal LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_);
  417. gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name);
  418. gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t);
  419. gb_internal bool lb_is_expr_untyped_const(Ast *expr);
  420. gb_internal LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name = "");
  421. gb_internal void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment);
  422. gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr);
  423. gb_internal lbBranchBlocks lb_lookup_branch_blocks(lbProcedure *p, Ast *ident);
  424. gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t);
  425. gb_internal LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align);
  426. gb_internal LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask);
  427. gb_internal LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count);
  428. gb_internal void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false);
  429. gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false);
  430. gb_internal LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile);
  431. gb_internal LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, usize len, unsigned alignment, bool is_volatile);
  432. gb_internal gb_inline i64 lb_max_zero_init_size(void) {
  433. return cast(i64)(4*build_context.int_size);
  434. }
  435. gb_internal LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type);
  436. gb_internal LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type);
  437. gb_internal String lb_filepath_ll_for_module(lbModule *m);
  438. gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type);
  439. gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos);
  440. gb_internal LLVMMetadataRef lb_debug_location_from_token_pos(lbProcedure *p, TokenPos pos);
  441. gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t ElementCount) {
  442. #if LB_USE_NEW_PASS_SYSTEM
  443. return LLVMArrayType2(ElementType, ElementCount);
  444. #else
  445. return LLVMArrayType(ElementType, cast(unsigned)ElementCount);
  446. #endif
  447. }
  448. gb_internal void lb_set_metadata_custom_u64(lbModule *m, LLVMValueRef v_ref, String name, u64 value);
  449. gb_internal u64 lb_get_metadata_custom_u64(lbModule *m, LLVMValueRef v_ref, String name);
  450. #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
  451. #define LB_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime"
  452. #define LB_TYPE_INFO_DATA_NAME "__$type_info_data"
  453. #define LB_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
  454. #define LB_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
  455. #define LB_TYPE_INFO_OFFSETS_NAME "__$type_info_offsets_data"
  456. #define LB_TYPE_INFO_USINGS_NAME "__$type_info_usings_data"
  457. #define LB_TYPE_INFO_TAGS_NAME "__$type_info_tags_data"
  458. enum lbCallingConventionKind : unsigned {
  459. lbCallingConvention_C = 0,
  460. lbCallingConvention_Fast = 8,
  461. lbCallingConvention_Cold = 9,
  462. lbCallingConvention_GHC = 10,
  463. lbCallingConvention_HiPE = 11,
  464. lbCallingConvention_WebKit_JS = 12,
  465. lbCallingConvention_AnyReg = 13,
  466. lbCallingConvention_PreserveMost = 14,
  467. lbCallingConvention_PreserveAll = 15,
  468. lbCallingConvention_Swift = 16,
  469. lbCallingConvention_CXX_FAST_TLS = 17,
  470. lbCallingConvention_FirstTargetCC = 64,
  471. lbCallingConvention_X86_StdCall = 64,
  472. lbCallingConvention_X86_FastCall = 65,
  473. lbCallingConvention_ARM_APCS = 66,
  474. lbCallingConvention_ARM_AAPCS = 67,
  475. lbCallingConvention_ARM_AAPCS_VFP = 68,
  476. lbCallingConvention_MSP430_INTR = 69,
  477. lbCallingConvention_X86_ThisCall = 70,
  478. lbCallingConvention_PTX_Kernel = 71,
  479. lbCallingConvention_PTX_Device = 72,
  480. lbCallingConvention_SPIR_FUNC = 75,
  481. lbCallingConvention_SPIR_KERNEL = 76,
  482. lbCallingConvention_Intel_OCL_BI = 77,
  483. lbCallingConvention_X86_64_SysV = 78,
  484. lbCallingConvention_Win64 = 79,
  485. lbCallingConvention_X86_VectorCall = 80,
  486. lbCallingConvention_HHVM = 81,
  487. lbCallingConvention_HHVM_C = 82,
  488. lbCallingConvention_X86_INTR = 83,
  489. lbCallingConvention_AVR_INTR = 84,
  490. lbCallingConvention_AVR_SIGNAL = 85,
  491. lbCallingConvention_AVR_BUILTIN = 86,
  492. lbCallingConvention_AMDGPU_VS = 87,
  493. lbCallingConvention_AMDGPU_GS = 88,
  494. lbCallingConvention_AMDGPU_PS = 89,
  495. lbCallingConvention_AMDGPU_CS = 90,
  496. lbCallingConvention_AMDGPU_KERNEL = 91,
  497. lbCallingConvention_X86_RegCall = 92,
  498. lbCallingConvention_AMDGPU_HS = 93,
  499. lbCallingConvention_MSP430_BUILTIN = 94,
  500. lbCallingConvention_AMDGPU_LS = 95,
  501. lbCallingConvention_AMDGPU_ES = 96,
  502. lbCallingConvention_AArch64_VectorCall = 97,
  503. lbCallingConvention_AArch64_SVE_VectorCall = 98,
  504. lbCallingConvention_WASM_EmscriptenInvoke = 99,
  505. lbCallingConvention_MaxID = 1023,
  506. };
  507. lbCallingConventionKind const lb_calling_convention_map[ProcCC_MAX] = {
  508. lbCallingConvention_C, // ProcCC_Invalid,
  509. lbCallingConvention_C, // ProcCC_Odin,
  510. lbCallingConvention_C, // ProcCC_Contextless,
  511. lbCallingConvention_C, // ProcCC_CDecl,
  512. lbCallingConvention_X86_StdCall, // ProcCC_StdCall,
  513. lbCallingConvention_X86_FastCall, // ProcCC_FastCall,
  514. lbCallingConvention_C, // ProcCC_None,
  515. lbCallingConvention_C, // ProcCC_Naked,
  516. lbCallingConvention_C, // ProcCC_InlineAsm,
  517. lbCallingConvention_Win64, // ProcCC_Win64,
  518. lbCallingConvention_X86_64_SysV, // ProcCC_SysV,
  519. };
  520. enum : LLVMDWARFTypeEncoding {
  521. LLVMDWARFTypeEncoding_Address = 1,
  522. LLVMDWARFTypeEncoding_Boolean = 2,
  523. LLVMDWARFTypeEncoding_ComplexFloat = 3,
  524. LLVMDWARFTypeEncoding_Float = 4,
  525. LLVMDWARFTypeEncoding_Signed = 5,
  526. LLVMDWARFTypeEncoding_SignedChar = 6,
  527. LLVMDWARFTypeEncoding_Unsigned = 7,
  528. LLVMDWARFTypeEncoding_UnsignedChar = 8,
  529. LLVMDWARFTypeEncoding_ImaginaryFloat = 9,
  530. LLVMDWARFTypeEncoding_PackedDecimal = 10,
  531. LLVMDWARFTypeEncoding_NumericString = 11,
  532. LLVMDWARFTypeEncoding_Edited = 12,
  533. LLVMDWARFTypeEncoding_SignedFixed = 13,
  534. LLVMDWARFTypeEncoding_UnsignedFixed = 14,
  535. LLVMDWARFTypeEncoding_DecimalFloat = 15,
  536. LLVMDWARFTypeEncoding_Utf = 16,
  537. LLVMDWARFTypeEncoding_LoUser = 128,
  538. LLVMDWARFTypeEncoding_HiUser = 255
  539. };
  540. enum {
  541. DW_TAG_array_type = 1,
  542. DW_TAG_enumeration_type = 4,
  543. DW_TAG_structure_type = 19,
  544. DW_TAG_union_type = 23,
  545. DW_TAG_vector_type = 259,
  546. DW_TAG_subroutine_type = 21,
  547. DW_TAG_inheritance = 28,
  548. };
  549. enum : LLVMAttributeIndex {
  550. LLVMAttributeIndex_ReturnIndex = 0u,
  551. LLVMAttributeIndex_FunctionIndex = ~0u,
  552. LLVMAttributeIndex_FirstArgIndex = 1,
  553. };
  554. gb_global char const *llvm_linkage_strings[] = {
  555. "external linkage",
  556. "available externally linkage",
  557. "link once any linkage",
  558. "link once odr linkage",
  559. "link once odr auto hide linkage",
  560. "weak any linkage",
  561. "weak odr linkage",
  562. "appending linkage",
  563. "internal linkage",
  564. "private linkage",
  565. "dllimport linkage",
  566. "dllexport linkage",
  567. "external weak linkage",
  568. "ghost linkage",
  569. "common linkage",
  570. "linker private linkage",
  571. "linker private weak linkage"
  572. };
  573. #define ODIN_METADATA_IS_PACKED str_lit("odin-is-packed")