llvm_backend_debug.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. gb_internal LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) {
  2. if (key == nullptr) {
  3. return nullptr;
  4. }
  5. mutex_lock(&m->debug_values_mutex);
  6. auto found = map_get(&m->debug_values, key);
  7. mutex_unlock(&m->debug_values_mutex);
  8. if (found) {
  9. return *found;
  10. }
  11. return nullptr;
  12. }
  13. gb_internal void lb_set_llvm_metadata(lbModule *m, void *key, LLVMMetadataRef value) {
  14. if (key != nullptr) {
  15. mutex_lock(&m->debug_values_mutex);
  16. map_set(&m->debug_values, key, value);
  17. mutex_unlock(&m->debug_values_mutex);
  18. }
  19. }
  20. gb_internal LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) {
  21. GB_ASSERT_MSG(p->debug_info != nullptr, "missing debug information for %.*s", LIT(p->name));
  22. for (isize i = p->scope_stack.count-1; i >= 0; i--) {
  23. Scope *s = p->scope_stack[i];
  24. LLVMMetadataRef md = lb_get_llvm_metadata(p->module, s);
  25. if (md) {
  26. return md;
  27. }
  28. }
  29. return p->debug_info;
  30. }
  31. gb_internal LLVMMetadataRef lb_debug_location_from_token_pos(lbProcedure *p, TokenPos pos) {
  32. LLVMMetadataRef scope = lb_get_current_debug_scope(p);
  33. GB_ASSERT_MSG(scope != nullptr, "%.*s", LIT(p->name));
  34. return LLVMDIBuilderCreateDebugLocation(p->module->ctx, cast(unsigned)pos.line, cast(unsigned)pos.column, scope, nullptr);
  35. }
  36. gb_internal LLVMMetadataRef lb_debug_location_from_ast(lbProcedure *p, Ast *node) {
  37. GB_ASSERT(node != nullptr);
  38. return lb_debug_location_from_token_pos(p, ast_token(node).pos);
  39. }
  40. gb_internal LLVMMetadataRef lb_debug_end_location_from_ast(lbProcedure *p, Ast *node) {
  41. GB_ASSERT(node != nullptr);
  42. return lb_debug_location_from_token_pos(p, ast_end_token(node).pos);
  43. }
  44. gb_internal void lb_debug_file_line(lbModule *m, Ast *node, LLVMMetadataRef *file, unsigned *line) {
  45. if (*file == nullptr) {
  46. if (node) {
  47. *file = lb_get_llvm_metadata(m, node->file());
  48. *line = cast(unsigned)ast_token(node).pos.line;
  49. }
  50. }
  51. }
  52. gb_internal LLVMMetadataRef lb_debug_procedure_parameters(lbModule *m, Type *type) {
  53. if (is_type_proc(type)) {
  54. return lb_debug_type(m, t_rawptr);
  55. }
  56. if (type->kind == Type_Tuple && type->Tuple.variables.count == 1) {
  57. return lb_debug_procedure_parameters(m, type->Tuple.variables[0]->type);
  58. }
  59. return lb_debug_type(m, type);
  60. }
  61. gb_internal LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) {
  62. i64 size = type_size_of(type); // Check size
  63. gb_unused(size);
  64. GB_ASSERT(type != t_invalid);
  65. /* unsigned const ptr_size = cast(unsigned)build_context.ptr_size;
  66. unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size); */
  67. GB_ASSERT(type->kind == Type_Proc);
  68. unsigned parameter_count = 1;
  69. for (i32 i = 0; i < type->Proc.param_count; i++) {
  70. Entity *e = type->Proc.params->Tuple.variables[i];
  71. if (e->kind == Entity_Variable) {
  72. parameter_count += 1;
  73. }
  74. }
  75. auto parameters = array_make<LLVMMetadataRef>(permanent_allocator(), 0, type->Proc.param_count+type->Proc.result_count+2);
  76. array_add(&parameters, cast(LLVMMetadataRef)nullptr);
  77. bool return_is_tuple = false;
  78. if (type->Proc.result_count != 0) {
  79. Type *single_ret = reduce_tuple_to_single_type(type->Proc.results);
  80. if (is_type_proc(single_ret)) {
  81. single_ret = t_rawptr;
  82. }
  83. if (is_type_tuple(single_ret) && is_calling_convention_odin(type->Proc.calling_convention)) {
  84. LLVMTypeRef actual = lb_type_internal_for_procedures_raw(m, type);
  85. actual = LLVMGetReturnType(actual);
  86. if (actual == nullptr) {
  87. // results were passed as a single pointer
  88. parameters[0] = lb_debug_procedure_parameters(m, single_ret);
  89. } else {
  90. LLVMTypeRef possible = lb_type(m, type->Proc.results);
  91. if (possible == actual) {
  92. // results were returned directly
  93. parameters[0] = lb_debug_procedure_parameters(m, single_ret);
  94. } else {
  95. // resulsts were returned separately
  96. return_is_tuple = true;
  97. }
  98. }
  99. } else {
  100. parameters[0] = lb_debug_procedure_parameters(m, single_ret);
  101. }
  102. }
  103. LLVMMetadataRef file = nullptr;
  104. for (i32 i = 0; i < type->Proc.param_count; i++) {
  105. Entity *e = type->Proc.params->Tuple.variables[i];
  106. if (e->kind != Entity_Variable) {
  107. continue;
  108. }
  109. array_add(&parameters, lb_debug_procedure_parameters(m, e->type));
  110. }
  111. if (return_is_tuple) {
  112. Type *results = type->Proc.results;
  113. GB_ASSERT(results != nullptr && results->kind == Type_Tuple);
  114. isize count = results->Tuple.variables.count;
  115. parameters[0] = lb_debug_procedure_parameters(m, results->Tuple.variables[count-1]->type);
  116. for (isize i = 0; i < count-1; i++) {
  117. array_add(&parameters, lb_debug_procedure_parameters(m, results->Tuple.variables[i]->type));
  118. }
  119. }
  120. if (type->Proc.calling_convention == ProcCC_Odin) {
  121. array_add(&parameters, lb_debug_type(m, t_context_ptr));
  122. }
  123. LLVMDIFlags flags = LLVMDIFlagZero;
  124. if (type->Proc.diverging) {
  125. flags = LLVMDIFlagNoReturn;
  126. }
  127. return LLVMDIBuilderCreateSubroutineType(m->debug_builder, file, parameters.data, cast(unsigned)parameters.count, flags);
  128. }
  129. gb_internal LLVMMetadataRef lb_debug_struct_field(lbModule *m, String const &name, Type *type, u64 offset_in_bits) {
  130. unsigned field_line = 1;
  131. LLVMDIFlags field_flags = LLVMDIFlagZero;
  132. AstPackage *pkg = m->info->runtime_package;
  133. GB_ASSERT(pkg->files.count != 0);
  134. LLVMMetadataRef file = lb_get_llvm_metadata(m, pkg->files[0]);
  135. LLVMMetadataRef scope = file;
  136. return LLVMDIBuilderCreateMemberType(m->debug_builder, scope, cast(char const *)name.text, name.len, file, field_line,
  137. 8*cast(u64)type_size_of(type), 8*cast(u32)type_align_of(type), offset_in_bits,
  138. field_flags, lb_debug_type(m, type)
  139. );
  140. }
  141. gb_internal LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &name, u64 size_in_bits, u32 align_in_bits, LLVMMetadataRef *elements, unsigned element_count) {
  142. AstPackage *pkg = m->info->runtime_package;
  143. GB_ASSERT(pkg->files.count != 0);
  144. LLVMMetadataRef file = lb_get_llvm_metadata(m, pkg->files[0]);
  145. LLVMMetadataRef scope = file;
  146. return LLVMDIBuilderCreateStructType(m->debug_builder, scope, cast(char const *)name.text, name.len, file, 1, size_in_bits, align_in_bits, LLVMDIFlagZero, nullptr, elements, element_count, 0, nullptr, "", 0);
  147. }
  148. gb_internal LLVMMetadataRef lb_debug_struct(lbModule *m, Type *type, Type *bt, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  149. GB_ASSERT(bt->kind == Type_Struct);
  150. lb_debug_file_line(m, bt->Struct.node, &file, &line);
  151. unsigned tag = DW_TAG_structure_type;
  152. if (is_type_raw_union(bt)) {
  153. tag = DW_TAG_union_type;
  154. }
  155. u64 size_in_bits = 8*type_size_of(bt);
  156. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  157. LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType(
  158. m->debug_builder, tag,
  159. cast(char const *)name.text, cast(size_t)name.len,
  160. scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0
  161. );
  162. lb_set_llvm_metadata(m, type, temp_forward_decl);
  163. type_set_offsets(bt);
  164. unsigned element_count = cast(unsigned)(bt->Struct.fields.count);
  165. LLVMMetadataRef *elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count);
  166. LLVMMetadataRef member_scope = lb_get_llvm_metadata(m, bt->Struct.scope);
  167. for_array(j, bt->Struct.fields) {
  168. Entity *f = bt->Struct.fields[j];
  169. String fname = f->token.string;
  170. unsigned field_line = 0;
  171. LLVMDIFlags field_flags = LLVMDIFlagZero;
  172. GB_ASSERT(bt->Struct.offsets != nullptr);
  173. u64 offset_in_bits = 8*cast(u64)bt->Struct.offsets[j];
  174. elements[j] = LLVMDIBuilderCreateMemberType(
  175. m->debug_builder,
  176. member_scope,
  177. cast(char const *)fname.text, cast(size_t)fname.len,
  178. file, field_line,
  179. 8*cast(u64)type_size_of(f->type), 8*cast(u32)type_align_of(f->type),
  180. offset_in_bits,
  181. field_flags,
  182. lb_debug_type(m, f->type)
  183. );
  184. }
  185. LLVMMetadataRef final_decl = nullptr;
  186. if (tag == DW_TAG_union_type) {
  187. final_decl = LLVMDIBuilderCreateUnionType(
  188. m->debug_builder, scope,
  189. cast(char const*)name.text, cast(size_t)name.len,
  190. file, line,
  191. size_in_bits, align_in_bits,
  192. LLVMDIFlagZero,
  193. elements, element_count,
  194. 0,
  195. "", 0
  196. );
  197. } else {
  198. final_decl = LLVMDIBuilderCreateStructType(
  199. m->debug_builder, scope,
  200. cast(char const *)name.text, cast(size_t)name.len,
  201. file, line,
  202. size_in_bits, align_in_bits,
  203. LLVMDIFlagZero,
  204. nullptr,
  205. elements, element_count,
  206. 0,
  207. nullptr,
  208. "", 0
  209. );
  210. }
  211. LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl);
  212. lb_set_llvm_metadata(m, type, final_decl);
  213. return final_decl;
  214. }
  215. gb_internal LLVMMetadataRef lb_debug_slice(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  216. Type *bt = base_type(type);
  217. GB_ASSERT(bt->kind == Type_Slice);
  218. unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size);
  219. u64 size_in_bits = 8*type_size_of(bt);
  220. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  221. LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType(
  222. m->debug_builder, DW_TAG_structure_type,
  223. cast(char const *)name.text, cast(size_t)name.len,
  224. scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0
  225. );
  226. lb_set_llvm_metadata(m, type, temp_forward_decl);
  227. unsigned element_count = 2;
  228. LLVMMetadataRef elements[2];
  229. // LLVMMetadataRef member_scope = lb_get_llvm_metadata(m, bt->Slice.scope);
  230. LLVMMetadataRef member_scope = nullptr;
  231. Type *elem_type = alloc_type_pointer(bt->Slice.elem);
  232. elements[0] = LLVMDIBuilderCreateMemberType(
  233. m->debug_builder, member_scope,
  234. "data", 4,
  235. file, line,
  236. 8*cast(u64)type_size_of(elem_type), 8*cast(u32)type_align_of(elem_type),
  237. 0,
  238. LLVMDIFlagZero, lb_debug_type(m, elem_type)
  239. );
  240. elements[1] = LLVMDIBuilderCreateMemberType(
  241. m->debug_builder, member_scope,
  242. "len", 3,
  243. file, line,
  244. 8*cast(u64)type_size_of(t_int), 8*cast(u32)type_align_of(t_int),
  245. ptr_bits,
  246. LLVMDIFlagZero, lb_debug_type(m, t_int)
  247. );
  248. LLVMMetadataRef final_decl = LLVMDIBuilderCreateStructType(
  249. m->debug_builder, scope,
  250. cast(char const *)name.text, cast(size_t)name.len,
  251. file, line,
  252. size_in_bits, align_in_bits,
  253. LLVMDIFlagZero,
  254. nullptr,
  255. elements, element_count,
  256. 0,
  257. nullptr,
  258. "", 0
  259. );
  260. LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl);
  261. lb_set_llvm_metadata(m, type, final_decl);
  262. return final_decl;
  263. }
  264. gb_internal LLVMMetadataRef lb_debug_dynamic_array(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  265. Type *bt = base_type(type);
  266. GB_ASSERT(bt->kind == Type_DynamicArray);
  267. unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size);
  268. unsigned const int_bits = cast(unsigned)(8*build_context.int_size);
  269. u64 size_in_bits = 8*type_size_of(bt);
  270. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  271. LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType(
  272. m->debug_builder, DW_TAG_structure_type,
  273. cast(char const *)name.text, cast(size_t)name.len,
  274. scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0
  275. );
  276. lb_set_llvm_metadata(m, type, temp_forward_decl);
  277. unsigned element_count = 4;
  278. LLVMMetadataRef elements[4];
  279. // LLVMMetadataRef member_scope = lb_get_llvm_metadata(m, bt->DynamicArray.scope);
  280. LLVMMetadataRef member_scope = nullptr;
  281. Type *elem_type = alloc_type_pointer(bt->DynamicArray.elem);
  282. elements[0] = LLVMDIBuilderCreateMemberType(
  283. m->debug_builder, member_scope,
  284. "data", 4,
  285. file, line,
  286. 8*cast(u64)type_size_of(elem_type), 8*cast(u32)type_align_of(elem_type),
  287. 0,
  288. LLVMDIFlagZero, lb_debug_type(m, elem_type)
  289. );
  290. elements[1] = LLVMDIBuilderCreateMemberType(
  291. m->debug_builder, member_scope,
  292. "len", 3,
  293. file, line,
  294. 8*cast(u64)type_size_of(t_int), 8*cast(u32)type_align_of(t_int),
  295. ptr_bits,
  296. LLVMDIFlagZero, lb_debug_type(m, t_int)
  297. );
  298. elements[2] = LLVMDIBuilderCreateMemberType(
  299. m->debug_builder, member_scope,
  300. "cap", 3,
  301. file, line,
  302. 8*cast(u64)type_size_of(t_int), 8*cast(u32)type_align_of(t_int),
  303. ptr_bits+int_bits,
  304. LLVMDIFlagZero, lb_debug_type(m, t_int)
  305. );
  306. elements[3] = LLVMDIBuilderCreateMemberType(
  307. m->debug_builder, member_scope,
  308. "allocator", 9,
  309. file, line,
  310. 8*cast(u64)type_size_of(t_allocator), 8*cast(u32)type_align_of(t_allocator),
  311. ptr_bits+int_bits+int_bits,
  312. LLVMDIFlagZero, lb_debug_type(m, t_allocator)
  313. );
  314. LLVMMetadataRef final_decl = LLVMDIBuilderCreateStructType(
  315. m->debug_builder, scope,
  316. cast(char const *)name.text, cast(size_t)name.len,
  317. file, line,
  318. size_in_bits, align_in_bits,
  319. LLVMDIFlagZero,
  320. nullptr,
  321. elements, element_count,
  322. 0,
  323. nullptr,
  324. "", 0
  325. );
  326. LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl);
  327. lb_set_llvm_metadata(m, type, final_decl);
  328. return final_decl;
  329. }
  330. gb_internal LLVMMetadataRef lb_debug_union(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  331. Type *bt = base_type(type);
  332. GB_ASSERT(bt->kind == Type_Union);
  333. lb_debug_file_line(m, bt->Union.node, &file, &line);
  334. u64 size_in_bits = 8*type_size_of(bt);
  335. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  336. LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType(
  337. m->debug_builder, DW_TAG_union_type,
  338. cast(char const *)name.text, cast(size_t)name.len,
  339. scope, file, line, 0, size_in_bits, align_in_bits, LLVMDIFlagZero, "", 0
  340. );
  341. lb_set_llvm_metadata(m, type, temp_forward_decl);
  342. isize index_offset = 1;
  343. isize variant_offset = 1;
  344. if (is_type_union_maybe_pointer(bt)) {
  345. index_offset = 0;
  346. variant_offset = 0;
  347. } else if (bt->Union.kind == UnionType_no_nil) {
  348. variant_offset = 0;
  349. }
  350. LLVMMetadataRef member_scope = lb_get_llvm_metadata(m, bt->Union.scope);
  351. unsigned element_count = cast(unsigned)bt->Union.variants.count;
  352. if (index_offset > 0) {
  353. GB_ASSERT(index_offset == 1);
  354. element_count += 1;
  355. }
  356. LLVMMetadataRef *elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count);
  357. if (index_offset > 0) {
  358. Type *tag_type = union_tag_type(bt);
  359. u64 offset_in_bits = 8*cast(u64)bt->Union.variant_block_size;
  360. elements[0] = LLVMDIBuilderCreateMemberType(
  361. m->debug_builder, member_scope,
  362. "tag", 3,
  363. file, line,
  364. 8*cast(u64)type_size_of(tag_type), 8*cast(u32)type_align_of(tag_type),
  365. offset_in_bits,
  366. LLVMDIFlagZero, lb_debug_type(m, tag_type)
  367. );
  368. }
  369. for_array(j, bt->Union.variants) {
  370. Type *variant = bt->Union.variants[j];
  371. char name[32] = {};
  372. gb_snprintf(name, gb_size_of(name), "v%td", variant_offset+j);
  373. isize name_len = gb_strlen(name);
  374. elements[index_offset+j] = LLVMDIBuilderCreateMemberType(
  375. m->debug_builder, member_scope,
  376. name, name_len,
  377. file, line,
  378. 8*cast(u64)type_size_of(variant), 8*cast(u32)type_align_of(variant),
  379. 0,
  380. LLVMDIFlagZero, lb_debug_type(m, variant)
  381. );
  382. }
  383. LLVMMetadataRef final_decl = LLVMDIBuilderCreateUnionType(
  384. m->debug_builder,
  385. scope,
  386. cast(char const *)name.text, cast(size_t)name.len,
  387. file, line,
  388. size_in_bits, align_in_bits,
  389. LLVMDIFlagZero,
  390. elements,
  391. element_count,
  392. 0,
  393. "", 0
  394. );
  395. LLVMMetadataReplaceAllUsesWith(temp_forward_decl, final_decl);
  396. lb_set_llvm_metadata(m, type, final_decl);
  397. return final_decl;
  398. }
  399. gb_internal LLVMMetadataRef lb_debug_bitset(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  400. Type *bt = base_type(type);
  401. GB_ASSERT(bt->kind == Type_BitSet);
  402. lb_debug_file_line(m, bt->BitSet.node, &file, &line);
  403. u64 size_in_bits = 8*type_size_of(bt);
  404. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  405. LLVMMetadataRef bit_set_field_type = lb_debug_type(m, t_bool);
  406. unsigned element_count = 0;
  407. LLVMMetadataRef *elements = nullptr;
  408. Type *elem = base_type(bt->BitSet.elem);
  409. if (elem->kind == Type_Enum) {
  410. element_count = cast(unsigned)elem->Enum.fields.count;
  411. elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count);
  412. for_array(i, elem->Enum.fields) {
  413. Entity *f = elem->Enum.fields[i];
  414. GB_ASSERT(f->kind == Entity_Constant);
  415. i64 val = exact_value_to_i64(f->Constant.value);
  416. String field_name = f->token.string;
  417. u64 offset_in_bits = cast(u64)(val - bt->BitSet.lower);
  418. elements[i] = LLVMDIBuilderCreateBitFieldMemberType(
  419. m->debug_builder,
  420. scope,
  421. cast(char const *)field_name.text, field_name.len,
  422. file, line,
  423. 1,
  424. offset_in_bits,
  425. 0,
  426. LLVMDIFlagZero,
  427. bit_set_field_type
  428. );
  429. }
  430. } else {
  431. char name[32] = {};
  432. GB_ASSERT(is_type_integer(elem));
  433. i64 count = bt->BitSet.upper - bt->BitSet.lower + 1;
  434. GB_ASSERT(0 <= count);
  435. element_count = cast(unsigned)count;
  436. elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count);
  437. for (unsigned i = 0; i < element_count; i++) {
  438. u64 offset_in_bits = i;
  439. i64 val = bt->BitSet.lower + cast(i64)i;
  440. gb_snprintf(name, gb_count_of(name), "%lld", cast(long long)val);
  441. elements[i] = LLVMDIBuilderCreateBitFieldMemberType(
  442. m->debug_builder,
  443. scope,
  444. name, gb_strlen(name),
  445. file, line,
  446. 1,
  447. offset_in_bits,
  448. 0,
  449. LLVMDIFlagZero,
  450. bit_set_field_type
  451. );
  452. }
  453. }
  454. LLVMMetadataRef final_decl = LLVMDIBuilderCreateUnionType(
  455. m->debug_builder,
  456. scope,
  457. cast(char const *)name.text, cast(size_t)name.len,
  458. file, line,
  459. size_in_bits, align_in_bits,
  460. LLVMDIFlagZero,
  461. elements,
  462. element_count,
  463. 0,
  464. "", 0
  465. );
  466. lb_set_llvm_metadata(m, type, final_decl);
  467. return final_decl;
  468. }
  469. gb_internal LLVMMetadataRef lb_debug_bitfield(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  470. Type *bt = base_type(type);
  471. GB_ASSERT(bt->kind == Type_BitField);
  472. lb_debug_file_line(m, bt->BitField.node, &file, &line);
  473. u64 size_in_bits = 8*type_size_of(bt);
  474. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  475. unsigned element_count = cast(unsigned)bt->BitField.fields.count;
  476. LLVMMetadataRef *elements = gb_alloc_array(permanent_allocator(), LLVMMetadataRef, element_count);
  477. u64 offset_in_bits = 0;
  478. for (unsigned i = 0; i < element_count; i++) {
  479. Entity *f = bt->BitField.fields[i];
  480. u8 bit_size = bt->BitField.bit_sizes[i];
  481. GB_ASSERT(f->kind == Entity_Variable);
  482. String name = f->token.string;
  483. elements[i] = LLVMDIBuilderCreateBitFieldMemberType(m->debug_builder, scope, cast(char const *)name.text, name.len, file, line,
  484. bit_size, offset_in_bits, 0,
  485. LLVMDIFlagZero, lb_debug_type(m, f->type)
  486. );
  487. offset_in_bits += bit_size;
  488. }
  489. LLVMMetadataRef final_decl = LLVMDIBuilderCreateStructType(
  490. m->debug_builder, scope,
  491. cast(char const *)name.text, cast(size_t)name.len,
  492. file, line,
  493. size_in_bits, align_in_bits,
  494. LLVMDIFlagZero,
  495. nullptr,
  496. elements, element_count,
  497. 0,
  498. nullptr,
  499. "", 0
  500. );
  501. lb_set_llvm_metadata(m, type, final_decl);
  502. return final_decl;
  503. }
  504. gb_internal LLVMMetadataRef lb_debug_enum(lbModule *m, Type *type, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
  505. Type *bt = base_type(type);
  506. GB_ASSERT(bt->kind == Type_Enum);
  507. lb_debug_file_line(m, bt->Enum.node, &file, &line);
  508. u64 size_in_bits = 8*type_size_of(bt);
  509. u32 align_in_bits = 8*cast(u32)type_align_of(bt);
  510. unsigned element_count = cast(unsigned)bt->Enum.fields.count;
  511. LLVMMetadataRef *elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count);
  512. Type *bt_enum = base_enum_type(bt);
  513. LLVMBool is_unsigned = is_type_unsigned(bt_enum);
  514. for (unsigned i = 0; i < element_count; i++) {
  515. Entity *f = bt->Enum.fields[i];
  516. GB_ASSERT(f->kind == Entity_Constant);
  517. String enum_name = f->token.string;
  518. i64 value = exact_value_to_i64(f->Constant.value);
  519. elements[i] = LLVMDIBuilderCreateEnumerator(m->debug_builder, cast(char const *)enum_name.text, cast(size_t)enum_name.len, value, is_unsigned);
  520. }
  521. LLVMMetadataRef class_type = lb_debug_type(m, bt_enum);
  522. LLVMMetadataRef final_decl = LLVMDIBuilderCreateEnumerationType(
  523. m->debug_builder,
  524. scope,
  525. cast(char const *)name.text, cast(size_t)name.len,
  526. file, line,
  527. size_in_bits, align_in_bits,
  528. elements, element_count,
  529. class_type
  530. );
  531. lb_set_llvm_metadata(m, type, final_decl);
  532. return final_decl;
  533. }
  534. gb_internal LLVMMetadataRef lb_debug_type_basic_type(lbModule *m, String const &name, u64 size_in_bits, LLVMDWARFTypeEncoding encoding, LLVMDIFlags flags = LLVMDIFlagZero) {
  535. LLVMMetadataRef basic_type = LLVMDIBuilderCreateBasicType(m->debug_builder, cast(char const *)name.text, name.len, size_in_bits, encoding, flags);
  536. #if 1
  537. LLVMMetadataRef final_decl = LLVMDIBuilderCreateTypedef(m->debug_builder, basic_type, cast(char const *)name.text, name.len, nullptr, 0, nullptr, cast(u32)size_in_bits);
  538. return final_decl;
  539. #else
  540. return basic_type;
  541. #endif
  542. }
  543. gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
  544. i64 size = type_size_of(type); // Check size
  545. gb_unused(size);
  546. GB_ASSERT(type != t_invalid);
  547. /* unsigned const ptr_size = cast(unsigned)build_context.ptr_size; */
  548. unsigned const int_bits = cast(unsigned)(8*build_context.int_size);
  549. unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size);
  550. switch (type->kind) {
  551. case Type_Basic:
  552. switch (type->Basic.kind) {
  553. case Basic_llvm_bool: return lb_debug_type_basic_type(m, str_lit("llvm bool"), 1, LLVMDWARFTypeEncoding_Boolean);
  554. case Basic_bool: return lb_debug_type_basic_type(m, str_lit("bool"), 8, LLVMDWARFTypeEncoding_Boolean);
  555. case Basic_b8: return lb_debug_type_basic_type(m, str_lit("b8"), 8, LLVMDWARFTypeEncoding_Boolean);
  556. case Basic_b16: return lb_debug_type_basic_type(m, str_lit("b16"), 16, LLVMDWARFTypeEncoding_Boolean);
  557. case Basic_b32: return lb_debug_type_basic_type(m, str_lit("b32"), 32, LLVMDWARFTypeEncoding_Boolean);
  558. case Basic_b64: return lb_debug_type_basic_type(m, str_lit("b64"), 64, LLVMDWARFTypeEncoding_Boolean);
  559. case Basic_i8: return lb_debug_type_basic_type(m, str_lit("i8"), 8, LLVMDWARFTypeEncoding_Signed);
  560. case Basic_u8: return lb_debug_type_basic_type(m, str_lit("u8"), 8, LLVMDWARFTypeEncoding_Unsigned);
  561. case Basic_i16: return lb_debug_type_basic_type(m, str_lit("i16"), 16, LLVMDWARFTypeEncoding_Signed);
  562. case Basic_u16: return lb_debug_type_basic_type(m, str_lit("u16"), 16, LLVMDWARFTypeEncoding_Unsigned);
  563. case Basic_i32: return lb_debug_type_basic_type(m, str_lit("i32"), 32, LLVMDWARFTypeEncoding_Signed);
  564. case Basic_u32: return lb_debug_type_basic_type(m, str_lit("u32"), 32, LLVMDWARFTypeEncoding_Unsigned);
  565. case Basic_i64: return lb_debug_type_basic_type(m, str_lit("i64"), 64, LLVMDWARFTypeEncoding_Signed);
  566. case Basic_u64: return lb_debug_type_basic_type(m, str_lit("u64"), 64, LLVMDWARFTypeEncoding_Unsigned);
  567. case Basic_i128: return lb_debug_type_basic_type(m, str_lit("i128"), 128, LLVMDWARFTypeEncoding_Signed);
  568. case Basic_u128: return lb_debug_type_basic_type(m, str_lit("u128"), 128, LLVMDWARFTypeEncoding_Unsigned);
  569. case Basic_rune: return lb_debug_type_basic_type(m, str_lit("rune"), 32, LLVMDWARFTypeEncoding_Utf);
  570. case Basic_f16: return lb_debug_type_basic_type(m, str_lit("f16"), 16, LLVMDWARFTypeEncoding_Float);
  571. case Basic_f32: return lb_debug_type_basic_type(m, str_lit("f32"), 32, LLVMDWARFTypeEncoding_Float);
  572. case Basic_f64: return lb_debug_type_basic_type(m, str_lit("f64"), 64, LLVMDWARFTypeEncoding_Float);
  573. case Basic_int: return lb_debug_type_basic_type(m, str_lit("int"), int_bits, LLVMDWARFTypeEncoding_Signed);
  574. case Basic_uint: return lb_debug_type_basic_type(m, str_lit("uint"), int_bits, LLVMDWARFTypeEncoding_Unsigned);
  575. case Basic_uintptr: return lb_debug_type_basic_type(m, str_lit("uintptr"), ptr_bits, LLVMDWARFTypeEncoding_Unsigned);
  576. case Basic_typeid:
  577. return lb_debug_type_basic_type(m, str_lit("typeid"), ptr_bits, LLVMDWARFTypeEncoding_Unsigned);
  578. // Endian Specific Types
  579. case Basic_i16le: return lb_debug_type_basic_type(m, str_lit("i16le"), 16, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagLittleEndian);
  580. case Basic_u16le: return lb_debug_type_basic_type(m, str_lit("u16le"), 16, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagLittleEndian);
  581. case Basic_i32le: return lb_debug_type_basic_type(m, str_lit("i32le"), 32, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagLittleEndian);
  582. case Basic_u32le: return lb_debug_type_basic_type(m, str_lit("u32le"), 32, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagLittleEndian);
  583. case Basic_i64le: return lb_debug_type_basic_type(m, str_lit("i64le"), 64, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagLittleEndian);
  584. case Basic_u64le: return lb_debug_type_basic_type(m, str_lit("u64le"), 64, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagLittleEndian);
  585. case Basic_i128le: return lb_debug_type_basic_type(m, str_lit("i128le"), 128, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagLittleEndian);
  586. case Basic_u128le: return lb_debug_type_basic_type(m, str_lit("u128le"), 128, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagLittleEndian);
  587. case Basic_f16le: return lb_debug_type_basic_type(m, str_lit("f16le"), 16, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  588. case Basic_f32le: return lb_debug_type_basic_type(m, str_lit("f32le"), 32, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  589. case Basic_f64le: return lb_debug_type_basic_type(m, str_lit("f64le"), 64, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  590. case Basic_i16be: return lb_debug_type_basic_type(m, str_lit("i16be"), 16, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagBigEndian);
  591. case Basic_u16be: return lb_debug_type_basic_type(m, str_lit("u16be"), 16, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagBigEndian);
  592. case Basic_i32be: return lb_debug_type_basic_type(m, str_lit("i32be"), 32, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagBigEndian);
  593. case Basic_u32be: return lb_debug_type_basic_type(m, str_lit("u32be"), 32, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagBigEndian);
  594. case Basic_i64be: return lb_debug_type_basic_type(m, str_lit("i64be"), 64, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagBigEndian);
  595. case Basic_u64be: return lb_debug_type_basic_type(m, str_lit("u64be"), 64, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagBigEndian);
  596. case Basic_i128be: return lb_debug_type_basic_type(m, str_lit("i128be"), 128, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagBigEndian);
  597. case Basic_u128be: return lb_debug_type_basic_type(m, str_lit("u128be"), 128, LLVMDWARFTypeEncoding_Unsigned, LLVMDIFlagBigEndian);
  598. case Basic_f16be: return lb_debug_type_basic_type(m, str_lit("f16be"), 16, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  599. case Basic_f32be: return lb_debug_type_basic_type(m, str_lit("f32be"), 32, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  600. case Basic_f64be: return lb_debug_type_basic_type(m, str_lit("f64be"), 64, LLVMDWARFTypeEncoding_Float, LLVMDIFlagLittleEndian);
  601. case Basic_complex32:
  602. {
  603. LLVMMetadataRef elements[2] = {};
  604. elements[0] = lb_debug_struct_field(m, str_lit("real"), t_f16, 0*16);
  605. elements[1] = lb_debug_struct_field(m, str_lit("imag"), t_f16, 1*16);
  606. return lb_debug_basic_struct(m, str_lit("complex32"), 64, 32, elements, gb_count_of(elements));
  607. }
  608. case Basic_complex64:
  609. {
  610. LLVMMetadataRef elements[2] = {};
  611. elements[0] = lb_debug_struct_field(m, str_lit("real"), t_f32, 0*32);
  612. elements[1] = lb_debug_struct_field(m, str_lit("imag"), t_f32, 2*32);
  613. return lb_debug_basic_struct(m, str_lit("complex64"), 64, 32, elements, gb_count_of(elements));
  614. }
  615. case Basic_complex128:
  616. {
  617. LLVMMetadataRef elements[2] = {};
  618. elements[0] = lb_debug_struct_field(m, str_lit("real"), t_f64, 0*64);
  619. elements[1] = lb_debug_struct_field(m, str_lit("imag"), t_f64, 1*64);
  620. return lb_debug_basic_struct(m, str_lit("complex128"), 128, 64, elements, gb_count_of(elements));
  621. }
  622. case Basic_quaternion64:
  623. {
  624. LLVMMetadataRef elements[4] = {};
  625. elements[0] = lb_debug_struct_field(m, str_lit("imag"), t_f16, 0*16);
  626. elements[1] = lb_debug_struct_field(m, str_lit("jmag"), t_f16, 1*16);
  627. elements[2] = lb_debug_struct_field(m, str_lit("kmag"), t_f16, 2*16);
  628. elements[3] = lb_debug_struct_field(m, str_lit("real"), t_f16, 3*16);
  629. return lb_debug_basic_struct(m, str_lit("quaternion64"), 128, 32, elements, gb_count_of(elements));
  630. }
  631. case Basic_quaternion128:
  632. {
  633. LLVMMetadataRef elements[4] = {};
  634. elements[0] = lb_debug_struct_field(m, str_lit("imag"), t_f32, 0*32);
  635. elements[1] = lb_debug_struct_field(m, str_lit("jmag"), t_f32, 1*32);
  636. elements[2] = lb_debug_struct_field(m, str_lit("kmag"), t_f32, 2*32);
  637. elements[3] = lb_debug_struct_field(m, str_lit("real"), t_f32, 3*32);
  638. return lb_debug_basic_struct(m, str_lit("quaternion128"), 128, 32, elements, gb_count_of(elements));
  639. }
  640. case Basic_quaternion256:
  641. {
  642. LLVMMetadataRef elements[4] = {};
  643. elements[0] = lb_debug_struct_field(m, str_lit("imag"), t_f64, 0*64);
  644. elements[1] = lb_debug_struct_field(m, str_lit("jmag"), t_f64, 1*64);
  645. elements[2] = lb_debug_struct_field(m, str_lit("kmag"), t_f64, 2*64);
  646. elements[3] = lb_debug_struct_field(m, str_lit("real"), t_f64, 3*64);
  647. return lb_debug_basic_struct(m, str_lit("quaternion256"), 256, 32, elements, gb_count_of(elements));
  648. }
  649. case Basic_rawptr:
  650. {
  651. LLVMMetadataRef void_type = lb_debug_type_basic_type(m, str_lit("void"), 8, LLVMDWARFTypeEncoding_Unsigned);
  652. return LLVMDIBuilderCreatePointerType(m->debug_builder, void_type, ptr_bits, ptr_bits, LLVMDWARFTypeEncoding_Address, "rawptr", 6);
  653. }
  654. case Basic_string:
  655. {
  656. LLVMMetadataRef elements[2] = {};
  657. elements[0] = lb_debug_struct_field(m, str_lit("data"), t_u8_ptr, 0);
  658. elements[1] = lb_debug_struct_field(m, str_lit("len"), t_int, int_bits);
  659. return lb_debug_basic_struct(m, str_lit("string"), 2*int_bits, int_bits, elements, gb_count_of(elements));
  660. }
  661. case Basic_cstring:
  662. {
  663. LLVMMetadataRef char_type = lb_debug_type_basic_type(m, str_lit("char"), 8, LLVMDWARFTypeEncoding_Unsigned);
  664. return LLVMDIBuilderCreatePointerType(m->debug_builder, char_type, ptr_bits, ptr_bits, 0, "cstring", 7);
  665. }
  666. case Basic_any:
  667. {
  668. LLVMMetadataRef elements[2] = {};
  669. elements[0] = lb_debug_struct_field(m, str_lit("data"), t_rawptr, 0);
  670. elements[1] = lb_debug_struct_field(m, str_lit("id"), t_typeid, ptr_bits);
  671. return lb_debug_basic_struct(m, str_lit("any"), 2*ptr_bits, ptr_bits, elements, gb_count_of(elements));
  672. }
  673. // Untyped types
  674. case Basic_UntypedBool: GB_PANIC("Basic_UntypedBool"); break;
  675. case Basic_UntypedInteger: GB_PANIC("Basic_UntypedInteger"); break;
  676. case Basic_UntypedFloat: GB_PANIC("Basic_UntypedFloat"); break;
  677. case Basic_UntypedComplex: GB_PANIC("Basic_UntypedComplex"); break;
  678. case Basic_UntypedQuaternion: GB_PANIC("Basic_UntypedQuaternion"); break;
  679. case Basic_UntypedString: GB_PANIC("Basic_UntypedString"); break;
  680. case Basic_UntypedRune: GB_PANIC("Basic_UntypedRune"); break;
  681. case Basic_UntypedNil: GB_PANIC("Basic_UntypedNil"); break;
  682. case Basic_UntypedUninit: GB_PANIC("Basic_UntypedUninit"); break;
  683. default: GB_PANIC("Basic Unhandled"); break;
  684. }
  685. break;
  686. case Type_Named:
  687. GB_PANIC("Type_Named should be handled in lb_debug_type separately");
  688. case Type_SoaPointer:
  689. return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->SoaPointer.elem), int_bits, int_bits, 0, nullptr, 0);
  690. case Type_Pointer:
  691. return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->Pointer.elem), ptr_bits, ptr_bits, 0, nullptr, 0);
  692. case Type_MultiPointer:
  693. return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->MultiPointer.elem), ptr_bits, ptr_bits, 0, nullptr, 0);
  694. case Type_Array: {
  695. LLVMMetadataRef subscripts[1] = {};
  696. subscripts[0] = LLVMDIBuilderGetOrCreateSubrange(m->debug_builder,
  697. 0ll,
  698. type->Array.count
  699. );
  700. return LLVMDIBuilderCreateArrayType(m->debug_builder,
  701. 8*cast(uint64_t)type_size_of(type),
  702. 8*cast(unsigned)type_align_of(type),
  703. lb_debug_type(m, type->Array.elem),
  704. subscripts, gb_count_of(subscripts));
  705. }
  706. case Type_EnumeratedArray: {
  707. LLVMMetadataRef subscripts[1] = {};
  708. subscripts[0] = LLVMDIBuilderGetOrCreateSubrange(m->debug_builder,
  709. 0ll,
  710. type->EnumeratedArray.count
  711. );
  712. LLVMMetadataRef array_type = LLVMDIBuilderCreateArrayType(m->debug_builder,
  713. 8*cast(uint64_t)type_size_of(type),
  714. 8*cast(unsigned)type_align_of(type),
  715. lb_debug_type(m, type->EnumeratedArray.elem),
  716. subscripts, gb_count_of(subscripts));
  717. gbString name = temp_canonical_string(type);
  718. return LLVMDIBuilderCreateTypedef(m->debug_builder, array_type, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type)));
  719. }
  720. case Type_Map: {
  721. init_map_internal_debug_types(type);
  722. Type *bt = base_type(type->Map.debug_metadata_type);
  723. GB_ASSERT(bt->kind == Type_Struct);
  724. return lb_debug_struct(m, type, bt, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  725. }
  726. case Type_Struct: return lb_debug_struct( m, type, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  727. case Type_Slice: return lb_debug_slice( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  728. case Type_DynamicArray: return lb_debug_dynamic_array(m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  729. case Type_Union: return lb_debug_union( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  730. case Type_BitSet: return lb_debug_bitset( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  731. case Type_Enum: return lb_debug_enum( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  732. case Type_BitField: return lb_debug_bitfield( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
  733. case Type_Tuple:
  734. if (type->Tuple.variables.count == 1) {
  735. return lb_debug_type(m, type->Tuple.variables[0]->type);
  736. } else {
  737. type_set_offsets(type);
  738. LLVMMetadataRef parent_scope = nullptr;
  739. LLVMMetadataRef scope = nullptr;
  740. LLVMMetadataRef file = nullptr;
  741. unsigned line = 0;
  742. u64 size_in_bits = 8*cast(u64)type_size_of(type);
  743. u32 align_in_bits = 8*cast(u32)type_align_of(type);
  744. LLVMDIFlags flags = LLVMDIFlagZero;
  745. unsigned element_count = cast(unsigned)type->Tuple.variables.count;
  746. LLVMMetadataRef *elements = gb_alloc_array(permanent_allocator(), LLVMMetadataRef, element_count);
  747. for (unsigned i = 0; i < element_count; i++) {
  748. Entity *f = type->Tuple.variables[i];
  749. GB_ASSERT(f->kind == Entity_Variable);
  750. String name = f->token.string;
  751. unsigned field_line = 0;
  752. LLVMDIFlags field_flags = LLVMDIFlagZero;
  753. u64 offset_in_bits = 8*cast(u64)type->Tuple.offsets[i];
  754. elements[i] = LLVMDIBuilderCreateMemberType(m->debug_builder, scope, cast(char const *)name.text, name.len, file, field_line,
  755. 8*cast(u64)type_size_of(f->type), 8*cast(u32)type_align_of(f->type), offset_in_bits,
  756. field_flags, lb_debug_type(m, f->type)
  757. );
  758. }
  759. return LLVMDIBuilderCreateStructType(m->debug_builder, parent_scope, "", 0, file, line,
  760. size_in_bits, align_in_bits, flags,
  761. nullptr, elements, element_count, 0, nullptr,
  762. "", 0
  763. );
  764. }
  765. case Type_Proc:
  766. {
  767. LLVMMetadataRef proc_underlying_type = lb_debug_type_internal_proc(m, type);
  768. LLVMMetadataRef pointer_type = LLVMDIBuilderCreatePointerType(m->debug_builder, proc_underlying_type, ptr_bits, ptr_bits, 0, nullptr, 0);
  769. gbString name = temp_canonical_string(type);
  770. return LLVMDIBuilderCreateTypedef(m->debug_builder, pointer_type, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type)));
  771. }
  772. break;
  773. case Type_SimdVector:
  774. {
  775. LLVMMetadataRef elem = lb_debug_type(m, type->SimdVector.elem);
  776. LLVMMetadataRef subscripts[1] = {};
  777. subscripts[0] = LLVMDIBuilderGetOrCreateSubrange(m->debug_builder,
  778. 0ll,
  779. type->SimdVector.count
  780. );
  781. return LLVMDIBuilderCreateVectorType(
  782. m->debug_builder,
  783. 8*cast(unsigned)type_size_of(type), 8*cast(unsigned)type_align_of(type),
  784. elem, subscripts, gb_count_of(subscripts));
  785. }
  786. case Type_Matrix: {
  787. LLVMMetadataRef subscripts[1] = {};
  788. subscripts[0] = LLVMDIBuilderGetOrCreateSubrange(m->debug_builder,
  789. 0ll,
  790. matrix_type_total_internal_elems(type)
  791. );
  792. return LLVMDIBuilderCreateArrayType(m->debug_builder,
  793. 8*cast(uint64_t)type_size_of(type),
  794. 8*cast(unsigned)type_align_of(type),
  795. lb_debug_type(m, type->Matrix.elem),
  796. subscripts, gb_count_of(subscripts));
  797. }
  798. }
  799. GB_PANIC("Invalid type %s", type_to_string(type));
  800. return nullptr;
  801. }
  802. gb_internal LLVMMetadataRef lb_get_base_scope_metadata(lbModule *m, Scope *scope) {
  803. LLVMMetadataRef found = nullptr;
  804. for (;;) {
  805. if (scope == nullptr) {
  806. return nullptr;
  807. }
  808. if (scope->flags & ScopeFlag_Proc) {
  809. found = lb_get_llvm_metadata(m, scope->procedure_entity);
  810. if (found) {
  811. return found;
  812. }
  813. }
  814. if (scope->flags & ScopeFlag_File) {
  815. found = lb_get_llvm_metadata(m, scope->file);
  816. if (found) {
  817. return found;
  818. }
  819. }
  820. scope = scope->parent;
  821. }
  822. }
  823. gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) {
  824. GB_ASSERT(type != nullptr);
  825. LLVMMetadataRef found = lb_get_llvm_metadata(m, type);
  826. if (found != nullptr) {
  827. return found;
  828. }
  829. MUTEX_GUARD(&m->debug_values_mutex);
  830. if (type->kind == Type_Named) {
  831. LLVMMetadataRef file = nullptr;
  832. unsigned line = 0;
  833. LLVMMetadataRef scope = nullptr;
  834. if (type->Named.type_name != nullptr) {
  835. Entity *e = type->Named.type_name;
  836. scope = lb_get_base_scope_metadata(m, e->scope);
  837. if (scope != nullptr) {
  838. file = LLVMDIScopeGetFile(scope);
  839. }
  840. line = cast(unsigned)e->token.pos.line;
  841. }
  842. String name = type_to_canonical_string(temporary_allocator(), type);
  843. Type *bt = base_type(type->Named.base);
  844. switch (bt->kind) {
  845. default: {
  846. u32 align_in_bits = 8*cast(u32)type_align_of(type);
  847. LLVMMetadataRef debug_bt = lb_debug_type(m, bt);
  848. LLVMMetadataRef final_decl = LLVMDIBuilderCreateTypedef(
  849. m->debug_builder,
  850. debug_bt,
  851. cast(char const *)name.text, cast(size_t)name.len,
  852. file, line, scope, align_in_bits
  853. );
  854. lb_set_llvm_metadata(m, type, final_decl);
  855. return final_decl;
  856. }
  857. case Type_Map: {
  858. init_map_internal_debug_types(bt);
  859. bt = base_type(bt->Map.debug_metadata_type);
  860. GB_ASSERT(bt->kind == Type_Struct);
  861. return lb_debug_struct(m, type, bt, name, scope, file, line);
  862. }
  863. case Type_Struct: return lb_debug_struct(m, type, bt, name, scope, file, line);
  864. case Type_Slice: return lb_debug_slice(m, type, name, scope, file, line);
  865. case Type_DynamicArray: return lb_debug_dynamic_array(m, type, name, scope, file, line);
  866. case Type_Union: return lb_debug_union(m, type, name, scope, file, line);
  867. case Type_BitSet: return lb_debug_bitset(m, type, name, scope, file, line);
  868. case Type_Enum: return lb_debug_enum(m, type, name, scope, file, line);
  869. case Type_BitField: return lb_debug_bitfield(m, type, name, scope, file, line);
  870. }
  871. }
  872. LLVMMetadataRef dt = lb_debug_type_internal(m, type);
  873. lb_set_llvm_metadata(m, type, dt);
  874. return dt;
  875. }
  876. gb_internal void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token) {
  877. if (p->debug_info == nullptr) {
  878. return;
  879. }
  880. if (type == nullptr) {
  881. return;
  882. }
  883. if (type == t_invalid) {
  884. return;
  885. }
  886. if (p->body == nullptr) {
  887. return;
  888. }
  889. lbModule *m = p->module;
  890. String const &name = token.string;
  891. if (name == "" || name == "_") {
  892. return;
  893. }
  894. if (lb_get_llvm_metadata(m, ptr) != nullptr) {
  895. // Already been set
  896. return;
  897. }
  898. AstFile *file = p->body->file();
  899. LLVMMetadataRef llvm_scope = lb_get_current_debug_scope(p);
  900. LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, file);
  901. GB_ASSERT(llvm_scope != nullptr);
  902. if (llvm_file == nullptr) {
  903. llvm_file = LLVMDIScopeGetFile(llvm_scope);
  904. }
  905. if (llvm_file == nullptr) {
  906. return;
  907. }
  908. unsigned alignment_in_bits = cast(unsigned)(8*type_align_of(type));
  909. LLVMDIFlags flags = LLVMDIFlagZero;
  910. LLVMBool always_preserve = build_context.optimization_level == 0;
  911. LLVMMetadataRef debug_type = lb_debug_type(m, type);
  912. LLVMMetadataRef var_info = LLVMDIBuilderCreateAutoVariable(
  913. m->debug_builder, llvm_scope,
  914. cast(char const *)name.text, cast(size_t)name.len,
  915. llvm_file, token.pos.line,
  916. debug_type,
  917. always_preserve, flags, alignment_in_bits
  918. );
  919. LLVMValueRef storage = ptr;
  920. LLVMBasicBlockRef block = p->curr_block->block;
  921. LLVMMetadataRef llvm_debug_loc = lb_debug_location_from_token_pos(p, token.pos);
  922. LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
  923. lb_set_llvm_metadata(m, ptr, llvm_expr);
  924. #if LLVM_VERSION_MAJOR <= 18
  925. LLVMDIBuilderInsertDeclareAtEnd(m->debug_builder, storage, var_info, llvm_expr, llvm_debug_loc, block);
  926. #else
  927. LLVMDIBuilderInsertDbgValueRecordAtEnd(m->debug_builder, storage, var_info, llvm_expr, llvm_debug_loc, block);
  928. #endif
  929. }
  930. gb_internal void lb_add_debug_param_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token, unsigned arg_number, lbBlock *block) {
  931. if (p->debug_info == nullptr) {
  932. return;
  933. }
  934. if (type == nullptr) {
  935. return;
  936. }
  937. if (type == t_invalid) {
  938. return;
  939. }
  940. if (p->body == nullptr) {
  941. return;
  942. }
  943. lbModule *m = p->module;
  944. String const &name = token.string;
  945. if (name == "" || name == "_") {
  946. return;
  947. }
  948. if (lb_get_llvm_metadata(m, ptr) != nullptr) {
  949. // Already been set
  950. return;
  951. }
  952. AstFile *file = p->body->file();
  953. LLVMMetadataRef llvm_scope = lb_get_current_debug_scope(p);
  954. LLVMMetadataRef llvm_file = lb_get_llvm_metadata(m, file);
  955. GB_ASSERT(llvm_scope != nullptr);
  956. if (llvm_file == nullptr) {
  957. llvm_file = LLVMDIScopeGetFile(llvm_scope);
  958. }
  959. if (llvm_file == nullptr) {
  960. return;
  961. }
  962. LLVMDIFlags flags = LLVMDIFlagZero;
  963. LLVMBool always_preserve = build_context.optimization_level == 0;
  964. LLVMMetadataRef debug_type = lb_debug_type(m, type);
  965. LLVMMetadataRef var_info = LLVMDIBuilderCreateParameterVariable(
  966. m->debug_builder, llvm_scope,
  967. cast(char const *)name.text, cast(size_t)name.len,
  968. arg_number,
  969. llvm_file, token.pos.line,
  970. debug_type,
  971. always_preserve, flags
  972. );
  973. LLVMValueRef storage = ptr;
  974. LLVMMetadataRef llvm_debug_loc = lb_debug_location_from_token_pos(p, token.pos);
  975. LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
  976. lb_set_llvm_metadata(m, ptr, llvm_expr);
  977. // NOTE(bill, 2022-02-01): For parameter values, you must insert them at the end of the decl block
  978. // The reason is that if the parameter is at index 0 and a pointer, there is not such things as an
  979. // instruction "before" it.
  980. LLVMDIBuilderInsertDeclareAtEnd(m->debug_builder, storage, var_info, llvm_expr, llvm_debug_loc, block->block);
  981. }
  982. gb_internal void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx) {
  983. if (!p->debug_info || !p->body) {
  984. return;
  985. }
  986. LLVMMetadataRef loc = LLVMGetCurrentDebugLocation2(p->builder);
  987. if (!loc) {
  988. return;
  989. }
  990. TokenPos pos = {};
  991. pos.file_id = p->body->file_id;
  992. pos.line = LLVMDILocationGetLine(loc);
  993. pos.column = LLVMDILocationGetColumn(loc);
  994. Token token = {};
  995. token.kind = Token_context;
  996. token.string = str_lit("context");
  997. token.pos = pos;
  998. LLVMValueRef ptr = ctx.addr.value;
  999. while (LLVMIsABitCastInst(ptr)) {
  1000. ptr = LLVMGetOperand(ptr, 0);
  1001. }
  1002. lb_add_debug_local_variable(p, ptr, t_context, token);
  1003. }
  1004. gb_internal String debug_info_mangle_constant_name(Entity *e, gbAllocator const &allocator, bool *did_allocate_) {
  1005. String name = e->token.string;
  1006. if (e->pkg && e->pkg->name.len > 0) {
  1007. gbString s = string_canonical_entity_name(allocator, e);
  1008. name = make_string(cast(u8 const *)s, gb_string_length(s));
  1009. if (did_allocate_) *did_allocate_ = true;
  1010. }
  1011. return name;
  1012. }
  1013. gb_internal void add_debug_info_global_variable_expr(lbModule *m, String const &name, LLVMMetadataRef dtype, LLVMMetadataRef expr) {
  1014. LLVMMetadataRef scope = nullptr;
  1015. LLVMMetadataRef file = nullptr;
  1016. unsigned line = 0;
  1017. LLVMMetadataRef decl = nullptr;
  1018. LLVMDIBuilderCreateGlobalVariableExpression(
  1019. m->debug_builder, scope,
  1020. cast(char const *)name.text, cast(size_t)name.len,
  1021. "", 0, // Linkage
  1022. file, line, dtype,
  1023. false, // local to unit
  1024. expr, decl, 8/*AlignInBits*/);
  1025. }
  1026. gb_internal void add_debug_info_for_global_constant_internal_i64(lbModule *m, Entity *e, LLVMMetadataRef dtype, i64 v) {
  1027. LLVMMetadataRef expr = LLVMDIBuilderCreateConstantValueExpression(m->debug_builder, v);
  1028. TEMPORARY_ALLOCATOR_GUARD();
  1029. String name = debug_info_mangle_constant_name(e, temporary_allocator(), nullptr);
  1030. add_debug_info_global_variable_expr(m, name, dtype, expr);
  1031. if ((e->pkg && e->pkg->kind == Package_Init) ||
  1032. (e->scope && (e->scope->flags & ScopeFlag_Global))) {
  1033. add_debug_info_global_variable_expr(m, e->token.string, dtype, expr);
  1034. }
  1035. }
  1036. gb_internal void add_debug_info_for_global_constant_from_entity(lbGenerator *gen, Entity *e) {
  1037. if (e == nullptr || e->kind != Entity_Constant) {
  1038. return;
  1039. }
  1040. if (is_blank_ident(e->token)) {
  1041. return;
  1042. }
  1043. lbModule *m = &gen->default_module;
  1044. if (USE_SEPARATE_MODULES) {
  1045. m = lb_module_of_entity(gen, e);
  1046. }
  1047. GB_ASSERT(m != nullptr);
  1048. if (is_type_integer(e->type)) {
  1049. ExactValue const &value = e->Constant.value;
  1050. if (value.kind == ExactValue_Integer) {
  1051. LLVMMetadataRef dtype = nullptr;
  1052. i64 v = 0;
  1053. bool is_signed = false;
  1054. if (big_int_is_neg(&value.value_integer)) {
  1055. v = exact_value_to_i64(value);
  1056. is_signed = true;
  1057. } else {
  1058. v = cast(i64)exact_value_to_u64(value);
  1059. }
  1060. if (is_type_untyped(e->type)) {
  1061. dtype = lb_debug_type(m, is_signed ? t_i64 : t_u64);
  1062. } else {
  1063. dtype = lb_debug_type(m, e->type);
  1064. }
  1065. add_debug_info_for_global_constant_internal_i64(m, e, dtype, v);
  1066. }
  1067. } else if (is_type_rune(e->type)) {
  1068. ExactValue const &value = e->Constant.value;
  1069. if (value.kind == ExactValue_Integer) {
  1070. LLVMMetadataRef dtype = lb_debug_type(m, t_rune);
  1071. i64 v = exact_value_to_i64(value);
  1072. add_debug_info_for_global_constant_internal_i64(m, e, dtype, v);
  1073. }
  1074. } else if (is_type_boolean(e->type)) {
  1075. ExactValue const &value = e->Constant.value;
  1076. if (value.kind == ExactValue_Bool) {
  1077. LLVMMetadataRef dtype = lb_debug_type(m, default_type(e->type));
  1078. i64 v = cast(i64)value.value_bool;
  1079. add_debug_info_for_global_constant_internal_i64(m, e, dtype, v);
  1080. }
  1081. } else if (is_type_enum(e->type)) {
  1082. ExactValue const &value = e->Constant.value;
  1083. if (value.kind == ExactValue_Integer) {
  1084. LLVMMetadataRef dtype = lb_debug_type(m, default_type(e->type));
  1085. i64 v = 0;
  1086. if (big_int_is_neg(&value.value_integer)) {
  1087. v = exact_value_to_i64(value);
  1088. } else {
  1089. v = cast(i64)exact_value_to_u64(value);
  1090. }
  1091. add_debug_info_for_global_constant_internal_i64(m, e, dtype, v);
  1092. }
  1093. } else if (is_type_pointer(e->type)) {
  1094. ExactValue const &value = e->Constant.value;
  1095. if (value.kind == ExactValue_Integer) {
  1096. LLVMMetadataRef dtype = lb_debug_type(m, default_type(e->type));
  1097. i64 v = cast(i64)exact_value_to_u64(value);
  1098. add_debug_info_for_global_constant_internal_i64(m, e, dtype, v);
  1099. }
  1100. }
  1101. }