psabiehpi.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Information about the current procedure that is being compiled
  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 psabiehpi;
  18. { $define debug_eh}
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { symtable }
  27. symconst,symtype,symdef,symsym,
  28. node,nutils,
  29. { aasm }
  30. cpubase,cgbase,cgutils,
  31. aasmbase,aasmdata,aasmtai,
  32. psub;
  33. type
  34. TPSABIEHAction = class
  35. landingpad : TAsmLabel;
  36. actiontablelabel : TAsmLabel;
  37. actionlist : TAsmList;
  38. first : boolean;
  39. constructor Create(pad : TAsmLabel);
  40. destructor Destroy; override;
  41. function AddAction(p: tobjectdef): LongInt;
  42. end;
  43. { This object gives information on the current routine being
  44. compiled.
  45. }
  46. tpsabiehprocinfo = class(tcgprocinfo)
  47. { set if the procedure needs exception tables because it
  48. has exception generating nodes }
  49. CreateExceptionTable: Boolean;
  50. { if a procedure needs exception tables, this is the outmost landing pad
  51. with "no action", covering everything not covered by other landing pads
  52. since a procedure which has one landing pad need to be covered completely by landing pads }
  53. OutmostLandingPad: TPSABIEHAction;
  54. { This is a "no action" action for re-use, normally equal to OutmostLandingPad }
  55. NoAction: TPSABIEHAction;
  56. { label to language specific data }
  57. LSDALabel : TAsmLabel;
  58. callsite_table_data,
  59. action_table_data,
  60. gcc_except_table_data : TAsmList;
  61. typefilterlistlabel,typefilterlistlabelref,
  62. callsitetablestart,callsitetableend,
  63. { first label which must be inserted into the entry code }
  64. entrycallsitestart,
  65. callsitelaststart : TAsmLabel;
  66. typefilterlist,
  67. landingpadstack,
  68. actionstack : tfplist;
  69. CurrentCallSiteNumber : Longint;
  70. destructor destroy; override;
  71. { PSABIEH stuff }
  72. procedure PushAction(action: TPSABIEHAction);
  73. function CurrentAction: TPSABIEHAction;inline;
  74. function PopAction(action: TPSABIEHAction): boolean;
  75. function FinalizeAndPopAction(action: TPSABIEHAction): boolean;
  76. { a landing pad is also an action, however, when the landing pad is popped from the stack
  77. the area covered by this landing pad ends, i.e. it is popped at the beginning of the finally/except clause,
  78. the action above is popped at the end of the finally/except clause, so if on clauses add new types, they
  79. are added to CurrentAction }
  80. procedure PushLandingPad(action: TPSABIEHAction);
  81. function CurrentLandingPad: TPSABIEHAction;inline;
  82. function PopLandingPad(action: TPSABIEHAction): boolean;
  83. procedure CreateNewPSABIEHCallsite(list: TAsmList);
  84. { adds a new type to the type filter list and returns its index
  85. be aware, that this method can also handle catch all filters so it
  86. is valid to pass nil }
  87. function AddTypeFilter(p: tobjectdef): Longint;
  88. procedure set_eh_info; override;
  89. procedure setup_eh; override;
  90. procedure finish_eh; override;
  91. procedure start_eh(list : TAsmList); override;
  92. procedure end_eh(list : TAsmList); override;
  93. function find_exception_handling(var n: tnode; para: pointer): foreachnoderesult; virtual;
  94. end;
  95. implementation
  96. uses
  97. cutils,
  98. verbose,
  99. systems,
  100. dwarfbase,
  101. cfidwarf,
  102. globals,
  103. procinfo,
  104. symtable,
  105. defutil,
  106. tgobj,
  107. cgobj,cgexcept,
  108. parabase,paramgr,
  109. hlcgobj,
  110. pass_2
  111. {$ifdef i386}
  112. ,aasmcpu
  113. {$endif i386}
  114. ;
  115. type
  116. { Utility class for exception handling state management that is used
  117. by tryexcept/tryfinally/on nodes (in a separate class so it can both
  118. be shared and overridden)
  119. Never instantiated. }
  120. tpsabiehexceptionstatehandler = class(tcgexceptionstatehandler)
  121. protected
  122. class procedure begin_catch_internal(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; add_catch: boolean; out exceptlocdef: tdef; out
  123. exceptlocreg: tregister);
  124. class procedure catch_all_start_internal(list: TAsmList; add_catch: boolean);
  125. public
  126. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  127. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  128. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  129. { start of "except/finally" block }
  130. class procedure emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptionstate: texceptionstate;var exceptiontemps:texceptiontemps); override;
  131. { end of a try-block, label comes after the end of try/except or
  132. try/finally }
  133. class procedure end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel); override;
  134. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  135. class procedure handle_reraise(list:TAsmList;const t:texceptiontemps;const entrystate: texceptionstate; const exceptframekind: texceptframekind); override;
  136. { start of an "on" (catch) block }
  137. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  138. { end of an "on" (catch) block }
  139. class procedure end_catch(list: TAsmList); override;
  140. { called for a catch all exception }
  141. class procedure catch_all_start(list: TAsmList); override;
  142. class procedure catch_all_end(list: TAsmList); override;
  143. class procedure catch_all_add(list: TAsmList); override;
  144. class procedure cleanupobjectstack(list: TAsmList); override;
  145. class procedure popaddrstack(list: TAsmList); override;
  146. end;
  147. constructor TPSABIEHAction.Create(pad: TAsmLabel);
  148. begin
  149. landingpad:=pad;
  150. actionlist:=TAsmList.create;
  151. current_asmdata.getlabel(actiontablelabel,alt_data);
  152. actionlist.concat(tai_label.create(actiontablelabel));
  153. first:=true;
  154. end;
  155. destructor TPSABIEHAction.Destroy;
  156. begin
  157. if not(actionlist.Empty) then
  158. Internalerror(2019020501);
  159. actionlist.Free;
  160. inherited Destroy;
  161. end;
  162. function TPSABIEHAction.AddAction(p: tobjectdef) : LongInt;
  163. var
  164. index: LongInt;
  165. begin
  166. { if not first entry, signal that another action follows }
  167. if not(first) then
  168. actionlist.concat(tai_const.Create_sleb128bit(1));
  169. first:=false;
  170. { catch all? }
  171. if p=tobjectdef(-1) then
  172. index:=(current_procinfo as tpsabiehprocinfo).AddTypeFilter(nil)
  173. else if assigned(p) then
  174. index:=(current_procinfo as tpsabiehprocinfo).AddTypeFilter(p)
  175. else
  176. index:=-1;
  177. {$ifdef debug_eh}
  178. if p=tobjectdef(-1) then
  179. actionlist.concat(tai_comment.Create(strpnew('Catch all')))
  180. else if assigned(p) then
  181. actionlist.concat(tai_comment.Create(strpnew('Action for '+p.GetTypeName)))
  182. else
  183. actionlist.concat(tai_comment.Create(strpnew('Cleanup')));
  184. {$endif debug_eh}
  185. if assigned(p) then
  186. actionlist.concat(tai_const.Create_sleb128bit(index+1))
  187. else
  188. actionlist.concat(tai_const.Create_sleb128bit(0));
  189. Result:=index;
  190. end;
  191. {****************************************************************************
  192. tpsabiehprocinfo
  193. ****************************************************************************}
  194. destructor tpsabiehprocinfo.destroy;
  195. begin
  196. gcc_except_table_data.free;
  197. actionstack.free;
  198. landingpadstack.free;
  199. typefilterlist.free;
  200. callsite_table_data.Free;
  201. action_table_data.Free;
  202. inherited;
  203. end;
  204. procedure tpsabiehprocinfo.PushAction(action: TPSABIEHAction);
  205. begin
  206. actionstack.add(action);
  207. end;
  208. function tpsabiehprocinfo.PopAction(action: TPSABIEHAction): boolean;
  209. begin
  210. if CurrentAction<>action then
  211. internalerror(2019022501);
  212. actionstack.count:=actionstack.count-1;
  213. result:=actionstack.count=0;
  214. end;
  215. function tpsabiehprocinfo.FinalizeAndPopAction(action: TPSABIEHAction): boolean;
  216. var
  217. curpos: tasmlabel;
  218. begin
  219. include(flags,pi_has_except_table_data);
  220. if CurrentAction<>action then
  221. internalerror(2019021006);
  222. { no further actions follow, finalize table
  223. we check for >1 as the outmost landing pad has no action, so
  224. we can ignore it }
  225. if landingpadstack.count>1 then
  226. begin
  227. current_asmdata.getlabel(curpos,alt_data);
  228. action.actionlist.concat(tai_label.create(curpos));
  229. action.actionlist.concat(tai_const.Create_rel_sym(aitconst_sleb128bit,curpos,TPSABIEHAction(landingpadstack[landingpadstack.count-1]).actiontablelabel));
  230. end
  231. else
  232. action.actionlist.concat(tai_const.Create_sleb128bit(0));
  233. action_table_data.concatList(action.actionlist);
  234. actionstack.count:=actionstack.count-1;
  235. result:=actionstack.count=0;
  236. end;
  237. procedure tpsabiehprocinfo.PushLandingPad(action: TPSABIEHAction);
  238. begin
  239. landingpadstack.add(action);
  240. end;
  241. function tpsabiehprocinfo.CurrentLandingPad: TPSABIEHAction;
  242. begin
  243. result:=TPSABIEHAction(landingpadstack.last);
  244. end;
  245. function tpsabiehprocinfo.PopLandingPad(action: TPSABIEHAction): boolean;
  246. begin
  247. if CurrentLandingPad<>action then
  248. internalerror(2019021007);
  249. landingpadstack.count:=landingpadstack.count-1;
  250. result:=landingpadstack.count=0;
  251. end;
  252. procedure tpsabiehprocinfo.CreateNewPSABIEHCallsite(list : TAsmList);
  253. var
  254. callsiteend : TAsmLabel;
  255. begin
  256. include(flags,pi_has_except_table_data);
  257. { first, finish last entry }
  258. if assigned(callsitelaststart) and assigned(CurrentLandingPad) then
  259. begin
  260. {$ifdef debug_eh}
  261. if assigned(CurrentLandingPad.actiontablelabel) then
  262. callsite_table_data.concat(tai_comment.Create(strpnew('Call site '+tostr(CurrentCallSiteNumber)+', action table index = '+tostr(landingpadstack.count-1))))
  263. else
  264. callsite_table_data.concat(tai_comment.Create(strpnew('Call site '+tostr(CurrentCallSiteNumber)+', no action')));
  265. {$endif debug_eh}
  266. callsite_table_data.concat(tai_const.create_rel_sym(aitconst_uleb128bit,current_asmdata.AsmCFI.get_frame_start,callsitelaststart));
  267. current_asmdata.getlabel(callsiteend,alt_eh_end);
  268. list.concat(tai_label.create(callsiteend));
  269. callsite_table_data.concat(tai_const.create_rel_sym(aitconst_uleb128bit,callsitelaststart,callsiteend));
  270. { landing pad? }
  271. if assigned(CurrentLandingPad.landingpad) then
  272. callsite_table_data.concat(tai_const.create_rel_sym(aitconst_uleb128bit,current_asmdata.AsmCFI.get_frame_start,CurrentLandingPad.landingpad))
  273. else
  274. callsite_table_data.concat(tai_const.Create_uleb128bit(0));
  275. { action number set? if yes, concat }
  276. if assigned(CurrentLandingPad.actiontablelabel) then
  277. begin
  278. callsite_table_data.concat(tai_const.Create_rel_sym_offset(aitconst_uleb128bit,callsitetableend,CurrentLandingPad.actiontablelabel,1));
  279. {$ifdef debug_eh}
  280. list.concat(tai_comment.Create(strpnew('New call site '+tostr(CurrentCallSiteNumber)+', action table index = '+tostr(landingpadstack.count-1))));
  281. {$endif debug_eh}
  282. end
  283. else
  284. begin
  285. callsite_table_data.concat(tai_const.Create_uleb128bit(0));
  286. {$ifdef debug_eh}
  287. list.concat(tai_comment.Create(strpnew('New call site '+tostr(CurrentCallSiteNumber)+', no action')));
  288. {$endif debug_eh}
  289. end;
  290. current_asmdata.getlabel(callsitelaststart,alt_eh_begin);
  291. list.concat(tai_label.create(callsitelaststart));
  292. end
  293. else
  294. begin
  295. current_asmdata.getlabel(entrycallsitestart,alt_eh_begin);
  296. callsitelaststart:=entrycallsitestart
  297. end;
  298. Inc(CurrentCallSiteNumber);
  299. end;
  300. function tpsabiehprocinfo.AddTypeFilter(p: tobjectdef) : Longint;
  301. var
  302. i: Integer;
  303. begin
  304. for i:=0 to typefilterlist.count-1 do
  305. begin
  306. if tobjectdef(typefilterlist[i])=p then
  307. begin
  308. result:=i;
  309. exit;
  310. end;
  311. end;
  312. result:=typefilterlist.add(p);
  313. end;
  314. procedure tpsabiehprocinfo.set_eh_info;
  315. begin
  316. inherited set_eh_info;
  317. if (tf_use_psabieh in target_info.flags) and not(pi_has_except_table_data in flags) then
  318. LSDALabel:=nil
  319. else
  320. current_asmdata.AsmCFI.get_cfa_list.concat(tdwarfitem.create_sym(DW_Set_LSDALabel,doe_32bit,LSDALabel));
  321. end;
  322. function tpsabiehprocinfo.CurrentAction: TPSABIEHAction; inline;
  323. begin
  324. result:=TPSABIEHAction(actionstack.last);
  325. end;
  326. function tpsabiehprocinfo.find_exception_handling(var n: tnode; para: pointer): foreachnoderesult;
  327. begin
  328. if n.nodetype in [tryfinallyn,tryexceptn,raisen,onn] then
  329. Result:=fen_norecurse_true
  330. else
  331. Result:=fen_false;
  332. end;
  333. procedure tpsabiehprocinfo.setup_eh;
  334. var
  335. gcc_except_table: tai_section;
  336. begin
  337. if tf_use_psabieh in target_info.flags then
  338. begin
  339. CreateExceptionTable:=foreachnode(code,@find_exception_handling,nil);
  340. gcc_except_table_data:=TAsmList.Create;
  341. callsite_table_data:=TAsmList.Create;
  342. action_table_data:=TAsmList.Create;
  343. actionstack:=TFPList.Create;
  344. landingpadstack:=TFPList.Create;
  345. typefilterlist:=TFPList.Create;
  346. gcc_except_table:=new_section(gcc_except_table_data,sec_gcc_except_table,'',0);
  347. gcc_except_table.secflags:=[SF_A];
  348. gcc_except_table.secprogbits:=SPB_PROGBITS;
  349. {$ifdef debug_eh}
  350. gcc_except_table_data.concat(tai_comment.Create(strpnew('gcc_except_table for '+procdef.fullprocname(true))));
  351. {$endif debug_eh}
  352. current_asmdata.getlabel(LSDALabel,alt_data);
  353. current_asmdata.getlabel(callsitetablestart,alt_data);
  354. current_asmdata.getlabel(callsitetableend,alt_data);
  355. callsite_table_data.concat(tai_label.create(callsitetablestart));
  356. cexceptionstatehandler:=tpsabiehexceptionstatehandler;
  357. if CreateExceptionTable then
  358. begin
  359. CreateNewPSABIEHCallsite(current_asmdata.CurrAsmList);
  360. OutmostLandingPad:=TPSABIEHAction.Create(nil);
  361. NoAction:=OutmostLandingPad;
  362. PushAction(OutmostLandingPad);
  363. PushLandingPad(OutmostLandingPad);
  364. OutmostLandingPad.AddAction(nil);
  365. end;
  366. end;
  367. end;
  368. procedure tpsabiehprocinfo.finish_eh;
  369. var
  370. i: Integer;
  371. begin
  372. if tf_use_psabieh in target_info.flags then
  373. begin
  374. if pi_has_except_table_data in flags then
  375. begin
  376. gcc_except_table_data.concat(tai_label.create(LSDALabel));
  377. { landing pad base is relative to procedure start, so write an omit }
  378. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_omit));
  379. if typefilterlist.count>0 then
  380. begin
  381. {$if defined(CPU64BITADDR)}
  382. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_udata8));
  383. {$elseif defined(CPU32BITADDR)}
  384. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_udata4));
  385. {$elseif defined(CPU16BITADDR)}
  386. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_udata2));
  387. {$endif}
  388. current_asmdata.getlabel(typefilterlistlabel,alt_data);
  389. current_asmdata.getlabel(typefilterlistlabelref,alt_data);
  390. gcc_except_table_data.concat(tai_const.create_rel_sym(aitconst_uleb128bit,typefilterlistlabel,typefilterlistlabelref));
  391. gcc_except_table_data.concat(tai_label.create(typefilterlistlabel));
  392. end
  393. else
  394. { default types table encoding }
  395. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_omit));
  396. { call-site table encoded using uleb128 }
  397. gcc_except_table_data.concat(tai_const.create_8bit(DW_EH_PE_uleb128));
  398. gcc_except_table_data.concat(tai_const.create_rel_sym(aitconst_uleb128bit,callsitetablestart,callsitetableend));
  399. callsite_table_data.concat(tai_label.create(callsitetableend));
  400. {$ifdef debug_eh}
  401. gcc_except_table_data.concat(tai_comment.Create(strpnew('Call site table for '+procdef.fullprocname(true))));
  402. {$endif debug_eh}
  403. gcc_except_table_data.concatList(callsite_table_data);
  404. { action table must follow immediatly after callsite table }
  405. {$ifdef debug_eh}
  406. if not(action_table_data.Empty) then
  407. gcc_except_table_data.concat(tai_comment.Create(strpnew('Action table for '+procdef.fullprocname(true))));
  408. {$endif debug_eh}
  409. gcc_except_table_data.concatlist(action_table_data);
  410. if typefilterlist.count>0 then
  411. begin
  412. {$ifdef debug_eh}
  413. gcc_except_table_data.concat(tai_comment.Create(strpnew('Type filter list for '+procdef.fullprocname(true))));
  414. {$endif debug_eh}
  415. for i:=typefilterlist.count-1 downto 0 do
  416. begin
  417. {$ifdef debug_eh}
  418. gcc_except_table_data.concat(tai_comment.Create(strpnew('Type filter '+tostr(i))));
  419. {$endif debug_eh}
  420. if assigned(typefilterlist[i]) then
  421. gcc_except_table_data.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(tobjectdef(typefilterlist[i]).vmt_mangledname, AT_DATA)))
  422. else
  423. gcc_except_table_data.concat(tai_const.Create_sym(nil));
  424. end;
  425. { the types are resolved by the negative offset, so the label must be written after all types }
  426. gcc_except_table_data.concat(tai_label.create(typefilterlistlabelref));
  427. end;
  428. new_section(gcc_except_table_data,sec_code,'',0);
  429. aktproccode.concatlist(gcc_except_table_data);
  430. end;
  431. end;
  432. end;
  433. procedure tpsabiehprocinfo.start_eh(list: TAsmList);
  434. begin
  435. inherited start_eh(list);
  436. if CreateExceptionTable then
  437. list.insert(tai_label.create(entrycallsitestart));
  438. end;
  439. procedure tpsabiehprocinfo.end_eh(list: TAsmList);
  440. begin
  441. inherited end_eh(list);
  442. if CreateExceptionTable then
  443. begin
  444. CreateNewPSABIEHCallsite(list);
  445. PopLandingPad(CurrentLandingPad);
  446. FinalizeAndPopAction(OutmostLandingPad);
  447. end;
  448. end;
  449. class procedure tpsabiehexceptionstatehandler.get_exception_temps(list: TAsmList; var t: texceptiontemps);
  450. begin
  451. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  452. end;
  453. class procedure tpsabiehexceptionstatehandler.unget_exception_temps(list: TAsmList; const t: texceptiontemps);
  454. begin
  455. tg.ungettemp(list,t.reasonbuf);
  456. (current_procinfo as tpsabiehprocinfo).FinalizeAndPopAction((current_procinfo as tpsabiehprocinfo).CurrentAction);
  457. end;
  458. class procedure tpsabiehexceptionstatehandler.new_exception(list: TAsmList; const t: texceptiontemps;
  459. const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  460. var
  461. reg: tregister;
  462. action: TPSABIEHAction;
  463. begin
  464. exceptstate.oldflowcontrol:=flowcontrol;
  465. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  466. if exceptframekind<>tek_except then
  467. begin
  468. current_asmdata.getjumplabel(exceptstate.finallycodelabel);
  469. action:=TPSABIEHAction.Create(exceptstate.finallycodelabel);
  470. end
  471. else
  472. begin
  473. exceptstate.finallycodelabel:=nil;
  474. action:=TPSABIEHAction.Create(exceptstate.exceptionlabel);
  475. end;
  476. (current_procinfo as tpsabiehprocinfo).CreateNewPSABIEHCallsite(list);
  477. (current_procinfo as tpsabiehprocinfo).PushAction(action);
  478. (current_procinfo as tpsabiehprocinfo).PushLandingPad(action);
  479. if exceptframekind<>tek_except then
  480. { no safecall? }
  481. if use_cleanup(exceptframekind) then
  482. (current_procinfo as tpsabiehprocinfo).CurrentAction.AddAction(nil)
  483. else
  484. { if safecall, catch all }
  485. (current_procinfo as tpsabiehprocinfo).CurrentAction.AddAction(tobjectdef(-1));
  486. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  487. if exceptframekind<>tek_except then
  488. begin
  489. reg:=hlcg.getintregister(list,ossinttype);
  490. hlcg.a_load_const_reg(list,ossinttype,1,reg);
  491. hlcg.g_exception_reason_save(list,ossinttype,ossinttype,reg,t.reasonbuf);
  492. end;
  493. end;
  494. class procedure tpsabiehexceptionstatehandler.emit_except_label(list: TAsmList; exceptframekind: texceptframekind;
  495. var exceptionstate: texceptionstate;var exceptiontemps:texceptiontemps);
  496. begin
  497. hlcg.g_unreachable(list);
  498. hlcg.a_label(list,exceptionstate.exceptionlabel);
  499. if exceptframekind<>tek_except then
  500. begin
  501. if not assigned(exceptionstate.finallycodelabel) then
  502. internalerror(2019021002);
  503. hlcg.a_label(list,exceptionstate.finallycodelabel);
  504. exceptionstate.finallycodelabel:=nil;
  505. exceptiontemps.unwind_info:=cg.getaddressregister(list);
  506. hlcg.a_load_reg_reg(list,voidpointertype,voidpointertype,NR_FUNCTION_RESULT_REG,exceptiontemps.unwind_info);
  507. end;
  508. end;
  509. class procedure tpsabiehexceptionstatehandler.end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps;
  510. var exceptionstate: texceptionstate; endlabel: TAsmLabel);
  511. var
  512. reg: TRegister;
  513. begin
  514. if exceptframekind<>tek_except then
  515. begin
  516. { record that no exception happened in the reason buf, in case we are in a try block of a finally statement }
  517. reg:=hlcg.getintregister(list,ossinttype);
  518. hlcg.a_load_const_reg(list,ossinttype,0,reg);
  519. hlcg.g_exception_reason_save(list,ossinttype,ossinttype,reg,t.reasonbuf);
  520. end;
  521. inherited;
  522. if exceptframekind=tek_except then
  523. hlcg.a_jmp_always(list,endlabel);
  524. (current_procinfo as tpsabiehprocinfo).CreateNewPSABIEHCallsite(list);
  525. (current_procinfo as tpsabiehprocinfo).PopLandingPad((current_procinfo as tpsabiehprocinfo).CurrentLandingPad);
  526. end;
  527. class procedure tpsabiehexceptionstatehandler.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint;
  528. endexceptlabel: tasmlabel; onlyfree: boolean);
  529. begin
  530. { nothing to do }
  531. end;
  532. class procedure tpsabiehexceptionstatehandler.handle_reraise(list: TAsmList; const t: texceptiontemps; const entrystate: texceptionstate;
  533. const exceptframekind: texceptframekind);
  534. var
  535. cgpara1: tcgpara;
  536. pd: tprocdef;
  537. ReRaiseLandingPad: TPSABIEHAction;
  538. psabiehprocinfo: tpsabiehprocinfo;
  539. begin
  540. if not(fc_catching_exceptions in flowcontrol) and
  541. use_cleanup(exceptframekind) then
  542. begin
  543. { Resume might not be called outside of an landing pad else
  544. the unwind is immediatly terminated, so create an empty landing pad }
  545. psabiehprocinfo:=current_procinfo as tpsabiehprocinfo;
  546. if psabiehprocinfo.landingpadstack.count>1 then
  547. begin
  548. psabiehprocinfo.CreateNewPSABIEHCallsite(list);
  549. psabiehprocinfo.PushAction(psabiehprocinfo.NoAction);
  550. psabiehprocinfo.PushLandingPad(psabiehprocinfo.NoAction);
  551. end;
  552. pd:=search_system_proc('_unwind_resume');
  553. cgpara1.init;
  554. paramanager.getcgtempparaloc(list,pd,1,cgpara1);
  555. hlcg.a_load_reg_cgpara(list,voidpointertype,t.unwind_info,cgpara1);
  556. paramanager.freecgpara(list,cgpara1);
  557. hlcg.g_call_system_proc(list,'_unwind_resume',[@cgpara1],nil).resetiftemp;
  558. { we do not have to clean up the stack, we never return }
  559. cgpara1.done;
  560. if psabiehprocinfo.landingpadstack.count>1 then
  561. begin
  562. psabiehprocinfo.CreateNewPSABIEHCallsite(list);
  563. psabiehprocinfo.PopLandingPad(psabiehprocinfo.NoAction);
  564. psabiehprocinfo.PopAction(psabiehprocinfo.NoAction);
  565. end;
  566. end
  567. else
  568. begin
  569. psabiehprocinfo:=current_procinfo as tpsabiehprocinfo;
  570. { empty landing pad needed to avoid immediate termination? }
  571. if psabiehprocinfo.landingpadstack.Count=0 then
  572. begin
  573. psabiehprocinfo.CreateNewPSABIEHCallsite(list);
  574. ReRaiseLandingPad:=psabiehprocinfo.NoAction;
  575. psabiehprocinfo.PushAction(ReRaiseLandingPad);
  576. psabiehprocinfo.PushLandingPad(ReRaiseLandingPad);
  577. end
  578. else
  579. ReRaiseLandingPad:=nil;
  580. hlcg.g_call_system_proc(list,'fpc_reraise',[],nil).resetiftemp;
  581. if assigned(ReRaiseLandingPad) then
  582. begin
  583. psabiehprocinfo.CreateNewPSABIEHCallsite(list);
  584. psabiehprocinfo.PopLandingPad(psabiehprocinfo.CurrentLandingPad);
  585. psabiehprocinfo.PopAction(ReRaiseLandingPad);
  586. end;
  587. end;
  588. end;
  589. class procedure tpsabiehexceptionstatehandler.begin_catch_internal(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel;
  590. add_catch: boolean; out exceptlocdef: tdef; out exceptlocreg: tregister);
  591. var
  592. catchstartlab : tasmlabel;
  593. begincatchres,
  594. paraloc1: tcgpara;
  595. pd: tprocdef;
  596. rttisym: TAsmSymbol;
  597. rttidef: tdef;
  598. wrappedexception: tregister;
  599. exceptloc: tlocation;
  600. indirect: boolean;
  601. otherunit: boolean;
  602. typeindex : aint;
  603. begin
  604. paraloc1.init;
  605. rttidef:=nil;
  606. rttisym:=nil;
  607. wrappedexception:=hlcg.getaddressregister(list,voidpointertype);
  608. hlcg.a_load_reg_reg(list,voidpointertype,voidpointertype,NR_FUNCTION_RESULT_REG,wrappedexception);
  609. if add_catch then
  610. begin
  611. if assigned(excepttype) then
  612. begin
  613. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  614. indirect:=(tf_supports_packages in target_info.flags) and
  615. (target_info.system in systems_indirect_var_imports) and
  616. (cs_imported_data in current_settings.localswitches) and
  617. otherunit;
  618. { add "catch exceptiontype" clause to the landing pad }
  619. rttidef:=cpointerdef.getreusable(excepttype.vmt_def);
  620. rttisym:=current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect);
  621. end;
  622. end;
  623. { check if the exception is handled by this node }
  624. if assigned(excepttype) then
  625. begin
  626. typeindex:=(current_procinfo as tpsabiehprocinfo).CurrentAction.AddAction(excepttype);
  627. current_asmdata.getjumplabel(catchstartlab);
  628. {$if defined(i386)}
  629. hlcg.a_cmp_const_reg_label (list,osuinttype,OC_EQ,typeindex+1,NR_FUNCTION_RESULT64_HIGH_REG,catchstartlab);
  630. {$elseif defined(x86_64)}
  631. hlcg.a_cmp_const_reg_label (list,s32inttype,OC_EQ,typeindex+1,NR_EDX,catchstartlab);
  632. {$else}
  633. { we need to find a way to fix this in a generic way }
  634. Internalerror(2019021008);
  635. {$endif}
  636. hlcg.a_jmp_always(list,nextonlabel);
  637. hlcg.a_label(list,catchstartlab);
  638. end
  639. else
  640. (current_procinfo as tpsabiehprocinfo).CurrentAction.AddAction(tobjectdef(-1));
  641. pd:=search_system_proc('fpc_psabi_begin_catch');
  642. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  643. hlcg.a_load_reg_cgpara(list,voidpointertype,wrappedexception,paraloc1);
  644. begincatchres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  645. location_reset(exceptloc, LOC_REGISTER, def_cgsize(begincatchres.def));
  646. exceptloc.register:=hlcg.getaddressregister(list, begincatchres.def);
  647. hlcg.gen_load_cgpara_loc(list, begincatchres.def, begincatchres, exceptloc, true);
  648. begincatchres.resetiftemp;
  649. paraloc1.done;
  650. exceptlocdef:=begincatchres.def;
  651. exceptlocreg:=exceptloc.register;
  652. end;
  653. class procedure tpsabiehexceptionstatehandler.catch_all_start_internal(list: TAsmList; add_catch: boolean);
  654. var
  655. exceptlocdef: tdef;
  656. exceptlocreg: tregister;
  657. begin
  658. begin_catch_internal(list,nil,nil,add_catch,exceptlocdef,exceptlocreg);
  659. end;
  660. class procedure tpsabiehexceptionstatehandler.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out
  661. exceptlocreg: tregister);
  662. begin
  663. begin_catch_internal(list,excepttype,nextonlabel,true,exceptlocdef,exceptlocreg);
  664. end;
  665. class procedure tpsabiehexceptionstatehandler.end_catch(list: TAsmList);
  666. begin
  667. hlcg.g_call_system_proc(list,'fpc_psabi_end_catch',[],nil).resetiftemp;
  668. inherited;
  669. end;
  670. class procedure tpsabiehexceptionstatehandler.catch_all_start(list: TAsmList);
  671. begin
  672. catch_all_start_internal(list,true);
  673. end;
  674. class procedure tpsabiehexceptionstatehandler.catch_all_add(list: TAsmList);
  675. begin
  676. (current_procinfo as tpsabiehprocinfo).CurrentAction.AddAction(nil);
  677. end;
  678. class procedure tpsabiehexceptionstatehandler.catch_all_end(list: TAsmList);
  679. begin
  680. hlcg.g_call_system_proc(list,'fpc_psabi_end_catch',[],nil).resetiftemp;
  681. end;
  682. class procedure tpsabiehexceptionstatehandler.cleanupobjectstack(list: TAsmList);
  683. begin
  684. { there is nothing to do }
  685. end;
  686. class procedure tpsabiehexceptionstatehandler.popaddrstack(list: TAsmList);
  687. begin
  688. { there is no addr stack, so do nothing }
  689. end;
  690. end.