llvm_backend.hpp 13 KB

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