nppcadd.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the PowerPC
  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 nppcadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,ngppcadd,cpubase;
  22. type
  23. tppcaddnode = class(tgenppcaddnode)
  24. procedure pass_generate_code;override;
  25. protected
  26. function use_generic_mul32to64: boolean; override;
  27. private
  28. procedure emit_compare(unsigned : boolean); override;
  29. {$ifdef SUPPORT_MMX}
  30. procedure second_addmmx;override;
  31. {$endif SUPPORT_MMX}
  32. procedure second_add64bit;override;
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,
  37. cutils,verbose,globals,
  38. symconst,symdef,paramgr,
  39. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  40. cgbase,cpuinfo,pass_1,pass_2,
  41. cpupara,cgcpu,cgutils,procinfo,
  42. ncon,nset,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,hlcgobj,cg64f32;
  44. {*****************************************************************************
  45. Pass 1
  46. *****************************************************************************}
  47. function tppcaddnode.use_generic_mul32to64: boolean;
  48. begin
  49. result := false;
  50. end;
  51. {*****************************************************************************
  52. Helpers
  53. *****************************************************************************}
  54. procedure tppcaddnode.emit_compare(unsigned: boolean);
  55. var
  56. op : tasmop;
  57. tmpreg : tregister;
  58. useconst : boolean;
  59. begin
  60. tmpreg:=NR_NO;
  61. // get the constant on the right if there is one
  62. if (left.location.loc = LOC_CONSTANT) then
  63. swapleftright;
  64. // can we use an immediate, or do we have to load the
  65. // constant in a register first?
  66. if (right.location.loc = LOC_CONSTANT) then
  67. begin
  68. {$ifdef dummy}
  69. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>$ffffffff) or unsigned) then
  70. internalerror(2002080301);
  71. {$endif extdebug}
  72. if (nodetype in [equaln,unequaln]) then
  73. if (unsigned and
  74. (aword(right.location.value) > high(word))) or
  75. (not unsigned and
  76. (aint(right.location.value) < low(smallint)) or
  77. (aint(right.location.value) > high(smallint))) then
  78. { we can then maybe use a constant in the 'othersigned' case
  79. (the sign doesn't matter for // equal/unequal)}
  80. unsigned := not unsigned;
  81. if (unsigned and
  82. (aword(right.location.value) <= high(word))) or
  83. (not(unsigned) and
  84. (aint(right.location.value) >= low(smallint)) and
  85. (aint(right.location.value) <= high(smallint))) then
  86. useconst := true
  87. else
  88. begin
  89. useconst := false;
  90. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  91. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  92. right.location.value,tmpreg);
  93. end
  94. end
  95. else
  96. useconst := false;
  97. location.loc := LOC_FLAGS;
  98. location.resflags := getresflags;
  99. if not unsigned then
  100. if useconst then
  101. op := A_CMPWI
  102. else
  103. op := A_CMPW
  104. else
  105. if useconst then
  106. op := A_CMPLWI
  107. else
  108. op := A_CMPLW;
  109. if (right.location.loc = LOC_CONSTANT) then
  110. begin
  111. if useconst then
  112. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op,left.location.register,longint(right.location.value)))
  113. else
  114. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,left.location.register,tmpreg));
  115. end
  116. else
  117. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  118. left.location.register,right.location.register));
  119. end;
  120. {*****************************************************************************
  121. Add64bit
  122. *****************************************************************************}
  123. procedure tppcaddnode.second_add64bit;
  124. var
  125. truelabel,
  126. falselabel : tasmlabel;
  127. op : TOpCG;
  128. op1,op2 : TAsmOp;
  129. cmpop,
  130. unsigned : boolean;
  131. procedure emit_cmp64_hi;
  132. var
  133. oldleft, oldright: tlocation;
  134. begin
  135. // put the high part of the location in the low part
  136. location_copy(oldleft,left.location);
  137. location_copy(oldright,right.location);
  138. if left.location.loc = LOC_CONSTANT then
  139. left.location.value64 := left.location.value64 shr 32
  140. else
  141. left.location.register64.reglo := left.location.register64.reghi;
  142. if right.location.loc = LOC_CONSTANT then
  143. right.location.value64 := right.location.value64 shr 32
  144. else
  145. right.location.register64.reglo := right.location.register64.reghi;
  146. // and call the normal emit_compare
  147. emit_compare(unsigned);
  148. location_copy(left.location,oldleft);
  149. location_copy(right.location,oldright);
  150. end;
  151. procedure emit_cmp64_lo;
  152. begin
  153. emit_compare(true);
  154. end;
  155. procedure firstjmp64bitcmp;
  156. var
  157. oldnodetype: tnodetype;
  158. begin
  159. {$ifdef OLDREGVARS}
  160. load_all_regvars(current_asmdata.CurrAsmList);
  161. {$endif OLDREGVARS}
  162. { the jump the sequence is a little bit hairy }
  163. case nodetype of
  164. ltn,gtn:
  165. begin
  166. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  167. { cheat a little bit for the negative test }
  168. toggleflag(nf_swapped);
  169. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  170. toggleflag(nf_swapped);
  171. end;
  172. lten,gten:
  173. begin
  174. oldnodetype:=nodetype;
  175. if nodetype=lten then
  176. nodetype:=ltn
  177. else
  178. nodetype:=gtn;
  179. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  180. { cheat for the negative test }
  181. if nodetype=ltn then
  182. nodetype:=gtn
  183. else
  184. nodetype:=ltn;
  185. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  186. nodetype:=oldnodetype;
  187. end;
  188. equaln:
  189. begin
  190. nodetype := unequaln;
  191. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  192. nodetype := equaln;
  193. end;
  194. unequaln:
  195. begin
  196. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  197. end;
  198. else
  199. internalerror(2019050947);
  200. end;
  201. end;
  202. procedure secondjmp64bitcmp;
  203. begin
  204. { the jump the sequence is a little bit hairy }
  205. case nodetype of
  206. ltn,gtn,lten,gten:
  207. begin
  208. { the comparison of the low dword always has }
  209. { to be always unsigned! }
  210. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  211. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  212. end;
  213. equaln:
  214. begin
  215. nodetype := unequaln;
  216. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  217. cg.a_jmp_always(current_asmdata.CurrAsmList,truelabel);
  218. nodetype := equaln;
  219. end;
  220. unequaln:
  221. begin
  222. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  223. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  224. end;
  225. else
  226. internalerror(2019050946);
  227. end;
  228. end;
  229. var
  230. tempreg64: tregister64;
  231. begin
  232. truelabel:=nil;
  233. falselabel:=nil;
  234. firstcomplex(self);
  235. pass_left_and_right;
  236. cmpop:=false;
  237. unsigned:=((left.resultdef.typ=orddef) and
  238. (torddef(left.resultdef).ordtype=u64bit)) or
  239. ((right.resultdef.typ=orddef) and
  240. (torddef(right.resultdef).ordtype=u64bit));
  241. case nodetype of
  242. addn :
  243. begin
  244. op:=OP_ADD;
  245. end;
  246. subn :
  247. begin
  248. op:=OP_SUB;
  249. if (nf_swapped in flags) then
  250. swapleftright;
  251. end;
  252. ltn,lten,
  253. gtn,gten,
  254. equaln,unequaln:
  255. begin
  256. op:=OP_NONE;
  257. cmpop:=true;
  258. end;
  259. xorn:
  260. op:=OP_XOR;
  261. orn:
  262. op:=OP_OR;
  263. andn:
  264. op:=OP_AND;
  265. muln:
  266. begin
  267. { should be handled in pass_1 (JM) }
  268. if not(torddef(left.resultdef).ordtype in [U32bit,s32bit]) or
  269. (torddef(left.resultdef).typ <> torddef(right.resultdef).typ) then
  270. internalerror(200109051);
  271. { handled separately }
  272. op := OP_NONE;
  273. end;
  274. else
  275. internalerror(2002072705);
  276. end;
  277. if not cmpop or
  278. (nodetype in [equaln,unequaln]) then
  279. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  280. else
  281. begin
  282. { we call emit_cmp, which will set location.loc to LOC_FLAGS ->
  283. wait till the end with setting the location }
  284. current_asmdata.getjumplabel(truelabel);
  285. current_asmdata.getjumplabel(falselabel);
  286. end;
  287. load_left_right(cmpop,needoverflowcheck or (nodetype = muln));
  288. if (nodetype<>muln) and
  289. (not needoverflowcheck or
  290. not(nodetype in [addn,subn])) then
  291. begin
  292. case nodetype of
  293. ltn,lten,
  294. gtn,gten:
  295. begin
  296. emit_cmp64_hi;
  297. firstjmp64bitcmp;
  298. emit_cmp64_lo;
  299. secondjmp64bitcmp;
  300. end;
  301. equaln,unequaln:
  302. begin
  303. // instead of doing a complicated compare, do
  304. // (left.hi xor right.hi) or (left.lo xor right.lo)
  305. // (somewhate optimized so that no superfluous 'mr's are
  306. // generated)
  307. if (left.location.loc = LOC_CONSTANT) then
  308. swapleftright;
  309. if (right.location.loc = LOC_CONSTANT) then
  310. begin
  311. if left.location.loc = LOC_REGISTER then
  312. begin
  313. tempreg64.reglo := left.location.register64.reglo;
  314. tempreg64.reghi := left.location.register64.reghi;
  315. end
  316. else
  317. begin
  318. if (aint(right.location.value64) <> 0) then
  319. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  320. else
  321. tempreg64.reglo := left.location.register64.reglo;
  322. if ((right.location.value64 shr 32) <> 0) then
  323. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  324. else
  325. tempreg64.reghi := left.location.register64.reghi;
  326. end;
  327. if (aint(right.location.value64) <> 0) then
  328. { negative values can be handled using SUB, }
  329. { positive values < 65535 using XOR. }
  330. if (longint(right.location.value64) >= -32767) and
  331. (longint(right.location.value64) < 0) then
  332. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  333. aint(right.location.value64),
  334. left.location.register64.reglo,tempreg64.reglo)
  335. else
  336. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  337. aint(right.location.value64),
  338. left.location.register64.reglo,tempreg64.reglo);
  339. if ((right.location.value64 shr 32) <> 0) then
  340. if (longint(right.location.value64 shr 32) >= -32767) and
  341. (longint(right.location.value64 shr 32) < 0) then
  342. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  343. aint(right.location.value64 shr 32),
  344. left.location.register64.reghi,tempreg64.reghi)
  345. else
  346. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  347. aint(right.location.value64 shr 32),
  348. left.location.register64.reghi,tempreg64.reghi);
  349. end
  350. else
  351. begin
  352. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  353. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  354. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,location.size,
  355. left.location.register64,right.location.register64,
  356. tempreg64);
  357. end;
  358. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  359. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,NR_R0,
  360. tempreg64.reglo,tempreg64.reghi));
  361. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  362. location_reset(location,LOC_FLAGS,OS_NO);
  363. location.resflags := getresflags;
  364. end;
  365. xorn,orn,andn,addn:
  366. begin
  367. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  368. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  369. if (left.location.loc = LOC_CONSTANT) then
  370. swapleftright;
  371. if (right.location.loc = LOC_CONSTANT) then
  372. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.value64,
  373. left.location.register64,location.register64)
  374. else
  375. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.register64,
  376. left.location.register64,location.register64);
  377. end;
  378. subn:
  379. begin
  380. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  381. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  382. if left.location.loc <> LOC_CONSTANT then
  383. begin
  384. if right.location.loc <> LOC_CONSTANT then
  385. // reg64 - reg64
  386. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  387. right.location.register64,left.location.register64,
  388. location.register64)
  389. else
  390. // reg64 - const64
  391. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  392. right.location.value64,left.location.register64,
  393. location.register64)
  394. end
  395. else if ((left.location.value64 shr 32) = 0) then
  396. begin
  397. if (int64(left.location.value64) >= low(smallint)) and
  398. (int64(left.location.value64) <= high(smallint)) then
  399. begin
  400. // consts16 - reg64
  401. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  402. location.register64.reglo,right.location.register64.reglo,
  403. left.location.value));
  404. end
  405. else
  406. begin
  407. // const32 - reg64
  408. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  409. left.resultdef,u32inttype,true);
  410. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBC,
  411. location.register64.reglo,left.location.register64.reglo,
  412. right.location.register64.reglo));
  413. end;
  414. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBFZE,
  415. location.register64.reghi,right.location.register64.reghi));
  416. end
  417. else if (aint(left.location.value64) = 0) then
  418. begin
  419. // (const32 shl 32) - reg64
  420. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  421. location.register64.reglo,right.location.register64.reglo,0));
  422. left.location.value64 := left.location.value64 shr 32;
  423. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  424. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,
  425. location.register64.reghi,right.location.register64.reghi,
  426. left.location.register));
  427. end
  428. else
  429. begin
  430. // const64 - reg64
  431. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  432. left.resultdef,left.resultdef,false);
  433. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  434. right.location.register64,left.location.register64,
  435. location.register64);
  436. end;
  437. end;
  438. else
  439. internalerror(2002072803);
  440. end;
  441. end
  442. else
  443. begin
  444. if is_signed(left.resultdef) and
  445. is_signed(right.resultdef) then
  446. begin
  447. case nodetype of
  448. addn:
  449. begin
  450. op1 := A_ADDC;
  451. op2 := A_ADDEO;
  452. end;
  453. subn:
  454. begin
  455. op1 := A_SUBC;
  456. op2 := A_SUBFEO;
  457. end;
  458. muln:
  459. begin
  460. op1 := A_MULLW;
  461. op2 := A_MULHW
  462. end;
  463. else
  464. internalerror(2002072806);
  465. end
  466. end
  467. else
  468. begin
  469. case nodetype of
  470. addn:
  471. begin
  472. op1 := A_ADDC;
  473. op2 := A_ADDE;
  474. end;
  475. subn:
  476. begin
  477. op1 := A_SUBC;
  478. op2 := A_SUBFE;
  479. end;
  480. muln:
  481. begin
  482. op1 := A_MULLW;
  483. op2 := A_MULHWU
  484. end;
  485. else
  486. internalerror(2014082040);
  487. end;
  488. end;
  489. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op1,location.register64.reglo,
  490. left.location.register64.reglo,right.location.register64.reglo));
  491. if (nodetype <> muln) then
  492. begin
  493. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  494. right.location.register64.reghi,left.location.register64.reghi));
  495. if not(is_signed(resultdef)) then
  496. if nodetype = addn then
  497. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register64.reghi,left.location.register64.reghi))
  498. else
  499. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register64.reghi,location.register64.reghi));
  500. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  501. end
  502. else
  503. begin
  504. { 32 * 32 -> 64 cannot overflow }
  505. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  506. left.location.register64.reglo,right.location.register64.reglo));
  507. end
  508. end;
  509. { set result location }
  510. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  511. { real location only now) (JM) }
  512. if cmpop and
  513. not(nodetype in [equaln,unequaln]) then
  514. location_reset_jump(location,truelabel,falselabel);
  515. end;
  516. {*****************************************************************************
  517. pass_2
  518. *****************************************************************************}
  519. procedure tppcaddnode.pass_generate_code;
  520. { is also being used for xor, and "mul", "sub, or and comparative }
  521. { operators }
  522. var
  523. cgop : topcg;
  524. op : tasmop;
  525. tmpreg : tregister;
  526. hl : tasmlabel;
  527. cmpop : boolean;
  528. { true, if unsigned types are compared }
  529. unsigned : boolean;
  530. checkoverflow : boolean;
  531. begin
  532. { to make it more readable, string and set (not smallset!) have their
  533. own procedures }
  534. case left.resultdef.typ of
  535. orddef :
  536. begin
  537. { handling boolean expressions }
  538. if is_boolean(left.resultdef) and
  539. is_boolean(right.resultdef) then
  540. begin
  541. second_addboolean;
  542. exit;
  543. end
  544. { 64bit operations }
  545. else if is_64bit(resultdef) or
  546. is_64bit(left.resultdef) then
  547. begin
  548. second_add64bit;
  549. exit;
  550. end;
  551. end;
  552. stringdef :
  553. begin
  554. internalerror(2002072402);
  555. exit;
  556. end;
  557. setdef :
  558. begin
  559. { normalsets are already handled in pass1 }
  560. if not is_smallset(left.resultdef) then
  561. internalerror(200109042);
  562. second_addsmallset;
  563. exit;
  564. end;
  565. arraydef :
  566. begin
  567. {$ifdef SUPPORT_MMX}
  568. if is_mmx_able_array(left.resultdef) then
  569. begin
  570. second_addmmx;
  571. exit;
  572. end;
  573. {$endif SUPPORT_MMX}
  574. end;
  575. floatdef :
  576. begin
  577. second_addfloat;
  578. exit;
  579. end;
  580. else
  581. ;
  582. end;
  583. { defaults }
  584. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  585. unsigned:=not(is_signed(left.resultdef)) or
  586. not(is_signed(right.resultdef));
  587. pass_left_and_right;
  588. { Convert flags to register first }
  589. { can any of these things be in the flags actually?? (JM) }
  590. if (left.location.loc = LOC_FLAGS) or
  591. (right.location.loc = LOC_FLAGS) then
  592. internalerror(2002072602);
  593. { set result location }
  594. if not cmpop then
  595. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  596. else
  597. location_reset(location,LOC_FLAGS,OS_NO);
  598. checkoverflow:=
  599. (nodetype in [addn,subn,muln]) and
  600. needoverflowcheck;
  601. load_left_right(cmpop, checkoverflow);
  602. if not(cmpop) then
  603. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  604. if not(checkoverflow) then
  605. begin
  606. case nodetype of
  607. addn, muln, xorn, orn, andn:
  608. begin
  609. case nodetype of
  610. addn:
  611. cgop := OP_ADD;
  612. muln:
  613. if unsigned then
  614. cgop := OP_MUL
  615. else
  616. cgop := OP_IMUL;
  617. xorn:
  618. cgop := OP_XOR;
  619. orn:
  620. cgop := OP_OR;
  621. andn:
  622. cgop := OP_AND;
  623. else
  624. internalerror(2014082041);
  625. end;
  626. if (left.location.loc = LOC_CONSTANT) then
  627. swapleftright;
  628. if (right.location.loc <> LOC_CONSTANT) then
  629. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  630. left.location.register,right.location.register,
  631. location.register)
  632. else
  633. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  634. right.location.value,left.location.register,
  635. location.register);
  636. end;
  637. subn:
  638. begin
  639. if (nf_swapped in flags) then
  640. swapleftright;
  641. if left.location.loc <> LOC_CONSTANT then
  642. if right.location.loc <> LOC_CONSTANT then
  643. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  644. right.location.register,left.location.register,
  645. location.register)
  646. else
  647. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  648. right.location.value,left.location.register,
  649. location.register)
  650. else
  651. if (longint(left.location.value) >= low(smallint)) and
  652. (longint(left.location.value) <= high(smallint)) then
  653. begin
  654. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  655. location.register,right.location.register,
  656. longint(left.location.value)));
  657. end
  658. else
  659. begin
  660. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  661. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  662. left.location.value,tmpreg);
  663. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  664. right.location.register,tmpreg,location.register);
  665. end;
  666. end;
  667. ltn,lten,gtn,gten,equaln,unequaln :
  668. begin
  669. emit_compare(unsigned);
  670. end;
  671. else
  672. internalerror(2019050945);
  673. end;
  674. end
  675. else
  676. // overflow checking is on and we have an addn, subn or muln
  677. begin
  678. if is_signed(resultdef) then
  679. begin
  680. case nodetype of
  681. addn:
  682. op := A_ADDO;
  683. subn:
  684. begin
  685. op := A_SUBO;
  686. if (nf_swapped in flags) then
  687. swapleftright;
  688. end;
  689. muln:
  690. op := A_MULLWO;
  691. else
  692. internalerror(2002072601);
  693. end;
  694. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,
  695. left.location.register,right.location.register));
  696. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  697. end
  698. else
  699. begin
  700. case nodetype of
  701. addn:
  702. begin
  703. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register,
  704. left.location.register,right.location.register));
  705. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register,left.location.register));
  706. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  707. end;
  708. subn:
  709. begin
  710. if nf_swapped in flags then
  711. swapleftright;
  712. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  713. left.location.register,right.location.register));
  714. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register,location.register));
  715. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  716. end;
  717. muln:
  718. begin
  719. { calculate the upper 32 bits of the product, = 0 if no overflow }
  720. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  721. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHWU_,NR_R0,
  722. left.location.register,right.location.register));
  723. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  724. { calculate the real result }
  725. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLW,location.register,
  726. left.location.register,right.location.register));
  727. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  728. current_asmdata.getjumplabel(hl);
  729. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,hl);
  730. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  731. cg.a_label(current_asmdata.CurrAsmList,hl);
  732. end;
  733. else
  734. internalerror(2019050944);
  735. end;
  736. end;
  737. end;
  738. end;
  739. begin
  740. caddnode:=tppcaddnode;
  741. end.