dbgllvm.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  1. {
  2. Copyright (c) 2021-2022 by Jonas Maebe,
  3. member of the Free Pascal Compiler development team
  4. This units contains support for LLVM debug info generation
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {
  19. This units contains support for LLVM debug info generation.
  20. LLVM debug information is stored as metadata in the LLVM bitcode, and is
  21. loosely based on DWARF (it also reuses some DWARF constants)
  22. }
  23. unit dbgllvm;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cclasses,globtype,
  28. cgbase,
  29. aasmbase,aasmtai,aasmdata,aasmcnst,aasmllvm,aasmllvmmetadata,
  30. symbase,symconst,symtype,symdef,symsym,
  31. finput,
  32. DbgBase, dbgdwarfconst;
  33. type
  34. TLLVMMetaDefHashSetItem = record
  35. { HashSetItem.Data: LLVM metadata which other types reference when
  36. referring to this type (usually a typedef) }
  37. HashSetItem: THashSetItem;
  38. { in case of a class, the field layout (since a class itself is just a
  39. pointer }
  40. struct_metadef: tai_llvmspecialisedmetadatanode;
  41. { the metadata actually containing the type definition (usually
  42. referenced by HashSetItem.Data), filled in by appenddef_* }
  43. implmetadef: tai_llvmspecialisedmetadatanode;
  44. end;
  45. PLLVMMetaDefHashSetItem = ^TLLVMMetaDefHashSetItem;
  46. TLLVMMetaDefHashSet = class(THashSet)
  47. class function SizeOfItem: Integer; override;
  48. end;
  49. TDebugInfoLLVM = class(TDebugInfo)
  50. strict private
  51. type
  52. tmembercallbackinfo = record
  53. structnode: tai_llvmspecialisedmetadatanode;
  54. list: tasmlist;
  55. end;
  56. pmembercallbackinfo = ^tmembercallbackinfo;
  57. var
  58. { lookup table for def -> LLVMMeta info }
  59. fdefmeta: TLLVMMetaDefHashSet;
  60. { lookup table for file -> LLVMMeta info (DIFile) }
  61. ffilemeta: THashSet;
  62. { lookup table for line,column,scope -> LLVMMeta info (DILocation) }
  63. flocationmeta: THashSet;
  64. { lookup table for scope,file -> LLVMMeta info (DILexicalBlockFile, for include files) }
  65. flexicalblockfilemeta: THashSet;
  66. { lookup table for tsym -> taillvmdecl }
  67. fsymdecl: THashSet;
  68. fcunode: tai_llvmspecialisedmetadatanode;
  69. fenums: tai_llvmunnamedmetadatanode;
  70. fretainedtypes: tai_llvmunnamedmetadatanode;
  71. fglobals: tai_llvmunnamedmetadatanode;
  72. { reusable empty expression node }
  73. femptyexpression: tai_llvmspecialisedmetadatanode;
  74. function absolute_llvm_path(const s:tcmdstr):tcmdstr;
  75. protected
  76. vardatadef: trecorddef;
  77. procedure try_add_file_metaref(dinode: tai_llvmspecialisedmetadatanode; const fileinfo: tfileposinfo; includescope: boolean);
  78. function add_line_metanode(const fileinfo: tfileposinfo): tai_llvmspecialisedmetadatanode;
  79. function def_meta_impl(def: tdef) : tai_llvmspecialisedmetadatanode;
  80. function def_set_meta_impl(def: tdef; meta_impl: tai_llvmspecialisedmetadatanode): tai_llvmspecialisedmetadatanode;
  81. function def_meta_class_struct(def: tobjectdef) : tai_llvmbasemetadatanode;
  82. function def_meta_node(def: tdef): tai_llvmspecialisedmetadatanode;
  83. function def_meta_ref(def: tdef): tai_simpletypedconst;
  84. function file_getmetanode(moduleindex: tfileposmoduleindex; fileindex: tfileposfileindex): tai_llvmspecialisedmetadatanode;
  85. function filepos_getmetanode(const filepos: tfileposinfo; const functionfileinfo: tfileposinfo; const functionscope: tai_llvmspecialisedmetadatanode; nolineinfo: boolean): tai_llvmspecialisedmetadatanode;
  86. function get_def_metatai(def:tdef): PLLVMMetaDefHashSetItem;
  87. procedure sym_set_decl(sym: tsym; decl: tai);
  88. function sym_get_decl(sym: tsym): taillvmdecl;
  89. procedure appenddef_array_internal(list: TAsmList; fordef: tdef; eledef: tdef; lowrange, highrange: asizeint);
  90. function getabstractprocdeftypes(list: TAsmList; def:tabstractprocdef): tai_llvmbasemetadatanode;
  91. procedure afterappenddef(list: TAsmList; def: tdef); override;
  92. procedure appenddef_ord(list:TAsmList;def:torddef);override;
  93. procedure appenddef_float(list:TAsmList;def:tfloatdef);override;
  94. procedure appenddef_enum(list:TAsmList;def:tenumdef);override;
  95. procedure appenddef_array(list:TAsmList;def:tarraydef);override;
  96. procedure appenddef_record_named(list: TAsmList; fordef: tdef; def: trecorddef; const name: TSymStr);
  97. procedure appenddef_record(list:TAsmList;def:trecorddef);override;
  98. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
  99. procedure appenddef_formal(list:TAsmList;def:tformaldef); override;
  100. procedure appenddef_string(list:TAsmList;def:tstringdef);override;
  101. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
  102. procedure appenddef_file(list:TAsmList;def:tfiledef); override;
  103. procedure appenddef_object(list:TAsmList;def:tobjectdef); override;
  104. procedure appenddef_set(list:TAsmList;def:tsetdef); override;
  105. procedure appenddef_undefined(list:TAsmList;def:tundefineddef); override;
  106. procedure appenddef_classref(list: TAsmList; def: tclassrefdef); override;
  107. procedure appenddef_variant(list:TAsmList;def:tvariantdef); override;
  108. procedure appendprocdef(list:TAsmList;def:tprocdef);override;
  109. procedure adddefinitionlocal(dinode: tai_llvmspecialisedmetadatanode; definition, local, usedispflags: boolean; out dispFlags: tsymstr);
  110. function get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  111. procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  112. procedure appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: TSymStr; def: tdef; offset: pint(*; const flags: tdwarfvarsymflags*));
  113. { used for fields and properties mapped to fields }
  114. procedure appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  115. procedure appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  116. procedure beforeappendsym(list:TAsmList;sym:tsym);override;
  117. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  118. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  119. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  120. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  121. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  122. procedure appendsym_type(list:TAsmList;sym:ttypesym);override;
  123. procedure appendsym_label(list:TAsmList;sym:tlabelsym);override;
  124. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
  125. procedure appendsym_property(list:TAsmList;sym:tpropertysym);override;
  126. function symdebugname(sym:tsym): TSymStr;
  127. function symname(sym: tsym; manglename: boolean): TSymStr; virtual;
  128. function visibilitydiflag(vis: tvisibility): TSymStr;
  129. procedure enum_membersyms_callback(p:TObject;arg:pointer);
  130. procedure ensuremetainit;
  131. procedure resetfornewmodule;
  132. procedure collectglobalsyms;
  133. public
  134. constructor Create;override;
  135. destructor Destroy;override;
  136. procedure insertmoduleinfo;override;
  137. procedure inserttypeinfo;override;
  138. procedure insertlineinfo(list:TAsmList);override;
  139. function dwarf_version: Word; virtual; abstract;
  140. end;
  141. implementation
  142. uses
  143. sysutils,cutils,cfileutl,constexp,
  144. version,globals,verbose,systems,
  145. cpubase,cpuinfo,paramgr,
  146. fmodule,
  147. defutil,symtable,symcpu,ppu,
  148. llvminfo,llvmbase
  149. ;
  150. {$push}
  151. {$scopedenums on}
  152. type
  153. TLLVMDIFlags = (
  154. DIFlagNone = 0,
  155. DIFlagPrivate = 1,
  156. DIFlagProtected = 2,
  157. DIFlagPublic = 3,
  158. DIFlagFwdDecl = 1 shl 2,
  159. DIFlagAppleBlock = 1 shl 3,
  160. DIFlagReservedBit4 = 1 shl 4,
  161. { virtual inheritance at the C++ struct level, not at method level; use the SPFlag for that virtual methods) }
  162. DIFlagVirtual = 1 shl 5,
  163. DIFlagArtificial = 1 shl 6,
  164. DIFlagExplicit = 1 shl 7,
  165. DIFlagPrototyped = 1 shl 8,
  166. DIFlagObjcClassComplete = 1 shl 9,
  167. DIFlagObjectPointer = 1 shl 10,
  168. DIFlagVector = 1 shl 11,
  169. DIFlagStaticMember = 1 shl 12,
  170. DIFlagLValueReference = 1 shl 13,
  171. DIFlagRValueReference = 1 shl 14,
  172. DIFlagReserved = 1 shl 15,
  173. DIFlagSingleInheritance = 1 shl 16,
  174. DIFlagMultipleInheritance = 1 shl 17,
  175. DIFlagVirtualInheritance = 1 shl 18,
  176. DIFlagIntroducedVirtual = 1 shl 19,
  177. DIFlagBitField = 1 shl 20,
  178. DIFlagNoReturn = 1 shl 21,
  179. { at the type level, DWARF 5 DW_CC_pass_by_value }
  180. DIFlagTypePassByValue = 1 shl 22,
  181. { at the type level, DWARF 5 DW_CC_pass_by_reference }
  182. DIFlagTypePassByReference = 1 shl 23,
  183. DIFlagEnumClass = 1 shl 24,
  184. DIFlagThunk = 1 shl 25,
  185. { moved to DISPFlags in LLVM 8.0 }
  186. DIFlagMainSubprogram_Deprecated = 1 shl 21
  187. { introduced/renamed after LLVM 7.0, but nothing we need right now
  188. ,
  189. DIFlagNonTrivial,
  190. DIFlagBigEndian,
  191. DIFlagLittleEndian
  192. }
  193. );
  194. TLLVMDISPFlags = (
  195. DISPFlagVirtual = 1,
  196. DISPFlagPureVirtual = 2,
  197. DISPFlagLocalToUnit = 1 shl 2,
  198. DISPFlagDefinition = 1 shl 3,
  199. DISPFlagOptimized = 1 shl 4,
  200. DISPFlagPure = 1 shl 5,
  201. DISPFlagElemental = 1 shl 6,
  202. DISPFlagRecursive = 1 shl 7,
  203. DISPFlagMainSubprogram = 1 shl 8,
  204. DISPFlagDeleted = 1 shl 9,
  205. DISPFlagObjCDirect = 1 shl 11
  206. );
  207. {$pop}
  208. TLLVMLocationAtom = (
  209. DW_OP_LLVM_fragment = $1000, ///< Only used in LLVM metadata.
  210. DW_OP_LLVM_convert = $1001, ///< Only used in LLVM metadata.
  211. DW_OP_LLVM_tag_offset = $1002, ///< Only used in LLVM metadata.
  212. DW_OP_LLVM_entry_value = $1003, ///< Only used in LLVM metadata.
  213. DW_OP_LLVM_implicit_pointer = $1004, ///< Only used in LLVM metadata.
  214. DW_OP_LLVM_arg = $1005 ///< Only used in LLVM metadata.
  215. );
  216. {****************************************************************************
  217. TLLVMMetaDefHashSet
  218. ****************************************************************************}
  219. class function TLLVMMetaDefHashSet.SizeOfItem: Integer;
  220. begin
  221. Result:=sizeof(TLLVMMetaDefHashSetItem);
  222. end;
  223. {****************************************************************************
  224. TDebugInfoLLVM
  225. ****************************************************************************}
  226. function TDebugInfoLLVM.absolute_llvm_path(const s:tcmdstr):tcmdstr;
  227. begin
  228. { Remove trailing / and ./ prefixes and always use a / }
  229. result:=BsToSlash(ExcludeTrailingPathDelimiter(FixFileName(ExpandFileName(s))));
  230. end;
  231. function TDebugInfoLLVM.get_def_metatai(def:tdef): PLLVMMetaDefHashSetItem;
  232. var
  233. needstructdeflab: boolean;
  234. begin
  235. if def.dbg_state=dbg_state_unused then
  236. def.dbg_state:=dbg_state_used;
  237. { Need a new meta item? }
  238. result:=PLLVMMetaDefHashSetItem(fdefmeta.FindOrAdd(@def,sizeof(def)));
  239. { the other fields besides Data are not initialised }
  240. if not assigned(result^.HashSetItem.Data) then
  241. begin
  242. { will be turned into a pointerdef (in case of Objective-C types) or
  243. typedef later on. We only really need a typedef if this def has
  244. a typesym (to add the name), but it allows us to create a generic
  245. specialised metatype node that can represent any type. Otherwise
  246. we have to duplicate the logic here to determine whether it's a
  247. basic, derived or composite type.
  248. exception: procdefs because we cannot make typedefs for those}
  249. if def.typ<>procdef then
  250. begin
  251. result^.HashSetItem.Data:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType);
  252. if is_implicit_pointer_object_type(def) then
  253. result^.struct_metadef:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType)
  254. else
  255. result^.struct_metadef:=nil;
  256. result^.implmetadef:=nil;
  257. end
  258. else
  259. begin
  260. result^.HashSetItem.Data:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubprogram);
  261. result^.struct_metadef:=nil;
  262. result^.implmetadef:=nil;
  263. end;
  264. if def.dbg_state=dbg_state_used then
  265. deftowritelist.Add(def);
  266. defnumberlist.Add(def);
  267. end;
  268. end;
  269. procedure TDebugInfoLLVM.sym_set_decl(sym: tsym; decl: tai);
  270. var
  271. entry: PHashSetItem;
  272. begin
  273. entry:=fsymdecl.FindOrAdd(@sym,sizeof(sym));
  274. if assigned(entry^.Data) then
  275. internalerror(2022051701);
  276. entry^.Data:=decl;
  277. end;
  278. function TDebugInfoLLVM.sym_get_decl(sym: tsym): taillvmdecl;
  279. var
  280. entry: PHashSetItem;
  281. begin
  282. result:=nil;
  283. entry:=fsymdecl.Find(@sym,sizeof(sym));
  284. if assigned(entry) then
  285. result:=taillvmdecl(entry^.Data);
  286. end;
  287. procedure TDebugInfoLLVM.appenddef_array_internal(list: TAsmList; fordef: tdef; eledef: tdef; lowrange, highrange: asizeint);
  288. var
  289. dinode,
  290. subrangenode,
  291. exprnode: tai_llvmspecialisedmetadatanode;
  292. arrayrangenode: tai_llvmunnamedmetadatanode;
  293. begin
  294. { range of the array }
  295. subrangenode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubrange);
  296. { include length }
  297. subrangenode.addqword('lowerBound',lowRange);
  298. if highrange>=0 then
  299. subrangenode.addqword('count',qword(highRange)+1)
  300. else
  301. subrangenode.addint64('count',highRange+1);
  302. list.concat(subrangenode);
  303. { collection containing the one range }
  304. arrayrangenode:=tai_llvmunnamedmetadatanode.create;
  305. arrayrangenode.addvalue(llvm_getmetadatareftypedconst(subrangenode));
  306. list.concat(arrayrangenode);
  307. { the array definition }
  308. dinode:=def_set_meta_impl(fordef,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  309. dinode.addqword('tag',ord(DW_TAG_array_type));
  310. dinode.addmetadatarefto('baseType',def_meta_node(eledef));
  311. dinode.addqword('size',eledef.size*(highrange-lowrange+1)*8);
  312. dinode.addmetadatarefto('elements',arrayrangenode);
  313. list.concat(dinode);
  314. end;
  315. function TDebugInfoLLVM.getabstractprocdeftypes(list: TAsmList; def: tabstractprocdef): tai_llvmbasemetadatanode;
  316. var
  317. types: tai_llvmunnamedmetadatanode;
  318. i: longint;
  319. begin
  320. types:=tai_llvmunnamedmetadatanode.create;
  321. list.concat(types);
  322. { we still need a DISubProgramType in this case, but not the list of types }
  323. if not(cs_debuginfo in current_settings.moduleswitches) then
  324. exit;
  325. if is_void(def.returndef) then
  326. types.addvalue(tai_simpletypedconst.create(llvm_metadatatype,nil))
  327. else
  328. types.addvalue(def_meta_ref(def.returndef));
  329. for i:=0 to def.paras.count-1 do
  330. begin
  331. types.addvalue(def_meta_ref(tparavarsym(def.paras[i]).vardef));
  332. end;
  333. result:=types;
  334. end;
  335. function TDebugInfoLLVM.def_meta_impl(def: tdef): tai_llvmspecialisedmetadatanode;
  336. begin
  337. result:=tai_llvmspecialisedmetadatanode(get_def_metatai(def)^.implmetadef);
  338. end;
  339. function TDebugInfoLLVM.def_set_meta_impl(def: tdef; meta_impl: tai_llvmspecialisedmetadatanode): tai_llvmspecialisedmetadatanode;
  340. begin
  341. tai_llvmspecialisedmetadatanode(get_def_metatai(def)^.implmetadef):=meta_impl;
  342. result:=meta_impl;
  343. end;
  344. function TDebugInfoLLVM.def_meta_class_struct(def: tobjectdef): tai_llvmbasemetadatanode;
  345. begin
  346. result:=tai_llvmbasemetadatanode(get_def_metatai(def)^.struct_metadef);
  347. end;
  348. function TDebugInfoLLVM.def_meta_node(def: tdef): tai_llvmspecialisedmetadatanode;
  349. begin
  350. if not is_void(def) then
  351. result:=tai_llvmspecialisedmetadatanode(get_def_metatai(def)^.HashSetItem.Data)
  352. else
  353. result:=nil;
  354. end;
  355. function TDebugInfoLLVM.def_meta_ref(def: tdef): tai_simpletypedconst;
  356. begin
  357. result:=llvm_getmetadatareftypedconst(def_meta_node(def));
  358. end;
  359. constructor TDebugInfoLLVM.Create;
  360. begin
  361. inherited Create;
  362. fenums:=nil;
  363. fretainedtypes:=nil;
  364. fglobals:=nil;
  365. femptyexpression:=nil;
  366. fcunode:=nil;
  367. ffilemeta:=thashset.Create(10000,true,false);
  368. flocationmeta:=thashset.Create(10000,true,false);
  369. flexicalblockfilemeta:=thashset.Create(100,true,false);
  370. fdefmeta:=TLLVMMetaDefHashSet.Create(10000,true,false);
  371. fsymdecl:=thashset.create(10000,true,false);
  372. defnumberlist:=TFPObjectList.create(false);
  373. deftowritelist:=TFPObjectList.create(false);
  374. vardatadef:=nil;
  375. end;
  376. destructor TDebugInfoLLVM.Destroy;
  377. begin
  378. // don't free fenums/fretainedtypes/fglobals, they get emitted in the assembler list
  379. ffilemeta.free;
  380. ffilemeta:=nil;
  381. flocationmeta.free;
  382. flocationmeta:=nil;
  383. flexicalblockfilemeta.free;
  384. flexicalblockfilemeta:=nil;
  385. fdefmeta.free;
  386. fdefmeta:=nil;
  387. fsymdecl.free;
  388. fsymdecl:=nil;
  389. defnumberlist.free;
  390. defnumberlist:=nil;
  391. deftowritelist.free;
  392. deftowritelist:=nil;
  393. fcunode.free;
  394. fcunode:=nil;
  395. inherited Destroy;
  396. end;
  397. procedure TDebugInfoLLVM.enum_membersyms_callback(p:TObject; arg: pointer);
  398. begin
  399. (*
  400. case tsym(p).typ of
  401. fieldvarsym:
  402. appendsym_fieldvar(pmembercallbackinfo(arg)^.list,pmembercallbackinfo(arg)^.structnode,tfieldvarsym(p));
  403. propertysym:
  404. appendsym_property(pmembercallbackinfo(arg)^.list,pmembercallbackinfo(arg)^.structnode,tpropertysym(p));
  405. constsym:
  406. appendsym_const_member(pmembercallbackinfo(arg)^.list,pmembercallbackinfo(arg)^.structnode,tconstsym(p),true);
  407. else
  408. ;
  409. end;
  410. *)
  411. end;
  412. procedure TDebugInfoLLVM.ensuremetainit;
  413. begin
  414. if not assigned(fenums) then
  415. begin
  416. fenums:=tai_llvmunnamedmetadatanode.create;
  417. fretainedtypes:=tai_llvmunnamedmetadatanode.create;
  418. fglobals:=tai_llvmunnamedmetadatanode.create;
  419. femptyexpression:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIExpression);
  420. fcunode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompileUnit);
  421. end;
  422. end;
  423. procedure TDebugInfoLLVM.resetfornewmodule;
  424. var
  425. i: longint;
  426. begin
  427. { for LLVM, we need to generate the procdef type info (or at least
  428. temporary references to it) already during the generation of the line
  429. info (all line info metadata needs a reference to its parent scope,
  430. the procdef). Since the line info is generated per procedure and
  431. the type info only at the end, we can't allocate the type info
  432. structures at the start of the type info generation like for other
  433. debug info producers. Instead, we have to initialise everything in the
  434. constructor, and then reset it at the end of the debug info pass
  435. (inserting the module info) }
  436. ffilemeta.Clear;
  437. flocationmeta.Clear;
  438. flexicalblockfilemeta.Clear;
  439. fdefmeta.free;
  440. fsymdecl.Clear;
  441. { one item per def, plus some extra space in case of nested types,
  442. externally used types etc (it will grow further if necessary) }
  443. i:=current_module.localsymtable.DefList.count*4;
  444. if assigned(current_module.globalsymtable) then
  445. inc(i,current_module.globalsymtable.DefList.count*2);
  446. fdefmeta:=TLLVMMetaDefHashSet.Create(i,true,false);
  447. defnumberlist.Clear;
  448. deftowritelist.Clear;
  449. fcunode:=nil;
  450. fenums:=nil;
  451. fretainedtypes:=nil;
  452. fglobals:=nil;
  453. femptyexpression:=nil;
  454. end;
  455. procedure TDebugInfoLLVM.collectglobalsyms;
  456. var
  457. i: TAsmListType;
  458. hp: tai;
  459. begin
  460. for i in globaldataasmlisttypes do
  461. begin
  462. if not assigned(current_asmdata.AsmLists[i]) then
  463. continue;
  464. hp:=tai(current_asmdata.AsmLists[i].First);
  465. while assigned(hp) do
  466. begin
  467. if (hp.typ=ait_llvmdecl) and
  468. assigned(taillvmdecl(hp).sym) then
  469. sym_set_decl(taillvmdecl(hp).sym,hp);
  470. hp:=tai(hp.next);
  471. end;
  472. end;
  473. end;
  474. function TDebugInfoLLVM.file_getmetanode(moduleindex: tfileposmoduleindex; fileindex: tfileposfileindex): tai_llvmspecialisedmetadatanode;
  475. var
  476. infile: tinputfile;
  477. dirname: TSymStr;
  478. item: PHashSetItem;
  479. metaitem: tai_llvmspecialisedmetadatanode;
  480. modfileindex: packed record
  481. moduleindex: tfileposmoduleindex;
  482. fileindex: tfileposfileindex;
  483. end;
  484. begin
  485. modfileindex.moduleindex:=moduleindex;
  486. modfileindex.fileindex:=fileindex;
  487. item:=ffilemeta.FindOrAdd(@modfileindex,sizeof(modfileindex));
  488. if not assigned(item^.Data) then
  489. begin
  490. infile:=get_module(moduleindex).sourcefiles.get_file(fileindex);
  491. if not assigned(infile) then
  492. begin
  493. result:=nil;
  494. exit;
  495. end;
  496. if infile.path = '' then
  497. dirname:=absolute_llvm_path('.')
  498. else
  499. begin
  500. { add the canonical form here already to avoid problems with }
  501. { paths such as './' etc }
  502. dirname:=absolute_llvm_path(infile.path);
  503. end;
  504. if dirname='' then
  505. dirname:='.';
  506. metaitem:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIFile);
  507. metaitem.addstring('filename',infile.name);
  508. metaitem.addstring('directory',dirname);
  509. current_asmdata.AsmLists[al_dwarf_line].concat(metaitem);
  510. item^.Data:=metaitem;
  511. end;
  512. result:=tai_llvmspecialisedmetadatanode(item^.Data);
  513. end;
  514. function TDebugInfoLLVM.filepos_getmetanode(const filepos: tfileposinfo; const functionfileinfo: tfileposinfo; const functionscope: tai_llvmspecialisedmetadatanode; nolineinfo: boolean): tai_llvmspecialisedmetadatanode;
  515. var
  516. item: PHashSetItem;
  517. filemeta,
  518. locationscopemeta: tai_llvmspecialisedmetadatanode;
  519. lexicalblockkey: packed record
  520. scopemeta,
  521. filemeta: tai_llvmspecialisedmetadatanode;
  522. end;
  523. locationkey: packed record
  524. scope: tai_llvmspecialisedmetadatanode;
  525. line: tfileposline;
  526. column: tfileposcolumn;
  527. end;
  528. begin
  529. result:=nil;
  530. if (filepos.fileindex<>0) then
  531. filemeta:=file_getmetanode(filepos.moduleindex,filepos.fileindex)
  532. else
  533. filemeta:=file_getmetanode(functionfileinfo.moduleindex,functionfileinfo.fileindex);
  534. if not assigned(filemeta) then
  535. exit;
  536. if (filepos.fileindex<>0) and
  537. (filepos.fileindex<>functionfileinfo.fileindex) then
  538. begin
  539. lexicalblockkey.scopemeta:=functionscope;
  540. lexicalblockkey.filemeta:=filemeta;
  541. item:=flexicalblockfilemeta.FindOrAdd(@lexicalblockkey,sizeof(lexicalblockkey));
  542. if not assigned(item^.Data) then
  543. begin
  544. locationscopemeta:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DILexicalBlockFile);
  545. locationscopemeta.addmetadatarefto('scope',functionscope);
  546. locationscopemeta.addmetadatarefto('file',filemeta);
  547. locationscopemeta.addint64('discriminator',0);
  548. current_asmdata.AsmLists[al_dwarf_line].concat(locationscopemeta);
  549. item^.Data:=locationscopemeta;
  550. end
  551. else
  552. locationscopemeta:=tai_llvmspecialisedmetadatanode(item^.Data);
  553. end
  554. else
  555. locationscopemeta:=functionscope;
  556. locationkey.scope:=locationscopemeta;
  557. if not nolineinfo then
  558. begin
  559. locationkey.line:=filepos.line;
  560. locationkey.column:=filepos.column;
  561. end
  562. else
  563. begin
  564. locationkey.line:=0;
  565. locationkey.column:=0;
  566. end;
  567. item:=flocationmeta.FindOrAdd(@locationkey,sizeof(locationkey));
  568. if not assigned(item^.Data) then
  569. begin
  570. result:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DILocation);
  571. if not nolineinfo then
  572. begin
  573. result.addqword('line',filepos.line);
  574. result.addqword('column',filepos.column);
  575. end
  576. else
  577. result.addqword('line',0);
  578. result.addmetadatarefto('scope',locationscopemeta);
  579. current_asmdata.AsmLists[al_dwarf_line].concat(result);
  580. item^.Data:=result;
  581. end
  582. else
  583. result:=tai_llvmspecialisedmetadatanode(item^.Data);
  584. end;
  585. procedure TDebugInfoLLVM.try_add_file_metaref(dinode: tai_llvmspecialisedmetadatanode; const fileinfo: tfileposinfo; includescope: boolean);
  586. var
  587. filemeta: tai_llvmbasemetadatanode;
  588. begin
  589. filemeta:=file_getmetanode(fileinfo.moduleindex,fileinfo.fileindex);
  590. if assigned(filemeta) then
  591. begin
  592. if includescope then
  593. begin
  594. dinode.addmetadatarefto('scope',filemeta);
  595. end;
  596. dinode.addmetadatarefto('file',filemeta);
  597. dinode.addqword('line',fileinfo.line);
  598. end;
  599. end;
  600. function TDebugInfoLLVM.add_line_metanode(const fileinfo: tfileposinfo): tai_llvmspecialisedmetadatanode;
  601. var
  602. filemeta: tai_llvmbasemetadatanode;
  603. begin
  604. filemeta:=file_getmetanode(fileinfo.moduleindex,fileinfo.fileindex);
  605. if not assigned(filemeta) then
  606. internalerror(2022041701);
  607. result:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DILocation);
  608. result.addqword('line',fileinfo.line);
  609. result.addqword('column',fileinfo.column);
  610. result.addmetadatarefto('scope',filemeta);
  611. current_asmdata.AsmLists[al_dwarf_line].concat(result);
  612. end;
  613. procedure TDebugInfoLLVM.appenddef_ord(list:TAsmList;def:torddef);
  614. var
  615. ordtype: tordtype;
  616. dinode: tai_llvmspecialisedmetadatanode;
  617. begin
  618. { nothing, must be referenced as "null" in the using declaration }
  619. if is_void(def) then
  620. exit;
  621. ordtype:=def.ordtype;
  622. if ordtype=customint then
  623. ordtype:=range_to_basetype(def.low,def.high);
  624. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIBasicType));
  625. case ordtype of
  626. s8bit,
  627. s16bit,
  628. s32bit,
  629. u8bit,
  630. u16bit,
  631. u32bit,
  632. u64bit,
  633. s64bit,
  634. u128bit,
  635. s128bit:
  636. begin
  637. dinode.addqword('size',def.size*8);
  638. if def.alignment<>def.size then
  639. dinode.addqword('align',def.alignment*8);
  640. { generate proper signed/unsigned info for types like 0..3 }
  641. { these are s8bit, but should be identified as unsigned }
  642. { because otherwise they are interpreted wrongly when used }
  643. { in a bitpacked record }
  644. if def.low<0 then
  645. dinode.addqword('encoding',ord(DW_ATE_signed))
  646. else
  647. dinode.addqword('encoding',ord(DW_ATE_unsigned));
  648. end;
  649. uvoid :
  650. begin
  651. { checked above }
  652. end;
  653. uchar,
  654. uwidechar :
  655. begin
  656. dinode.addqword('size',def.size*8);
  657. dinode.addint64('encoding',ord(DW_ATE_unsigned_char));
  658. end;
  659. pasbool1,
  660. pasbool8,
  661. bool8bit,
  662. pasbool16,
  663. bool16bit,
  664. pasbool32,
  665. bool32bit,
  666. pasbool64,
  667. bool64bit:
  668. begin
  669. dinode.addqword('size',def.size*8);
  670. dinode.addint64('encoding',ord(DW_ATE_boolean));
  671. end;
  672. scurrency:
  673. begin
  674. { we should use DW_ATE_signed_fixed, however it isn't supported yet by LLVM }
  675. dinode.addqword('size',def.size*8);
  676. dinode.addint64('encoding',ord(DW_ATE_signed));
  677. end;
  678. customint:
  679. internalerror(2021111502);
  680. end;
  681. list.concat(dinode);
  682. end;
  683. procedure TDebugInfoLLVM.appenddef_float(list:TAsmList;def:tfloatdef);
  684. var
  685. dinode: tai_llvmspecialisedmetadatanode;
  686. begin
  687. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIBasicType));
  688. case def.floattype of
  689. s32real,
  690. s64real,
  691. s80real,
  692. sc80real,
  693. s128real:
  694. begin
  695. dinode.addqword('size',def.size*8);
  696. if def.alignment<>def.size then
  697. dinode.addqword('align',def.alignment*8);
  698. dinode.addint64('encoding',ord(DW_ATE_float));
  699. end;
  700. s64currency:
  701. begin
  702. { we should use DW_ATE_signed_fixed, however it isn't supported yet by LLVM }
  703. dinode.addqword('size',def.size*8);
  704. dinode.addint64('encoding',ord(DW_ATE_signed));
  705. end;
  706. s64comp:
  707. begin
  708. { we should use DW_ATE_signed_fixed, however it isn't supported yet by LLVM }
  709. dinode.addqword('size',def.size*8);
  710. dinode.addint64('encoding',ord(DW_ATE_signed));
  711. end;
  712. end;
  713. list.concat(dinode);
  714. end;
  715. procedure TDebugInfoLLVM.appenddef_enum(list:TAsmList;def:tenumdef);
  716. var
  717. hp : tenumsym;
  718. i : longint;
  719. dinode: tai_llvmspecialisedmetadatanode;
  720. enumelem: tai_llvmspecialisedmetadatanode;
  721. enumlist: tai_llvmunnamedmetadatanode;
  722. begin
  723. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  724. dinode.addqword('tag',ord(DW_TAG_enumeration_type));
  725. dinode.addqword('size',def.size*8);
  726. dinode.addstring('identifier',def.mangledparaname);
  727. { register in module's list of enums (to ensure the debug info gets
  728. emitted even if the enum is not used in the current module) }
  729. fenums.addvalue(llvm_getmetadatareftypedconst(dinode));
  730. enumlist:=tai_llvmunnamedmetadatanode.create;
  731. { add enum symbols }
  732. for i:=0 to def.symtable.SymList.Count-1 do
  733. begin
  734. hp:=tenumsym(def.symtable.SymList[i]);
  735. if hp.value<def.minval then
  736. continue
  737. else if hp.value>def.maxval then
  738. break;
  739. enumelem:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIEnumerator);
  740. enumelem.addstring('name',symname(hp, false));
  741. enumelem.addint64('value',hp.value);
  742. list.concat(enumelem);
  743. enumlist.addvalue(llvm_getmetadatareftypedconst(enumelem));
  744. end;
  745. if enumlist.valuecount<>0 then
  746. begin
  747. list.concat(enumlist);
  748. dinode.addmetadatarefto('elements',enumlist);
  749. end
  750. else
  751. begin
  752. enumlist.free;
  753. end;
  754. list.concat(dinode);
  755. end;
  756. procedure TDebugInfoLLVM.appenddef_array(list:TAsmList;def:tarraydef);
  757. var
  758. dinode,
  759. subrangenode,
  760. exprnode: tai_llvmspecialisedmetadatanode;
  761. arrayrangenode: tai_llvmunnamedmetadatanode;
  762. size : qword;
  763. nesteddef: tdef;
  764. power: longint;
  765. flags: TLLVMDIFlags;
  766. begin
  767. if is_dynamic_array(def) { and
  768. not(llvmflag_array_datalocation in llvmversion_properties[current_settings.llvmversion]) } then
  769. begin
  770. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  771. dinode.addqword('tag',ord(DW_TAG_pointer_type));
  772. dinode.addmetadatarefto('baseType',def_meta_node(def.elementdef));
  773. dinode.addqword('size',def.size*8);
  774. list.concat(dinode);
  775. exit;
  776. end;
  777. { open arrays etc need to access the high parameter to define their range,
  778. which is not possible here since we need the parasym rather than the def }
  779. if is_open_array(def) then
  780. begin
  781. (*
  782. if llvmflag_array_datalocation in llvmversion_properties[current_settings.llvmversion] then
  783. begin
  784. dinode:=def_meta_impl(def);
  785. { should be generated as part of the parasym }
  786. if not assigned(dinode) then
  787. internalerror(2021112002);
  788. end
  789. else *)
  790. begin
  791. { no idea about the size, generate an array of 1 element -- although it could be empty }
  792. appenddef_array_internal(list,def,def.elementdef,0,1);
  793. end;
  794. exit;
  795. end;
  796. if is_array_of_const(def) then
  797. begin
  798. { no idea about the size, generate an array of 1 element -- although it could be empty }
  799. appenddef_array_internal(list,def,def.elementdef,0,1);
  800. exit;
  801. end;
  802. if is_special_array(def)
  803. and not((llvmflag_array_datalocation in llvmversion_properties[current_settings.llvmversion]) and
  804. is_dynamic_array(def)) then
  805. internalerror(2021121902);
  806. { todo: proper support for bitpacked arrays }
  807. if is_packed_array(def) and
  808. (((def.elementdef.packedbitsize mod 8)<>0) or
  809. not ispowerof2(def.elementdef.packedbitsize div 8,power)) then
  810. begin
  811. { for now just encode as an array of bytes }
  812. appenddef_array_internal(list,def,u8inttype,0,def.size-1);
  813. exit;
  814. end;
  815. { collection of all ranges of the array (to support multi-dimensional arrays) }
  816. arrayrangenode:=tai_llvmunnamedmetadatanode.create;
  817. list.concat(arrayrangenode);
  818. { range of the array }
  819. subrangenode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubrange);
  820. if is_dynamic_array(def) then
  821. begin
  822. exprnode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIExpression);
  823. exprnode.addint64('',ord(DW_OP_push_object_address));
  824. exprnode.addint64('',ord(DW_OP_constu));
  825. exprnode.addint64('',ord(sizeof(pint)));
  826. exprnode.addint64('',ord(DW_OP_minus));
  827. exprnode.addint64('',ord(DW_OP_deref));
  828. list.concat(exprnode);
  829. subrangenode.addmetadatarefto('upperBound',exprnode);
  830. subrangenode.addint64('lowerBound',def.lowrange);
  831. end
  832. else
  833. begin
  834. subrangenode.addqword('count',def.highrange-def.lowrange+1);
  835. subrangenode.addint64('lowerBound',def.lowrange);
  836. end;
  837. list.concat(subrangenode);
  838. nesteddef:=def.elementdef;
  839. arrayrangenode.addvalue(llvm_getmetadatareftypedconst(subrangenode));
  840. while (nesteddef.typ=arraydef) and
  841. not is_special_array(nesteddef) do
  842. begin
  843. subrangenode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubrange);
  844. subrangenode.addqword('count',tarraydef(nesteddef).highrange-tarraydef(nesteddef).lowrange+1);
  845. subrangenode.addint64('lowerBound',tarraydef(nesteddef).lowrange);
  846. list.concat(subrangenode);
  847. arrayrangenode.addvalue(llvm_getmetadatareftypedconst(subrangenode));
  848. nesteddef:=tarraydef(nesteddef).elementdef;
  849. end;
  850. { the array definition }
  851. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  852. dinode.addqword('tag',ord(DW_TAG_array_type));
  853. dinode.addmetadatarefto('baseType',def_meta_node(nesteddef));
  854. dinode.addmetadatarefto('elements',arrayrangenode);
  855. if is_vector(def) then
  856. dinode.addenum('flags','DIFlagVector');
  857. if not is_dynamic_array(def) then
  858. dinode.addqword('size',def.size*8)
  859. else
  860. begin
  861. exprnode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIExpression);
  862. exprnode.addqword('',ord(DW_OP_LLVM_implicit_pointer));
  863. list.concat(exprnode);
  864. dinode.addmetadatarefto('dataLocation',exprnode);
  865. end;
  866. list.concat(dinode);
  867. end;
  868. procedure TDebugInfoLLVM.appenddef_record(list:TAsmList;def:trecorddef);
  869. begin
  870. if assigned(def.objname) then
  871. appenddef_record_named(list,def,def,def.objname^)
  872. else
  873. appenddef_record_named(list,def,def,'');
  874. end;
  875. procedure TDebugInfoLLVM.appenddef_record_named(list:TAsmList; fordef: tdef; def:trecorddef; const name: TSymStr);
  876. var
  877. dinode: tai_llvmspecialisedmetadatanode;
  878. begin
  879. dinode:=def_set_meta_impl(fordef,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  880. dinode.addint64('tag',ord(DW_TAG_structure_type));
  881. if (name<>'') then
  882. dinode.addstring('name',name);
  883. dinode.addqword('size',def.size*8);
  884. list.concat(dinode);
  885. // def.symtable.symList.ForEachCall(@enum_membersyms_callback,dinode);
  886. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  887. end;
  888. procedure TDebugInfoLLVM.appenddef_pointer(list:TAsmList;def:tpointerdef);
  889. var
  890. dinode: tai_llvmspecialisedmetadatanode;
  891. begin
  892. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  893. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  894. if not(is_voidpointer(def)) then
  895. dinode.addmetadatarefto('baseType',def_meta_node(def.pointeddef))
  896. else
  897. dinode.addmetadatarefto('baseType',nil);
  898. list.concat(dinode);
  899. end;
  900. procedure TDebugInfoLLVM.appenddef_formal(list: TAsmList; def: tformaldef);
  901. var
  902. dinode: tai_llvmspecialisedmetadatanode;
  903. begin
  904. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  905. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  906. dinode.addmetadatarefto('baseType',nil);
  907. list.concat(dinode);
  908. end;
  909. procedure TDebugInfoLLVM.appenddef_string(list:TAsmList;def:tstringdef);
  910. procedure addnormalstringdef(const name: TSymStr; lendef: tdef; maxlen: asizeuint);
  911. var
  912. dinode,
  913. subrangenode,
  914. exprnode: tai_llvmspecialisedmetadatanode;
  915. arrayrangenode: tai_aggregatetypedconst;
  916. { maxlen can be > high(int64) }
  917. slen : asizeuint;
  918. arr : tasmlabel;
  919. begin
  920. { fix length of openshortstring }
  921. slen:=aword(def.len);
  922. if (slen=0) or
  923. (slen>maxlen) then
  924. slen:=maxlen;
  925. appenddef_array_internal(list,def,cansichartype,0,slen);
  926. end;
  927. var
  928. dinode: tai_llvmspecialisedmetadatanode;
  929. begin
  930. case def.stringtype of
  931. st_shortstring:
  932. begin
  933. addnormalstringdef('ShortString',u8inttype,255);
  934. end;
  935. st_longstring:
  936. begin
  937. { a) we don't actually support variables of this type currently
  938. b) this type is only used as the type for constant strings
  939. > 255 characters
  940. c) in such a case, gdb will allocate and initialise enough
  941. memory to hold the maximum size for such a string
  942. -> don't use high(qword)/high(cardinal) as maximum, since that
  943. will cause exhausting the VM space, but some "reasonably high"
  944. number that should be enough for most constant strings
  945. }
  946. {$ifdef cpu64bitaddr}
  947. addnormalstringdef('LongString',u64inttype,qword(1024*1024));
  948. {$endif cpu64bitaddr}
  949. {$ifdef cpu32bitaddr}
  950. addnormalstringdef('LongString',u32inttype,cardinal(1024*1024));
  951. {$endif cpu32bitaddr}
  952. {$ifdef cpu16bitaddr}
  953. addnormalstringdef('LongString',u16inttype,cardinal(1024));
  954. {$endif cpu16bitaddr}
  955. end;
  956. st_ansistring:
  957. begin
  958. // Todo: dynamic length "array"
  959. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  960. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  961. dinode.addmetadatarefto('baseType',def_meta_node(cansichartype));
  962. list.concat(dinode);
  963. end;
  964. st_unicodestring,
  965. st_widestring:
  966. begin
  967. // Todo: dynamic length "array"
  968. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  969. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  970. dinode.addmetadatarefto('baseType',def_meta_node(cwidechartype));
  971. list.concat(dinode);
  972. end;
  973. end;
  974. end;
  975. procedure TDebugInfoLLVM.appenddef_procvar(list:TAsmList;def:tprocvardef);
  976. var
  977. dinode: tai_llvmspecialisedmetadatanode;
  978. begin
  979. { plain pointer for now }
  980. if def.is_addressonly then
  981. begin
  982. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  983. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  984. dinode.addmetadatarefto('baseType',nil);
  985. list.concat(dinode);
  986. end
  987. else
  988. begin
  989. appenddef_array_internal(list,def,voidcodepointertype,1,2);
  990. end;
  991. end;
  992. procedure TDebugInfoLLVM.appenddef_file(list: TAsmList; def: tfiledef);
  993. var
  994. dinode: tai_llvmspecialisedmetadatanode;
  995. begin
  996. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  997. dinode.addint64('tag',ord(DW_TAG_structure_type));
  998. if assigned(def.typesym) then
  999. dinode.addstring('name',symname(def.typesym, false));
  1000. dinode.addqword('size',def.size*8);
  1001. list.concat(dinode);
  1002. end;
  1003. procedure TDebugInfoLLVM.appenddef_object(list: TAsmList; def: tobjectdef);
  1004. var
  1005. dinode: tai_llvmspecialisedmetadatanode;
  1006. begin
  1007. if is_implicit_pointer_object_type(def) then
  1008. begin
  1009. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1010. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1011. dinode.addmetadatarefto('baseType',nil);
  1012. end
  1013. else
  1014. begin
  1015. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  1016. dinode.addint64('tag',ord(DW_TAG_structure_type));
  1017. if assigned(def.typesym) then
  1018. dinode.addstring('name',symname(def.typesym, false));
  1019. dinode.addqword('size',def.size*8);
  1020. end;
  1021. list.concat(dinode);
  1022. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  1023. end;
  1024. procedure TDebugInfoLLVM.appenddef_set(list: TAsmList; def: tsetdef);
  1025. begin
  1026. appenddef_array_internal(list,def,u8inttype,0,def.size-1);
  1027. end;
  1028. procedure TDebugInfoLLVM.appenddef_undefined(list: TAsmList; def: tundefineddef);
  1029. var
  1030. dinode: tai_llvmspecialisedmetadatanode;
  1031. begin
  1032. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1033. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1034. dinode.addmetadatarefto('baseType',nil);
  1035. list.concat(dinode);
  1036. end;
  1037. procedure TDebugInfoLLVM.appenddef_classref(list: TAsmList; def: tclassrefdef);
  1038. var
  1039. dinode: tai_llvmspecialisedmetadatanode;
  1040. begin
  1041. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1042. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1043. dinode.addmetadatarefto('baseType',nil);
  1044. list.concat(dinode);
  1045. end;
  1046. procedure TDebugInfoLLVM.appenddef_variant(list: TAsmList; def: tvariantdef);
  1047. begin
  1048. if assigned(vardatadef) then
  1049. appenddef_record_named(list,def,trecorddef(vardatadef),'Variant');
  1050. end;
  1051. procedure TDebugInfoLLVM.afterappenddef(list:TAsmList;def:tdef);
  1052. var
  1053. tempdinode,
  1054. refdinode,
  1055. impldinode: tai_llvmspecialisedmetadatanode;
  1056. begin
  1057. if def.typ=procdef then
  1058. exit;
  1059. if is_void(def) then
  1060. exit;
  1061. refdinode:=def_meta_node(def);
  1062. if is_objc_class_or_protocol(def) then
  1063. begin
  1064. { for Objective-C classes, the named typedef must refer to the
  1065. struct itself, not to the pointer of the struct; Objective-C
  1066. classes are not implicit pointers in Objective-C itself, only
  1067. in FPC. So make the def label point to a pointer to the
  1068. typedef, which in turn refers to the actual struct (for Delphi-
  1069. style classes, the def points to the typedef, which refers to
  1070. a pointer to the actual struct) }
  1071. { implicit pointer }
  1072. tempdinode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType);
  1073. refdinode.addint64('tag',ord(DW_TAG_pointer_type));
  1074. refdinode.addmetadatarefto('baseType',tempdinode);
  1075. list.concat(refdinode);
  1076. { typedef }
  1077. refdinode:=tempdinode;
  1078. end;
  1079. refdinode.addint64('tag',ord(DW_TAG_typedef));
  1080. if assigned(def.typesym) and
  1081. not(df_generic in def.defoptions) then
  1082. begin
  1083. refdinode.addstring('name',symname(def.typesym,false));
  1084. try_add_file_metaref(refdinode,def.typesym.fileinfo,false);
  1085. end;
  1086. impldinode:=def_meta_impl(def);
  1087. if not assigned(impldinode) then
  1088. internalerror(2021120501);
  1089. refdinode.addmetadatarefto('baseType',impldinode);
  1090. list.concat(refdinode);
  1091. end;
  1092. procedure TDebugInfoLLVM.appendprocdef(list:TAsmList; def:tprocdef);
  1093. procedure adddispflags(dinode: tai_llvmspecialisedmetadatanode; is_definition, is_virtual: boolean);
  1094. var
  1095. dispflags: TSymStr;
  1096. islocal: boolean;
  1097. begin
  1098. islocal:=
  1099. not((po_global in def.procoptions) and
  1100. (def.parast.symtablelevel<=normal_function_level));
  1101. adddefinitionlocal(dinode,is_definition,islocal,not(llvmflag_NoDISPFlags in llvmversion_properties[current_settings.llvmversion]),dispflags);
  1102. if llvmflag_NoDISPFlags in llvmversion_properties[current_settings.llvmversion] then
  1103. begin
  1104. if is_virtual then
  1105. begin
  1106. if not(po_abstractmethod in def.procoptions) then
  1107. dinode.addenum('virtuality','DW_VIRTUALITY_virtual')
  1108. else
  1109. dinode.addenum('virtuality','DW_VIRTUALITY_pure_virtual');
  1110. end;
  1111. exit;
  1112. end;
  1113. if is_virtual then
  1114. begin
  1115. if dispflags<>'' then
  1116. dispflags:=dispflags+'|';
  1117. if not(po_abstractmethod in def.procoptions) then
  1118. dispflags:=dispflags+'DISPFlagVirtual'
  1119. else
  1120. dispflags:=dispflags+'DISPFlagPureVirtual';
  1121. end
  1122. else
  1123. begin
  1124. { this one will always be a definition, so no need to check
  1125. whether result is empty }
  1126. if not(llvmflag_NoDISPFlagMainSubprogram in llvmversion_properties[current_settings.llvmversion]) and
  1127. (def.proctypeoption=potype_proginit) then
  1128. dispflags:=dispflags+'|DISPFlagMainSubprogram';
  1129. end;
  1130. if dispflags<>'' then
  1131. dinode.addenum('spFlags',dispflags);
  1132. end;
  1133. procedure adddiflags(dinode: tai_llvmspecialisedmetadatanode; is_definition: boolean);
  1134. var
  1135. diflags: TSymStr;
  1136. begin
  1137. if (llvmflag_NoDISPFlagMainSubprogram in llvmversion_properties[current_settings.llvmversion]) and
  1138. (def.proctypeoption=potype_proginit) then
  1139. diflags:='DIFlagMainSubprogram'
  1140. else if def.owner.symtabletype in [objectsymtable,recordsymtable] then
  1141. diflags:=visibilitydiflag(def.visibility)
  1142. else
  1143. diflags:='';
  1144. if diflags<>'' then
  1145. dinode.addenum('flags',diflags);
  1146. end;
  1147. var
  1148. dinode,
  1149. ditypenode : tai_llvmspecialisedmetadatanode;
  1150. fileref : tai_simpletypedconst;
  1151. procdeftai : tai;
  1152. st : tsymtable;
  1153. vmtoffset : pint;
  1154. flags : TSymStr;
  1155. in_currentunit,
  1156. is_virtual : boolean;
  1157. begin
  1158. { only write debug info for procedures defined in the current module,
  1159. except in case of methods (clang-compatible)
  1160. }
  1161. in_currentunit:=def.in_currentunit;
  1162. if not in_currentunit and
  1163. not (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1164. exit;
  1165. { happens for init procdef of units without init section }
  1166. if in_currentunit and
  1167. not assigned(def.procstarttai) then
  1168. exit;
  1169. { These don't contain a taillvmdecl, they are completely generated
  1170. in native assembly. If we want to add debug information to these,
  1171. we have to do it using the regular debug info generation }
  1172. if po_assembler in def.procoptions then
  1173. exit;
  1174. if df_generic in def.defoptions then
  1175. exit;
  1176. { Procdefs are not handled by the regular def writing code, so
  1177. dbg_state is not set/checked for them. Do it here. }
  1178. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  1179. exit;
  1180. defnumberlist.Add(def);
  1181. def.dbg_state:=dbg_state_writing;
  1182. { difference compared to other kinds of defs: the DISubProgram gets
  1183. created directly in get_def_metatai because a typedef for a
  1184. DISubProgram does not make sense and is not supported by LLVM ->
  1185. don't set the implementation of the metadata def here and just use
  1186. the regular node }
  1187. dinode:=def_meta_node(def);
  1188. list.concat(dinode);
  1189. { we have to attach the debug info to the definition instruction of the
  1190. proc }
  1191. procdeftai:=nil;
  1192. if in_currentunit then
  1193. begin
  1194. procdeftai:=def.procstarttai;
  1195. if (procdeftai.typ<>ait_llvmdecl) or
  1196. (taillvmdecl(procdeftai).def<>def) then
  1197. internalerror(2022022010);
  1198. taillvmdecl(procdeftai).addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',dinode));
  1199. end;
  1200. dinode.addstring('name',symdebugname(def.procsym));
  1201. try_add_file_metaref(dinode,def.fileinfo,true);
  1202. if not(cs_debuginfo in current_settings.moduleswitches) then
  1203. begin
  1204. def.dbg_state:=dbg_state_written;
  1205. exit;
  1206. end;
  1207. is_virtual:=
  1208. (([po_abstractmethod, po_virtualmethod, po_overridingmethod]*def.procoptions)<>[]) and
  1209. not is_objc_class_or_protocol(def.struct) and
  1210. not is_objectpascal_helper(def.struct);
  1211. adddispflags(dinode,in_currentunit,is_virtual);
  1212. if is_virtual then
  1213. begin
  1214. { the sizeof(pint) is a bit iffy, since vmtmethodoffset() calculates
  1215. using a combination of voidcodepointer.size, voidpointer.size, and
  1216. sizeof(pint). But that's what the debugger will use }
  1217. dinode.addint64('virtualIndex',tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber) div sizeof(pint));
  1218. {$ifdef extdebug}
  1219. if (tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber) mod sizeof(pint))<>0 then
  1220. internalerror(2022043001);
  1221. {$endif}
  1222. end;
  1223. adddiflags(dinode,in_currentunit);
  1224. dinode.addmetadatarefto('unit',fcunode);
  1225. ditypenode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubroutineType);
  1226. ditypenode.addmetadatarefto('types',getabstractprocdeftypes(list,def));
  1227. list.concat(ditypenode);
  1228. dinode.addmetadatarefto('type',ditypenode);
  1229. (*
  1230. if assigned(def.parast) then
  1231. begin
  1232. { First insert self, because gdb uses the fact whether or not the
  1233. first parameter of a method is artificial to distinguish static
  1234. from regular methods. }
  1235. { fortunately, self is the always the first parameter in the
  1236. paralist, since it has the lowest paranr. Note that this is not
  1237. true for Objective-C, but those methods are detected in
  1238. another way (by reading the ObjC run time information) }
  1239. write_symtable_parasyms(current_asmdata.asmlists[al_dwarf_info],def.paras);
  1240. end;
  1241. { local type defs and vars should not be written
  1242. inside the main proc }
  1243. if in_currentunit and
  1244. assigned(def.localst) and
  1245. (def.localst.symtabletype=localsymtable) then
  1246. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1247. { last write the types from this procdef }
  1248. if assigned(def.parast) then
  1249. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
  1250. { only try to write the localst if the routine is implemented here }
  1251. if in_currentunit and
  1252. assigned(def.localst) and
  1253. (def.localst.symtabletype=localsymtable) then
  1254. begin
  1255. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1256. { Write nested procedures -- disabled, see scope check at the
  1257. beginning; currently, these are still written in the global
  1258. scope. }
  1259. // write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1260. end;
  1261. finish_children;
  1262. *)
  1263. def.dbg_state:=dbg_state_written;
  1264. end;
  1265. procedure TDebugInfoLLVM.adddefinitionlocal(dinode: tai_llvmspecialisedmetadatanode; definition, local, usedispflags: boolean; out dispFlags: tsymstr);
  1266. begin
  1267. dispflags:='';
  1268. if not usedispflags then
  1269. begin
  1270. dinode.addboolean('isDefinition',definition);
  1271. if definition then
  1272. begin
  1273. dinode.addboolean('isLocal',local);
  1274. end;
  1275. exit;
  1276. end;
  1277. if definition then
  1278. begin
  1279. dispflags:='DISPFlagDefinition';
  1280. if local then
  1281. dispflags:=dispflags+'|DISPFlagLocalToUnit';
  1282. end;
  1283. end;
  1284. function TDebugInfoLLVM.get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  1285. (*
  1286. var
  1287. elesize : pint;
  1288. currdef : tdef;
  1289. indirection: boolean;
  1290. *)
  1291. begin
  1292. result:=false;
  1293. (*
  1294. if not assigned(symlist) then
  1295. exit;
  1296. sym:=nil;
  1297. offset:=0;
  1298. currdef:=nil;
  1299. indirection:=false;
  1300. repeat
  1301. case symlist^.sltype of
  1302. sl_load:
  1303. begin
  1304. if assigned(sym) then
  1305. internalerror(2009031203);
  1306. if not(symlist^.sym.typ in [paravarsym,localvarsym,staticvarsym,fieldvarsym]) then
  1307. { can't handle... }
  1308. exit;
  1309. sym:=tabstractvarsym(symlist^.sym);
  1310. currdef:=tabstractvarsym(sym).vardef;
  1311. if ((sym.typ=paravarsym) and
  1312. paramanager.push_addr_param(tparavarsym(sym).varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption)) then
  1313. indirection:=true;
  1314. end;
  1315. sl_subscript:
  1316. begin
  1317. if not assigned(currdef) then
  1318. internalerror(2009031301);
  1319. if (symlist^.sym.typ<>fieldvarsym) then
  1320. internalerror(2009031202);
  1321. { can't handle offsets with indirections yet }
  1322. if indirection then
  1323. exit;
  1324. if is_packed_record_or_object(currdef) then
  1325. begin
  1326. { can't calculate the address of a non-byte aligned field }
  1327. if (tfieldvarsym(symlist^.sym).fieldoffset mod 8) <> 0 then
  1328. exit;
  1329. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset div 8)
  1330. end
  1331. else
  1332. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset);
  1333. currdef:=tfieldvarsym(symlist^.sym).vardef;
  1334. end;
  1335. sl_absolutetype,
  1336. sl_typeconv:
  1337. begin
  1338. currdef:=symlist^.def;
  1339. { ignore, these don't change the address }
  1340. end;
  1341. sl_vec:
  1342. begin
  1343. if not assigned(currdef) or
  1344. (currdef.typ<>arraydef) then
  1345. internalerror(2009031201);
  1346. { can't handle offsets with indirections yet }
  1347. if indirection then
  1348. exit;
  1349. if not is_packed_array(currdef) then
  1350. elesize:=tarraydef(currdef).elesize
  1351. else
  1352. begin
  1353. elesize:=tarraydef(currdef).elepackedbitsize;
  1354. { can't calculate the address of a non-byte aligned element }
  1355. if (elesize mod 8)<>0 then
  1356. exit;
  1357. elesize:=elesize div 8;
  1358. end;
  1359. inc(offset,(symlist^.value.svalue-tarraydef(currdef).lowrange)*elesize);
  1360. currdef:=tarraydef(currdef).elementdef;
  1361. end;
  1362. else
  1363. internalerror(2009031403);
  1364. end;
  1365. symlist:=symlist^.next;
  1366. until not assigned(symlist);
  1367. if not assigned(sym) then
  1368. internalerror(2009031205);
  1369. result:=true;
  1370. *)
  1371. end;
  1372. procedure TDebugInfoLLVM.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  1373. begin
  1374. // appendsym_var_with_name_type_offset(list,sym,symname(sym, false),sym.vardef,0,[]);
  1375. end;
  1376. procedure TDebugInfoLLVM.appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: TSymStr; def: tdef; offset: pint(*; const flags: tdwarfvarsymflags*));
  1377. (*
  1378. var
  1379. templist : TAsmList;
  1380. blocksize,size_of_int : longint;
  1381. tag : tdwarf_tag;
  1382. has_high_reg : boolean;
  1383. dreg,dreghigh : shortint;
  1384. {$ifdef i8086}
  1385. has_segment_sym_name : boolean=false;
  1386. segment_sym_name : TSymStr='';
  1387. segment_reg: TRegister=NR_NO;
  1388. {$endif i8086}
  1389. *)
  1390. begin
  1391. (*
  1392. if vo_is_external in sym.varoptions then
  1393. exit;
  1394. blocksize:=0;
  1395. dreghigh:=0;
  1396. { There is no space allocated for not referenced locals }
  1397. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1398. exit;
  1399. templist:=TAsmList.create;
  1400. case sym.localloc.loc of
  1401. LOC_REGISTER,
  1402. LOC_CREGISTER,
  1403. LOC_MMREGISTER,
  1404. LOC_CMMREGISTER,
  1405. LOC_FPUREGISTER,
  1406. LOC_CFPUREGISTER :
  1407. begin
  1408. { dwarf_reg_no_error might return -1
  1409. in case the register variable has been optimized out }
  1410. dreg:=dwarf_reg_no_error(sym.localloc.register);
  1411. has_high_reg:=(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and (sym.localloc.registerhi<>NR_NO);
  1412. if has_high_reg then
  1413. dreghigh:=dwarf_reg_no_error(sym.localloc.registerhi);
  1414. if dreghigh=-1 then
  1415. has_high_reg:=false;
  1416. if (sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1417. (sym.typ=paravarsym) and
  1418. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1419. not(vo_has_local_copy in sym.varoptions) and
  1420. not is_open_string(sym.vardef) and (dreg>=0) then
  1421. begin
  1422. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  1423. templist.concat(tai_const.create_uleb128bit(dreg));
  1424. templist.concat(tai_const.create_sleb128bit(0));
  1425. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(0);
  1426. end
  1427. else
  1428. begin
  1429. if has_high_reg then
  1430. begin
  1431. templist.concat(tai_comment.create(strpnew('high:low reg pair variable')));
  1432. size_of_int:=sizeof(aint);
  1433. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1434. templist.concat(tai_const.create_uleb128bit(dreg));
  1435. blocksize:=1+Lengthuleb128(dreg);
  1436. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  1437. templist.concat(tai_const.create_uleb128bit(size_of_int));
  1438. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  1439. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1440. templist.concat(tai_const.create_uleb128bit(dreghigh));
  1441. blocksize:=blocksize+1+Lengthuleb128(dreghigh);
  1442. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  1443. templist.concat(tai_const.create_uleb128bit(size_of_int));
  1444. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  1445. end
  1446. else if (dreg>=0) then
  1447. begin
  1448. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1449. templist.concat(tai_const.create_uleb128bit(dreg));
  1450. blocksize:=1+Lengthuleb128(dreg);
  1451. end;
  1452. end;
  1453. end;
  1454. else
  1455. begin
  1456. case sym.typ of
  1457. staticvarsym:
  1458. begin
  1459. if vo_is_thread_var in sym.varoptions then
  1460. begin
  1461. if tf_section_threadvars in target_info.flags then
  1462. begin
  1463. case sizeof(puint) of
  1464. 2:
  1465. templist.concat(tai_const.create_8bit(ord(DW_OP_const2u)));
  1466. 4:
  1467. templist.concat(tai_const.create_8bit(ord(DW_OP_const4u)));
  1468. 8:
  1469. templist.concat(tai_const.create_8bit(ord(DW_OP_const8u)));
  1470. else
  1471. Internalerror(2019100501);
  1472. end;
  1473. {$push}
  1474. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  1475. templist.concat(tai_const.Create_type_name(aitconst_dtpoff,sym.mangledname,0));
  1476. { so far, aitconst_dtpoff is solely 32 bit }
  1477. if (sizeof(puint)=8) and (target_info.endian=endian_little) then
  1478. templist.concat(tai_const.create_32bit(0));
  1479. templist.concat(tai_const.create_8bit(ord(DW_OP_GNU_push_tls_address)));
  1480. if (sizeof(puint)=8) and (target_info.endian=endian_big) then
  1481. templist.concat(tai_const.create_32bit(0));
  1482. {$pop}
  1483. blocksize:=2+sizeof(puint);
  1484. end
  1485. else
  1486. begin
  1487. { TODO: !!! FIXME: dwarf for thread vars !!!}
  1488. { This is only a minimal change to at least be able to get a value
  1489. in only one thread is present PM 2014-11-21, like for stabs format }
  1490. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  1491. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,
  1492. offset+sizeof(pint)));
  1493. blocksize:=1+sizeof(puint);
  1494. end;
  1495. end
  1496. else
  1497. begin
  1498. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  1499. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,offset));
  1500. blocksize:=1+sizeof(puint);
  1501. {$ifdef i8086}
  1502. segment_sym_name:=sym.mangledname;
  1503. has_segment_sym_name:=true;
  1504. {$endif i8086}
  1505. end;
  1506. end;
  1507. paravarsym,
  1508. localvarsym:
  1509. begin
  1510. { Happens when writing debug info for paras of procdefs not
  1511. implemented in the current module. Can't add a general check
  1512. for LOC_INVALID above, because staticvarsyms may also have it.
  1513. }
  1514. if sym.localloc.loc<> LOC_INVALID then
  1515. begin
  1516. if is_fbreg(sym.localloc.reference.base) then
  1517. begin
  1518. templist.concat(tai_const.create_8bit(ord(DW_OP_fbreg)));
  1519. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1520. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  1521. end
  1522. else
  1523. begin
  1524. dreg:=dwarf_reg(sym.localloc.reference.base);
  1525. if dreg<=31 then
  1526. begin
  1527. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  1528. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1529. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  1530. end
  1531. else
  1532. begin
  1533. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  1534. templist.concat(tai_const.create_uleb128bit(dreg));
  1535. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1536. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(sym.localloc.reference.offset+offset);
  1537. end;
  1538. end;
  1539. {$ifdef i8086}
  1540. segment_reg:=sym.localloc.reference.segment;
  1541. {$endif i8086}
  1542. {$ifndef gdb_supports_DW_AT_variable_parameter}
  1543. { Parameters which are passed by reference. (var and the like)
  1544. Hide the reference-pointer and dereference the pointer
  1545. in the DW_AT_location block.
  1546. }
  1547. if (sym.typ=paravarsym) and
  1548. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1549. not(vo_has_local_copy in sym.varoptions) and
  1550. not is_open_string(sym.vardef) then
  1551. begin
  1552. templist.concat(tai_const.create_8bit(ord(DW_OP_deref)));
  1553. inc(blocksize);
  1554. end
  1555. {$endif not gdb_supports_DW_AT_variable_parameter}
  1556. end;
  1557. end
  1558. else
  1559. internalerror(200601288);
  1560. end;
  1561. end;
  1562. end;
  1563. { function results must not be added to the parameter list,
  1564. as they are not part of the signature of the function
  1565. (gdb automatically adds them according to the ABI specifications
  1566. when calling the function)
  1567. }
  1568. if (sym.typ=paravarsym) and
  1569. not(dvf_force_local_var in flags) and
  1570. not(vo_is_funcret in sym.varoptions) then
  1571. tag:=DW_TAG_formal_parameter
  1572. else
  1573. tag:=DW_TAG_variable;
  1574. { must be parasym of externally implemented procdef, but
  1575. the parasymtable can con also contain e.g. absolutevarsyms
  1576. -> check symtabletype}
  1577. if (sym.owner.symtabletype=parasymtable) and
  1578. (sym.localloc.loc=LOC_INVALID) then
  1579. begin
  1580. if (sym.owner.symtabletype<>parasymtable) then
  1581. internalerror(2009101001);
  1582. append_entry(tag,false,[
  1583. DW_AT_name,DW_FORM_string,name+#0
  1584. {
  1585. DW_AT_decl_file,DW_FORM_data1,0,
  1586. DW_AT_decl_line,DW_FORM_data1,
  1587. }
  1588. ])
  1589. end
  1590. else if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  1591. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  1592. ((sym.owner.symtabletype = globalsymtable) or
  1593. (sp_static in sym.symoptions) or
  1594. (vo_is_public in sym.varoptions)) then
  1595. append_entry(tag,false,[
  1596. DW_AT_name,DW_FORM_string,name+#0,
  1597. {
  1598. DW_AT_decl_file,DW_FORM_data1,0,
  1599. DW_AT_decl_line,DW_FORM_data1,
  1600. }
  1601. DW_AT_external,DW_FORM_flag,true,
  1602. { data continues below }
  1603. DW_AT_location,DW_FORM_block1,blocksize
  1604. ])
  1605. {$ifdef gdb_supports_DW_AT_variable_parameter}
  1606. else if (sym.typ=paravarsym) and
  1607. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1608. not(vo_has_local_copy in sym.varoptions) and
  1609. not is_open_string(sym.vardef) then
  1610. append_entry(tag,false,[
  1611. DW_AT_name,DW_FORM_string,name+#0,
  1612. DW_AT_variable_parameter,DW_FORM_flag,true,
  1613. {
  1614. DW_AT_decl_file,DW_FORM_data1,0,
  1615. DW_AT_decl_line,DW_FORM_data1,
  1616. }
  1617. { data continues below }
  1618. DW_AT_location,DW_FORM_block1,blocksize
  1619. ])
  1620. {$endif gdb_supports_DW_AT_variable_parameter}
  1621. else
  1622. append_entry(tag,false,[
  1623. DW_AT_name,DW_FORM_string,name+#0,
  1624. {
  1625. DW_AT_decl_file,DW_FORM_data1,0,
  1626. DW_AT_decl_line,DW_FORM_data1,
  1627. }
  1628. { data continues below }
  1629. DW_AT_location,DW_FORM_block1,blocksize
  1630. ]);
  1631. { append block data }
  1632. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  1633. { Mark self as artificial for methods, because gdb uses the fact
  1634. whether or not the first parameter of a method is artificial to
  1635. distinguish regular from static methods (since there are no
  1636. no vo_is_self parameters for static methods, we don't have to check
  1637. that). }
  1638. if (vo_is_self in sym.varoptions) then
  1639. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  1640. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1641. {$ifdef i8086}
  1642. if has_segment_sym_name then
  1643. append_seg_name(segment_sym_name)
  1644. else if segment_reg<>NR_NO then
  1645. append_seg_reg(segment_reg);
  1646. {$endif i8086}
  1647. templist.free;
  1648. finish_entry;
  1649. *)
  1650. end;
  1651. procedure TDebugInfoLLVM.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1652. var
  1653. decl: taillvmdecl;
  1654. globalvarexpression, globalvar: tai_llvmspecialisedmetadatanode;
  1655. dispflags: tsymstr;
  1656. islocal: boolean;
  1657. begin
  1658. decl:=sym_get_decl(sym);
  1659. if not assigned(decl) then
  1660. begin
  1661. list.concat(tai_comment.create(strpnew('no declaration found for '+sym.mangledname)));
  1662. exit;
  1663. end;
  1664. globalvar:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIGlobalVariable);
  1665. list.concat(globalvar);
  1666. globalvarexpression:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIGlobalVariableExpression);
  1667. globalvarexpression.addmetadatarefto('var',globalvar);
  1668. globalvarexpression.addmetadatarefto('expr',femptyexpression);
  1669. list.concat(globalvarexpression);
  1670. fglobals.addvalue(llvm_getmetadatareftypedconst(globalvarexpression));
  1671. decl.addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',globalvarexpression));
  1672. globalvar.addstring('name',symname(sym,false));
  1673. if not assigned(sym.owner.defowner) then
  1674. globalvar.addmetadatarefto('scope',fcunode)
  1675. else
  1676. globalvar.addmetadatarefto('scope',def_meta_node(tdef(sym.owner.defowner)));
  1677. try_add_file_metaref(globalvar,sym.fileinfo,false);
  1678. globalvar.addmetadatarefto('type',def_meta_node(sym.vardef));
  1679. islocal:=not(
  1680. ((sym.owner.symtabletype = globalsymtable) or
  1681. (sp_static in sym.symoptions) or
  1682. (vo_is_public in sym.varoptions))
  1683. );
  1684. adddefinitionlocal(globalvar,not(vo_is_external in sym.varoptions),islocal,false,dispflags);
  1685. if dispflags<>'' then
  1686. globalvar.addenum('spFlags',dispflags);
  1687. end;
  1688. procedure TDebugInfoLLVM.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  1689. begin
  1690. // appendsym_var(list,sym);
  1691. end;
  1692. procedure TDebugInfoLLVM.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  1693. begin
  1694. // appendsym_var(list,sym);
  1695. end;
  1696. procedure TDebugInfoLLVM.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  1697. begin
  1698. appendsym_fieldvar_with_name_offset(list,sym,symname(sym, false),sym.vardef,0);
  1699. end;
  1700. procedure TDebugInfoLLVM.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  1701. var
  1702. bitoffset,
  1703. fieldoffset,
  1704. fieldnatsize: asizeint;
  1705. begin
  1706. (*
  1707. if (sp_static in sym.symoptions) or
  1708. (sym.visibility=vis_hidden) then
  1709. exit;
  1710. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  1711. { only ordinals are bitpacked }
  1712. not is_ordinal(sym.vardef) then
  1713. begin
  1714. { other kinds of fields can however also appear in a bitpacked }
  1715. { record, and then their offset is also specified in bits rather }
  1716. { than in bytes }
  1717. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  1718. fieldoffset:=sym.fieldoffset
  1719. else
  1720. fieldoffset:=sym.fieldoffset div 8;
  1721. inc(fieldoffset,offset);
  1722. append_entry(DW_TAG_member,false,[
  1723. DW_AT_name,DW_FORM_string,name+#0,
  1724. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  1725. ]);
  1726. end
  1727. else
  1728. begin
  1729. if (sym.vardef.packedbitsize > 255) then
  1730. internalerror(2007061201);
  1731. { we don't bitpack according to the ABI, but as close as }
  1732. { possible, i.e., equivalent to gcc's }
  1733. { __attribute__((__packed__)), which is also what gpc }
  1734. { does. }
  1735. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  1736. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  1737. inc(fieldoffset,offset);
  1738. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  1739. if (target_info.endian=endian_little) then
  1740. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  1741. append_entry(DW_TAG_member,false,[
  1742. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  1743. { gcc also generates both a bit and byte size attribute }
  1744. { we don't support ordinals >= 256 bits }
  1745. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  1746. { nor >= 256 bits (not yet, anyway, see IE above) }
  1747. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  1748. { data1 and data2 are unsigned, bitoffset can also be negative }
  1749. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  1750. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  1751. ]);
  1752. end;
  1753. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  1754. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  1755. if (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1756. append_visibility(sym.visibility);
  1757. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1758. finish_entry;
  1759. *)
  1760. end;
  1761. procedure TDebugInfoLLVM.appendsym_const(list:TAsmList;sym:tconstsym);
  1762. begin
  1763. appendsym_const_member(list,sym,false);
  1764. end;
  1765. procedure TDebugInfoLLVM.appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  1766. var
  1767. i,
  1768. size: aint;
  1769. usedef: tdef;
  1770. begin
  1771. (*
  1772. { These are default values of parameters. These should be encoded
  1773. via DW_AT_default_value, not as a separate sym. Moreover, their
  1774. type is not available when writing the debug info for external
  1775. procedures.
  1776. }
  1777. if (sym.owner.symtabletype=parasymtable) then
  1778. exit;
  1779. if ismember then
  1780. append_entry(DW_TAG_member,false,[
  1781. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  1782. { The DW_AT_declaration tag is invalid according to the DWARF specifications.
  1783. But gcc adds this to static const members and gdb checks
  1784. for this flag. So we have to set it also.
  1785. }
  1786. DW_AT_declaration,DW_FORM_flag,true,
  1787. DW_AT_external,DW_FORM_flag,true
  1788. ])
  1789. else
  1790. append_entry(DW_TAG_variable,false,[
  1791. DW_AT_name,DW_FORM_string,symname(sym, false)+#0
  1792. ]);
  1793. { for string constants, constdef isn't set because they have no real type }
  1794. case sym.consttyp of
  1795. conststring:
  1796. begin
  1797. { if DW_FORM_string is used below one day, this usedef should
  1798. probably become nil }
  1799. { note: < 255 instead of <= 255 because we have to store the
  1800. entire length of the string as well, and 256 does not fit in
  1801. a byte }
  1802. if (sym.value.len<255) then
  1803. usedef:=cshortstringtype
  1804. else
  1805. usedef:=clongstringtype;
  1806. end;
  1807. constresourcestring,
  1808. constwstring:
  1809. usedef:=nil;
  1810. else
  1811. usedef:=sym.constdef;
  1812. end;
  1813. if assigned(usedef) then
  1814. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  1815. AddConstToAbbrev(ord(DW_AT_const_value));
  1816. case sym.consttyp of
  1817. conststring:
  1818. begin
  1819. { DW_FORM_string isn't supported yet by the Pascal value printer
  1820. -> create a string using raw bytes }
  1821. if (sym.value.len<255) then
  1822. begin
  1823. AddConstToAbbrev(ord(DW_FORM_block1));
  1824. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  1825. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  1826. end
  1827. else
  1828. begin
  1829. AddConstToAbbrev(ord(DW_FORM_block));
  1830. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizesinttype.size));
  1831. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_sizeint_unaligned(sym.value.len));
  1832. end;
  1833. i:=0;
  1834. size:=sym.value.len;
  1835. while(i<size) do
  1836. begin
  1837. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  1838. inc(i);
  1839. end;
  1840. end;
  1841. constguid,
  1842. constset:
  1843. begin
  1844. AddConstToAbbrev(ord(DW_FORM_block1));
  1845. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  1846. i:=0;
  1847. size:=sym.constdef.size;
  1848. while (i<size) do
  1849. begin
  1850. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  1851. inc(i);
  1852. end;
  1853. end;
  1854. constwstring,
  1855. constresourcestring:
  1856. begin
  1857. { write dummy for now }
  1858. AddConstToAbbrev(ord(DW_FORM_string));
  1859. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  1860. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  1861. end;
  1862. constord:
  1863. begin
  1864. if (sym.value.valueord<0) then
  1865. begin
  1866. AddConstToAbbrev(ord(DW_FORM_sdata));
  1867. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  1868. end
  1869. else
  1870. begin
  1871. AddConstToAbbrev(ord(DW_FORM_udata));
  1872. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  1873. end;
  1874. end;
  1875. constnil:
  1876. begin
  1877. {$ifdef cpu64bitaddr}
  1878. AddConstToAbbrev(ord(DW_FORM_data8));
  1879. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(0));
  1880. {$else cpu64bitaddr}
  1881. AddConstToAbbrev(ord(DW_FORM_data4));
  1882. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(0));
  1883. {$endif cpu64bitaddr}
  1884. end;
  1885. constpointer:
  1886. begin
  1887. {$ifdef cpu64bitaddr}
  1888. AddConstToAbbrev(ord(DW_FORM_data8));
  1889. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(int64(sym.value.valueordptr)));
  1890. {$else cpu64bitaddr}
  1891. AddConstToAbbrev(ord(DW_FORM_data4));
  1892. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint(sym.value.valueordptr)));
  1893. {$endif cpu64bitaddr}
  1894. end;
  1895. constreal:
  1896. begin
  1897. AddConstToAbbrev(ord(DW_FORM_block1));
  1898. case tfloatdef(sym.constdef).floattype of
  1899. s32real:
  1900. begin
  1901. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  1902. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s32real(pbestreal(sym.value.valueptr)^));
  1903. end;
  1904. s64real:
  1905. begin
  1906. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  1907. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s64real(pbestreal(sym.value.valueptr)^));
  1908. end;
  1909. s64comp,
  1910. s64currency:
  1911. begin
  1912. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  1913. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(trunc(pbestreal(sym.value.valueptr)^)));
  1914. end;
  1915. s80real,
  1916. sc80real:
  1917. begin
  1918. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.constdef.size));
  1919. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s80real(pextended(sym.value.valueptr)^,sym.constdef.size));
  1920. end;
  1921. else
  1922. internalerror(200601291);
  1923. end;
  1924. end;
  1925. else
  1926. internalerror(200601292);
  1927. end;
  1928. finish_entry;
  1929. *)
  1930. end;
  1931. procedure TDebugInfoLLVM.appendsym_label(list:TAsmList;sym: tlabelsym);
  1932. begin
  1933. { ignore label syms for now, the problem is that a label sym
  1934. can have more than one label associated e.g. in case of
  1935. an inline procedure expansion }
  1936. end;
  1937. procedure TDebugInfoLLVM.appendsym_property(list:TAsmList;sym: tpropertysym);
  1938. var
  1939. symlist: ppropaccesslistitem;
  1940. tosym: tabstractvarsym;
  1941. offset: pint;
  1942. begin
  1943. (*
  1944. if assigned(sym.propaccesslist[palt_read]) and
  1945. not assigned(sym.propaccesslist[palt_read].procdef) then
  1946. symlist:=sym.propaccesslist[palt_read].firstsym
  1947. else
  1948. { can't handle }
  1949. exit;
  1950. if not get_symlist_sym_offset(symlist,tosym,offset) then
  1951. exit;
  1952. if not (tosym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1953. begin
  1954. if (tosym.typ=fieldvarsym) then
  1955. internalerror(2009031404);
  1956. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),sym.propdef,offset,[])
  1957. end
  1958. else
  1959. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym, false),sym.propdef,offset)
  1960. *)
  1961. end;
  1962. function TDebugInfoLLVM.symdebugname(sym: tsym): TSymStr;
  1963. begin
  1964. if ds_dwarf_cpp in current_settings.debugswitches then
  1965. begin
  1966. result:=sym.RealName;
  1967. if (result<>'') and
  1968. (result[1]='$') then
  1969. delete(result,1,1);
  1970. end
  1971. else
  1972. result:=sym.name
  1973. end;
  1974. procedure TDebugInfoLLVM.appendsym_type(list:TAsmList;sym: ttypesym);
  1975. begin
  1976. { just queue the def if needed, beforeappenddef will
  1977. emit the typedef if necessary }
  1978. get_def_metatai(sym.typedef);
  1979. {
  1980. if FindUnitSymtable(sym.Owner).iscurrentunit then
  1981. fretainedtypes.addvalue(def_meta_ref(sym.typedef));
  1982. }
  1983. end;
  1984. procedure TDebugInfoLLVM.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  1985. (*
  1986. var
  1987. templist : TAsmList;
  1988. blocksize : longint;
  1989. symlist : ppropaccesslistitem;
  1990. tosym: tabstractvarsym;
  1991. offset: pint;
  1992. flags: tdwarfvarsymflags;
  1993. *)
  1994. begin
  1995. (*
  1996. templist:=TAsmList.create;
  1997. case tabsolutevarsym(sym).abstyp of
  1998. toaddr :
  1999. begin
  2000. { MWE: replaced ifdef i368 }
  2001. {
  2002. if target_cpu = cpu_i386 then
  2003. begin
  2004. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2005. however I doubt that gdb supports this (FK) }
  2006. end;
  2007. }
  2008. templist.concat(tai_const.create_8bit(3));
  2009. {$ifdef avr}
  2010. // Add $800000 to indicate that the address is in memory space
  2011. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset + $800000, aitconst_ptr_unaligned));
  2012. {$else}
  2013. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset));
  2014. {$endif}
  2015. blocksize:=1+sizeof(puint);
  2016. end;
  2017. toasm :
  2018. begin
  2019. templist.concat(tai_const.create_8bit(3));
  2020. templist.concat(tai_const.create_type_name(aitconst_ptr_unaligned,sym.mangledname,0));
  2021. blocksize:=1+sizeof(puint);
  2022. end;
  2023. tovar:
  2024. begin
  2025. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2026. if get_symlist_sym_offset(symlist,tosym,offset) then
  2027. begin
  2028. if (tosym.typ=fieldvarsym) then
  2029. internalerror(2009031402);
  2030. flags:=[];
  2031. if (sym.owner.symtabletype=localsymtable) then
  2032. include(flags,dvf_force_local_var);
  2033. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),tabstractvarsym(sym).vardef,offset,flags);
  2034. end;
  2035. templist.free;
  2036. exit;
  2037. end;
  2038. end;
  2039. append_entry(DW_TAG_variable,false,[
  2040. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2041. {
  2042. DW_AT_decl_file,DW_FORM_data1,0,
  2043. DW_AT_decl_line,DW_FORM_data1,
  2044. }
  2045. DW_AT_external,DW_FORM_flag,true,
  2046. { data continues below }
  2047. DW_AT_location,DW_FORM_block1,blocksize
  2048. ]);
  2049. { append block data }
  2050. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2051. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2052. templist.free;
  2053. finish_entry;
  2054. *)
  2055. end;
  2056. procedure TDebugInfoLLVM.beforeappendsym(list:TAsmList;sym:tsym);
  2057. begin
  2058. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym, true))));
  2059. end;
  2060. procedure TDebugInfoLLVM.insertmoduleinfo;
  2061. var
  2062. culist: tai_llvmnamedmetadatanode;
  2063. dwarfversionflag: tai_llvmbasemetadatanode;
  2064. lang: tdwarf_source_language;
  2065. objcruntimeversion: longint;
  2066. begin
  2067. ensuremetainit;
  2068. if (ds_dwarf_cpp in current_settings.debugswitches) then
  2069. lang:=DW_LANG_C_plus_plus
  2070. else
  2071. lang:=DW_LANG_Pascal83;
  2072. { debug info header }
  2073. fcunode.addint64('language',ord(lang));
  2074. fcunode.addmetadatarefto('file',file_getmetanode(current_filepos.moduleindex,current_filepos.fileindex));
  2075. fcunode.addstring('producer','Free Pascal Compiler '+full_version_string);
  2076. fcunode.addboolean('isOptimized',cs_opt_level2 in current_settings.optimizerswitches);
  2077. if target_info.system in systems_objc_supported then
  2078. begin
  2079. if ([m_objectivec1,m_objectivec2]*current_settings.modeswitches)<>[] then
  2080. if target_info.system in systems_objc_nfabi then
  2081. objcruntimeversion:=2
  2082. else
  2083. objcruntimeversion:=1
  2084. else
  2085. objcruntimeversion:=0;
  2086. fcunode.addint64('runtimeVersion',objcruntimeversion);
  2087. end;
  2088. if cs_debuginfo in current_settings.moduleswitches then
  2089. fcunode.addenum('emissionKind','FullDebug')
  2090. else
  2091. fcunode.addenum('emissionKind','LineTablesOnly');
  2092. if fenums.valuecount<>0 then
  2093. begin
  2094. fcunode.addmetadatarefto('enums',fenums);
  2095. current_asmdata.AsmLists[al_dwarf_info].Concat(fenums);
  2096. end
  2097. else
  2098. begin
  2099. fcunode.addmetadatarefto('enums',nil);
  2100. fenums.free;
  2101. end;
  2102. fenums:=nil;
  2103. if fretainedtypes.valuecount<>0 then
  2104. begin
  2105. fcunode.addmetadatarefto('retainedTypes',fretainedtypes);
  2106. current_asmdata.AsmLists[al_dwarf_info].Concat(fretainedtypes);
  2107. end
  2108. else
  2109. begin
  2110. fcunode.addmetadatarefto('retainedTypes',nil);
  2111. fretainedtypes.free;
  2112. end;
  2113. fretainedtypes:=nil;
  2114. if fglobals.valuecount<>0 then
  2115. begin
  2116. fcunode.addmetadatarefto('globals',fglobals);
  2117. current_asmdata.AsmLists[al_dwarf_info].Concat(fglobals);
  2118. end
  2119. else
  2120. begin
  2121. fcunode.addmetadatarefto('globals',nil);
  2122. fglobals.free;
  2123. end;
  2124. fglobals:=nil;
  2125. current_asmdata.AsmLists[al_dwarf_info].Concat(femptyexpression);
  2126. femptyexpression:=nil;
  2127. if target_info.system in systems_darwin then
  2128. fcunode.addenum('nameTableKind','GNU');
  2129. current_asmdata.AsmLists[al_dwarf_info].Concat(fcunode);
  2130. culist:=tai_llvmnamedmetadatanode.create('llvm.dbg.cu');
  2131. current_asmdata.AsmLists[al_dwarf_info].Concat(culist);
  2132. culist.addvalue(llvm_getmetadatareftypedconst(fcunode));
  2133. resetfornewmodule;
  2134. end;
  2135. procedure TDebugInfoLLVM.inserttypeinfo;
  2136. var
  2137. storefilepos : tfileposinfo;
  2138. i : longint;
  2139. (*
  2140. lenstartlabel,arangestartlabel: tasmlabel;
  2141. *)
  2142. def: tdef;
  2143. (*
  2144. dbgname: string;
  2145. *)
  2146. vardatatype: ttypesym;
  2147. begin
  2148. ensuremetainit;
  2149. storefilepos:=current_filepos;
  2150. current_filepos:=current_module.mainfilepos;
  2151. vardatatype:=try_search_system_type('TVARDATA');
  2152. if assigned(vardatatype) then
  2153. vardatadef:=trecorddef(vardatatype.typedef);
  2154. collectglobalsyms;
  2155. { write all global/local variables. This will flag all required tdefs }
  2156. if assigned(current_module.globalsymtable) then
  2157. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2158. if assigned(current_module.localsymtable) then
  2159. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2160. { write all procedures and methods. This will flag all required tdefs }
  2161. if assigned(current_module.globalsymtable) then
  2162. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2163. if assigned(current_module.localsymtable) then
  2164. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2165. { reset unit type info flag }
  2166. reset_unit_type_info;
  2167. { write used types from the used units }
  2168. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  2169. { last write the types from this unit }
  2170. if assigned(current_module.globalsymtable) then
  2171. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2172. if assigned(current_module.localsymtable) then
  2173. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2174. { write defs not written yet }
  2175. write_remaining_defs_to_write(current_asmdata.asmlists[al_dwarf_info]);
  2176. { reset all def debug states for LLVMTypeInfo (which also uses this
  2177. field, to track for which types type info has been inserted already }
  2178. for i:=0 to defnumberlist.count-1 do
  2179. begin
  2180. def := tdef(defnumberlist[i]);
  2181. if assigned(def) then
  2182. def.dbg_state:=dbg_state_unused;
  2183. end;
  2184. current_filepos:=storefilepos;
  2185. end;
  2186. function TDebugInfoLLVM.symname(sym: tsym; manglename: boolean): TSymStr;
  2187. begin
  2188. if (sym.typ=paravarsym) and
  2189. (vo_is_self in tparavarsym(sym).varoptions) then
  2190. { We use 'this' for regular methods because that's what gdb triggers
  2191. on to automatically search fields. Don't do this for class methods,
  2192. because search class fields is not supported, and gdb 7.0+ fails
  2193. in this case because "this" is not a record in that case (it's a
  2194. pointer to a vmt) }
  2195. if not is_objc_class_or_protocol(tdef(sym.owner.defowner.owner.defowner)) and
  2196. not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  2197. result:='this'
  2198. else
  2199. result:='self'
  2200. else if (sym.typ=typesym) and
  2201. is_objc_class_or_protocol(ttypesym(sym).typedef) then
  2202. result:=tobjectdef(ttypesym(sym).typedef).objextname^
  2203. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  2204. (sym.typ=procsym) and
  2205. (tprocsym(sym).owner.symtabletype in [objectsymtable,recordsymtable]) then
  2206. begin
  2207. result:=tprocsym(sym).owner.name^+'__';
  2208. if manglename then
  2209. result := result + sym.name
  2210. else
  2211. result := result + symdebugname(sym);
  2212. end
  2213. else
  2214. begin
  2215. if manglename then
  2216. result := sym.name
  2217. else
  2218. result := symdebugname(sym);
  2219. end;
  2220. end;
  2221. function TDebugInfoLLVM.visibilitydiflag(vis: tvisibility): TSymStr;
  2222. begin
  2223. case vis of
  2224. vis_hidden,
  2225. vis_private,
  2226. vis_strictprivate:
  2227. result:='DIFlagPrivate';
  2228. vis_protected,
  2229. vis_strictprotected:
  2230. result:='DIFlagProtected';
  2231. vis_published,
  2232. vis_public:
  2233. result:='DIFlagPublic';
  2234. vis_none:
  2235. internalerror(2022050101);
  2236. end;
  2237. end;
  2238. procedure TDebugInfoLLVM.insertlineinfo(list:TAsmList);
  2239. var
  2240. hp: tai;
  2241. functionscope,
  2242. positionmeta: tai_llvmspecialisedmetadatanode;
  2243. procdeffileinfo: tfileposinfo;
  2244. nolineinfolevel : longint;
  2245. firstline: boolean;
  2246. begin
  2247. ensuremetainit;
  2248. hp:=tai(list.first);
  2249. while assigned(hp) and
  2250. ((hp.typ<>ait_llvmdecl) or
  2251. (taillvmdecl(hp).def.typ<>procdef)) do
  2252. begin
  2253. hp:=tai(hp.next);
  2254. end;
  2255. if not assigned(hp) then
  2256. exit;
  2257. procdeffileinfo:=tprocdef(taillvmdecl(hp).def).fileinfo;
  2258. { might trigger for certain kinds of internally generated code }
  2259. if procdeffileinfo.fileindex=0 then
  2260. exit;
  2261. functionscope:=def_meta_node(taillvmdecl(hp).def);
  2262. nolineinfolevel:=0;
  2263. hp:=tai(hp.next);
  2264. firstline:=true;
  2265. while assigned(hp) do
  2266. begin
  2267. case hp.typ of
  2268. ait_marker:
  2269. begin
  2270. case tai_marker(hp).kind of
  2271. mark_NoLineInfoStart:
  2272. inc(nolineinfolevel);
  2273. mark_NoLineInfoEnd:
  2274. dec(nolineinfolevel);
  2275. else
  2276. ;
  2277. end;
  2278. end;
  2279. else
  2280. ;
  2281. end;
  2282. if (hp.typ=ait_llvmins) and
  2283. ((nolineinfolevel=0) or
  2284. (taillvm(hp).llvmopcode=la_call)) then
  2285. begin
  2286. positionmeta:=nil;
  2287. { valid file -> add info }
  2288. if (tailineinfo(hp).fileinfo.fileindex<>0) then
  2289. begin
  2290. if firstline and
  2291. (nolineinfolevel=0) then
  2292. begin
  2293. functionscope.addint64('scopeLine',tailineinfo(hp).fileinfo.line);
  2294. firstline:=false;
  2295. end;
  2296. positionmeta:=filepos_getmetanode(tailineinfo(hp).fileinfo,procdeffileinfo,functionscope,nolineinfolevel<>0);
  2297. end
  2298. { LLVM requires line info for call instructions that may
  2299. potentially be inlined }
  2300. else if taillvm(hp).llvmopcode=la_call then
  2301. begin
  2302. positionmeta:=filepos_getmetanode(tailineinfo(hp).fileinfo,procdeffileinfo,functionscope,true);
  2303. end;
  2304. if assigned(positionmeta) then
  2305. taillvm(hp).addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',positionmeta));
  2306. end;
  2307. hp:=tai(hp.next);
  2308. end;
  2309. end;
  2310. {****************************************************************************
  2311. ****************************************************************************}
  2312. const
  2313. dbg_llvm_info : tdbginfo =
  2314. (
  2315. id : dbg_llvm;
  2316. idtxt : 'LLVM';
  2317. );
  2318. initialization
  2319. RegisterDebugInfo(dbg_llvm_info,TDebugInfoLLVM);
  2320. end.