dbgllvm.pas 114 KB

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