llvm_backend_type.cpp 59 KB

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