njvmflw.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. {
  2. Copyright (c) 1998-2011 by Florian Klaempfl and Jonas Maebe
  3. Generate assembler for nodes that influence the flow for the JVM
  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 njvmflw;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,node,nflw;
  22. type
  23. tjvmraisenode = class(traisenode)
  24. function pass_typecheck: tnode; override;
  25. procedure pass_generate_code;override;
  26. end;
  27. tjvmtryexceptnode = class(ttryexceptnode)
  28. procedure pass_generate_code;override;
  29. end;
  30. tjvmtryfinallynode = class(ttryfinallynode)
  31. procedure pass_generate_code;override;
  32. end;
  33. tjvmonnode = class(tonnode)
  34. procedure pass_generate_code;override;
  35. end;
  36. implementation
  37. uses
  38. verbose,globals,systems,globtype,constexp,
  39. symconst,symdef,symsym,aasmtai,aasmdata,aasmcpu,defutil,jvmdef,
  40. procinfo,cgbase,pass_2,parabase,
  41. cpubase,cpuinfo,
  42. nld,ncon,
  43. tgobj,paramgr,
  44. cgutils,hlcgobj,hlcgcpu
  45. ;
  46. {*****************************************************************************
  47. SecondRaise
  48. *****************************************************************************}
  49. var
  50. current_except_loc: tlocation;
  51. function tjvmraisenode.pass_typecheck: tnode;
  52. begin
  53. Result:=inherited pass_typecheck;
  54. if codegenerror then
  55. exit;
  56. { Java exceptions must descend from java.lang.Throwable }
  57. if assigned(left) and
  58. not(left.resultdef).is_related(java_jlthrowable) then
  59. MessagePos2(left.fileinfo,type_e_incompatible_types,left.resultdef.typename,'class(JLThrowable)');
  60. { Java exceptions cannot be raised "at" a specific location }
  61. if assigned(right) then
  62. MessagePos(right.fileinfo,parser_e_illegal_expression);
  63. end;
  64. procedure tjvmraisenode.pass_generate_code;
  65. begin
  66. if assigned(left) then
  67. begin
  68. secondpass(left);
  69. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  70. end
  71. else
  72. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,java_jlthrowable,current_except_loc);
  73. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_athrow));
  74. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  75. end;
  76. {*****************************************************************************
  77. SecondTryExcept
  78. *****************************************************************************}
  79. var
  80. begintrylabel,
  81. endtrylabel: tasmlabel;
  82. endexceptlabel : tasmlabel;
  83. procedure tjvmtryexceptnode.pass_generate_code;
  84. var
  85. oldendexceptlabel,
  86. oldbegintrylabel,
  87. oldendtrylabel,
  88. defaultcatchlabel: tasmlabel;
  89. oldflowcontrol,tryflowcontrol,
  90. exceptflowcontrol : tflowcontrol;
  91. begin
  92. location_reset(location,LOC_VOID,OS_NO);
  93. oldflowcontrol:=flowcontrol;
  94. flowcontrol:=[fc_inflowcontrol];
  95. { this can be called recursivly }
  96. oldbegintrylabel:=begintrylabel;
  97. oldendtrylabel:=endtrylabel;
  98. oldendexceptlabel:=endexceptlabel;
  99. { get new labels for the control flow statements }
  100. current_asmdata.getaddrlabel(begintrylabel);
  101. current_asmdata.getaddrlabel(endtrylabel);
  102. current_asmdata.getjumplabel(endexceptlabel);
  103. { try block }
  104. { set control flow labels for the try block }
  105. hlcg.a_label(current_asmdata.CurrAsmList,begintrylabel);
  106. secondpass(left);
  107. hlcg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  108. tryflowcontrol:=flowcontrol;
  109. { jump over exception handling blocks }
  110. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  111. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  112. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  113. { set control flow labels for the except block }
  114. { and the on statements }
  115. flowcontrol:=[fc_inflowcontrol];
  116. { on-statements }
  117. if assigned(right) then
  118. secondpass(right);
  119. { default handling except handling }
  120. if assigned(t1) then
  121. begin
  122. current_asmdata.getaddrlabel(defaultcatchlabel);
  123. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  124. 'all',begintrylabel,endtrylabel,defaultcatchlabel));
  125. hlcg.a_label(current_asmdata.CurrAsmList,defaultcatchlabel);
  126. { here we don't have to reset flowcontrol }
  127. { the default and on flowcontrols are handled equal }
  128. { get the exception object from the stack and store it for use by
  129. the exception code (in case of an anonymous "raise") }
  130. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  131. location_reset_ref(current_except_loc,LOC_REFERENCE,OS_ADDR,4);
  132. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),java_jlthrowable,current_except_loc.reference);
  133. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  134. thlcgjvm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,java_jlthrowable,current_except_loc);
  135. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  136. { and generate the exception handling code }
  137. secondpass(t1);
  138. { free the temp containing the exception and invalidate }
  139. tg.UngetLocal(current_asmdata.CurrAsmList,current_except_loc.reference);
  140. current_except_loc.loc:=LOC_INVALID;
  141. exceptflowcontrol:=flowcontrol;
  142. end
  143. else
  144. exceptflowcontrol:=flowcontrol;
  145. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  146. { restore all saved labels }
  147. begintrylabel:=oldbegintrylabel;
  148. endtrylabel:=oldendtrylabel;
  149. endexceptlabel:=oldendexceptlabel;
  150. { return all used control flow statements }
  151. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  152. tryflowcontrol - [fc_inflowcontrol]);
  153. end;
  154. {*****************************************************************************
  155. SecondOn
  156. *****************************************************************************}
  157. procedure tjvmonnode.pass_generate_code;
  158. var
  159. thisonlabel : tasmlabel;
  160. oldflowcontrol : tflowcontrol;
  161. exceptvarsym : tlocalvarsym;
  162. begin
  163. location_reset(location,LOC_VOID,OS_NO);
  164. oldflowcontrol:=flowcontrol;
  165. flowcontrol:=[fc_inflowcontrol];
  166. current_asmdata.getjumplabel(thisonlabel);
  167. hlcg.a_label(current_asmdata.CurrAsmList,thisonlabel);
  168. if assigned(excepTSymtable) then
  169. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  170. else
  171. internalerror(2011020402);
  172. { add exception catching information for the JVM: exception type
  173. (will have to be adjusted if/when support for catching class
  174. reference types is added), begin/end of code in which the exception
  175. can be raised, and start of this exception handling code }
  176. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  177. tobjectdef(exceptvarsym.vardef).jvm_full_typename(true),
  178. begintrylabel,endtrylabel,thisonlabel));
  179. { Retrieve exception variable }
  180. { 1) prepare the location where we'll store it }
  181. location_reset_ref(exceptvarsym.localloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  182. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),exceptvarsym.vardef,exceptvarsym.localloc.reference);
  183. current_except_loc:=exceptvarsym.localloc;
  184. { 2) the exception variable is at the top of the evaluation stack
  185. (placed there by the JVM) -> adjust stack count, then store it }
  186. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  187. thlcgjvm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,exceptvarsym.vardef,current_except_loc);
  188. if assigned(right) then
  189. secondpass(right);
  190. { clear some stuff }
  191. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  192. exceptvarsym.localloc.loc:=LOC_INVALID;
  193. current_except_loc.loc:=LOC_INVALID;
  194. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  195. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  196. { next on node }
  197. if assigned(left) then
  198. secondpass(left);
  199. end;
  200. {*****************************************************************************
  201. SecondTryFinally
  202. *****************************************************************************}
  203. procedure tjvmtryfinallynode.pass_generate_code;
  204. var
  205. begintrylabel,
  206. endtrylabel,
  207. reraiselabel,
  208. finallylabel,
  209. finallyexceptlabel,
  210. endfinallylabel,
  211. exitfinallylabel,
  212. continuefinallylabel,
  213. breakfinallylabel,
  214. oldCurrExitLabel,
  215. oldContinueLabel,
  216. oldBreakLabel : tasmlabel;
  217. oldflowcontrol,tryflowcontrol : tflowcontrol;
  218. finallycodecopy: tnode;
  219. reasonbuf,
  220. exceptreg: tregister;
  221. begin
  222. { not necessary on a garbage-collected platform }
  223. if implicitframe then
  224. internalerror(2011031803);
  225. location_reset(location,LOC_VOID,OS_NO);
  226. { check if child nodes do a break/continue/exit }
  227. oldflowcontrol:=flowcontrol;
  228. flowcontrol:=[fc_inflowcontrol];
  229. current_asmdata.getjumplabel(finallylabel);
  230. current_asmdata.getjumplabel(endfinallylabel);
  231. current_asmdata.getjumplabel(reraiselabel);
  232. { the finally block must catch break, continue and exit }
  233. { statements }
  234. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  235. current_asmdata.getjumplabel(exitfinallylabel);
  236. current_procinfo.CurrExitLabel:=exitfinallylabel;
  237. if assigned(current_procinfo.CurrBreakLabel) then
  238. begin
  239. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  240. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  241. current_asmdata.getjumplabel(breakfinallylabel);
  242. current_asmdata.getjumplabel(continuefinallylabel);
  243. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  244. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  245. end;
  246. { allocate reg to store the reason why the finally block was entered
  247. (no exception, break, continue, exit), so we can continue to the
  248. right label afterwards. In case of an exception, we use a separate
  249. (duplicate) finally block because otherwise the JVM's bytecode
  250. verification cannot statically prove that the exception reraise code
  251. will only execute in case an exception actually happened }
  252. reasonbuf:=hlcg.getaddressregister(current_asmdata.CurrAsmList,s32inttype);
  253. { try code }
  254. begintrylabel:=nil;
  255. endtrylabel:=nil;
  256. if assigned(left) then
  257. begin
  258. current_asmdata.getaddrlabel(begintrylabel);
  259. current_asmdata.getaddrlabel(endtrylabel);
  260. hlcg.a_label(current_asmdata.CurrAsmList,begintrylabel);
  261. secondpass(left);
  262. hlcg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  263. tryflowcontrol:=flowcontrol;
  264. if codegenerror then
  265. exit;
  266. { reason: no exception occurred }
  267. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,0,reasonbuf);
  268. end
  269. else
  270. tryflowcontrol:=[fc_inflowcontrol];
  271. { begin of the finally code }
  272. hlcg.a_label(current_asmdata.CurrAsmList,finallylabel);
  273. { finally code }
  274. flowcontrol:=[fc_inflowcontrol];
  275. { duplicate finally code for case when exception happened }
  276. if assigned(begintrylabel) then
  277. finallycodecopy:=right.getcopy;
  278. secondpass(right);
  279. { goto is allowed if it stays inside the finally block,
  280. this is checked using the exception block number }
  281. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  282. CGMessage(cg_e_control_flow_outside_finally);
  283. if codegenerror then
  284. begin
  285. if assigned(begintrylabel) then
  286. finallycodecopy.free;
  287. exit;
  288. end;
  289. { don't generate line info for internal cleanup }
  290. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  291. { the reasonbuf holds the reason why this (non-exception) finally code
  292. was executed:
  293. 0 = try code simply finished
  294. 1 = (unused) exception raised
  295. 2 = exit called
  296. 3 = break called
  297. 4 = continue called }
  298. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,0,reasonbuf,endfinallylabel);
  299. if fc_exit in tryflowcontrol then
  300. if ([fc_break,fc_continue]*tryflowcontrol)<>[] then
  301. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,2,reasonbuf,oldCurrExitLabel)
  302. else
  303. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  304. if fc_break in tryflowcontrol then
  305. if fc_continue in tryflowcontrol then
  306. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,3,reasonbuf,oldBreakLabel)
  307. else
  308. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  309. if fc_continue in tryflowcontrol then
  310. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  311. { now generate the trampolines for exit/break/continue to load the reasonbuf }
  312. if fc_exit in tryflowcontrol then
  313. begin
  314. hlcg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  315. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,2,reasonbuf);
  316. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  317. end;
  318. if fc_break in tryflowcontrol then
  319. begin
  320. hlcg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  321. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,3,reasonbuf);
  322. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  323. end;
  324. if fc_continue in tryflowcontrol then
  325. begin
  326. hlcg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  327. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,4,reasonbuf);
  328. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  329. end;
  330. { jump over finally-code-in-case-an-exception-happened }
  331. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  332. { generate finally code in case an exception occurred }
  333. if assigned(begintrylabel) then
  334. begin
  335. current_asmdata.getaddrlabel(finallyexceptlabel);
  336. hlcg.a_label(current_asmdata.CurrAsmList,finallyexceptlabel);
  337. { catch the exceptions }
  338. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  339. 'all',begintrylabel,endtrylabel,finallyexceptlabel));
  340. { store the generated exception object to a temp }
  341. exceptreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlthrowable);
  342. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  343. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,java_jlthrowable,exceptreg);
  344. { generate the finally code again }
  345. secondpass(finallycodecopy);
  346. finallycodecopy.free;
  347. { reraise the exception }
  348. thlcgjvm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,java_jlthrowable,exceptreg);
  349. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_athrow));
  350. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  351. end;
  352. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  353. { end cleanup }
  354. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  355. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  356. if assigned(current_procinfo.CurrBreakLabel) then
  357. begin
  358. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  359. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  360. end;
  361. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  362. end;
  363. begin
  364. craisenode:=tjvmraisenode;
  365. ctryexceptnode:=tjvmtryexceptnode;
  366. ctryfinallynode:=tjvmtryfinallynode;
  367. connode:=tjvmonnode;
  368. end.