dbgllvm.pas 99 KB

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