llvm_backend.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. if (build_context.disallow_rtti) {
  487. return nullptr;
  488. }
  489. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  490. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  491. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  492. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
  493. lbProcedure *p = lb_create_dummy_procedure(m, str_lit(LB_STARTUP_TYPE_INFO_PROC_NAME), proc_type);
  494. p->is_startup = true;
  495. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  496. lb_begin_procedure_body(p);
  497. lb_setup_type_info_data(p);
  498. lb_end_procedure_body(p);
  499. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  500. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  501. LLVMDumpValue(p->value);
  502. gb_printf_err("\n\n\n\n");
  503. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  504. }
  505. lb_run_function_pass_manager(default_function_pass_manager, p);
  506. return p;
  507. }
  508. lbProcedure *lb_create_objc_names(lbModule *main_module) {
  509. if (build_context.metrics.os != TargetOs_darwin) {
  510. return nullptr;
  511. }
  512. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl);
  513. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit("__$init_objc_names"), proc_type);
  514. p->is_startup = true;
  515. return p;
  516. }
  517. void lb_finalize_objc_names(lbProcedure *p) {
  518. if (p == nullptr) {
  519. return;
  520. }
  521. lbModule *m = p->module;
  522. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  523. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  524. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  525. auto args = array_make<lbValue>(permanent_allocator(), 1);
  526. LLVMSetLinkage(p->value, LLVMInternalLinkage);
  527. lb_begin_procedure_body(p);
  528. for_array(i, m->objc_classes.entries) {
  529. auto const &entry = m->objc_classes.entries[i];
  530. String name = entry.key.string;
  531. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  532. lbValue ptr = lb_emit_runtime_call(p, "objc_lookUpClass", args);
  533. lb_addr_store(p, entry.value, ptr);
  534. }
  535. for_array(i, m->objc_selectors.entries) {
  536. auto const &entry = m->objc_selectors.entries[i];
  537. String name = entry.key.string;
  538. args[0] = lb_const_value(m, t_cstring, exact_value_string(name));
  539. lbValue ptr = lb_emit_runtime_call(p, "sel_registerName", args);
  540. lb_addr_store(p, entry.value, ptr);
  541. }
  542. lb_end_procedure_body(p);
  543. lb_run_function_pass_manager(default_function_pass_manager, p);
  544. }
  545. lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array<lbGlobalVariable> &global_variables) { // Startup Runtime
  546. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(main_module->mod);
  547. lb_populate_function_pass_manager(main_module, default_function_pass_manager, false, build_context.optimization_level);
  548. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  549. Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin);
  550. lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type);
  551. p->is_startup = true;
  552. lb_begin_procedure_body(p);
  553. if (startup_type_info) {
  554. LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, startup_type_info->type)), startup_type_info->value, nullptr, 0, "");
  555. }
  556. if (objc_names) {
  557. LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, objc_names->type)), objc_names->value, nullptr, 0, "");
  558. }
  559. for_array(i, global_variables) {
  560. auto *var = &global_variables[i];
  561. if (var->is_initialized) {
  562. continue;
  563. }
  564. lbModule *entity_module = main_module;
  565. Entity *e = var->decl->entity;
  566. GB_ASSERT(e->kind == Entity_Variable);
  567. e->code_gen_module = entity_module;
  568. Ast *init_expr = var->decl->init_expr;
  569. if (init_expr != nullptr) {
  570. lbValue init = lb_build_expr(p, init_expr);
  571. if (init.value == nullptr) {
  572. LLVMTypeRef global_type = LLVMGetElementType(LLVMTypeOf(var->var.value));
  573. if (is_type_untyped_undef(init.type)) {
  574. // LLVMSetInitializer(var->var.value, LLVMGetUndef(global_type));
  575. LLVMSetInitializer(var->var.value, LLVMConstNull(global_type));
  576. var->is_initialized = true;
  577. continue;
  578. } else if (is_type_untyped_nil(init.type)) {
  579. LLVMSetInitializer(var->var.value, LLVMConstNull(global_type));
  580. var->is_initialized = true;
  581. continue;
  582. }
  583. GB_PANIC("Invalid init value, got %s", expr_to_string(init_expr));
  584. }
  585. if (is_type_any(e->type) || is_type_union(e->type)) {
  586. var->init = init;
  587. } else if (lb_is_const_or_global(init)) {
  588. if (!var->is_initialized) {
  589. LLVMSetInitializer(var->var.value, init.value);
  590. var->is_initialized = true;
  591. continue;
  592. }
  593. } else {
  594. var->init = init;
  595. }
  596. }
  597. if (var->init.value != nullptr) {
  598. GB_ASSERT(!var->is_initialized);
  599. Type *t = type_deref(var->var.type);
  600. if (is_type_any(t)) {
  601. // NOTE(bill): Edge case for 'any' type
  602. Type *var_type = default_type(var->init.type);
  603. lbAddr g = lb_add_global_generated(main_module, var_type, var->init);
  604. lb_addr_store(p, g, var->init);
  605. lbValue gp = lb_addr_get_ptr(p, g);
  606. lbValue data = lb_emit_struct_ep(p, var->var, 0);
  607. lbValue ti = lb_emit_struct_ep(p, var->var, 1);
  608. lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr));
  609. lb_emit_store(p, ti, lb_type_info(main_module, var_type));
  610. } else {
  611. LLVMTypeRef pvt = LLVMTypeOf(var->var.value);
  612. LLVMTypeRef vt = LLVMGetElementType(pvt);
  613. lbValue src0 = lb_emit_conv(p, var->init, t);
  614. LLVMValueRef src = OdinLLVMBuildTransmute(p, src0.value, vt);
  615. LLVMValueRef dst = var->var.value;
  616. LLVMBuildStore(p->builder, src, dst);
  617. }
  618. var->is_initialized = true;
  619. }
  620. }
  621. CheckerInfo *info = main_module->gen->info;
  622. for (Entity *e : info->init_procedures) {
  623. lbValue value = lb_find_procedure_value_from_entity(main_module, e);
  624. lb_emit_call(p, value, {}, ProcInlining_none, false);
  625. }
  626. lb_end_procedure_body(p);
  627. if (!main_module->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  628. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  629. LLVMDumpValue(p->value);
  630. gb_printf_err("\n\n\n\n");
  631. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  632. }
  633. lb_run_function_pass_manager(default_function_pass_manager, p);
  634. return p;
  635. }
  636. lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) {
  637. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  638. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  639. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  640. Type *params = alloc_type_tuple();
  641. Type *results = alloc_type_tuple();
  642. Type *t_ptr_cstring = alloc_type_pointer(t_cstring);
  643. bool call_cleanup = true;
  644. bool has_args = false;
  645. bool is_dll_main = false;
  646. String name = str_lit("main");
  647. if (build_context.metrics.os == TargetOs_windows && build_context.build_mode == BuildMode_DynamicLibrary) {
  648. is_dll_main = true;
  649. name = str_lit("DllMain");
  650. slice_init(&params->Tuple.variables, permanent_allocator(), 3);
  651. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("hinstDLL"), t_rawptr, false, true);
  652. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("fdwReason"), t_u32, false, true);
  653. params->Tuple.variables[2] = alloc_entity_param(nullptr, make_token_ident("lpReserved"), t_rawptr, false, true);
  654. call_cleanup = false;
  655. } else if (build_context.metrics.os == TargetOs_windows && (build_context.metrics.arch == TargetArch_i386 || build_context.no_crt)) {
  656. name = str_lit("mainCRTStartup");
  657. } else if (is_arch_wasm()) {
  658. name = str_lit("_start");
  659. call_cleanup = false;
  660. } else {
  661. has_args = true;
  662. slice_init(&params->Tuple.variables, permanent_allocator(), 2);
  663. params->Tuple.variables[0] = alloc_entity_param(nullptr, make_token_ident("argc"), t_i32, false, true);
  664. params->Tuple.variables[1] = alloc_entity_param(nullptr, make_token_ident("argv"), t_ptr_cstring, false, true);
  665. }
  666. slice_init(&results->Tuple.variables, permanent_allocator(), 1);
  667. results->Tuple.variables[0] = alloc_entity_param(nullptr, blank_token, t_i32, false, true);
  668. Type *proc_type = alloc_type_proc(nullptr,
  669. params, params->Tuple.variables.count,
  670. results, results->Tuple.variables.count, false, ProcCC_CDecl);
  671. lbProcedure *p = lb_create_dummy_procedure(m, name, proc_type);
  672. p->is_startup = true;
  673. lb_begin_procedure_body(p);
  674. if (has_args) { // initialize `runtime.args__`
  675. lbValue argc = {LLVMGetParam(p->value, 0), t_i32};
  676. lbValue argv = {LLVMGetParam(p->value, 1), t_ptr_cstring};
  677. LLVMSetValueName2(argc.value, "argc", 4);
  678. LLVMSetValueName2(argv.value, "argv", 4);
  679. argc = lb_emit_conv(p, argc, t_int);
  680. lbAddr args = lb_addr(lb_find_runtime_value(p->module, str_lit("args__")));
  681. lb_fill_slice(p, args, argv, argc);
  682. }
  683. lbValue startup_runtime_value = {startup_runtime->value, startup_runtime->type};
  684. lb_emit_call(p, startup_runtime_value, {}, ProcInlining_none, false);
  685. if (build_context.command_kind == Command_test) {
  686. Type *t_Internal_Test = find_type_in_pkg(m->info, str_lit("testing"), str_lit("Internal_Test"));
  687. Type *array_type = alloc_type_array(t_Internal_Test, m->info->testing_procedures.count);
  688. Type *slice_type = alloc_type_slice(t_Internal_Test);
  689. lbAddr all_tests_array_addr = lb_add_global_generated(p->module, array_type, {});
  690. lbValue all_tests_array = lb_addr_get_ptr(p, all_tests_array_addr);
  691. LLVMValueRef indices[2] = {};
  692. indices[0] = LLVMConstInt(lb_type(m, t_i32), 0, false);
  693. for_array(i, m->info->testing_procedures) {
  694. Entity *testing_proc = m->info->testing_procedures[i];
  695. String name = testing_proc->token.string;
  696. String pkg_name = {};
  697. if (testing_proc->pkg != nullptr) {
  698. pkg_name = testing_proc->pkg->name;
  699. }
  700. lbValue v_pkg = lb_find_or_add_entity_string(m, pkg_name);
  701. lbValue v_name = lb_find_or_add_entity_string(m, name);
  702. lbValue v_proc = lb_find_procedure_value_from_entity(m, testing_proc);
  703. indices[1] = LLVMConstInt(lb_type(m, t_int), i, false);
  704. LLVMValueRef vals[3] = {};
  705. vals[0] = v_pkg.value;
  706. vals[1] = v_name.value;
  707. vals[2] = v_proc.value;
  708. GB_ASSERT(LLVMIsConstant(vals[0]));
  709. GB_ASSERT(LLVMIsConstant(vals[1]));
  710. GB_ASSERT(LLVMIsConstant(vals[2]));
  711. LLVMValueRef dst = LLVMConstInBoundsGEP(all_tests_array.value, indices, gb_count_of(indices));
  712. LLVMValueRef src = llvm_const_named_struct(m, t_Internal_Test, vals, gb_count_of(vals));
  713. LLVMBuildStore(p->builder, src, dst);
  714. }
  715. lbAddr all_tests_slice = lb_add_local_generated(p, slice_type, true);
  716. lb_fill_slice(p, all_tests_slice,
  717. lb_array_elem(p, all_tests_array),
  718. lb_const_int(m, t_int, m->info->testing_procedures.count));
  719. lbValue runner = lb_find_package_value(m, str_lit("testing"), str_lit("runner"));
  720. auto args = array_make<lbValue>(heap_allocator(), 1);
  721. args[0] = lb_addr_load(p, all_tests_slice);
  722. lb_emit_call(p, runner, args);
  723. } else {
  724. if (m->info->entry_point != nullptr) {
  725. lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point);
  726. lb_emit_call(p, entry_point, {}, ProcInlining_no_inline);
  727. }
  728. }
  729. if (call_cleanup) {
  730. lbValue cleanup_runtime_value = lb_find_runtime_value(m, str_lit("_cleanup_runtime"));
  731. lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none, false);
  732. }
  733. if (is_dll_main) {
  734. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false));
  735. } else {
  736. LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false));
  737. }
  738. lb_end_procedure_body(p);
  739. LLVMSetLinkage(p->value, LLVMExternalLinkage);
  740. if (is_arch_wasm()) {
  741. lb_set_wasm_export_attributes(p->value, p->name);
  742. }
  743. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  744. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main");
  745. LLVMDumpValue(p->value);
  746. gb_printf_err("\n\n\n\n");
  747. LLVMVerifyFunction(p->value, LLVMAbortProcessAction);
  748. }
  749. lb_run_function_pass_manager(default_function_pass_manager, p);
  750. return p;
  751. }
  752. String lb_filepath_ll_for_module(lbModule *m) {
  753. String path = m->gen->output_base;
  754. if (m->pkg) {
  755. path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
  756. } else if (USE_SEPARATE_MODULES) {
  757. path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin"));
  758. }
  759. path = concatenate_strings(permanent_allocator(), path, STR_LIT(".ll"));
  760. return path;
  761. }
  762. String lb_filepath_obj_for_module(lbModule *m) {
  763. String path = m->gen->output_base;
  764. if (m->pkg) {
  765. path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
  766. }
  767. String ext = {};
  768. if (build_context.build_mode == BuildMode_Assembly) {
  769. ext = STR_LIT(".S");
  770. } else {
  771. if (is_arch_wasm()) {
  772. ext = STR_LIT(".wasm.o");
  773. } else {
  774. switch (build_context.metrics.os) {
  775. case TargetOs_windows:
  776. ext = STR_LIT(".obj");
  777. break;
  778. default:
  779. case TargetOs_darwin:
  780. case TargetOs_linux:
  781. case TargetOs_essence:
  782. ext = STR_LIT(".o");
  783. break;
  784. case TargetOs_freestanding:
  785. switch (build_context.metrics.abi) {
  786. default:
  787. case TargetABI_Default:
  788. case TargetABI_SysV:
  789. ext = STR_LIT(".o");
  790. break;
  791. case TargetABI_Win64:
  792. ext = STR_LIT(".obj");
  793. break;
  794. }
  795. break;
  796. }
  797. }
  798. }
  799. return concatenate_strings(permanent_allocator(), path, ext);
  800. }
  801. bool lb_is_module_empty(lbModule *m) {
  802. if (LLVMGetFirstFunction(m->mod) == nullptr &&
  803. LLVMGetFirstGlobal(m->mod) == nullptr) {
  804. return true;
  805. }
  806. for (auto fn = LLVMGetFirstFunction(m->mod); fn != nullptr; fn = LLVMGetNextFunction(fn)) {
  807. if (LLVMGetFirstBasicBlock(fn) != nullptr) {
  808. return false;
  809. }
  810. }
  811. for (auto g = LLVMGetFirstGlobal(m->mod); g != nullptr; g = LLVMGetNextGlobal(g)) {
  812. if (LLVMGetLinkage(g) == LLVMExternalLinkage) {
  813. continue;
  814. }
  815. if (!LLVMIsExternallyInitialized(g)) {
  816. return false;
  817. }
  818. }
  819. return true;
  820. }
  821. struct lbLLVMEmitWorker {
  822. LLVMTargetMachineRef target_machine;
  823. LLVMCodeGenFileType code_gen_file_type;
  824. String filepath_obj;
  825. lbModule *m;
  826. };
  827. WORKER_TASK_PROC(lb_llvm_emit_worker_proc) {
  828. GB_ASSERT(MULTITHREAD_OBJECT_GENERATION);
  829. char *llvm_error = nullptr;
  830. auto wd = cast(lbLLVMEmitWorker *)data;
  831. if (LLVMTargetMachineEmitToFile(wd->target_machine, wd->m->mod, cast(char *)wd->filepath_obj.text, wd->code_gen_file_type, &llvm_error)) {
  832. gb_printf_err("LLVM Error: %s\n", llvm_error);
  833. gb_exit(1);
  834. }
  835. return 0;
  836. }
  837. WORKER_TASK_PROC(lb_llvm_function_pass_worker_proc) {
  838. GB_ASSERT(MULTITHREAD_OBJECT_GENERATION);
  839. auto m = cast(lbModule *)data;
  840. LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod);
  841. LLVMPassManagerRef function_pass_manager_minimal = LLVMCreateFunctionPassManagerForModule(m->mod);
  842. LLVMPassManagerRef function_pass_manager_size = LLVMCreateFunctionPassManagerForModule(m->mod);
  843. LLVMPassManagerRef function_pass_manager_speed = LLVMCreateFunctionPassManagerForModule(m->mod);
  844. LLVMInitializeFunctionPassManager(default_function_pass_manager);
  845. LLVMInitializeFunctionPassManager(function_pass_manager_minimal);
  846. LLVMInitializeFunctionPassManager(function_pass_manager_size);
  847. LLVMInitializeFunctionPassManager(function_pass_manager_speed);
  848. lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level);
  849. lb_populate_function_pass_manager_specific(m, function_pass_manager_minimal, 0);
  850. lb_populate_function_pass_manager_specific(m, function_pass_manager_size, 1);
  851. lb_populate_function_pass_manager_specific(m, function_pass_manager_speed, 2);
  852. LLVMFinalizeFunctionPassManager(default_function_pass_manager);
  853. LLVMFinalizeFunctionPassManager(function_pass_manager_minimal);
  854. LLVMFinalizeFunctionPassManager(function_pass_manager_size);
  855. LLVMFinalizeFunctionPassManager(function_pass_manager_speed);
  856. LLVMPassManagerRef default_function_pass_manager_without_memcpy = LLVMCreateFunctionPassManagerForModule(m->mod);
  857. LLVMInitializeFunctionPassManager(default_function_pass_manager_without_memcpy);
  858. lb_populate_function_pass_manager(m, default_function_pass_manager_without_memcpy, true, build_context.optimization_level);
  859. LLVMFinalizeFunctionPassManager(default_function_pass_manager_without_memcpy);
  860. for_array(i, m->procedures_to_generate) {
  861. lbProcedure *p = m->procedures_to_generate[i];
  862. if (p->body != nullptr) { // Build Procedure
  863. if (p->flags & lbProcedureFlag_WithoutMemcpyPass) {
  864. lb_run_function_pass_manager(default_function_pass_manager_without_memcpy, p);
  865. } else {
  866. if (p->entity && p->entity->kind == Entity_Procedure) {
  867. switch (p->entity->Procedure.optimization_mode) {
  868. case ProcedureOptimizationMode_None:
  869. case ProcedureOptimizationMode_Minimal:
  870. lb_run_function_pass_manager(function_pass_manager_minimal, p);
  871. break;
  872. case ProcedureOptimizationMode_Size:
  873. lb_run_function_pass_manager(function_pass_manager_size, p);
  874. break;
  875. case ProcedureOptimizationMode_Speed:
  876. lb_run_function_pass_manager(function_pass_manager_speed, p);
  877. break;
  878. default:
  879. lb_run_function_pass_manager(default_function_pass_manager, p);
  880. break;
  881. }
  882. } else {
  883. lb_run_function_pass_manager(default_function_pass_manager, p);
  884. }
  885. }
  886. }
  887. }
  888. for_array(i, m->equal_procs.entries) {
  889. lbProcedure *p = m->equal_procs.entries[i].value;
  890. lb_run_function_pass_manager(default_function_pass_manager, p);
  891. }
  892. for_array(i, m->hasher_procs.entries) {
  893. lbProcedure *p = m->hasher_procs.entries[i].value;
  894. lb_run_function_pass_manager(default_function_pass_manager, p);
  895. }
  896. return 0;
  897. }
  898. struct lbLLVMModulePassWorkerData {
  899. lbModule *m;
  900. LLVMTargetMachineRef target_machine;
  901. };
  902. WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) {
  903. auto wd = cast(lbLLVMModulePassWorkerData *)data;
  904. LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager();
  905. lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level);
  906. LLVMRunPassManager(module_pass_manager, wd->m->mod);
  907. return 0;
  908. }
  909. void lb_generate_procedure(lbModule *m, lbProcedure *p) {
  910. if (p->is_done) {
  911. return;
  912. }
  913. if (p->body != nullptr) { // Build Procedure
  914. m->curr_procedure = p;
  915. lb_begin_procedure_body(p);
  916. lb_build_stmt(p, p->body);
  917. lb_end_procedure_body(p);
  918. p->is_done = true;
  919. m->curr_procedure = nullptr;
  920. }
  921. lb_end_procedure(p);
  922. // Add Flags
  923. if (p->body != nullptr) {
  924. if (p->name == "memcpy" || p->name == "memmove" ||
  925. p->name == "runtime.mem_copy" || p->name == "mem_copy_non_overlapping" ||
  926. string_starts_with(p->name, str_lit("llvm.memcpy")) ||
  927. string_starts_with(p->name, str_lit("llvm.memmove"))) {
  928. p->flags |= lbProcedureFlag_WithoutMemcpyPass;
  929. }
  930. }
  931. if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) {
  932. char *llvm_error = nullptr;
  933. gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %.*s\n", LIT(p->name));
  934. LLVMDumpValue(p->value);
  935. gb_printf_err("\n\n\n\n");
  936. String filepath_ll = lb_filepath_ll_for_module(m);
  937. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  938. gb_printf_err("LLVM Error: %s\n", llvm_error);
  939. }
  940. LLVMVerifyFunction(p->value, LLVMPrintMessageAction);
  941. gb_exit(1);
  942. }
  943. }
  944. void lb_generate_code(lbGenerator *gen) {
  945. TIME_SECTION("LLVM Initializtion");
  946. isize thread_count = gb_max(build_context.thread_count, 1);
  947. isize worker_count = thread_count-1;
  948. LLVMBool do_threading = (LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0);
  949. lbModule *default_module = &gen->default_module;
  950. CheckerInfo *info = gen->info;
  951. auto *min_dep_set = &info->minimum_dependency_set;
  952. switch (build_context.metrics.arch) {
  953. case TargetArch_amd64:
  954. case TargetArch_i386:
  955. LLVMInitializeX86TargetInfo();
  956. LLVMInitializeX86Target();
  957. LLVMInitializeX86TargetMC();
  958. LLVMInitializeX86AsmPrinter();
  959. LLVMInitializeX86AsmParser();
  960. LLVMInitializeX86Disassembler();
  961. break;
  962. case TargetArch_arm64:
  963. LLVMInitializeAArch64TargetInfo();
  964. LLVMInitializeAArch64Target();
  965. LLVMInitializeAArch64TargetMC();
  966. LLVMInitializeAArch64AsmPrinter();
  967. LLVMInitializeAArch64AsmParser();
  968. LLVMInitializeAArch64Disassembler();
  969. break;
  970. case TargetArch_wasm32:
  971. case TargetArch_wasm64:
  972. LLVMInitializeWebAssemblyTargetInfo();
  973. LLVMInitializeWebAssemblyTarget();
  974. LLVMInitializeWebAssemblyTargetMC();
  975. LLVMInitializeWebAssemblyAsmPrinter();
  976. LLVMInitializeWebAssemblyAsmParser();
  977. LLVMInitializeWebAssemblyDisassembler();
  978. break;
  979. default:
  980. LLVMInitializeAllTargetInfos();
  981. LLVMInitializeAllTargets();
  982. LLVMInitializeAllTargetMCs();
  983. LLVMInitializeAllAsmPrinters();
  984. LLVMInitializeAllAsmParsers();
  985. LLVMInitializeAllDisassemblers();
  986. break;
  987. }
  988. if (build_context.microarch == "native") {
  989. LLVMInitializeNativeTarget();
  990. }
  991. char const *target_triple = alloc_cstring(permanent_allocator(), build_context.metrics.target_triplet);
  992. for_array(i, gen->modules.entries) {
  993. LLVMSetTarget(gen->modules.entries[i].value->mod, target_triple);
  994. }
  995. LLVMTargetRef target = {};
  996. char *llvm_error = nullptr;
  997. LLVMGetTargetFromTriple(target_triple, &target, &llvm_error);
  998. GB_ASSERT(target != nullptr);
  999. TIME_SECTION("LLVM Create Target Machine");
  1000. LLVMCodeModel code_mode = LLVMCodeModelDefault;
  1001. if (is_arch_wasm()) {
  1002. code_mode = LLVMCodeModelJITDefault;
  1003. } else if (build_context.metrics.os == TargetOs_freestanding) {
  1004. code_mode = LLVMCodeModelKernel;
  1005. }
  1006. char const *host_cpu_name = LLVMGetHostCPUName();
  1007. char const *llvm_cpu = "generic";
  1008. char const *llvm_features = "";
  1009. if (build_context.microarch.len != 0) {
  1010. if (build_context.microarch == "native") {
  1011. llvm_cpu = host_cpu_name;
  1012. } else {
  1013. llvm_cpu = alloc_cstring(permanent_allocator(), build_context.microarch);
  1014. }
  1015. if (gb_strcmp(llvm_cpu, host_cpu_name) == 0) {
  1016. llvm_features = LLVMGetHostCPUFeatures();
  1017. }
  1018. } else if (build_context.metrics.arch == TargetArch_amd64) {
  1019. // NOTE(bill): x86-64-v2 is more than enough for everyone
  1020. //
  1021. // x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2
  1022. // x86-64-v2: (close to Nehalem) CMPXCHG16B, LAHF-SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3
  1023. // x86-64-v3: (close to Haswell) AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, XSAVE
  1024. // x86-64-v4: AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL
  1025. if (ODIN_LLVM_MINIMUM_VERSION_12) {
  1026. if (build_context.metrics.os == TargetOs_freestanding) {
  1027. llvm_cpu = "x86-64";
  1028. } else {
  1029. llvm_cpu = "x86-64-v2";
  1030. }
  1031. }
  1032. }
  1033. if (build_context.target_features.len != 0) {
  1034. llvm_features = alloc_cstring(permanent_allocator(), build_context.target_features);
  1035. }
  1036. // GB_ASSERT_MSG(LLVMTargetHasAsmBackend(target));
  1037. LLVMCodeGenOptLevel code_gen_level = LLVMCodeGenLevelNone;
  1038. switch (build_context.optimization_level) {
  1039. case 0: code_gen_level = LLVMCodeGenLevelNone; break;
  1040. case 1: code_gen_level = LLVMCodeGenLevelLess; break;
  1041. case 2: code_gen_level = LLVMCodeGenLevelDefault; break;
  1042. case 3: code_gen_level = LLVMCodeGenLevelDefault; break; // NOTE(bill): force -opt:3 to be the same as -opt:2
  1043. // case 3: code_gen_level = LLVMCodeGenLevelAggressive; break;
  1044. }
  1045. // NOTE(bill): Target Machine Creation
  1046. // NOTE(bill, 2021-05-04): Target machines must be unique to each module because they are not thread safe
  1047. auto target_machines = array_make<LLVMTargetMachineRef>(permanent_allocator(), gen->modules.entries.count);
  1048. // NOTE(dweiler): Dynamic libraries require position-independent code.
  1049. LLVMRelocMode reloc_mode = LLVMRelocDefault;
  1050. if (build_context.build_mode == BuildMode_DynamicLibrary) {
  1051. reloc_mode = LLVMRelocPIC;
  1052. }
  1053. switch (build_context.reloc_mode) {
  1054. case RelocMode_Default:
  1055. if (build_context.metrics.os == TargetOs_openbsd) {
  1056. // Always use PIC for OpenBSD: it defaults to PIE
  1057. reloc_mode = LLVMRelocPIC;
  1058. }
  1059. break;
  1060. case RelocMode_Static:
  1061. reloc_mode = LLVMRelocStatic;
  1062. break;
  1063. case RelocMode_PIC:
  1064. reloc_mode = LLVMRelocPIC;
  1065. break;
  1066. case RelocMode_DynamicNoPIC:
  1067. reloc_mode = LLVMRelocDynamicNoPic;
  1068. break;
  1069. }
  1070. for_array(i, gen->modules.entries) {
  1071. target_machines[i] = LLVMCreateTargetMachine(
  1072. target, target_triple, llvm_cpu,
  1073. llvm_features,
  1074. code_gen_level,
  1075. reloc_mode,
  1076. code_mode);
  1077. LLVMSetModuleDataLayout(gen->modules.entries[i].value->mod, LLVMCreateTargetDataLayout(target_machines[i]));
  1078. }
  1079. for_array(i, gen->modules.entries) {
  1080. lbModule *m = gen->modules.entries[i].value;
  1081. if (m->debug_builder) { // Debug Info
  1082. for_array(i, info->files.entries) {
  1083. AstFile *f = info->files.entries[i].value;
  1084. String fullpath = f->fullpath;
  1085. String filename = remove_directory_from_path(fullpath);
  1086. String directory = directory_from_path(fullpath);
  1087. LLVMMetadataRef res = LLVMDIBuilderCreateFile(m->debug_builder,
  1088. cast(char const *)filename.text, filename.len,
  1089. cast(char const *)directory.text, directory.len);
  1090. lb_set_llvm_metadata(m, f, res);
  1091. }
  1092. gbString producer = gb_string_make(heap_allocator(), "odin");
  1093. // producer = gb_string_append_fmt(producer, " version %.*s", LIT(ODIN_VERSION));
  1094. // #ifdef NIGHTLY
  1095. // producer = gb_string_appendc(producer, "-nightly");
  1096. // #endif
  1097. // #ifdef GIT_SHA
  1098. // producer = gb_string_append_fmt(producer, "-%s", GIT_SHA);
  1099. // #endif
  1100. gbString split_name = gb_string_make(heap_allocator(), "");
  1101. LLVMBool is_optimized = build_context.optimization_level > 0;
  1102. AstFile *init_file = m->info->init_package->files[0];
  1103. if (Entity *entry_point = m->info->entry_point) {
  1104. if (Ast *ident = entry_point->identifier.load()) {
  1105. if (ident->file_id) {
  1106. init_file = ident->file();
  1107. }
  1108. }
  1109. }
  1110. LLVMBool split_debug_inlining = false;
  1111. LLVMBool debug_info_for_profiling = false;
  1112. m->debug_compile_unit = LLVMDIBuilderCreateCompileUnit(m->debug_builder, LLVMDWARFSourceLanguageC99,
  1113. lb_get_llvm_metadata(m, init_file),
  1114. producer, gb_string_length(producer),
  1115. is_optimized, "", 0,
  1116. 1, split_name, gb_string_length(split_name),
  1117. LLVMDWARFEmissionFull,
  1118. 0, split_debug_inlining,
  1119. debug_info_for_profiling,
  1120. "", 0, // sys_root
  1121. "", 0 // SDK
  1122. );
  1123. GB_ASSERT(m->debug_compile_unit != nullptr);
  1124. }
  1125. }
  1126. TIME_SECTION("LLVM Global Variables");
  1127. if (!build_context.disallow_rtti) {
  1128. lbModule *m = default_module;
  1129. { // Add type info data
  1130. isize max_type_info_count = info->minimum_dependency_type_info_set.entries.count+1;
  1131. // gb_printf_err("max_type_info_count: %td\n", max_type_info_count);
  1132. Type *t = alloc_type_array(t_type_info, max_type_info_count);
  1133. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), LB_TYPE_INFO_DATA_NAME);
  1134. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1135. if (!USE_SEPARATE_MODULES) {
  1136. LLVMSetLinkage(g, LLVMInternalLinkage);
  1137. }
  1138. lbValue value = {};
  1139. value.value = g;
  1140. value.type = alloc_type_pointer(t);
  1141. lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved);
  1142. lb_add_entity(m, lb_global_type_info_data_entity, value);
  1143. }
  1144. { // Type info member buffer
  1145. // NOTE(bill): Removes need for heap allocation by making it global memory
  1146. isize count = 0;
  1147. for_array(entry_index, m->info->type_info_types) {
  1148. Type *t = m->info->type_info_types[entry_index];
  1149. isize index = lb_type_info_index(m->info, t, false);
  1150. if (index < 0) {
  1151. continue;
  1152. }
  1153. switch (t->kind) {
  1154. case Type_Union:
  1155. count += t->Union.variants.count;
  1156. break;
  1157. case Type_Struct:
  1158. count += t->Struct.fields.count;
  1159. break;
  1160. case Type_Tuple:
  1161. count += t->Tuple.variables.count;
  1162. break;
  1163. }
  1164. }
  1165. if (count > 0) {
  1166. {
  1167. char const *name = LB_TYPE_INFO_TYPES_NAME;
  1168. Type *t = alloc_type_array(t_type_info_ptr, count);
  1169. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1170. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1171. LLVMSetLinkage(g, LLVMInternalLinkage);
  1172. lb_global_type_info_member_types = lb_addr({g, alloc_type_pointer(t)});
  1173. }
  1174. {
  1175. char const *name = LB_TYPE_INFO_NAMES_NAME;
  1176. Type *t = alloc_type_array(t_string, count);
  1177. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1178. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1179. LLVMSetLinkage(g, LLVMInternalLinkage);
  1180. lb_global_type_info_member_names = lb_addr({g, alloc_type_pointer(t)});
  1181. }
  1182. {
  1183. char const *name = LB_TYPE_INFO_OFFSETS_NAME;
  1184. Type *t = alloc_type_array(t_uintptr, count);
  1185. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1186. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1187. LLVMSetLinkage(g, LLVMInternalLinkage);
  1188. lb_global_type_info_member_offsets = lb_addr({g, alloc_type_pointer(t)});
  1189. }
  1190. {
  1191. char const *name = LB_TYPE_INFO_USINGS_NAME;
  1192. Type *t = alloc_type_array(t_bool, count);
  1193. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1194. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1195. LLVMSetLinkage(g, LLVMInternalLinkage);
  1196. lb_global_type_info_member_usings = lb_addr({g, alloc_type_pointer(t)});
  1197. }
  1198. {
  1199. char const *name = LB_TYPE_INFO_TAGS_NAME;
  1200. Type *t = alloc_type_array(t_string, count);
  1201. LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name);
  1202. LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t)));
  1203. LLVMSetLinkage(g, LLVMInternalLinkage);
  1204. lb_global_type_info_member_tags = lb_addr({g, alloc_type_pointer(t)});
  1205. }
  1206. }
  1207. }
  1208. }
  1209. isize global_variable_max_count = 0;
  1210. bool already_has_entry_point = false;
  1211. for_array(i, info->entities) {
  1212. Entity *e = info->entities[i];
  1213. String name = e->token.string;
  1214. if (e->kind == Entity_Variable) {
  1215. global_variable_max_count++;
  1216. } else if (e->kind == Entity_Procedure) {
  1217. if ((e->scope->flags&ScopeFlag_Init) && name == "main") {
  1218. GB_ASSERT(e == info->entry_point);
  1219. }
  1220. if (build_context.command_kind == Command_test &&
  1221. (e->Procedure.is_export || e->Procedure.link_name.len > 0)) {
  1222. String link_name = e->Procedure.link_name;
  1223. if (e->pkg->kind == Package_Runtime) {
  1224. if (link_name == "main" ||
  1225. link_name == "DllMain" ||
  1226. link_name == "WinMain" ||
  1227. link_name == "wWinMain" ||
  1228. link_name == "mainCRTStartup" ||
  1229. link_name == "_start") {
  1230. already_has_entry_point = true;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. auto global_variables = array_make<lbGlobalVariable>(permanent_allocator(), 0, global_variable_max_count);
  1237. for_array(i, info->variable_init_order) {
  1238. DeclInfo *d = info->variable_init_order[i];
  1239. Entity *e = d->entity;
  1240. if ((e->scope->flags & ScopeFlag_File) == 0) {
  1241. continue;
  1242. }
  1243. if (!ptr_set_exists(min_dep_set, e)) {
  1244. continue;
  1245. }
  1246. DeclInfo *decl = decl_info_of_entity(e);
  1247. if (decl == nullptr) {
  1248. continue;
  1249. }
  1250. GB_ASSERT(e->kind == Entity_Variable);
  1251. bool is_foreign = e->Variable.is_foreign;
  1252. bool is_export = e->Variable.is_export;
  1253. lbModule *m = &gen->default_module;
  1254. String name = lb_get_entity_name(m, e);
  1255. lbValue g = {};
  1256. g.value = LLVMAddGlobal(m->mod, lb_type(m, e->type), alloc_cstring(permanent_allocator(), name));
  1257. g.type = alloc_type_pointer(e->type);
  1258. if (e->Variable.thread_local_model != "") {
  1259. LLVMSetThreadLocal(g.value, true);
  1260. String m = e->Variable.thread_local_model;
  1261. LLVMThreadLocalMode mode = LLVMGeneralDynamicTLSModel;
  1262. if (m == "default") {
  1263. mode = LLVMGeneralDynamicTLSModel;
  1264. } else if (m == "localdynamic") {
  1265. mode = LLVMLocalDynamicTLSModel;
  1266. } else if (m == "initialexec") {
  1267. mode = LLVMInitialExecTLSModel;
  1268. } else if (m == "localexec") {
  1269. mode = LLVMLocalExecTLSModel;
  1270. } else {
  1271. GB_PANIC("Unhandled thread local mode %.*s", LIT(m));
  1272. }
  1273. LLVMSetThreadLocalMode(g.value, mode);
  1274. }
  1275. if (is_foreign) {
  1276. LLVMSetLinkage(g.value, LLVMExternalLinkage);
  1277. LLVMSetExternallyInitialized(g.value, true);
  1278. lb_add_foreign_library_path(m, e->Variable.foreign_library);
  1279. lb_set_wasm_import_attributes(g.value, e, name);
  1280. } else {
  1281. LLVMSetInitializer(g.value, LLVMConstNull(lb_type(m, e->type)));
  1282. }
  1283. if (is_export) {
  1284. LLVMSetLinkage(g.value, LLVMDLLExportLinkage);
  1285. LLVMSetDLLStorageClass(g.value, LLVMDLLExportStorageClass);
  1286. } else if (!is_foreign) {
  1287. if (USE_SEPARATE_MODULES) {
  1288. LLVMSetLinkage(g.value, LLVMExternalLinkage);
  1289. } else {
  1290. LLVMSetLinkage(g.value, LLVMInternalLinkage);
  1291. }
  1292. }
  1293. lb_set_linkage_from_entity_flags(m, g.value, e->flags);
  1294. if (e->Variable.link_section.len > 0) {
  1295. LLVMSetSection(g.value, alloc_cstring(permanent_allocator(), e->Variable.link_section));
  1296. }
  1297. lbGlobalVariable var = {};
  1298. var.var = g;
  1299. var.decl = decl;
  1300. if (decl->init_expr != nullptr) {
  1301. TypeAndValue tav = type_and_value_of_expr(decl->init_expr);
  1302. if (!is_type_any(e->type) && !is_type_union(e->type)) {
  1303. if (tav.mode != Addressing_Invalid) {
  1304. if (tav.value.kind != ExactValue_Invalid) {
  1305. ExactValue v = tav.value;
  1306. lbValue init = lb_const_value(m, tav.type, v);
  1307. LLVMSetInitializer(g.value, init.value);
  1308. var.is_initialized = true;
  1309. }
  1310. }
  1311. }
  1312. if (!var.is_initialized &&
  1313. (is_type_untyped_nil(tav.type) || is_type_untyped_undef(tav.type))) {
  1314. var.is_initialized = true;
  1315. }
  1316. }
  1317. array_add(&global_variables, var);
  1318. lb_add_entity(m, e, g);
  1319. lb_add_member(m, name, g);
  1320. if (m->debug_builder) {
  1321. String global_name = e->token.string;
  1322. if (global_name.len != 0 && global_name != "_") {
  1323. LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, e->file);
  1324. LLVMMetadataRef llvm_scope = llvm_file;
  1325. LLVMBool local_to_unit = LLVMGetLinkage(g.value) == LLVMInternalLinkage;
  1326. LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
  1327. LLVMMetadataRef llvm_decl = nullptr;
  1328. u32 align_in_bits = cast(u32)(8*type_align_of(e->type));
  1329. LLVMMetadataRef global_variable_metadata = LLVMDIBuilderCreateGlobalVariableExpression(
  1330. m->debug_builder, llvm_scope,
  1331. cast(char const *)global_name.text, global_name.len,
  1332. "", 0, // linkage
  1333. llvm_file, e->token.pos.line,
  1334. lb_debug_type(m, e->type),
  1335. local_to_unit,
  1336. llvm_expr,
  1337. llvm_decl,
  1338. align_in_bits
  1339. );
  1340. lb_set_llvm_metadata(m, g.value, global_variable_metadata);
  1341. LLVMGlobalSetMetadata(g.value, 0, global_variable_metadata);
  1342. }
  1343. }
  1344. }
  1345. TIME_SECTION("LLVM Runtime Type Information Creation");
  1346. lbProcedure *startup_type_info = lb_create_startup_type_info(default_module);
  1347. lbProcedure *objc_names = lb_create_objc_names(default_module);
  1348. TIME_SECTION("LLVM Runtime Startup Creation (Global Variables)");
  1349. lbProcedure *startup_runtime = lb_create_startup_runtime(default_module, startup_type_info, objc_names, global_variables);
  1350. gb_unused(startup_runtime);
  1351. TIME_SECTION("LLVM Global Procedures and Types");
  1352. for_array(i, info->entities) {
  1353. Entity *e = info->entities[i];
  1354. String name = e->token.string;
  1355. Scope * scope = e->scope;
  1356. if ((scope->flags & ScopeFlag_File) == 0) {
  1357. continue;
  1358. }
  1359. Scope *package_scope = scope->parent;
  1360. GB_ASSERT(package_scope->flags & ScopeFlag_Pkg);
  1361. switch (e->kind) {
  1362. case Entity_Variable:
  1363. // NOTE(bill): Handled above as it requires a specific load order
  1364. continue;
  1365. case Entity_ProcGroup:
  1366. continue;
  1367. case Entity_TypeName:
  1368. case Entity_Procedure:
  1369. break;
  1370. }
  1371. bool polymorphic_struct = false;
  1372. if (e->type != nullptr && e->kind == Entity_TypeName) {
  1373. Type *bt = base_type(e->type);
  1374. if (bt->kind == Type_Struct) {
  1375. polymorphic_struct = is_type_polymorphic(bt);
  1376. }
  1377. }
  1378. if (!polymorphic_struct && !ptr_set_exists(min_dep_set, e)) {
  1379. // NOTE(bill): Nothing depends upon it so doesn't need to be built
  1380. continue;
  1381. }
  1382. lbModule *m = &gen->default_module;
  1383. if (USE_SEPARATE_MODULES) {
  1384. m = lb_pkg_module(gen, e->pkg);
  1385. }
  1386. String mangled_name = lb_get_entity_name(m, e);
  1387. switch (e->kind) {
  1388. case Entity_TypeName:
  1389. lb_type(m, e->type);
  1390. break;
  1391. case Entity_Procedure:
  1392. {
  1393. lbProcedure *p = lb_create_procedure(m, e);
  1394. array_add(&m->procedures_to_generate, p);
  1395. }
  1396. break;
  1397. }
  1398. }
  1399. TIME_SECTION("LLVM Procedure Generation");
  1400. for_array(j, gen->modules.entries) {
  1401. lbModule *m = gen->modules.entries[j].value;
  1402. for_array(i, m->procedures_to_generate) {
  1403. lbProcedure *p = m->procedures_to_generate[i];
  1404. lb_generate_procedure(m, p);
  1405. }
  1406. }
  1407. if (build_context.command_kind == Command_test && !already_has_entry_point) {
  1408. TIME_SECTION("LLVM main");
  1409. lb_create_main_procedure(default_module, startup_runtime);
  1410. }
  1411. for_array(j, gen->modules.entries) {
  1412. lbModule *m = gen->modules.entries[j].value;
  1413. for_array(i, m->missing_procedures_to_check) {
  1414. lbProcedure *p = m->missing_procedures_to_check[i];
  1415. debugf("Generate missing procedure: %.*s\n", LIT(p->name));
  1416. lb_generate_procedure(m, p);
  1417. }
  1418. }
  1419. lb_finalize_objc_names(objc_names);
  1420. if (build_context.ODIN_DEBUG) {
  1421. TIME_SECTION("LLVM Debug Info Complete Types and Finalize");
  1422. for_array(j, gen->modules.entries) {
  1423. lbModule *m = gen->modules.entries[j].value;
  1424. if (m->debug_builder != nullptr) {
  1425. lb_debug_complete_types(m);
  1426. LLVMDIBuilderFinalize(m->debug_builder);
  1427. }
  1428. }
  1429. }
  1430. TIME_SECTION("LLVM Function Pass");
  1431. for_array(i, gen->modules.entries) {
  1432. lbModule *m = gen->modules.entries[i].value;
  1433. lb_llvm_function_pass_worker_proc(m);
  1434. }
  1435. TIME_SECTION("LLVM Module Pass");
  1436. for_array(i, gen->modules.entries) {
  1437. lbModule *m = gen->modules.entries[i].value;
  1438. lb_run_remove_unused_function_pass(m);
  1439. lb_run_remove_unused_globals_pass(m);
  1440. auto wd = gb_alloc_item(permanent_allocator(), lbLLVMModulePassWorkerData);
  1441. wd->m = m;
  1442. wd->target_machine = target_machines[i];
  1443. lb_llvm_module_pass_worker_proc(wd);
  1444. }
  1445. llvm_error = nullptr;
  1446. defer (LLVMDisposeMessage(llvm_error));
  1447. LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile;
  1448. if (build_context.build_mode == BuildMode_Assembly) {
  1449. code_gen_file_type = LLVMAssemblyFile;
  1450. }
  1451. for_array(j, gen->modules.entries) {
  1452. lbModule *m = gen->modules.entries[j].value;
  1453. if (LLVMVerifyModule(m->mod, LLVMReturnStatusAction, &llvm_error)) {
  1454. gb_printf_err("LLVM Error:\n%s\n", llvm_error);
  1455. if (build_context.keep_temp_files) {
  1456. TIME_SECTION("LLVM Print Module to File");
  1457. String filepath_ll = lb_filepath_ll_for_module(m);
  1458. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  1459. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1460. gb_exit(1);
  1461. return;
  1462. }
  1463. }
  1464. gb_exit(1);
  1465. return;
  1466. }
  1467. }
  1468. llvm_error = nullptr;
  1469. if (build_context.keep_temp_files ||
  1470. build_context.build_mode == BuildMode_LLVM_IR) {
  1471. TIME_SECTION("LLVM Print Module to File");
  1472. for_array(j, gen->modules.entries) {
  1473. lbModule *m = gen->modules.entries[j].value;
  1474. if (lb_is_module_empty(m)) {
  1475. continue;
  1476. }
  1477. String filepath_ll = lb_filepath_ll_for_module(m);
  1478. if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) {
  1479. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1480. gb_exit(1);
  1481. return;
  1482. }
  1483. array_add(&gen->output_temp_paths, filepath_ll);
  1484. }
  1485. if (build_context.build_mode == BuildMode_LLVM_IR) {
  1486. gb_exit(0);
  1487. return;
  1488. }
  1489. }
  1490. TIME_SECTION("LLVM Add Foreign Library Paths");
  1491. for_array(j, gen->modules.entries) {
  1492. lbModule *m = gen->modules.entries[j].value;
  1493. for_array(i, m->info->required_foreign_imports_through_force) {
  1494. Entity *e = m->info->required_foreign_imports_through_force[i];
  1495. lb_add_foreign_library_path(m, e);
  1496. }
  1497. if (lb_is_module_empty(m)) {
  1498. continue;
  1499. }
  1500. }
  1501. TIME_SECTION("LLVM Object Generation");
  1502. isize non_empty_module_count = 0;
  1503. for_array(j, gen->modules.entries) {
  1504. lbModule *m = gen->modules.entries[j].value;
  1505. if (!lb_is_module_empty(m)) {
  1506. non_empty_module_count += 1;
  1507. }
  1508. }
  1509. if (do_threading && non_empty_module_count > 1) {
  1510. for_array(j, gen->modules.entries) {
  1511. lbModule *m = gen->modules.entries[j].value;
  1512. if (lb_is_module_empty(m)) {
  1513. continue;
  1514. }
  1515. String filepath_ll = lb_filepath_ll_for_module(m);
  1516. String filepath_obj = lb_filepath_obj_for_module(m);
  1517. array_add(&gen->output_object_paths, filepath_obj);
  1518. array_add(&gen->output_temp_paths, filepath_ll);
  1519. auto *wd = gb_alloc_item(permanent_allocator(), lbLLVMEmitWorker);
  1520. wd->target_machine = target_machines[j];
  1521. wd->code_gen_file_type = code_gen_file_type;
  1522. wd->filepath_obj = filepath_obj;
  1523. wd->m = m;
  1524. global_thread_pool_add_task(lb_llvm_emit_worker_proc, wd);
  1525. }
  1526. thread_pool_wait(&global_thread_pool);
  1527. } else {
  1528. for_array(j, gen->modules.entries) {
  1529. lbModule *m = gen->modules.entries[j].value;
  1530. if (lb_is_module_empty(m)) {
  1531. continue;
  1532. }
  1533. String filepath_obj = lb_filepath_obj_for_module(m);
  1534. array_add(&gen->output_object_paths, filepath_obj);
  1535. String short_name = remove_directory_from_path(filepath_obj);
  1536. gbString section_name = gb_string_make(heap_allocator(), "LLVM Generate Object: ");
  1537. section_name = gb_string_append_length(section_name, short_name.text, short_name.len);
  1538. TIME_SECTION_WITH_LEN(section_name, gb_string_length(section_name));
  1539. if (LLVMTargetMachineEmitToFile(target_machines[j], m->mod, cast(char *)filepath_obj.text, code_gen_file_type, &llvm_error)) {
  1540. gb_printf_err("LLVM Error: %s\n", llvm_error);
  1541. gb_exit(1);
  1542. return;
  1543. }
  1544. }
  1545. }
  1546. }