ngppcadd.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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,
  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 : boolean;
  166. begin
  167. { calculate the operator which is more difficult }
  168. firstcomplex(self);
  169. cmpop:=false;
  170. if (torddef(left.resultdef).ordtype in [pasbool1,pasbool8,bool8bit]) or
  171. (torddef(right.resultdef).ordtype in [pasbool1,pasbool8,bool8bit]) then
  172. cgsize:=OS_8
  173. else if (torddef(left.resultdef).ordtype in [pasbool16,bool16bit]) or
  174. (torddef(right.resultdef).ordtype in [pasbool16,bool16bit]) then
  175. cgsize:=OS_16
  176. else if (torddef(left.resultdef).ordtype in [pasbool32,bool32bit]) or
  177. (torddef(right.resultdef).ordtype in [pasbool32,bool32bit]) then
  178. cgsize:=OS_32
  179. else
  180. cgsize:=OS_64;
  181. if {$ifndef cpu64bitalu}(cgsize<>OS_64) and{$endif}
  182. (((cs_full_boolean_eval in current_settings.localswitches) and
  183. not(nf_short_bool in flags)) or
  184. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn])) then
  185. begin
  186. if left.nodetype in [ordconstn,realconstn] then
  187. swapleftright;
  188. secondpass(left);
  189. if (left.expectloc=LOC_JUMP)<>
  190. (left.location.loc=LOC_JUMP) then
  191. internalerror(2003122901);
  192. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  193. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(cgsize),false);
  194. secondpass(right);
  195. if (right.expectloc=LOC_JUMP)<>
  196. (right.location.loc=LOC_JUMP) then
  197. internalerror(200312292);
  198. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  199. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cgsize),false);
  200. cmpop := nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  201. { set result location }
  202. if not cmpop then
  203. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  204. else
  205. location_reset(location,LOC_FLAGS,OS_NO);
  206. load_left_right(cmpop,false);
  207. if (left.location.loc = LOC_CONSTANT) then
  208. swapleftright;
  209. { compare the }
  210. case nodetype of
  211. ltn,lten,gtn,gten,
  212. equaln,unequaln :
  213. begin
  214. if (right.location.loc <> LOC_CONSTANT) then
  215. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,
  216. left.location.register,right.location.register))
  217. else
  218. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPLWI,
  219. left.location.register,longint(right.location.value)));
  220. location.resflags := getresflags;
  221. end;
  222. else
  223. begin
  224. case nodetype of
  225. xorn :
  226. cgop:=OP_XOR;
  227. orn :
  228. cgop:=OP_OR;
  229. andn :
  230. cgop:=OP_AND;
  231. else
  232. internalerror(200203247);
  233. end;
  234. if right.location.loc <> LOC_CONSTANT then
  235. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  236. left.location.register,right.location.register,
  237. location.register)
  238. else
  239. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  240. right.location.value,left.location.register,
  241. location.register);
  242. end;
  243. end;
  244. end
  245. else
  246. inherited second_addboolean;
  247. end;
  248. {*****************************************************************************
  249. AddFloat
  250. *****************************************************************************}
  251. procedure tgenppcaddnode.second_addfloat;
  252. var
  253. op : TAsmOp;
  254. cmpop,
  255. singleprec : boolean;
  256. begin
  257. pass_left_and_right;
  258. cmpop:=false;
  259. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  260. case nodetype of
  261. addn :
  262. if singleprec then
  263. op:=A_FADDS
  264. else
  265. op:=A_FADD;
  266. muln :
  267. if singleprec then
  268. op:=A_FMULS
  269. else
  270. op:=A_FMUL;
  271. subn :
  272. if singleprec then
  273. op:=A_FSUBS
  274. else
  275. op:=A_FSUB;
  276. slashn :
  277. if singleprec then
  278. op:=A_FDIVS
  279. else
  280. op:=A_FDIV;
  281. ltn,lten,gtn,gten,
  282. equaln,unequaln :
  283. begin
  284. { clang does not recognize fcmpo instruction,
  285. so we need to fall back to fcmpu, which does not
  286. generate the same exeception information }
  287. if target_asm.id = as_clang then
  288. op:=A_FCMPU
  289. else
  290. op:=A_FCMPO;
  291. cmpop:=true;
  292. end;
  293. else
  294. internalerror(200403182);
  295. end;
  296. // get the operands in the correct order, there are no special cases
  297. // here, everything is register-based
  298. if nf_swapped in flags then
  299. swapleftright;
  300. // put both operands in a register
  301. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  302. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  303. // initialize de result
  304. if not cmpop then
  305. begin
  306. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  307. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  308. end
  309. else
  310. begin
  311. location_reset(location,LOC_FLAGS,OS_NO);
  312. location.resflags := getresflags;
  313. end;
  314. // emit the actual operation
  315. if not cmpop then
  316. begin
  317. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  318. location.register,left.location.register,
  319. right.location.register))
  320. end
  321. else
  322. begin
  323. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  324. newreg(R_SPECIALREGISTER,location.resflags.cr,R_SUBNONE),left.location.register,right.location.register))
  325. end;
  326. end;
  327. {*****************************************************************************
  328. AddSmallSet
  329. *****************************************************************************}
  330. procedure tgenppcaddnode.second_addsmallset;
  331. var
  332. cgop : TOpCg;
  333. setbase: aint;
  334. tmpreg : tregister;
  335. opdone,
  336. cmpop : boolean;
  337. begin
  338. if target_info.endian=endian_little then
  339. begin
  340. { this code currently assumes big endian }
  341. if (left.nodetype=setelementn) or (right.nodetype=setelementn) then
  342. begin
  343. inherited second_addsmallsetelement;
  344. exit;
  345. end
  346. end;
  347. cgop:=OP_None;
  348. pass_left_and_right;
  349. { when a setdef is passed, it has to be a smallset }
  350. if (not(nf_swapped in flags) and
  351. not is_smallset(left.resultdef) or
  352. (not is_smallset(right.resultdef) and
  353. (right.nodetype<>setelementn))) or
  354. ((nf_swapped in flags) and
  355. not is_smallset(right.resultdef) or
  356. (not is_smallset(left.resultdef) and
  357. (left.nodetype<>setelementn))) then
  358. internalerror(200203359);
  359. opdone := false;
  360. cmpop:=nodetype in [equaln,unequaln,lten,gten];
  361. { set result location }
  362. if not cmpop then
  363. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  364. else
  365. location_reset(location,LOC_FLAGS,OS_NO);
  366. load_left_right(cmpop,false);
  367. if not(cmpop) then
  368. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  369. if (left.resultdef.typ=setdef) then
  370. setbase:=tsetdef(left.resultdef).setbase
  371. else
  372. setbase:=tsetdef(right.resultdef).setbase;
  373. if (nf_swapped in flags) and
  374. ((nodetype=subn) or
  375. (left.nodetype=setelementn)) then
  376. swapleftright;
  377. { we don't support two constant locations (should ideally be handled
  378. in simplify }
  379. if (left.location.loc=LOC_CONSTANT) and
  380. (right.location.loc=LOC_CONSTANT) then
  381. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  382. case nodetype of
  383. addn :
  384. begin
  385. { are we adding set elements ? }
  386. if right.nodetype=setelementn then
  387. begin
  388. { no range support for smallsets! }
  389. if assigned(tsetelementnode(right).right) then
  390. internalerror(43244);
  391. if (right.location.loc = LOC_CONSTANT) then
  392. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  393. aint((aword(1) shl (resultdef.size*8-1)) shr aword(right.location.value-setbase)),
  394. left.location.register,location.register)
  395. else
  396. begin
  397. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  398. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  399. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,aint((aword(1) shl (resultdef.size*8-1))),tmpreg);
  400. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,u32inttype,right.location,setbase);
  401. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,
  402. right.location.register,tmpreg);
  403. if left.location.loc <> LOC_CONSTANT then
  404. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,tmpreg,
  405. left.location.register,location.register)
  406. else
  407. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  408. left.location.value,tmpreg,location.register);
  409. end;
  410. opdone := true;
  411. end
  412. else
  413. cgop := OP_OR;
  414. end;
  415. symdifn :
  416. cgop:=OP_XOR;
  417. muln :
  418. cgop:=OP_AND;
  419. subn :
  420. begin
  421. cgop:=OP_AND;
  422. if (right.location.loc=LOC_CONSTANT) then
  423. right.location.value := not(right.location.value)
  424. else
  425. opdone := true;
  426. if opdone then
  427. begin
  428. if left.location.loc = LOC_CONSTANT then
  429. begin
  430. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  431. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  432. left.location.value,tmpreg);
  433. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  434. location.register,tmpreg,right.location.register));
  435. end
  436. else
  437. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  438. location.register,left.location.register,
  439. right.location.register));
  440. end;
  441. end;
  442. equaln,
  443. unequaln :
  444. begin
  445. emit_compare(true);
  446. opdone := true;
  447. end;
  448. lten,gten:
  449. begin
  450. If (not(nf_swapped in flags) and
  451. (nodetype = lten)) or
  452. ((nf_swapped in flags) and
  453. (nodetype = gten)) then
  454. swapleftright;
  455. // now we have to check whether left >= right
  456. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  457. if left.location.loc = LOC_CONSTANT then
  458. begin
  459. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,
  460. not(left.location.value),right.location.register,tmpreg);
  461. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPWI,tmpreg,0));
  462. // the two instructions above should be folded together by
  463. // the peepholeoptimizer
  464. end
  465. else
  466. begin
  467. if right.location.loc = LOC_CONSTANT then
  468. begin
  469. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  470. right.location.value,tmpreg);
  471. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  472. tmpreg,left.location.register));
  473. end
  474. else
  475. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  476. right.location.register,left.location.register));
  477. end;
  478. location.resflags.cr := RS_CR0;
  479. location.resflags.flag := F_EQ;
  480. opdone := true;
  481. end;
  482. else
  483. internalerror(2002072701);
  484. end;
  485. if not opdone then
  486. begin
  487. // these are all commutative operations
  488. if (left.location.loc = LOC_CONSTANT) then
  489. swapleftright;
  490. if (right.location.loc = LOC_CONSTANT) then
  491. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  492. right.location.value,left.location.register,
  493. location.register)
  494. else
  495. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  496. right.location.register,left.location.register,
  497. location.register);
  498. end;
  499. end;
  500. end.