nppcadd.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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,cpubase;
  22. type
  23. tppcaddnode = class(tcgaddnode)
  24. function pass_1: tnode; override;
  25. procedure pass_2;override;
  26. protected
  27. function use_generic_mul32to64: boolean; override;
  28. private
  29. procedure pass_left_and_right;
  30. procedure load_left_right(cmpop, load_constants: boolean);
  31. function getresflags : tresflags;
  32. procedure emit_compare(unsigned : boolean);
  33. procedure second_addfloat;override;
  34. procedure second_addboolean;override;
  35. procedure second_addsmallset;override;
  36. {$ifdef SUPPORT_MMX}
  37. procedure second_addmmx;override;
  38. {$endif SUPPORT_MMX}
  39. procedure second_add64bit;override;
  40. end;
  41. implementation
  42. uses
  43. globtype,systems,
  44. cutils,verbose,globals,
  45. symconst,symdef,paramgr,
  46. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  47. cgbase,cpuinfo,pass_1,pass_2,regvars,
  48. cpupara,cgcpu,cgutils,procinfo,
  49. ncon,nset,
  50. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  51. {*****************************************************************************
  52. Pass 1
  53. *****************************************************************************}
  54. function tppcaddnode.pass_1: tnode;
  55. begin
  56. resulttypepass(left);
  57. if (nodetype in [equaln,unequaln]) and
  58. (left.resulttype.def.deftype = orddef) and
  59. is_64bit(left.resulttype.def) then
  60. begin
  61. result := nil;
  62. firstpass(left);
  63. firstpass(right);
  64. expectloc := LOC_FLAGS;
  65. calcregisters(self,2,0,0);
  66. exit;
  67. end;
  68. result := inherited pass_1;
  69. end;
  70. function tppcaddnode.use_generic_mul32to64: boolean;
  71. begin
  72. result := false;
  73. end;
  74. {*****************************************************************************
  75. Helpers
  76. *****************************************************************************}
  77. procedure tppcaddnode.pass_left_and_right;
  78. begin
  79. { calculate the operator which is more difficult }
  80. firstcomplex(self);
  81. { in case of constant put it to the left }
  82. if (left.nodetype=ordconstn) then
  83. swapleftright;
  84. secondpass(left);
  85. secondpass(right);
  86. end;
  87. procedure tppcaddnode.load_left_right(cmpop, load_constants: boolean);
  88. procedure load_node(var n: tnode);
  89. begin
  90. case n.location.loc of
  91. LOC_CREGISTER:
  92. ;
  93. LOC_REGISTER:
  94. if (not cmpop) and
  95. ((nodetype <> muln) or
  96. not is_64bit(resulttype.def)) then
  97. begin
  98. location.register := n.location.register;
  99. if is_64bit(n.resulttype.def) then
  100. location.register64.reghi := n.location.register64.reghi;
  101. end;
  102. LOC_REFERENCE,LOC_CREFERENCE:
  103. begin
  104. location_force_reg(current_asmdata.CurrAsmList,n.location,def_cgsize(n.resulttype.def),false);
  105. if (not cmpop) and
  106. ((nodetype <> muln) or
  107. not is_64bit(resulttype.def)) then
  108. begin
  109. location.register := n.location.register;
  110. if is_64bit(n.resulttype.def) then
  111. location.register64.reghi := n.location.register64.reghi;
  112. end;
  113. end;
  114. LOC_CONSTANT:
  115. begin
  116. if load_constants then
  117. begin
  118. location_force_reg(current_asmdata.CurrAsmList,n.location,def_cgsize(n.resulttype.def),false);
  119. if (not cmpop) and
  120. ((nodetype <> muln) or
  121. not is_64bit(resulttype.def)) then
  122. begin
  123. location.register := n.location.register;
  124. if is_64bit(n.resulttype.def) then
  125. location.register64.reghi := n.location.register64.reghi;
  126. end;
  127. end;
  128. end;
  129. else
  130. location_force_reg(current_asmdata.CurrAsmList,n.location,def_cgsize(n.resulttype.def),false);
  131. end;
  132. end;
  133. begin
  134. load_node(left);
  135. load_node(right);
  136. if not(cmpop) then
  137. begin
  138. if (location.register = NR_NO) then
  139. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  140. if is_64bit(resulttype.def) and
  141. (location.register64.reghi = NR_NO) then
  142. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  143. end;
  144. end;
  145. function tppcaddnode.getresflags : tresflags;
  146. begin
  147. if (left.resulttype.def.deftype <> floatdef) then
  148. result.cr := RS_CR0
  149. else
  150. result.cr := RS_CR1;
  151. case nodetype of
  152. equaln : result.flag:=F_EQ;
  153. unequaln : result.flag:=F_NE;
  154. else
  155. if nf_swaped in flags then
  156. case nodetype of
  157. ltn : result.flag:=F_GT;
  158. lten : result.flag:=F_GE;
  159. gtn : result.flag:=F_LT;
  160. gten : result.flag:=F_LE;
  161. end
  162. else
  163. case nodetype of
  164. ltn : result.flag:=F_LT;
  165. lten : result.flag:=F_LE;
  166. gtn : result.flag:=F_GT;
  167. gten : result.flag:=F_GE;
  168. end;
  169. end
  170. end;
  171. procedure tppcaddnode.emit_compare(unsigned: boolean);
  172. var
  173. op : tasmop;
  174. tmpreg : tregister;
  175. useconst : boolean;
  176. begin
  177. // get the constant on the right if there is one
  178. if (left.location.loc = LOC_CONSTANT) then
  179. swapleftright;
  180. // can we use an immediate, or do we have to load the
  181. // constant in a register first?
  182. if (right.location.loc = LOC_CONSTANT) then
  183. begin
  184. {$ifdef dummy}
  185. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>$ffffffff) or unsigned) then
  186. internalerror(2002080301);
  187. {$endif extdebug}
  188. if (nodetype in [equaln,unequaln]) then
  189. if (unsigned and
  190. (aword(right.location.value) > high(word))) or
  191. (not unsigned and
  192. (aint(right.location.value) < low(smallint)) or
  193. (aint(right.location.value) > high(smallint))) then
  194. { we can then maybe use a constant in the 'othersigned' case
  195. (the sign doesn't matter for // equal/unequal)}
  196. unsigned := not unsigned;
  197. if (unsigned and
  198. (aword(right.location.value) <= high(word))) or
  199. (not(unsigned) and
  200. (aint(right.location.value) >= low(smallint)) and
  201. (aint(right.location.value) <= high(smallint))) then
  202. useconst := true
  203. else
  204. begin
  205. useconst := false;
  206. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  207. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  208. right.location.value,tmpreg);
  209. end
  210. end
  211. else
  212. useconst := false;
  213. location.loc := LOC_FLAGS;
  214. location.resflags := getresflags;
  215. if not unsigned then
  216. if useconst then
  217. op := A_CMPWI
  218. else
  219. op := A_CMPW
  220. else
  221. if useconst then
  222. op := A_CMPLWI
  223. else
  224. op := A_CMPLW;
  225. if (right.location.loc = LOC_CONSTANT) then
  226. begin
  227. if useconst then
  228. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op,left.location.register,longint(right.location.value)))
  229. else
  230. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,left.location.register,tmpreg));
  231. end
  232. else
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  234. left.location.register,right.location.register));
  235. end;
  236. {*****************************************************************************
  237. AddBoolean
  238. *****************************************************************************}
  239. procedure tppcaddnode.second_addboolean;
  240. var
  241. cgop : TOpCg;
  242. cgsize : TCgSize;
  243. cmpop,
  244. isjump : boolean;
  245. otl,ofl : tasmlabel;
  246. begin
  247. { calculate the operator which is more difficult }
  248. firstcomplex(self);
  249. cmpop:=false;
  250. if (torddef(left.resulttype.def).typ=bool8bit) or
  251. (torddef(right.resulttype.def).typ=bool8bit) then
  252. cgsize:=OS_8
  253. else
  254. if (torddef(left.resulttype.def).typ=bool16bit) or
  255. (torddef(right.resulttype.def).typ=bool16bit) then
  256. cgsize:=OS_16
  257. else
  258. cgsize:=OS_32;
  259. if (cs_full_boolean_eval in aktlocalswitches) or
  260. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  261. begin
  262. if left.nodetype in [ordconstn,realconstn] then
  263. swapleftright;
  264. isjump:=(left.expectloc=LOC_JUMP);
  265. if isjump then
  266. begin
  267. otl:=current_procinfo.CurrTrueLabel;
  268. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  269. ofl:=current_procinfo.CurrFalseLabel;
  270. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  271. end;
  272. secondpass(left);
  273. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  274. location_force_reg(current_asmdata.CurrAsmList,left.location,cgsize,false);
  275. if isjump then
  276. begin
  277. current_procinfo.CurrTrueLabel:=otl;
  278. current_procinfo.CurrFalseLabel:=ofl;
  279. end
  280. else if left.location.loc=LOC_JUMP then
  281. internalerror(2003122901);
  282. isjump:=(right.expectloc=LOC_JUMP);
  283. if isjump then
  284. begin
  285. otl:=current_procinfo.CurrTrueLabel;
  286. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  287. ofl:=current_procinfo.CurrFalseLabel;
  288. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  289. end;
  290. secondpass(right);
  291. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  292. location_force_reg(current_asmdata.CurrAsmList,right.location,cgsize,false);
  293. if isjump then
  294. begin
  295. current_procinfo.CurrTrueLabel:=otl;
  296. current_procinfo.CurrFalseLabel:=ofl;
  297. end
  298. else if right.location.loc=LOC_JUMP then
  299. internalerror(200312292);
  300. cmpop := nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  301. { set result location }
  302. if not cmpop then
  303. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def))
  304. else
  305. location_reset(location,LOC_FLAGS,OS_NO);
  306. load_left_right(cmpop,false);
  307. if (left.location.loc = LOC_CONSTANT) then
  308. swapleftright;
  309. { compare the }
  310. case nodetype of
  311. ltn,lten,gtn,gten,
  312. equaln,unequaln :
  313. begin
  314. if (right.location.loc <> LOC_CONSTANT) then
  315. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,
  316. left.location.register,right.location.register))
  317. else
  318. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPLWI,
  319. left.location.register,longint(right.location.value)));
  320. location.resflags := getresflags;
  321. end;
  322. else
  323. begin
  324. case nodetype of
  325. xorn :
  326. cgop:=OP_XOR;
  327. orn :
  328. cgop:=OP_OR;
  329. andn :
  330. cgop:=OP_AND;
  331. else
  332. internalerror(200203247);
  333. end;
  334. if right.location.loc <> LOC_CONSTANT then
  335. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  336. left.location.register,right.location.register,
  337. location.register)
  338. else
  339. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  340. right.location.value,left.location.register,
  341. location.register);
  342. end;
  343. end;
  344. end
  345. else
  346. begin
  347. // just to make sure we free the right registers
  348. cmpop := true;
  349. case nodetype of
  350. andn,
  351. orn :
  352. begin
  353. location_reset(location,LOC_JUMP,OS_NO);
  354. case nodetype of
  355. andn :
  356. begin
  357. otl:=current_procinfo.CurrTrueLabel;
  358. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  359. secondpass(left);
  360. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  361. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  362. current_procinfo.CurrTrueLabel:=otl;
  363. end;
  364. orn :
  365. begin
  366. ofl:=current_procinfo.CurrFalseLabel;
  367. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  368. secondpass(left);
  369. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  370. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  371. current_procinfo.CurrFalseLabel:=ofl;
  372. end;
  373. else
  374. internalerror(200403181);
  375. end;
  376. secondpass(right);
  377. maketojumpbool(current_asmdata.CurrAsmList,right,lr_load_regvars);
  378. end;
  379. end;
  380. end;
  381. end;
  382. {*****************************************************************************
  383. AddFloat
  384. *****************************************************************************}
  385. procedure tppcaddnode.second_addfloat;
  386. var
  387. op : TAsmOp;
  388. cmpop : boolean;
  389. begin
  390. pass_left_and_right;
  391. cmpop:=false;
  392. case nodetype of
  393. addn :
  394. op:=A_FADD;
  395. muln :
  396. op:=A_FMUL;
  397. subn :
  398. op:=A_FSUB;
  399. slashn :
  400. op:=A_FDIV;
  401. ltn,lten,gtn,gten,
  402. equaln,unequaln :
  403. begin
  404. op:=A_FCMPO;
  405. cmpop:=true;
  406. end;
  407. else
  408. internalerror(200403182);
  409. end;
  410. // get the operands in the correct order, there are no special cases
  411. // here, everything is register-based
  412. if nf_swaped in flags then
  413. swapleftright;
  414. // put both operands in a register
  415. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  416. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  417. // initialize de result
  418. if not cmpop then
  419. begin
  420. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  421. if left.location.loc = LOC_FPUREGISTER then
  422. location.register := left.location.register
  423. else if right.location.loc = LOC_FPUREGISTER then
  424. location.register := right.location.register
  425. else
  426. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  427. end
  428. else
  429. begin
  430. location_reset(location,LOC_FLAGS,OS_NO);
  431. location.resflags := getresflags;
  432. end;
  433. // emit the actual operation
  434. if not cmpop then
  435. begin
  436. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  437. location.register,left.location.register,
  438. right.location.register))
  439. end
  440. else
  441. begin
  442. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  443. newreg(R_SPECIALREGISTER,location.resflags.cr,R_SUBNONE),left.location.register,right.location.register))
  444. end;
  445. end;
  446. {*****************************************************************************
  447. AddSmallSet
  448. *****************************************************************************}
  449. procedure tppcaddnode.second_addsmallset;
  450. var
  451. cgop : TOpCg;
  452. tmpreg : tregister;
  453. opdone,
  454. cmpop : boolean;
  455. begin
  456. pass_left_and_right;
  457. { when a setdef is passed, it has to be a smallset }
  458. if ((left.resulttype.def.deftype=setdef) and
  459. (tsetdef(left.resulttype.def).settype<>smallset)) or
  460. ((right.resulttype.def.deftype=setdef) and
  461. (tsetdef(right.resulttype.def).settype<>smallset)) then
  462. internalerror(200203301);
  463. opdone := false;
  464. cmpop:=nodetype in [equaln,unequaln,lten,gten];
  465. { set result location }
  466. if not cmpop then
  467. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def))
  468. else
  469. location_reset(location,LOC_FLAGS,OS_NO);
  470. load_left_right(cmpop,false);
  471. if not(cmpop) and
  472. (location.register = NR_NO) then
  473. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  474. case nodetype of
  475. addn :
  476. begin
  477. if (nf_swaped in flags) and (left.nodetype=setelementn) then
  478. swapleftright;
  479. { are we adding set elements ? }
  480. if right.nodetype=setelementn then
  481. begin
  482. { no range support for smallsets! }
  483. if assigned(tsetelementnode(right).right) then
  484. internalerror(43244);
  485. if (right.location.loc = LOC_CONSTANT) then
  486. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  487. aint(aword(1) shl aword(right.location.value)),
  488. left.location.register,location.register)
  489. else
  490. begin
  491. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  492. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,tmpreg);
  493. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_INT,
  494. right.location.register,tmpreg);
  495. if left.location.loc <> LOC_CONSTANT then
  496. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,tmpreg,
  497. left.location.register,location.register)
  498. else
  499. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,
  500. left.location.value,tmpreg,location.register);
  501. end;
  502. opdone := true;
  503. end
  504. else
  505. cgop := OP_OR;
  506. end;
  507. symdifn :
  508. cgop:=OP_XOR;
  509. muln :
  510. cgop:=OP_AND;
  511. subn :
  512. begin
  513. cgop:=OP_AND;
  514. if (not(nf_swaped in flags)) then
  515. if (right.location.loc=LOC_CONSTANT) then
  516. right.location.value := not(right.location.value)
  517. else
  518. opdone := true
  519. else if (left.location.loc=LOC_CONSTANT) then
  520. left.location.value := not(left.location.value)
  521. else
  522. begin
  523. swapleftright;
  524. opdone := true;
  525. end;
  526. if opdone then
  527. begin
  528. if left.location.loc = LOC_CONSTANT then
  529. begin
  530. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  531. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  532. left.location.value,tmpreg);
  533. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  534. location.register,tmpreg,right.location.register));
  535. end
  536. else
  537. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  538. location.register,left.location.register,
  539. right.location.register));
  540. end;
  541. end;
  542. equaln,
  543. unequaln :
  544. begin
  545. emit_compare(true);
  546. opdone := true;
  547. end;
  548. lten,gten:
  549. begin
  550. If (not(nf_swaped in flags) and
  551. (nodetype = lten)) or
  552. ((nf_swaped in flags) and
  553. (nodetype = gten)) then
  554. swapleftright;
  555. // now we have to check whether left >= right
  556. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  557. if left.location.loc = LOC_CONSTANT then
  558. begin
  559. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,
  560. not(left.location.value),right.location.register,tmpreg);
  561. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPWI,tmpreg,0));
  562. // the two instructions above should be folded together by
  563. // the peepholeoptimizer
  564. end
  565. else
  566. begin
  567. if right.location.loc = LOC_CONSTANT then
  568. begin
  569. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  570. right.location.value,tmpreg);
  571. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  572. tmpreg,left.location.register));
  573. end
  574. else
  575. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_,tmpreg,
  576. right.location.register,left.location.register));
  577. end;
  578. location.resflags.cr := RS_CR0;
  579. location.resflags.flag := F_EQ;
  580. opdone := true;
  581. end;
  582. else
  583. internalerror(2002072701);
  584. end;
  585. if not opdone then
  586. begin
  587. // these are all commutative operations
  588. if (left.location.loc = LOC_CONSTANT) then
  589. swapleftright;
  590. if (right.location.loc = LOC_CONSTANT) then
  591. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  592. right.location.value,left.location.register,
  593. location.register)
  594. else
  595. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  596. right.location.register,left.location.register,
  597. location.register);
  598. end;
  599. end;
  600. {*****************************************************************************
  601. Add64bit
  602. *****************************************************************************}
  603. procedure tppcaddnode.second_add64bit;
  604. var
  605. op : TOpCG;
  606. op1,op2 : TAsmOp;
  607. cmpop,
  608. unsigned : boolean;
  609. procedure emit_cmp64_hi;
  610. var
  611. oldleft, oldright: tlocation;
  612. begin
  613. // put the high part of the location in the low part
  614. location_copy(oldleft,left.location);
  615. location_copy(oldright,right.location);
  616. if left.location.loc = LOC_CONSTANT then
  617. left.location.value64 := left.location.value64 shr 32
  618. else
  619. left.location.register64.reglo := left.location.register64.reghi;
  620. if right.location.loc = LOC_CONSTANT then
  621. right.location.value64 := right.location.value64 shr 32
  622. else
  623. right.location.register64.reglo := right.location.register64.reghi;
  624. // and call the normal emit_compare
  625. emit_compare(unsigned);
  626. location_copy(left.location,oldleft);
  627. location_copy(right.location,oldright);
  628. end;
  629. procedure emit_cmp64_lo;
  630. begin
  631. emit_compare(true);
  632. end;
  633. procedure firstjmp64bitcmp;
  634. var
  635. oldnodetype: tnodetype;
  636. begin
  637. {$ifdef OLDREGVARS}
  638. load_all_regvars(current_asmdata.CurrAsmList);
  639. {$endif OLDREGVARS}
  640. { the jump the sequence is a little bit hairy }
  641. case nodetype of
  642. ltn,gtn:
  643. begin
  644. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrTrueLabel);
  645. { cheat a little bit for the negative test }
  646. toggleflag(nf_swaped);
  647. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrFalseLabel);
  648. toggleflag(nf_swaped);
  649. end;
  650. lten,gten:
  651. begin
  652. oldnodetype:=nodetype;
  653. if nodetype=lten then
  654. nodetype:=ltn
  655. else
  656. nodetype:=gtn;
  657. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrTrueLabel);
  658. { cheat for the negative test }
  659. if nodetype=ltn then
  660. nodetype:=gtn
  661. else
  662. nodetype:=ltn;
  663. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrFalseLabel);
  664. nodetype:=oldnodetype;
  665. end;
  666. equaln:
  667. begin
  668. nodetype := unequaln;
  669. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrFalseLabel);
  670. nodetype := equaln;
  671. end;
  672. unequaln:
  673. begin
  674. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrTrueLabel);
  675. end;
  676. end;
  677. end;
  678. procedure secondjmp64bitcmp;
  679. begin
  680. { the jump the sequence is a little bit hairy }
  681. case nodetype of
  682. ltn,gtn,lten,gten:
  683. begin
  684. { the comparison of the low dword always has }
  685. { to be always unsigned! }
  686. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrTrueLabel);
  687. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  688. end;
  689. equaln:
  690. begin
  691. nodetype := unequaln;
  692. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrFalseLabel);
  693. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  694. nodetype := equaln;
  695. end;
  696. unequaln:
  697. begin
  698. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags,current_procinfo.CurrTrueLabel);
  699. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  700. end;
  701. end;
  702. end;
  703. var
  704. tempreg64: tregister64;
  705. begin
  706. firstcomplex(self);
  707. pass_left_and_right;
  708. cmpop:=false;
  709. unsigned:=((left.resulttype.def.deftype=orddef) and
  710. (torddef(left.resulttype.def).typ=u64bit)) or
  711. ((right.resulttype.def.deftype=orddef) and
  712. (torddef(right.resulttype.def).typ=u64bit));
  713. case nodetype of
  714. addn :
  715. begin
  716. op:=OP_ADD;
  717. end;
  718. subn :
  719. begin
  720. op:=OP_SUB;
  721. if (nf_swaped in flags) then
  722. swapleftright;
  723. end;
  724. ltn,lten,
  725. gtn,gten,
  726. equaln,unequaln:
  727. begin
  728. op:=OP_NONE;
  729. cmpop:=true;
  730. end;
  731. xorn:
  732. op:=OP_XOR;
  733. orn:
  734. op:=OP_OR;
  735. andn:
  736. op:=OP_AND;
  737. muln:
  738. begin
  739. { should be handled in pass_1 (JM) }
  740. if not(torddef(left.resulttype.def).typ in [U32bit,s32bit]) or
  741. (torddef(left.resulttype.def).typ <> torddef(right.resulttype.def).typ) then
  742. internalerror(200109051);
  743. { handled separately }
  744. op := OP_NONE;
  745. end;
  746. else
  747. internalerror(2002072705);
  748. end;
  749. if not cmpop then
  750. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  751. load_left_right(cmpop,((cs_check_overflow in aktlocalswitches) and
  752. (nodetype in [addn,subn])) or (nodetype = muln));
  753. if (nodetype <> muln) and
  754. (not(cs_check_overflow in aktlocalswitches) or
  755. not(nodetype in [addn,subn])) then
  756. begin
  757. case nodetype of
  758. ltn,lten,
  759. gtn,gten:
  760. begin
  761. emit_cmp64_hi;
  762. firstjmp64bitcmp;
  763. emit_cmp64_lo;
  764. secondjmp64bitcmp;
  765. end;
  766. equaln,unequaln:
  767. begin
  768. // instead of doing a complicated compare, do
  769. // (left.hi xor right.hi) or (left.lo xor right.lo)
  770. // (somewhate optimized so that no superfluous 'mr's are
  771. // generated)
  772. if (left.location.loc = LOC_CONSTANT) then
  773. swapleftright;
  774. if (right.location.loc = LOC_CONSTANT) then
  775. begin
  776. if left.location.loc = LOC_REGISTER then
  777. begin
  778. tempreg64.reglo := left.location.register64.reglo;
  779. tempreg64.reghi := left.location.register64.reghi;
  780. end
  781. else
  782. begin
  783. if (aint(right.location.value64) <> 0) then
  784. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  785. else
  786. tempreg64.reglo := left.location.register64.reglo;
  787. if ((right.location.value64 shr 32) <> 0) then
  788. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_32)
  789. else
  790. tempreg64.reghi := left.location.register64.reghi;
  791. end;
  792. if (aint(right.location.value64) <> 0) then
  793. { negative values can be handled using SUB, }
  794. { positive values < 65535 using XOR. }
  795. if (longint(right.location.value64) >= -32767) and
  796. (longint(right.location.value64) < 0) then
  797. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  798. aint(right.location.value64),
  799. left.location.register64.reglo,tempreg64.reglo)
  800. else
  801. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  802. aint(right.location.value64),
  803. left.location.register64.reglo,tempreg64.reglo);
  804. if ((right.location.value64 shr 32) <> 0) then
  805. if (longint(right.location.value64 shr 32) >= -32767) and
  806. (longint(right.location.value64 shr 32) < 0) then
  807. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  808. aint(right.location.value64 shr 32),
  809. left.location.register64.reghi,tempreg64.reghi)
  810. else
  811. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  812. aint(right.location.value64 shr 32),
  813. left.location.register64.reghi,tempreg64.reghi);
  814. end
  815. else
  816. begin
  817. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  818. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  819. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,location.size,
  820. left.location.register64,right.location.register64,
  821. tempreg64);
  822. end;
  823. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  824. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,NR_R0,
  825. tempreg64.reglo,tempreg64.reghi));
  826. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  827. location_reset(location,LOC_FLAGS,OS_NO);
  828. location.resflags := getresflags;
  829. end;
  830. xorn,orn,andn,addn:
  831. begin
  832. if (location.register64.reglo = NR_NO) then
  833. begin
  834. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  835. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  836. end;
  837. if (left.location.loc = LOC_CONSTANT) then
  838. swapleftright;
  839. if (right.location.loc = LOC_CONSTANT) then
  840. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.value64,
  841. left.location.register64,location.register64)
  842. else
  843. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,op,location.size,right.location.register64,
  844. left.location.register64,location.register64);
  845. end;
  846. subn:
  847. begin
  848. if left.location.loc <> LOC_CONSTANT then
  849. begin
  850. if (location.register64.reglo = NR_NO) then
  851. begin
  852. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  853. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  854. end;
  855. if right.location.loc <> LOC_CONSTANT then
  856. // reg64 - reg64
  857. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  858. right.location.register64,left.location.register64,
  859. location.register64)
  860. else
  861. // reg64 - const64
  862. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  863. right.location.value64,left.location.register64,
  864. location.register64)
  865. end
  866. else if ((left.location.value64 shr 32) = 0) then
  867. begin
  868. if (location.register64.reglo = NR_NO) then
  869. begin
  870. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  871. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  872. end;
  873. if (int64(left.location.value64) >= low(smallint)) and
  874. (int64(left.location.value64) <= high(smallint)) then
  875. begin
  876. // consts16 - reg64
  877. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  878. location.register64.reglo,right.location.register64.reglo,
  879. left.location.value));
  880. end
  881. else
  882. begin
  883. // const32 - reg64
  884. location_force_reg(current_asmdata.CurrAsmList,left.location,
  885. OS_32,true);
  886. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBC,
  887. location.register64.reglo,left.location.register64.reglo,
  888. right.location.register64.reglo));
  889. end;
  890. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBFZE,
  891. location.register64.reghi,right.location.register64.reghi));
  892. end
  893. else if (aint(left.location.value64) = 0) then
  894. begin
  895. // (const32 shl 32) - reg64
  896. if (location.register64.reglo = NR_NO) then
  897. begin
  898. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  899. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  900. end;
  901. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  902. location.register64.reglo,right.location.register64.reglo,0));
  903. left.location.value64 := left.location.value64 shr 32;
  904. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_32,true);
  905. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,
  906. location.register64.reghi,right.location.register64.reghi,
  907. left.location.register));
  908. end
  909. else
  910. begin
  911. // const64 - reg64
  912. location_force_reg(current_asmdata.CurrAsmList,left.location,
  913. def_cgsize(left.resulttype.def),false);
  914. if (left.location.loc = LOC_REGISTER) then
  915. location.register64 := left.location.register64
  916. else if (location.register64.reglo = NR_NO) then
  917. begin
  918. location.register64.reglo := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  919. location.register64.reghi := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  920. end;
  921. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,
  922. right.location.register64,left.location.register64,
  923. location.register64);
  924. end;
  925. end;
  926. else
  927. internalerror(2002072803);
  928. end;
  929. end
  930. else
  931. begin
  932. if is_signed(resulttype.def) then
  933. begin
  934. case nodetype of
  935. addn:
  936. begin
  937. op1 := A_ADDC;
  938. op2 := A_ADDEO;
  939. end;
  940. subn:
  941. begin
  942. op1 := A_SUBC;
  943. op2 := A_SUBFEO;
  944. end;
  945. muln:
  946. begin
  947. op1 := A_MULLW;
  948. op2 := A_MULHW
  949. end;
  950. else
  951. internalerror(2002072806);
  952. end
  953. end
  954. else
  955. begin
  956. case nodetype of
  957. addn:
  958. begin
  959. op1 := A_ADDC;
  960. op2 := A_ADDE;
  961. end;
  962. subn:
  963. begin
  964. op1 := A_SUBC;
  965. op2 := A_SUBFE;
  966. end;
  967. muln:
  968. begin
  969. op1 := A_MULLW;
  970. op2 := A_MULHWU
  971. end;
  972. end;
  973. end;
  974. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op1,location.register64.reglo,
  975. left.location.register64.reglo,right.location.register64.reglo));
  976. if (nodetype <> muln) then
  977. begin
  978. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  979. right.location.register64.reghi,left.location.register64.reghi));
  980. if not(is_signed(resulttype.def)) then
  981. if nodetype = addn then
  982. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register64.reghi,left.location.register64.reghi))
  983. else
  984. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register64.reghi,location.register64.reghi));
  985. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resulttype.def);
  986. end
  987. else
  988. begin
  989. { 32 * 32 -> 64 cannot overflow }
  990. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op2,location.register64.reghi,
  991. left.location.register64.reglo,right.location.register64.reglo));
  992. end
  993. end;
  994. { set result location }
  995. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  996. { real location only now) (JM) }
  997. if cmpop and
  998. not(nodetype in [equaln,unequaln]) then
  999. location_reset(location,LOC_JUMP,OS_NO);
  1000. end;
  1001. {*****************************************************************************
  1002. AddMMX
  1003. *****************************************************************************}
  1004. {$ifdef SUPPORT_MMX}
  1005. procedure ti386addnode.second_addmmx;
  1006. var
  1007. op : TAsmOp;
  1008. cmpop : boolean;
  1009. mmxbase : tmmxtype;
  1010. hregister : tregister;
  1011. begin
  1012. pass_left_and_right;
  1013. cmpop:=false;
  1014. mmxbase:=mmx_type(left.resulttype.def);
  1015. case nodetype of
  1016. addn :
  1017. begin
  1018. if (cs_mmx_saturation in aktlocalswitches) then
  1019. begin
  1020. case mmxbase of
  1021. mmxs8bit:
  1022. op:=A_PADDSB;
  1023. mmxu8bit:
  1024. op:=A_PADDUSB;
  1025. mmxs16bit,mmxfixed16:
  1026. op:=A_PADDSB;
  1027. mmxu16bit:
  1028. op:=A_PADDUSW;
  1029. end;
  1030. end
  1031. else
  1032. begin
  1033. case mmxbase of
  1034. mmxs8bit,mmxu8bit:
  1035. op:=A_PADDB;
  1036. mmxs16bit,mmxu16bit,mmxfixed16:
  1037. op:=A_PADDW;
  1038. mmxs32bit,mmxu32bit:
  1039. op:=A_PADDD;
  1040. end;
  1041. end;
  1042. end;
  1043. muln :
  1044. begin
  1045. case mmxbase of
  1046. mmxs16bit,mmxu16bit:
  1047. op:=A_PMULLW;
  1048. mmxfixed16:
  1049. op:=A_PMULHW;
  1050. end;
  1051. end;
  1052. subn :
  1053. begin
  1054. if (cs_mmx_saturation in aktlocalswitches) then
  1055. begin
  1056. case mmxbase of
  1057. mmxs8bit:
  1058. op:=A_PSUBSB;
  1059. mmxu8bit:
  1060. op:=A_PSUBUSB;
  1061. mmxs16bit,mmxfixed16:
  1062. op:=A_PSUBSB;
  1063. mmxu16bit:
  1064. op:=A_PSUBUSW;
  1065. end;
  1066. end
  1067. else
  1068. begin
  1069. case mmxbase of
  1070. mmxs8bit,mmxu8bit:
  1071. op:=A_PSUBB;
  1072. mmxs16bit,mmxu16bit,mmxfixed16:
  1073. op:=A_PSUBW;
  1074. mmxs32bit,mmxu32bit:
  1075. op:=A_PSUBD;
  1076. end;
  1077. end;
  1078. end;
  1079. xorn:
  1080. op:=A_PXOR;
  1081. orn:
  1082. op:=A_POR;
  1083. andn:
  1084. op:=A_PAND;
  1085. else
  1086. internalerror(200403183);
  1087. end;
  1088. { left and right no register? }
  1089. { then one must be demanded }
  1090. if (left.location.loc<>LOC_MMXREGISTER) then
  1091. begin
  1092. if (right.location.loc=LOC_MMXREGISTER) then
  1093. begin
  1094. location_swap(left.location,right.location);
  1095. toggleflag(nf_swaped);
  1096. end
  1097. else
  1098. begin
  1099. { register variable ? }
  1100. if (left.location.loc=LOC_CMMXREGISTER) then
  1101. begin
  1102. hregister:=rg.getregistermm(current_asmdata.CurrAsmList);
  1103. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  1104. end
  1105. else
  1106. begin
  1107. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1108. internalerror(200203245);
  1109. location_release(current_asmdata.CurrAsmList,left.location);
  1110. hregister:=rg.getregistermm(current_asmdata.CurrAsmList);
  1111. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  1112. end;
  1113. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  1114. left.location.register:=hregister;
  1115. end;
  1116. end;
  1117. { at this point, left.location.loc should be LOC_MMXREGISTER }
  1118. if right.location.loc<>LOC_MMXREGISTER then
  1119. begin
  1120. if (nodetype=subn) and (nf_swaped in flags) then
  1121. begin
  1122. if right.location.loc=LOC_CMMXREGISTER then
  1123. begin
  1124. emit_reg_reg(A_MOVQ,S_NO,right.location.register,R_MM7);
  1125. emit_reg_reg(op,S_NO,left.location.register,R_MM7);
  1126. emit_reg_reg(A_MOVQ,S_NO,R_MM7,left.location.register);
  1127. end
  1128. else
  1129. begin
  1130. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1131. internalerror(200203247);
  1132. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,R_MM7);
  1133. emit_reg_reg(op,S_NO,left.location.register,R_MM7);
  1134. emit_reg_reg(A_MOVQ,S_NO,R_MM7,left.location.register);
  1135. location_release(current_asmdata.CurrAsmList,right.location);
  1136. end;
  1137. end
  1138. else
  1139. begin
  1140. if (right.location.loc=LOC_CMMXREGISTER) then
  1141. begin
  1142. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  1143. end
  1144. else
  1145. begin
  1146. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  1147. internalerror(200203246);
  1148. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  1149. location_release(current_asmdata.CurrAsmList,right.location);
  1150. end;
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. { right.location=LOC_MMXREGISTER }
  1156. if (nodetype=subn) and (nf_swaped in flags) then
  1157. begin
  1158. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  1159. location_swap(left.location,right.location);
  1160. toggleflag(nf_swaped);
  1161. end
  1162. else
  1163. begin
  1164. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  1165. end;
  1166. end;
  1167. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1168. location_release(current_asmdata.CurrAsmList,right.location);
  1169. if cmpop then
  1170. begin
  1171. location_freetemp(current_asmdata.CurrAsmList,left.location);
  1172. location_release(current_asmdata.CurrAsmList,left.location);
  1173. end;
  1174. set_result_location(cmpop,true);
  1175. end;
  1176. {$endif SUPPORT_MMX}
  1177. {*****************************************************************************
  1178. pass_2
  1179. *****************************************************************************}
  1180. procedure tppcaddnode.pass_2;
  1181. { is also being used for xor, and "mul", "sub, or and comparative }
  1182. { operators }
  1183. var
  1184. cgop : topcg;
  1185. op : tasmop;
  1186. tmpreg : tregister;
  1187. hl : tasmlabel;
  1188. cmpop : boolean;
  1189. { true, if unsigned types are compared }
  1190. unsigned : boolean;
  1191. begin
  1192. { to make it more readable, string and set (not smallset!) have their
  1193. own procedures }
  1194. case left.resulttype.def.deftype of
  1195. orddef :
  1196. begin
  1197. { handling boolean expressions }
  1198. if is_boolean(left.resulttype.def) and
  1199. is_boolean(right.resulttype.def) then
  1200. begin
  1201. second_addboolean;
  1202. exit;
  1203. end
  1204. { 64bit operations }
  1205. else if is_64bit(resulttype.def) or
  1206. is_64bit(left.resulttype.def) then
  1207. begin
  1208. second_add64bit;
  1209. exit;
  1210. end;
  1211. end;
  1212. stringdef :
  1213. begin
  1214. internalerror(2002072402);
  1215. exit;
  1216. end;
  1217. setdef :
  1218. begin
  1219. { normalsets are already handled in pass1 }
  1220. if (tsetdef(left.resulttype.def).settype<>smallset) then
  1221. internalerror(200109041);
  1222. second_addsmallset;
  1223. exit;
  1224. end;
  1225. arraydef :
  1226. begin
  1227. {$ifdef SUPPORT_MMX}
  1228. if is_mmx_able_array(left.resulttype.def) then
  1229. begin
  1230. second_addmmx;
  1231. exit;
  1232. end;
  1233. {$endif SUPPORT_MMX}
  1234. end;
  1235. floatdef :
  1236. begin
  1237. second_addfloat;
  1238. exit;
  1239. end;
  1240. end;
  1241. { defaults }
  1242. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  1243. unsigned:=not(is_signed(left.resulttype.def)) or
  1244. not(is_signed(right.resulttype.def));
  1245. pass_left_and_right;
  1246. { Convert flags to register first }
  1247. { can any of these things be in the flags actually?? (JM) }
  1248. if (left.location.loc = LOC_FLAGS) or
  1249. (right.location.loc = LOC_FLAGS) then
  1250. internalerror(2002072602);
  1251. { set result location }
  1252. if not cmpop then
  1253. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def))
  1254. else
  1255. location_reset(location,LOC_FLAGS,OS_NO);
  1256. load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  1257. (nodetype in [addn,subn,muln]));
  1258. if (location.register = NR_NO) and
  1259. not(cmpop) then
  1260. location.register := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1261. if not(cs_check_overflow in aktlocalswitches) or
  1262. (cmpop) or
  1263. (nodetype in [orn,andn,xorn]) then
  1264. begin
  1265. case nodetype of
  1266. addn, muln, xorn, orn, andn:
  1267. begin
  1268. case nodetype of
  1269. addn:
  1270. cgop := OP_ADD;
  1271. muln:
  1272. if unsigned then
  1273. cgop := OP_MUL
  1274. else
  1275. cgop := OP_IMUL;
  1276. xorn:
  1277. cgop := OP_XOR;
  1278. orn:
  1279. cgop := OP_OR;
  1280. andn:
  1281. cgop := OP_AND;
  1282. end;
  1283. if (left.location.loc = LOC_CONSTANT) then
  1284. swapleftright;
  1285. if (right.location.loc <> LOC_CONSTANT) then
  1286. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  1287. left.location.register,right.location.register,
  1288. location.register)
  1289. else
  1290. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,cgop,OS_INT,
  1291. right.location.value,left.location.register,
  1292. location.register);
  1293. end;
  1294. subn:
  1295. begin
  1296. if (nf_swaped in flags) then
  1297. swapleftright;
  1298. if left.location.loc <> LOC_CONSTANT then
  1299. if right.location.loc <> LOC_CONSTANT then
  1300. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  1301. right.location.register,left.location.register,
  1302. location.register)
  1303. else
  1304. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  1305. right.location.value,left.location.register,
  1306. location.register)
  1307. else
  1308. if (longint(left.location.value) >= low(smallint)) and
  1309. (longint(left.location.value) <= high(smallint)) then
  1310. begin
  1311. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  1312. location.register,right.location.register,
  1313. longint(left.location.value)));
  1314. end
  1315. else
  1316. begin
  1317. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  1318. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  1319. left.location.value,tmpreg);
  1320. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  1321. right.location.register,tmpreg,location.register);
  1322. end;
  1323. end;
  1324. ltn,lten,gtn,gten,equaln,unequaln :
  1325. begin
  1326. emit_compare(unsigned);
  1327. end;
  1328. end;
  1329. end
  1330. else
  1331. // overflow checking is on and we have an addn, subn or muln
  1332. begin
  1333. if is_signed(resulttype.def) then
  1334. begin
  1335. case nodetype of
  1336. addn:
  1337. op := A_ADDO;
  1338. subn:
  1339. begin
  1340. op := A_SUBO;
  1341. if (nf_swaped in flags) then
  1342. swapleftright;
  1343. end;
  1344. muln:
  1345. op := A_MULLWO;
  1346. else
  1347. internalerror(2002072601);
  1348. end;
  1349. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,
  1350. left.location.register,right.location.register));
  1351. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resulttype.def);
  1352. end
  1353. else
  1354. begin
  1355. case nodetype of
  1356. addn:
  1357. begin
  1358. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register,
  1359. left.location.register,right.location.register));
  1360. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,location.register,left.location.register));
  1361. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resulttype.def);
  1362. end;
  1363. subn:
  1364. begin
  1365. if nf_swaped in flags then
  1366. swapleftright;
  1367. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  1368. left.location.register,right.location.register));
  1369. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,left.location.register,location.register));
  1370. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resulttype.def);
  1371. end;
  1372. muln:
  1373. begin
  1374. { calculate the upper 32 bits of the product, = 0 if no overflow }
  1375. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_R0);
  1376. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHWU_,NR_R0,
  1377. left.location.register,right.location.register));
  1378. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_R0);
  1379. { calculate the real result }
  1380. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLW,location.register,
  1381. left.location.register,right.location.register));
  1382. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  1383. current_asmdata.getjumplabel(hl);
  1384. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList,OC_EQ,hl);
  1385. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW');
  1386. cg.a_label(current_asmdata.CurrAsmList,hl);
  1387. end;
  1388. end;
  1389. end;
  1390. end;
  1391. end;
  1392. begin
  1393. caddnode:=tppcaddnode;
  1394. end.