nppcadd.pas 32 KB

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