llvm_backend_debug.cpp 45 KB

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