nppcadd.pas 32 KB

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