nppcadd.pas 31 KB

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