llvm_backend_type.cpp 30 KB

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