cpupi.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. {
  2. Copyright (c) 2002-2010 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the CPU specific part of tprocinfo
  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 cpupi;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,globtype,aasmdata,
  22. procinfo,cpuinfo, symtype,aasmbase,cgbase,
  23. psub, cclasses;
  24. type
  25. { tcpuprocinfo }
  26. tcpuprocinfo=class(tcgprocinfo)
  27. public
  28. { label to the nearest local exception handler }
  29. CurrRaiseLabel : tasmlabel;
  30. constructor create(aparent: tprocinfo); override;
  31. function calc_stackframe_size : longint;override;
  32. procedure setup_eh; override;
  33. procedure generate_exit_label(list: tasmlist); override;
  34. procedure postprocess_code; override;
  35. procedure set_first_temp_offset;override;
  36. end;
  37. implementation
  38. uses
  39. systems,verbose,globals,cpubase,tgcpu,aasmcpu,aasmtai,cgexcept,
  40. tgobj,paramgr,symconst,symdef,symtable,symcpu,cgutils,pass_2,parabase,
  41. fmodule,hlcgobj,hlcgcpu,defutil;
  42. {*****************************************************************************
  43. twasmexceptionstatehandler_noexceptions
  44. *****************************************************************************}
  45. type
  46. { twasmexceptionstatehandler_noexceptions }
  47. twasmexceptionstatehandler_noexceptions = class(tcgexceptionstatehandler)
  48. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  49. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  50. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  51. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  52. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  53. end;
  54. class procedure twasmexceptionstatehandler_noexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  55. begin
  56. if not assigned(exceptionreasontype) then
  57. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  58. reference_reset(t.envbuf,0,[]);
  59. reference_reset(t.jmpbuf,0,[]);
  60. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  61. end;
  62. class procedure twasmexceptionstatehandler_noexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  63. begin
  64. tg.ungettemp(list,t.reasonbuf);
  65. end;
  66. class procedure twasmexceptionstatehandler_noexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  67. begin
  68. exceptstate.exceptionlabel:=nil;
  69. exceptstate.oldflowcontrol:=flowcontrol;
  70. exceptstate.finallycodelabel:=nil;
  71. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  72. end;
  73. class procedure twasmexceptionstatehandler_noexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  74. begin
  75. end;
  76. class procedure twasmexceptionstatehandler_noexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  77. begin
  78. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  79. end;
  80. {*****************************************************************************
  81. twasmexceptionstatehandler_jsexceptions
  82. *****************************************************************************}
  83. type
  84. twasmexceptionstatehandler_jsexceptions = class(tcgexceptionstatehandler)
  85. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  86. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  87. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  88. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  89. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  90. end;
  91. class procedure twasmexceptionstatehandler_jsexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  92. begin
  93. if not assigned(exceptionreasontype) then
  94. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  95. reference_reset(t.envbuf,0,[]);
  96. reference_reset(t.jmpbuf,0,[]);
  97. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  98. end;
  99. class procedure twasmexceptionstatehandler_jsexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  100. begin
  101. tg.ungettemp(list,t.reasonbuf);
  102. end;
  103. class procedure twasmexceptionstatehandler_jsexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  104. begin
  105. exceptstate.exceptionlabel:=nil;
  106. exceptstate.oldflowcontrol:=flowcontrol;
  107. exceptstate.finallycodelabel:=nil;
  108. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  109. end;
  110. class procedure twasmexceptionstatehandler_jsexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  111. begin
  112. end;
  113. class procedure twasmexceptionstatehandler_jsexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  114. begin
  115. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  116. end;
  117. {*****************************************************************************
  118. twasmexceptionstatehandler_nativeexceptions
  119. *****************************************************************************}
  120. type
  121. { twasmexceptionstatehandler_nativeexceptions }
  122. twasmexceptionstatehandler_nativeexceptions = class(tcgexceptionstatehandler)
  123. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  124. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  125. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  126. { start of an "on" (catch) block }
  127. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  128. { end of an "on" (catch) block }
  129. class procedure end_catch(list: TAsmList); override;
  130. end;
  131. class procedure twasmexceptionstatehandler_nativeexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  132. begin
  133. exceptstate.exceptionlabel:=nil;
  134. exceptstate.oldflowcontrol:=flowcontrol;
  135. exceptstate.finallycodelabel:=nil;
  136. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  137. end;
  138. class procedure twasmexceptionstatehandler_nativeexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  139. begin
  140. end;
  141. class procedure twasmexceptionstatehandler_nativeexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  142. begin
  143. internalerror(2021100503);
  144. end;
  145. class procedure twasmexceptionstatehandler_nativeexceptions.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  146. var
  147. pd: tprocdef;
  148. href2: treference;
  149. fpc_catches_res,
  150. paraloc1: tcgpara;
  151. exceptloc: tlocation;
  152. indirect: boolean;
  153. otherunit: boolean;
  154. begin
  155. paraloc1.init;
  156. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  157. indirect:=(tf_supports_packages in target_info.flags) and
  158. (target_info.system in systems_indirect_var_imports) and
  159. (cs_imported_data in current_settings.localswitches) and
  160. otherunit;
  161. { send the vmt parameter }
  162. pd:=search_system_proc('fpc_catches');
  163. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  164. if otherunit then
  165. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  166. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  167. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  168. paramanager.freecgpara(list, paraloc1);
  169. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  170. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  171. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  172. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  173. { is it this catch? }
  174. thlcgwasm(hlcg).a_cmp_const_reg_stack(list, fpc_catches_res.def, OC_NE, 0, exceptloc.register);
  175. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  176. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  177. paraloc1.done;
  178. exceptlocdef:=fpc_catches_res.def;
  179. exceptlocreg:=exceptloc.register;
  180. end;
  181. class procedure twasmexceptionstatehandler_nativeexceptions.end_catch(list: TAsmList);
  182. begin
  183. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  184. end;
  185. {*****************************************************************************
  186. twasmexceptionstatehandler_bfexceptions
  187. *****************************************************************************}
  188. type
  189. { twasmexceptionstatehandler_bfexceptions }
  190. twasmexceptionstatehandler_bfexceptions = class(tcgexceptionstatehandler)
  191. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  192. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  193. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  194. { start of an "on" (catch) block }
  195. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  196. { end of an "on" (catch) block }
  197. class procedure end_catch(list: TAsmList); override;
  198. end;
  199. class procedure twasmexceptionstatehandler_bfexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  200. begin
  201. exceptstate.exceptionlabel:=nil;
  202. exceptstate.oldflowcontrol:=flowcontrol;
  203. exceptstate.finallycodelabel:=nil;
  204. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  205. end;
  206. class procedure twasmexceptionstatehandler_bfexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  207. begin
  208. end;
  209. class procedure twasmexceptionstatehandler_bfexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  210. begin
  211. internalerror(2021100502);
  212. end;
  213. class procedure twasmexceptionstatehandler_bfexceptions.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  214. var
  215. pd: tprocdef;
  216. href2: treference;
  217. fpc_catches_res,
  218. paraloc1: tcgpara;
  219. exceptloc: tlocation;
  220. indirect: boolean;
  221. otherunit: boolean;
  222. begin
  223. paraloc1.init;
  224. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  225. indirect:=(tf_supports_packages in target_info.flags) and
  226. (target_info.system in systems_indirect_var_imports) and
  227. (cs_imported_data in current_settings.localswitches) and
  228. otherunit;
  229. { send the vmt parameter }
  230. pd:=search_system_proc('fpc_catches');
  231. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  232. if otherunit then
  233. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  234. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  235. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  236. paramanager.freecgpara(list, paraloc1);
  237. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  238. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  239. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  240. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  241. { is it this catch? }
  242. thlcgwasm(hlcg).a_cmp_const_reg_stack(list, fpc_catches_res.def, OC_NE, 0, exceptloc.register);
  243. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  244. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  245. paraloc1.done;
  246. exceptlocdef:=fpc_catches_res.def;
  247. exceptlocreg:=exceptloc.register;
  248. end;
  249. class procedure twasmexceptionstatehandler_bfexceptions.end_catch(list: TAsmList);
  250. begin
  251. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  252. end;
  253. {*****************************************************************************
  254. twasmblockitem
  255. *****************************************************************************}
  256. type
  257. { twasmblockitem }
  258. twasmblockitem = class(TLinkedListItem)
  259. blockstart: taicpu;
  260. elseinstr: taicpu;
  261. constructor Create(ablockstart: taicpu);
  262. end;
  263. constructor twasmblockitem.Create(ablockstart: taicpu);
  264. begin
  265. blockstart:=ablockstart;
  266. end;
  267. {*****************************************************************************
  268. twasmblockstack
  269. *****************************************************************************}
  270. type
  271. { twasmblockstack }
  272. twasmblockstack = class(tlinkedlist)
  273. end;
  274. {*****************************************************************************
  275. tcpuprocinfo
  276. *****************************************************************************}
  277. constructor tcpuprocinfo.create(aparent: tprocinfo);
  278. begin
  279. inherited create(aparent);
  280. if ts_wasm_bf_exceptions in current_settings.targetswitches then
  281. current_asmdata.getjumplabel(CurrRaiseLabel);
  282. end;
  283. function tcpuprocinfo.calc_stackframe_size: longint;
  284. begin
  285. { the stack frame in WebAssembly should always have a 16-byte alignment }
  286. Result:=Align(inherited calc_stackframe_size,16);
  287. end;
  288. procedure tcpuprocinfo.setup_eh;
  289. begin
  290. if ts_wasm_native_exceptions in current_settings.targetswitches then
  291. cexceptionstatehandler:=twasmexceptionstatehandler_nativeexceptions
  292. else if ts_wasm_js_exceptions in current_settings.targetswitches then
  293. cexceptionstatehandler:=twasmexceptionstatehandler_jsexceptions
  294. else if ts_wasm_no_exceptions in current_settings.targetswitches then
  295. cexceptionstatehandler:=twasmexceptionstatehandler_noexceptions
  296. else if ts_wasm_bf_exceptions in current_settings.targetswitches then
  297. cexceptionstatehandler:=twasmexceptionstatehandler_bfexceptions
  298. else
  299. internalerror(2021091701);
  300. end;
  301. procedure tcpuprocinfo.generate_exit_label(list: tasmlist);
  302. begin
  303. list.concat(taicpu.op_none(a_end_block));
  304. inherited generate_exit_label(list);
  305. end;
  306. procedure tcpuprocinfo.postprocess_code;
  307. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  308. var
  309. hp: tai;
  310. begin
  311. result:=nil;
  312. if not assigned(asmlist) then
  313. exit;
  314. hp:=tai(asmlist.first);
  315. while assigned(hp) do
  316. begin
  317. if hp.typ=ait_functype then
  318. begin
  319. result:=tai_functype(hp);
  320. exit;
  321. end;
  322. hp:=tai(hp.Next);
  323. end;
  324. end;
  325. procedure replace_local_frame_pointer(asmlist: TAsmList);
  326. var
  327. hp: tai;
  328. instr: taicpu;
  329. l: Integer;
  330. begin
  331. if not assigned(asmlist) then
  332. exit;
  333. hp:=tai(asmlist.first);
  334. while assigned(hp) do
  335. begin
  336. if hp.typ=ait_instruction then
  337. begin
  338. instr:=taicpu(hp);
  339. for l:=0 to instr.ops-1 do
  340. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  341. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  342. end;
  343. hp:=tai(hp.Next);
  344. end;
  345. end;
  346. function FindNextInstruction(hp: tai): taicpu;
  347. begin
  348. result:=nil;
  349. if not assigned(hp) then
  350. exit;
  351. repeat
  352. hp:=tai(hp.next);
  353. until not assigned(hp) or (hp.typ=ait_instruction);
  354. if assigned(hp) then
  355. result:=taicpu(hp);
  356. end;
  357. procedure resolve_labels_pass1(asmlist: TAsmList);
  358. var
  359. hp: tai;
  360. lastinstr, nextinstr: taicpu;
  361. cur_nesting_depth: longint;
  362. lbl: tai_label;
  363. blockstack: twasmblockstack;
  364. cblock: twasmblockitem;
  365. begin
  366. blockstack:=twasmblockstack.create;
  367. cur_nesting_depth:=0;
  368. lastinstr:=nil;
  369. hp:=tai(asmlist.first);
  370. while assigned(hp) do
  371. begin
  372. case hp.typ of
  373. ait_instruction:
  374. begin
  375. lastinstr:=taicpu(hp);
  376. case lastinstr.opcode of
  377. a_block,
  378. a_loop,
  379. a_if,
  380. a_try:
  381. begin
  382. blockstack.Concat(twasmblockitem.create(lastinstr));
  383. inc(cur_nesting_depth);
  384. end;
  385. a_else:
  386. begin
  387. cblock:=twasmblockitem(blockstack.Last);
  388. if (cblock=nil) or
  389. (cblock.blockstart.opcode<>a_if) or
  390. assigned(cblock.elseinstr) then
  391. internalerror(2021102302);
  392. cblock.elseinstr:=lastinstr;
  393. end;
  394. a_end_block,
  395. a_end_loop,
  396. a_end_if,
  397. a_end_try:
  398. begin
  399. dec(cur_nesting_depth);
  400. if cur_nesting_depth<0 then
  401. internalerror(2021102001);
  402. cblock:=twasmblockitem(blockstack.GetLast);
  403. if (cblock=nil) or
  404. ((cblock.blockstart.opcode=a_block) and (lastinstr.opcode<>a_end_block)) or
  405. ((cblock.blockstart.opcode=a_loop) and (lastinstr.opcode<>a_end_loop)) or
  406. ((cblock.blockstart.opcode=a_if) and (lastinstr.opcode<>a_end_if)) or
  407. ((cblock.blockstart.opcode=a_try) and (lastinstr.opcode<>a_end_try)) then
  408. internalerror(2021102301);
  409. cblock.free;
  410. end;
  411. else
  412. ;
  413. end;
  414. end;
  415. ait_label:
  416. begin
  417. lbl:=tai_label(hp);
  418. lbl.labsym.nestingdepth:=-1;
  419. nextinstr:=FindNextInstruction(hp);
  420. if assigned(nextinstr) and (nextinstr.opcode in [a_end_block,a_end_try,a_end_if]) then
  421. lbl.labsym.nestingdepth:=cur_nesting_depth
  422. else if assigned(lastinstr) and (lastinstr.opcode=a_loop) then
  423. lbl.labsym.nestingdepth:=cur_nesting_depth
  424. else if assigned(lastinstr) and (lastinstr.opcode in [a_end_block,a_end_try,a_end_if]) then
  425. lbl.labsym.nestingdepth:=cur_nesting_depth+1
  426. else if assigned(nextinstr) and (nextinstr.opcode=a_loop) then
  427. lbl.labsym.nestingdepth:=cur_nesting_depth+1;
  428. end;
  429. else
  430. ;
  431. end;
  432. hp:=tai(hp.Next);
  433. end;
  434. if cur_nesting_depth<>0 then
  435. internalerror(2021102002);
  436. blockstack.free;
  437. end;
  438. procedure resolve_labels_pass2(asmlist: TAsmList);
  439. var
  440. hp: tai;
  441. instr: taicpu;
  442. cur_nesting_depth: longint;
  443. begin
  444. cur_nesting_depth:=0;
  445. hp:=tai(asmlist.first);
  446. while assigned(hp) do
  447. begin
  448. if hp.typ=ait_instruction then
  449. begin
  450. instr:=taicpu(hp);
  451. case instr.opcode of
  452. a_block,
  453. a_loop,
  454. a_if,
  455. a_try:
  456. inc(cur_nesting_depth);
  457. a_end_block,
  458. a_end_loop,
  459. a_end_if,
  460. a_end_try:
  461. begin
  462. dec(cur_nesting_depth);
  463. if cur_nesting_depth<0 then
  464. internalerror(2021102003);
  465. end;
  466. a_br,
  467. a_br_if:
  468. begin
  469. if instr.ops<>1 then
  470. internalerror(2021102004);
  471. if instr.oper[0]^.typ=top_ref then
  472. begin
  473. if not assigned(instr.oper[0]^.ref^.symbol) then
  474. internalerror(2021102005);
  475. if (instr.oper[0]^.ref^.base<>NR_NO) or
  476. (instr.oper[0]^.ref^.index<>NR_NO) or
  477. (instr.oper[0]^.ref^.offset<>0) then
  478. internalerror(2021102006);
  479. if (instr.oper[0]^.ref^.symbol.nestingdepth<>-1) and
  480. (cur_nesting_depth>=instr.oper[0]^.ref^.symbol.nestingdepth) then
  481. instr.loadconst(0,cur_nesting_depth-instr.oper[0]^.ref^.symbol.nestingdepth)
  482. else
  483. begin
  484. {$ifndef EXTDEBUG}
  485. internalerror(2021102007);
  486. {$endif EXTDEBUG}
  487. end;
  488. end;
  489. end;
  490. else
  491. ;
  492. end;
  493. end;
  494. hp:=tai(hp.Next);
  495. end;
  496. if cur_nesting_depth<>0 then
  497. internalerror(2021102008);
  498. end;
  499. procedure resolve_labels(asmlist: TAsmList);
  500. begin
  501. if not assigned(asmlist) then
  502. exit;
  503. resolve_labels_pass1(asmlist);
  504. resolve_labels_pass2(asmlist);
  505. end;
  506. var
  507. templist : TAsmList;
  508. l : TWasmLocal;
  509. first: Boolean;
  510. local: tai_local;
  511. begin
  512. templist:=TAsmList.create;
  513. local:=nil;
  514. first:=true;
  515. l:=ttgwasm(tg).localvars.first;
  516. while Assigned(l) do
  517. begin
  518. local:=tai_local.create(l.typ);
  519. local.first:=first;
  520. first:=false;
  521. templist.Concat(local);
  522. l:=l.nextseq;
  523. end;
  524. if assigned(local) then
  525. local.last:=true;
  526. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  527. templist.Free;
  528. replace_local_frame_pointer(aktproccode);
  529. resolve_labels(aktproccode);
  530. inherited postprocess_code;
  531. end;
  532. procedure tcpuprocinfo.set_first_temp_offset;
  533. var
  534. sz : integer;
  535. i : integer;
  536. sym: tsym;
  537. begin
  538. {
  539. Stackframe layout:
  540. sp:
  541. <incoming parameters>
  542. sp+first_temp_offset:
  543. <locals>
  544. <temp>
  545. }
  546. procdef.init_paraloc_info(calleeside);
  547. sz := procdef.calleeargareasize;
  548. tg.setfirsttemp(sz);
  549. end;
  550. initialization
  551. cprocinfo:=tcpuprocinfo;
  552. end.