nppcadd.pas 31 KB

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