dbgllvm.pas 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922
  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_llvmspecialisedmetadatanode;
  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.DICompositeType)
  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_llvmspecialisedmetadatanode;
  363. begin
  364. result:=tai_llvmspecialisedmetadatanode(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. if (sp_static in field.symoptions) or
  1059. (field.visibility=vis_hidden) then
  1060. exit;
  1061. { start of a new variant part? }
  1062. if vo_is_first_field in field.varoptions then
  1063. begin
  1064. if not assigned(variantinfolist) then
  1065. begin
  1066. variantinfolist:=tfplist.create;
  1067. end;
  1068. varindex:=variantinfolist.count-1;
  1069. if (varindex=-1) or
  1070. (pvariantinfo(variantinfolist[varindex])^.startfield.fieldoffset<field.fieldoffset) then
  1071. begin
  1072. { more deeply nested variant }
  1073. uniondi:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType);
  1074. list.concat(uniondi);
  1075. fieldlist.addvalue(llvm_getmetadatareftypedconst(uniondi));
  1076. uniondi.addenum('tag','DW_TAG_union_type');
  1077. uniondi.addmetadatarefto('scope',scope);
  1078. try_add_file_metaref(uniondi,field.fileinfo,false);
  1079. { the size of this variant part is the total size of the
  1080. record minus the start of this field; not 100% correct
  1081. in case of multiple parallel nested variants, but not
  1082. really important since it's all padding anyway }
  1083. uniondi.addint64('size',cappedsize-min(field.bitoffset,cappedsize));
  1084. fieldlist:=tai_llvmunnamedmetadatanode.create;
  1085. list.concat(fieldlist);
  1086. uniondi.addmetadatarefto('elements',fieldlist);
  1087. scope:=uniondi;
  1088. new(variantinfo);
  1089. variantinfo^.startfield:=field;
  1090. variantinfo^.uniondi:=uniondi;
  1091. variantinfo^.variantfieldlist:=fieldlist;
  1092. variantinfo^.curvariantstructfieldlist:=nil;
  1093. variantinfolist.Add(variantinfo);
  1094. inc(varindex);
  1095. end
  1096. else
  1097. begin
  1098. {finalise more deeply nested variants }
  1099. while (varindex>=0) and
  1100. (pvariantinfo(variantinfolist[varindex])^.startfield.fieldoffset>field.fieldoffset) do
  1101. begin
  1102. dispose(pvariantinfo(variantinfolist[varindex]));
  1103. dec(varindex);
  1104. end;
  1105. if (varindex<0) then
  1106. internalerror(2022060610);
  1107. variantinfo:=pvariantinfo(variantinfolist[varindex]);
  1108. if variantinfo^.startfield.fieldoffset<>field.fieldoffset then
  1109. internalerror(2022060611);
  1110. { a variant part is always the last part -> end of previous
  1111. struct, if any}
  1112. variantinfo^.curvariantstructfieldlist:=nil;
  1113. fieldlist:=variantinfo^.variantfieldlist;
  1114. scope:=variantinfo^.uniondi;
  1115. { variant at the same level as a previous one }
  1116. variantinfolist.count:=varindex+1;
  1117. end;
  1118. if not variantfieldstartsnewstruct(field,recst,i) then
  1119. begin
  1120. variantinfo^.curvariantstructfieldlist:=nil;
  1121. fieldlist:=variantinfo^.variantfieldlist;
  1122. scope:=variantinfo^.uniondi;
  1123. end
  1124. else
  1125. begin
  1126. structdi:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType);
  1127. list.concat(structdi);
  1128. structdi.addenum('tag','DW_TAG_structure_type');
  1129. structdi.addmetadatarefto('scope',variantinfo^.uniondi);
  1130. structdi.addint64('size',cappedsize-min(field.bitoffset,cappedsize));
  1131. variantinfo^.curvariantstructfieldlist:=tai_llvmunnamedmetadatanode.create;
  1132. list.concat(variantinfo^.curvariantstructfieldlist);
  1133. structdi.addmetadatarefto('elements',variantinfo^.curvariantstructfieldlist);
  1134. fieldlist.addvalue(llvm_getmetadatareftypedconst(structdi));
  1135. fieldlist:=variantinfo^.curvariantstructfieldlist;
  1136. scope:=structdi;
  1137. end;
  1138. end;
  1139. fielddi:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType);
  1140. fielddi.addenum('tag','DW_TAG_member');
  1141. fielddi.addstring('name',symname(field,false));
  1142. fielddi.addmetadatarefto('scope',scope);
  1143. try_add_file_metaref(fielddi,field.fileinfo,false);
  1144. fielddi.addmetadatarefto('baseType',def_meta_node(field.vardef));
  1145. if bpackedrecst and
  1146. is_ordinal(field.vardef) then
  1147. fielddi.addqword('size',field.getpackedbitsize)
  1148. else
  1149. fielddi.addqword('size',min(asizeuint(field.getsize)*8,cappedsize));
  1150. bitoffset:=bitoffsetfromvariantstart(field,variantinfolist,cappedsize);
  1151. if bitoffset<>0 then
  1152. fielddi.addqword('offset',bitoffset);
  1153. fieldlist.addvalue(llvm_getmetadatareftypedconst(fielddi));
  1154. list.concat(fielddi);
  1155. end;
  1156. if assigned(variantinfolist) then
  1157. begin
  1158. for i:=0 to variantinfolist.count-1 do
  1159. begin
  1160. dispose(pvariantinfo(variantinfolist[i]));
  1161. end;
  1162. end;
  1163. variantinfolist.free;
  1164. end;
  1165. procedure TDebugInfoLLVM.appenddef_pointer(list:TAsmList;def:tpointerdef);
  1166. var
  1167. dinode: tai_llvmspecialisedmetadatanode;
  1168. begin
  1169. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1170. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1171. if not(is_voidpointer(def)) then
  1172. dinode.addmetadatarefto('baseType',def_meta_node(def.pointeddef))
  1173. else
  1174. dinode.addmetadatarefto('baseType',nil);
  1175. list.concat(dinode);
  1176. end;
  1177. procedure TDebugInfoLLVM.appenddef_formal(list: TAsmList; def: tformaldef);
  1178. var
  1179. dinode: tai_llvmspecialisedmetadatanode;
  1180. begin
  1181. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1182. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1183. dinode.addmetadatarefto('baseType',nil);
  1184. list.concat(dinode);
  1185. end;
  1186. procedure TDebugInfoLLVM.appenddef_string(list:TAsmList;def:tstringdef);
  1187. procedure addnormalstringdef(const name: TSymStr; lendef: tdef; maxlen: asizeuint);
  1188. var
  1189. dinode,
  1190. subrangenode,
  1191. exprnode: tai_llvmspecialisedmetadatanode;
  1192. arrayrangenode: tai_aggregatetypedconst;
  1193. { maxlen can be > high(int64) }
  1194. slen : asizeuint;
  1195. arr : tasmlabel;
  1196. begin
  1197. { fix length of openshortstring }
  1198. slen:=aword(def.len);
  1199. if (slen=0) or
  1200. (slen>maxlen) then
  1201. slen:=maxlen;
  1202. appenddef_array_internal(list,def,cansichartype,0,slen);
  1203. end;
  1204. var
  1205. dinode: tai_llvmspecialisedmetadatanode;
  1206. begin
  1207. case def.stringtype of
  1208. st_shortstring:
  1209. begin
  1210. addnormalstringdef('ShortString',u8inttype,255);
  1211. end;
  1212. st_longstring:
  1213. begin
  1214. { a) we don't actually support variables of this type currently
  1215. b) this type is only used as the type for constant strings
  1216. > 255 characters
  1217. c) in such a case, gdb will allocate and initialise enough
  1218. memory to hold the maximum size for such a string
  1219. -> don't use high(qword)/high(cardinal) as maximum, since that
  1220. will cause exhausting the VM space, but some "reasonably high"
  1221. number that should be enough for most constant strings
  1222. }
  1223. {$ifdef cpu64bitaddr}
  1224. addnormalstringdef('LongString',u64inttype,qword(1024*1024));
  1225. {$endif cpu64bitaddr}
  1226. {$ifdef cpu32bitaddr}
  1227. addnormalstringdef('LongString',u32inttype,cardinal(1024*1024));
  1228. {$endif cpu32bitaddr}
  1229. {$ifdef cpu16bitaddr}
  1230. addnormalstringdef('LongString',u16inttype,cardinal(1024));
  1231. {$endif cpu16bitaddr}
  1232. end;
  1233. st_ansistring:
  1234. begin
  1235. // Todo: dynamic length "array"
  1236. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1237. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1238. dinode.addmetadatarefto('baseType',def_meta_node(cansichartype));
  1239. list.concat(dinode);
  1240. end;
  1241. st_unicodestring,
  1242. st_widestring:
  1243. begin
  1244. // Todo: dynamic length "array"
  1245. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1246. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1247. dinode.addmetadatarefto('baseType',def_meta_node(cwidechartype));
  1248. list.concat(dinode);
  1249. end;
  1250. end;
  1251. end;
  1252. procedure TDebugInfoLLVM.appenddef_procvar(list:TAsmList;def:tprocvardef);
  1253. var
  1254. dinode: tai_llvmspecialisedmetadatanode;
  1255. begin
  1256. { plain pointer for now }
  1257. if def.is_addressonly then
  1258. begin
  1259. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1260. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1261. dinode.addmetadatarefto('baseType',nil);
  1262. list.concat(dinode);
  1263. end
  1264. else
  1265. begin
  1266. appenddef_array_internal(list,def,voidcodepointertype,1,2);
  1267. end;
  1268. end;
  1269. procedure TDebugInfoLLVM.appenddef_file(list: TAsmList; def: tfiledef);
  1270. var
  1271. dinode: tai_llvmspecialisedmetadatanode;
  1272. begin
  1273. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  1274. dinode.addint64('tag',ord(DW_TAG_structure_type));
  1275. if assigned(def.typesym) then
  1276. dinode.addstring('name',symname(def.typesym, false));
  1277. dinode.addqword('size',def.size*8);
  1278. list.concat(dinode);
  1279. end;
  1280. procedure TDebugInfoLLVM.appenddef_object(list: TAsmList; def: tobjectdef);
  1281. var
  1282. dinode,
  1283. structdi,
  1284. inheritancedi: tai_llvmspecialisedmetadatanode;
  1285. fields: tai_llvmunnamedmetadatanode;
  1286. begin
  1287. inheritancedi:=nil;
  1288. fields:=tai_llvmunnamedmetadatanode.create;
  1289. if assigned(def.childof) then
  1290. begin
  1291. inheritancedi:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType);
  1292. list.concat(inheritancedi);
  1293. inheritancedi.addenum('tag','DW_TAG_inheritance');
  1294. if is_implicit_pointer_object_type(def) then
  1295. inheritancedi.addmetadatarefto('baseType',def_meta_class_struct(def.childof))
  1296. else
  1297. inheritancedi.addmetadatarefto('baseType',def_meta_node(def.childof));
  1298. { Pascal only has public inheritance }
  1299. if def.objecttype<>odt_cppclass then
  1300. inheritancedi.addenum('flags','DIFlagPublic');
  1301. fields.addvalue(llvm_getmetadatareftypedconst(inheritancedi));
  1302. end;
  1303. if is_implicit_pointer_object_type(def) then
  1304. begin
  1305. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1306. dinode.addenum('tag','DW_TAG_pointer_type');
  1307. structdi:=def_meta_class_struct(def);
  1308. list.concat(structdi);
  1309. structdi.addenum('tag','DW_TAG_class_type');
  1310. appenddef_struct_named(list,def,structdi,fields,def.objname^);
  1311. { implicit pointer }
  1312. dinode.addmetadatarefto('baseType',structdi);
  1313. end
  1314. else case def.objecttype of
  1315. odt_cppclass,
  1316. odt_object:
  1317. begin
  1318. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  1319. dinode.addenum('tag','DW_TAG_class_type');
  1320. appenddef_struct_named(list,def,dinode,fields,def.objname^);
  1321. end;
  1322. odt_objcclass:
  1323. begin
  1324. { Objective-C class: same as regular class, except for
  1325. a) Apple-specific tag that identifies it as an Objective-C class
  1326. b) use extname^ instead of objname
  1327. }
  1328. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DICompositeType));
  1329. dinode.addenum('tag','DW_TAG_class_type');
  1330. dinode.addenum('runtimeLang','DW_LANG_ObjC');
  1331. appenddef_struct_named(list,def,dinode,fields,def.objextname^);
  1332. end;
  1333. odt_objcprotocol:
  1334. begin
  1335. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1336. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1337. dinode.addmetadatarefto('baseType',nil);
  1338. end;
  1339. else
  1340. internalerror(2022060710);
  1341. end;
  1342. list.concat(dinode);
  1343. if assigned(inheritancedi) then
  1344. inheritancedi.addmetadatarefto('scope',dinode);
  1345. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.symtable);
  1346. end;
  1347. procedure TDebugInfoLLVM.appenddef_set(list: TAsmList; def: tsetdef);
  1348. begin
  1349. appenddef_array_internal(list,def,u8inttype,0,def.size-1);
  1350. end;
  1351. procedure TDebugInfoLLVM.appenddef_undefined(list: TAsmList; def: tundefineddef);
  1352. var
  1353. dinode: tai_llvmspecialisedmetadatanode;
  1354. begin
  1355. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1356. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1357. dinode.addmetadatarefto('baseType',nil);
  1358. list.concat(dinode);
  1359. end;
  1360. procedure TDebugInfoLLVM.appenddef_classref(list: TAsmList; def: tclassrefdef);
  1361. var
  1362. dinode: tai_llvmspecialisedmetadatanode;
  1363. begin
  1364. dinode:=def_set_meta_impl(def,tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType));
  1365. dinode.addint64('tag',ord(DW_TAG_pointer_type));
  1366. dinode.addmetadatarefto('baseType',nil);
  1367. list.concat(dinode);
  1368. end;
  1369. procedure TDebugInfoLLVM.appenddef_variant(list: TAsmList; def: tvariantdef);
  1370. begin
  1371. if assigned(vardatadef) then
  1372. appenddef_record_named(list,def,trecorddef(vardatadef),'Variant');
  1373. end;
  1374. procedure TDebugInfoLLVM.afterappenddef(list:TAsmList;def:tdef);
  1375. var
  1376. tempdinode,
  1377. refdinode,
  1378. impldinode: tai_llvmspecialisedmetadatanode;
  1379. begin
  1380. if def.typ=procdef then
  1381. exit;
  1382. if is_void(def) then
  1383. exit;
  1384. refdinode:=def_meta_node(def);
  1385. if is_objc_class_or_protocol(def) then
  1386. begin
  1387. { for Objective-C classes, the named typedef must refer to the
  1388. struct itself, not to the pointer of the struct; Objective-C
  1389. classes are not implicit pointers in Objective-C itself, only
  1390. in FPC. So make the def label point to a pointer to the
  1391. typedef, which in turn refers to the actual struct (for Delphi-
  1392. style classes, the def points to the typedef, which refers to
  1393. a pointer to the actual struct) }
  1394. { implicit pointer }
  1395. tempdinode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIDerivedType);
  1396. refdinode.addint64('tag',ord(DW_TAG_pointer_type));
  1397. refdinode.addmetadatarefto('baseType',tempdinode);
  1398. list.concat(refdinode);
  1399. { typedef }
  1400. refdinode:=tempdinode;
  1401. end;
  1402. refdinode.addint64('tag',ord(DW_TAG_typedef));
  1403. if assigned(def.typesym) and
  1404. not(df_generic in def.defoptions) then
  1405. begin
  1406. refdinode.addstring('name',symname(def.typesym,false));
  1407. try_add_file_metaref(refdinode,def.typesym.fileinfo,false);
  1408. end;
  1409. impldinode:=def_meta_impl(def);
  1410. if not assigned(impldinode) then
  1411. internalerror(2021120501);
  1412. refdinode.addmetadatarefto('baseType',impldinode);
  1413. list.concat(refdinode);
  1414. end;
  1415. procedure TDebugInfoLLVM.appendprocdef(list:TAsmList; def:tprocdef);
  1416. procedure adddispflags(dinode: tai_llvmspecialisedmetadatanode; is_definition, is_virtual: boolean);
  1417. var
  1418. dispflags: TSymStr;
  1419. islocal: boolean;
  1420. begin
  1421. islocal:=
  1422. not((po_global in def.procoptions) and
  1423. (def.parast.symtablelevel<=normal_function_level));
  1424. adddefinitionlocal(dinode,is_definition,islocal,not(llvmflag_NoDISPFlags in llvmversion_properties[current_settings.llvmversion]),dispflags);
  1425. if llvmflag_NoDISPFlags in llvmversion_properties[current_settings.llvmversion] then
  1426. begin
  1427. if is_virtual then
  1428. begin
  1429. if not(po_abstractmethod in def.procoptions) then
  1430. dinode.addenum('virtuality','DW_VIRTUALITY_virtual')
  1431. else
  1432. dinode.addenum('virtuality','DW_VIRTUALITY_pure_virtual');
  1433. end;
  1434. exit;
  1435. end;
  1436. if is_virtual then
  1437. begin
  1438. if dispflags<>'' then
  1439. dispflags:=dispflags+'|';
  1440. if not(po_abstractmethod in def.procoptions) then
  1441. dispflags:=dispflags+'DISPFlagVirtual'
  1442. else
  1443. dispflags:=dispflags+'DISPFlagPureVirtual';
  1444. end
  1445. else
  1446. begin
  1447. { this one will always be a definition, so no need to check
  1448. whether result is empty }
  1449. if not(llvmflag_NoDISPFlagMainSubprogram in llvmversion_properties[current_settings.llvmversion]) and
  1450. (def.proctypeoption=potype_proginit) then
  1451. dispflags:=dispflags+'|DISPFlagMainSubprogram';
  1452. end;
  1453. if dispflags<>'' then
  1454. dinode.addenum('spFlags',dispflags);
  1455. end;
  1456. procedure adddiflags(dinode: tai_llvmspecialisedmetadatanode; is_definition: boolean);
  1457. var
  1458. diflags: TSymStr;
  1459. begin
  1460. if (llvmflag_NoDISPFlagMainSubprogram in llvmversion_properties[current_settings.llvmversion]) and
  1461. (def.proctypeoption=potype_proginit) then
  1462. diflags:='DIFlagMainSubprogram'
  1463. else if def.owner.symtabletype in [objectsymtable,recordsymtable] then
  1464. diflags:=visibilitydiflag(def.visibility)
  1465. else
  1466. diflags:='';
  1467. if diflags<>'' then
  1468. dinode.addenum('flags',diflags);
  1469. end;
  1470. var
  1471. dinode,
  1472. ditypenode : tai_llvmspecialisedmetadatanode;
  1473. fileref : tai_simpletypedconst;
  1474. procdeftai : tai;
  1475. st : tsymtable;
  1476. vmtoffset : pint;
  1477. flags : TSymStr;
  1478. in_currentunit,
  1479. is_virtual : boolean;
  1480. begin
  1481. { only write debug info for procedures defined in the current module,
  1482. except in case of methods (clang-compatible)
  1483. }
  1484. in_currentunit:=def.in_currentunit;
  1485. if not in_currentunit and
  1486. not (def.owner.symtabletype in [objectsymtable,recordsymtable]) then
  1487. exit;
  1488. { happens for init procdef of units without init section }
  1489. if in_currentunit and
  1490. not assigned(def.procstarttai) then
  1491. exit;
  1492. { These don't contain a taillvmdecl, they are completely generated
  1493. in native assembly. If we want to add debug information to these,
  1494. we have to do it using the regular debug info generation }
  1495. if po_assembler in def.procoptions then
  1496. exit;
  1497. if df_generic in def.defoptions then
  1498. exit;
  1499. { Procdefs are not handled by the regular def writing code, so
  1500. dbg_state is not set/checked for them. Do it here. }
  1501. if (def.dbg_state in [dbg_state_writing,dbg_state_written]) then
  1502. exit;
  1503. defnumberlist.Add(def);
  1504. def.dbg_state:=dbg_state_writing;
  1505. { difference compared to other kinds of defs: the DISubProgram gets
  1506. created directly in get_def_metatai because a typedef for a
  1507. DISubProgram does not make sense and is not supported by LLVM ->
  1508. don't set the implementation of the metadata def here and just use
  1509. the regular node }
  1510. dinode:=def_meta_node(def);
  1511. list.concat(dinode);
  1512. { we have to attach the debug info to the definition instruction of the
  1513. proc }
  1514. procdeftai:=nil;
  1515. if in_currentunit then
  1516. begin
  1517. procdeftai:=def.procstarttai;
  1518. if (procdeftai.typ<>ait_llvmdecl) or
  1519. (taillvmdecl(procdeftai).def<>def) then
  1520. internalerror(2022022010);
  1521. taillvmdecl(procdeftai).addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',dinode));
  1522. end;
  1523. dinode.addstring('name',symdebugname(def.procsym));
  1524. try_add_file_metaref(dinode,def.fileinfo,true);
  1525. if not(cs_debuginfo in current_settings.moduleswitches) then
  1526. begin
  1527. def.dbg_state:=dbg_state_written;
  1528. exit;
  1529. end;
  1530. is_virtual:=
  1531. (([po_abstractmethod, po_virtualmethod, po_overridingmethod]*def.procoptions)<>[]) and
  1532. not is_objc_class_or_protocol(def.struct) and
  1533. not is_objectpascal_helper(def.struct);
  1534. adddispflags(dinode,in_currentunit,is_virtual);
  1535. if is_virtual then
  1536. begin
  1537. { the sizeof(pint) is a bit iffy, since vmtmethodoffset() calculates
  1538. using a combination of voidcodepointer.size, voidpointer.size, and
  1539. sizeof(pint). But that's what the debugger will use }
  1540. dinode.addint64('virtualIndex',tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber) div sizeof(pint));
  1541. {$ifdef extdebug}
  1542. if (tobjectdef(def.owner.defowner).vmtmethodoffset(def.extnumber) mod sizeof(pint))<>0 then
  1543. internalerror(2022043001);
  1544. {$endif}
  1545. end;
  1546. adddiflags(dinode,in_currentunit);
  1547. dinode.addmetadatarefto('unit',fcunode);
  1548. ditypenode:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DISubroutineType);
  1549. ditypenode.addmetadatarefto('types',getabstractprocdeftypes(list,def));
  1550. list.concat(ditypenode);
  1551. dinode.addmetadatarefto('type',ditypenode);
  1552. (*
  1553. if assigned(def.parast) then
  1554. begin
  1555. { First insert self, because gdb uses the fact whether or not the
  1556. first parameter of a method is artificial to distinguish static
  1557. from regular methods. }
  1558. { fortunately, self is the always the first parameter in the
  1559. paralist, since it has the lowest paranr. Note that this is not
  1560. true for Objective-C, but those methods are detected in
  1561. another way (by reading the ObjC run time information) }
  1562. write_symtable_parasyms(current_asmdata.asmlists[al_dwarf_info],def.paras);
  1563. end;
  1564. { local type defs and vars should not be written
  1565. inside the main proc }
  1566. if in_currentunit and
  1567. assigned(def.localst) and
  1568. (def.localst.symtabletype=localsymtable) then
  1569. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1570. { last write the types from this procdef }
  1571. if assigned(def.parast) then
  1572. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.parast);
  1573. { only try to write the localst if the routine is implemented here }
  1574. if in_currentunit and
  1575. assigned(def.localst) and
  1576. (def.localst.symtabletype=localsymtable) then
  1577. begin
  1578. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1579. { Write nested procedures -- disabled, see scope check at the
  1580. beginning; currently, these are still written in the global
  1581. scope. }
  1582. // write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],def.localst);
  1583. end;
  1584. finish_children;
  1585. *)
  1586. def.dbg_state:=dbg_state_written;
  1587. end;
  1588. procedure TDebugInfoLLVM.adddefinitionlocal(dinode: tai_llvmspecialisedmetadatanode; definition, local, usedispflags: boolean; out dispFlags: tsymstr);
  1589. begin
  1590. dispflags:='';
  1591. if not usedispflags then
  1592. begin
  1593. dinode.addboolean('isDefinition',definition);
  1594. if definition then
  1595. begin
  1596. dinode.addboolean('isLocal',local);
  1597. end;
  1598. exit;
  1599. end;
  1600. if definition then
  1601. begin
  1602. dispflags:='DISPFlagDefinition';
  1603. if local then
  1604. dispflags:=dispflags+'|DISPFlagLocalToUnit';
  1605. end;
  1606. end;
  1607. function TDebugInfoLLVM.get_symlist_sym_offset(symlist: ppropaccesslistitem; out sym: tabstractvarsym; out offset: pint): boolean;
  1608. (*
  1609. var
  1610. elesize : pint;
  1611. currdef : tdef;
  1612. indirection: boolean;
  1613. *)
  1614. begin
  1615. result:=false;
  1616. (*
  1617. if not assigned(symlist) then
  1618. exit;
  1619. sym:=nil;
  1620. offset:=0;
  1621. currdef:=nil;
  1622. indirection:=false;
  1623. repeat
  1624. case symlist^.sltype of
  1625. sl_load:
  1626. begin
  1627. if assigned(sym) then
  1628. internalerror(2009031203);
  1629. if not(symlist^.sym.typ in [paravarsym,localvarsym,staticvarsym,fieldvarsym]) then
  1630. { can't handle... }
  1631. exit;
  1632. sym:=tabstractvarsym(symlist^.sym);
  1633. currdef:=tabstractvarsym(sym).vardef;
  1634. if ((sym.typ=paravarsym) and
  1635. paramanager.push_addr_param(tparavarsym(sym).varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption)) then
  1636. indirection:=true;
  1637. end;
  1638. sl_subscript:
  1639. begin
  1640. if not assigned(currdef) then
  1641. internalerror(2009031301);
  1642. if (symlist^.sym.typ<>fieldvarsym) then
  1643. internalerror(2009031202);
  1644. { can't handle offsets with indirections yet }
  1645. if indirection then
  1646. exit;
  1647. if is_packed_record_or_object(currdef) then
  1648. begin
  1649. { can't calculate the address of a non-byte aligned field }
  1650. if (tfieldvarsym(symlist^.sym).fieldoffset mod 8) <> 0 then
  1651. exit;
  1652. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset div 8)
  1653. end
  1654. else
  1655. inc(offset,tfieldvarsym(symlist^.sym).fieldoffset);
  1656. currdef:=tfieldvarsym(symlist^.sym).vardef;
  1657. end;
  1658. sl_absolutetype,
  1659. sl_typeconv:
  1660. begin
  1661. currdef:=symlist^.def;
  1662. { ignore, these don't change the address }
  1663. end;
  1664. sl_vec:
  1665. begin
  1666. if not assigned(currdef) or
  1667. (currdef.typ<>arraydef) then
  1668. internalerror(2009031201);
  1669. { can't handle offsets with indirections yet }
  1670. if indirection then
  1671. exit;
  1672. if not is_packed_array(currdef) then
  1673. elesize:=tarraydef(currdef).elesize
  1674. else
  1675. begin
  1676. elesize:=tarraydef(currdef).elepackedbitsize;
  1677. { can't calculate the address of a non-byte aligned element }
  1678. if (elesize mod 8)<>0 then
  1679. exit;
  1680. elesize:=elesize div 8;
  1681. end;
  1682. inc(offset,(symlist^.value.svalue-tarraydef(currdef).lowrange)*elesize);
  1683. currdef:=tarraydef(currdef).elementdef;
  1684. end;
  1685. else
  1686. internalerror(2009031403);
  1687. end;
  1688. symlist:=symlist^.next;
  1689. until not assigned(symlist);
  1690. if not assigned(sym) then
  1691. internalerror(2009031205);
  1692. result:=true;
  1693. *)
  1694. end;
  1695. procedure TDebugInfoLLVM.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  1696. begin
  1697. // appendsym_var_with_name_type_offset(list,sym,symname(sym, false),sym.vardef,0,[]);
  1698. end;
  1699. procedure TDebugInfoLLVM.appendsym_var_with_name_type_offset(list:TAsmList; sym:tabstractnormalvarsym; const name: TSymStr; def: tdef; offset: pint(*; const flags: tdwarfvarsymflags*));
  1700. (*
  1701. var
  1702. templist : TAsmList;
  1703. blocksize,size_of_int : longint;
  1704. tag : tdwarf_tag;
  1705. has_high_reg : boolean;
  1706. dreg,dreghigh : shortint;
  1707. {$ifdef i8086}
  1708. has_segment_sym_name : boolean=false;
  1709. segment_sym_name : TSymStr='';
  1710. segment_reg: TRegister=NR_NO;
  1711. {$endif i8086}
  1712. *)
  1713. begin
  1714. (*
  1715. if vo_is_external in sym.varoptions then
  1716. exit;
  1717. blocksize:=0;
  1718. dreghigh:=0;
  1719. { There is no space allocated for not referenced locals }
  1720. if (sym.owner.symtabletype=localsymtable) and (sym.refs=0) then
  1721. exit;
  1722. templist:=TAsmList.create;
  1723. case sym.localloc.loc of
  1724. LOC_REGISTER,
  1725. LOC_CREGISTER,
  1726. LOC_MMREGISTER,
  1727. LOC_CMMREGISTER,
  1728. LOC_FPUREGISTER,
  1729. LOC_CFPUREGISTER :
  1730. begin
  1731. { dwarf_reg_no_error might return -1
  1732. in case the register variable has been optimized out }
  1733. dreg:=dwarf_reg_no_error(sym.localloc.register);
  1734. has_high_reg:=(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and (sym.localloc.registerhi<>NR_NO);
  1735. if has_high_reg then
  1736. dreghigh:=dwarf_reg_no_error(sym.localloc.registerhi);
  1737. if dreghigh=-1 then
  1738. has_high_reg:=false;
  1739. if (sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  1740. (sym.typ=paravarsym) and
  1741. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1742. not(vo_has_local_copy in sym.varoptions) and
  1743. not is_open_string(sym.vardef) and (dreg>=0) then
  1744. begin
  1745. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  1746. templist.concat(tai_const.create_uleb128bit(dreg));
  1747. templist.concat(tai_const.create_sleb128bit(0));
  1748. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(0);
  1749. end
  1750. else
  1751. begin
  1752. if has_high_reg then
  1753. begin
  1754. templist.concat(tai_comment.create(strpnew('high:low reg pair variable')));
  1755. size_of_int:=sizeof(aint);
  1756. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1757. templist.concat(tai_const.create_uleb128bit(dreg));
  1758. blocksize:=1+Lengthuleb128(dreg);
  1759. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  1760. templist.concat(tai_const.create_uleb128bit(size_of_int));
  1761. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  1762. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1763. templist.concat(tai_const.create_uleb128bit(dreghigh));
  1764. blocksize:=blocksize+1+Lengthuleb128(dreghigh);
  1765. templist.concat(tai_const.create_8bit(ord(DW_OP_piece)));
  1766. templist.concat(tai_const.create_uleb128bit(size_of_int));
  1767. blocksize:=blocksize+1+Lengthuleb128(size_of_int);
  1768. end
  1769. else if (dreg>=0) then
  1770. begin
  1771. templist.concat(tai_const.create_8bit(ord(DW_OP_regx)));
  1772. templist.concat(tai_const.create_uleb128bit(dreg));
  1773. blocksize:=1+Lengthuleb128(dreg);
  1774. end;
  1775. end;
  1776. end;
  1777. else
  1778. begin
  1779. case sym.typ of
  1780. staticvarsym:
  1781. begin
  1782. if vo_is_thread_var in sym.varoptions then
  1783. begin
  1784. if tf_section_threadvars in target_info.flags then
  1785. begin
  1786. case sizeof(puint) of
  1787. 2:
  1788. templist.concat(tai_const.create_8bit(ord(DW_OP_const2u)));
  1789. 4:
  1790. templist.concat(tai_const.create_8bit(ord(DW_OP_const4u)));
  1791. 8:
  1792. templist.concat(tai_const.create_8bit(ord(DW_OP_const8u)));
  1793. else
  1794. Internalerror(2019100501);
  1795. end;
  1796. {$push}
  1797. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  1798. templist.concat(tai_const.Create_type_name(aitconst_dtpoff,sym.mangledname,0));
  1799. { so far, aitconst_dtpoff is solely 32 bit }
  1800. if (sizeof(puint)=8) and (target_info.endian=endian_little) then
  1801. templist.concat(tai_const.create_32bit(0));
  1802. templist.concat(tai_const.create_8bit(ord(DW_OP_GNU_push_tls_address)));
  1803. if (sizeof(puint)=8) and (target_info.endian=endian_big) then
  1804. templist.concat(tai_const.create_32bit(0));
  1805. {$pop}
  1806. blocksize:=2+sizeof(puint);
  1807. end
  1808. else
  1809. begin
  1810. { TODO: !!! FIXME: dwarf for thread vars !!!}
  1811. { This is only a minimal change to at least be able to get a value
  1812. in only one thread is present PM 2014-11-21, like for stabs format }
  1813. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  1814. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,
  1815. offset+sizeof(pint)));
  1816. blocksize:=1+sizeof(puint);
  1817. end;
  1818. end
  1819. else
  1820. begin
  1821. templist.concat(tai_const.create_8bit(ord(DW_OP_addr)));
  1822. templist.concat(tai_const.Create_type_name(aitconst_ptr_unaligned,sym.mangledname,offset));
  1823. blocksize:=1+sizeof(puint);
  1824. {$ifdef i8086}
  1825. segment_sym_name:=sym.mangledname;
  1826. has_segment_sym_name:=true;
  1827. {$endif i8086}
  1828. end;
  1829. end;
  1830. paravarsym,
  1831. localvarsym:
  1832. begin
  1833. { Happens when writing debug info for paras of procdefs not
  1834. implemented in the current module. Can't add a general check
  1835. for LOC_INVALID above, because staticvarsyms may also have it.
  1836. }
  1837. if sym.localloc.loc<> LOC_INVALID then
  1838. begin
  1839. if is_fbreg(sym.localloc.reference.base) then
  1840. begin
  1841. templist.concat(tai_const.create_8bit(ord(DW_OP_fbreg)));
  1842. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1843. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  1844. end
  1845. else
  1846. begin
  1847. dreg:=dwarf_reg(sym.localloc.reference.base);
  1848. if dreg<=31 then
  1849. begin
  1850. templist.concat(tai_const.create_8bit(ord(DW_OP_breg0)+dreg));
  1851. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1852. blocksize:=1+Lengthsleb128(sym.localloc.reference.offset+offset);
  1853. end
  1854. else
  1855. begin
  1856. templist.concat(tai_const.create_8bit(ord(DW_OP_bregx)));
  1857. templist.concat(tai_const.create_uleb128bit(dreg));
  1858. templist.concat(tai_const.create_sleb128bit(sym.localloc.reference.offset+offset));
  1859. blocksize:=1+Lengthuleb128(dreg)+LengthSleb128(sym.localloc.reference.offset+offset);
  1860. end;
  1861. end;
  1862. {$ifdef i8086}
  1863. segment_reg:=sym.localloc.reference.segment;
  1864. {$endif i8086}
  1865. {$ifndef gdb_supports_DW_AT_variable_parameter}
  1866. { Parameters which are passed by reference. (var and the like)
  1867. Hide the reference-pointer and dereference the pointer
  1868. in the DW_AT_location block.
  1869. }
  1870. if (sym.typ=paravarsym) and
  1871. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1872. not(vo_has_local_copy in sym.varoptions) and
  1873. not is_open_string(sym.vardef) then
  1874. begin
  1875. templist.concat(tai_const.create_8bit(ord(DW_OP_deref)));
  1876. inc(blocksize);
  1877. end
  1878. {$endif not gdb_supports_DW_AT_variable_parameter}
  1879. end;
  1880. end
  1881. else
  1882. internalerror(200601288);
  1883. end;
  1884. end;
  1885. end;
  1886. { function results must not be added to the parameter list,
  1887. as they are not part of the signature of the function
  1888. (gdb automatically adds them according to the ABI specifications
  1889. when calling the function)
  1890. }
  1891. if (sym.typ=paravarsym) and
  1892. not(dvf_force_local_var in flags) and
  1893. not(vo_is_funcret in sym.varoptions) then
  1894. tag:=DW_TAG_formal_parameter
  1895. else
  1896. tag:=DW_TAG_variable;
  1897. { must be parasym of externally implemented procdef, but
  1898. the parasymtable can con also contain e.g. absolutevarsyms
  1899. -> check symtabletype}
  1900. if (sym.owner.symtabletype=parasymtable) and
  1901. (sym.localloc.loc=LOC_INVALID) then
  1902. begin
  1903. if (sym.owner.symtabletype<>parasymtable) then
  1904. internalerror(2009101001);
  1905. append_entry(tag,false,[
  1906. DW_AT_name,DW_FORM_string,name+#0
  1907. {
  1908. DW_AT_decl_file,DW_FORM_data1,0,
  1909. DW_AT_decl_line,DW_FORM_data1,
  1910. }
  1911. ])
  1912. end
  1913. else if not(sym.localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  1914. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  1915. ((sym.owner.symtabletype = globalsymtable) or
  1916. (sp_static in sym.symoptions) or
  1917. (vo_is_public in sym.varoptions)) then
  1918. append_entry(tag,false,[
  1919. DW_AT_name,DW_FORM_string,name+#0,
  1920. {
  1921. DW_AT_decl_file,DW_FORM_data1,0,
  1922. DW_AT_decl_line,DW_FORM_data1,
  1923. }
  1924. DW_AT_external,DW_FORM_flag,true,
  1925. { data continues below }
  1926. DW_AT_location,DW_FORM_block1,blocksize
  1927. ])
  1928. {$ifdef gdb_supports_DW_AT_variable_parameter}
  1929. else if (sym.typ=paravarsym) and
  1930. paramanager.push_addr_param(sym.varspez,sym.vardef,tprocdef(sym.owner.defowner).proccalloption) and
  1931. not(vo_has_local_copy in sym.varoptions) and
  1932. not is_open_string(sym.vardef) then
  1933. append_entry(tag,false,[
  1934. DW_AT_name,DW_FORM_string,name+#0,
  1935. DW_AT_variable_parameter,DW_FORM_flag,true,
  1936. {
  1937. DW_AT_decl_file,DW_FORM_data1,0,
  1938. DW_AT_decl_line,DW_FORM_data1,
  1939. }
  1940. { data continues below }
  1941. DW_AT_location,DW_FORM_block1,blocksize
  1942. ])
  1943. {$endif gdb_supports_DW_AT_variable_parameter}
  1944. else
  1945. append_entry(tag,false,[
  1946. DW_AT_name,DW_FORM_string,name+#0,
  1947. {
  1948. DW_AT_decl_file,DW_FORM_data1,0,
  1949. DW_AT_decl_line,DW_FORM_data1,
  1950. }
  1951. { data continues below }
  1952. DW_AT_location,DW_FORM_block1,blocksize
  1953. ]);
  1954. { append block data }
  1955. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  1956. { Mark self as artificial for methods, because gdb uses the fact
  1957. whether or not the first parameter of a method is artificial to
  1958. distinguish regular from static methods (since there are no
  1959. no vo_is_self parameters for static methods, we don't have to check
  1960. that). }
  1961. if (vo_is_self in sym.varoptions) then
  1962. append_attribute(DW_AT_artificial,DW_FORM_flag,[true]);
  1963. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  1964. {$ifdef i8086}
  1965. if has_segment_sym_name then
  1966. append_seg_name(segment_sym_name)
  1967. else if segment_reg<>NR_NO then
  1968. append_seg_reg(segment_reg);
  1969. {$endif i8086}
  1970. templist.free;
  1971. finish_entry;
  1972. *)
  1973. end;
  1974. procedure TDebugInfoLLVM.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  1975. var
  1976. decl: taillvmdecl;
  1977. globalvarexpression, globalvar: tai_llvmspecialisedmetadatanode;
  1978. dispflags: tsymstr;
  1979. islocal: boolean;
  1980. begin
  1981. decl:=staticvarsym_get_decl(sym);
  1982. if not assigned(decl) then
  1983. begin
  1984. list.concat(tai_comment.create(strpnew('no declaration found for '+sym.mangledname)));
  1985. exit;
  1986. end;
  1987. globalvar:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIGlobalVariable);
  1988. list.concat(globalvar);
  1989. globalvarexpression:=tai_llvmspecialisedmetadatanode.create(tspecialisedmetadatanodekind.DIGlobalVariableExpression);
  1990. globalvarexpression.addmetadatarefto('var',globalvar);
  1991. globalvarexpression.addmetadatarefto('expr',femptyexpression);
  1992. list.concat(globalvarexpression);
  1993. fglobals.addvalue(llvm_getmetadatareftypedconst(globalvarexpression));
  1994. decl.addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',globalvarexpression));
  1995. globalvar.addstring('name',symname(sym,false));
  1996. if not assigned(sym.owner.defowner) then
  1997. globalvar.addmetadatarefto('scope',fcunode)
  1998. else
  1999. globalvar.addmetadatarefto('scope',def_meta_node(tdef(sym.owner.defowner)));
  2000. try_add_file_metaref(globalvar,sym.fileinfo,false);
  2001. globalvar.addmetadatarefto('type',def_meta_node(sym.vardef));
  2002. islocal:=not(
  2003. ((sym.owner.symtabletype = globalsymtable) or
  2004. (sp_static in sym.symoptions) or
  2005. (vo_is_public in sym.varoptions))
  2006. );
  2007. adddefinitionlocal(globalvar,not(vo_is_external in sym.varoptions),islocal,false,dispflags);
  2008. if dispflags<>'' then
  2009. globalvar.addenum('spFlags',dispflags);
  2010. end;
  2011. procedure TDebugInfoLLVM.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  2012. begin
  2013. // appendsym_var(list,sym);
  2014. end;
  2015. procedure TDebugInfoLLVM.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  2016. begin
  2017. // appendsym_var(list,sym);
  2018. end;
  2019. procedure TDebugInfoLLVM.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  2020. begin
  2021. appendsym_fieldvar_with_name_offset(list,sym,symname(sym, false),sym.vardef,0);
  2022. end;
  2023. procedure TDebugInfoLLVM.appendsym_fieldvar_with_name_offset(list:TAsmList;sym: tfieldvarsym;const name: string; def: tdef; offset: pint);
  2024. var
  2025. bitoffset,
  2026. fieldoffset,
  2027. fieldnatsize: asizeint;
  2028. begin
  2029. (*
  2030. if (sp_static in sym.symoptions) or
  2031. (sym.visibility=vis_hidden) then
  2032. exit;
  2033. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) or
  2034. { only ordinals are bitpacked }
  2035. not is_ordinal(sym.vardef) then
  2036. begin
  2037. { other kinds of fields can however also appear in a bitpacked }
  2038. { record, and then their offset is also specified in bits rather }
  2039. { than in bytes }
  2040. if (tabstractrecordsymtable(sym.owner).usefieldalignment<>bit_alignment) then
  2041. fieldoffset:=sym.fieldoffset
  2042. else
  2043. fieldoffset:=sym.fieldoffset div 8;
  2044. inc(fieldoffset,offset);
  2045. append_entry(DW_TAG_member,false,[
  2046. DW_AT_name,DW_FORM_string,name+#0,
  2047. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2048. ]);
  2049. end
  2050. else
  2051. begin
  2052. if (sym.vardef.packedbitsize > 255) then
  2053. internalerror(2007061201);
  2054. { we don't bitpack according to the ABI, but as close as }
  2055. { possible, i.e., equivalent to gcc's }
  2056. { __attribute__((__packed__)), which is also what gpc }
  2057. { does. }
  2058. fieldnatsize:=max(sizeof(pint),sym.vardef.size);
  2059. fieldoffset:=(sym.fieldoffset div (fieldnatsize*8)) * fieldnatsize;
  2060. inc(fieldoffset,offset);
  2061. bitoffset:=sym.fieldoffset mod (fieldnatsize*8);
  2062. if (target_info.endian=endian_little) then
  2063. bitoffset:=(fieldnatsize*8)-bitoffset-sym.vardef.packedbitsize;
  2064. append_entry(DW_TAG_member,false,[
  2065. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2066. { gcc also generates both a bit and byte size attribute }
  2067. { we don't support ordinals >= 256 bits }
  2068. DW_AT_byte_size,DW_FORM_data1,fieldnatsize,
  2069. { nor >= 256 bits (not yet, anyway, see IE above) }
  2070. DW_AT_bit_size,DW_FORM_data1,sym.vardef.packedbitsize,
  2071. { data1 and data2 are unsigned, bitoffset can also be negative }
  2072. DW_AT_bit_offset,DW_FORM_data4,bitoffset,
  2073. DW_AT_data_member_location,DW_FORM_block1,1+lengthuleb128(fieldoffset)
  2074. ]);
  2075. end;
  2076. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(ord(DW_OP_plus_uconst)));
  2077. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(fieldoffset));
  2078. if (sym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2079. append_visibility(sym.visibility);
  2080. append_labelentry_ref(DW_AT_type,def_dwarf_lab(def));
  2081. finish_entry;
  2082. *)
  2083. end;
  2084. procedure TDebugInfoLLVM.appendsym_const(list:TAsmList;sym:tconstsym);
  2085. begin
  2086. appendsym_const_member(list,sym,false);
  2087. end;
  2088. procedure TDebugInfoLLVM.appendsym_const_member(list:TAsmList;sym:tconstsym;ismember:boolean);
  2089. var
  2090. i,
  2091. size: aint;
  2092. usedef: tdef;
  2093. begin
  2094. (*
  2095. { These are default values of parameters. These should be encoded
  2096. via DW_AT_default_value, not as a separate sym. Moreover, their
  2097. type is not available when writing the debug info for external
  2098. procedures.
  2099. }
  2100. if (sym.owner.symtabletype=parasymtable) then
  2101. exit;
  2102. if ismember then
  2103. append_entry(DW_TAG_member,false,[
  2104. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2105. { The DW_AT_declaration tag is invalid according to the DWARF specifications.
  2106. But gcc adds this to static const members and gdb checks
  2107. for this flag. So we have to set it also.
  2108. }
  2109. DW_AT_declaration,DW_FORM_flag,true,
  2110. DW_AT_external,DW_FORM_flag,true
  2111. ])
  2112. else
  2113. append_entry(DW_TAG_variable,false,[
  2114. DW_AT_name,DW_FORM_string,symname(sym, false)+#0
  2115. ]);
  2116. { for string constants, constdef isn't set because they have no real type }
  2117. case sym.consttyp of
  2118. conststring:
  2119. begin
  2120. { if DW_FORM_string is used below one day, this usedef should
  2121. probably become nil }
  2122. { note: < 255 instead of <= 255 because we have to store the
  2123. entire length of the string as well, and 256 does not fit in
  2124. a byte }
  2125. if (sym.value.len<255) then
  2126. usedef:=cshortstringtype
  2127. else
  2128. usedef:=clongstringtype;
  2129. end;
  2130. constresourcestring,
  2131. constwstring:
  2132. usedef:=nil;
  2133. else
  2134. usedef:=sym.constdef;
  2135. end;
  2136. if assigned(usedef) then
  2137. append_labelentry_ref(DW_AT_type,def_dwarf_lab(usedef));
  2138. AddConstToAbbrev(ord(DW_AT_const_value));
  2139. case sym.consttyp of
  2140. conststring:
  2141. begin
  2142. { DW_FORM_string isn't supported yet by the Pascal value printer
  2143. -> create a string using raw bytes }
  2144. if (sym.value.len<255) then
  2145. begin
  2146. AddConstToAbbrev(ord(DW_FORM_block1));
  2147. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len+1));
  2148. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.value.len));
  2149. end
  2150. else
  2151. begin
  2152. AddConstToAbbrev(ord(DW_FORM_block));
  2153. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.len+sizesinttype.size));
  2154. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.Create_sizeint_unaligned(sym.value.len));
  2155. end;
  2156. i:=0;
  2157. size:=sym.value.len;
  2158. while(i<size) do
  2159. begin
  2160. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2161. inc(i);
  2162. end;
  2163. end;
  2164. constguid,
  2165. constset:
  2166. begin
  2167. AddConstToAbbrev(ord(DW_FORM_block1));
  2168. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(usedef.size));
  2169. i:=0;
  2170. size:=sym.constdef.size;
  2171. while (i<size) do
  2172. begin
  2173. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit((pbyte(sym.value.valueptr+i)^)));
  2174. inc(i);
  2175. end;
  2176. end;
  2177. constwstring,
  2178. constresourcestring:
  2179. begin
  2180. { write dummy for now }
  2181. AddConstToAbbrev(ord(DW_FORM_string));
  2182. current_asmdata.asmlists[al_dwarf_info].concat(tai_string.create(''));
  2183. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(0));
  2184. end;
  2185. constord:
  2186. begin
  2187. if (sym.value.valueord<0) then
  2188. begin
  2189. AddConstToAbbrev(ord(DW_FORM_sdata));
  2190. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_sleb128bit(sym.value.valueord.svalue));
  2191. end
  2192. else
  2193. begin
  2194. AddConstToAbbrev(ord(DW_FORM_udata));
  2195. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_uleb128bit(sym.value.valueord.uvalue));
  2196. end;
  2197. end;
  2198. constnil:
  2199. begin
  2200. {$ifdef cpu64bitaddr}
  2201. AddConstToAbbrev(ord(DW_FORM_data8));
  2202. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(0));
  2203. {$else cpu64bitaddr}
  2204. AddConstToAbbrev(ord(DW_FORM_data4));
  2205. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(0));
  2206. {$endif cpu64bitaddr}
  2207. end;
  2208. constpointer:
  2209. begin
  2210. {$ifdef cpu64bitaddr}
  2211. AddConstToAbbrev(ord(DW_FORM_data8));
  2212. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(int64(sym.value.valueordptr)));
  2213. {$else cpu64bitaddr}
  2214. AddConstToAbbrev(ord(DW_FORM_data4));
  2215. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_32bit_unaligned(longint(sym.value.valueordptr)));
  2216. {$endif cpu64bitaddr}
  2217. end;
  2218. constreal:
  2219. begin
  2220. AddConstToAbbrev(ord(DW_FORM_block1));
  2221. case tfloatdef(sym.constdef).floattype of
  2222. s32real:
  2223. begin
  2224. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(4));
  2225. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s32real(pbestreal(sym.value.valueptr)^));
  2226. end;
  2227. s64real:
  2228. begin
  2229. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2230. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s64real(pbestreal(sym.value.valueptr)^));
  2231. end;
  2232. s64comp,
  2233. s64currency:
  2234. begin
  2235. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(8));
  2236. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_64bit_unaligned(trunc(pbestreal(sym.value.valueptr)^)));
  2237. end;
  2238. s80real,
  2239. sc80real:
  2240. begin
  2241. current_asmdata.asmlists[al_dwarf_info].concat(tai_const.create_8bit(sym.constdef.size));
  2242. current_asmdata.asmlists[al_dwarf_info].concat(tai_realconst.create_s80real(pextended(sym.value.valueptr)^,sym.constdef.size));
  2243. end;
  2244. else
  2245. internalerror(200601291);
  2246. end;
  2247. end;
  2248. else
  2249. internalerror(200601292);
  2250. end;
  2251. finish_entry;
  2252. *)
  2253. end;
  2254. procedure TDebugInfoLLVM.appendsym_label(list:TAsmList;sym: tlabelsym);
  2255. begin
  2256. { ignore label syms for now, the problem is that a label sym
  2257. can have more than one label associated e.g. in case of
  2258. an inline procedure expansion }
  2259. end;
  2260. procedure TDebugInfoLLVM.appendsym_property(list:TAsmList;sym: tpropertysym);
  2261. var
  2262. symlist: ppropaccesslistitem;
  2263. tosym: tabstractvarsym;
  2264. offset: pint;
  2265. begin
  2266. (*
  2267. if assigned(sym.propaccesslist[palt_read]) and
  2268. not assigned(sym.propaccesslist[palt_read].procdef) then
  2269. symlist:=sym.propaccesslist[palt_read].firstsym
  2270. else
  2271. { can't handle }
  2272. exit;
  2273. if not get_symlist_sym_offset(symlist,tosym,offset) then
  2274. exit;
  2275. if not (tosym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2276. begin
  2277. if (tosym.typ=fieldvarsym) then
  2278. internalerror(2009031404);
  2279. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),sym.propdef,offset,[])
  2280. end
  2281. else
  2282. appendsym_fieldvar_with_name_offset(list,tfieldvarsym(tosym),symname(sym, false),sym.propdef,offset)
  2283. *)
  2284. end;
  2285. function TDebugInfoLLVM.symdebugname(sym: tsym): TSymStr;
  2286. begin
  2287. if ds_dwarf_cpp in current_settings.debugswitches then
  2288. begin
  2289. result:=sym.RealName;
  2290. if (result<>'') and
  2291. (result[1]='$') then
  2292. delete(result,1,1);
  2293. end
  2294. else
  2295. result:=sym.name
  2296. end;
  2297. procedure TDebugInfoLLVM.appendsym_type(list:TAsmList;sym: ttypesym);
  2298. begin
  2299. { just queue the def if needed, beforeappenddef will
  2300. emit the typedef if necessary }
  2301. get_def_metatai(sym.typedef);
  2302. {
  2303. if FindUnitSymtable(sym.Owner).iscurrentunit then
  2304. fretainedtypes.addvalue(def_meta_ref(sym.typedef));
  2305. }
  2306. end;
  2307. procedure TDebugInfoLLVM.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  2308. (*
  2309. var
  2310. templist : TAsmList;
  2311. blocksize : longint;
  2312. symlist : ppropaccesslistitem;
  2313. tosym: tabstractvarsym;
  2314. offset: pint;
  2315. flags: tdwarfvarsymflags;
  2316. *)
  2317. begin
  2318. (*
  2319. templist:=TAsmList.create;
  2320. case tabsolutevarsym(sym).abstyp of
  2321. toaddr :
  2322. begin
  2323. { MWE: replaced ifdef i368 }
  2324. {
  2325. if target_cpu = cpu_i386 then
  2326. begin
  2327. { in theory, we could write a DW_AT_segment entry here for sym.absseg,
  2328. however I doubt that gdb supports this (FK) }
  2329. end;
  2330. }
  2331. templist.concat(tai_const.create_8bit(3));
  2332. {$ifdef avr}
  2333. // Add $800000 to indicate that the address is in memory space
  2334. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset + $800000, aitconst_ptr_unaligned));
  2335. {$else}
  2336. templist.concat(tai_const.create_int_dataptr_unaligned(sym.addroffset));
  2337. {$endif}
  2338. blocksize:=1+sizeof(puint);
  2339. end;
  2340. toasm :
  2341. begin
  2342. templist.concat(tai_const.create_8bit(3));
  2343. templist.concat(tai_const.create_type_name(aitconst_ptr_unaligned,sym.mangledname,0));
  2344. blocksize:=1+sizeof(puint);
  2345. end;
  2346. tovar:
  2347. begin
  2348. symlist:=tabsolutevarsym(sym).ref.firstsym;
  2349. if get_symlist_sym_offset(symlist,tosym,offset) then
  2350. begin
  2351. if (tosym.typ=fieldvarsym) then
  2352. internalerror(2009031402);
  2353. flags:=[];
  2354. if (sym.owner.symtabletype=localsymtable) then
  2355. include(flags,dvf_force_local_var);
  2356. appendsym_var_with_name_type_offset(list,tabstractnormalvarsym(tosym),symname(sym, false),tabstractvarsym(sym).vardef,offset,flags);
  2357. end;
  2358. templist.free;
  2359. exit;
  2360. end;
  2361. end;
  2362. append_entry(DW_TAG_variable,false,[
  2363. DW_AT_name,DW_FORM_string,symname(sym, false)+#0,
  2364. {
  2365. DW_AT_decl_file,DW_FORM_data1,0,
  2366. DW_AT_decl_line,DW_FORM_data1,
  2367. }
  2368. DW_AT_external,DW_FORM_flag,true,
  2369. { data continues below }
  2370. DW_AT_location,DW_FORM_block1,blocksize
  2371. ]);
  2372. { append block data }
  2373. current_asmdata.asmlists[al_dwarf_info].concatlist(templist);
  2374. append_labelentry_ref(DW_AT_type,def_dwarf_lab(sym.vardef));
  2375. templist.free;
  2376. finish_entry;
  2377. *)
  2378. end;
  2379. procedure TDebugInfoLLVM.beforeappendsym(list:TAsmList;sym:tsym);
  2380. begin
  2381. current_asmdata.asmlists[al_dwarf_info].concat(tai_comment.Create(strpnew('Symbol '+symname(sym, true))));
  2382. end;
  2383. procedure TDebugInfoLLVM.insertmoduleinfo;
  2384. var
  2385. culist: tai_llvmnamedmetadatanode;
  2386. dwarfversionflag: tai_llvmbasemetadatanode;
  2387. lang: tdwarf_source_language;
  2388. objcruntimeversion: longint;
  2389. begin
  2390. ensuremetainit;
  2391. if (ds_dwarf_cpp in current_settings.debugswitches) then
  2392. lang:=DW_LANG_C_plus_plus
  2393. else
  2394. lang:=DW_LANG_Pascal83;
  2395. { debug info header }
  2396. fcunode.addint64('language',ord(lang));
  2397. fcunode.addmetadatarefto('file',file_getmetanode(current_filepos.moduleindex,current_filepos.fileindex));
  2398. fcunode.addstring('producer','Free Pascal Compiler '+full_version_string);
  2399. fcunode.addboolean('isOptimized',cs_opt_level2 in current_settings.optimizerswitches);
  2400. if target_info.system in systems_objc_supported then
  2401. begin
  2402. if ([m_objectivec1,m_objectivec2]*current_settings.modeswitches)<>[] then
  2403. if target_info.system in systems_objc_nfabi then
  2404. objcruntimeversion:=2
  2405. else
  2406. objcruntimeversion:=1
  2407. else
  2408. objcruntimeversion:=0;
  2409. fcunode.addint64('runtimeVersion',objcruntimeversion);
  2410. end;
  2411. if cs_debuginfo in current_settings.moduleswitches then
  2412. fcunode.addenum('emissionKind','FullDebug')
  2413. else
  2414. fcunode.addenum('emissionKind','LineTablesOnly');
  2415. if fenums.valuecount<>0 then
  2416. begin
  2417. fcunode.addmetadatarefto('enums',fenums);
  2418. current_asmdata.AsmLists[al_dwarf_info].Concat(fenums);
  2419. end
  2420. else
  2421. begin
  2422. fcunode.addmetadatarefto('enums',nil);
  2423. fenums.free;
  2424. end;
  2425. fenums:=nil;
  2426. if fretainedtypes.valuecount<>0 then
  2427. begin
  2428. fcunode.addmetadatarefto('retainedTypes',fretainedtypes);
  2429. current_asmdata.AsmLists[al_dwarf_info].Concat(fretainedtypes);
  2430. end
  2431. else
  2432. begin
  2433. fcunode.addmetadatarefto('retainedTypes',nil);
  2434. fretainedtypes.free;
  2435. end;
  2436. fretainedtypes:=nil;
  2437. if fglobals.valuecount<>0 then
  2438. begin
  2439. fcunode.addmetadatarefto('globals',fglobals);
  2440. current_asmdata.AsmLists[al_dwarf_info].Concat(fglobals);
  2441. end
  2442. else
  2443. begin
  2444. fcunode.addmetadatarefto('globals',nil);
  2445. fglobals.free;
  2446. end;
  2447. fglobals:=nil;
  2448. current_asmdata.AsmLists[al_dwarf_info].Concat(femptyexpression);
  2449. femptyexpression:=nil;
  2450. current_asmdata.AsmLists[al_dwarf_info].Concat(fderefexpression);
  2451. fderefexpression:=nil;
  2452. if target_info.system in systems_darwin then
  2453. fcunode.addenum('nameTableKind','GNU');
  2454. current_asmdata.AsmLists[al_dwarf_info].Concat(fcunode);
  2455. culist:=tai_llvmnamedmetadatanode.create('llvm.dbg.cu');
  2456. current_asmdata.AsmLists[al_dwarf_info].Concat(culist);
  2457. culist.addvalue(llvm_getmetadatareftypedconst(fcunode));
  2458. resetfornewmodule;
  2459. end;
  2460. procedure TDebugInfoLLVM.inserttypeinfo;
  2461. var
  2462. storefilepos : tfileposinfo;
  2463. i : longint;
  2464. (*
  2465. lenstartlabel,arangestartlabel: tasmlabel;
  2466. *)
  2467. def: tdef;
  2468. (*
  2469. dbgname: string;
  2470. *)
  2471. vardatatype: ttypesym;
  2472. begin
  2473. ensuremetainit;
  2474. storefilepos:=current_filepos;
  2475. current_filepos:=current_module.mainfilepos;
  2476. vardatatype:=try_search_system_type('TVARDATA');
  2477. if assigned(vardatatype) then
  2478. vardatadef:=trecorddef(vardatatype.typedef);
  2479. collectglobalsyms;
  2480. { write all global/local variables. This will flag all required tdefs }
  2481. if assigned(current_module.globalsymtable) then
  2482. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2483. if assigned(current_module.localsymtable) then
  2484. write_symtable_syms(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2485. { write all procedures and methods. This will flag all required tdefs }
  2486. if assigned(current_module.globalsymtable) then
  2487. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2488. if assigned(current_module.localsymtable) then
  2489. write_symtable_procdefs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2490. { reset unit type info flag }
  2491. reset_unit_type_info;
  2492. { write used types from the used units }
  2493. write_used_unit_type_info(current_asmdata.asmlists[al_dwarf_info],current_module);
  2494. { last write the types from this unit }
  2495. if assigned(current_module.globalsymtable) then
  2496. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.globalsymtable);
  2497. if assigned(current_module.localsymtable) then
  2498. write_symtable_defs(current_asmdata.asmlists[al_dwarf_info],current_module.localsymtable);
  2499. { write defs not written yet }
  2500. write_remaining_defs_to_write(current_asmdata.asmlists[al_dwarf_info]);
  2501. { reset all def debug states for LLVMTypeInfo (which also uses this
  2502. field, to track for which types type info has been inserted already }
  2503. for i:=0 to defnumberlist.count-1 do
  2504. begin
  2505. def := tdef(defnumberlist[i]);
  2506. if assigned(def) then
  2507. def.dbg_state:=dbg_state_unused;
  2508. end;
  2509. current_filepos:=storefilepos;
  2510. end;
  2511. function TDebugInfoLLVM.symname(sym: tsym; manglename: boolean): TSymStr;
  2512. begin
  2513. if (sym.typ=paravarsym) and
  2514. (vo_is_self in tparavarsym(sym).varoptions) then
  2515. { We use 'this' for regular methods because that's what gdb triggers
  2516. on to automatically search fields. Don't do this for class methods,
  2517. because search class fields is not supported, and gdb 7.0+ fails
  2518. in this case because "this" is not a record in that case (it's a
  2519. pointer to a vmt) }
  2520. if not is_objc_class_or_protocol(tdef(sym.owner.defowner.owner.defowner)) and
  2521. not(po_classmethod in tabstractprocdef(sym.owner.defowner).procoptions) then
  2522. result:='this'
  2523. else
  2524. result:='self'
  2525. else if (sym.typ=typesym) and
  2526. is_objc_class_or_protocol(ttypesym(sym).typedef) then
  2527. result:=tobjectdef(ttypesym(sym).typedef).objextname^
  2528. else if (ds_dwarf_method_class_prefix in current_settings.debugswitches) and
  2529. (sym.typ=procsym) and
  2530. (tprocsym(sym).owner.symtabletype in [objectsymtable,recordsymtable]) then
  2531. begin
  2532. result:=tprocsym(sym).owner.name^+'__';
  2533. if manglename then
  2534. result := result + sym.name
  2535. else
  2536. result := result + symdebugname(sym);
  2537. end
  2538. else
  2539. begin
  2540. if manglename then
  2541. result := sym.name
  2542. else
  2543. result := symdebugname(sym);
  2544. end;
  2545. end;
  2546. function TDebugInfoLLVM.visibilitydiflag(vis: tvisibility): TSymStr;
  2547. begin
  2548. case vis of
  2549. vis_hidden,
  2550. vis_private,
  2551. vis_strictprivate:
  2552. result:='DIFlagPrivate';
  2553. vis_protected,
  2554. vis_strictprotected:
  2555. result:='DIFlagProtected';
  2556. vis_published,
  2557. vis_public:
  2558. result:='DIFlagPublic';
  2559. vis_none:
  2560. internalerror(2022050101);
  2561. end;
  2562. end;
  2563. procedure TDebugInfoLLVM.insertlineinfo(list:TAsmList);
  2564. var
  2565. hp: tai;
  2566. functionscope,
  2567. positionmeta: tai_llvmspecialisedmetadatanode;
  2568. pd: tprocdef;
  2569. procdeffileinfo: tfileposinfo;
  2570. nolineinfolevel : longint;
  2571. firstline: boolean;
  2572. begin
  2573. ensuremetainit;
  2574. hp:=tai(list.first);
  2575. while assigned(hp) and
  2576. ((hp.typ<>ait_llvmdecl) or
  2577. (taillvmdecl(hp).def.typ<>procdef)) do
  2578. begin
  2579. hp:=tai(hp.next);
  2580. end;
  2581. if not assigned(hp) then
  2582. exit;
  2583. pd:=tprocdef(taillvmdecl(hp).def);
  2584. procdeffileinfo:=pd.fileinfo;
  2585. { might trigger for certain kinds of internally generated code }
  2586. if procdeffileinfo.fileindex=0 then
  2587. exit;
  2588. flocalvarsymmeta.free;
  2589. flocalvarsymmeta:=THashSet.Create((pd.localst.SymList.count+pd.parast.SymList.count)*4+1,true,false);
  2590. functionscope:=def_meta_node(pd);
  2591. nolineinfolevel:=0;
  2592. hp:=tai(hp.next);
  2593. firstline:=true;
  2594. while assigned(hp) do
  2595. begin
  2596. case hp.typ of
  2597. ait_marker:
  2598. begin
  2599. case tai_marker(hp).kind of
  2600. mark_NoLineInfoStart:
  2601. inc(nolineinfolevel);
  2602. mark_NoLineInfoEnd:
  2603. dec(nolineinfolevel);
  2604. else
  2605. ;
  2606. end;
  2607. end;
  2608. else
  2609. ;
  2610. end;
  2611. if (hp.typ=ait_llvmins) and
  2612. ((nolineinfolevel=0) or
  2613. (taillvm(hp).llvmopcode=la_call)) then
  2614. begin
  2615. positionmeta:=nil;
  2616. { valid file -> add info }
  2617. if (tailineinfo(hp).fileinfo.fileindex<>0) then
  2618. begin
  2619. if firstline and
  2620. (nolineinfolevel=0) then
  2621. begin
  2622. functionscope.addint64('scopeLine',tailineinfo(hp).fileinfo.line);
  2623. firstline:=false;
  2624. end;
  2625. positionmeta:=filepos_getmetanode(tailineinfo(hp).fileinfo,procdeffileinfo,functionscope,nolineinfolevel<>0);
  2626. end
  2627. { LLVM requires line info for call instructions that may
  2628. potentially be inlined }
  2629. else if taillvm(hp).llvmopcode=la_call then
  2630. begin
  2631. positionmeta:=filepos_getmetanode(tailineinfo(hp).fileinfo,procdeffileinfo,functionscope,true);
  2632. end;
  2633. if assigned(positionmeta) then
  2634. taillvm(hp).addinsmetadata(tai_llvmmetadatareferenceoperand.createreferenceto('dbg',positionmeta));
  2635. if (cs_debuginfo in current_settings.moduleswitches) and
  2636. (taillvm(hp).llvmopcode=la_call) then
  2637. updatelocalvardbginfo(taillvm(hp),pd,functionscope);
  2638. end;
  2639. hp:=tai(hp.next);
  2640. end;
  2641. end;
  2642. {****************************************************************************
  2643. ****************************************************************************}
  2644. const
  2645. dbg_llvm_info : tdbginfo =
  2646. (
  2647. id : dbg_llvm;
  2648. idtxt : 'LLVM';
  2649. );
  2650. initialization
  2651. RegisterDebugInfo(dbg_llvm_info,TDebugInfoLLVM);
  2652. end.