llvm_backend_type.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 lbValue lb_typeid(lbModule *m, Type *type) {
  17. GB_ASSERT(!build_context.no_rtti);
  18. type = default_type(type);
  19. u64 id = cast(u64)lb_type_info_index(m->info, type);
  20. GB_ASSERT(id >= 0);
  21. u64 kind = Typeid_Invalid;
  22. u64 named = is_type_named(type) && type->kind != Type_Basic;
  23. u64 special = 0;
  24. u64 reserved = 0;
  25. Type *bt = base_type(type);
  26. TypeKind tk = bt->kind;
  27. switch (tk) {
  28. case Type_Basic: {
  29. u32 flags = bt->Basic.flags;
  30. if (flags & BasicFlag_Boolean) kind = Typeid_Boolean;
  31. if (flags & BasicFlag_Integer) kind = Typeid_Integer;
  32. if (flags & BasicFlag_Unsigned) kind = Typeid_Integer;
  33. if (flags & BasicFlag_Float) kind = Typeid_Float;
  34. if (flags & BasicFlag_Complex) kind = Typeid_Complex;
  35. if (flags & BasicFlag_Pointer) kind = Typeid_Pointer;
  36. if (flags & BasicFlag_String) kind = Typeid_String;
  37. if (flags & BasicFlag_Rune) kind = Typeid_Rune;
  38. } break;
  39. case Type_Pointer: kind = Typeid_Pointer; break;
  40. case Type_MultiPointer: kind = Typeid_Multi_Pointer; break;
  41. case Type_Array: kind = Typeid_Array; break;
  42. case Type_Matrix: kind = Typeid_Matrix; break;
  43. case Type_EnumeratedArray: kind = Typeid_Enumerated_Array; break;
  44. case Type_Slice: kind = Typeid_Slice; break;
  45. case Type_DynamicArray: kind = Typeid_Dynamic_Array; break;
  46. case Type_Map: kind = Typeid_Map; break;
  47. case Type_Struct: kind = Typeid_Struct; break;
  48. case Type_Enum: kind = Typeid_Enum; break;
  49. case Type_Union: kind = Typeid_Union; break;
  50. case Type_Tuple: kind = Typeid_Tuple; break;
  51. case Type_Proc: kind = Typeid_Procedure; break;
  52. case Type_BitSet: kind = Typeid_Bit_Set; break;
  53. case Type_SimdVector: kind = Typeid_Simd_Vector; break;
  54. case Type_RelativePointer: kind = Typeid_Relative_Pointer; break;
  55. case Type_RelativeMultiPointer: kind = Typeid_Relative_Multi_Pointer; break;
  56. case Type_SoaPointer: kind = Typeid_SoaPointer; break;
  57. }
  58. if (is_type_cstring(type)) {
  59. special = 1;
  60. } else if (is_type_integer(type) && !is_type_unsigned(type)) {
  61. special = 1;
  62. }
  63. u64 data = 0;
  64. if (build_context.ptr_size == 4) {
  65. GB_ASSERT(id <= (1u<<24u));
  66. data |= (id &~ (1u<<24)) << 0u; // index
  67. data |= (kind &~ (1u<<5)) << 24u; // kind
  68. data |= (named &~ (1u<<1)) << 29u; // named
  69. data |= (special &~ (1u<<1)) << 30u; // special
  70. data |= (reserved &~ (1u<<1)) << 31u; // reserved
  71. } else {
  72. GB_ASSERT(build_context.ptr_size == 8);
  73. GB_ASSERT(id <= (1ull<<56u));
  74. data |= (id &~ (1ull<<56)) << 0ul; // index
  75. data |= (kind &~ (1ull<<5)) << 56ull; // kind
  76. data |= (named &~ (1ull<<1)) << 61ull; // named
  77. data |= (special &~ (1ull<<1)) << 62ull; // special
  78. data |= (reserved &~ (1ull<<1)) << 63ull; // reserved
  79. }
  80. lbValue res = {};
  81. res.value = LLVMConstInt(lb_type(m, t_typeid), data, false);
  82. res.type = t_typeid;
  83. return res;
  84. }
  85. gb_internal lbValue lb_type_info(lbModule *m, Type *type) {
  86. GB_ASSERT(!build_context.no_rtti);
  87. type = default_type(type);
  88. isize index = lb_type_info_index(m->info, type);
  89. GB_ASSERT(index >= 0);
  90. lbValue data = lb_global_type_info_data_ptr(m);
  91. return lb_emit_array_epi(m, data, index);
  92. }
  93. gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) {
  94. return lb_type_internal_for_procedures_raw(m, type);
  95. }
  96. gb_internal lbValue lb_type_info_member_types_offset(lbProcedure *p, isize count) {
  97. GB_ASSERT(p->module == &p->module->gen->default_module);
  98. lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_types.addr, lb_global_type_info_member_types_index);
  99. lb_global_type_info_member_types_index += cast(i32)count;
  100. return offset;
  101. }
  102. gb_internal lbValue lb_type_info_member_names_offset(lbProcedure *p, isize count) {
  103. GB_ASSERT(p->module == &p->module->gen->default_module);
  104. lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_names.addr, lb_global_type_info_member_names_index);
  105. lb_global_type_info_member_names_index += cast(i32)count;
  106. return offset;
  107. }
  108. gb_internal lbValue lb_type_info_member_offsets_offset(lbProcedure *p, isize count) {
  109. GB_ASSERT(p->module == &p->module->gen->default_module);
  110. lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_offsets.addr, lb_global_type_info_member_offsets_index);
  111. lb_global_type_info_member_offsets_index += cast(i32)count;
  112. return offset;
  113. }
  114. gb_internal lbValue lb_type_info_member_usings_offset(lbProcedure *p, isize count) {
  115. GB_ASSERT(p->module == &p->module->gen->default_module);
  116. lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_usings.addr, lb_global_type_info_member_usings_index);
  117. lb_global_type_info_member_usings_index += cast(i32)count;
  118. return offset;
  119. }
  120. gb_internal lbValue lb_type_info_member_tags_offset(lbProcedure *p, isize count) {
  121. GB_ASSERT(p->module == &p->module->gen->default_module);
  122. lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_tags.addr, lb_global_type_info_member_tags_index);
  123. lb_global_type_info_member_tags_index += cast(i32)count;
  124. return offset;
  125. }
  126. gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data
  127. if (build_context.no_rtti) {
  128. return;
  129. }
  130. lbModule *m = p->module;
  131. CheckerInfo *info = m->info;
  132. i64 global_type_info_data_entity_count = 0;
  133. {
  134. // NOTE(bill): Set the type_table slice with the global backing array
  135. lbValue global_type_table = lb_find_runtime_value(m, str_lit("type_table"));
  136. Type *type = base_type(lb_global_type_info_data_entity->type);
  137. GB_ASSERT(is_type_array(type));
  138. global_type_info_data_entity_count = type->Array.count;
  139. LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
  140. LLVMValueRef data = LLVMConstInBoundsGEP2(lb_type(m, lb_global_type_info_data_entity->type), lb_global_type_info_data_ptr(m).value, indices, gb_count_of(indices));
  141. LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), type->Array.count, true);
  142. Type *t = type_deref(global_type_table.type);
  143. GB_ASSERT(is_type_slice(t));
  144. LLVMValueRef slice = llvm_const_slice_internal(m, data, len);
  145. LLVMSetInitializer(global_type_table.value, slice);
  146. }
  147. // Useful types
  148. Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags"));
  149. Type *t_type_info_flags = type_info_flags_entity->type;
  150. auto entries_handled = slice_make<bool>(heap_allocator(), cast(isize)global_type_info_data_entity_count);
  151. defer (gb_free(heap_allocator(), entries_handled.data));
  152. entries_handled[0] = true;
  153. for_array(type_info_type_index, info->type_info_types) {
  154. Type *t = info->type_info_types[type_info_type_index];
  155. if (t == nullptr || t == t_invalid) {
  156. continue;
  157. }
  158. isize entry_index = lb_type_info_index(info, t, false);
  159. if (entry_index <= 0) {
  160. continue;
  161. }
  162. if (entries_handled[entry_index]) {
  163. continue;
  164. }
  165. entries_handled[entry_index] = true;
  166. lbValue global_data_ptr = lb_global_type_info_data_ptr(m);
  167. lbValue tag = {};
  168. lbValue ti_ptr = lb_emit_array_epi(p, global_data_ptr, cast(i32)entry_index);
  169. i64 size = type_size_of(t);
  170. i64 align = type_align_of(t);
  171. u32 flags = type_info_flags_of_type(t);
  172. lbValue id = lb_typeid(m, t);
  173. GB_ASSERT_MSG(align != 0, "%lld %s", align, type_to_string(t));
  174. lbValue type_info_flags = lb_const_int(p->module, t_type_info_flags, flags);
  175. lbValue size_ptr = lb_emit_struct_ep(p, ti_ptr, 0);
  176. lbValue align_ptr = lb_emit_struct_ep(p, ti_ptr, 1);
  177. lbValue flags_ptr = lb_emit_struct_ep(p, ti_ptr, 2);
  178. lbValue id_ptr = lb_emit_struct_ep(p, ti_ptr, 3);
  179. lb_emit_store(p, size_ptr, lb_const_int(m, t_int, size));
  180. lb_emit_store(p, align_ptr, lb_const_int(m, t_int, align));
  181. lb_emit_store(p, flags_ptr, type_info_flags);
  182. lb_emit_store(p, id_ptr, id);
  183. lbValue variant_ptr = lb_emit_struct_ep(p, ti_ptr, 4);
  184. switch (t->kind) {
  185. case Type_Named: {
  186. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_named_ptr);
  187. LLVMValueRef pkg_name = nullptr;
  188. if (t->Named.type_name->pkg) {
  189. pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value;
  190. } else {
  191. pkg_name = LLVMConstNull(lb_type(m, t_string));
  192. }
  193. String proc_name = {};
  194. if (t->Named.type_name->parent_proc_decl) {
  195. DeclInfo *decl = t->Named.type_name->parent_proc_decl;
  196. if (decl->entity && decl->entity->kind == Entity_Procedure) {
  197. proc_name = decl->entity->token.string;
  198. }
  199. }
  200. TokenPos pos = t->Named.type_name->token.pos;
  201. lbValue loc = lb_emit_source_code_location_const(p, proc_name, pos);
  202. LLVMValueRef vals[4] = {
  203. lb_const_string(p->module, t->Named.type_name->token.string).value,
  204. lb_type_info(m, t->Named.base).value,
  205. pkg_name,
  206. loc.value
  207. };
  208. lbValue res = {};
  209. res.type = type_deref(tag.type);
  210. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  211. lb_emit_store(p, tag, res);
  212. break;
  213. }
  214. case Type_Basic:
  215. switch (t->Basic.kind) {
  216. case Basic_bool:
  217. case Basic_b8:
  218. case Basic_b16:
  219. case Basic_b32:
  220. case Basic_b64:
  221. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_boolean_ptr);
  222. break;
  223. case Basic_i8:
  224. case Basic_u8:
  225. case Basic_i16:
  226. case Basic_u16:
  227. case Basic_i32:
  228. case Basic_u32:
  229. case Basic_i64:
  230. case Basic_u64:
  231. case Basic_i128:
  232. case Basic_u128:
  233. case Basic_i16le:
  234. case Basic_u16le:
  235. case Basic_i32le:
  236. case Basic_u32le:
  237. case Basic_i64le:
  238. case Basic_u64le:
  239. case Basic_i128le:
  240. case Basic_u128le:
  241. case Basic_i16be:
  242. case Basic_u16be:
  243. case Basic_i32be:
  244. case Basic_u32be:
  245. case Basic_i64be:
  246. case Basic_u64be:
  247. case Basic_i128be:
  248. case Basic_u128be:
  249. case Basic_int:
  250. case Basic_uint:
  251. case Basic_uintptr: {
  252. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_integer_ptr);
  253. lbValue is_signed = lb_const_bool(m, t_bool, (t->Basic.flags & BasicFlag_Unsigned) == 0);
  254. // NOTE(bill): This is matches the runtime layout
  255. u8 endianness_value = 0;
  256. if (t->Basic.flags & BasicFlag_EndianLittle) {
  257. endianness_value = 1;
  258. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  259. endianness_value = 2;
  260. }
  261. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  262. LLVMValueRef vals[2] = {
  263. is_signed.value,
  264. endianness.value,
  265. };
  266. lbValue res = {};
  267. res.type = type_deref(tag.type);
  268. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  269. lb_emit_store(p, tag, res);
  270. break;
  271. }
  272. case Basic_rune:
  273. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_rune_ptr);
  274. break;
  275. case Basic_f16:
  276. case Basic_f32:
  277. case Basic_f64:
  278. case Basic_f16le:
  279. case Basic_f32le:
  280. case Basic_f64le:
  281. case Basic_f16be:
  282. case Basic_f32be:
  283. case Basic_f64be:
  284. {
  285. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_float_ptr);
  286. // NOTE(bill): This is matches the runtime layout
  287. u8 endianness_value = 0;
  288. if (t->Basic.flags & BasicFlag_EndianLittle) {
  289. endianness_value = 1;
  290. } else if (t->Basic.flags & BasicFlag_EndianBig) {
  291. endianness_value = 2;
  292. }
  293. lbValue endianness = lb_const_int(m, t_u8, endianness_value);
  294. LLVMValueRef vals[1] = {
  295. endianness.value,
  296. };
  297. lbValue res = {};
  298. res.type = type_deref(tag.type);
  299. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  300. lb_emit_store(p, tag, res);
  301. }
  302. break;
  303. case Basic_complex32:
  304. case Basic_complex64:
  305. case Basic_complex128:
  306. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_complex_ptr);
  307. break;
  308. case Basic_quaternion64:
  309. case Basic_quaternion128:
  310. case Basic_quaternion256:
  311. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_quaternion_ptr);
  312. break;
  313. case Basic_rawptr:
  314. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr);
  315. break;
  316. case Basic_string:
  317. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr);
  318. break;
  319. case Basic_cstring:
  320. {
  321. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr);
  322. LLVMValueRef vals[1] = {
  323. lb_const_bool(m, t_bool, true).value,
  324. };
  325. lbValue res = {};
  326. res.type = type_deref(tag.type);
  327. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  328. lb_emit_store(p, tag, res);
  329. }
  330. break;
  331. case Basic_any:
  332. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_any_ptr);
  333. break;
  334. case Basic_typeid:
  335. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_typeid_ptr);
  336. break;
  337. }
  338. break;
  339. case Type_Pointer: {
  340. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr);
  341. lbValue gep = lb_type_info(m, t->Pointer.elem);
  342. LLVMValueRef vals[1] = {
  343. gep.value,
  344. };
  345. lbValue res = {};
  346. res.type = type_deref(tag.type);
  347. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  348. lb_emit_store(p, tag, res);
  349. break;
  350. }
  351. case Type_MultiPointer: {
  352. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_multi_pointer_ptr);
  353. lbValue gep = lb_type_info(m, t->MultiPointer.elem);
  354. LLVMValueRef vals[1] = {
  355. gep.value,
  356. };
  357. lbValue res = {};
  358. res.type = type_deref(tag.type);
  359. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  360. lb_emit_store(p, tag, res);
  361. break;
  362. }
  363. case Type_SoaPointer: {
  364. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_soa_pointer_ptr);
  365. lbValue gep = lb_type_info(m, t->SoaPointer.elem);
  366. LLVMValueRef vals[1] = {
  367. gep.value,
  368. };
  369. lbValue res = {};
  370. res.type = type_deref(tag.type);
  371. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  372. lb_emit_store(p, tag, res);
  373. break;
  374. }
  375. case Type_Array: {
  376. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_array_ptr);
  377. i64 ez = type_size_of(t->Array.elem);
  378. LLVMValueRef vals[3] = {
  379. lb_type_info(m, t->Array.elem).value,
  380. lb_const_int(m, t_int, ez).value,
  381. lb_const_int(m, t_int, t->Array.count).value,
  382. };
  383. lbValue res = {};
  384. res.type = type_deref(tag.type);
  385. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  386. lb_emit_store(p, tag, res);
  387. break;
  388. }
  389. case Type_EnumeratedArray: {
  390. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enumerated_array_ptr);
  391. LLVMValueRef vals[7] = {
  392. lb_type_info(m, t->EnumeratedArray.elem).value,
  393. lb_type_info(m, t->EnumeratedArray.index).value,
  394. lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value,
  395. lb_const_int(m, t_int, t->EnumeratedArray.count).value,
  396. // Unions
  397. LLVMConstNull(lb_type(m, t_type_info_enum_value)),
  398. LLVMConstNull(lb_type(m, t_type_info_enum_value)),
  399. lb_const_bool(m, t_bool, t->EnumeratedArray.is_sparse).value,
  400. };
  401. lbValue res = {};
  402. res.type = type_deref(tag.type);
  403. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  404. lb_emit_store(p, tag, res);
  405. // NOTE(bill): Union assignment
  406. lbValue min_value = lb_emit_struct_ep(p, tag, 4);
  407. lbValue max_value = lb_emit_struct_ep(p, tag, 5);
  408. lbValue min_v = lb_const_value(m, t_i64, *t->EnumeratedArray.min_value);
  409. lbValue max_v = lb_const_value(m, t_i64, *t->EnumeratedArray.max_value);
  410. lb_emit_store(p, min_value, min_v);
  411. lb_emit_store(p, max_value, max_v);
  412. break;
  413. }
  414. case Type_DynamicArray: {
  415. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_dynamic_array_ptr);
  416. LLVMValueRef vals[2] = {
  417. lb_type_info(m, t->DynamicArray.elem).value,
  418. lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value,
  419. };
  420. lbValue res = {};
  421. res.type = type_deref(tag.type);
  422. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  423. lb_emit_store(p, tag, res);
  424. break;
  425. }
  426. case Type_Slice: {
  427. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_slice_ptr);
  428. LLVMValueRef vals[2] = {
  429. lb_type_info(m, t->Slice.elem).value,
  430. lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value,
  431. };
  432. lbValue res = {};
  433. res.type = type_deref(tag.type);
  434. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  435. lb_emit_store(p, tag, res);
  436. break;
  437. }
  438. case Type_Proc: {
  439. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_procedure_ptr);
  440. LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr));
  441. LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr));
  442. if (t->Proc.params != nullptr) {
  443. params = lb_type_info(m, t->Proc.params).value;
  444. }
  445. if (t->Proc.results != nullptr) {
  446. results = lb_type_info(m, t->Proc.results).value;
  447. }
  448. LLVMValueRef vals[4] = {
  449. params,
  450. results,
  451. lb_const_bool(m, t_bool, t->Proc.variadic).value,
  452. lb_const_int(m, t_u8, t->Proc.calling_convention).value,
  453. };
  454. lbValue res = {};
  455. res.type = type_deref(tag.type);
  456. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  457. lb_emit_store(p, tag, res);
  458. break;
  459. }
  460. case Type_Tuple: {
  461. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_parameters_ptr);
  462. lbValue memory_types = lb_type_info_member_types_offset(p, t->Tuple.variables.count);
  463. lbValue memory_names = lb_type_info_member_names_offset(p, t->Tuple.variables.count);
  464. for_array(i, t->Tuple.variables) {
  465. // NOTE(bill): offset is not used for tuples
  466. Entity *f = t->Tuple.variables[i];
  467. lbValue index = lb_const_int(m, t_int, i);
  468. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  469. // TODO(bill): Make this constant if possible, 'lb_const_store' does not work
  470. lb_emit_store(p, type_info, lb_type_info(m, f->type));
  471. if (f->token.string.len > 0) {
  472. lbValue name = lb_emit_ptr_offset(p, memory_names, index);
  473. lb_emit_store(p, name, lb_const_string(m, f->token.string));
  474. }
  475. }
  476. lbValue count = lb_const_int(m, t_int, t->Tuple.variables.count);
  477. LLVMValueRef types_slice = llvm_const_slice(m, memory_types, count);
  478. LLVMValueRef names_slice = llvm_const_slice(m, memory_names, count);
  479. LLVMValueRef vals[2] = {
  480. types_slice,
  481. names_slice,
  482. };
  483. lbValue res = {};
  484. res.type = type_deref(tag.type);
  485. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  486. lb_emit_store(p, tag, res);
  487. break;
  488. }
  489. case Type_Enum:
  490. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enum_ptr);
  491. {
  492. GB_ASSERT(t->Enum.base_type != nullptr);
  493. // 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));
  494. LLVMValueRef vals[3] = {};
  495. vals[0] = lb_type_info(m, t->Enum.base_type).value;
  496. if (t->Enum.fields.count > 0) {
  497. auto fields = t->Enum.fields;
  498. lbValue name_array = lb_generate_global_array(m, t_string, fields.count,
  499. str_lit("$enum_names"), cast(i64)entry_index);
  500. lbValue value_array = lb_generate_global_array(m, t_type_info_enum_value, fields.count,
  501. str_lit("$enum_values"), cast(i64)entry_index);
  502. LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  503. LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count);
  504. GB_ASSERT(is_type_integer(t->Enum.base_type));
  505. for_array(i, fields) {
  506. name_values[i] = lb_const_string(m, fields[i]->token.string).value;
  507. value_values[i] = lb_const_value(m, t_i64, fields[i]->Constant.value).value;
  508. }
  509. LLVMValueRef name_init = llvm_const_array(lb_type(m, t_string), name_values, cast(unsigned)fields.count);
  510. LLVMValueRef value_init = llvm_const_array(lb_type(m, t_type_info_enum_value), value_values, cast(unsigned)fields.count);
  511. LLVMSetInitializer(name_array.value, name_init);
  512. LLVMSetInitializer(value_array.value, value_init);
  513. LLVMSetGlobalConstant(name_array.value, true);
  514. LLVMSetGlobalConstant(value_array.value, true);
  515. lbValue v_count = lb_const_int(m, t_int, fields.count);
  516. vals[1] = llvm_const_slice(m, lb_array_elem(p, name_array), v_count);
  517. vals[2] = llvm_const_slice(m, lb_array_elem(p, value_array), v_count);
  518. } else {
  519. vals[1] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[1]->type));
  520. vals[2] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[2]->type));
  521. }
  522. lbValue res = {};
  523. res.type = type_deref(tag.type);
  524. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  525. lb_emit_store(p, tag, res);
  526. }
  527. break;
  528. case Type_Union: {
  529. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_union_ptr);
  530. {
  531. LLVMValueRef vals[7] = {};
  532. isize variant_count = gb_max(0, t->Union.variants.count);
  533. lbValue memory_types = lb_type_info_member_types_offset(p, variant_count);
  534. // NOTE(bill): Zeroth is nil so ignore it
  535. for (isize variant_index = 0; variant_index < variant_count; variant_index++) {
  536. Type *vt = t->Union.variants[variant_index];
  537. lbValue tip = lb_type_info(m, vt);
  538. lbValue index = lb_const_int(m, t_int, variant_index);
  539. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  540. lb_emit_store(p, type_info, lb_type_info(m, vt));
  541. }
  542. lbValue count = lb_const_int(m, t_int, variant_count);
  543. vals[0] = llvm_const_slice(m, memory_types, count);
  544. i64 tag_size = union_tag_size(t);
  545. if (tag_size > 0) {
  546. i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size);
  547. vals[1] = lb_const_int(m, t_uintptr, tag_offset).value;
  548. vals[2] = lb_type_info(m, union_tag_type(t)).value;
  549. } else {
  550. vals[1] = lb_const_int(m, t_uintptr, 0).value;
  551. vals[2] = LLVMConstNull(lb_type(m, t_type_info_ptr));
  552. }
  553. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  554. vals[3] = lb_equal_proc_for_type(m, t).value;
  555. }
  556. vals[4] = lb_const_bool(m, t_bool, t->Union.custom_align != 0).value;
  557. vals[5] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_no_nil).value;
  558. vals[6] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_shared_nil).value;
  559. for (isize i = 0; i < gb_count_of(vals); i++) {
  560. if (vals[i] == nullptr) {
  561. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i)));
  562. }
  563. }
  564. lbValue res = {};
  565. res.type = type_deref(tag.type);
  566. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  567. lb_emit_store(p, tag, res);
  568. }
  569. break;
  570. }
  571. case Type_Struct: {
  572. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_struct_ptr);
  573. LLVMValueRef vals[13] = {};
  574. {
  575. lbValue is_packed = lb_const_bool(m, t_bool, t->Struct.is_packed);
  576. lbValue is_raw_union = lb_const_bool(m, t_bool, t->Struct.is_raw_union);
  577. lbValue is_no_copy = lb_const_bool(m, t_bool, t->Struct.is_no_copy);
  578. lbValue is_custom_align = lb_const_bool(m, t_bool, t->Struct.custom_align != 0);
  579. vals[5] = is_packed.value;
  580. vals[6] = is_raw_union.value;
  581. vals[7] = is_no_copy.value;
  582. vals[8] = is_custom_align.value;
  583. if (is_type_comparable(t) && !is_type_simple_compare(t)) {
  584. vals[9] = lb_equal_proc_for_type(m, t).value;
  585. }
  586. if (t->Struct.soa_kind != StructSoa_None) {
  587. lbValue kind = lb_emit_struct_ep(p, tag, 10);
  588. Type *kind_type = type_deref(kind.type);
  589. lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind));
  590. lbValue soa_type = lb_type_info(m, t->Struct.soa_elem);
  591. lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count);
  592. vals[10] = soa_kind.value;
  593. vals[11] = soa_type.value;
  594. vals[12] = soa_len.value;
  595. }
  596. }
  597. isize count = t->Struct.fields.count;
  598. if (count > 0) {
  599. lbValue memory_types = lb_type_info_member_types_offset (p, count);
  600. lbValue memory_names = lb_type_info_member_names_offset (p, count);
  601. lbValue memory_offsets = lb_type_info_member_offsets_offset(p, count);
  602. lbValue memory_usings = lb_type_info_member_usings_offset (p, count);
  603. lbValue memory_tags = lb_type_info_member_tags_offset (p, count);
  604. type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet
  605. for (isize source_index = 0; source_index < count; source_index++) {
  606. Entity *f = t->Struct.fields[source_index];
  607. lbValue tip = lb_type_info(m, f->type);
  608. i64 foffset = 0;
  609. if (!t->Struct.is_raw_union) {
  610. GB_ASSERT(t->Struct.offsets != nullptr);
  611. GB_ASSERT(0 <= f->Variable.field_index && f->Variable.field_index < count);
  612. foffset = t->Struct.offsets[source_index];
  613. }
  614. GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field);
  615. lbValue index = lb_const_int(m, t_int, source_index);
  616. lbValue type_info = lb_emit_ptr_offset(p, memory_types, index);
  617. lbValue offset = lb_emit_ptr_offset(p, memory_offsets, index);
  618. lbValue is_using = lb_emit_ptr_offset(p, memory_usings, index);
  619. lb_emit_store(p, type_info, lb_type_info(m, f->type));
  620. if (f->token.string.len > 0) {
  621. lbValue name = lb_emit_ptr_offset(p, memory_names, index);
  622. lb_emit_store(p, name, lb_const_string(m, f->token.string));
  623. }
  624. lb_emit_store(p, offset, lb_const_int(m, t_uintptr, foffset));
  625. lb_emit_store(p, is_using, lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0));
  626. if (t->Struct.tags != nullptr) {
  627. String tag_string = t->Struct.tags[source_index];
  628. if (tag_string.len > 0) {
  629. lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index);
  630. lb_emit_store(p, tag_ptr, lb_const_string(m, tag_string));
  631. }
  632. }
  633. }
  634. lbValue cv = lb_const_int(m, t_int, count);
  635. vals[0] = llvm_const_slice(m, memory_types, cv);
  636. vals[1] = llvm_const_slice(m, memory_names, cv);
  637. vals[2] = llvm_const_slice(m, memory_offsets, cv);
  638. vals[3] = llvm_const_slice(m, memory_usings, cv);
  639. vals[4] = llvm_const_slice(m, memory_tags, cv);
  640. }
  641. for (isize i = 0; i < gb_count_of(vals); i++) {
  642. if (vals[i] == nullptr) {
  643. vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i)));
  644. }
  645. }
  646. lbValue res = {};
  647. res.type = type_deref(tag.type);
  648. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  649. lb_emit_store(p, tag, res);
  650. break;
  651. }
  652. case Type_Map: {
  653. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr);
  654. init_map_internal_types(t);
  655. LLVMValueRef vals[3] = {
  656. lb_type_info(m, t->Map.key).value,
  657. lb_type_info(m, t->Map.value).value,
  658. lb_gen_map_info_ptr(p->module, t).value
  659. };
  660. lbValue res = {};
  661. res.type = type_deref(tag.type);
  662. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  663. lb_emit_store(p, tag, res);
  664. break;
  665. }
  666. case Type_BitSet:
  667. {
  668. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_bit_set_ptr);
  669. GB_ASSERT(is_type_typed(t->BitSet.elem));
  670. LLVMValueRef vals[4] = {
  671. lb_type_info(m, t->BitSet.elem).value,
  672. LLVMConstNull(lb_type(m, t_type_info_ptr)),
  673. lb_const_int(m, t_i64, t->BitSet.lower).value,
  674. lb_const_int(m, t_i64, t->BitSet.upper).value,
  675. };
  676. if (t->BitSet.underlying != nullptr) {
  677. vals[1] =lb_type_info(m, t->BitSet.underlying).value;
  678. }
  679. lbValue res = {};
  680. res.type = type_deref(tag.type);
  681. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  682. lb_emit_store(p, tag, res);
  683. }
  684. break;
  685. case Type_SimdVector:
  686. {
  687. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_simd_vector_ptr);
  688. LLVMValueRef vals[3] = {};
  689. vals[0] = lb_type_info(m, t->SimdVector.elem).value;
  690. vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value;
  691. vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value;
  692. lbValue res = {};
  693. res.type = type_deref(tag.type);
  694. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  695. lb_emit_store(p, tag, res);
  696. }
  697. break;
  698. case Type_RelativePointer:
  699. {
  700. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_pointer_ptr);
  701. LLVMValueRef vals[2] = {
  702. lb_type_info(m, t->RelativePointer.pointer_type).value,
  703. lb_type_info(m, t->RelativePointer.base_integer).value,
  704. };
  705. lbValue res = {};
  706. res.type = type_deref(tag.type);
  707. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  708. lb_emit_store(p, tag, res);
  709. }
  710. break;
  711. case Type_RelativeMultiPointer:
  712. {
  713. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_multi_pointer_ptr);
  714. LLVMValueRef vals[2] = {
  715. lb_type_info(m, t->RelativeMultiPointer.pointer_type).value,
  716. lb_type_info(m, t->RelativeMultiPointer.base_integer).value,
  717. };
  718. lbValue res = {};
  719. res.type = type_deref(tag.type);
  720. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  721. lb_emit_store(p, tag, res);
  722. }
  723. break;
  724. case Type_Matrix:
  725. {
  726. tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_matrix_ptr);
  727. i64 ez = type_size_of(t->Matrix.elem);
  728. LLVMValueRef vals[5] = {
  729. lb_type_info(m, t->Matrix.elem).value,
  730. lb_const_int(m, t_int, ez).value,
  731. lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value,
  732. lb_const_int(m, t_int, t->Matrix.row_count).value,
  733. lb_const_int(m, t_int, t->Matrix.column_count).value,
  734. };
  735. lbValue res = {};
  736. res.type = type_deref(tag.type);
  737. res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals));
  738. lb_emit_store(p, tag, res);
  739. }
  740. break;
  741. }
  742. if (tag.value != nullptr) {
  743. Type *tag_type = type_deref(tag.type);
  744. GB_ASSERT(is_type_named(tag_type));
  745. // lb_emit_store_union_variant(p, variant_ptr, lb_emit_load(p, tag), tag_type);
  746. lb_emit_store_union_variant_tag(p, variant_ptr, tag_type);
  747. } else {
  748. if (t != t_llvm_bool) {
  749. GB_PANIC("Unhandled Type_Info variant: %s", type_to_string(t));
  750. }
  751. }
  752. }
  753. for_array(i, entries_handled) {
  754. if (!entries_handled[i]) {
  755. GB_PANIC("UNHANDLED ENTRY %td (%td)", i, entries_handled.count);
  756. }
  757. }
  758. }