nppcadd.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. { the jump the sequence is a little bit hairy }
  160. case nodetype of
  161. ltn,gtn:
  162. begin
  163. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  164. { cheat a little bit for the negative test }
  165. toggleflag(nf_swapped);
  166. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  167. toggleflag(nf_swapped);
  168. end;
  169. lten,gten:
  170. begin
  171. oldnodetype:=nodetype;
  172. if nodetype=lten then
  173. nodetype:=ltn
  174. else
  175. nodetype:=gtn;
  176. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  177. { cheat for the negative test }
  178. if nodetype=ltn then
  179. nodetype:=gtn
  180. else
  181. nodetype:=ltn;
  182. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  183. nodetype:=oldnodetype;
  184. end;
  185. equaln:
  186. begin
  187. nodetype := unequaln;
  188. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  189. nodetype := equaln;
  190. end;
  191. unequaln:
  192. begin
  193. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  194. end;
  195. else
  196. internalerror(2019050947);
  197. end;
  198. end;
  199. procedure secondjmp64bitcmp;
  200. begin
  201. { the jump the sequence is a little bit hairy }
  202. case nodetype of
  203. ltn,gtn,lten,gten:
  204. begin
  205. { the comparison of the low dword always has }
  206. { to be always unsigned! }
  207. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  208. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  209. end;
  210. equaln:
  211. begin
  212. nodetype := unequaln;
  213. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  214. cg.a_jmp_always(current_asmdata.CurrAsmList,truelabel);
  215. nodetype := equaln;
  216. end;
  217. unequaln:
  218. begin
  219. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  220. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  221. end;
  222. else
  223. internalerror(2019050946);
  224. end;
  225. end;
  226. var
  227. tempreg64: tregister64;
  228. begin
  229. truelabel:=nil;
  230. falselabel:=nil;
  231. firstcomplex(self);
  232. pass_left_and_right;
  233. cmpop:=false;
  234. unsigned:=((left.resultdef.typ=orddef) and
  235. (torddef(left.resultdef).ordtype=u64bit)) or
  236. ((right.resultdef.typ=orddef) and
  237. (torddef(right.resultdef).ordtype=u64bit));
  238. case nodetype of
  239. addn :
  240. begin
  241. op:=OP_ADD;
  242. end;
  243. subn :
  244. begin
  245. op:=OP_SUB;
  246. if (nf_swapped in flags) then
  247. swapleftright;
  248. end;
  249. ltn,lten,
  250. gtn,gten,
  251. equaln,unequaln:
  252. begin
  253. op:=OP_NONE;
  254. cmpop:=true;
  255. end;
  256. xorn:
  257. op:=OP_XOR;
  258. orn:
  259. op:=OP_OR;
  260. andn:
  261. op:=OP_AND;
  262. muln:
  263. begin
  264. { should be handled in pass_1 (JM) }
  265. if not(torddef(left.resultdef).ordtype in [U32bit,s32bit]) or
  266. (torddef(left.resultdef).typ <> torddef(right.resultdef).typ) then
  267. internalerror(2001090504);
  268. { handled separately }
  269. op := OP_NONE;
  270. end;
  271. else
  272. internalerror(2002072703);
  273. end;
  274. if not cmpop or
  275. (nodetype in [equaln,unequaln]) then
  276. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  277. else
  278. begin
  279. { we call emit_cmp, which will set location.loc to LOC_FLAGS ->
  280. wait till the end with setting the location }
  281. current_asmdata.getjumplabel(truelabel);
  282. current_asmdata.getjumplabel(falselabel);
  283. end;
  284. load_left_right(cmpop,needoverflowcheck or (nodetype = muln));
  285. if (nodetype<>muln) and
  286. (not needoverflowcheck or
  287. not(nodetype in [addn,subn])) then
  288. begin
  289. case nodetype of
  290. ltn,lten,
  291. gtn,gten:
  292. begin
  293. emit_cmp64_hi;
  294. firstjmp64bitcmp;
  295. emit_cmp64_lo;
  296. secondjmp64bitcmp;
  297. end;
  298. equaln,unequaln:
  299. begin
  300. // instead of doing a complicated compare, do
  301. // (left.hi xor right.hi) or (left.lo xor right.lo)
  302. // (somewhate optimized so that no superfluous 'mr's are
  303. // generated)
  304. if (left.location.loc = LOC_CONSTANT) then
  305. swapleftright;
  306. if (right.location.loc = LOC_CONSTANT) then
  307. begin
  308. if left.location.loc = LOC_REGISTER then
  309. begin
  310. tempreg64.reglo := left.location.register64.reglo;
  311. tempreg64.reghi := left.location.register64.reghi;
  312. end
  313. else
  314. begin
  315. if (aint(right.location.value64) <> 0) then
  316. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  317. else
  318. tempreg64.reglo := left.location.register64.reglo;
  319. if ((right.location.value64 shr 32) <> 0) then
  320. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  321. else
  322. tempreg64.reghi := left.location.register64.reghi;
  323. end;
  324. if (aint(right.location.value64) <> 0) then
  325. { negative values can be handled using SUB, }
  326. { positive values < 65535 using XOR. }
  327. if (longint(right.location.value64) >= -32767) and
  328. (longint(right.location.value64) < 0) then
  329. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  330. aint(right.location.value64),
  331. left.location.register64.reglo,tempreg64.reglo)
  332. else
  333. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  334. aint(right.location.value64),
  335. left.location.register64.reglo,tempreg64.reglo);
  336. if ((right.location.value64 shr 32) <> 0) then
  337. if (longint(right.location.value64 shr 32) >= -32767) and
  338. (longint(right.location.value64 shr 32) < 0) then
  339. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  340. aint(right.location.value64 shr 32),
  341. left.location.register64.reghi,tempreg64.reghi)
  342. else
  343. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  344. aint(right.location.value64 shr 32),
  345. left.location.register64.reghi,tempreg64.reghi);
  346. end
  347. else
  348. begin
  349. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  350. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  351. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,location.size,
  352. left.location.register64,right.location.register64,
  353. tempreg64);
  354. end;
  355. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  356. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,NR_R0,
  357. tempreg64.reglo,tempreg64.reghi));
  358. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  359. location_reset(location,LOC_FLAGS,OS_NO);
  360. location.resflags := getresflags;
  361. end;
  362. xorn,orn,andn,addn:
  363. begin
  364. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  365. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  366. if (left.location.loc = LOC_CONSTANT) then
  367. swapleftright;
  368. if (right.location.loc = LOC_CONSTANT) then
  369. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.value64,
  370. left.location.register64,location.register64)
  371. else
  372. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.register64,
  373. left.location.register64,location.register64);
  374. end;
  375. subn:
  376. begin
  377. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  378. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  379. if left.location.loc <> LOC_CONSTANT then
  380. begin
  381. if right.location.loc <> LOC_CONSTANT then
  382. // reg64 - reg64
  383. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  384. right.location.register64,left.location.register64,
  385. location.register64)
  386. else
  387. // reg64 - const64
  388. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  389. right.location.value64,left.location.register64,
  390. location.register64)
  391. end
  392. else if ((left.location.value64 shr 32) = 0) then
  393. begin
  394. if (int64(left.location.value64) >= low(smallint)) and
  395. (int64(left.location.value64) <= high(smallint)) then
  396. begin
  397. // consts16 - reg64
  398. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  399. location.register64.reglo,right.location.register64.reglo,
  400. left.location.value));
  401. end
  402. else
  403. begin
  404. // const32 - reg64
  405. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  406. left.resultdef,u32inttype,true);
  407. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBC,
  408. location.register64.reglo,left.location.register64.reglo,
  409. right.location.register64.reglo));
  410. end;
  411. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBFZE,
  412. location.register64.reghi,right.location.register64.reghi));
  413. end
  414. else if (aint(left.location.value64) = 0) then
  415. begin
  416. // (const32 shl 32) - reg64
  417. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  418. location.register64.reglo,right.location.register64.reglo,0));
  419. left.location.value64 := left.location.value64 shr 32;
  420. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,u32inttype,true);
  421. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,
  422. location.register64.reghi,right.location.register64.reghi,
  423. left.location.register));
  424. end
  425. else
  426. begin
  427. // const64 - reg64
  428. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  429. left.resultdef,left.resultdef,false);
  430. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  431. right.location.register64,left.location.register64,
  432. location.register64);
  433. end;
  434. end;
  435. else
  436. internalerror(2002072805);
  437. end;
  438. end
  439. else
  440. begin
  441. if is_signed(left.resultdef) and
  442. is_signed(right.resultdef) then
  443. begin
  444. case nodetype of
  445. addn:
  446. begin
  447. op1 := A_ADDC;
  448. op2 := A_ADDEO;
  449. end;
  450. subn:
  451. begin
  452. op1 := A_SUBC;
  453. op2 := A_SUBFEO;
  454. end;
  455. muln:
  456. begin
  457. op1 := A_MULLW;
  458. op2 := A_MULHW
  459. end;
  460. else
  461. internalerror(2002072806);
  462. end
  463. end
  464. else
  465. begin
  466. case nodetype of
  467. addn:
  468. begin
  469. op1 := A_ADDC;
  470. op2 := A_ADDE;
  471. end;
  472. subn:
  473. begin
  474. op1 := A_SUBC;
  475. op2 := A_SUBFE;
  476. end;
  477. muln:
  478. begin
  479. op1 := A_MULLW;
  480. op2 := A_MULHWU
  481. end;
  482. else
  483. internalerror(2014082040);
  484. end;
  485. end;
  486. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op1,location.register64.reglo,
  487. left.location.register64.reglo,right.location.register64.reglo));
  488. if (nodetype <> muln) then
  489. begin
  490. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  491. right.location.register64.reghi,left.location.register64.reghi));
  492. if not(is_signed(resultdef)) then
  493. if nodetype = addn then
  494. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register64.reghi,left.location.register64.reghi))
  495. else
  496. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register64.reghi,location.register64.reghi));
  497. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  498. end
  499. else
  500. begin
  501. { 32 * 32 -> 64 cannot overflow }
  502. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  503. left.location.register64.reglo,right.location.register64.reglo));
  504. end
  505. end;
  506. { set result location }
  507. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  508. { real location only now) (JM) }
  509. if cmpop and
  510. not(nodetype in [equaln,unequaln]) then
  511. location_reset_jump(location,truelabel,falselabel);
  512. end;
  513. {*****************************************************************************
  514. pass_2
  515. *****************************************************************************}
  516. procedure tppcaddnode.pass_generate_code;
  517. { is also being used for xor, and "mul", "sub, or and comparative }
  518. { operators }
  519. var
  520. cgop : topcg;
  521. op : tasmop;
  522. tmpreg : tregister;
  523. hl : tasmlabel;
  524. cmpop : boolean;
  525. { true, if unsigned types are compared }
  526. unsigned : boolean;
  527. checkoverflow : boolean;
  528. begin
  529. { to make it more readable, string and set (not smallset!) have their
  530. own procedures }
  531. case left.resultdef.typ of
  532. orddef :
  533. begin
  534. { handling boolean expressions }
  535. if is_boolean(left.resultdef) and
  536. is_boolean(right.resultdef) then
  537. begin
  538. second_addboolean;
  539. exit;
  540. end
  541. { 64bit operations }
  542. else if is_64bit(resultdef) or
  543. is_64bit(left.resultdef) then
  544. begin
  545. second_add64bit;
  546. exit;
  547. end;
  548. end;
  549. stringdef :
  550. begin
  551. internalerror(2002072403);
  552. exit;
  553. end;
  554. setdef :
  555. begin
  556. { normalsets are already handled in pass1 }
  557. if not is_smallset(left.resultdef) then
  558. internalerror(200109042);
  559. second_addsmallset;
  560. exit;
  561. end;
  562. arraydef :
  563. begin
  564. {$ifdef SUPPORT_MMX}
  565. if is_mmx_able_array(left.resultdef) then
  566. begin
  567. second_addmmx;
  568. exit;
  569. end;
  570. {$endif SUPPORT_MMX}
  571. end;
  572. floatdef :
  573. begin
  574. second_addfloat;
  575. exit;
  576. end;
  577. else
  578. ;
  579. end;
  580. { defaults }
  581. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  582. unsigned:=not(is_signed(left.resultdef)) or
  583. not(is_signed(right.resultdef));
  584. pass_left_and_right;
  585. { Convert flags to register first }
  586. { can any of these things be in the flags actually?? (JM) }
  587. if (left.location.loc = LOC_FLAGS) or
  588. (right.location.loc = LOC_FLAGS) then
  589. internalerror(2002072602);
  590. { set result location }
  591. if not cmpop then
  592. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  593. else
  594. location_reset(location,LOC_FLAGS,OS_NO);
  595. checkoverflow:=
  596. (nodetype in [addn,subn,muln]) and
  597. needoverflowcheck;
  598. load_left_right(cmpop, checkoverflow);
  599. if not(cmpop) then
  600. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  601. if not(checkoverflow) then
  602. begin
  603. case nodetype of
  604. addn, muln, xorn, orn, andn:
  605. begin
  606. case nodetype of
  607. addn:
  608. cgop := OP_ADD;
  609. muln:
  610. if unsigned then
  611. cgop := OP_MUL
  612. else
  613. cgop := OP_IMUL;
  614. xorn:
  615. cgop := OP_XOR;
  616. orn:
  617. cgop := OP_OR;
  618. andn:
  619. cgop := OP_AND;
  620. else
  621. internalerror(2014082041);
  622. end;
  623. if (left.location.loc = LOC_CONSTANT) then
  624. swapleftright;
  625. if (right.location.loc <> LOC_CONSTANT) then
  626. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  627. left.location.register,right.location.register,
  628. location.register)
  629. else
  630. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  631. right.location.value,left.location.register,
  632. location.register);
  633. end;
  634. subn:
  635. begin
  636. if (nf_swapped in flags) then
  637. swapleftright;
  638. if left.location.loc <> LOC_CONSTANT then
  639. if right.location.loc <> LOC_CONSTANT then
  640. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  641. right.location.register,left.location.register,
  642. location.register)
  643. else
  644. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  645. right.location.value,left.location.register,
  646. location.register)
  647. else
  648. if (longint(left.location.value) >= low(smallint)) and
  649. (longint(left.location.value) <= high(smallint)) then
  650. begin
  651. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  652. location.register,right.location.register,
  653. longint(left.location.value)));
  654. end
  655. else
  656. begin
  657. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  658. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  659. left.location.value,tmpreg);
  660. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  661. right.location.register,tmpreg,location.register);
  662. end;
  663. end;
  664. ltn,lten,gtn,gten,equaln,unequaln :
  665. begin
  666. emit_compare(unsigned);
  667. end;
  668. else
  669. internalerror(2019050945);
  670. end;
  671. end
  672. else
  673. // overflow checking is on and we have an addn, subn or muln
  674. begin
  675. if is_signed(resultdef) then
  676. begin
  677. case nodetype of
  678. addn:
  679. op := A_ADDO;
  680. subn:
  681. begin
  682. op := A_SUBO;
  683. if (nf_swapped in flags) then
  684. swapleftright;
  685. end;
  686. muln:
  687. op := A_MULLWO;
  688. else
  689. internalerror(2002072601);
  690. end;
  691. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,
  692. left.location.register,right.location.register));
  693. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  694. end
  695. else
  696. begin
  697. case nodetype of
  698. addn:
  699. begin
  700. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register,
  701. left.location.register,right.location.register));
  702. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register,left.location.register));
  703. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  704. end;
  705. subn:
  706. begin
  707. if nf_swapped in flags then
  708. swapleftright;
  709. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  710. left.location.register,right.location.register));
  711. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register,location.register));
  712. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  713. end;
  714. muln:
  715. begin
  716. { calculate the upper 32 bits of the product, = 0 if no overflow }
  717. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  718. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHWU_,NR_R0,
  719. left.location.register,right.location.register));
  720. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  721. { calculate the real result }
  722. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLW,location.register,
  723. left.location.register,right.location.register));
  724. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  725. current_asmdata.getjumplabel(hl);
  726. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,hl);
  727. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  728. cg.a_label(current_asmdata.CurrAsmList,hl);
  729. end;
  730. else
  731. internalerror(2019050944);
  732. end;
  733. end;
  734. end;
  735. end;
  736. begin
  737. caddnode:=tppcaddnode;
  738. end.