nppcadd.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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,regvars,
  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. end;
  199. end;
  200. procedure secondjmp64bitcmp;
  201. begin
  202. { the jump the sequence is a little bit hairy }
  203. case nodetype of
  204. ltn,gtn,lten,gten:
  205. begin
  206. { the comparison of the low dword always has }
  207. { to be always unsigned! }
  208. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  209. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  210. end;
  211. equaln:
  212. begin
  213. nodetype := unequaln;
  214. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,falselabel);
  215. cg.a_jmp_always(current_asmdata.CurrAsmList,truelabel);
  216. nodetype := equaln;
  217. end;
  218. unequaln:
  219. begin
  220. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,truelabel);
  221. cg.a_jmp_always(current_asmdata.CurrAsmList,falselabel);
  222. end;
  223. end;
  224. end;
  225. var
  226. tempreg64: tregister64;
  227. begin
  228. truelabel:=nil;
  229. falselabel:=nil;
  230. firstcomplex(self);
  231. pass_left_and_right;
  232. cmpop:=false;
  233. unsigned:=((left.resultdef.typ=orddef) and
  234. (torddef(left.resultdef).ordtype=u64bit)) or
  235. ((right.resultdef.typ=orddef) and
  236. (torddef(right.resultdef).ordtype=u64bit));
  237. case nodetype of
  238. addn :
  239. begin
  240. op:=OP_ADD;
  241. end;
  242. subn :
  243. begin
  244. op:=OP_SUB;
  245. if (nf_swapped in flags) then
  246. swapleftright;
  247. end;
  248. ltn,lten,
  249. gtn,gten,
  250. equaln,unequaln:
  251. begin
  252. op:=OP_NONE;
  253. cmpop:=true;
  254. end;
  255. xorn:
  256. op:=OP_XOR;
  257. orn:
  258. op:=OP_OR;
  259. andn:
  260. op:=OP_AND;
  261. muln:
  262. begin
  263. { should be handled in pass_1 (JM) }
  264. if not(torddef(left.resultdef).ordtype in [U32bit,s32bit]) or
  265. (torddef(left.resultdef).typ <> torddef(right.resultdef).typ) then
  266. internalerror(200109051);
  267. { handled separately }
  268. op := OP_NONE;
  269. end;
  270. else
  271. internalerror(2002072705);
  272. end;
  273. if not cmpop or
  274. (nodetype in [equaln,unequaln]) then
  275. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  276. else
  277. begin
  278. { we call emit_cmp, which will set location.loc to LOC_FLAGS ->
  279. wait till the end with setting the location }
  280. current_asmdata.getjumplabel(truelabel);
  281. current_asmdata.getjumplabel(falselabel);
  282. end;
  283. load_left_right(cmpop,((cs_check_overflow in current_settings.localswitches) and
  284. (nodetype in [addn,subn])) or (nodetype = muln));
  285. if (nodetype <> muln) and
  286. (not(cs_check_overflow in current_settings.localswitches) 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(2002072803);
  437. end;
  438. end
  439. else
  440. begin
  441. if is_signed(resultdef) then
  442. begin
  443. case nodetype of
  444. addn:
  445. begin
  446. op1 := A_ADDC;
  447. op2 := A_ADDEO;
  448. end;
  449. subn:
  450. begin
  451. op1 := A_SUBC;
  452. op2 := A_SUBFEO;
  453. end;
  454. muln:
  455. begin
  456. op1 := A_MULLW;
  457. op2 := A_MULHW
  458. end;
  459. else
  460. internalerror(2002072806);
  461. end
  462. end
  463. else
  464. begin
  465. case nodetype of
  466. addn:
  467. begin
  468. op1 := A_ADDC;
  469. op2 := A_ADDE;
  470. end;
  471. subn:
  472. begin
  473. op1 := A_SUBC;
  474. op2 := A_SUBFE;
  475. end;
  476. muln:
  477. begin
  478. op1 := A_MULLW;
  479. op2 := A_MULHWU
  480. end;
  481. else
  482. internalerror(2014082040);
  483. end;
  484. end;
  485. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op1,location.register64.reglo,
  486. left.location.register64.reglo,right.location.register64.reglo));
  487. if (nodetype <> muln) then
  488. begin
  489. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  490. right.location.register64.reghi,left.location.register64.reghi));
  491. if not(is_signed(resultdef)) then
  492. if nodetype = addn then
  493. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register64.reghi,left.location.register64.reghi))
  494. else
  495. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register64.reghi,location.register64.reghi));
  496. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  497. end
  498. else
  499. begin
  500. { 32 * 32 -> 64 cannot overflow }
  501. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  502. left.location.register64.reglo,right.location.register64.reglo));
  503. end
  504. end;
  505. { set result location }
  506. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  507. { real location only now) (JM) }
  508. if cmpop and
  509. not(nodetype in [equaln,unequaln]) then
  510. location_reset_jump(location,truelabel,falselabel);
  511. end;
  512. {*****************************************************************************
  513. pass_2
  514. *****************************************************************************}
  515. procedure tppcaddnode.pass_generate_code;
  516. { is also being used for xor, and "mul", "sub, or and comparative }
  517. { operators }
  518. var
  519. cgop : topcg;
  520. op : tasmop;
  521. tmpreg : tregister;
  522. hl : tasmlabel;
  523. cmpop : boolean;
  524. { true, if unsigned types are compared }
  525. unsigned : boolean;
  526. checkoverflow : boolean;
  527. begin
  528. { to make it more readable, string and set (not smallset!) have their
  529. own procedures }
  530. case left.resultdef.typ of
  531. orddef :
  532. begin
  533. { handling boolean expressions }
  534. if is_boolean(left.resultdef) and
  535. is_boolean(right.resultdef) then
  536. begin
  537. second_addboolean;
  538. exit;
  539. end
  540. { 64bit operations }
  541. else if is_64bit(resultdef) or
  542. is_64bit(left.resultdef) then
  543. begin
  544. second_add64bit;
  545. exit;
  546. end;
  547. end;
  548. stringdef :
  549. begin
  550. internalerror(2002072402);
  551. exit;
  552. end;
  553. setdef :
  554. begin
  555. { normalsets are already handled in pass1 }
  556. if not is_smallset(left.resultdef) then
  557. internalerror(200109042);
  558. second_addsmallset;
  559. exit;
  560. end;
  561. arraydef :
  562. begin
  563. {$ifdef SUPPORT_MMX}
  564. if is_mmx_able_array(left.resultdef) then
  565. begin
  566. second_addmmx;
  567. exit;
  568. end;
  569. {$endif SUPPORT_MMX}
  570. end;
  571. floatdef :
  572. begin
  573. second_addfloat;
  574. exit;
  575. end;
  576. end;
  577. { defaults }
  578. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  579. unsigned:=not(is_signed(left.resultdef)) or
  580. not(is_signed(right.resultdef));
  581. pass_left_and_right;
  582. { Convert flags to register first }
  583. { can any of these things be in the flags actually?? (JM) }
  584. if (left.location.loc = LOC_FLAGS) or
  585. (right.location.loc = LOC_FLAGS) then
  586. internalerror(2002072602);
  587. { set result location }
  588. if not cmpop then
  589. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  590. else
  591. location_reset(location,LOC_FLAGS,OS_NO);
  592. checkoverflow:=
  593. (nodetype in [addn,subn,muln]) and
  594. (cs_check_overflow in current_settings.localswitches) and
  595. (left.resultdef.typ<>pointerdef) and
  596. (right.resultdef.typ<>pointerdef);
  597. load_left_right(cmpop, checkoverflow);
  598. if not(cmpop) then
  599. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  600. if not(checkoverflow) then
  601. begin
  602. case nodetype of
  603. addn, muln, xorn, orn, andn:
  604. begin
  605. case nodetype of
  606. addn:
  607. cgop := OP_ADD;
  608. muln:
  609. if unsigned then
  610. cgop := OP_MUL
  611. else
  612. cgop := OP_IMUL;
  613. xorn:
  614. cgop := OP_XOR;
  615. orn:
  616. cgop := OP_OR;
  617. andn:
  618. cgop := OP_AND;
  619. else
  620. internalerror(2014082041);
  621. end;
  622. if (left.location.loc = LOC_CONSTANT) then
  623. swapleftright;
  624. if (right.location.loc <> LOC_CONSTANT) then
  625. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  626. left.location.register,right.location.register,
  627. location.register)
  628. else
  629. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  630. right.location.value,left.location.register,
  631. location.register);
  632. end;
  633. subn:
  634. begin
  635. if (nf_swapped in flags) then
  636. swapleftright;
  637. if left.location.loc <> LOC_CONSTANT then
  638. if right.location.loc <> LOC_CONSTANT then
  639. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  640. right.location.register,left.location.register,
  641. location.register)
  642. else
  643. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  644. right.location.value,left.location.register,
  645. location.register)
  646. else
  647. if (longint(left.location.value) >= low(smallint)) and
  648. (longint(left.location.value) <= high(smallint)) then
  649. begin
  650. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  651. location.register,right.location.register,
  652. longint(left.location.value)));
  653. end
  654. else
  655. begin
  656. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  657. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  658. left.location.value,tmpreg);
  659. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  660. right.location.register,tmpreg,location.register);
  661. end;
  662. end;
  663. ltn,lten,gtn,gten,equaln,unequaln :
  664. begin
  665. emit_compare(unsigned);
  666. end;
  667. end;
  668. end
  669. else
  670. // overflow checking is on and we have an addn, subn or muln
  671. begin
  672. if is_signed(resultdef) then
  673. begin
  674. case nodetype of
  675. addn:
  676. op := A_ADDO;
  677. subn:
  678. begin
  679. op := A_SUBO;
  680. if (nf_swapped in flags) then
  681. swapleftright;
  682. end;
  683. muln:
  684. op := A_MULLWO;
  685. else
  686. internalerror(2002072601);
  687. end;
  688. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,
  689. left.location.register,right.location.register));
  690. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  691. end
  692. else
  693. begin
  694. case nodetype of
  695. addn:
  696. begin
  697. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register,
  698. left.location.register,right.location.register));
  699. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register,left.location.register));
  700. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  701. end;
  702. subn:
  703. begin
  704. if nf_swapped in flags then
  705. swapleftright;
  706. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  707. left.location.register,right.location.register));
  708. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register,location.register));
  709. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  710. end;
  711. muln:
  712. begin
  713. { calculate the upper 32 bits of the product, = 0 if no overflow }
  714. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  715. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHWU_,NR_R0,
  716. left.location.register,right.location.register));
  717. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  718. { calculate the real result }
  719. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLW,location.register,
  720. left.location.register,right.location.register));
  721. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  722. current_asmdata.getjumplabel(hl);
  723. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,hl);
  724. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  725. cg.a_label(current_asmdata.CurrAsmList,hl);
  726. end;
  727. end;
  728. end;
  729. end;
  730. end;
  731. begin
  732. caddnode:=tppcaddnode;
  733. end.