llvm_backend_debug.cpp 51 KB

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