llvm_backend.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. #define MULTITHREAD_OBJECT_GENERATION 1
  2. #ifndef USE_SEPARATE_MODULES
  3. #define USE_SEPARATE_MODULES build_context.use_separate_modules
  4. #endif
  5. #ifndef MULTITHREAD_OBJECT_GENERATION
  6. #define MULTITHREAD_OBJECT_GENERATION 0
  7. #endif
  8. #include "llvm_backend.hpp"
  9. #include "llvm_abi.cpp"
  10. #include "llvm_backend_opt.cpp"
  11. #include "llvm_backend_general.cpp"
  12. #include "llvm_backend_debug.cpp"
  13. #include "llvm_backend_const.cpp"
  14. #include "llvm_backend_type.cpp"
  15. #include "llvm_backend_utility.cpp"
  16. #include "llvm_backend_expr.cpp"
  17. #include "llvm_backend_stmt.cpp"
  18. #include "llvm_backend_proc.cpp"
  19. void lb_add_foreign_library_path(lbModule *m, Entity *e) {
  20. if (e == nullptr) {
  21. return;
  22. }
  23. GB_ASSERT(e->kind == Entity_LibraryName);
  24. GB_ASSERT(e->flags & EntityFlag_Used);
  25. for_array(i, e->LibraryName.paths) {
  26. String library_path = e->LibraryName.paths[i];
  27. if (library_path.len == 0) {
  28. continue;
  29. }
  30. bool ok = true;
  31. for_array(path_index, m->foreign_library_paths) {
  32. String path = m->foreign_library_paths[path_index];
  33. #if defined(GB_SYSTEM_WINDOWS)
  34. if (str_eq_ignore_case(path, library_path)) {
  35. #else
  36. if (str_eq(path, library_path)) {
  37. #endif
  38. ok = false;
  39. break;
  40. }
  41. }
  42. if (ok) {
  43. array_add(&m->foreign_library_paths, library_path);
  44. }
  45. }
  46. }
  47. void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name) {
  48. if (other_module == nullptr) {
  49. return;
  50. }
  51. char const *cname = alloc_cstring(temporary_allocator(), name);
  52. LLVMValueRef other_global = nullptr;
  53. if (e->kind == Entity_Variable) {
  54. other_global = LLVMGetNamedGlobal(other_module->mod, cname);
  55. } else if (e->kind == Entity_Procedure) {
  56. other_global = LLVMGetNamedFunction(other_module->mod, cname);
  57. }
  58. if (other_global) {
  59. LLVMSetLinkage(other_global, LLVMExternalLinkage);
  60. }
  61. }
  62. void lb_emit_init_context(lbProcedure *p, lbAddr addr) {
  63. GB_ASSERT(addr.kind == lbAddr_Context);
  64. GB_ASSERT(addr.ctx.sel.index.count == 0);
  65. auto args = array_make<lbValue>(permanent_allocator(), 1);
  66. args[0] = addr.addr;
  67. lb_emit_runtime_call(p, "__init_context", args);
  68. }
  69. lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p) {
  70. Type *pt = base_type(p->type);
  71. GB_ASSERT(pt->kind == Type_Proc);
  72. GB_ASSERT(pt->Proc.calling_convention == ProcCC_Odin);
  73. String name = str_lit("__.context_ptr");
  74. Entity *e = alloc_entity_param(nullptr, make_token_ident(name), t_context_ptr, false, false);
  75. e->flags |= EntityFlag_NoAlias;
  76. LLVMValueRef context_ptr = LLVMGetParam(p->value, LLVMCountParams(p->value)-1);
  77. LLVMSetValueName2(context_ptr, cast(char const *)name.text, name.len);
  78. context_ptr = LLVMBuildPointerCast(p->builder, context_ptr, lb_type(p->module, e->type), "");
  79. lbValue param = {context_ptr, e->type};
  80. lb_add_entity(p->module, e, param);
  81. lbAddr ctx_addr = {};
  82. ctx_addr.kind = lbAddr_Context;
  83. ctx_addr.addr = param;
  84. lbContextData *cd = array_add_and_get(&p->context_stack);
  85. cd->ctx = ctx_addr;
  86. cd->scope_index = -1;
  87. cd->uses = +1; // make sure it has been used already
  88. return cd;
  89. }
  90. lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) {
  91. ctx.kind = lbAddr_Context;
  92. lbContextData *cd = array_add_and_get(&p->context_stack);
  93. cd->ctx = ctx;
  94. cd->scope_index = p->scope_index;
  95. return cd;
  96. }
  97. lbValue lb_get_equal_proc_for_type(lbModule *m, Type *type) {
  98. type = base_type(type);
  99. GB_ASSERT(is_type_comparable(type));
  100. Type *pt = alloc_type_pointer(type);
  101. LLVMTypeRef ptr_type = lb_type(m, pt);
  102. lbProcedure **found = map_get(&m->equal_procs, type);
  103. lbProcedure *compare_proc = nullptr;
  104. if (found) {
  105. compare_proc = *found;
  106. GB_ASSERT(compare_proc != nullptr);
  107. return {compare_proc->value, compare_proc->type};
  108. }
  109. static u32 proc_index = 0;
  110. char buf[16] = {};
  111. isize n = gb_snprintf(buf, 16, "__$equal%u", ++proc_index);
  112. char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
  113. String proc_name = make_string_c(str);
  114. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
  115. map_set(&m->equal_procs, type, p);
  116. lb_begin_procedure_body(p);
  117. LLVMValueRef x = LLVMGetParam(p->value, 0);
  118. LLVMValueRef y = LLVMGetParam(p->value, 1);
  119. x = LLVMBuildPointerCast(p->builder, x, ptr_type, "");
  120. y = LLVMBuildPointerCast(p->builder, y, ptr_type, "");
  121. lbValue lhs = {x, pt};
  122. lbValue rhs = {y, pt};
  123. lbBlock *block_same_ptr = lb_create_block(p, "same_ptr");
  124. lbBlock *block_diff_ptr = lb_create_block(p, "diff_ptr");
  125. lbValue same_ptr = lb_emit_comp(p, Token_CmpEq, lhs, rhs);
  126. lb_emit_if(p, same_ptr, block_same_ptr, block_diff_ptr);
  127. lb_start_block(p, block_same_ptr);
  128. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  129. lb_start_block(p, block_diff_ptr);
  130. if (type->kind == Type_Struct) {
  131. type_set_offsets(type);
  132. lbBlock *block_false = lb_create_block(p, "bfalse");
  133. lbValue res = lb_const_bool(m, t_bool, true);
  134. for_array(i, type->Struct.fields) {
  135. lbBlock *next_block = lb_create_block(p, "btrue");
  136. lbValue pleft = lb_emit_struct_ep(p, lhs, cast(i32)i);
  137. lbValue pright = lb_emit_struct_ep(p, rhs, cast(i32)i);
  138. lbValue left = lb_emit_load(p, pleft);
  139. lbValue right = lb_emit_load(p, pright);
  140. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  141. lb_emit_if(p, ok, next_block, block_false);
  142. lb_emit_jump(p, next_block);
  143. lb_start_block(p, next_block);
  144. }
  145. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false));
  146. lb_start_block(p, block_false);
  147. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
  148. } else if (type->kind == Type_Union) {
  149. if (is_type_union_maybe_pointer(type)) {
  150. Type *v = type->Union.variants[0];
  151. Type *pv = alloc_type_pointer(v);
  152. lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, pv));
  153. lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, pv));
  154. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  155. ok = lb_emit_conv(p, ok, t_bool);
  156. LLVMBuildRet(p->builder, ok.value);
  157. } else {
  158. lbBlock *block_false = lb_create_block(p, "bfalse");
  159. lbBlock *block_switch = lb_create_block(p, "bswitch");
  160. lbValue left_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, lhs));
  161. lbValue right_tag = lb_emit_load(p, lb_emit_union_tag_ptr(p, rhs));
  162. lbValue tag_eq = lb_emit_comp(p, Token_CmpEq, left_tag, right_tag);
  163. lb_emit_if(p, tag_eq, block_switch, block_false);
  164. lb_start_block(p, block_switch);
  165. LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, left_tag.value, block_false->block, cast(unsigned)type->Union.variants.count);
  166. for_array(i, type->Union.variants) {
  167. lbBlock *case_block = lb_create_block(p, "bcase");
  168. lb_start_block(p, case_block);
  169. Type *v = type->Union.variants[i];
  170. lbValue case_tag = lb_const_union_tag(p->module, type, v);
  171. Type *vp = alloc_type_pointer(v);
  172. lbValue left = lb_emit_load(p, lb_emit_conv(p, lhs, vp));
  173. lbValue right = lb_emit_load(p, lb_emit_conv(p, rhs, vp));
  174. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  175. ok = lb_emit_conv(p, ok, t_bool);
  176. LLVMBuildRet(p->builder, ok.value);
  177. LLVMAddCase(v_switch, case_tag.value, case_block->block);
  178. }
  179. lb_start_block(p, block_false);
  180. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false));
  181. }
  182. } else {
  183. lbValue left = lb_emit_load(p, lhs);
  184. lbValue right = lb_emit_load(p, rhs);
  185. lbValue ok = lb_emit_comp(p, Token_CmpEq, left, right);
  186. ok = lb_emit_conv(p, ok, t_bool);
  187. LLVMBuildRet(p->builder, ok.value);
  188. }
  189. lb_end_procedure_body(p);
  190. compare_proc = p;
  191. return {compare_proc->value, compare_proc->type};
  192. }
  193. lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) {
  194. GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
  195. i64 sz = type_size_of(type);
  196. if (1 <= sz && sz <= 16) {
  197. char name[20] = {};
  198. gb_snprintf(name, 20, "default_hasher%d", cast(i32)sz);
  199. auto args = array_make<lbValue>(permanent_allocator(), 2);
  200. args[0] = data;
  201. args[1] = seed;
  202. return lb_emit_runtime_call(p, name, args);
  203. }
  204. auto args = array_make<lbValue>(permanent_allocator(), 3);
  205. args[0] = data;
  206. args[1] = seed;
  207. args[2] = lb_const_int(p->module, t_int, type_size_of(type));
  208. return lb_emit_runtime_call(p, "default_hasher_n", args);
  209. }
  210. lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
  211. type = core_type(type);
  212. GB_ASSERT(is_type_valid_for_keys(type));
  213. Type *pt = alloc_type_pointer(type);
  214. lbProcedure **found = map_get(&m->hasher_procs, type);
  215. if (found) {
  216. GB_ASSERT(*found != nullptr);
  217. return {(*found)->value, (*found)->type};
  218. }
  219. static u32 proc_index = 0;
  220. char buf[16] = {};
  221. isize n = gb_snprintf(buf, 16, "__$hasher%u", ++proc_index);
  222. char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1);
  223. String proc_name = make_string_c(str);
  224. lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
  225. map_set(&m->hasher_procs, type, p);
  226. lb_begin_procedure_body(p);
  227. defer (lb_end_procedure_body(p));
  228. LLVMValueRef x = LLVMGetParam(p->value, 0);
  229. LLVMValueRef y = LLVMGetParam(p->value, 1);
  230. lbValue data = {x, t_rawptr};
  231. lbValue seed = {y, t_uintptr};
  232. LLVMAttributeRef nonnull_attr = lb_create_enum_attribute(m->ctx, "nonnull");
  233. LLVMAddAttributeAtIndex(p->value, 1+0, nonnull_attr);
  234. if (is_type_simple_compare(type)) {
  235. lbValue res = lb_simple_compare_hash(p, type, data, seed);
  236. LLVMBuildRet(p->builder, res.value);
  237. return {p->value, p->type};
  238. }
  239. if (type->kind == Type_Struct) {
  240. type_set_offsets(type);
  241. data = lb_emit_conv(p, data, t_u8_ptr);
  242. auto args = array_make<lbValue>(permanent_allocator(), 2);
  243. for_array(i, type->Struct.fields) {
  244. GB_ASSERT(type->Struct.offsets != nullptr);
  245. i64 offset = type->Struct.offsets[i];
  246. Entity *field = type->Struct.fields[i];
  247. lbValue field_hasher = lb_get_hasher_proc_for_type(m, field->type);
  248. lbValue ptr = lb_emit_ptr_offset(p, data, lb_const_int(m, t_uintptr, offset));
  249. args[0] = ptr;
  250. args[1] = seed;
  251. seed = lb_emit_call(p, field_hasher, args);
  252. }
  253. LLVMBuildRet(p->builder, seed.value);
  254. } else if (type->kind == Type_Union) {
  255. auto args = array_make<lbValue>(permanent_allocator(), 2);
  256. if (is_type_union_maybe_pointer(type)) {
  257. Type *v = type->Union.variants[0];
  258. lbValue variant_hasher = lb_get_hasher_proc_for_type(m, v);
  259. args[0] = data;
  260. args[1] = seed;
  261. lbValue res = lb_emit_call(p, variant_hasher, args);
  262. LLVMBuildRet(p->builder, res.value);
  263. }
  264. lbBlock *end_block = lb_create_block(p, "bend");
  265. data = lb_emit_conv(p, data, pt);
  266. lbValue tag_ptr = lb_emit_union_tag_ptr(p, data);
  267. lbValue tag = lb_emit_load(p, tag_ptr);
  268. LLVMValueRef v_switch = LLVMBuildSwitch(p->builder, tag.value, end_block->block, cast(unsigned)type->Union.variants.count);
  269. for_array(i, type->Union.variants) {
  270. lbBlock *case_block = lb_create_block(p, "bcase");
  271. lb_start_block(p, case_block);
  272. Type *v = type->Union.variants[i];
  273. lbValue case_tag = lb_const_union_tag(p->module, type, v);
  274. lbValue variant_hasher = lb_get_hasher_proc_for_type(m, v);
  275. args[0] = data;
  276. args[1] = seed;
  277. lbValue res = lb_emit_call(p, variant_hasher, args);
  278. LLVMBuildRet(p->builder, res.value);
  279. LLVMAddCase(v_switch, case_tag.value, case_block->block);
  280. }
  281. lb_start_block(p, end_block);
  282. LLVMBuildRet(p->builder, seed.value);
  283. } else if (type->kind == Type_Array) {
  284. lbAddr pres = lb_add_local_generated(p, t_uintptr, false);
  285. lb_addr_store(p, pres, seed);
  286. auto args = array_make<lbValue>(permanent_allocator(), 2);
  287. lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->Array.elem);
  288. auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32);
  289. data = lb_emit_conv(p, data, pt);
  290. lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
  291. args[0] = ptr;
  292. args[1] = lb_addr_load(p, pres);
  293. lbValue new_seed = lb_emit_call(p, elem_hasher, args);
  294. lb_addr_store(p, pres, new_seed);
  295. lb_loop_end(p, loop_data);
  296. lbValue res = lb_addr_load(p, pres);
  297. LLVMBuildRet(p->builder, res.value);
  298. } else if (type->kind == Type_EnumeratedArray) {
  299. lbAddr res = lb_add_local_generated(p, t_uintptr, false);
  300. lb_addr_store(p, res, seed);
  301. auto args = array_make<lbValue>(permanent_allocator(), 2);
  302. lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->EnumeratedArray.elem);
  303. auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32);
  304. data = lb_emit_conv(p, data, pt);
  305. lbValue ptr = lb_emit_array_ep(p, data, loop_data.idx);
  306. args[0] = ptr;
  307. args[1] = lb_addr_load(p, res);
  308. lbValue new_seed = lb_emit_call(p, elem_hasher, args);
  309. lb_addr_store(p, res, new_seed);
  310. lb_loop_end(p, loop_data);
  311. lbValue vres = lb_addr_load(p, res);
  312. LLVMBuildRet(p->builder, vres.value);
  313. } else if (is_type_cstring(type)) {
  314. auto args = array_make<lbValue>(permanent_allocator(), 2);
  315. args[0] = data;
  316. args[1] = seed;
  317. lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args);
  318. LLVMBuildRet(p->builder, res.value);
  319. } else if (is_type_string(type)) {
  320. auto args = array_make<lbValue>(permanent_allocator(), 2);
  321. args[0] = data;
  322. args[1] = seed;
  323. lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args);
  324. LLVMBuildRet(p->builder, res.value);
  325. } else {
  326. GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
  327. }
  328. return {p->value, p->type};
  329. }
  330. lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) {
  331. lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr);
  332. if (found) {
  333. return lb_find_procedure_value_from_entity(m, (*found)->entity);
  334. }
  335. ast_node(pl, ProcLit, expr);
  336. // NOTE(bill): Generate a new name
  337. // parent$count
  338. isize name_len = prefix_name.len + 1 + 8 + 1;
  339. char *name_text = gb_alloc_array(permanent_allocator(), char, name_len);
  340. i32 name_id = cast(i32)m->gen->anonymous_proc_lits.entries.count;
  341. name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id);
  342. String name = make_string((u8 *)name_text, name_len-1);
  343. Type *type = type_of_expr(expr);
  344. Token token = {};
  345. token.pos = ast_token(expr).pos;
  346. token.kind = Token_Ident;
  347. token.string = name;
  348. Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags);
  349. e->file = expr->file();
  350. e->decl_info = pl->decl;
  351. e->code_gen_module = m;
  352. e->flags |= EntityFlag_ProcBodyChecked;
  353. lbProcedure *p = lb_create_procedure(m, e);
  354. lbValue value = {};
  355. value.value = p->value;
  356. value.type = p->type;
  357. array_add(&m->procedures_to_generate, p);
  358. if (parent != nullptr) {
  359. array_add(&parent->children, p);
  360. } else {
  361. string_map_set(&m->members, name, value);
  362. }
  363. map_set(&m->gen->anonymous_proc_lits, expr, p);
  364. return value;
  365. }
  366. lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
  367. GB_ASSERT_MSG(is_type_pointer(map_val_ptr.type), "%s", type_to_string(map_val_ptr.type));
  368. lbAddr h = lb_add_local_generated(p, t_map_header, false); // all the values will be initialzed later
  369. map_type = base_type(map_type);
  370. GB_ASSERT(map_type->kind == Type_Map);
  371. Type *key_type = map_type->Map.key;
  372. Type *val_type = map_type->Map.value;
  373. gb_unused(val_type);
  374. GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
  375. map_type->Map.entry_type->cached_size = -1;
  376. map_type->Map.entry_type->Struct.are_offsets_set = false;
  377. i64 entry_size = type_size_of (map_type->Map.entry_type);
  378. i64 entry_align = type_align_of (map_type->Map.entry_type);
  379. i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
  380. i64 key_size = type_size_of (map_type->Map.key);
  381. i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
  382. i64 value_size = type_size_of (map_type->Map.value);
  383. Type *map_header_base = base_type(t_map_header);
  384. GB_ASSERT(map_header_base->Struct.fields.count == 8);
  385. Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
  386. LLVMValueRef const_values[8] = {};
  387. const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
  388. const_values[1] = lb_get_equal_proc_for_type(p->module, key_type) .value;
  389. const_values[2] = lb_const_int(p->module, t_int, entry_size) .value;
  390. const_values[3] = lb_const_int(p->module, t_int, entry_align) .value;
  391. const_values[4] = lb_const_int(p->module, t_uintptr, key_offset) .value;
  392. const_values[5] = lb_const_int(p->module, t_int, key_size) .value;
  393. const_values[6] = lb_const_int(p->module, t_uintptr, value_offset).value;
  394. const_values[7] = lb_const_int(p->module, t_int, value_size) .value;
  395. LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
  396. LLVMBuildStore(p->builder, const_value, h.addr.value);
  397. // NOTE(bill): Removes unnecessary allocation if split gep
  398. lbValue gep0 = lb_emit_struct_ep(p, h.addr, 0);
  399. lbValue m = lb_emit_conv(p, map_val_ptr, type_deref(gep0.type));
  400. lb_emit_store(p, gep0, m);
  401. return lb_addr_load(p, h);
  402. }
  403. lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
  404. if (true) {
  405. return {};
  406. }
  407. lbValue hashed_key = {};
  408. #if 0
  409. if (lb_is_const(key)) {
  410. u64 hash = 0xcbf29ce484222325;
  411. if (is_type_cstring(key_type)) {
  412. size_t length = 0;
  413. char const *text = LLVMGetAsString(key.value, &length);
  414. hash = fnv64a(text, cast(isize)length);
  415. } else if (is_type_string(key_type)) {
  416. unsigned data_indices[] = {0};
  417. unsigned len_indices[] = {1};
  418. LLVMValueRef data = LLVMConstExtractValue(key.value, data_indices, gb_count_of(data_indices));
  419. LLVMValueRef len = LLVMConstExtractValue(key.value, len_indices, gb_count_of(len_indices));
  420. i64 length = LLVMConstIntGetSExtValue(len);
  421. char const *text = nullptr;
  422. if (false && length != 0) {
  423. if (LLVMGetConstOpcode(data) != LLVMGetElementPtr) {
  424. return {};
  425. }
  426. // TODO(bill): THIS IS BROKEN! THIS NEEDS FIXING :P
  427. size_t ulength = 0;
  428. text = LLVMGetAsString(data, &ulength);
  429. gb_printf_err("%lld %llu %s\n", length, ulength, text);
  430. length = gb_min(length, cast(i64)ulength);
  431. }
  432. hash = fnv64a(text, cast(isize)length);
  433. } else {
  434. return {};
  435. }
  436. // TODO(bill): other const hash types
  437. if (build_context.word_size == 4) {
  438. hash &= 0xffffffffull;
  439. }
  440. hashed_key = lb_const_int(m, t_uintptr, hash);
  441. }
  442. #endif
  443. return hashed_key;
  444. }
  445. lbValue lb_gen_map_hash(lbProcedure *p, lbValue key, Type *key_type) {
  446. lbAddr v = lb_add_local_generated(p, t_map_hash, true);
  447. lbValue vp = lb_addr_get_ptr(p, v);
  448. key = lb_emit_conv(p, key, key_type);
  449. lbValue key_ptr = lb_address_from_load_or_generate_local(p, key);
  450. key_ptr = lb_emit_conv(p, key_ptr, t_rawptr);
  451. lbValue hashed_key = lb_const_hash(p->module, key, key_type);
  452. if (hashed_key.value == nullptr) {
  453. lbValue hasher = lb_get_hasher_proc_for_type(p->module, key_type);
  454. auto args = array_make<lbValue>(permanent_allocator(), 2);
  455. args[0] = key_ptr;
  456. args[1] = lb_const_int(p->module, t_uintptr, 0);
  457. hashed_key = lb_emit_call(p, hasher, args);
  458. }
  459. lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hashed_key);
  460. lb_emit_store(p, lb_emit_struct_ep(p, vp, 1), key_ptr);
  461. return lb_addr_load(p, v);
  462. }
  463. void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type,
  464. lbValue map_key, lbValue map_value, Ast *node) {
  465. map_type = base_type(map_type);
  466. GB_ASSERT(map_type->kind == Type_Map);
  467. lbValue h = lb_gen_map_header(p, addr.addr, map_type);
  468. lbValue key = lb_gen_map_hash(p, map_key, map_type->Map.key);
  469. lbValue v = lb_emit_conv(p, map_value, map_type->Map.value);
  470. lbAddr value_addr = lb_add_local_generated(p, v.type, false);
  471. lb_addr_store(p, value_addr, v);
  472. auto args = array_make<lbValue>(permanent_allocator(), 4);
  473. args[0] = h;
  474. args[1] = key;
  475. args[2] = lb_emit_conv(p, value_addr.addr, t_rawptr);
  476. args[3] = lb_emit_source_code_location(p, node);
  477. lb_emit_runtime_call(p, "__dynamic_map_set", args);
  478. }
  479. struct lbGlobalVariable {
  480. lbValue var;
  481. lbValue init;
  482. DeclInfo *decl;
  483. bool is_initialized;
  484. };
  485. lbProcedure *lb_create_startup_type_info(lbModule *m) {
  486. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  487. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  488. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  489. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
  490. lbProcedure *p = lb_create_dummy_procedure(m, str_lit(LB_STARTUP_TYPE_INFO_PROC_NAME), proc_type);
  491. p->is_startup = true;
  492. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  493. lb_begin_procedure_body(p);
  494. lb_setup_type_info_data(p);
  495. lb_end_procedure_body(p);
  496. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  497. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  498. LLVMDumpValue(p->value);
  499. gb_printf_err("\n\n\n\n");
  500. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  501. }
  502. lb_run_function_pass_manager(default_function_pass_manager, p);
  503. return p;
  504. }
  505. lbProcedure *lb_create_objc_names(lbModule *main_module) {
  506. if (build_context.metrics.os != TargetOs_darwin) {
  507. return nullptr;
  508. }
  509. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
  510. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit("__$init_objc_names"), proc_type);
  511. p->is_startup = true;
  512. return p;
  513. }
  514. void lb_finalize_objc_names(lbProcedure *p) {
  515. if (p == nullptr) {
  516. return;
  517. }
  518. lbModule *m = p->module;
  519. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  520. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  521. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  522. auto args = array_make<lbValue>(permanent_allocator(), 1);
  523. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  524. lb_begin_procedure_body(p);
  525. for_array(i, m->objc_classes.entries) {
  526. auto const &entry = m->objc_classes.entries[i];
  527. String name = entry.key.string;
  528. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  529. lbValue ptr = lb_emit_runtime_call(p, "objc_lookUpClass", args);
  530. lb_addr_store(p, entry.value, ptr);
  531. }
  532. for_array(i, m->objc_selectors.entries) {
  533. auto const &entry = m->objc_selectors.entries[i];
  534. String name = entry.key.string;
  535. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  536. lbValue ptr = lb_emit_runtime_call(p, "sel_registerName", args);
  537. lb_addr_store(p, entry.value, ptr);
  538. }
  539. lb_end_procedure_body(p);
  540. lb_run_function_pass_manager(default_function_pass_manager, p);
  541. }
  542. lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array<lbGlobalVariable> &global_variables) { // Startup Runtime
  543. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(main_module->mod);
  544. lb_populate_function_pass_manager(main_module, default_function_pass_manager, false, build_context.optimization_level);
  545. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  546. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
  547. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type);
  548. p->is_startup = true;
  549. lb_begin_procedure_body(p);
  550. LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, startup_type_info->type)), startup_type_info->value, nullptr, 0, "");
  551. if (objc_names) {
  552. LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, objc_names->type)), objc_names->value, nullptr, 0, "");
  553. }
  554. for_array(i, global_variables) {
  555. auto *var = &global_variables[i];
  556. if (var->is_initialized) {
  557. continue;
  558. }
  559. lbModule *entity_module = main_module;
  560. Entity *e = var->decl->entity;
  561. GB_ASSERT(e->kind == Entity_Variable);
  562. e->code_gen_module = entity_module;
  563. Ast *init_expr = var->decl->init_expr;
  564. if (init_expr != nullptr) {
  565. lbValue init = lb_build_expr(p, init_expr);
  566. if (init.value == nullptr) {
  567. LLVMTypeRef global_type = LLVMGetElementType(LLVMTypeOf(var->var.value));
  568. if (is_type_untyped_undef(init.type)) {
  569. // LLVMSetInitializer(var->var.value, LLVMGetUndef(global_type));
  570. LLVMSetInitializer(var->var.value, LLVMConstNull(global_type));
  571. var->is_initialized = true;
  572. continue;
  573. } else if (is_type_untyped_nil(init.type)) {
  574. LLVMSetInitializer(var->var.value, LLVMConstNull(global_type));
  575. var->is_initialized = true;
  576. continue;
  577. }
  578. GB_PANIC("Invalid init value, got %s", expr_to_string(init_expr));
  579. }
  580. if (is_type_any(e->type) || is_type_union(e->type)) {
  581. var->init = init;
  582. } else if (lb_is_const_or_global(init)) {
  583. if (!var->is_initialized) {
  584. LLVMSetInitializer(var->var.value, init.value);
  585. var->is_initialized = true;
  586. continue;
  587. }
  588. } else {
  589. var->init = init;
  590. }
  591. }
  592. if (var->init.value != nullptr) {
  593. GB_ASSERT(!var->is_initialized);
  594. Type *t = type_deref(var->var.type);
  595. if (is_type_any(t)) {
  596. // NOTE(bill): Edge case for 'any' type
  597. Type *var_type = default_type(var->init.type);
  598. lbAddr g = lb_add_global_generated(main_module, var_type, var->init);
  599. lb_addr_store(p, g, var->init);
  600. lbValue gp = lb_addr_get_ptr(p, g);
  601. lbValue data = lb_emit_struct_ep(p, var->var, 0);
  602. lbValue ti = lb_emit_struct_ep(p, var->var, 1);
  603. lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
  604. lb_emit_store(p, ti, lb_type_info(main_module, var_type));
  605. } else {
  606. LLVMTypeRef pvt = LLVMTypeOf(var->var.value);
  607. LLVMTypeRef vt = LLVMGetElementType(pvt);
  608. lbValue src0 = lb_emit_conv(p, var->init, t);
  609. LLVMValueRef src = OdinLLVMBuildTransmute(p, src0.value, vt);
  610. LLVMValueRef dst = var->var.value;
  611. LLVMBuildStore(p->builder, src, dst);
  612. }
  613. var->is_initialized = true;
  614. }
  615. }
  616. CheckerInfo *info = main_module->gen->info;
  617. for (Entity *e : info->init_procedures) {
  618. lbValue value = lb_find_procedure_value_from_entity(main_module, e);
  619. lb_emit_call(p, value, {}, ProcInlining_none, false);
  620. }
  621. lb_end_procedure_body(p);
  622. if (!main_module->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  623. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  624. LLVMDumpValue(p->value);
  625. gb_printf_err("\n\n\n\n");
  626. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  627. }
  628. lb_run_function_pass_manager(default_function_pass_manager, p);
  629. return p;
  630. }
  631. lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) {
  632. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  633. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  634. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  635. Type *params = alloc_type_tuple();
  636. Type *results = alloc_type_tuple();
  637. Type *t_ptr_cstring = alloc_type_pointer(t_cstring);
  638. bool call_cleanup = true;
  639. bool has_args = false;
  640. bool is_dll_main = false;
  641. String name = str_lit("main");
  642. if (build_context.metrics.os == TargetOs_windows && build_context.build_mode == BuildMode_DynamicLibrary) {
  643. is_dll_main = true;
  644. name = str_lit("DllMain");
  645. slice_init(&params->Tuple.variables, permanent_allocator(), 3);
  646. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("hinstDLL"), t_rawptr, false, true);
  647. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("fdwReason"), t_u32, false, true);
  648. params->Tuple.variables[2] = alloc_entity_param(nullptr, make_token_ident("lpReserved"), t_rawptr, false, true);
  649. call_cleanup = false;
  650. } else if (build_context.metrics.os == TargetOs_windows && (build_context.metrics.arch == TargetArch_i386 || build_context.no_crt)) {
  651. name = str_lit("mainCRTStartup");
  652. } else if (is_arch_wasm()) {
  653. name = str_lit("_start");
  654. call_cleanup = false;
  655. } else {
  656. has_args = true;
  657. slice_init(&params->Tuple.variables, permanent_allocator(), 2);
  658. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("argc"), t_i32, false, true);
  659. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("argv"), t_ptr_cstring, false, true);
  660. }
  661. slice_init(&results->Tuple.variables, permanent_allocator(), 1);
  662. results->Tuple.variables[0] = alloc_entity_param(nullptr, blank_token, t_i32, false, true);
  663. Type *proc_type = alloc_type_proc(nullptr,
  664. params, params->Tuple.variables.count,
  665. results, results->Tuple.variables.count, false, ProcCC_CDecl);
  666. lbProcedure *p = lb_create_dummy_procedure(m, name, proc_type);
  667. p->is_startup = true;
  668. lb_begin_procedure_body(p);
  669. if (has_args) { // initialize `runtime.args__`
  670. lbValue argc = {LLVMGetParam(p->value, 0), t_i32};
  671. lbValue argv = {LLVMGetParam(p->value, 1), t_ptr_cstring};
  672. LLVMSetValueName2(argc.value, "argc", 4);
  673. LLVMSetValueName2(argv.value, "argv", 4);
  674. argc = lb_emit_conv(p, argc, t_int);
  675. lbAddr args = lb_addr(lb_find_runtime_value(p->module, str_lit("args__")));
  676. lb_fill_slice(p, args, argv, argc);
  677. }
  678. lbValue startup_runtime_value = {startup_runtime->value, startup_runtime->type};
  679. lb_emit_call(p, startup_runtime_value, {}, ProcInlining_none, false);
  680. if (build_context.command_kind == Command_test) {
  681. Type *t_Internal_Test = find_type_in_pkg(m->info, str_lit("testing"), str_lit("Internal_Test"));
  682. Type *array_type = alloc_type_array(t_Internal_Test, m->info->testing_procedures.count);
  683. Type *slice_type = alloc_type_slice(t_Internal_Test);
  684. lbAddr all_tests_array_addr = lb_add_global_generated(p->module, array_type, {});
  685. lbValue all_tests_array = lb_addr_get_ptr(p, all_tests_array_addr);
  686. LLVMValueRef indices[2] = {};
  687. indices[0] = LLVMConstInt(lb_type(m, t_i32), 0, false);
  688. for_array(i, m->info->testing_procedures) {
  689. Entity *testing_proc = m->info->testing_procedures[i];
  690. String name = testing_proc->token.string;
  691. String pkg_name = {};
  692. if (testing_proc->pkg != nullptr) {
  693. pkg_name = testing_proc->pkg->name;
  694. }
  695. lbValue v_pkg = lb_find_or_add_entity_string(m, pkg_name);
  696. lbValue v_name = lb_find_or_add_entity_string(m, name);
  697. lbValue v_proc = lb_find_procedure_value_from_entity(m, testing_proc);
  698. indices[1] = LLVMConstInt(lb_type(m, t_int), i, false);
  699. LLVMValueRef vals[3] = {};
  700. vals[0] = v_pkg.value;
  701. vals[1] = v_name.value;
  702. vals[2] = v_proc.value;
  703. GB_ASSERT(LLVMIsConstant(vals[0]));
  704. GB_ASSERT(LLVMIsConstant(vals[1]));
  705. GB_ASSERT(LLVMIsConstant(vals[2]));
  706. LLVMValueRef dst = LLVMConstInBoundsGEP(all_tests_array.value, indices, gb_count_of(indices));
  707. LLVMValueRef src = llvm_const_named_struct(m, t_Internal_Test, vals, gb_count_of(vals));
  708. LLVMBuildStore(p->builder, src, dst);
  709. }
  710. lbAddr all_tests_slice = lb_add_local_generated(p, slice_type, true);
  711. lb_fill_slice(p, all_tests_slice,
  712. lb_array_elem(p, all_tests_array),
  713. lb_const_int(m, t_int, m->info->testing_procedures.count));
  714. lbValue runner = lb_find_package_value(m, str_lit("testing"), str_lit("runner"));
  715. auto args = array_make<lbValue>(heap_allocator(), 1);
  716. args[0] = lb_addr_load(p, all_tests_slice);
  717. lb_emit_call(p, runner, args);
  718. } else {
  719. if (m->info->entry_point != nullptr) {
  720. lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point);
  721. lb_emit_call(p, entry_point, {}, ProcInlining_no_inline);
  722. }
  723. }
  724. if (call_cleanup) {
  725. lbValue cleanup_runtime_value = lb_find_runtime_value(m, str_lit("_cleanup_runtime"));
  726. lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none, false);
  727. }
  728. if (is_dll_main) {
  729. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false));
  730. } else {
  731. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false));
  732. }
  733. lb_end_procedure_body(p);
  734. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  735. if (is_arch_wasm()) {
  736. lb_set_wasm_export_attributes(p->value, p->name);
  737. }
  738. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  739. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  740. LLVMDumpValue(p->value);
  741. gb_printf_err("\n\n\n\n");
  742. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  743. }
  744. lb_run_function_pass_manager(default_function_pass_manager, p);
  745. return p;
  746. }
  747. String lb_filepath_ll_for_module(lbModule *m) {
  748. String path = m->gen->output_base;
  749. if (m->pkg) {
  750. path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
  751. } else if (USE_SEPARATE_MODULES) {
  752. path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin"));
  753. }
  754. path = concatenate_strings(permanent_allocator(), path, STR_LIT(".ll"));
  755. return path;
  756. }
  757. String lb_filepath_obj_for_module(lbModule *m) {
  758. String path = m->gen->output_base;
  759. if (m->pkg) {
  760. path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
  761. }
  762. String ext = {};
  763. if (build_context.build_mode == BuildMode_Assembly) {
  764. ext = STR_LIT(".S");
  765. } else {
  766. if (is_arch_wasm()) {
  767. ext = STR_LIT(".wasm.o");
  768. } else {
  769. switch (build_context.metrics.os) {
  770. case TargetOs_windows:
  771. ext = STR_LIT(".obj");
  772. break;
  773. default:
  774. case TargetOs_darwin:
  775. case TargetOs_linux:
  776. case TargetOs_essence:
  777. ext = STR_LIT(".o");
  778. break;
  779. }
  780. }
  781. }
  782. return concatenate_strings(permanent_allocator(), path, ext);
  783. }
  784. bool lb_is_module_empty(lbModule *m) {
  785. if (LLVMGetFirstFunction(m->mod) == nullptr &&
  786. LLVMGetFirstGlobal(m->mod) == nullptr) {
  787. return true;
  788. }
  789. for (auto fn = LLVMGetFirstFunction(m->mod); fn != nullptr; fn = LLVMGetNextFunction(fn)) {
  790. if (LLVMGetFirstBasicBlock(fn) != nullptr) {
  791. return false;
  792. }
  793. }
  794. for (auto g = LLVMGetFirstGlobal(m->mod); g != nullptr; g = LLVMGetNextGlobal(g)) {
  795. if (LLVMGetLinkage(g) == LLVMExternalLinkage) {
  796. continue;
  797. }
  798. if (!LLVMIsExternallyInitialized(g)) {
  799. return false;
  800. }
  801. }
  802. return true;
  803. }
  804. struct lbLLVMEmitWorker {
  805. LLVMTargetMachineRef target_machine;
  806. LLVMCodeGenFileType code_gen_file_type;
  807. String filepath_obj;
  808. lbModule *m;
  809. };
  810. WORKER_TASK_PROC(lb_llvm_emit_worker_proc) {
  811. GB_ASSERT(MULTITHREAD_OBJECT_GENERATION);
  812. char *llvm_error = nullptr;
  813. auto wd = cast(lbLLVMEmitWorker *)data;
  814. if (LLVMTargetMachineEmitToFile(wd->target_machine, wd->m->mod, cast(char *)wd->filepath_obj.text, wd->code_gen_file_type, &llvm_error)) {
  815. gb_printf_err("LLVM Error: %s\n", llvm_error);
  816. gb_exit(1);
  817. }
  818. return 0;
  819. }
  820. WORKER_TASK_PROC(lb_llvm_function_pass_worker_proc) {
  821. GB_ASSERT(MULTITHREAD_OBJECT_GENERATION);
  822. auto m = cast(lbModule *)data;
  823. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  824. LLVMPassManagerRef function_pass_manager_minimal = LLVMCreateFunctionPassManagerForModule(m->mod);
  825. LLVMPassManagerRef function_pass_manager_size = LLVMCreateFunctionPassManagerForModule(m->mod);
  826. LLVMPassManagerRef function_pass_manager_speed = LLVMCreateFunctionPassManagerForModule(m->mod);
  827. LLVMInitializeFunctionPassManager(default_function_pass_manager);
  828. LLVMInitializeFunctionPassManager(function_pass_manager_minimal);
  829. LLVMInitializeFunctionPassManager(function_pass_manager_size);
  830. LLVMInitializeFunctionPassManager(function_pass_manager_speed);
  831. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  832. lb_populate_function_pass_manager_specific(m, function_pass_manager_minimal, 0);
  833. lb_populate_function_pass_manager_specific(m, function_pass_manager_size, 1);
  834. lb_populate_function_pass_manager_specific(m, function_pass_manager_speed, 2);
  835. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  836. LLVMFinalizeFunctionPassManager(function_pass_manager_minimal);
  837. LLVMFinalizeFunctionPassManager(function_pass_manager_size);
  838. LLVMFinalizeFunctionPassManager(function_pass_manager_speed);
  839. LLVMPassManagerRef default_function_pass_manager_without_memcpy = LLVMCreateFunctionPassManagerForModule(m->mod);
  840. LLVMInitializeFunctionPassManager(default_function_pass_manager_without_memcpy);
  841. lb_populate_function_pass_manager(m, default_function_pass_manager_without_memcpy, true, build_context.optimization_level);
  842. LLVMFinalizeFunctionPassManager(default_function_pass_manager_without_memcpy);
  843. for_array(i, m->procedures_to_generate) {
  844. lbProcedure *p = m->procedures_to_generate[i];
  845. if (p->body != nullptr) { // Build Procedure
  846. if (p->flags & lbProcedureFlag_WithoutMemcpyPass) {
  847. lb_run_function_pass_manager(default_function_pass_manager_without_memcpy, p);
  848. } else {
  849. if (p->entity && p->entity->kind == Entity_Procedure) {
  850. switch (p->entity->Procedure.optimization_mode) {
  851. case ProcedureOptimizationMode_None:
  852. case ProcedureOptimizationMode_Minimal:
  853. lb_run_function_pass_manager(function_pass_manager_minimal, p);
  854. break;
  855. case ProcedureOptimizationMode_Size:
  856. lb_run_function_pass_manager(function_pass_manager_size, p);
  857. break;
  858. case ProcedureOptimizationMode_Speed:
  859. lb_run_function_pass_manager(function_pass_manager_speed, p);
  860. break;
  861. default:
  862. lb_run_function_pass_manager(default_function_pass_manager, p);
  863. break;
  864. }
  865. } else {
  866. lb_run_function_pass_manager(default_function_pass_manager, p);
  867. }
  868. }
  869. }
  870. }
  871. for_array(i, m->equal_procs.entries) {
  872. lbProcedure *p = m->equal_procs.entries[i].value;
  873. lb_run_function_pass_manager(default_function_pass_manager, p);
  874. }
  875. for_array(i, m->hasher_procs.entries) {
  876. lbProcedure *p = m->hasher_procs.entries[i].value;
  877. lb_run_function_pass_manager(default_function_pass_manager, p);
  878. }
  879. return 0;
  880. }
  881. struct lbLLVMModulePassWorkerData {
  882. lbModule *m;
  883. LLVMTargetMachineRef target_machine;
  884. };
  885. WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) {
  886. auto wd = cast(lbLLVMModulePassWorkerData *)data;
  887. LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager();
  888. lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level);
  889. LLVMRunPassManager(module_pass_manager, wd->m->mod);
  890. return 0;
  891. }
  892. void lb_generate_procedure(lbModule *m, lbProcedure *p) {
  893. if (p->is_done) {
  894. return;
  895. }
  896. if (p->body != nullptr) { // Build Procedure
  897. m->curr_procedure = p;
  898. lb_begin_procedure_body(p);
  899. lb_build_stmt(p, p->body);
  900. lb_end_procedure_body(p);
  901. p->is_done = true;
  902. m->curr_procedure = nullptr;
  903. }
  904. lb_end_procedure(p);
  905. // Add Flags
  906. if (p->body != nullptr) {
  907. if (p->name == "memcpy" || p->name == "memmove" ||
  908. p->name == "runtime.mem_copy" || p->name == "mem_copy_non_overlapping" ||
  909. string_starts_with(p->name, str_lit("llvm.memcpy")) ||
  910. string_starts_with(p->name, str_lit("llvm.memmove"))) {
  911. p->flags |= lbProcedureFlag_WithoutMemcpyPass;
  912. }
  913. }
  914. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  915. char *llvm_error = nullptr;
  916. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %.*s\n", LIT(p->name));
  917. LLVMDumpValue(p->value);
  918. gb_printf_err("\n\n\n\n");
  919. String filepath_ll = lb_filepath_ll_for_module(m);
  920. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  921. gb_printf_err("LLVM Error: %s\n", llvm_error);
  922. }
  923. LLVMVerifyFunction(p->value, LLVMPrintMessageAction);
  924. gb_exit(1);
  925. }
  926. }
  927. void lb_generate_code(lbGenerator *gen) {
  928. TIME_SECTION("LLVM Initializtion");
  929. isize thread_count = gb_max(build_context.thread_count, 1);
  930. isize worker_count = thread_count-1;
  931. LLVMBool do_threading = (LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0);
  932. lbModule *default_module = &gen->default_module;
  933. CheckerInfo *info = gen->info;
  934. auto *min_dep_set = &info->minimum_dependency_set;
  935. switch (build_context.metrics.arch) {
  936. case TargetArch_amd64:
  937. case TargetArch_i386:
  938. LLVMInitializeX86TargetInfo();
  939. LLVMInitializeX86Target();
  940. LLVMInitializeX86TargetMC();
  941. LLVMInitializeX86AsmPrinter();
  942. LLVMInitializeX86AsmParser();
  943. LLVMInitializeX86Disassembler();
  944. break;
  945. case TargetArch_arm64:
  946. LLVMInitializeAArch64TargetInfo();
  947. LLVMInitializeAArch64Target();
  948. LLVMInitializeAArch64TargetMC();
  949. LLVMInitializeAArch64AsmPrinter();
  950. LLVMInitializeAArch64AsmParser();
  951. LLVMInitializeAArch64Disassembler();
  952. break;
  953. case TargetArch_wasm32:
  954. case TargetArch_wasm64:
  955. LLVMInitializeWebAssemblyTargetInfo();
  956. LLVMInitializeWebAssemblyTarget();
  957. LLVMInitializeWebAssemblyTargetMC();
  958. LLVMInitializeWebAssemblyAsmPrinter();
  959. LLVMInitializeWebAssemblyAsmParser();
  960. LLVMInitializeWebAssemblyDisassembler();
  961. break;
  962. default:
  963. LLVMInitializeAllTargetInfos();
  964. LLVMInitializeAllTargets();
  965. LLVMInitializeAllTargetMCs();
  966. LLVMInitializeAllAsmPrinters();
  967. LLVMInitializeAllAsmParsers();
  968. LLVMInitializeAllDisassemblers();
  969. break;
  970. }
  971. if (build_context.microarch == "native") {
  972. LLVMInitializeNativeTarget();
  973. }
  974. char const *target_triple = alloc_cstring(permanent_allocator(), build_context.metrics.target_triplet);
  975. for_array(i, gen->modules.entries) {
  976. LLVMSetTarget(gen->modules.entries[i].value->mod, target_triple);
  977. }
  978. LLVMTargetRef target = {};
  979. char *llvm_error = nullptr;
  980. LLVMGetTargetFromTriple(target_triple, &target, &llvm_error);
  981. GB_ASSERT(target != nullptr);
  982. TIME_SECTION("LLVM Create Target Machine");
  983. LLVMCodeModel code_mode = LLVMCodeModelDefault;
  984. if (is_arch_wasm()) {
  985. code_mode = LLVMCodeModelJITDefault;
  986. }
  987. char const *host_cpu_name = LLVMGetHostCPUName();
  988. char const *llvm_cpu = "generic";
  989. char const *llvm_features = "";
  990. if (build_context.microarch.len != 0) {
  991. if (build_context.microarch == "native") {
  992. llvm_cpu = host_cpu_name;
  993. } else {
  994. llvm_cpu = alloc_cstring(permanent_allocator(), build_context.microarch);
  995. }
  996. if (gb_strcmp(llvm_cpu, host_cpu_name) == 0) {
  997. llvm_features = LLVMGetHostCPUFeatures();
  998. }
  999. } else if (build_context.metrics.arch == TargetArch_amd64) {
  1000. // NOTE(bill): x86-64-v2 is more than enough for everyone
  1001. //
  1002. // x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
  1003. // x86-64-v2: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
  1004. // x86-64-v3: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
  1005. // x86-64-v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
  1006. if (ODIN_LLVM_MINIMUM_VERSION_12) {
  1007. llvm_cpu = "x86-64-v2";
  1008. }
  1009. }
  1010. // GB_ASSERT_MSG(LLVMTargetHasAsmBackend(target));
  1011. LLVMCodeGenOptLevel code_gen_level = LLVMCodeGenLevelNone;
  1012. switch (build_context.optimization_level) {
  1013. case 0: code_gen_level = LLVMCodeGenLevelNone; break;
  1014. case 1: code_gen_level = LLVMCodeGenLevelLess; break;
  1015. case 2: code_gen_level = LLVMCodeGenLevelDefault; break;
  1016. case 3: code_gen_level = LLVMCodeGenLevelDefault; break; // NOTE(bill): force -opt:3 to be the same as -opt:2
  1017. // case 3: code_gen_level = LLVMCodeGenLevelAggressive; break;
  1018. }
  1019. // NOTE(bill): Target Machine Creation
  1020. // NOTE(bill, 2021-05-04): Target machines must be unique to each module because they are not thread safe
  1021. auto target_machines = array_make<LLVMTargetMachineRef>(permanent_allocator(), gen->modules.entries.count);
  1022. // NOTE(dweiler): Dynamic libraries require position-independent code.
  1023. LLVMRelocMode reloc_mode = LLVMRelocDefault;
  1024. if (build_context.build_mode == BuildMode_DynamicLibrary) {
  1025. reloc_mode = LLVMRelocPIC;
  1026. }
  1027. for_array(i, gen->modules.entries) {
  1028. target_machines[i] = LLVMCreateTargetMachine(
  1029. target, target_triple, llvm_cpu,
  1030. llvm_features,
  1031. code_gen_level,
  1032. reloc_mode,
  1033. code_mode);
  1034. LLVMSetModuleDataLayout(gen->modules.entries[i].value->mod, LLVMCreateTargetDataLayout(target_machines[i]));
  1035. }
  1036. for_array(i, gen->modules.entries) {
  1037. lbModule *m = gen->modules.entries[i].value;
  1038. if (m->debug_builder) { // Debug Info
  1039. for_array(i, info->files.entries) {
  1040. AstFile *f = info->files.entries[i].value;
  1041. String fullpath = f->fullpath;
  1042. String filename = remove_directory_from_path(fullpath);
  1043. String directory = directory_from_path(fullpath);
  1044. LLVMMetadataRef res = LLVMDIBuilderCreateFile(m->debug_builder,
  1045. cast(char const *)filename.text, filename.len,
  1046. cast(char const *)directory.text, directory.len);
  1047. lb_set_llvm_metadata(m, f, res);
  1048. }
  1049. gbString producer = gb_string_make(heap_allocator(), "odin");
  1050. // producer = gb_string_append_fmt(producer, " version %.*s", LIT(ODIN_VERSION));
  1051. // #ifdef NIGHTLY
  1052. // producer = gb_string_appendc(producer, "-nightly");
  1053. // #endif
  1054. // #ifdef GIT_SHA
  1055. // producer = gb_string_append_fmt(producer, "-%s", GIT_SHA);
  1056. // #endif
  1057. gbString split_name = gb_string_make(heap_allocator(), "");
  1058. LLVMBool is_optimized = build_context.optimization_level > 0;
  1059. AstFile *init_file = m->info->init_package->files[0];
  1060. if (Entity *entry_point = m->info->entry_point) {
  1061. if (Ast *ident = entry_point->identifier.load()) {
  1062. if (ident->file_id) {
  1063. init_file = ident->file();
  1064. }
  1065. }
  1066. }
  1067. LLVMBool split_debug_inlining = false;
  1068. LLVMBool debug_info_for_profiling = false;
  1069. m->debug_compile_unit = LLVMDIBuilderCreateCompileUnit(m->debug_builder, LLVMDWARFSourceLanguageC99,
  1070. lb_get_llvm_metadata(m, init_file),
  1071. producer, gb_string_length(producer),
  1072. is_optimized, "", 0,
  1073. 1, split_name, gb_string_length(split_name),
  1074. LLVMDWARFEmissionFull,
  1075. 0, split_debug_inlining,
  1076. debug_info_for_profiling,
  1077. "", 0, // sys_root
  1078. "", 0 // SDK
  1079. );
  1080. GB_ASSERT(m->debug_compile_unit != nullptr);
  1081. }
  1082. }
  1083. TIME_SECTION("LLVM Global Variables");
  1084. {
  1085. lbModule *m = default_module;
  1086. { // Add type info data
  1087. isize max_type_info_count = info->minimum_dependency_type_info_set.entries.count+1;
  1088. // gb_printf_err("max_type_info_count: %td\n", max_type_info_count);
  1089. Type *t = alloc_type_array(t_type_info, max_type_info_count);
  1090. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), LB_TYPE_INFO_DATA_NAME);
  1091. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1092. if (!USE_SEPARATE_MODULES) {
  1093. LLVMSetLinkage(g, LLVMInternalLinkage);
  1094. }
  1095. lbValue value = {};
  1096. value.value = g;
  1097. value.type = alloc_type_pointer(t);
  1098. lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved);
  1099. lb_add_entity(m, lb_global_type_info_data_entity, value);
  1100. }
  1101. { // Type info member buffer
  1102. // NOTE(bill): Removes need for heap allocation by making it global memory
  1103. isize count = 0;
  1104. for_array(entry_index, m->info->type_info_types) {
  1105. Type *t = m->info->type_info_types[entry_index];
  1106. isize index = lb_type_info_index(m->info, t, false);
  1107. if (index < 0) {
  1108. continue;
  1109. }
  1110. switch (t->kind) {
  1111. case Type_Union:
  1112. count += t->Union.variants.count;
  1113. break;
  1114. case Type_Struct:
  1115. count += t->Struct.fields.count;
  1116. break;
  1117. case Type_Tuple:
  1118. count += t->Tuple.variables.count;
  1119. break;
  1120. }
  1121. }
  1122. if (count > 0) {
  1123. {
  1124. char const *name = LB_TYPE_INFO_TYPES_NAME;
  1125. Type *t = alloc_type_array(t_type_info_ptr, count);
  1126. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1127. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1128. LLVMSetLinkage(g, LLVMInternalLinkage);
  1129. lb_global_type_info_member_types = lb_addr({g, alloc_type_pointer(t)});
  1130. }
  1131. {
  1132. char const *name = LB_TYPE_INFO_NAMES_NAME;
  1133. Type *t = alloc_type_array(t_string, count);
  1134. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1135. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1136. LLVMSetLinkage(g, LLVMInternalLinkage);
  1137. lb_global_type_info_member_names = lb_addr({g, alloc_type_pointer(t)});
  1138. }
  1139. {
  1140. char const *name = LB_TYPE_INFO_OFFSETS_NAME;
  1141. Type *t = alloc_type_array(t_uintptr, count);
  1142. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1143. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1144. LLVMSetLinkage(g, LLVMInternalLinkage);
  1145. lb_global_type_info_member_offsets = lb_addr({g, alloc_type_pointer(t)});
  1146. }
  1147. {
  1148. char const *name = LB_TYPE_INFO_USINGS_NAME;
  1149. Type *t = alloc_type_array(t_bool, count);
  1150. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1151. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1152. LLVMSetLinkage(g, LLVMInternalLinkage);
  1153. lb_global_type_info_member_usings = lb_addr({g, alloc_type_pointer(t)});
  1154. }
  1155. {
  1156. char const *name = LB_TYPE_INFO_TAGS_NAME;
  1157. Type *t = alloc_type_array(t_string, count);
  1158. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1159. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1160. LLVMSetLinkage(g, LLVMInternalLinkage);
  1161. lb_global_type_info_member_tags = lb_addr({g, alloc_type_pointer(t)});
  1162. }
  1163. }
  1164. }
  1165. }
  1166. isize global_variable_max_count = 0;
  1167. bool already_has_entry_point = false;
  1168. for_array(i, info->entities) {
  1169. Entity *e = info->entities[i];
  1170. String name = e->token.string;
  1171. if (e->kind == Entity_Variable) {
  1172. global_variable_max_count++;
  1173. } else if (e->kind == Entity_Procedure) {
  1174. if ((e->scope->flags&ScopeFlag_Init) && name == "main") {
  1175. GB_ASSERT(e == info->entry_point);
  1176. }
  1177. if (e->Procedure.is_export ||
  1178. (e->Procedure.link_name.len > 0) ||
  1179. ((e->scope->flags&ScopeFlag_File) && e->Procedure.link_name.len > 0)) {
  1180. String link_name = e->Procedure.link_name;
  1181. if (e->pkg->kind == Package_Runtime) {
  1182. if (link_name == "main" ||
  1183. link_name == "DllMain" ||
  1184. link_name == "WinMain" ||
  1185. link_name == "wWinMain" ||
  1186. link_name == "mainCRTStartup" ||
  1187. link_name == "_start") {
  1188. already_has_entry_point = true;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. auto global_variables = array_make<lbGlobalVariable>(permanent_allocator(), 0, global_variable_max_count);
  1195. for_array(i, info->variable_init_order) {
  1196. DeclInfo *d = info->variable_init_order[i];
  1197. Entity *e = d->entity;
  1198. if ((e->scope->flags & ScopeFlag_File) == 0) {
  1199. continue;
  1200. }
  1201. if (!ptr_set_exists(min_dep_set, e)) {
  1202. continue;
  1203. }
  1204. DeclInfo *decl = decl_info_of_entity(e);
  1205. if (decl == nullptr) {
  1206. continue;
  1207. }
  1208. GB_ASSERT(e->kind == Entity_Variable);
  1209. bool is_foreign = e->Variable.is_foreign;
  1210. bool is_export = e->Variable.is_export;
  1211. lbModule *m = &gen->default_module;
  1212. String name = lb_get_entity_name(m, e);
  1213. lbValue g = {};
  1214. g.value = LLVMAddGlobal(m->mod, lb_type(m, e->type), alloc_cstring(permanent_allocator(), name));
  1215. g.type = alloc_type_pointer(e->type);
  1216. if (e->Variable.thread_local_model != "") {
  1217. LLVMSetThreadLocal(g.value, true);
  1218. String m = e->Variable.thread_local_model;
  1219. LLVMThreadLocalMode mode = LLVMGeneralDynamicTLSModel;
  1220. if (m == "default") {
  1221. mode = LLVMGeneralDynamicTLSModel;
  1222. } else if (m == "localdynamic") {
  1223. mode = LLVMLocalDynamicTLSModel;
  1224. } else if (m == "initialexec") {
  1225. mode = LLVMInitialExecTLSModel;
  1226. } else if (m == "localexec") {
  1227. mode = LLVMLocalExecTLSModel;
  1228. } else {
  1229. GB_PANIC("Unhandled thread local mode %.*s", LIT(m));
  1230. }
  1231. LLVMSetThreadLocalMode(g.value, mode);
  1232. }
  1233. if (is_foreign) {
  1234. LLVMSetLinkage(g.value, LLVMExternalLinkage);
  1235. LLVMSetExternallyInitialized(g.value, true);
  1236. lb_add_foreign_library_path(m, e->Variable.foreign_library);
  1237. lb_set_wasm_import_attributes(g.value, e, name);
  1238. } else {
  1239. LLVMSetInitializer(g.value, LLVMConstNull(lb_type(m, e->type)));
  1240. }
  1241. if (is_export) {
  1242. LLVMSetLinkage(g.value, LLVMDLLExportLinkage);
  1243. LLVMSetDLLStorageClass(g.value, LLVMDLLExportStorageClass);
  1244. } else if (!is_foreign) {
  1245. if (USE_SEPARATE_MODULES) {
  1246. LLVMSetLinkage(g.value, LLVMExternalLinkage);
  1247. } else {
  1248. LLVMSetLinkage(g.value, LLVMInternalLinkage);
  1249. }
  1250. }
  1251. lb_set_linkage_from_entity_flags(m, g.value, e->flags);
  1252. if (e->Variable.link_section.len > 0) {
  1253. LLVMSetSection(g.value, alloc_cstring(permanent_allocator(), e->Variable.link_section));
  1254. }
  1255. lbGlobalVariable var = {};
  1256. var.var = g;
  1257. var.decl = decl;
  1258. if (decl->init_expr != nullptr) {
  1259. TypeAndValue tav = type_and_value_of_expr(decl->init_expr);
  1260. if (!is_type_any(e->type) && !is_type_union(e->type)) {
  1261. if (tav.mode != Addressing_Invalid) {
  1262. if (tav.value.kind != ExactValue_Invalid) {
  1263. ExactValue v = tav.value;
  1264. lbValue init = lb_const_value(m, tav.type, v);
  1265. LLVMSetInitializer(g.value, init.value);
  1266. var.is_initialized = true;
  1267. }
  1268. }
  1269. }
  1270. if (!var.is_initialized &&
  1271. (is_type_untyped_nil(tav.type) || is_type_untyped_undef(tav.type))) {
  1272. var.is_initialized = true;
  1273. }
  1274. }
  1275. array_add(&global_variables, var);
  1276. lb_add_entity(m, e, g);
  1277. lb_add_member(m, name, g);
  1278. if (m->debug_builder) {
  1279. String global_name = e->token.string;
  1280. if (global_name.len != 0 && global_name != "_") {
  1281. LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, e->file);
  1282. LLVMMetadataRef llvm_scope = llvm_file;
  1283. LLVMBool local_to_unit = LLVMGetLinkage(g.value) == LLVMInternalLinkage;
  1284. LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
  1285. LLVMMetadataRef llvm_decl = nullptr;
  1286. u32 align_in_bits = cast(u32)(8*type_align_of(e->type));
  1287. LLVMMetadataRef global_variable_metadata = LLVMDIBuilderCreateGlobalVariableExpression(
  1288. m->debug_builder, llvm_scope,
  1289. cast(char const *)global_name.text, global_name.len,
  1290. "", 0, // linkage
  1291. llvm_file, e->token.pos.line,
  1292. lb_debug_type(m, e->type),
  1293. local_to_unit,
  1294. llvm_expr,
  1295. llvm_decl,
  1296. align_in_bits
  1297. );
  1298. lb_set_llvm_metadata(m, g.value, global_variable_metadata);
  1299. LLVMGlobalSetMetadata(g.value, 0, global_variable_metadata);
  1300. }
  1301. }
  1302. }
  1303. TIME_SECTION("LLVM Runtime Type Information Creation");
  1304. lbProcedure *startup_type_info = lb_create_startup_type_info(default_module);
  1305. lbProcedure *objc_names = lb_create_objc_names(default_module);
  1306. TIME_SECTION("LLVM Runtime Startup Creation (Global Variables)");
  1307. lbProcedure *startup_runtime = lb_create_startup_runtime(default_module, startup_type_info, objc_names, global_variables);
  1308. gb_unused(startup_runtime);
  1309. TIME_SECTION("LLVM Global Procedures and Types");
  1310. for_array(i, info->entities) {
  1311. Entity *e = info->entities[i];
  1312. String name = e->token.string;
  1313. Scope * scope = e->scope;
  1314. if ((scope->flags & ScopeFlag_File) == 0) {
  1315. continue;
  1316. }
  1317. Scope *package_scope = scope->parent;
  1318. GB_ASSERT(package_scope->flags & ScopeFlag_Pkg);
  1319. switch (e->kind) {
  1320. case Entity_Variable:
  1321. // NOTE(bill): Handled above as it requires a specific load order
  1322. continue;
  1323. case Entity_ProcGroup:
  1324. continue;
  1325. case Entity_TypeName:
  1326. case Entity_Procedure:
  1327. break;
  1328. }
  1329. bool polymorphic_struct = false;
  1330. if (e->type != nullptr && e->kind == Entity_TypeName) {
  1331. Type *bt = base_type(e->type);
  1332. if (bt->kind == Type_Struct) {
  1333. polymorphic_struct = is_type_polymorphic(bt);
  1334. }
  1335. }
  1336. if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) {
  1337. // NOTE(bill): Nothing depends upon it so doesn't need to be built
  1338. continue;
  1339. }
  1340. lbModule *m = &gen->default_module;
  1341. if (USE_SEPARATE_MODULES) {
  1342. m = lb_pkg_module(gen, e->pkg);
  1343. }
  1344. String mangled_name = lb_get_entity_name(m, e);
  1345. switch (e->kind) {
  1346. case Entity_TypeName:
  1347. lb_type(m, e->type);
  1348. break;
  1349. case Entity_Procedure:
  1350. {
  1351. lbProcedure *p = lb_create_procedure(m, e);
  1352. array_add(&m->procedures_to_generate, p);
  1353. }
  1354. break;
  1355. }
  1356. }
  1357. TIME_SECTION("LLVM Procedure Generation");
  1358. for_array(j, gen->modules.entries) {
  1359. lbModule *m = gen->modules.entries[j].value;
  1360. for_array(i, m->procedures_to_generate) {
  1361. lbProcedure *p = m->procedures_to_generate[i];
  1362. lb_generate_procedure(m, p);
  1363. }
  1364. }
  1365. if (build_context.command_kind == Command_test && !already_has_entry_point) {
  1366. TIME_SECTION("LLVM main");
  1367. lb_create_main_procedure(default_module, startup_runtime);
  1368. }
  1369. for_array(j, gen->modules.entries) {
  1370. lbModule *m = gen->modules.entries[j].value;
  1371. for_array(i, m->missing_procedures_to_check) {
  1372. lbProcedure *p = m->missing_procedures_to_check[i];
  1373. debugf("Generate missing procedure: %.*s\n", LIT(p->name));
  1374. lb_generate_procedure(m, p);
  1375. }
  1376. }
  1377. lb_finalize_objc_names(objc_names);
  1378. if (build_context.ODIN_DEBUG) {
  1379. TIME_SECTION("LLVM Debug Info Complete Types and Finalize");
  1380. for_array(j, gen->modules.entries) {
  1381. lbModule *m = gen->modules.entries[j].value;
  1382. if (m->debug_builder != nullptr) {
  1383. lb_debug_complete_types(m);
  1384. LLVMDIBuilderFinalize(m->debug_builder);
  1385. }
  1386. }
  1387. }
  1388. TIME_SECTION("LLVM Function Pass");
  1389. for_array(i, gen->modules.entries) {
  1390. lbModule *m = gen->modules.entries[i].value;
  1391. lb_llvm_function_pass_worker_proc(m);
  1392. }
  1393. TIME_SECTION("LLVM Module Pass");
  1394. for_array(i, gen->modules.entries) {
  1395. lbModule *m = gen->modules.entries[i].value;
  1396. lb_run_remove_unused_function_pass(m);
  1397. lb_run_remove_unused_globals_pass(m);
  1398. auto wd = gb_alloc_item(permanent_allocator(), lbLLVMModulePassWorkerData);
  1399. wd->m = m;
  1400. wd->target_machine = target_machines[i];
  1401. lb_llvm_module_pass_worker_proc(wd);
  1402. }
  1403. llvm_error = nullptr;
  1404. defer (LLVMDisposeMessage(llvm_error));
  1405. LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile;
  1406. if (build_context.build_mode == BuildMode_Assembly) {
  1407. code_gen_file_type = LLVMAssemblyFile;
  1408. }
  1409. for_array(j, gen->modules.entries) {
  1410. lbModule *m = gen->modules.entries[j].value;
  1411. if (LLVMVerifyModule(m->mod, LLVMReturnStatusAction, &llvm_error)) {
  1412. gb_printf_err("LLVM Error:\n%s\n", llvm_error);
  1413. if (build_context.keep_temp_files) {
  1414. TIME_SECTION("LLVM Print Module to File");
  1415. String filepath_ll = lb_filepath_ll_for_module(m);
  1416. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  1417. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1418. gb_exit(1);
  1419. return;
  1420. }
  1421. }
  1422. gb_exit(1);
  1423. return;
  1424. }
  1425. }
  1426. llvm_error = nullptr;
  1427. if (build_context.keep_temp_files ||
  1428. build_context.build_mode == BuildMode_LLVM_IR) {
  1429. TIME_SECTION("LLVM Print Module to File");
  1430. for_array(j, gen->modules.entries) {
  1431. lbModule *m = gen->modules.entries[j].value;
  1432. if (lb_is_module_empty(m)) {
  1433. continue;
  1434. }
  1435. String filepath_ll = lb_filepath_ll_for_module(m);
  1436. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  1437. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1438. gb_exit(1);
  1439. return;
  1440. }
  1441. array_add(&gen->output_temp_paths, filepath_ll);
  1442. }
  1443. if (build_context.build_mode == BuildMode_LLVM_IR) {
  1444. gb_exit(0);
  1445. return;
  1446. }
  1447. }
  1448. TIME_SECTION("LLVM Add Foreign Library Paths");
  1449. for_array(j, gen->modules.entries) {
  1450. lbModule *m = gen->modules.entries[j].value;
  1451. for_array(i, m->info->required_foreign_imports_through_force) {
  1452. Entity *e = m->info->required_foreign_imports_through_force[i];
  1453. lb_add_foreign_library_path(m, e);
  1454. }
  1455. if (lb_is_module_empty(m)) {
  1456. continue;
  1457. }
  1458. }
  1459. TIME_SECTION("LLVM Object Generation");
  1460. isize non_empty_module_count = 0;
  1461. for_array(j, gen->modules.entries) {
  1462. lbModule *m = gen->modules.entries[j].value;
  1463. if (!lb_is_module_empty(m)) {
  1464. non_empty_module_count += 1;
  1465. }
  1466. }
  1467. if (do_threading && non_empty_module_count > 1) {
  1468. for_array(j, gen->modules.entries) {
  1469. lbModule *m = gen->modules.entries[j].value;
  1470. if (lb_is_module_empty(m)) {
  1471. continue;
  1472. }
  1473. String filepath_ll = lb_filepath_ll_for_module(m);
  1474. String filepath_obj = lb_filepath_obj_for_module(m);
  1475. array_add(&gen->output_object_paths, filepath_obj);
  1476. array_add(&gen->output_temp_paths, filepath_ll);
  1477. auto *wd = gb_alloc_item(permanent_allocator(), lbLLVMEmitWorker);
  1478. wd->target_machine = target_machines[j];
  1479. wd->code_gen_file_type = code_gen_file_type;
  1480. wd->filepath_obj = filepath_obj;
  1481. wd->m = m;
  1482. global_thread_pool_add_task(lb_llvm_emit_worker_proc, wd);
  1483. }
  1484. thread_pool_wait(&global_thread_pool);
  1485. } else {
  1486. for_array(j, gen->modules.entries) {
  1487. lbModule *m = gen->modules.entries[j].value;
  1488. if (lb_is_module_empty(m)) {
  1489. continue;
  1490. }
  1491. String filepath_obj = lb_filepath_obj_for_module(m);
  1492. array_add(&gen->output_object_paths, filepath_obj);
  1493. String short_name = remove_directory_from_path(filepath_obj);
  1494. gbString section_name = gb_string_make(heap_allocator(), "LLVM Generate Object: ");
  1495. section_name = gb_string_append_length(section_name, short_name.text, short_name.len);
  1496. TIME_SECTION_WITH_LEN(section_name, gb_string_length(section_name));
  1497. if (LLVMTargetMachineEmitToFile(target_machines[j], m->mod, cast(char *)filepath_obj.text, code_gen_file_type, &llvm_error)) {
  1498. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1499. gb_exit(1);
  1500. return;
  1501. }
  1502. }
  1503. }
  1504. }