cfidwarf.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. {
  2. Copyright (c) 2003-2004 by Peter Vreman and Florian Klaempfl
  3. This units contains special support for DWARF debug info
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cfidwarf;
  18. {$i fpcdefs.inc}
  19. { $define debug_eh}
  20. interface
  21. uses
  22. cclasses,
  23. globtype,
  24. cgbase,cpubase,
  25. aasmbase,aasmcfi,aasmtai,aasmdata;
  26. const
  27. maxdwarfops = 2;
  28. type
  29. tdwarfoperenc=(doe_uleb,doe_sleb,doe_ptr,doe_32bit,doe_16bit,doe_8bit);
  30. tdwarfopertype=(dop_reg,dop_const,dop_sym,dop_reloffset);
  31. tdwarfoper=record
  32. enc : tdwarfoperenc;
  33. case typ:tdwarfopertype of
  34. dop_reg : (register:tregister);
  35. dop_const : (value:int64);
  36. dop_sym : (sym:tasmsymbol);
  37. dop_reloffset : (beginsym,endsym:tasmsymbol);
  38. end;
  39. tdwarfitem=class(TLinkedListItem)
  40. op : byte;
  41. ops : byte;
  42. oper : array[0..maxdwarfops-1] of tdwarfoper;
  43. constructor create(aop:byte);
  44. constructor create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  45. constructor create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  46. constructor create_sym(aop: byte; enc1: tdwarfoperenc; sym: TAsmSymbol);
  47. constructor create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  48. constructor create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  49. procedure generate_code(list:TAsmList);
  50. end;
  51. TDwarfAsmCFI=class(TAsmCFI)
  52. public type
  53. TDataType = (
  54. dt_none,
  55. dt_debug,
  56. dt_eh_frame
  57. );
  58. public
  59. datatype : TDataType;
  60. constructor create;override;
  61. end;
  62. TDwarfAsmCFILowLevel=class(TDwarfAsmCFI)
  63. private
  64. FDwarfList : TLinkedList;
  65. FFrameStartLabel,
  66. FFrameEndLabel,
  67. FLastloclabel : tasmlabel;
  68. procedure cfa_advance_loc(list:TAsmList);
  69. procedure generate_initial_instructions(list:TAsmList);virtual;
  70. protected
  71. code_alignment_factor,
  72. data_alignment_factor : shortint;
  73. property DwarfList:TlinkedList read FDwarfList;
  74. public
  75. constructor create;override;
  76. destructor destroy;override;
  77. procedure generate_code(list:TAsmList);override;
  78. function get_frame_start: TAsmLabel;override;
  79. function get_cfa_list : TAsmList;override;
  80. { operations }
  81. procedure start_frame(list:TAsmList);override;
  82. procedure end_frame(list:TAsmList);override;
  83. procedure outmost_frame(list: TAsmList);override;
  84. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  85. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  86. procedure cfa_def_cfa(list:TAsmList;reg:tregister;ofs:longint);override;
  87. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  88. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  89. end;
  90. TDwarfAsmCFIHighLevel=class(TDwarfAsmCFILowLevel)
  91. public
  92. procedure generate_code(list:TAsmList);override;
  93. { operations }
  94. procedure start_frame(list:TAsmList);override;
  95. procedure end_frame(list:TAsmList);override;
  96. procedure outmost_frame(list: TAsmList);override;
  97. procedure cfa_offset(list:TAsmList;reg:tregister;ofs:longint);override;
  98. procedure cfa_restore(list:TAsmList;reg:tregister);override;
  99. procedure cfa_def_cfa(list:TAsmList;reg:tregister;ofs:longint);override;
  100. procedure cfa_def_cfa_register(list:TAsmList;reg:tregister);override;
  101. procedure cfa_def_cfa_offset(list:TAsmList;ofs:longint);override;
  102. end;
  103. implementation
  104. uses
  105. systems,globals,
  106. cutils,
  107. verbose,
  108. dwarfbase;
  109. {****************************************************************************
  110. TDWARFITEM
  111. ****************************************************************************}
  112. constructor tdwarfitem.create(aop:byte);
  113. begin
  114. inherited create;
  115. op:=aop;
  116. ops:=0;
  117. end;
  118. constructor tdwarfitem.create_reg(aop:byte;enc1:tdwarfoperenc;reg:tregister);
  119. begin
  120. inherited create;
  121. op:=aop;
  122. ops:=1;
  123. oper[0].typ:=dop_reg;
  124. oper[0].enc:=enc1;
  125. oper[0].register:=reg;
  126. end;
  127. constructor tdwarfitem.create_const(aop:byte;enc1:tdwarfoperenc;val:int64);
  128. begin
  129. inherited create;
  130. op:=aop;
  131. ops:=1;
  132. oper[0].typ:=dop_const;
  133. oper[0].enc:=enc1;
  134. oper[0].value:=val;
  135. end;
  136. constructor tdwarfitem.create_sym(aop:byte;enc1:tdwarfoperenc;sym:TAsmSymbol);
  137. begin
  138. inherited create;
  139. op:=aop;
  140. ops:=1;
  141. oper[0].typ:=dop_sym;
  142. oper[0].enc:=enc1;
  143. oper[0].sym:=sym;
  144. end;
  145. constructor tdwarfitem.create_reloffset(aop:byte;enc1:tdwarfoperenc;beginlab,endlab:tasmsymbol);
  146. begin
  147. inherited create;
  148. op:=aop;
  149. ops:=1;
  150. { relative offsets are passed }
  151. oper[0].typ:=dop_reloffset;
  152. oper[0].enc:=enc1;
  153. oper[0].beginsym:=beginlab;
  154. oper[0].endsym:=endlab;
  155. end;
  156. constructor tdwarfitem.create_reg_const(aop:byte;enc1:tdwarfoperenc;reg:tregister;enc2:tdwarfoperenc;val:longint);
  157. begin
  158. inherited create;
  159. op:=aop;
  160. ops:=2;
  161. oper[0].typ:=dop_reg;
  162. oper[0].enc:=enc1;
  163. oper[0].register:=reg;
  164. oper[1].typ:=dop_const;
  165. oper[1].enc:=enc2;
  166. oper[1].value:=val;
  167. end;
  168. procedure tdwarfitem.generate_code(list:TAsmList);
  169. const
  170. enc2ait_const : array[tdwarfoperenc] of taiconst_type = (
  171. aitconst_uleb128bit,aitconst_sleb128bit,aitconst_ptr,
  172. aitconst_32bit,aitconst_16bit,aitconst_8bit
  173. );
  174. var
  175. i : integer;
  176. begin
  177. list.concat(tai_const.create_8bit(op));
  178. for i:=0 to ops-1 do
  179. begin
  180. case oper[i].typ of
  181. dop_const :
  182. list.concat(tai_const.create(enc2ait_const[oper[i].enc],oper[i].value));
  183. dop_sym :
  184. begin
  185. if oper[i].enc<>doe_ptr then
  186. internalerror(200404127);
  187. list.concat(tai_const.create_sym(oper[i].sym));
  188. end;
  189. dop_reloffset :
  190. list.concat(tai_const.create_rel_sym(enc2ait_const[oper[i].enc],oper[i].beginsym,oper[i].endsym));
  191. dop_reg :
  192. list.concat(tai_const.create(enc2ait_const[oper[i].enc],dwarf_reg(oper[i].register)));
  193. end;
  194. end;
  195. end;
  196. {****************************************************************************
  197. TDwarfAsmCFI
  198. ****************************************************************************}
  199. constructor TDwarfAsmCFI.create;
  200. begin
  201. inherited;
  202. if tf_use_psabieh in target_info.flags then
  203. datatype:=dt_eh_frame
  204. else
  205. { The CFI-information is always generated, regardless of the debug
  206. settings. This way the CFI-information for units is always available
  207. and during linking it can be omitted or not, based on the debug
  208. settings. }
  209. datatype:=dt_debug;
  210. end;
  211. {****************************************************************************
  212. TDwarfAsmCFILowLevel
  213. ****************************************************************************}
  214. constructor TDwarfAsmCFILowLevel.create;
  215. begin
  216. inherited create;
  217. FFrameStartLabel:=nil;
  218. FFrameEndLabel:=nil;
  219. FLastLocLabel:=nil;
  220. code_alignment_factor:=1;
  221. {$if defined(avr)}
  222. data_alignment_factor:=-1;
  223. {$else defined(avr)}
  224. data_alignment_factor:=-4;
  225. {$endif defined(avr)}
  226. FDwarfList:=TAsmList.Create;
  227. end;
  228. destructor TDwarfAsmCFILowLevel.destroy;
  229. begin
  230. FDwarfList.Free;
  231. FDwarfList := nil;
  232. end;
  233. {$if defined(i386)}
  234. { if more cpu dependend stuff is implemented, this needs more refactoring }
  235. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  236. begin
  237. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  238. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  239. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  240. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  241. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  242. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  243. end;
  244. {$elseif defined(avr)}
  245. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  246. begin
  247. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  248. list.concat(tai_const.create_uleb128bit(32));
  249. list.concat(tai_const.create_uleb128bit(2));
  250. list.concat(tai_const.create_8bit(DW_CFA_offset+36));
  251. list.concat(tai_const.create_uleb128bit((-1) div data_alignment_factor));
  252. end;
  253. {$elseif defined(arm)}
  254. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  255. begin
  256. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  257. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  258. list.concat(tai_const.create_uleb128bit(0));
  259. end;
  260. {$elseif defined(aarch64)}
  261. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  262. begin
  263. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  264. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  265. list.concat(tai_const.create_uleb128bit(0));
  266. end;
  267. {$elseif defined(riscv)}
  268. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  269. begin
  270. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  271. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  272. list.concat(tai_const.create_uleb128bit(0));
  273. end;
  274. {$elseif defined(loongarch64)}
  275. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  276. begin
  277. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  278. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  279. list.concat(tai_const.create_uleb128bit(0));
  280. end;
  281. {$else}
  282. { if more cpu dependend stuff is implemented, this needs more refactoring }
  283. procedure TDwarfAsmCFILowLevel.generate_initial_instructions(list:TAsmList);
  284. begin
  285. list.concat(tai_const.create_8bit(DW_CFA_def_cfa));
  286. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_STACK_POINTER_REG)));
  287. list.concat(tai_const.create_uleb128bit(sizeof(aint)));
  288. list.concat(tai_const.create_8bit(DW_CFA_offset_extended));
  289. list.concat(tai_const.create_uleb128bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  290. list.concat(tai_const.create_uleb128bit((-sizeof(aint)) div data_alignment_factor));
  291. end;
  292. {$endif i386}
  293. procedure TDwarfAsmCFILowLevel.generate_code(list:TAsmList);
  294. var
  295. hp : tdwarfitem;
  296. CurrentLSDALabel,
  297. cielabel,
  298. lenstartlabel,
  299. lenendlabel,
  300. augendlabel,
  301. augstartlabel,
  302. fdeofslabel, curpos: tasmlabel;
  303. tc : tai_const;
  304. begin
  305. if DwarfList.Empty then
  306. exit;
  307. CurrentLSDALabel:=nil;
  308. case datatype of
  309. dt_none:
  310. exit;
  311. dt_debug:
  312. new_section(list,sec_debug_frame,'',0);
  313. dt_eh_frame:
  314. new_section(list,sec_eh_frame,'',0);
  315. end;
  316. { debug_frame:
  317. CIE
  318. DWORD length
  319. DWORD CIE_Id = 0xffffffff
  320. BYTE version = 1
  321. STRING augmentation = "" = BYTE 0
  322. ULEB128 code alignment factor = 1
  323. ULEB128 data alignment factor = -1
  324. BYTE return address register
  325. <...> augmentation
  326. <...> start sequence
  327. eh_frame:
  328. CIE
  329. DWORD length
  330. DWORD CIE_Id = 0
  331. BYTE version = 1
  332. STRING augmentation = 'zPLR'#0
  333. ULEB128 code alignment factor = 1
  334. ULEB128 data alignment factor = -1
  335. BYTE return address register
  336. <...> start sequence
  337. }
  338. current_asmdata.getlabel(cielabel,alt_dbgframe);
  339. list.concat(tai_label.create(cielabel));
  340. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  341. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  342. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  343. list.concat(tai_label.create(lenstartlabel));
  344. if datatype=dt_eh_frame then
  345. begin
  346. list.concat(tai_const.create_32bit(0));
  347. list.concat(tai_const.create_8bit(1));
  348. list.concat(tai_const.create_8bit(ord('z')));
  349. list.concat(tai_const.create_8bit(ord('P')));
  350. list.concat(tai_const.create_8bit(ord('L')));
  351. list.concat(tai_const.create_8bit(ord('R')));
  352. list.concat(tai_const.create_8bit(0));
  353. end
  354. else
  355. begin
  356. list.concat(tai_const.create_32bit(longint($ffffffff)));
  357. list.concat(tai_const.create_8bit(1));
  358. list.concat(tai_const.create_8bit(0)); { empty string }
  359. end;
  360. list.concat(tai_const.create_uleb128bit(code_alignment_factor));
  361. list.concat(tai_const.create_sleb128bit(data_alignment_factor));
  362. list.concat(tai_const.create_8bit(dwarf_reg(NR_RETURN_ADDRESS_REG)));
  363. { augmentation data }
  364. if datatype=dt_eh_frame then
  365. begin
  366. current_asmdata.getlabel(augstartlabel,alt_dbgframe);
  367. current_asmdata.getlabel(augendlabel,alt_dbgframe);
  368. { size of augmentation data ('z') }
  369. list.concat(tai_const.create_rel_sym(aitconst_uleb128bit,augstartlabel,augendlabel));
  370. list.concat(tai_label.create(augstartlabel));
  371. { personality function ('P') }
  372. { encoding }
  373. list.concat(tai_const.create_8bit({DW_EH_PE_indirect or DW_EH_PE_pcrel or} DW_EH_PE_sdata4));
  374. { address of personality function }
  375. list.concat(tai_const.Createname('_FPC_psabieh_personality_v0',AT_FUNCTION,0));
  376. { LSDA encoding ('L')}
  377. list.concat(tai_const.create_8bit({DW_EH_PE_pcrel or }DW_EH_PE_sdata4));
  378. { FDE encoding ('R') }
  379. list.concat(tai_const.create_8bit({DW_EH_PE_pcrel or }DW_EH_PE_sdata4));
  380. list.concat(tai_label.create(augendlabel));
  381. end;
  382. { Generate standard code
  383. def_cfa(stackpointer,sizeof(aint))
  384. cfa_offset_extended(returnaddres,-sizeof(aint))
  385. }
  386. generate_initial_instructions(list);
  387. list.concat(cai_align.create_zeros(sizeof(pint)));
  388. list.concat(tai_label.create(lenendlabel));
  389. lenstartlabel:=nil;
  390. lenendlabel:=nil;
  391. hp:=TDwarfItem(DwarfList.first);
  392. while assigned(hp) do
  393. begin
  394. case hp.op of
  395. DW_CFA_Start_Frame :
  396. begin
  397. if assigned(lenstartlabel) then
  398. internalerror(200404125);
  399. if (hp.ops<>1) or
  400. (hp.oper[0].typ<>dop_reloffset) then
  401. internalerror(200404126);
  402. current_asmdata.getlabel(lenstartlabel,alt_dbgframe);
  403. current_asmdata.getlabel(lenendlabel,alt_dbgframe);
  404. { FDE
  405. DWORD length
  406. DWORD CIE-pointer = cielabel relative to section start
  407. PTRSIZE initial location = oper[0]
  408. PTRSIZE function size = oper[1]
  409. }
  410. list.concat(tai_const.create_rel_sym(aitconst_32bit,lenstartlabel,lenendlabel));
  411. list.concat(tai_label.create(lenstartlabel));
  412. if datatype=dt_eh_frame then
  413. begin
  414. { relative offset to the CIE }
  415. current_asmdata.getlabel(fdeofslabel,alt_dbgframe);
  416. list.concat(tai_label.create(fdeofslabel));
  417. list.concat(tai_const.create_rel_sym(aitconst_32bit,cielabel,fdeofslabel));
  418. end
  419. else
  420. begin
  421. { according to the dwarf (2 to 4) standard, this is an uword being always 32 bit unsigned }
  422. tc:=tai_const.create_type_sym(aitconst_32bit,cielabel);
  423. { force label offset to secrel32 for windows systems }
  424. if (target_info.system in systems_windows+systems_wince) then
  425. tc.consttype:=aitconst_secrel32_symbol;
  426. list.concat(tc);
  427. end;
  428. current_asmdata.getlabel(curpos,alt_dbgframe);
  429. list.concat(tai_label.create(curpos));
  430. list.concat(tai_const.Create_sym(hp.oper[0].beginsym));
  431. {$if defined(avr)}
  432. list.concat(tai_const.create_rel_sym(aitconst_32bit,hp.oper[0].beginsym,hp.oper[0].endsym));
  433. {$else defined(avr)}
  434. list.concat(tai_const.create_rel_sym(aitconst_ptr,hp.oper[0].beginsym,hp.oper[0].endsym));
  435. {$endif defined(avr)}
  436. { we wrote a 'z' into the CIE augmentation data }
  437. if datatype=dt_eh_frame then
  438. begin
  439. { size of augmentation }
  440. list.concat(tai_const.create_8bit(sizeof(pint)));
  441. {$ifdef debug_eh}
  442. list.concat(tai_comment.Create(strpnew('LSDA')));
  443. {$endif debug_eh}
  444. { address of LSDA}
  445. list.concat(tai_const.Create_sym(CurrentLSDALabel));
  446. { do not reuse LSDA label }
  447. CurrentLSDALabel:=nil;
  448. end;
  449. end;
  450. DW_CFA_End_Frame :
  451. begin
  452. list.concat(cai_align.create_zeros(sizeof(pint)));
  453. list.concat(tai_label.create(lenendlabel));
  454. lenstartlabel:=nil;
  455. lenendlabel:=nil;
  456. end;
  457. DW_Set_LSDALabel:
  458. CurrentLSDALabel:=hp.oper[0].sym as TAsmLabel;
  459. else
  460. hp.generate_code(list);
  461. end;
  462. hp:=TDwarfItem(hp.next);
  463. end;
  464. { Check for open frames }
  465. if assigned(lenstartlabel) then
  466. internalerror(2004041210);
  467. { DwarfList is processed, remove items }
  468. DwarfList.Clear;
  469. end;
  470. procedure TDwarfAsmCFILowLevel.start_frame(list:TAsmList);
  471. begin
  472. if datatype=dt_none then
  473. exit;
  474. current_asmdata.getlabel(FFrameEndLabel,alt_dbgframe);
  475. FLastloclabel:=get_frame_start;
  476. list.concat(tai_label.create(get_frame_start));
  477. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_start_frame,doe_32bit,get_frame_start,FFrameEndLabel));
  478. end;
  479. function TDwarfAsmCFILowLevel.get_frame_start : TAsmLabel;
  480. begin
  481. if not(assigned(FFrameStartLabel)) then
  482. current_asmdata.getlabel(FFrameStartLabel,alt_dbgframe);
  483. Result:=FFrameStartLabel;
  484. end;
  485. function TDwarfAsmCFILowLevel.get_cfa_list: TAsmList;
  486. begin
  487. Result:=TAsmList(DwarfList);
  488. end;
  489. procedure TDwarfAsmCFILowLevel.outmost_frame(list: TAsmList);
  490. begin
  491. if datatype=dt_none then
  492. exit;
  493. cfa_advance_loc(list);
  494. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_undefined,doe_uleb,NR_RETURN_ADDRESS_REG));
  495. end;
  496. procedure TDwarfAsmCFILowLevel.end_frame(list:TAsmList);
  497. begin
  498. if datatype=dt_none then
  499. exit;
  500. if not assigned(FFrameStartLabel) then
  501. internalerror(2004041213);
  502. DwarfList.concat(tdwarfitem.create(DW_CFA_end_frame));
  503. list.concat(tai_label.create(FFrameEndLabel));
  504. FFrameStartLabel:=nil;
  505. FFrameEndLabel:=nil;
  506. FLastLocLabel:=nil;
  507. end;
  508. procedure TDwarfAsmCFILowLevel.cfa_advance_loc(list:TAsmList);
  509. var
  510. currloclabel : tasmlabel;
  511. hp : tai;
  512. instrcount : longint;
  513. dwarfloc: Integer;
  514. begin
  515. if FLastloclabel=nil then
  516. internalerror(200404082);
  517. { search the list backwards and check if we really need an advance loc,
  518. i.e. if real code/data has been generated since the last cfa_advance_loc
  519. call
  520. }
  521. hp:=tai(list.Last);
  522. while assigned(hp) do
  523. begin
  524. { if we encounter FLastloclabel without encountering code/data, see check below,
  525. we do not need insert an advance_loc entry }
  526. if (hp.typ=ait_label) and (tai_label(hp).labsym=FLastloclabel) then
  527. exit;
  528. { stop if we find any tai which results in code or data }
  529. if not(hp.typ in ([ait_label]+SkipInstr)) then
  530. break;
  531. hp:=tai(hp.Previous);
  532. end;
  533. { check if the last advance entry is less then 8 instructions away:
  534. as x86 instructions might not be bigger than 15 bytes and most other
  535. CPUs use only 4 byte instructions or smaller, this is safe
  536. we could search even more but this takes more time and 8 instructions should be normally enough
  537. }
  538. hp:=tai(list.Last);
  539. instrcount:=0;
  540. dwarfloc:=DW_CFA_advance_loc4;
  541. while assigned(hp) and (instrcount<8) do
  542. begin
  543. { stop if we find any tai which results in code or data }
  544. if not(hp.typ in ([ait_label,ait_instruction]+SkipInstr)) then
  545. break;
  546. if (hp.typ=ait_label) and (tai_label(hp).labsym=FLastloclabel) then
  547. begin
  548. dwarfloc:=DW_CFA_advance_loc1;
  549. break;
  550. end;
  551. if hp.typ=ait_instruction then
  552. inc(instrcount);
  553. hp:=tai(hp.Previous);
  554. end;
  555. current_asmdata.getlabel(currloclabel,alt_dbgframe);
  556. list.concat(tai_label.create(currloclabel));
  557. if dwarfloc=DW_CFA_advance_loc1 then
  558. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc1,doe_8bit,FLastloclabel,currloclabel))
  559. else
  560. DwarfList.concat(tdwarfitem.create_reloffset(DW_CFA_advance_loc4,doe_32bit,FLastloclabel,currloclabel));
  561. FLastloclabel:=currloclabel;
  562. end;
  563. procedure TDwarfAsmCFILowLevel.cfa_offset(list:TAsmList;reg:tregister;ofs:longint);
  564. begin
  565. if datatype=dt_none then
  566. exit;
  567. cfa_advance_loc(list);
  568. { TODO: check if ref is a temp}
  569. { offset must be positive }
  570. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_offset_extended,doe_uleb,reg,doe_uleb,ofs div data_alignment_factor));
  571. end;
  572. procedure TDwarfAsmCFILowLevel.cfa_restore(list:TAsmList;reg:tregister);
  573. begin
  574. if datatype=dt_none then
  575. exit;
  576. cfa_advance_loc(list);
  577. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_restore_extended,doe_uleb,reg));
  578. end;
  579. procedure TDwarfAsmCFILowLevel.cfa_def_cfa(list:TAsmList;reg:tregister;ofs:longint);
  580. begin
  581. if datatype=dt_none then
  582. exit;
  583. cfa_advance_loc(list);
  584. DwarfList.concat(tdwarfitem.create_reg_const(DW_CFA_def_cfa,doe_uleb,reg,doe_uleb,ofs));
  585. end;
  586. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_register(list:TAsmList;reg:tregister);
  587. begin
  588. if datatype=dt_none then
  589. exit;
  590. cfa_advance_loc(list);
  591. DwarfList.concat(tdwarfitem.create_reg(DW_CFA_def_cfa_register,doe_uleb,reg));
  592. end;
  593. procedure TDwarfAsmCFILowLevel.cfa_def_cfa_offset(list:TAsmList;ofs:longint);
  594. begin
  595. if datatype=dt_none then
  596. exit;
  597. cfa_advance_loc(list);
  598. DwarfList.concat(tdwarfitem.create_const(DW_CFA_def_cfa_offset,doe_uleb,ofs));
  599. end;
  600. {****************************************************************************
  601. TDwarfAsmCFILowLevel
  602. ****************************************************************************}
  603. procedure TDwarfAsmCFIHighLevel.generate_code(list: TAsmList);
  604. begin
  605. if not(tf_use_hlcfi in target_info.flags) then
  606. begin
  607. inherited;
  608. exit;
  609. end;
  610. end;
  611. procedure TDwarfAsmCFIHighLevel.start_frame(list: TAsmList);
  612. begin
  613. if not(tf_use_hlcfi in target_info.flags) then
  614. begin
  615. inherited;
  616. exit;
  617. end;
  618. if not(af_supports_hlcfi in target_asm.flags) then
  619. exit;
  620. list.concat(tai_cfi_op_none.create(cfi_startproc));
  621. end;
  622. procedure TDwarfAsmCFIHighLevel.end_frame(list: TAsmList);
  623. begin
  624. if not(tf_use_hlcfi in target_info.flags) then
  625. begin
  626. inherited;
  627. exit;
  628. end;
  629. if not(af_supports_hlcfi in target_asm.flags) then
  630. exit;
  631. list.concat(tai_cfi_op_none.create(cfi_endproc));
  632. end;
  633. procedure TDwarfAsmCFIHighLevel.outmost_frame(list: TAsmList);
  634. begin
  635. if not(tf_use_hlcfi in target_info.flags) then
  636. begin
  637. inherited;
  638. exit;
  639. end;
  640. if not(af_supports_hlcfi in target_asm.flags) then
  641. exit;
  642. list.concat(tai_cfi_op_reg.create(cfi_undefined,NR_RETURN_ADDRESS_REG));
  643. end;
  644. procedure TDwarfAsmCFIHighLevel.cfa_offset(list: TAsmList; reg: tregister; ofs: longint);
  645. begin
  646. if not(tf_use_hlcfi in target_info.flags) then
  647. begin
  648. inherited;
  649. exit;
  650. end;
  651. if not(af_supports_hlcfi in target_asm.flags) then
  652. exit;
  653. list.concat(tai_cfi_op_reg_val.create(cfi_offset,reg,ofs));
  654. end;
  655. procedure TDwarfAsmCFIHighLevel.cfa_restore(list: TAsmList; reg: tregister);
  656. begin
  657. if not(tf_use_hlcfi in target_info.flags) then
  658. begin
  659. inherited;
  660. exit;
  661. end;
  662. if not(af_supports_hlcfi in target_asm.flags) then
  663. exit;
  664. list.concat(tai_cfi_op_reg.create(cfi_restore,reg));
  665. end;
  666. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa(list: TAsmList; reg: tregister; ofs: longint);
  667. begin
  668. if not(tf_use_hlcfi in target_info.flags) then
  669. begin
  670. inherited;
  671. exit;
  672. end;
  673. if not(af_supports_hlcfi in target_asm.flags) then
  674. exit;
  675. list.concat(tai_cfi_op_reg_val.create(cfi_def_cfa,reg,ofs));
  676. end;
  677. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_register(list: TAsmList; reg: tregister);
  678. begin
  679. if not(tf_use_hlcfi in target_info.flags) then
  680. begin
  681. inherited;
  682. exit;
  683. end;
  684. if not(af_supports_hlcfi in target_asm.flags) then
  685. exit;
  686. list.concat(tai_cfi_op_reg.create(cfi_def_cfa_register,reg));
  687. end;
  688. procedure TDwarfAsmCFIHighLevel.cfa_def_cfa_offset(list: TAsmList; ofs: longint);
  689. begin
  690. if not(tf_use_hlcfi in target_info.flags) then
  691. begin
  692. inherited;
  693. exit;
  694. end;
  695. if not(af_supports_hlcfi in target_asm.flags) then
  696. exit;
  697. list.concat(tai_cfi_op_val.create(cfi_def_cfa_offset,ofs));
  698. end;
  699. begin
  700. CAsmCFI:=TDwarfAsmCFIHighLevel;
  701. end.