cfidwarf.pas 27 KB

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