llvm_backend.hpp 21 KB

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