llvm_backend_type.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=true) {
  2. auto *set = &info->minimum_dependency_type_info_set;
  3. isize index = type_info_index(info, type, err_on_not_found);
  4. if (index >= 0) {
  5. auto *found = map_get(set, index);
  6. if (found) {
  7. GB_ASSERT(*found >= 0);
  8. return *found + 1;
  9. }
  10. }
  11. if (err_on_not_found) {
  12. GB_PANIC("NOT FOUND lb_type_info_index %s @ index %td", type_to_string(type), index);
  13. }
  14. return -1;
  15. }
  16. gb_internal u64 lb_typeid_kind(lbModule *m, Type *type, u64 id=0) {
  17. GB_ASSERT(!build_context.no_rtti);
  18. type = default_type(type);
  19. if (id == 0) {
  20. id = cast(u64)lb_type_info_index(m->info, type);
  21. }
  22. u64 kind = Typeid_Invalid;
  23. Type *bt = base_type(type);
  24. TypeKind tk = bt->kind;
  25. switch (tk) {
  26. case Type_Basic: {
  27. u32 flags = bt->Basic.flags;
  28. if (flags & BasicFlag_Boolean) kind = Typeid_Boolean;
  29. if (flags & BasicFlag_Integer) kind = Typeid_Integer;
  30. if (flags & BasicFlag_Unsigned) kind = Typeid_Integer;
  31. if (flags & BasicFlag_Float) kind = Typeid_Float;
  32. if (flags & BasicFlag_Complex) kind = Typeid_Complex;
  33. if (flags & BasicFlag_Pointer) kind = Typeid_Pointer;
  34. if (flags & BasicFlag_String) kind = Typeid_String;
  35. if (flags & BasicFlag_Rune) kind = Typeid_Rune;
  36. } break;
  37. case Type_Pointer: kind = Typeid_Pointer; break;
  38. case Type_MultiPointer: kind = Typeid_Multi_Pointer; break;
  39. case Type_Array: kind = Typeid_Array; break;
  40. case Type_Matrix: kind = Typeid_Matrix; break;
  41. case Type_EnumeratedArray: kind = Typeid_Enumerated_Array; break;
  42. case Type_Slice: kind = Typeid_Slice; break;
  43. case Type_DynamicArray: kind = Typeid_Dynamic_Array; break;
  44. case Type_Map: kind = Typeid_Map; break;
  45. case Type_Struct: kind = Typeid_Struct; break;
  46. case Type_Enum: kind = Typeid_Enum; break;
  47. case Type_Union: kind = Typeid_Union; break;
  48. case Type_Tuple: kind = Typeid_Tuple; break;
  49. case Type_Proc: kind = Typeid_Procedure; break;
  50. case Type_BitSet: kind = Typeid_Bit_Set; break;
  51. case Type_SimdVector: kind = Typeid_Simd_Vector; break;
  52. case Type_RelativePointer: kind = Typeid_Relative_Pointer; break;
  53. case Type_RelativeMultiPointer: kind = Typeid_Relative_Multi_Pointer; break;
  54. case Type_SoaPointer: kind = Typeid_SoaPointer; break;
  55. }
  56. return kind;
  57. }
  58. gb_internal lbValue lb_typeid(lbModule *m, Type *type) {
  59. GB_ASSERT(!build_context.no_rtti);
  60. type = default_type(type);
  61. u64 id = cast(u64)lb_type_info_index(m->info, type);
  62. GB_ASSERT(id >= 0);
  63. u64 kind = lb_typeid_kind(m, type, id);
  64. u64 named = is_type_named(type) && type->kind != Type_Basic;
  65. u64 special = 0;
  66. u64 reserved = 0;
  67. if (is_type_cstring(type)) {
  68. special = 1;
  69. } else if (is_type_integer(type) && !is_type_unsigned(type)) {
  70. special = 1;
  71. }
  72. u64 data = 0;
  73. if (build_context.ptr_size == 4) {
  74. GB_ASSERT(id <= (1u<<24u));
  75. data |= (id &~ (1u<<24)) << 0u; // index
  76. data |= (kind &~ (1u<<5)) << 24u; // kind
  77. data |= (named &~ (1u<<1)) << 29u; // named
  78. data |= (special &~ (1u<<1)) << 30u; // special
  79. data |= (reserved &~ (1u<<1)) << 31u; // reserved
  80. } else {
  81. GB_ASSERT(build_context.ptr_size == 8);
  82. GB_ASSERT(id <= (1ull<<56u));
  83. data |= (id &~ (1ull<<56)) << 0ul; // index
  84. data |= (kind &~ (1ull<<5)) << 56ull; // kind
  85. data |= (named &~ (1ull<<1)) << 61ull; // named
  86. data |= (special &~ (1ull<<1)) << 62ull; // special
  87. data |= (reserved &~ (1ull<<1)) << 63ull; // reserved
  88. }
  89. lbValue res = {};
  90. res.value = LLVMConstInt(lb_type(m, t_typeid), data, false);
  91. res.type = t_typeid;
  92. return res;
  93. }
  94. gb_internal lbValue lb_type_info(lbModule *m, Type *type) {
  95. GB_ASSERT(!build_context.no_rtti);
  96. type = default_type(type);
  97. isize index = lb_type_info_index(m->info, type);
  98. GB_ASSERT(index >= 0);
  99. lbValue data = lb_global_type_info_data_ptr(m);
  100. return lb_emit_array_epi(m, data, index);
  101. }
  102. gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) {
  103. return lb_type_internal_for_procedures_raw(m, type);
  104. }
  105. gb_internal lbValue lb_const_array_epi(lbModule *m, lbValue value, isize index) {
  106. GB_ASSERT(is_type_pointer(value.type));
  107. Type *type = type_deref(value.type);
  108. LLVMValueRef indices[2] = {
  109. LLVMConstInt(lb_type(m, t_int), 0, false),
  110. LLVMConstInt(lb_type(m, t_int), cast(unsigned long long)index, false),
  111. };
  112. LLVMTypeRef llvm_type = lb_type(m, type);
  113. lbValue res = {};
  114. Type *ptr = base_array_type(type);
  115. res.type = alloc_type_pointer(ptr);
  116. GB_ASSERT(LLVMIsConstant(value.value));
  117. res.value = LLVMConstGEP2(llvm_type, value.value, indices, gb_count_of(indices));
  118. return res;
  119. }
  120. gb_internal lbValue lb_type_info_member_types_offset(lbModule *m, isize count, isize *offset_=nullptr) {
  121. GB_ASSERT(m == &m->gen->default_module);
  122. if (offset_) *offset_ = lb_global_type_info_member_types_index;
  123. lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_types.addr, lb_global_type_info_member_types_index);
  124. lb_global_type_info_member_types_index += cast(i32)count;
  125. return offset;
  126. }
  127. gb_internal lbValue lb_type_info_member_names_offset(lbModule *m, isize count, isize *offset_=nullptr) {
  128. GB_ASSERT(m == &m->gen->default_module);
  129. if (offset_) *offset_ = lb_global_type_info_member_names_index;
  130. lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_names.addr, lb_global_type_info_member_names_index);
  131. lb_global_type_info_member_names_index += cast(i32)count;
  132. return offset;
  133. }
  134. gb_internal lbValue lb_type_info_member_offsets_offset(lbModule *m, isize count, isize *offset_=nullptr) {
  135. GB_ASSERT(m == &m->gen->default_module);
  136. if (offset_) *offset_ = lb_global_type_info_member_offsets_index;
  137. lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_offsets.addr, lb_global_type_info_member_offsets_index);
  138. lb_global_type_info_member_offsets_index += cast(i32)count;
  139. return offset;
  140. }
  141. gb_internal lbValue lb_type_info_member_usings_offset(lbModule *m, isize count, isize *offset_=nullptr) {
  142. GB_ASSERT(m == &m->gen->default_module);
  143. if (offset_) *offset_ = lb_global_type_info_member_usings_index;
  144. lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_usings.addr, lb_global_type_info_member_usings_index);
  145. lb_global_type_info_member_usings_index += cast(i32)count;
  146. return offset;
  147. }
  148. gb_internal lbValue lb_type_info_member_tags_offset(lbModule *m, isize count, isize *offset_=nullptr) {
  149. GB_ASSERT(m == &m->gen->default_module);
  150. if (offset_) *offset_ = lb_global_type_info_member_tags_index;
  151. lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_tags.addr, lb_global_type_info_member_tags_index);
  152. lb_global_type_info_member_tags_index += cast(i32)count;
  153. return offset;
  154. }
  155. enum {LB_USE_GIANT_PACKED_STRUCT = true};
  156. gb_internal LLVMTypeRef lb_setup_type_info_data_internal_type(lbModule *m, isize max_type_info_count) {
  157. if (!LB_USE_GIANT_PACKED_STRUCT) {
  158. Type *t = alloc_type_array(t_type_info, max_type_info_count);
  159. return lb_type(m, t);
  160. }
  161. CheckerInfo *info = m->gen->info;
  162. LLVMTypeRef *element_types = gb_alloc_array(heap_allocator(), LLVMTypeRef, max_type_info_count);
  163. defer (gb_free(heap_allocator(), element_types));
  164. auto entries_handled = slice_make<bool>(heap_allocator(), max_type_info_count);
  165. defer (gb_free(heap_allocator(), entries_handled.data));
  166. entries_handled[0] = true;
  167. element_types[0] = lb_type(m, t_type_info);
  168. Type *tibt = base_type(t_type_info);
  169. GB_ASSERT(tibt->kind == Type_Struct);
  170. Type *ut = base_type(tibt->Struct.fields[tibt->Struct.fields.count-1]->type);
  171. GB_ASSERT(ut->kind == Type_Union);
  172. GB_ASSERT(tibt->Struct.fields.count == 5);
  173. LLVMTypeRef stypes[6] = {};
  174. stypes[0] = lb_type(m, tibt->Struct.fields[0]->type);
  175. stypes[1] = lb_type(m, tibt->Struct.fields[1]->type);
  176. stypes[2] = lb_type(m, tibt->Struct.fields[2]->type);
  177. isize variant_index = 0;
  178. if (build_context.int_size == 8) {
  179. stypes[3] = lb_type(m, t_i32); // padding
  180. stypes[4] = lb_type(m, tibt->Struct.fields[3]->type);
  181. variant_index = 5;
  182. } else {
  183. stypes[3] = lb_type(m, tibt->Struct.fields[3]->type);
  184. variant_index = 4;
  185. }
  186. LLVMTypeRef modified_types[32] = {};
  187. GB_ASSERT(gb_count_of(modified_types) >= ut->Union.variants.count);
  188. modified_types[0] = element_types[0];
  189. i64 tag_offset = ut->Union.variant_block_size;
  190. LLVMTypeRef tag = lb_type(m, union_tag_type(ut));
  191. for_array(i, ut->Union.variants) {
  192. Type *t = ut->Union.variants[i];
  193. LLVMTypeRef padding = llvm_array_type(lb_type(m, t_u8), tag_offset-type_size_of(t));
  194. LLVMTypeRef vtypes[3] = {};
  195. vtypes[0] = lb_type(m, t);
  196. vtypes[1] = padding;
  197. vtypes[2] = tag;
  198. LLVMTypeRef variant_type = LLVMStructType(vtypes, gb_count_of(vtypes), true);
  199. stypes[variant_index] = variant_type;
  200. LLVMTypeRef modified_type = LLVMStructType(stypes, cast(unsigned)(variant_index+1), false);
  201. modified_types[i] = modified_type;
  202. }
  203. for_array(type_info_type_index, info->type_info_types) {
  204. Type *t = info->type_info_types[type_info_type_index];
  205. if (t == nullptr || t == t_invalid) {
  206. continue;
  207. }
  208. isize entry_index = lb_type_info_index(info, t, false);
  209. if (entry_index <= 0) {
  210. continue;
  211. }
  212. if (entries_handled[entry_index]) {
  213. continue;
  214. }
  215. entries_handled[entry_index] = true;
  216. if (t->kind == Type_Named) {
  217. element_types[entry_index] = modified_types[0];
  218. } else {
  219. i64 variant_index = lb_typeid_kind(m, t);
  220. element_types[entry_index] = modified_types[variant_index];
  221. }
  222. GB_ASSERT(element_types[entry_index] != nullptr);
  223. }
  224. for_array(i, entries_handled) {
  225. GB_ASSERT(entries_handled[i]);
  226. }
  227. return LLVMStructType(element_types, cast(unsigned)max_type_info_count, true);
  228. }
  229. gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 global_type_info_data_entity_count) { // NOTE(bill): Setup type_info data
  230. CheckerInfo *info = m->info;
  231. // Useful types
  232. Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags"));
  233. Type *t_type_info_flags = type_info_flags_entity->type;
  234. gb_unused(t_type_info_flags);
  235. Type *ut = base_type(t_type_info);
  236. GB_ASSERT(ut->kind == Type_Struct);
  237. ut = base_type(ut->Struct.fields[ut->Struct.fields.count-1]->type);
  238. GB_ASSERT(ut->kind == Type_Union);
  239. auto entries_handled = slice_make<bool>(heap_allocator(), cast(isize)global_type_info_data_entity_count);
  240. defer (gb_free(heap_allocator(), entries_handled.data));
  241. entries_handled[0] = true;
  242. LLVMValueRef giant_struct = lb_global_type_info_data_ptr(m).value;
  243. LLVMTypeRef giant_struct_type = LLVMGlobalGetValueType(giant_struct);
  244. GB_ASSERT(LLVMGetTypeKind(giant_struct_type) == LLVMStructTypeKind);
  245. LLVMValueRef *giant_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, global_type_info_data_entity_count);
  246. defer (gb_free(heap_allocator(), giant_const_values));
  247. giant_const_values[0] = LLVMConstNull(LLVMStructGetTypeAtIndex(giant_struct_type, 0));
  248. LLVMValueRef *small_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, 6);
  249. defer (gb_free(heap_allocator(), small_const_values));
  250. #define type_info_allocate_values(name) \
  251. LLVMValueRef *name##_values = gb_alloc_array(heap_allocator(), LLVMValueRef, type_deref(name.addr.type)->Array.count); \
  252. defer (gb_free(heap_allocator(), name##_values)); \
  253. defer ({ \
  254. Type *at = type_deref(name.addr.type); \
  255. LLVMTypeRef elem = lb_type(m, at->Array.elem); \
  256. for (i64 i = 0; i < at->Array.count; i++) { \
  257. if ((name##_values)[i] == nullptr) { \
  258. (name##_values)[i] = LLVMConstNull(elem); \
  259. } \
  260. } \
  261. LLVMSetInitializer(name.addr.value, llvm_const_array(elem, name##_values, at->Array.count)); \
  262. })
  263. type_info_allocate_values(lb_global_type_info_member_types);
  264. type_info_allocate_values(lb_global_type_info_member_names);
  265. type_info_allocate_values(lb_global_type_info_member_offsets);
  266. type_info_allocate_values(lb_global_type_info_member_usings);
  267. type_info_allocate_values(lb_global_type_info_member_tags);
  268. for_array(type_info_type_index, info->type_info_types) {
  269. Type *t = info->type_info_types[type_info_type_index];
  270. if (t == nullptr || t == t_invalid) {
  271. continue;
  272. }
  273. isize entry_index = lb_type_info_index(info, t, false);
  274. if (entry_index <= 0) {
  275. continue;
  276. }
  277. if (entries_handled[entry_index]) {
  278. continue;
  279. }
  280. entries_handled[entry_index] = true;
  281. LLVMTypeRef stype = LLVMStructGetTypeAtIndex(giant_struct_type, cast(unsigned)entry_index);
  282. i64 size = type_size_of(t);
  283. i64 align = type_align_of(t);
  284. u32 flags = type_info_flags_of_type(t);
  285. lbValue id = lb_typeid(m, t);
  286. GB_ASSERT_MSG(align != 0, "%lld %s", align, type_to_string(t));
  287. lbValue type_info_flags = lb_const_int(m, t_type_info_flags, flags);
  288. small_const_values[0] = LLVMConstInt(lb_type(m, t_int), size, true);
  289. small_const_values[1] = LLVMConstInt(lb_type(m, t_int), align, true);
  290. small_const_values[2] = type_info_flags.value;
  291. unsigned variant_index = 0;
  292. if (build_context.int_size == 8) {
  293. small_const_values[3] = LLVMConstNull(LLVMStructGetTypeAtIndex(stype, 3));
  294. small_const_values[4] = id.value;
  295. variant_index = 5;
  296. } else {
  297. small_const_values[3] = id.value;
  298. variant_index = 4;
  299. }
  300. LLVMTypeRef full_variant_type = LLVMStructGetTypeAtIndex(stype, variant_index);
  301. unsigned full_variant_elem_count = LLVMCountStructElementTypes(full_variant_type);
  302. if (full_variant_elem_count != 2) {
  303. GB_ASSERT_MSG(LLVMCountStructElementTypes(full_variant_type) == 3, "%lld %s", entry_index, type_to_string(t)); // blob, padding, tag
  304. }
  305. LLVMValueRef variant_value = nullptr;
  306. Type *tag_type = nullptr;
  307. switch (t->kind) {
  308. case Type_Named: {
  309. tag_type = t_type_info_named;
  310. LLVMValueRef pkg_name = nullptr;
  311. if (t->Named.type_name->pkg) {
  312. pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value;
  313. } else {
  314. pkg_name = LLVMConstNull(lb_type(m, t_string));
  315. }
  316. String proc_name = {};
  317. if (t->Named.type_name->parent_proc_decl) {
  318. DeclInfo *decl = t->Named.type_name->parent_proc_decl;
  319. if (decl->entity && decl->entity->kind == Entity_Procedure) {
  320. proc_name = decl->entity->token.string;
  321. }
  322. }
  323. TokenPos pos = t->Named.type_name->token.pos;
  324. lbValue loc = lb_const_source_code_location_const(m, proc_name, pos);
  325. LLVMValueRef vals[4] = {
  326. lb_const_string(m, t->Named.type_name->token.string).value,
  327. lb_type_info(m, t->Named.base).value,
  328. pkg_name,
  329. loc.value
  330. };
  331. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  332. break;
  333. }
  334. case Type_Basic:
  335. switch (t->Basic.kind) {
  336. case Basic_bool:
  337. case Basic_b8:
  338. case Basic_b16:
  339. case Basic_b32:
  340. case Basic_b64:
  341. tag_type = t_type_info_boolean;
  342. break;
  343. case Basic_i8:
  344. case Basic_u8:
  345. case Basic_i16:
  346. case Basic_u16:
  347. case Basic_i32:
  348. case Basic_u32:
  349. case Basic_i64:
  350. case Basic_u64:
  351. case Basic_i128:
  352. case Basic_u128:
  353. case Basic_i16le:
  354. case Basic_u16le:
  355. case Basic_i32le:
  356. case Basic_u32le:
  357. case Basic_i64le:
  358. case Basic_u64le:
  359. case Basic_i128le:
  360. case Basic_u128le:
  361. case Basic_i16be:
  362. case Basic_u16be:
  363. case Basic_i32be:
  364. case Basic_u32be:
  365. case Basic_i64be:
  366. case Basic_u64be:
  367. case Basic_i128be:
  368. case Basic_u128be:
  369. case Basic_int:
  370. case Basic_uint:
  371. case Basic_uintptr: {
  372. tag_type = t_type_info_integer;
  373. lbValue is_signed = lb_const_bool(m, t_bool, (t->Basic.flags & BasicFlag_Unsigned) == 0);
  374. // NOTE(bill): This is matches the runtime layout
  375. u8 endianness_value = 0;
  376. if (t->Basic.flags & BasicFlag_EndianLittle) {
  377. endianness_value = 1;
  378. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  379. endianness_value = 2;
  380. }
  381. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  382. LLVMValueRef vals[2] = {
  383. is_signed.value,
  384. endianness.value,
  385. };
  386. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  387. break;
  388. }
  389. case Basic_rune:
  390. tag_type = t_type_info_rune;
  391. break;
  392. case Basic_f16:
  393. case Basic_f32:
  394. case Basic_f64:
  395. case Basic_f16le:
  396. case Basic_f32le:
  397. case Basic_f64le:
  398. case Basic_f16be:
  399. case Basic_f32be:
  400. case Basic_f64be:
  401. {
  402. tag_type = t_type_info_float;
  403. // NOTE(bill): This is matches the runtime layout
  404. u8 endianness_value = 0;
  405. if (t->Basic.flags & BasicFlag_EndianLittle) {
  406. endianness_value = 1;
  407. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  408. endianness_value = 2;
  409. }
  410. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  411. LLVMValueRef vals[1] = {
  412. endianness.value,
  413. };
  414. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  415. }
  416. break;
  417. case Basic_complex32:
  418. case Basic_complex64:
  419. case Basic_complex128:
  420. tag_type = t_type_info_complex;
  421. break;
  422. case Basic_quaternion64:
  423. case Basic_quaternion128:
  424. case Basic_quaternion256:
  425. tag_type = t_type_info_quaternion;
  426. break;
  427. case Basic_rawptr:
  428. tag_type = t_type_info_pointer;
  429. break;
  430. case Basic_string:
  431. tag_type = t_type_info_string;
  432. break;
  433. case Basic_cstring:
  434. {
  435. tag_type = t_type_info_string;
  436. LLVMValueRef vals[1] = {
  437. lb_const_bool(m, t_bool, true).value,
  438. };
  439. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  440. }
  441. break;
  442. case Basic_any:
  443. tag_type = t_type_info_any;
  444. break;
  445. case Basic_typeid:
  446. tag_type = t_type_info_typeid;
  447. break;
  448. }
  449. break;
  450. case Type_Pointer: {
  451. tag_type = t_type_info_pointer;
  452. lbValue gep = lb_type_info(m, t->Pointer.elem);
  453. LLVMValueRef vals[1] = {
  454. gep.value,
  455. };
  456. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  457. break;
  458. }
  459. case Type_MultiPointer: {
  460. tag_type = t_type_info_multi_pointer;
  461. lbValue gep = lb_type_info(m, t->MultiPointer.elem);
  462. LLVMValueRef vals[1] = {
  463. gep.value,
  464. };
  465. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  466. break;
  467. }
  468. case Type_SoaPointer: {
  469. tag_type = t_type_info_soa_pointer;
  470. lbValue gep = lb_type_info(m, t->SoaPointer.elem);
  471. LLVMValueRef vals[1] = {
  472. gep.value,
  473. };
  474. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  475. break;
  476. }
  477. case Type_Array: {
  478. tag_type = t_type_info_array;
  479. i64 ez = type_size_of(t->Array.elem);
  480. LLVMValueRef vals[3] = {
  481. lb_type_info(m, t->Array.elem).value,
  482. lb_const_int(m, t_int, ez).value,
  483. lb_const_int(m, t_int, t->Array.count).value,
  484. };
  485. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  486. break;
  487. }
  488. case Type_EnumeratedArray: {
  489. tag_type = t_type_info_enumerated_array;
  490. LLVMValueRef vals[7] = {
  491. lb_type_info(m, t->EnumeratedArray.elem).value,
  492. lb_type_info(m, t->EnumeratedArray.index).value,
  493. lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value,
  494. lb_const_int(m, t_int, t->EnumeratedArray.count).value,
  495. // Unions
  496. lb_const_value(m, t_type_info_enum_value, *t->EnumeratedArray.min_value).value,
  497. lb_const_value(m, t_type_info_enum_value, *t->EnumeratedArray.max_value).value,
  498. lb_const_bool(m, t_bool, t->EnumeratedArray.is_sparse).value,
  499. };
  500. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  501. break;
  502. }
  503. case Type_DynamicArray: {
  504. tag_type = t_type_info_dynamic_array;
  505. LLVMValueRef vals[2] = {
  506. lb_type_info(m, t->DynamicArray.elem).value,
  507. lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value,
  508. };
  509. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  510. break;
  511. }
  512. case Type_Slice: {
  513. tag_type = t_type_info_slice;
  514. LLVMValueRef vals[2] = {
  515. lb_type_info(m, t->Slice.elem).value,
  516. lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value,
  517. };
  518. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  519. break;
  520. }
  521. case Type_Proc: {
  522. tag_type = t_type_info_procedure;
  523. LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr));
  524. LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr));
  525. if (t->Proc.params != nullptr) {
  526. params = lb_type_info(m, t->Proc.params).value;
  527. }
  528. if (t->Proc.results != nullptr) {
  529. results = lb_type_info(m, t->Proc.results).value;
  530. }
  531. LLVMValueRef vals[4] = {
  532. params,
  533. results,
  534. lb_const_bool(m, t_bool, t->Proc.variadic).value,
  535. lb_const_int(m, t_u8, t->Proc.calling_convention).value,
  536. };
  537. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  538. break;
  539. }
  540. case Type_Tuple: {
  541. tag_type = t_type_info_parameters;
  542. i64 type_offset = 0;
  543. i64 name_offset = 0;
  544. lbValue memory_types = lb_type_info_member_types_offset(m, t->Tuple.variables.count, &type_offset);
  545. lbValue memory_names = lb_type_info_member_names_offset(m, t->Tuple.variables.count, &name_offset);
  546. for_array(i, t->Tuple.variables) {
  547. // NOTE(bill): offset is not used for tuples
  548. Entity *f = t->Tuple.variables[i];
  549. lbValue index = lb_const_int(m, t_int, i);
  550. lbValue type_info = lb_const_ptr_offset(m, memory_types, index);
  551. lb_global_type_info_member_types_values[type_offset+i] = lb_type_info(m, f->type).value;
  552. if (f->token.string.len > 0) {
  553. lb_global_type_info_member_names_values[name_offset+i] = lb_const_string(m, f->token.string).value;
  554. }
  555. }
  556. lbValue count = lb_const_int(m, t_int, t->Tuple.variables.count);
  557. LLVMValueRef types_slice = llvm_const_slice(m, memory_types, count);
  558. LLVMValueRef names_slice = llvm_const_slice(m, memory_names, count);
  559. LLVMValueRef vals[2] = {
  560. types_slice,
  561. names_slice,
  562. };
  563. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  564. break;
  565. }
  566. case Type_Enum:
  567. tag_type = t_type_info_enum;
  568. {
  569. GB_ASSERT(t->Enum.base_type != nullptr);
  570. // GB_ASSERT_MSG(type_size_of(t_type_info_enum_value) == 16, "%lld == 16", cast(long long)type_size_of(t_type_info_enum_value));
  571. LLVMValueRef vals[3] = {};
  572. vals[0] = lb_type_info(m, t->Enum.base_type).value;
  573. if (t->Enum.fields.count > 0) {
  574. auto fields = t->Enum.fields;
  575. lbValue name_array = lb_generate_global_array(m, t_string, fields.count,
  576. str_lit("$enum_names"), cast(i64)entry_index);
  577. lbValue value_array = lb_generate_global_array(m, t_type_info_enum_value, fields.count,
  578. str_lit("$enum_values"), cast(i64)entry_index);
  579. LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  580. LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  581. GB_ASSERT(is_type_integer(t->Enum.base_type));
  582. for_array(i, fields) {
  583. name_values[i] = lb_const_string(m, fields[i]->token.string).value;
  584. value_values[i] = lb_const_value(m, t_i64, fields[i]->Constant.value).value;
  585. }
  586. LLVMValueRef name_init = llvm_const_array(lb_type(m, t_string), name_values, cast(unsigned)fields.count);
  587. LLVMValueRef value_init = llvm_const_array(lb_type(m, t_type_info_enum_value), value_values, cast(unsigned)fields.count);
  588. LLVMSetInitializer(name_array.value, name_init);
  589. LLVMSetInitializer(value_array.value, value_init);
  590. LLVMSetGlobalConstant(name_array.value, true);
  591. LLVMSetGlobalConstant(value_array.value, true);
  592. lbValue v_count = lb_const_int(m, t_int, fields.count);
  593. vals[1] = llvm_const_slice(m, lbValue{name_array.value, alloc_type_pointer(t_string)}, v_count);
  594. vals[2] = llvm_const_slice(m, lbValue{value_array.value, alloc_type_pointer(t_type_info_enum_value)}, v_count);
  595. } else {
  596. vals[1] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[1]->type));
  597. vals[2] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[2]->type));
  598. }
  599. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  600. }
  601. break;
  602. case Type_Union: {
  603. tag_type = t_type_info_union;
  604. {
  605. LLVMValueRef vals[7] = {};
  606. isize variant_count = gb_max(0, t->Union.variants.count);
  607. i64 variant_offset = 0;
  608. lbValue memory_types = lb_type_info_member_types_offset(m, variant_count, &variant_offset);
  609. for (isize variant_index = 0; variant_index < variant_count; variant_index++) {
  610. Type *vt = t->Union.variants[variant_index];
  611. lbValue tip = lb_type_info(m, vt);
  612. lb_global_type_info_member_types_values[variant_offset+variant_index] = lb_type_info(m, vt).value;
  613. }
  614. lbValue count = lb_const_int(m, t_int, variant_count);
  615. vals[0] = llvm_const_slice(m, memory_types, count);
  616. i64 tag_size = union_tag_size(t);
  617. if (tag_size > 0) {
  618. i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size);
  619. vals[1] = lb_const_int(m, t_uintptr, tag_offset).value;
  620. vals[2] = lb_type_info(m, union_tag_type(t)).value;
  621. } else {
  622. vals[1] = lb_const_int(m, t_uintptr, 0).value;
  623. vals[2] = LLVMConstNull(lb_type(m, t_type_info_ptr));
  624. }
  625. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  626. vals[3] = lb_equal_proc_for_type(m, t).value;
  627. }
  628. vals[4] = lb_const_bool(m, t_bool, t->Union.custom_align != 0).value;
  629. vals[5] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_no_nil).value;
  630. vals[6] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_shared_nil).value;
  631. for (isize i = 0; i < gb_count_of(vals); i++) {
  632. if (vals[i] == nullptr) {
  633. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag_type, i)));
  634. }
  635. }
  636. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  637. }
  638. break;
  639. }
  640. case Type_Struct: {
  641. tag_type = t_type_info_struct;
  642. LLVMValueRef vals[13] = {};
  643. {
  644. lbValue is_packed = lb_const_bool(m, t_bool, t->Struct.is_packed);
  645. lbValue is_raw_union = lb_const_bool(m, t_bool, t->Struct.is_raw_union);
  646. lbValue is_no_copy = lb_const_bool(m, t_bool, t->Struct.is_no_copy);
  647. lbValue is_custom_align = lb_const_bool(m, t_bool, t->Struct.custom_align != 0);
  648. vals[5] = is_packed.value;
  649. vals[6] = is_raw_union.value;
  650. vals[7] = is_no_copy.value;
  651. vals[8] = is_custom_align.value;
  652. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  653. vals[9] = lb_equal_proc_for_type(m, t).value;
  654. }
  655. if (t->Struct.soa_kind != StructSoa_None) {
  656. Type *kind_type = get_struct_field_type(tag_type, 10);
  657. lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind));
  658. lbValue soa_type = lb_type_info(m, t->Struct.soa_elem);
  659. lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count);
  660. vals[10] = soa_kind.value;
  661. vals[11] = soa_type.value;
  662. vals[12] = soa_len.value;
  663. }
  664. }
  665. isize count = t->Struct.fields.count;
  666. if (count > 0) {
  667. i64 types_offset = 0;
  668. i64 names_offset = 0;
  669. i64 offsets_offset = 0;
  670. i64 usings_offset = 0;
  671. i64 tags_offset = 0;
  672. lbValue memory_types = lb_type_info_member_types_offset (m, count, &types_offset);
  673. lbValue memory_names = lb_type_info_member_names_offset (m, count, &names_offset);
  674. lbValue memory_offsets = lb_type_info_member_offsets_offset(m, count, &offsets_offset);
  675. lbValue memory_usings = lb_type_info_member_usings_offset (m, count, &usings_offset);
  676. lbValue memory_tags = lb_type_info_member_tags_offset (m, count, &tags_offset);
  677. type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet
  678. for (isize source_index = 0; source_index < count; source_index++) {
  679. Entity *f = t->Struct.fields[source_index];
  680. lbValue tip = lb_type_info(m, f->type);
  681. i64 foffset = 0;
  682. if (!t->Struct.is_raw_union) {
  683. GB_ASSERT(t->Struct.offsets != nullptr);
  684. GB_ASSERT(0 <= f->Variable.field_index && f->Variable.field_index < count);
  685. foffset = t->Struct.offsets[source_index];
  686. }
  687. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  688. lb_global_type_info_member_types_values[types_offset+source_index] = lb_type_info(m, f->type).value;
  689. lb_global_type_info_member_offsets_values[offsets_offset+source_index] = lb_const_int(m, t_uintptr, foffset).value;
  690. lb_global_type_info_member_usings_values[usings_offset+source_index] = lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0).value;
  691. if (f->token.string.len > 0) {
  692. lb_global_type_info_member_names_values[names_offset+source_index] = lb_const_string(m, f->token.string).value;
  693. }
  694. if (t->Struct.tags != nullptr) {
  695. String tag_string = t->Struct.tags[source_index];
  696. if (tag_string.len > 0) {
  697. lb_global_type_info_member_tags_values[tags_offset+source_index] = lb_const_string(m, tag_string).value;
  698. }
  699. }
  700. }
  701. lbValue cv = lb_const_int(m, t_int, count);
  702. vals[0] = llvm_const_slice(m, memory_types, cv);
  703. vals[1] = llvm_const_slice(m, memory_names, cv);
  704. vals[2] = llvm_const_slice(m, memory_offsets, cv);
  705. vals[3] = llvm_const_slice(m, memory_usings, cv);
  706. vals[4] = llvm_const_slice(m, memory_tags, cv);
  707. }
  708. for (isize i = 0; i < gb_count_of(vals); i++) {
  709. if (vals[i] == nullptr) {
  710. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag_type, i)));
  711. }
  712. }
  713. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  714. break;
  715. }
  716. case Type_Map: {
  717. tag_type = t_type_info_map;
  718. init_map_internal_types(t);
  719. LLVMValueRef vals[3] = {
  720. lb_type_info(m, t->Map.key).value,
  721. lb_type_info(m, t->Map.value).value,
  722. lb_gen_map_info_ptr(m, t).value
  723. };
  724. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  725. break;
  726. }
  727. case Type_BitSet:
  728. {
  729. tag_type = t_type_info_bit_set;
  730. GB_ASSERT(is_type_typed(t->BitSet.elem));
  731. LLVMValueRef vals[4] = {
  732. lb_type_info(m, t->BitSet.elem).value,
  733. LLVMConstNull(lb_type(m, t_type_info_ptr)),
  734. lb_const_int(m, t_i64, t->BitSet.lower).value,
  735. lb_const_int(m, t_i64, t->BitSet.upper).value,
  736. };
  737. if (t->BitSet.underlying != nullptr) {
  738. vals[1] = lb_type_info(m, t->BitSet.underlying).value;
  739. }
  740. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  741. }
  742. break;
  743. case Type_SimdVector:
  744. {
  745. tag_type = t_type_info_simd_vector;
  746. LLVMValueRef vals[3] = {};
  747. vals[0] = lb_type_info(m, t->SimdVector.elem).value;
  748. vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value;
  749. vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value;
  750. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  751. }
  752. break;
  753. case Type_RelativePointer:
  754. {
  755. tag_type = t_type_info_relative_pointer;
  756. LLVMValueRef vals[2] = {
  757. lb_type_info(m, t->RelativePointer.pointer_type).value,
  758. lb_type_info(m, t->RelativePointer.base_integer).value,
  759. };
  760. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  761. }
  762. break;
  763. case Type_RelativeMultiPointer:
  764. {
  765. tag_type = t_type_info_relative_multi_pointer;
  766. LLVMValueRef vals[2] = {
  767. lb_type_info(m, t->RelativeMultiPointer.pointer_type).value,
  768. lb_type_info(m, t->RelativeMultiPointer.base_integer).value,
  769. };
  770. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  771. }
  772. break;
  773. case Type_Matrix:
  774. {
  775. tag_type = t_type_info_matrix;
  776. i64 ez = type_size_of(t->Matrix.elem);
  777. LLVMValueRef vals[5] = {
  778. lb_type_info(m, t->Matrix.elem).value,
  779. lb_const_int(m, t_int, ez).value,
  780. lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value,
  781. lb_const_int(m, t_int, t->Matrix.row_count).value,
  782. lb_const_int(m, t_int, t->Matrix.column_count).value,
  783. };
  784. variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
  785. }
  786. break;
  787. }
  788. i64 tag_index = 0;
  789. if (tag_type != nullptr) {
  790. tag_index = union_variant_index(ut, tag_type);
  791. }
  792. LLVMValueRef full_variant_values[3] = {};
  793. if (full_variant_elem_count == 2) {
  794. if (variant_value == nullptr) {
  795. full_variant_values[0] = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 0));
  796. full_variant_values[1] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 1), tag_index, false);
  797. } else {
  798. full_variant_values[0] = variant_value;
  799. full_variant_values[1] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 1), tag_index, false);
  800. }
  801. } else {
  802. if (variant_value == nullptr) {
  803. variant_value = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 0));
  804. } else {
  805. GB_ASSERT_MSG(LLVMStructGetTypeAtIndex(full_variant_type, 0) == LLVMTypeOf(variant_value),
  806. "\n%s -> %s\n%s vs %s\n",
  807. type_to_string(t), LLVMPrintValueToString(variant_value),
  808. LLVMPrintTypeToString(LLVMStructGetTypeAtIndex(full_variant_type, 0)), LLVMPrintTypeToString(LLVMTypeOf(variant_value))
  809. );
  810. }
  811. full_variant_values[0] = variant_value;
  812. full_variant_values[1] = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 1));
  813. full_variant_values[2] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 2), tag_index, false);
  814. }
  815. LLVMValueRef full_variant_value = LLVMConstNamedStruct(full_variant_type, full_variant_values, full_variant_elem_count);
  816. small_const_values[variant_index] = full_variant_value;
  817. giant_const_values[entry_index] = LLVMConstNamedStruct(stype, small_const_values, variant_index+1);
  818. }
  819. LLVMValueRef giant_const = LLVMConstNamedStruct(giant_struct_type, giant_const_values, cast(unsigned)global_type_info_data_entity_count);
  820. LLVMSetInitializer(giant_struct, giant_const);
  821. }
  822. gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data
  823. if (build_context.no_rtti) {
  824. return;
  825. }
  826. lbModule *m = p->module;
  827. CheckerInfo *info = m->info;
  828. i64 global_type_info_data_entity_count = 0;
  829. {
  830. // NOTE(bill): Set the type_table slice with the global backing array
  831. lbValue global_type_table = lb_find_runtime_value(m, str_lit("type_table"));
  832. Type *type = base_type(lb_global_type_info_data_entity->type);
  833. GB_ASSERT(type->kind == Type_Array);
  834. global_type_info_data_entity_count = type->Array.count;
  835. LLVMValueRef data = lb_global_type_info_data_ptr(m).value;
  836. data = LLVMConstPointerCast(data, lb_type(m, alloc_type_pointer(type->Array.elem)));
  837. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), type->Array.count, true);
  838. Type *t = type_deref(global_type_table.type);
  839. GB_ASSERT(is_type_slice(t));
  840. LLVMValueRef slice = llvm_const_slice_internal(m, data, len);
  841. LLVMSetInitializer(global_type_table.value, slice);
  842. }
  843. if (LB_USE_GIANT_PACKED_STRUCT) {
  844. lb_setup_type_info_data_giant_packed_struct(m, global_type_info_data_entity_count);
  845. return;
  846. }
  847. // Useful types
  848. Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags"));
  849. Type *t_type_info_flags = type_info_flags_entity->type;
  850. auto entries_handled = slice_make<bool>(heap_allocator(), cast(isize)global_type_info_data_entity_count);
  851. defer (gb_free(heap_allocator(), entries_handled.data));
  852. entries_handled[0] = true;
  853. for_array(type_info_type_index, info->type_info_types) {
  854. Type *t = info->type_info_types[type_info_type_index];
  855. if (t == nullptr || t == t_invalid) {
  856. continue;
  857. }
  858. isize entry_index = lb_type_info_index(info, t, false);
  859. if (entry_index <= 0) {
  860. continue;
  861. }
  862. if (entries_handled[entry_index]) {
  863. continue;
  864. }
  865. entries_handled[entry_index] = true;
  866. lbValue global_data_ptr = lb_global_type_info_data_ptr(m);
  867. lbValue tag = {};
  868. lbValue ti_ptr = lb_emit_array_epi(p, global_data_ptr, cast(i32)entry_index);
  869. i64 size = type_size_of(t);
  870. i64 align = type_align_of(t);
  871. u32 flags = type_info_flags_of_type(t);
  872. lbValue id = lb_typeid(m, t);
  873. GB_ASSERT_MSG(align != 0, "%lld %s", align, type_to_string(t));
  874. lbValue type_info_flags = lb_const_int(p->module, t_type_info_flags, flags);
  875. lbValue size_ptr = lb_emit_struct_ep(p, ti_ptr, 0);
  876. lbValue align_ptr = lb_emit_struct_ep(p, ti_ptr, 1);
  877. lbValue flags_ptr = lb_emit_struct_ep(p, ti_ptr, 2);
  878. lbValue id_ptr = lb_emit_struct_ep(p, ti_ptr, 3);
  879. lb_emit_store(p, size_ptr, lb_const_int(m, t_int, size));
  880. lb_emit_store(p, align_ptr, lb_const_int(m, t_int, align));
  881. lb_emit_store(p, flags_ptr, type_info_flags);
  882. lb_emit_store(p, id_ptr, id);
  883. lbValue variant_ptr = lb_emit_struct_ep(p, ti_ptr, 4);
  884. switch (t->kind) {
  885. case Type_Named: {
  886. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_named_ptr);
  887. LLVMValueRef pkg_name = nullptr;
  888. if (t->Named.type_name->pkg) {
  889. pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value;
  890. } else {
  891. pkg_name = LLVMConstNull(lb_type(m, t_string));
  892. }
  893. String proc_name = {};
  894. if (t->Named.type_name->parent_proc_decl) {
  895. DeclInfo *decl = t->Named.type_name->parent_proc_decl;
  896. if (decl->entity && decl->entity->kind == Entity_Procedure) {
  897. proc_name = decl->entity->token.string;
  898. }
  899. }
  900. TokenPos pos = t->Named.type_name->token.pos;
  901. lbValue loc = lb_emit_source_code_location_const(p, proc_name, pos);
  902. LLVMValueRef vals[4] = {
  903. lb_const_string(p->module, t->Named.type_name->token.string).value,
  904. lb_type_info(m, t->Named.base).value,
  905. pkg_name,
  906. loc.value
  907. };
  908. lbValue res = {};
  909. res.type = type_deref(tag.type);
  910. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  911. lb_emit_store(p, tag, res);
  912. break;
  913. }
  914. case Type_Basic:
  915. switch (t->Basic.kind) {
  916. case Basic_bool:
  917. case Basic_b8:
  918. case Basic_b16:
  919. case Basic_b32:
  920. case Basic_b64:
  921. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_boolean_ptr);
  922. break;
  923. case Basic_i8:
  924. case Basic_u8:
  925. case Basic_i16:
  926. case Basic_u16:
  927. case Basic_i32:
  928. case Basic_u32:
  929. case Basic_i64:
  930. case Basic_u64:
  931. case Basic_i128:
  932. case Basic_u128:
  933. case Basic_i16le:
  934. case Basic_u16le:
  935. case Basic_i32le:
  936. case Basic_u32le:
  937. case Basic_i64le:
  938. case Basic_u64le:
  939. case Basic_i128le:
  940. case Basic_u128le:
  941. case Basic_i16be:
  942. case Basic_u16be:
  943. case Basic_i32be:
  944. case Basic_u32be:
  945. case Basic_i64be:
  946. case Basic_u64be:
  947. case Basic_i128be:
  948. case Basic_u128be:
  949. case Basic_int:
  950. case Basic_uint:
  951. case Basic_uintptr: {
  952. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_integer_ptr);
  953. lbValue is_signed = lb_const_bool(m, t_bool, (t->Basic.flags & BasicFlag_Unsigned) == 0);
  954. // NOTE(bill): This is matches the runtime layout
  955. u8 endianness_value = 0;
  956. if (t->Basic.flags & BasicFlag_EndianLittle) {
  957. endianness_value = 1;
  958. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  959. endianness_value = 2;
  960. }
  961. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  962. LLVMValueRef vals[2] = {
  963. is_signed.value,
  964. endianness.value,
  965. };
  966. lbValue res = {};
  967. res.type = type_deref(tag.type);
  968. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  969. lb_emit_store(p, tag, res);
  970. break;
  971. }
  972. case Basic_rune:
  973. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_rune_ptr);
  974. break;
  975. case Basic_f16:
  976. case Basic_f32:
  977. case Basic_f64:
  978. case Basic_f16le:
  979. case Basic_f32le:
  980. case Basic_f64le:
  981. case Basic_f16be:
  982. case Basic_f32be:
  983. case Basic_f64be:
  984. {
  985. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_float_ptr);
  986. // NOTE(bill): This is matches the runtime layout
  987. u8 endianness_value = 0;
  988. if (t->Basic.flags & BasicFlag_EndianLittle) {
  989. endianness_value = 1;
  990. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  991. endianness_value = 2;
  992. }
  993. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  994. LLVMValueRef vals[1] = {
  995. endianness.value,
  996. };
  997. lbValue res = {};
  998. res.type = type_deref(tag.type);
  999. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1000. lb_emit_store(p, tag, res);
  1001. }
  1002. break;
  1003. case Basic_complex32:
  1004. case Basic_complex64:
  1005. case Basic_complex128:
  1006. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_complex_ptr);
  1007. break;
  1008. case Basic_quaternion64:
  1009. case Basic_quaternion128:
  1010. case Basic_quaternion256:
  1011. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_quaternion_ptr);
  1012. break;
  1013. case Basic_rawptr:
  1014. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr);
  1015. break;
  1016. case Basic_string:
  1017. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr);
  1018. break;
  1019. case Basic_cstring:
  1020. {
  1021. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr);
  1022. LLVMValueRef vals[1] = {
  1023. lb_const_bool(m, t_bool, true).value,
  1024. };
  1025. lbValue res = {};
  1026. res.type = type_deref(tag.type);
  1027. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1028. lb_emit_store(p, tag, res);
  1029. }
  1030. break;
  1031. case Basic_any:
  1032. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_any_ptr);
  1033. break;
  1034. case Basic_typeid:
  1035. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_typeid_ptr);
  1036. break;
  1037. }
  1038. break;
  1039. case Type_Pointer: {
  1040. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr);
  1041. lbValue gep = lb_type_info(m, t->Pointer.elem);
  1042. LLVMValueRef vals[1] = {
  1043. gep.value,
  1044. };
  1045. lbValue res = {};
  1046. res.type = type_deref(tag.type);
  1047. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1048. lb_emit_store(p, tag, res);
  1049. break;
  1050. }
  1051. case Type_MultiPointer: {
  1052. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_multi_pointer_ptr);
  1053. lbValue gep = lb_type_info(m, t->MultiPointer.elem);
  1054. LLVMValueRef vals[1] = {
  1055. gep.value,
  1056. };
  1057. lbValue res = {};
  1058. res.type = type_deref(tag.type);
  1059. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1060. lb_emit_store(p, tag, res);
  1061. break;
  1062. }
  1063. case Type_SoaPointer: {
  1064. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_soa_pointer_ptr);
  1065. lbValue gep = lb_type_info(m, t->SoaPointer.elem);
  1066. LLVMValueRef vals[1] = {
  1067. gep.value,
  1068. };
  1069. lbValue res = {};
  1070. res.type = type_deref(tag.type);
  1071. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1072. lb_emit_store(p, tag, res);
  1073. break;
  1074. }
  1075. case Type_Array: {
  1076. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_array_ptr);
  1077. i64 ez = type_size_of(t->Array.elem);
  1078. LLVMValueRef vals[3] = {
  1079. lb_type_info(m, t->Array.elem).value,
  1080. lb_const_int(m, t_int, ez).value,
  1081. lb_const_int(m, t_int, t->Array.count).value,
  1082. };
  1083. lbValue res = {};
  1084. res.type = type_deref(tag.type);
  1085. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1086. lb_emit_store(p, tag, res);
  1087. break;
  1088. }
  1089. case Type_EnumeratedArray: {
  1090. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enumerated_array_ptr);
  1091. LLVMValueRef vals[7] = {
  1092. lb_type_info(m, t->EnumeratedArray.elem).value,
  1093. lb_type_info(m, t->EnumeratedArray.index).value,
  1094. lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value,
  1095. lb_const_int(m, t_int, t->EnumeratedArray.count).value,
  1096. // Unions
  1097. LLVMConstNull(lb_type(m, t_type_info_enum_value)),
  1098. LLVMConstNull(lb_type(m, t_type_info_enum_value)),
  1099. lb_const_bool(m, t_bool, t->EnumeratedArray.is_sparse).value,
  1100. };
  1101. lbValue res = {};
  1102. res.type = type_deref(tag.type);
  1103. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1104. lb_emit_store(p, tag, res);
  1105. // NOTE(bill): Union assignment
  1106. lbValue min_value = lb_emit_struct_ep(p, tag, 4);
  1107. lbValue max_value = lb_emit_struct_ep(p, tag, 5);
  1108. lbValue min_v = lb_const_value(m, t_i64, *t->EnumeratedArray.min_value);
  1109. lbValue max_v = lb_const_value(m, t_i64, *t->EnumeratedArray.max_value);
  1110. lb_emit_store(p, min_value, min_v);
  1111. lb_emit_store(p, max_value, max_v);
  1112. break;
  1113. }
  1114. case Type_DynamicArray: {
  1115. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_dynamic_array_ptr);
  1116. LLVMValueRef vals[2] = {
  1117. lb_type_info(m, t->DynamicArray.elem).value,
  1118. lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value,
  1119. };
  1120. lbValue res = {};
  1121. res.type = type_deref(tag.type);
  1122. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1123. lb_emit_store(p, tag, res);
  1124. break;
  1125. }
  1126. case Type_Slice: {
  1127. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_slice_ptr);
  1128. LLVMValueRef vals[2] = {
  1129. lb_type_info(m, t->Slice.elem).value,
  1130. lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value,
  1131. };
  1132. lbValue res = {};
  1133. res.type = type_deref(tag.type);
  1134. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1135. lb_emit_store(p, tag, res);
  1136. break;
  1137. }
  1138. case Type_Proc: {
  1139. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_procedure_ptr);
  1140. LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr));
  1141. LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr));
  1142. if (t->Proc.params != nullptr) {
  1143. params = lb_type_info(m, t->Proc.params).value;
  1144. }
  1145. if (t->Proc.results != nullptr) {
  1146. results = lb_type_info(m, t->Proc.results).value;
  1147. }
  1148. LLVMValueRef vals[4] = {
  1149. params,
  1150. results,
  1151. lb_const_bool(m, t_bool, t->Proc.variadic).value,
  1152. lb_const_int(m, t_u8, t->Proc.calling_convention).value,
  1153. };
  1154. lbValue res = {};
  1155. res.type = type_deref(tag.type);
  1156. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1157. lb_emit_store(p, tag, res);
  1158. break;
  1159. }
  1160. case Type_Tuple: {
  1161. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_parameters_ptr);
  1162. lbValue memory_types = lb_type_info_member_types_offset(m, t->Tuple.variables.count);
  1163. lbValue memory_names = lb_type_info_member_names_offset(m, t->Tuple.variables.count);
  1164. for_array(i, t->Tuple.variables) {
  1165. // NOTE(bill): offset is not used for tuples
  1166. Entity *f = t->Tuple.variables[i];
  1167. lbValue index = lb_const_int(m, t_int, i);
  1168. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  1169. // TODO(bill): Make this constant if possible, 'lb_const_store' does not work
  1170. lb_emit_store(p, type_info, lb_type_info(m, f->type));
  1171. if (f->token.string.len > 0) {
  1172. lbValue name = lb_emit_ptr_offset(p, memory_names, index);
  1173. lb_emit_store(p, name, lb_const_string(m, f->token.string));
  1174. }
  1175. }
  1176. lbValue count = lb_const_int(m, t_int, t->Tuple.variables.count);
  1177. LLVMValueRef types_slice = llvm_const_slice(m, memory_types, count);
  1178. LLVMValueRef names_slice = llvm_const_slice(m, memory_names, count);
  1179. LLVMValueRef vals[2] = {
  1180. types_slice,
  1181. names_slice,
  1182. };
  1183. lbValue res = {};
  1184. res.type = type_deref(tag.type);
  1185. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1186. lb_emit_store(p, tag, res);
  1187. break;
  1188. }
  1189. case Type_Enum:
  1190. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enum_ptr);
  1191. {
  1192. GB_ASSERT(t->Enum.base_type != nullptr);
  1193. // GB_ASSERT_MSG(type_size_of(t_type_info_enum_value) == 16, "%lld == 16", cast(long long)type_size_of(t_type_info_enum_value));
  1194. LLVMValueRef vals[3] = {};
  1195. vals[0] = lb_type_info(m, t->Enum.base_type).value;
  1196. if (t->Enum.fields.count > 0) {
  1197. auto fields = t->Enum.fields;
  1198. lbValue name_array = lb_generate_global_array(m, t_string, fields.count,
  1199. str_lit("$enum_names"), cast(i64)entry_index);
  1200. lbValue value_array = lb_generate_global_array(m, t_type_info_enum_value, fields.count,
  1201. str_lit("$enum_values"), cast(i64)entry_index);
  1202. LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  1203. LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  1204. GB_ASSERT(is_type_integer(t->Enum.base_type));
  1205. for_array(i, fields) {
  1206. name_values[i] = lb_const_string(m, fields[i]->token.string).value;
  1207. value_values[i] = lb_const_value(m, t_i64, fields[i]->Constant.value).value;
  1208. }
  1209. LLVMValueRef name_init = llvm_const_array(lb_type(m, t_string), name_values, cast(unsigned)fields.count);
  1210. LLVMValueRef value_init = llvm_const_array(lb_type(m, t_type_info_enum_value), value_values, cast(unsigned)fields.count);
  1211. LLVMSetInitializer(name_array.value, name_init);
  1212. LLVMSetInitializer(value_array.value, value_init);
  1213. LLVMSetGlobalConstant(name_array.value, true);
  1214. LLVMSetGlobalConstant(value_array.value, true);
  1215. lbValue v_count = lb_const_int(m, t_int, fields.count);
  1216. vals[1] = llvm_const_slice(m, lb_array_elem(p, name_array), v_count);
  1217. vals[2] = llvm_const_slice(m, lb_array_elem(p, value_array), v_count);
  1218. } else {
  1219. vals[1] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[1]->type));
  1220. vals[2] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[2]->type));
  1221. }
  1222. lbValue res = {};
  1223. res.type = type_deref(tag.type);
  1224. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1225. lb_emit_store(p, tag, res);
  1226. }
  1227. break;
  1228. case Type_Union: {
  1229. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_union_ptr);
  1230. {
  1231. LLVMValueRef vals[7] = {};
  1232. isize variant_count = gb_max(0, t->Union.variants.count);
  1233. lbValue memory_types = lb_type_info_member_types_offset(m, variant_count);
  1234. // NOTE(bill): Zeroth is nil so ignore it
  1235. for (isize variant_index = 0; variant_index < variant_count; variant_index++) {
  1236. Type *vt = t->Union.variants[variant_index];
  1237. lbValue tip = lb_type_info(m, vt);
  1238. lbValue index = lb_const_int(m, t_int, variant_index);
  1239. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  1240. lb_emit_store(p, type_info, lb_type_info(m, vt));
  1241. }
  1242. lbValue count = lb_const_int(m, t_int, variant_count);
  1243. vals[0] = llvm_const_slice(m, memory_types, count);
  1244. i64 tag_size = union_tag_size(t);
  1245. if (tag_size > 0) {
  1246. i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size);
  1247. vals[1] = lb_const_int(m, t_uintptr, tag_offset).value;
  1248. vals[2] = lb_type_info(m, union_tag_type(t)).value;
  1249. } else {
  1250. vals[1] = lb_const_int(m, t_uintptr, 0).value;
  1251. vals[2] = LLVMConstNull(lb_type(m, t_type_info_ptr));
  1252. }
  1253. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  1254. vals[3] = lb_equal_proc_for_type(m, t).value;
  1255. }
  1256. vals[4] = lb_const_bool(m, t_bool, t->Union.custom_align != 0).value;
  1257. vals[5] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_no_nil).value;
  1258. vals[6] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_shared_nil).value;
  1259. for (isize i = 0; i < gb_count_of(vals); i++) {
  1260. if (vals[i] == nullptr) {
  1261. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i)));
  1262. }
  1263. }
  1264. lbValue res = {};
  1265. res.type = type_deref(tag.type);
  1266. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1267. lb_emit_store(p, tag, res);
  1268. }
  1269. break;
  1270. }
  1271. case Type_Struct: {
  1272. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_struct_ptr);
  1273. LLVMValueRef vals[13] = {};
  1274. {
  1275. lbValue is_packed = lb_const_bool(m, t_bool, t->Struct.is_packed);
  1276. lbValue is_raw_union = lb_const_bool(m, t_bool, t->Struct.is_raw_union);
  1277. lbValue is_no_copy = lb_const_bool(m, t_bool, t->Struct.is_no_copy);
  1278. lbValue is_custom_align = lb_const_bool(m, t_bool, t->Struct.custom_align != 0);
  1279. vals[5] = is_packed.value;
  1280. vals[6] = is_raw_union.value;
  1281. vals[7] = is_no_copy.value;
  1282. vals[8] = is_custom_align.value;
  1283. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  1284. vals[9] = lb_equal_proc_for_type(m, t).value;
  1285. }
  1286. if (t->Struct.soa_kind != StructSoa_None) {
  1287. lbValue kind = lb_emit_struct_ep(p, tag, 10);
  1288. Type *kind_type = type_deref(kind.type);
  1289. lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind));
  1290. lbValue soa_type = lb_type_info(m, t->Struct.soa_elem);
  1291. lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count);
  1292. vals[10] = soa_kind.value;
  1293. vals[11] = soa_type.value;
  1294. vals[12] = soa_len.value;
  1295. }
  1296. }
  1297. isize count = t->Struct.fields.count;
  1298. if (count > 0) {
  1299. lbValue memory_types = lb_type_info_member_types_offset (m, count);
  1300. lbValue memory_names = lb_type_info_member_names_offset (m, count);
  1301. lbValue memory_offsets = lb_type_info_member_offsets_offset(m, count);
  1302. lbValue memory_usings = lb_type_info_member_usings_offset (m, count);
  1303. lbValue memory_tags = lb_type_info_member_tags_offset (m, count);
  1304. type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet
  1305. for (isize source_index = 0; source_index < count; source_index++) {
  1306. Entity *f = t->Struct.fields[source_index];
  1307. lbValue tip = lb_type_info(m, f->type);
  1308. i64 foffset = 0;
  1309. if (!t->Struct.is_raw_union) {
  1310. GB_ASSERT(t->Struct.offsets != nullptr);
  1311. GB_ASSERT(0 <= f->Variable.field_index && f->Variable.field_index < count);
  1312. foffset = t->Struct.offsets[source_index];
  1313. }
  1314. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  1315. lbValue index = lb_const_int(m, t_int, source_index);
  1316. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  1317. lbValue offset = lb_emit_ptr_offset(p, memory_offsets, index);
  1318. lbValue is_using = lb_emit_ptr_offset(p, memory_usings, index);
  1319. lb_emit_store(p, type_info, lb_type_info(m, f->type));
  1320. if (f->token.string.len > 0) {
  1321. lbValue name = lb_emit_ptr_offset(p, memory_names, index);
  1322. lb_emit_store(p, name, lb_const_string(m, f->token.string));
  1323. }
  1324. lb_emit_store(p, offset, lb_const_int(m, t_uintptr, foffset));
  1325. lb_emit_store(p, is_using, lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0));
  1326. if (t->Struct.tags != nullptr) {
  1327. String tag_string = t->Struct.tags[source_index];
  1328. if (tag_string.len > 0) {
  1329. lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index);
  1330. lb_emit_store(p, tag_ptr, lb_const_string(m, tag_string));
  1331. }
  1332. }
  1333. }
  1334. lbValue cv = lb_const_int(m, t_int, count);
  1335. vals[0] = llvm_const_slice(m, memory_types, cv);
  1336. vals[1] = llvm_const_slice(m, memory_names, cv);
  1337. vals[2] = llvm_const_slice(m, memory_offsets, cv);
  1338. vals[3] = llvm_const_slice(m, memory_usings, cv);
  1339. vals[4] = llvm_const_slice(m, memory_tags, cv);
  1340. }
  1341. for (isize i = 0; i < gb_count_of(vals); i++) {
  1342. if (vals[i] == nullptr) {
  1343. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i)));
  1344. }
  1345. }
  1346. lbValue res = {};
  1347. res.type = type_deref(tag.type);
  1348. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1349. lb_emit_store(p, tag, res);
  1350. break;
  1351. }
  1352. case Type_Map: {
  1353. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr);
  1354. init_map_internal_types(t);
  1355. LLVMValueRef vals[3] = {
  1356. lb_type_info(m, t->Map.key).value,
  1357. lb_type_info(m, t->Map.value).value,
  1358. lb_gen_map_info_ptr(p->module, t).value
  1359. };
  1360. lbValue res = {};
  1361. res.type = type_deref(tag.type);
  1362. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1363. lb_emit_store(p, tag, res);
  1364. break;
  1365. }
  1366. case Type_BitSet:
  1367. {
  1368. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_bit_set_ptr);
  1369. GB_ASSERT(is_type_typed(t->BitSet.elem));
  1370. LLVMValueRef vals[4] = {
  1371. lb_type_info(m, t->BitSet.elem).value,
  1372. LLVMConstNull(lb_type(m, t_type_info_ptr)),
  1373. lb_const_int(m, t_i64, t->BitSet.lower).value,
  1374. lb_const_int(m, t_i64, t->BitSet.upper).value,
  1375. };
  1376. if (t->BitSet.underlying != nullptr) {
  1377. vals[1] =lb_type_info(m, t->BitSet.underlying).value;
  1378. }
  1379. lbValue res = {};
  1380. res.type = type_deref(tag.type);
  1381. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1382. lb_emit_store(p, tag, res);
  1383. }
  1384. break;
  1385. case Type_SimdVector:
  1386. {
  1387. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_simd_vector_ptr);
  1388. LLVMValueRef vals[3] = {};
  1389. vals[0] = lb_type_info(m, t->SimdVector.elem).value;
  1390. vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value;
  1391. vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value;
  1392. lbValue res = {};
  1393. res.type = type_deref(tag.type);
  1394. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1395. lb_emit_store(p, tag, res);
  1396. }
  1397. break;
  1398. case Type_RelativePointer:
  1399. {
  1400. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_pointer_ptr);
  1401. LLVMValueRef vals[2] = {
  1402. lb_type_info(m, t->RelativePointer.pointer_type).value,
  1403. lb_type_info(m, t->RelativePointer.base_integer).value,
  1404. };
  1405. lbValue res = {};
  1406. res.type = type_deref(tag.type);
  1407. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1408. lb_emit_store(p, tag, res);
  1409. }
  1410. break;
  1411. case Type_RelativeMultiPointer:
  1412. {
  1413. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_multi_pointer_ptr);
  1414. LLVMValueRef vals[2] = {
  1415. lb_type_info(m, t->RelativeMultiPointer.pointer_type).value,
  1416. lb_type_info(m, t->RelativeMultiPointer.base_integer).value,
  1417. };
  1418. lbValue res = {};
  1419. res.type = type_deref(tag.type);
  1420. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1421. lb_emit_store(p, tag, res);
  1422. }
  1423. break;
  1424. case Type_Matrix:
  1425. {
  1426. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_matrix_ptr);
  1427. i64 ez = type_size_of(t->Matrix.elem);
  1428. LLVMValueRef vals[5] = {
  1429. lb_type_info(m, t->Matrix.elem).value,
  1430. lb_const_int(m, t_int, ez).value,
  1431. lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value,
  1432. lb_const_int(m, t_int, t->Matrix.row_count).value,
  1433. lb_const_int(m, t_int, t->Matrix.column_count).value,
  1434. };
  1435. lbValue res = {};
  1436. res.type = type_deref(tag.type);
  1437. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  1438. lb_emit_store(p, tag, res);
  1439. }
  1440. break;
  1441. }
  1442. if (tag.value != nullptr) {
  1443. Type *tag_type = type_deref(tag.type);
  1444. GB_ASSERT(is_type_named(tag_type));
  1445. // lb_emit_store_union_variant(p, variant_ptr, lb_emit_load(p, tag), tag_type);
  1446. lb_emit_store_union_variant_tag(p, variant_ptr, tag_type);
  1447. } else {
  1448. if (t != t_llvm_bool) {
  1449. GB_PANIC("Unhandled Type_Info variant: %s", type_to_string(t));
  1450. }
  1451. }
  1452. }
  1453. for_array(i, entries_handled) {
  1454. if (!entries_handled[i]) {
  1455. GB_PANIC("UNHANDLED ENTRY %td (%td)", i, entries_handled.count);
  1456. }
  1457. }
  1458. }