llvm_backend_type.cpp 30 KB

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