llvm_backend.hpp 12 KB

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