njvmflw.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. flowcontrol:=[fc_inflowcontrol];
  106. hlcg.a_label(current_asmdata.CurrAsmList,begintrylabel);
  107. secondpass(left);
  108. hlcg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  109. tryflowcontrol:=flowcontrol;
  110. { jump over exception handling blocks }
  111. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  112. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  113. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  114. { set control flow labels for the except block }
  115. { and the on statements }
  116. flowcontrol:=[fc_inflowcontrol];
  117. { on-statements }
  118. if assigned(right) then
  119. secondpass(right);
  120. { default handling except handling }
  121. if assigned(t1) then
  122. begin
  123. current_asmdata.getaddrlabel(defaultcatchlabel);
  124. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  125. 'all',begintrylabel,endtrylabel,defaultcatchlabel));
  126. hlcg.a_label(current_asmdata.CurrAsmList,defaultcatchlabel);
  127. { here we don't have to reset flowcontrol }
  128. { the default and on flowcontrols are handled equal }
  129. { get the exception object from the stack and store it for use by
  130. the exception code (in case of an anonymous "raise") }
  131. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  132. location_reset_ref(current_except_loc,LOC_REFERENCE,OS_ADDR,4);
  133. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),java_jlthrowable,current_except_loc.reference);
  134. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  135. thlcgjvm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,java_jlthrowable,current_except_loc);
  136. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  137. { and generate the exception handling code }
  138. secondpass(t1);
  139. { free the temp containing the exception and invalidate }
  140. tg.UngetLocal(current_asmdata.CurrAsmList,current_except_loc.reference);
  141. current_except_loc.loc:=LOC_INVALID;
  142. exceptflowcontrol:=flowcontrol;
  143. end;
  144. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  145. { restore all saved labels }
  146. begintrylabel:=oldbegintrylabel;
  147. endtrylabel:=oldendtrylabel;
  148. endexceptlabel:=oldendexceptlabel;
  149. { return all used control flow statements }
  150. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  151. tryflowcontrol - [fc_inflowcontrol]);
  152. end;
  153. {*****************************************************************************
  154. SecondOn
  155. *****************************************************************************}
  156. procedure tjvmonnode.pass_generate_code;
  157. var
  158. thisonlabel : tasmlabel;
  159. oldflowcontrol : tflowcontrol;
  160. exceptvarsym : tlocalvarsym;
  161. begin
  162. location_reset(location,LOC_VOID,OS_NO);
  163. oldflowcontrol:=flowcontrol;
  164. flowcontrol:=[fc_inflowcontrol];
  165. current_asmdata.getjumplabel(thisonlabel);
  166. hlcg.a_label(current_asmdata.CurrAsmList,thisonlabel);
  167. if assigned(excepTSymtable) then
  168. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  169. else
  170. internalerror(2011020402);
  171. { add exception catching information for the JVM: exception type
  172. (will have to be adjusted if/when support for catching class
  173. reference types is added), begin/end of code in which the exception
  174. can be raised, and start of this exception handling code }
  175. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  176. tobjectdef(exceptvarsym.vardef).jvm_full_typename(true),
  177. begintrylabel,endtrylabel,thisonlabel));
  178. { Retrieve exception variable }
  179. { 1) prepare the location where we'll store it }
  180. location_reset_ref(exceptvarsym.localloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  181. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),exceptvarsym.vardef,exceptvarsym.localloc.reference);
  182. current_except_loc:=exceptvarsym.localloc;
  183. { 2) the exception variable is at the top of the evaluation stack
  184. (placed there by the JVM) -> adjust stack count, then store it }
  185. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  186. thlcgjvm(hlcg).a_load_stack_loc(current_asmdata.CurrAsmList,exceptvarsym.vardef,current_except_loc);
  187. if assigned(right) then
  188. secondpass(right);
  189. { clear some stuff }
  190. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  191. exceptvarsym.localloc.loc:=LOC_INVALID;
  192. current_except_loc.loc:=LOC_INVALID;
  193. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  194. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  195. { next on node }
  196. if assigned(left) then
  197. secondpass(left);
  198. end;
  199. {*****************************************************************************
  200. SecondTryFinally
  201. *****************************************************************************}
  202. procedure tjvmtryfinallynode.pass_generate_code;
  203. var
  204. begintrylabel,
  205. endtrylabel,
  206. reraiselabel,
  207. finallylabel,
  208. finallyexceptlabel,
  209. endfinallylabel,
  210. exitfinallylabel,
  211. continuefinallylabel,
  212. breakfinallylabel,
  213. oldCurrExitLabel,
  214. oldContinueLabel,
  215. oldBreakLabel : tasmlabel;
  216. oldflowcontrol,tryflowcontrol : tflowcontrol;
  217. finallycodecopy: tnode;
  218. reasonbuf,
  219. exceptreg: tregister;
  220. begin
  221. { not necessary on a garbage-collected platform }
  222. if implicitframe then
  223. internalerror(2011031803);
  224. location_reset(location,LOC_VOID,OS_NO);
  225. { check if child nodes do a break/continue/exit }
  226. oldflowcontrol:=flowcontrol;
  227. flowcontrol:=[fc_inflowcontrol];
  228. current_asmdata.getjumplabel(finallylabel);
  229. current_asmdata.getjumplabel(endfinallylabel);
  230. current_asmdata.getjumplabel(reraiselabel);
  231. { the finally block must catch break, continue and exit }
  232. { statements }
  233. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  234. current_asmdata.getjumplabel(exitfinallylabel);
  235. current_procinfo.CurrExitLabel:=exitfinallylabel;
  236. if assigned(current_procinfo.CurrBreakLabel) then
  237. begin
  238. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  239. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  240. current_asmdata.getjumplabel(breakfinallylabel);
  241. current_asmdata.getjumplabel(continuefinallylabel);
  242. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  243. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  244. end;
  245. { allocate reg to store the reason why the finally block was entered
  246. (no exception, break, continue, exit), so we can continue to the
  247. right label afterwards. In case of an exception, we use a separate
  248. (duplicate) finally block because otherwise the JVM's bytecode
  249. verification cannot statically prove that the exception reraise code
  250. will only execute in case an exception actually happened }
  251. reasonbuf:=hlcg.getaddressregister(current_asmdata.CurrAsmList,s32inttype);
  252. { try code }
  253. begintrylabel:=nil;
  254. endtrylabel:=nil;
  255. if assigned(left) then
  256. begin
  257. current_asmdata.getaddrlabel(begintrylabel);
  258. current_asmdata.getaddrlabel(endtrylabel);
  259. hlcg.a_label(current_asmdata.CurrAsmList,begintrylabel);
  260. secondpass(left);
  261. hlcg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  262. tryflowcontrol:=flowcontrol;
  263. if codegenerror then
  264. exit;
  265. { reason: no exception occurred }
  266. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,0,reasonbuf);
  267. end;
  268. { begin of the finally code }
  269. hlcg.a_label(current_asmdata.CurrAsmList,finallylabel);
  270. { finally code }
  271. flowcontrol:=[fc_inflowcontrol];
  272. { duplicate finally code for case when exception happened }
  273. if assigned(begintrylabel) then
  274. finallycodecopy:=right.getcopy;
  275. secondpass(right);
  276. { goto is allowed if it stays inside the finally block,
  277. this is checked using the exception block number }
  278. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  279. CGMessage(cg_e_control_flow_outside_finally);
  280. if codegenerror then
  281. begin
  282. if assigned(begintrylabel) then
  283. finallycodecopy.free;
  284. exit;
  285. end;
  286. { don't generate line info for internal cleanup }
  287. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  288. { the reasonbuf holds the reason why this (non-exception) finally code
  289. was executed:
  290. 0 = try code simply finished
  291. 1 = (unused) exception raised
  292. 2 = exit called
  293. 3 = break called
  294. 4 = continue called }
  295. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,0,reasonbuf,endfinallylabel);
  296. if fc_exit in tryflowcontrol then
  297. if ([fc_break,fc_continue]*tryflowcontrol)<>[] then
  298. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,2,reasonbuf,oldCurrExitLabel)
  299. else
  300. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  301. if fc_break in tryflowcontrol then
  302. if fc_continue in tryflowcontrol then
  303. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,s32inttype,OC_EQ,3,reasonbuf,oldBreakLabel)
  304. else
  305. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  306. if fc_continue in tryflowcontrol then
  307. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  308. { now generate the trampolines for exit/break/continue to load the reasonbuf }
  309. if fc_exit in tryflowcontrol then
  310. begin
  311. hlcg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  312. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,2,reasonbuf);
  313. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  314. end;
  315. if fc_break in tryflowcontrol then
  316. begin
  317. hlcg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  318. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,3,reasonbuf);
  319. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  320. end;
  321. if fc_continue in tryflowcontrol then
  322. begin
  323. hlcg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  324. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,s32inttype,4,reasonbuf);
  325. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel);
  326. end;
  327. { jump over finally-code-in-case-an-exception-happened }
  328. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  329. { generate finally code in case an exception occurred }
  330. if assigned(begintrylabel) then
  331. begin
  332. current_asmdata.getaddrlabel(finallyexceptlabel);
  333. hlcg.a_label(current_asmdata.CurrAsmList,finallyexceptlabel);
  334. { catch the exceptions }
  335. current_asmdata.CurrAsmList.concat(tai_jcatch.create(
  336. 'all',begintrylabel,endtrylabel,finallyexceptlabel));
  337. { store the generated exception object to a temp }
  338. exceptreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlthrowable);
  339. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  340. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,java_jlthrowable,exceptreg);
  341. { generate the finally code again }
  342. secondpass(finallycodecopy);
  343. finallycodecopy.free;
  344. { reraise the exception }
  345. thlcgjvm(hlcg).a_load_reg_stack(current_asmdata.CurrAsmList,java_jlthrowable,exceptreg);
  346. current_asmdata.CurrAsmList.Concat(taicpu.op_none(a_athrow));
  347. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  348. end;
  349. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  350. { end cleanup }
  351. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  352. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  353. if assigned(current_procinfo.CurrBreakLabel) then
  354. begin
  355. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  356. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  357. end;
  358. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  359. end;
  360. begin
  361. craisenode:=tjvmraisenode;
  362. ctryexceptnode:=tjvmtryexceptnode;
  363. ctryfinallynode:=tjvmtryfinallynode;
  364. connode:=tjvmonnode;
  365. end.