nppcadd.pas 55 KB

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