/compiler/jvm/njvmflw.pas

https://github.com/slibre/freepascal · Pascal · 492 lines · 319 code · 63 blank · 110 comment · 28 complexity · 90f0dc15eda091b6c3b9534bbddc34b8 MD5 · raw file

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