nppcadd.pas 55 KB

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