llvm_backend_type.cpp 30 KB

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