nppcadd.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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,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. location_force_reg(current_asmdata.CurrAsmList,left.location,
  393. OS_32,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. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,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. location_force_reg(current_asmdata.CurrAsmList,left.location,
  416. def_cgsize(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. AddMMX
  499. *****************************************************************************}
  500. {$ifdef SUPPORT_MMX}
  501. procedure ti386addnode.second_addmmx;
  502. var
  503. op : TAsmOp;
  504. cmpop : boolean;
  505. mmxbase : tmmxtype;
  506. hregister : tregister;
  507. begin
  508. pass_left_and_right;
  509. cmpop:=false;
  510. mmxbase:=mmx_type(left.resultdef);
  511. case nodetype of
  512. addn :
  513. begin
  514. if (cs_mmx_saturation in current_settings.localswitches) then
  515. begin
  516. case mmxbase of
  517. mmxs8bit:
  518. op:=A_PADDSB;
  519. mmxu8bit:
  520. op:=A_PADDUSB;
  521. mmxs16bit,mmxfixed16:
  522. op:=A_PADDSB;
  523. mmxu16bit:
  524. op:=A_PADDUSW;
  525. end;
  526. end
  527. else
  528. begin
  529. case mmxbase of
  530. mmxs8bit,mmxu8bit:
  531. op:=A_PADDB;
  532. mmxs16bit,mmxu16bit,mmxfixed16:
  533. op:=A_PADDW;
  534. mmxs32bit,mmxu32bit:
  535. op:=A_PADDD;
  536. end;
  537. end;
  538. end;
  539. muln :
  540. begin
  541. case mmxbase of
  542. mmxs16bit,mmxu16bit:
  543. op:=A_PMULLW;
  544. mmxfixed16:
  545. op:=A_PMULHW;
  546. end;
  547. end;
  548. subn :
  549. begin
  550. if (cs_mmx_saturation in current_settings.localswitches) then
  551. begin
  552. case mmxbase of
  553. mmxs8bit:
  554. op:=A_PSUBSB;
  555. mmxu8bit:
  556. op:=A_PSUBUSB;
  557. mmxs16bit,mmxfixed16:
  558. op:=A_PSUBSB;
  559. mmxu16bit:
  560. op:=A_PSUBUSW;
  561. end;
  562. end
  563. else
  564. begin
  565. case mmxbase of
  566. mmxs8bit,mmxu8bit:
  567. op:=A_PSUBB;
  568. mmxs16bit,mmxu16bit,mmxfixed16:
  569. op:=A_PSUBW;
  570. mmxs32bit,mmxu32bit:
  571. op:=A_PSUBD;
  572. end;
  573. end;
  574. end;
  575. xorn:
  576. op:=A_PXOR;
  577. orn:
  578. op:=A_POR;
  579. andn:
  580. op:=A_PAND;
  581. else
  582. internalerror(200403183);
  583. end;
  584. { left and right no register? }
  585. { then one must be demanded }
  586. if (left.location.loc<>LOC_MMXREGISTER) then
  587. begin
  588. if (right.location.loc=LOC_MMXREGISTER) then
  589. begin
  590. location_swap(left.location,right.location);
  591. toggleflag(nf_swapped);
  592. end
  593. else
  594. begin
  595. { register variable ? }
  596. if (left.location.loc=LOC_CMMXREGISTER) then
  597. begin
  598. hregister:=rg.getregistermm(current_asmdata.CurrAsmList);
  599. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  600. end
  601. else
  602. begin
  603. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  604. internalerror(200203245);
  605. location_release(current_asmdata.CurrAsmList,left.location);
  606. hregister:=rg.getregistermm(current_asmdata.CurrAsmList);
  607. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  608. end;
  609. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  610. left.location.register:=hregister;
  611. end;
  612. end;
  613. { at this point, left.location.loc should be LOC_MMXREGISTER }
  614. if right.location.loc<>LOC_MMXREGISTER then
  615. begin
  616. if (nodetype=subn) and (nf_swapped in flags) then
  617. begin
  618. if right.location.loc=LOC_CMMXREGISTER then
  619. begin
  620. emit_reg_reg(A_MOVQ,S_NO,right.location.register,R_MM7);
  621. emit_reg_reg(op,S_NO,left.location.register,R_MM7);
  622. emit_reg_reg(A_MOVQ,S_NO,R_MM7,left.location.register);
  623. end
  624. else
  625. begin
  626. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  627. internalerror(200203247);
  628. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,R_MM7);
  629. emit_reg_reg(op,S_NO,left.location.register,R_MM7);
  630. emit_reg_reg(A_MOVQ,S_NO,R_MM7,left.location.register);
  631. location_release(current_asmdata.CurrAsmList,right.location);
  632. end;
  633. end
  634. else
  635. begin
  636. if (right.location.loc=LOC_CMMXREGISTER) then
  637. begin
  638. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  639. end
  640. else
  641. begin
  642. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  643. internalerror(200203246);
  644. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  645. location_release(current_asmdata.CurrAsmList,right.location);
  646. end;
  647. end;
  648. end
  649. else
  650. begin
  651. { right.location=LOC_MMXREGISTER }
  652. if (nodetype=subn) and (nf_swapped in flags) then
  653. begin
  654. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  655. location_swap(left.location,right.location);
  656. toggleflag(nf_swapped);
  657. end
  658. else
  659. begin
  660. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  661. end;
  662. end;
  663. location_freetemp(current_asmdata.CurrAsmList,right.location);
  664. location_release(current_asmdata.CurrAsmList,right.location);
  665. if cmpop then
  666. begin
  667. location_freetemp(current_asmdata.CurrAsmList,left.location);
  668. location_release(current_asmdata.CurrAsmList,left.location);
  669. end;
  670. set_result_location(cmpop,true);
  671. end;
  672. {$endif SUPPORT_MMX}
  673. {*****************************************************************************
  674. pass_2
  675. *****************************************************************************}
  676. procedure tppcaddnode.pass_generate_code;
  677. { is also being used for xor, and "mul", "sub, or and comparative }
  678. { operators }
  679. var
  680. cgop : topcg;
  681. op : tasmop;
  682. tmpreg : tregister;
  683. hl : tasmlabel;
  684. cmpop : boolean;
  685. { true, if unsigned types are compared }
  686. unsigned : boolean;
  687. checkoverflow : boolean;
  688. begin
  689. { to make it more readable, string and set (not smallset!) have their
  690. own procedures }
  691. case left.resultdef.typ of
  692. orddef :
  693. begin
  694. { handling boolean expressions }
  695. if is_boolean(left.resultdef) and
  696. is_boolean(right.resultdef) then
  697. begin
  698. second_addboolean;
  699. exit;
  700. end
  701. { 64bit operations }
  702. else if is_64bit(resultdef) or
  703. is_64bit(left.resultdef) then
  704. begin
  705. second_add64bit;
  706. exit;
  707. end;
  708. end;
  709. stringdef :
  710. begin
  711. internalerror(2002072402);
  712. exit;
  713. end;
  714. setdef :
  715. begin
  716. { normalsets are already handled in pass1 }
  717. if not is_smallset(left.resultdef) then
  718. internalerror(200109042);
  719. second_addsmallset;
  720. exit;
  721. end;
  722. arraydef :
  723. begin
  724. {$ifdef SUPPORT_MMX}
  725. if is_mmx_able_array(left.resultdef) then
  726. begin
  727. second_addmmx;
  728. exit;
  729. end;
  730. {$endif SUPPORT_MMX}
  731. end;
  732. floatdef :
  733. begin
  734. second_addfloat;
  735. exit;
  736. end;
  737. end;
  738. { defaults }
  739. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  740. unsigned:=not(is_signed(left.resultdef)) or
  741. not(is_signed(right.resultdef));
  742. pass_left_and_right;
  743. { Convert flags to register first }
  744. { can any of these things be in the flags actually?? (JM) }
  745. if (left.location.loc = LOC_FLAGS) or
  746. (right.location.loc = LOC_FLAGS) then
  747. internalerror(2002072602);
  748. { set result location }
  749. if not cmpop then
  750. location_reset(location,LOC_REGISTER,def_cgsize(resultdef))
  751. else
  752. location_reset(location,LOC_FLAGS,OS_NO);
  753. checkoverflow:=
  754. (nodetype in [addn,subn,muln]) and
  755. (cs_check_overflow in current_settings.localswitches) and
  756. (left.resultdef.typ<>pointerdef) and
  757. (right.resultdef.typ<>pointerdef);
  758. load_left_right(cmpop, checkoverflow);
  759. if not(cmpop) then
  760. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  761. if not(checkoverflow) then
  762. begin
  763. case nodetype of
  764. addn, muln, xorn, orn, andn:
  765. begin
  766. case nodetype of
  767. addn:
  768. cgop := OP_ADD;
  769. muln:
  770. if unsigned then
  771. cgop := OP_MUL
  772. else
  773. cgop := OP_IMUL;
  774. xorn:
  775. cgop := OP_XOR;
  776. orn:
  777. cgop := OP_OR;
  778. andn:
  779. cgop := OP_AND;
  780. end;
  781. if (left.location.loc = LOC_CONSTANT) then
  782. swapleftright;
  783. if (right.location.loc <> LOC_CONSTANT) then
  784. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  785. left.location.register,right.location.register,
  786. location.register)
  787. else
  788. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  789. right.location.value,left.location.register,
  790. location.register);
  791. end;
  792. subn:
  793. begin
  794. if (nf_swapped in flags) then
  795. swapleftright;
  796. if left.location.loc <> LOC_CONSTANT then
  797. if right.location.loc <> LOC_CONSTANT then
  798. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  799. right.location.register,left.location.register,
  800. location.register)
  801. else
  802. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  803. right.location.value,left.location.register,
  804. location.register)
  805. else
  806. if (longint(left.location.value) >= low(smallint)) and
  807. (longint(left.location.value) <= high(smallint)) then
  808. begin
  809. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  810. location.register,right.location.register,
  811. longint(left.location.value)));
  812. end
  813. else
  814. begin
  815. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  816. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  817. left.location.value,tmpreg);
  818. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  819. right.location.register,tmpreg,location.register);
  820. end;
  821. end;
  822. ltn,lten,gtn,gten,equaln,unequaln :
  823. begin
  824. emit_compare(unsigned);
  825. end;
  826. end;
  827. end
  828. else
  829. // overflow checking is on and we have an addn, subn or muln
  830. begin
  831. if is_signed(resultdef) then
  832. begin
  833. case nodetype of
  834. addn:
  835. op := A_ADDO;
  836. subn:
  837. begin
  838. op := A_SUBO;
  839. if (nf_swapped in flags) then
  840. swapleftright;
  841. end;
  842. muln:
  843. op := A_MULLWO;
  844. else
  845. internalerror(2002072601);
  846. end;
  847. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,
  848. left.location.register,right.location.register));
  849. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  850. end
  851. else
  852. begin
  853. case nodetype of
  854. addn:
  855. begin
  856. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register,
  857. left.location.register,right.location.register));
  858. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register,left.location.register));
  859. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  860. end;
  861. subn:
  862. begin
  863. if nf_swapped in flags then
  864. swapleftright;
  865. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  866. left.location.register,right.location.register));
  867. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register,location.register));
  868. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  869. end;
  870. muln:
  871. begin
  872. { calculate the upper 32 bits of the product, = 0 if no overflow }
  873. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  874. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHWU_,NR_R0,
  875. left.location.register,right.location.register));
  876. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  877. { calculate the real result }
  878. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLW,location.register,
  879. left.location.register,right.location.register));
  880. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  881. current_asmdata.getjumplabel(hl);
  882. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,hl);
  883. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  884. cg.a_label(current_asmdata.CurrAsmList,hl);
  885. end;
  886. end;
  887. end;
  888. end;
  889. end;
  890. begin
  891. caddnode:=tppcaddnode;
  892. end.