nppcadd.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the PowerPC64
  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. private
  27. procedure pass_left_and_right;
  28. procedure load_left_right(cmpop, load_constants: boolean);
  29. function getresflags: tresflags;
  30. procedure emit_compare(unsigned: boolean);
  31. procedure second_addfloat; override;
  32. procedure second_addboolean; override;
  33. procedure second_addsmallset; override;
  34. end;
  35. implementation
  36. uses
  37. sysutils,
  38. globtype, systems,
  39. cutils, verbose, globals,
  40. symconst, symdef, paramgr,
  41. aasmbase, aasmtai,aasmdata, aasmcpu, defutil, htypechk,
  42. cgbase, cpuinfo, pass_1, pass_2, regvars,
  43. cpupara, cgcpu, cgutils,procinfo,
  44. ncon, nset,
  45. ncgutil, tgobj, rgobj, rgcpu, cgobj;
  46. {*****************************************************************************
  47. Pass 1
  48. *****************************************************************************}
  49. function tppcaddnode.pass_1: tnode;
  50. begin
  51. resulttypepass(left);
  52. if (nodetype in [equaln, unequaln]) and
  53. (left.resulttype.def.deftype = orddef) {and
  54. is_64bit(left.resulttype.def)}then
  55. begin
  56. result := nil;
  57. firstpass(left);
  58. firstpass(right);
  59. expectloc := LOC_FLAGS;
  60. calcregisters(self, 2, 0, 0);
  61. exit;
  62. end;
  63. result := inherited pass_1;
  64. end;
  65. {*****************************************************************************
  66. Helpers
  67. *****************************************************************************}
  68. procedure tppcaddnode.pass_left_and_right;
  69. begin
  70. { calculate the operator which is more difficult }
  71. firstcomplex(self);
  72. { in case of constant put it to the left }
  73. if (left.nodetype = ordconstn) then
  74. swapleftright;
  75. secondpass(left);
  76. secondpass(right);
  77. end;
  78. procedure tppcaddnode.load_left_right(cmpop, load_constants: boolean);
  79. procedure load_node(var n: tnode);
  80. begin
  81. case n.location.loc of
  82. LOC_CREGISTER:
  83. ;
  84. LOC_REGISTER:
  85. if not cmpop then
  86. begin
  87. location.register := n.location.register;
  88. end;
  89. LOC_REFERENCE, LOC_CREFERENCE:
  90. begin
  91. location_force_reg(current_asmdata.CurrAsmList, n.location,
  92. def_cgsize(n.resulttype.def), false);
  93. end;
  94. LOC_CONSTANT:
  95. begin
  96. if load_constants then begin
  97. location_force_reg(current_asmdata.CurrAsmList, n.location,
  98. def_cgsize(n.resulttype.def), false);
  99. end;
  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 begin
  109. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  110. end;
  111. end;
  112. function tppcaddnode.getresflags: tresflags;
  113. begin
  114. if (left.resulttype.def.deftype <> floatdef) then
  115. result.cr := RS_CR0
  116. else
  117. result.cr := RS_CR1;
  118. case nodetype of
  119. equaln: result.flag := F_EQ;
  120. unequaln: result.flag := F_NE;
  121. else
  122. if nf_swaped in flags then
  123. case nodetype of
  124. ltn: result.flag := F_GT;
  125. lten: result.flag := F_GE;
  126. gtn: result.flag := F_LT;
  127. gten: result.flag := F_LE;
  128. end
  129. else
  130. case nodetype of
  131. ltn: result.flag := F_LT;
  132. lten: result.flag := F_LE;
  133. gtn: result.flag := F_GT;
  134. gten: result.flag := F_GE;
  135. end;
  136. end
  137. end;
  138. procedure tppcaddnode.emit_compare(unsigned: boolean);
  139. const
  140. { unsigned useconst 32bit-op }
  141. cmpop_table : array[boolean, boolean, boolean] of TAsmOp = (
  142. ((A_CMPD, A_CMPW), (A_CMPDI, A_CMPWI)),
  143. ((A_CMPLD, A_CMPLW), (A_CMPLDI, A_CMPLWI))
  144. );
  145. var
  146. op: TAsmOp;
  147. tmpreg: TRegister;
  148. useconst: boolean;
  149. opsize : TCgSize;
  150. begin
  151. { get the constant on the right if there is one }
  152. if (left.location.loc = LOC_CONSTANT) then
  153. swapleftright;
  154. opsize := def_cgsize(left.resulttype.def);
  155. {$IFDEF EXTDEBUG}
  156. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('tppcaddnode.emit_compare ' + inttostr(ord(opsize)) + ' ' + inttostr(tcgsize2size[opsize]))));
  157. {$ENDIF EXTDEBUG}
  158. { can we use a signed comparison or not? In case of equal/unequal comparison
  159. we can check whether this is possible because it does not matter. }
  160. if (right.location.loc = LOC_CONSTANT) then
  161. if (nodetype in [equaln,unequaln]) then
  162. if (unsigned and (aword(right.location.value) > high(word))) or
  163. (not unsigned and (aint(right.location.value) < low(smallint)) or
  164. (aint(right.location.value) > high(smallint))) then
  165. { we can then maybe use a constant in the 'othersigned' case
  166. (the sign doesn't matter for equal/unequal) }
  167. unsigned := not unsigned;
  168. { calculate the size of the comparison because ppc64 only has 32 and 64
  169. bit comparison opcodes; prefer 32 bits }
  170. if (not (opsize in [OS_32, OS_S32, OS_64, OS_S64])) then begin
  171. if (unsigned) then
  172. opsize := OS_32
  173. else
  174. opsize := OS_S32;
  175. cg.a_load_reg_reg(current_asmdata.CurrAsmList, def_cgsize(left.resulttype.def), opsize,
  176. left.location.register, left.location.register);
  177. end;
  178. { can we use an immediate, or do we have to load the
  179. constant in a register first? }
  180. if (right.location.loc = LOC_CONSTANT) then begin
  181. if (unsigned and
  182. (aword(right.location.value) <= high(word))) or
  183. (not (unsigned) and
  184. (aint(right.location.value) >= low(smallint)) and (aint(right.location.value) <= high(smallint))) then
  185. useconst := true
  186. else begin
  187. useconst := false;
  188. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  189. cg.a_load_const_reg(current_asmdata.CurrAsmList, opsize, right.location.value, tmpreg);
  190. end
  191. end else
  192. useconst := false;
  193. location.loc := LOC_FLAGS;
  194. location.resflags := getresflags;
  195. op := cmpop_table[unsigned, useconst, opsize in [OS_S32, OS_32]];
  196. { actually do the operation }
  197. if (right.location.loc = LOC_CONSTANT) then begin
  198. if useconst then
  199. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op, left.location.register,
  200. longint(right.location.value)))
  201. else
  202. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op, left.location.register, tmpreg));
  203. end else
  204. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op, left.location.register,
  205. right.location.register));
  206. end;
  207. {*****************************************************************************
  208. AddBoolean
  209. *****************************************************************************}
  210. procedure tppcaddnode.second_addboolean;
  211. var
  212. cgop: TOpCg;
  213. cgsize: TCgSize;
  214. cmpop,
  215. isjump: boolean;
  216. otl, ofl: tasmlabel;
  217. begin
  218. { calculate the operator which is more difficult }
  219. firstcomplex(self);
  220. cmpop := false;
  221. if (torddef(left.resulttype.def).typ = bool8bit) or
  222. (torddef(right.resulttype.def).typ = bool8bit) then
  223. cgsize := OS_8
  224. else 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 the result }
  389. if not cmpop then begin
  390. location_reset(location, LOC_FPUREGISTER, def_cgsize(resulttype.def));
  391. location.register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  392. end else begin
  393. location_reset(location, LOC_FLAGS, OS_NO);
  394. location.resflags := getresflags;
  395. end;
  396. { emit the actual operation }
  397. if not cmpop then begin
  398. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  399. location.register, left.location.register,
  400. right.location.register))
  401. end else begin
  402. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  403. newreg(R_SPECIALREGISTER, location.resflags.cr, R_SUBNONE),
  404. left.location.register, right.location.register))
  405. end;
  406. end;
  407. {*****************************************************************************
  408. AddSmallSet
  409. *****************************************************************************}
  410. procedure tppcaddnode.second_addsmallset;
  411. var
  412. cgop: TOpCg;
  413. tmpreg: tregister;
  414. opdone,
  415. cmpop: boolean;
  416. astring : string;
  417. // ts: todo - speed up by using 32 bit compares/adds/ands here
  418. begin
  419. pass_left_and_right;
  420. { when a setdef is passed, it has to be a smallset }
  421. if ((left.resulttype.def.deftype = setdef) and
  422. (tsetdef(left.resulttype.def).settype <> smallset)) or
  423. ((right.resulttype.def.deftype = setdef) and
  424. (tsetdef(right.resulttype.def).settype <> smallset)) then
  425. internalerror(200203301);
  426. opdone := false;
  427. cmpop := nodetype in [equaln, unequaln, lten, gten];
  428. { set result location }
  429. if not cmpop then
  430. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  431. else
  432. location_reset(location, LOC_FLAGS, OS_NO);
  433. load_left_right(cmpop, false);
  434. if not (cmpop) then
  435. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  436. {$ifdef extdebug}
  437. astring := 'addsmallset0 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  438. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  439. {$endif extdebug}
  440. case nodetype of
  441. addn:
  442. begin
  443. if (nf_swaped in flags) and (left.nodetype = setelementn) then
  444. swapleftright;
  445. { are we adding set elements ? }
  446. if right.nodetype = setelementn then 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 begin
  451. {$ifdef extdebug}
  452. astring := 'addsmallset1 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  453. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  454. {$endif extdebug}
  455. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64,
  456. aint(1) shl aint(right.location.value),
  457. left.location.register, location.register)
  458. end else
  459. begin
  460. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  461. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64, 1, tmpreg);
  462. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_64,
  463. right.location.register, tmpreg);
  464. if left.location.loc <> LOC_CONSTANT then begin
  465. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64, tmpreg,
  466. left.location.register, location.register)
  467. end else begin
  468. {$ifdef extdebug}
  469. astring := 'addsmallset2 ' + inttostr(left.location.value);
  470. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  471. {$endif extdebug}
  472. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64,
  473. left.location.value, tmpreg, location.register);
  474. end;
  475. end;
  476. opdone := true;
  477. end else begin
  478. cgop := OP_OR;
  479. end;
  480. end;
  481. symdifn:
  482. cgop := OP_XOR;
  483. muln:
  484. cgop := OP_AND;
  485. subn:
  486. begin
  487. cgop := OP_AND;
  488. if (not (nf_swaped in flags)) then
  489. if (right.location.loc = LOC_CONSTANT) then
  490. right.location.value := not (right.location.value)
  491. else
  492. opdone := true
  493. else if (left.location.loc = LOC_CONSTANT) then
  494. left.location.value := not (left.location.value)
  495. else begin
  496. swapleftright;
  497. opdone := true;
  498. end;
  499. if opdone then begin
  500. if left.location.loc = LOC_CONSTANT then
  501. begin
  502. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  503. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  504. left.location.value, tmpreg);
  505. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  506. location.register, tmpreg, right.location.register));
  507. end
  508. else
  509. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  510. location.register, left.location.register,
  511. right.location.register));
  512. end;
  513. end;
  514. equaln,
  515. unequaln:
  516. begin
  517. emit_compare(true);
  518. opdone := true;
  519. end;
  520. lten, gten:
  521. begin
  522. if (not (nf_swaped in flags) and
  523. (nodetype = lten)) or
  524. ((nf_swaped in flags) and
  525. (nodetype = gten)) then
  526. swapleftright;
  527. { now we have to check whether left >= right }
  528. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  529. if left.location.loc = LOC_CONSTANT then begin
  530. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_64,
  531. not (left.location.value), right.location.register, tmpreg);
  532. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPDI, tmpreg, 0));
  533. { the two instructions above should be folded together by
  534. the peepholeoptimizer }
  535. end else begin
  536. if right.location.loc = LOC_CONSTANT then begin
  537. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  538. right.location.value, tmpreg);
  539. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  540. tmpreg, left.location.register));
  541. end else
  542. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  543. right.location.register, left.location.register));
  544. end;
  545. location.resflags.cr := RS_CR0;
  546. location.resflags.flag := F_EQ;
  547. opdone := true;
  548. end;
  549. else
  550. internalerror(2002072701);
  551. end;
  552. if not opdone then begin
  553. { these are all commutative operations }
  554. if (left.location.loc = LOC_CONSTANT) then
  555. swapleftright;
  556. if (right.location.loc = LOC_CONSTANT) then begin
  557. {$ifdef extdebug}
  558. astring := 'addsmallset4 ' + inttostr(right.location.value);
  559. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  560. {$endif extdebug}
  561. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  562. right.location.value, left.location.register,
  563. location.register)
  564. end else begin
  565. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  566. right.location.register, left.location.register,
  567. location.register);
  568. end;
  569. end;
  570. end;
  571. {*****************************************************************************
  572. pass_2
  573. *****************************************************************************}
  574. procedure tppcaddnode.pass_2;
  575. { is also being used for xor, and "mul", "sub, or and comparative }
  576. { operators }
  577. var
  578. cgop: topcg;
  579. op: tasmop;
  580. tmpreg: tregister;
  581. hl: tasmlabel;
  582. cmpop: boolean;
  583. { true, if unsigned types are compared }
  584. unsigned: boolean;
  585. begin
  586. { to make it more readable, string and set (not smallset!) have their
  587. own procedures }
  588. case left.resulttype.def.deftype of
  589. orddef:
  590. begin
  591. { handling boolean expressions }
  592. if is_boolean(left.resulttype.def) and
  593. is_boolean(right.resulttype.def) then
  594. begin
  595. second_addboolean;
  596. exit;
  597. end;
  598. end;
  599. stringdef:
  600. begin
  601. internalerror(2002072402);
  602. exit;
  603. end;
  604. setdef:
  605. begin
  606. { normalsets are already handled in pass1 }
  607. if (tsetdef(left.resulttype.def).settype <> smallset) then
  608. internalerror(200109041);
  609. second_addsmallset;
  610. exit;
  611. end;
  612. arraydef:
  613. begin
  614. {$IFDEF SUPPORT_MMX}
  615. if is_mmx_able_array(left.resulttype.def) then
  616. begin
  617. second_addmmx;
  618. exit;
  619. end;
  620. {$ENDIF SUPPORT_MMX}
  621. end;
  622. floatdef:
  623. begin
  624. second_addfloat;
  625. exit;
  626. end;
  627. end;
  628. { defaults }
  629. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  630. unsigned := not (is_signed(left.resulttype.def)) or
  631. not (is_signed(right.resulttype.def));
  632. pass_left_and_right;
  633. { Convert flags to register first }
  634. { can any of these things be in the flags actually?? (JM) }
  635. if (left.location.loc = LOC_FLAGS) or
  636. (right.location.loc = LOC_FLAGS) then
  637. internalerror(2002072602);
  638. { set result location }
  639. if not cmpop then
  640. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  641. else
  642. location_reset(location, LOC_FLAGS, OS_NO);
  643. load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  644. (nodetype in [addn, subn, muln]));
  645. if not (cmpop) then
  646. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  647. if not (cs_check_overflow in aktlocalswitches) or (cmpop) or
  648. (nodetype in [orn, andn, xorn]) then begin
  649. case nodetype of
  650. addn, muln, xorn, orn, andn:
  651. begin
  652. case nodetype of
  653. addn:
  654. cgop := OP_ADD;
  655. muln:
  656. if unsigned then
  657. cgop := OP_MUL
  658. else
  659. cgop := OP_IMUL;
  660. xorn:
  661. cgop := OP_XOR;
  662. orn:
  663. cgop := OP_OR;
  664. andn:
  665. cgop := OP_AND;
  666. end;
  667. if (left.location.loc = LOC_CONSTANT) then
  668. swapleftright;
  669. if (right.location.loc <> LOC_CONSTANT) then
  670. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  671. left.location.register, right.location.register,
  672. location.register)
  673. else
  674. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  675. right.location.value, left.location.register,
  676. location.register);
  677. end;
  678. subn:
  679. begin
  680. if (nf_swaped in flags) then
  681. swapleftright;
  682. if left.location.loc <> LOC_CONSTANT then
  683. if right.location.loc <> LOC_CONSTANT then begin
  684. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  685. right.location.register, left.location.register,
  686. location.register);
  687. end else begin
  688. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  689. right.location.value, left.location.register,
  690. location.register);
  691. end
  692. else
  693. begin
  694. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  695. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT,
  696. left.location.value, tmpreg);
  697. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  698. right.location.register, tmpreg, location.register);
  699. end;
  700. end;
  701. ltn, lten, gtn, gten, equaln, unequaln:
  702. begin
  703. {$ifdef extdebug}
  704. current_asmdata.CurrAsmList.concat(tai_comment.create('tppcaddnode.pass2'));
  705. {$endif extdebug}
  706. emit_compare(unsigned);
  707. end;
  708. end;
  709. end
  710. else
  711. // overflow checking is on and we have an addn, subn or muln
  712. begin
  713. if is_signed(resulttype.def) then
  714. begin
  715. case nodetype of
  716. addn:
  717. op := A_ADDO;
  718. subn:
  719. begin
  720. op := A_SUBO;
  721. if (nf_swaped in flags) then
  722. swapleftright;
  723. end;
  724. muln:
  725. op := A_MULLDO;
  726. else
  727. internalerror(2002072601);
  728. end;
  729. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op, location.register,
  730. left.location.register, right.location.register));
  731. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  732. end
  733. else
  734. begin
  735. case nodetype of
  736. addn:
  737. begin
  738. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD, location.register,
  739. left.location.register, right.location.register));
  740. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD, location.register,
  741. left.location.register));
  742. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  743. end;
  744. subn:
  745. begin
  746. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB, location.register,
  747. left.location.register, right.location.register));
  748. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD,
  749. left.location.register, location.register));
  750. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  751. end;
  752. muln:
  753. begin
  754. { calculate the upper 64 bits of the product, = 0 if no overflow }
  755. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_R0);
  756. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU_, NR_R0,
  757. left.location.register, right.location.register));
  758. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_R0);
  759. { calculate the real result }
  760. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLD, location.register,
  761. left.location.register, right.location.register));
  762. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  763. current_asmdata.getjumplabel(hl);
  764. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, OC_EQ, hl);
  765. cg.a_call_name(current_asmdata.CurrAsmList, 'FPC_OVERFLOW');
  766. cg.a_label(current_asmdata.CurrAsmList, hl);
  767. end;
  768. end;
  769. end;
  770. end;
  771. end;
  772. begin
  773. caddnode := tppcaddnode;
  774. end.