dbgllvm.pas 116 KB

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