ngppcadd.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. {
  2. Copyright (c) 2000-2006 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the PowerPC (32 and 64 bit generic)
  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 ngppcadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,cpubase;
  22. type
  23. tgenppcaddnode = class(tcgaddnode)
  24. function pass_1: tnode; override;
  25. protected
  26. procedure pass_left_and_right;
  27. procedure load_left_right(cmpop, load_constants: boolean);
  28. function getresflags : tresflags;
  29. procedure emit_compare(unsigned: boolean); virtual; abstract;
  30. procedure second_addfloat;override;
  31. procedure second_addboolean;override;
  32. procedure second_addsmallset;override;
  33. end;
  34. implementation
  35. {*****************************************************************************
  36. Pass 1
  37. *****************************************************************************}
  38. uses
  39. globtype,systems,
  40. cutils,verbose,globals,
  41. symconst,symdef,paramgr,
  42. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  43. cgbase,cpuinfo,pass_1,pass_2,regvars,
  44. cpupara,cgcpu,cgutils,procinfo,
  45. ncon,nset,
  46. ncgutil,tgobj,rgobj,rgcpu,cgobj,hlcgobj;
  47. {*****************************************************************************
  48. Pass 1
  49. *****************************************************************************}
  50. function tgenppcaddnode.pass_1: tnode;
  51. begin
  52. typecheckpass(left);
  53. if (nodetype in [equaln,unequaln]) and
  54. (left.resultdef.typ = orddef) and
  55. is_64bit(left.resultdef) then
  56. begin
  57. result := nil;
  58. firstpass(left);
  59. firstpass(right);
  60. expectloc := LOC_FLAGS;
  61. exit;
  62. end;
  63. result := inherited pass_1;
  64. end;
  65. {*****************************************************************************
  66. Helpers
  67. *****************************************************************************}
  68. procedure tgenppcaddnode.pass_left_and_right;
  69. begin
  70. { calculate the operator which is more difficult }
  71. firstcomplex(self);
  72. { in case of constant put it to the left }
  73. if (left.nodetype=ordconstn) then
  74. swapleftright;
  75. secondpass(left);
  76. secondpass(right);
  77. end;
  78. procedure tgenppcaddnode.load_left_right(cmpop, load_constants: boolean);
  79. procedure load_node(var n: tnode);
  80. begin
  81. case n.location.loc of
  82. LOC_REGISTER,
  83. LOC_CREGISTER:
  84. ;
  85. LOC_CONSTANT:
  86. begin
  87. if load_constants then
  88. hlcg.location_force_reg(current_asmdata.CurrAsmList,n.location,n.resultdef,n.resultdef,false);
  89. end;
  90. else
  91. hlcg.location_force_reg(current_asmdata.CurrAsmList,n.location,n.resultdef,n.resultdef,false);
  92. end;
  93. end;
  94. begin
  95. load_node(left);
  96. load_node(right);
  97. if not(cmpop) then
  98. begin
  99. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  100. {$ifndef cpu64bitalu}
  101. if is_64bit(resultdef) then
  102. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  103. {$endif not cpu64bitalu}
  104. end;
  105. end;
  106. function tgenppcaddnode.getresflags : tresflags;
  107. begin
  108. if (left.resultdef.typ <> floatdef) then
  109. begin
  110. result.cr := RS_CR0;
  111. case nodetype of
  112. equaln : result.flag:=F_EQ;
  113. unequaln : result.flag:=F_NE;
  114. else
  115. if nf_swapped in flags then
  116. case nodetype of
  117. ltn : result.flag:=F_GT;
  118. lten : result.flag:=F_GE;
  119. gtn : result.flag:=F_LT;
  120. gten : result.flag:=F_LE;
  121. end
  122. else
  123. case nodetype of
  124. ltn : result.flag:=F_LT;
  125. lten : result.flag:=F_LE;
  126. gtn : result.flag:=F_GT;
  127. gten : result.flag:=F_GE;
  128. end;
  129. end
  130. end
  131. else
  132. begin
  133. result.cr := RS_CR1;
  134. if (nodetype=equaln) then
  135. result.flag:=F_EQ
  136. else if (nodetype=unequaln) then
  137. result.flag:=F_NE
  138. else if (nf_swapped in flags) then
  139. case nodetype of
  140. ltn : result.flag:=F_FA;
  141. lten : result.flag:=F_FAE;
  142. gtn : result.flag:=F_FB;
  143. gten : result.flag:=F_FBE;
  144. else
  145. internalerror(2014031902);
  146. end
  147. else
  148. case nodetype of
  149. ltn : result.flag:=F_FB;
  150. lten : result.flag:=F_FBE;
  151. gtn : result.flag:=F_FA;
  152. gten : result.flag:=F_FAE;
  153. else
  154. internalerror(2014031903);
  155. end;
  156. end;
  157. end;
  158. {*****************************************************************************
  159. AddBoolean
  160. *****************************************************************************}
  161. procedure tgenppcaddnode.second_addboolean;
  162. var
  163. cgop : TOpCg;
  164. cgsize : TCgSize;
  165. cmpop,
  166. isjump : boolean;
  167. otl,ofl : tasmlabel;
  168. begin
  169. { calculate the operator which is more difficult }
  170. firstcomplex(self);
  171. otl:=nil;
  172. ofl:=nil;
  173. cmpop:=false;
  174. if (torddef(left.resultdef).ordtype in [pasbool8,bool8bit]) or
  175. (torddef(right.resultdef).ordtype in [pasbool8,bool8bit]) then
  176. cgsize:=OS_8
  177. else if (torddef(left.resultdef).ordtype in [pasbool16,bool16bit]) or
  178. (torddef(right.resultdef).ordtype in [pasbool16,bool16bit]) then
  179. cgsize:=OS_16
  180. else if (torddef(left.resultdef).ordtype in [pasbool32,bool32bit]) or
  181. (torddef(right.resultdef).ordtype in [pasbool32,bool32bit]) then
  182. cgsize:=OS_32
  183. else
  184. cgsize:=OS_64;
  185. if {$ifndef cpu64bitalu}(cgsize<>OS_64) and{$endif}
  186. (((cs_full_boolean_eval in current_settings.localswitches) and
  187. not(nf_short_bool in flags)) or
  188. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn])) then
  189. begin
  190. if left.nodetype in [ordconstn,realconstn] then
  191. swapleftright;
  192. isjump:=(left.expectloc=LOC_JUMP);
  193. if isjump then
  194. begin
  195. otl:=current_procinfo.CurrTrueLabel;
  196. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  197. ofl:=current_procinfo.CurrFalseLabel;
  198. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  199. end;
  200. secondpass(left);
  201. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  202. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(cgsize),false);
  203. if isjump then
  204. begin
  205. current_procinfo.CurrTrueLabel:=otl;
  206. current_procinfo.CurrFalseLabel:=ofl;
  207. end
  208. else if left.location.loc=LOC_JUMP then
  209. internalerror(2003122901);
  210. isjump:=(right.expectloc=LOC_JUMP);
  211. if isjump then
  212. begin
  213. otl:=current_procinfo.CurrTrueLabel;
  214. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  215. ofl:=current_procinfo.CurrFalseLabel;
  216. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  217. end;
  218. secondpass(right);
  219. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  220. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cgsize),false);
  221. if isjump then
  222. begin
  223. current_procinfo.CurrTrueLabel:=otl;
  224. current_procinfo.CurrFalseLabel:=ofl;
  225. end
  226. else if right.location.loc=LOC_JUMP then
  227. internalerror(200312292);
  228. cmpop := nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  229. { set result location }
  230. if not cmpop then
  231. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  232. else
  233. location_reset(location,LOC_FLAGS,OS_NO);
  234. load_left_right(cmpop,false);
  235. if (left.location.loc = LOC_CONSTANT) then
  236. swapleftright;
  237. { compare the }
  238. case nodetype of
  239. ltn,lten,gtn,gten,
  240. equaln,unequaln :
  241. begin
  242. if (right.location.loc <> LOC_CONSTANT) then
  243. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,
  244. left.location.register,right.location.register))
  245. else
  246. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPLWI,
  247. left.location.register,longint(right.location.value)));
  248. location.resflags := getresflags;
  249. end;
  250. else
  251. begin
  252. case nodetype of
  253. xorn :
  254. cgop:=OP_XOR;
  255. orn :
  256. cgop:=OP_OR;
  257. andn :
  258. cgop:=OP_AND;
  259. else
  260. internalerror(200203247);
  261. end;
  262. if right.location.loc <> LOC_CONSTANT then
  263. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  264. left.location.register,right.location.register,
  265. location.register)
  266. else
  267. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  268. right.location.value,left.location.register,
  269. location.register);
  270. end;
  271. end;
  272. end
  273. else
  274. inherited second_addboolean;
  275. end;
  276. {*****************************************************************************
  277. AddFloat
  278. *****************************************************************************}
  279. procedure tgenppcaddnode.second_addfloat;
  280. var
  281. op : TAsmOp;
  282. cmpop,
  283. singleprec : boolean;
  284. begin
  285. pass_left_and_right;
  286. cmpop:=false;
  287. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  288. case nodetype of
  289. addn :
  290. if singleprec then
  291. op:=A_FADDS
  292. else
  293. op:=A_FADD;
  294. muln :
  295. if singleprec then
  296. op:=A_FMULS
  297. else
  298. op:=A_FMUL;
  299. subn :
  300. if singleprec then
  301. op:=A_FSUBS
  302. else
  303. op:=A_FSUB;
  304. slashn :
  305. if singleprec then
  306. op:=A_FDIVS
  307. else
  308. op:=A_FDIV;
  309. ltn,lten,gtn,gten,
  310. equaln,unequaln :
  311. begin
  312. op:=A_FCMPO;
  313. cmpop:=true;
  314. end;
  315. else
  316. internalerror(200403182);
  317. end;
  318. // get the operands in the correct order, there are no special cases
  319. // here, everything is register-based
  320. if nf_swapped in flags then
  321. swapleftright;
  322. // put both operands in a register
  323. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  324. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  325. // initialize de result
  326. if not cmpop then
  327. begin
  328. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  329. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  330. end
  331. else
  332. begin
  333. location_reset(location,LOC_FLAGS,OS_NO);
  334. location.resflags := getresflags;
  335. end;
  336. // emit the actual operation
  337. if not cmpop then
  338. begin
  339. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  340. location.register,left.location.register,
  341. right.location.register))
  342. end
  343. else
  344. begin
  345. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  346. newreg(R_SPECIALREGISTER,location.resflags.cr,R_SUBNONE),left.location.register,right.location.register))
  347. end;
  348. end;
  349. {*****************************************************************************
  350. AddSmallSet
  351. *****************************************************************************}
  352. procedure tgenppcaddnode.second_addsmallset;
  353. var
  354. cgop : TOpCg;
  355. setbase: aint;
  356. tmpreg : tregister;
  357. opdone,
  358. cmpop : boolean;
  359. begin
  360. cgop:=OP_None;
  361. pass_left_and_right;
  362. { when a setdef is passed, it has to be a smallset }
  363. if (not(nf_swapped in flags) and
  364. not is_smallset(left.resultdef) or
  365. (not is_smallset(right.resultdef) and
  366. (right.nodetype<>setelementn))) or
  367. ((nf_swapped in flags) and
  368. not is_smallset(right.resultdef) or
  369. (not is_smallset(left.resultdef) and
  370. (left.nodetype<>setelementn))) then
  371. internalerror(200203359);
  372. opdone := false;
  373. cmpop:=nodetype in [equaln,unequaln,lten,gten];
  374. { set result location }
  375. if not cmpop then
  376. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  377. else
  378. location_reset(location,LOC_FLAGS,OS_NO);
  379. load_left_right(cmpop,false);
  380. if not(cmpop) then
  381. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  382. if (left.resultdef.typ=setdef) then
  383. setbase:=tsetdef(left.resultdef).setbase
  384. else
  385. setbase:=tsetdef(right.resultdef).setbase;
  386. if (nf_swapped in flags) and
  387. ((nodetype=subn) or
  388. (left.nodetype=setelementn)) then
  389. swapleftright;
  390. { we don't support two constant locations (should ideally be handled
  391. in simplify }
  392. if (left.location.loc=LOC_CONSTANT) and
  393. (right.location.loc=LOC_CONSTANT) then
  394. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  395. case nodetype of
  396. addn :
  397. begin
  398. { are we adding set elements ? }
  399. if right.nodetype=setelementn then
  400. begin
  401. { no range support for smallsets! }
  402. if assigned(tsetelementnode(right).right) then
  403. internalerror(43244);
  404. if (right.location.loc = LOC_CONSTANT) then
  405. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  406. aint((aword(1) shl (resultdef.size*8-1)) shr aword(right.location.value-setbase)),
  407. left.location.register,location.register)
  408. else
  409. begin
  410. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  411. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,aint((aword(1) shl (resultdef.size*8-1))),tmpreg);
  412. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,right.location,setbase);
  413. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,
  414. right.location.register,tmpreg);
  415. if left.location.loc <> LOC_CONSTANT then
  416. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,tmpreg,
  417. left.location.register,location.register)
  418. else
  419. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  420. left.location.value,tmpreg,location.register);
  421. end;
  422. opdone := true;
  423. end
  424. else
  425. cgop := OP_OR;
  426. end;
  427. symdifn :
  428. cgop:=OP_XOR;
  429. muln :
  430. cgop:=OP_AND;
  431. subn :
  432. begin
  433. cgop:=OP_AND;
  434. if (right.location.loc=LOC_CONSTANT) then
  435. right.location.value := not(right.location.value)
  436. else
  437. opdone := true;
  438. if opdone then
  439. begin
  440. if left.location.loc = LOC_CONSTANT then
  441. begin
  442. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  443. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  444. left.location.value,tmpreg);
  445. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  446. location.register,tmpreg,right.location.register));
  447. end
  448. else
  449. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  450. location.register,left.location.register,
  451. right.location.register));
  452. end;
  453. end;
  454. equaln,
  455. unequaln :
  456. begin
  457. emit_compare(true);
  458. opdone := true;
  459. end;
  460. lten,gten:
  461. begin
  462. If (not(nf_swapped in flags) and
  463. (nodetype = lten)) or
  464. ((nf_swapped in flags) and
  465. (nodetype = gten)) then
  466. swapleftright;
  467. // now we have to check whether left >= right
  468. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  469. if left.location.loc = LOC_CONSTANT then
  470. begin
  471. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,
  472. not(left.location.value),right.location.register,tmpreg);
  473. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPWI,tmpreg,0));
  474. // the two instructions above should be folded together by
  475. // the peepholeoptimizer
  476. end
  477. else
  478. begin
  479. if right.location.loc = LOC_CONSTANT then
  480. begin
  481. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  482. right.location.value,tmpreg);
  483. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  484. tmpreg,left.location.register));
  485. end
  486. else
  487. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  488. right.location.register,left.location.register));
  489. end;
  490. location.resflags.cr := RS_CR0;
  491. location.resflags.flag := F_EQ;
  492. opdone := true;
  493. end;
  494. else
  495. internalerror(2002072701);
  496. end;
  497. if not opdone then
  498. begin
  499. // these are all commutative operations
  500. if (left.location.loc = LOC_CONSTANT) then
  501. swapleftright;
  502. if (right.location.loc = LOC_CONSTANT) then
  503. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  504. right.location.value,left.location.register,
  505. location.register)
  506. else
  507. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  508. right.location.register,left.location.register,
  509. location.register);
  510. end;
  511. end;
  512. end.