ngppcadd.pas 20 KB

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