2
0

njvmadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. {
  2. Copyright (c) 2000-2011 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on 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 njvmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,
  22. node,ncgadd,cpubase;
  23. type
  24. { tjvmaddnode }
  25. tjvmaddnode = class(tcgaddnode)
  26. function pass_1: tnode;override;
  27. protected
  28. function first_addstring: tnode; override;
  29. function cmpnode2signedtopcmp: TOpCmp;
  30. procedure second_generic_compare;
  31. procedure pass_left_right;override;
  32. procedure second_addfloat;override;
  33. procedure second_cmpfloat;override;
  34. procedure second_cmpboolean;override;
  35. procedure second_cmpsmallset;override;
  36. procedure second_cmp64bit;override;
  37. procedure second_cmpordinal;override;
  38. end;
  39. implementation
  40. uses
  41. systems,
  42. cutils,verbose,constexp,
  43. symtable,symdef,
  44. paramgr,procinfo,
  45. aasmtai,aasmdata,aasmcpu,defutil,
  46. hlcgobj,hlcgcpu,cgutils,
  47. cpupara,
  48. ncon,nset,nadd,ncal,
  49. cgobj;
  50. {*****************************************************************************
  51. tjvmaddnode
  52. *****************************************************************************}
  53. function tjvmaddnode.pass_1: tnode;
  54. begin
  55. result:=inherited pass_1;
  56. if expectloc=LOC_FLAGS then
  57. expectloc:=LOC_JUMP;
  58. end;
  59. function tjvmaddnode.first_addstring: tnode;
  60. var
  61. cmpfuncname: string;
  62. begin
  63. { when we get here, we are sure that both the left and the right }
  64. { node are both strings of the same stringtype (JM) }
  65. case nodetype of
  66. addn:
  67. begin
  68. {$ifndef nounsupported}
  69. if not is_wide_or_unicode_string(resultdef) then
  70. begin
  71. result:=left;
  72. left:=nil;
  73. exit;
  74. end;
  75. {$endif nounsupported}
  76. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  77. begin
  78. result:=right;
  79. left.free;
  80. left:=nil;
  81. right:=nil;
  82. exit;
  83. end;
  84. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  85. begin
  86. result:=left;
  87. left:=nil;
  88. right.free;
  89. right:=nil;
  90. exit;
  91. end;
  92. { create the call to the concat routine both strings as arguments }
  93. result:=ccallnode.createintern('fpc_'+
  94. tstringdef(resultdef).stringtypname+'_concat',
  95. ccallparanode.create(right,
  96. ccallparanode.create(left,nil)));
  97. { we reused the arguments }
  98. left := nil;
  99. right := nil;
  100. end;
  101. ltn,lten,gtn,gten,equaln,unequaln :
  102. begin
  103. {$ifndef nounsupported}
  104. left.resultdef:=cunicodestringtype;
  105. {$endif nounsupported}
  106. { call compare routine }
  107. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  108. { for equality checks use optimized version }
  109. if nodetype in [equaln,unequaln] then
  110. cmpfuncname := cmpfuncname + '_equal';
  111. result := ccallnode.createintern(cmpfuncname,
  112. ccallparanode.create(right,ccallparanode.create(left,nil)));
  113. { and compare its result with 0 according to the original operator }
  114. result := caddnode.create(nodetype,result,
  115. cordconstnode.create(0,s32inttype,false));
  116. left := nil;
  117. right := nil;
  118. end;
  119. else
  120. internalerror(2011031401);
  121. end;
  122. end;
  123. function tjvmaddnode.cmpnode2signedtopcmp: TOpCmp;
  124. begin
  125. case nodetype of
  126. gtn: result:=OC_GT;
  127. gten: result:=OC_GTE;
  128. ltn: result:=OC_LT;
  129. lten: result:=OC_LTE;
  130. equaln: result:=OC_EQ;
  131. unequaln: result:=OC_NE;
  132. else
  133. internalerror(2011010412);
  134. end;
  135. end;
  136. procedure tjvmaddnode.second_generic_compare;
  137. var
  138. cmpop: TOpCmp;
  139. begin
  140. pass_left_right;
  141. { swap the operands to make it easier for the optimizer to optimize
  142. the operand stack slot reloading in case both are in a register }
  143. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  144. (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  145. swapleftright;
  146. cmpop:=cmpnode2signedtopcmp;
  147. if (nf_swapped in flags) then
  148. cmpop:=swap_opcmp(cmpop);
  149. location_reset(location,LOC_JUMP,OS_NO);
  150. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  151. hlcg.a_cmp_loc_reg_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location,left.location.register,current_procinfo.CurrTrueLabel)
  152. else case right.location.loc of
  153. LOC_REGISTER,LOC_CREGISTER:
  154. hlcg.a_cmp_reg_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.register,left.location,current_procinfo.CurrTrueLabel);
  155. LOC_REFERENCE,LOC_CREFERENCE:
  156. hlcg.a_cmp_ref_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.reference,left.location,current_procinfo.CurrTrueLabel);
  157. LOC_CONSTANT:
  158. hlcg.a_cmp_const_loc_label(current_asmdata.CurrAsmList,left.resultdef,cmpop,right.location.value,left.location,current_procinfo.CurrTrueLabel);
  159. else
  160. internalerror(2011010413);
  161. end;
  162. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  163. end;
  164. procedure tjvmaddnode.pass_left_right;
  165. begin
  166. swapleftright;
  167. inherited pass_left_right;
  168. end;
  169. procedure tjvmaddnode.second_addfloat;
  170. var
  171. op : TAsmOp;
  172. commutative : boolean;
  173. begin
  174. pass_left_right;
  175. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  176. location.register:=hlcg.getfpuregister(current_asmdata.CurrAsmList,resultdef);
  177. commutative:=false;
  178. case nodetype of
  179. addn :
  180. begin
  181. if location.size=OS_F64 then
  182. op:=a_dadd
  183. else
  184. op:=a_fadd;
  185. commutative:=true;
  186. end;
  187. muln :
  188. begin
  189. if location.size=OS_F64 then
  190. op:=a_dmul
  191. else
  192. op:=a_fmul;
  193. commutative:=true;
  194. end;
  195. subn :
  196. begin
  197. if location.size=OS_F64 then
  198. op:=a_dsub
  199. else
  200. op:=a_fsub;
  201. end;
  202. slashn :
  203. begin
  204. if location.size=OS_F64 then
  205. op:=a_ddiv
  206. else
  207. op:=a_fdiv;
  208. end;
  209. else
  210. internalerror(2011010402);
  211. end;
  212. { swap the operands to make it easier for the optimizer to optimize
  213. the operand stack slot reloading (non-commutative operations must
  214. always be in the correct order though) }
  215. if (commutative and
  216. (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  217. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER])) or
  218. (not commutative and
  219. (nf_swapped in flags)) then
  220. swapleftright;
  221. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  222. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  223. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  224. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
  225. { could be optimized in the future by keeping the results on the stack,
  226. if we add code to swap the operands when necessary (a_swap for
  227. singles, store/load/load for doubles since there is no swap for
  228. 2-slot elements -- also adjust expectloc in that case! }
  229. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  230. end;
  231. procedure tjvmaddnode.second_cmpfloat;
  232. var
  233. op : tasmop;
  234. cmpop: TOpCmp;
  235. begin
  236. pass_left_right;
  237. { swap the operands to make it easier for the optimizer to optimize
  238. the operand stack slot reloading in case both are in a register }
  239. if (left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) and
  240. (right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  241. swapleftright;
  242. cmpop:=cmpnode2signedtopcmp;
  243. if (nf_swapped in flags) then
  244. cmpop:=swap_opcmp(cmpop);
  245. location_reset(location,LOC_JUMP,OS_NO);
  246. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  247. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  248. { compares two floating point values and puts 1/0/-1 on stack depending
  249. on whether value1 >/=/< value2 }
  250. if left.location.size=OS_F64 then
  251. { make sure that comparisons with NaNs always return false for </> }
  252. if nodetype in [ltn,lten] then
  253. op:=a_dcmpg
  254. else
  255. op:=a_dcmpl
  256. else if nodetype in [ltn,lten] then
  257. op:=a_fcmpg
  258. else
  259. op:=a_fcmpl;
  260. current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
  261. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
  262. current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpop],current_procinfo.CurrTrueLabel));
  263. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  264. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  265. end;
  266. procedure tjvmaddnode.second_cmpboolean;
  267. begin
  268. second_generic_compare;
  269. end;
  270. procedure tjvmaddnode.second_cmpsmallset;
  271. begin
  272. if (nodetype in [equaln,unequaln]) then
  273. begin
  274. second_generic_compare;
  275. exit;
  276. end;
  277. case nodetype of
  278. lten,gten:
  279. begin
  280. pass_left_right;
  281. If (not(nf_swapped in flags) and
  282. (nodetype=lten)) or
  283. ((nf_swapped in flags) and
  284. (nodetype=gten)) then
  285. swapleftright;
  286. location_reset(location,LOC_JUMP,OS_NO);
  287. // now we have to check whether left >= right:
  288. // (right and not(left)=0)
  289. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,left.resultdef,left.location);
  290. thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
  291. thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,OP_AND,right.resultdef,right.location);
  292. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ifeq,current_procinfo.CurrTrueLabel));
  293. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  294. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  295. end;
  296. else
  297. internalerror(2011010414);
  298. end;
  299. end;
  300. procedure tjvmaddnode.second_cmp64bit;
  301. begin
  302. second_generic_compare;
  303. end;
  304. procedure tjvmaddnode.second_cmpordinal;
  305. begin
  306. second_generic_compare;
  307. end;
  308. begin
  309. caddnode:=tjvmaddnode;
  310. end.