nppcadd.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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, aasmcpu, defutil, htypechk,
  42. cgbase, cpuinfo, pass_1, pass_2, regvars,
  43. cpupara, cgcpu, cgutils,
  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_REGISTER:
  83. if not cmpop then
  84. begin
  85. location.register := n.location.register;
  86. end;
  87. LOC_REFERENCE, LOC_CREFERENCE:
  88. begin
  89. location_force_reg(exprasmlist, n.location,
  90. def_cgsize(n.resulttype.def), false);
  91. if not cmpop then
  92. begin
  93. location.register := n.location.register;
  94. end;
  95. end;
  96. LOC_CONSTANT:
  97. begin
  98. if load_constants then
  99. begin
  100. location_force_reg(exprasmlist, n.location,
  101. def_cgsize(n.resulttype.def), false);
  102. if not cmpop then
  103. location.register := n.location.register;
  104. end;
  105. end;
  106. end;
  107. end;
  108. begin
  109. load_node(left);
  110. load_node(right);
  111. if not (cmpop) and
  112. (location.register = NR_NO) then
  113. begin
  114. location.register := cg.getintregister(exprasmlist, OS_INT);
  115. end;
  116. end;
  117. function tppcaddnode.getresflags: tresflags;
  118. begin
  119. if (left.resulttype.def.deftype <> floatdef) then
  120. result.cr := RS_CR0
  121. else
  122. result.cr := RS_CR1;
  123. case nodetype of
  124. equaln: result.flag := F_EQ;
  125. unequaln: result.flag := F_NE;
  126. else
  127. if nf_swaped in flags then
  128. case nodetype of
  129. ltn: result.flag := F_GT;
  130. lten: result.flag := F_GE;
  131. gtn: result.flag := F_LT;
  132. gten: result.flag := F_LE;
  133. end
  134. else
  135. case nodetype of
  136. ltn: result.flag := F_LT;
  137. lten: result.flag := F_LE;
  138. gtn: result.flag := F_GT;
  139. gten: result.flag := F_GE;
  140. end;
  141. end
  142. end;
  143. // Todo: ts: allow emiting word compares...
  144. procedure tppcaddnode.emit_compare(unsigned: boolean);
  145. var
  146. op: tasmop;
  147. tmpreg: tregister;
  148. useconst: boolean;
  149. begin
  150. // get the constant on the right if there is one
  151. if (left.location.loc = LOC_CONSTANT) then
  152. swapleftright;
  153. // can we use an immediate, or do we have to load the
  154. // constant in a register first?
  155. if (right.location.loc = LOC_CONSTANT) then
  156. begin
  157. if (nodetype in [equaln, unequaln]) then
  158. if (unsigned and
  159. (aword(right.location.value) > high(word))) or
  160. (not unsigned and
  161. (aint(right.location.value) < low(smallint)) or
  162. (aint(right.location.value) > high(smallint))) then
  163. { we can then maybe use a constant in the 'othersigned' case
  164. (the sign doesn't matter for // equal/unequal)}
  165. unsigned := not unsigned;
  166. if (unsigned and
  167. (aword(right.location.value) <= high(word))) or
  168. (not (unsigned) and
  169. (aint(right.location.value) >= low(smallint)) and
  170. (aint(right.location.value) <= high(smallint))) then
  171. useconst := true
  172. else
  173. begin
  174. useconst := false;
  175. tmpreg := cg.getintregister(exprasmlist, OS_INT);
  176. cg.a_load_const_reg(exprasmlist, OS_INT,
  177. right.location.value, tmpreg);
  178. end
  179. end
  180. else
  181. useconst := false;
  182. location.loc := LOC_FLAGS;
  183. location.resflags := getresflags;
  184. if not unsigned then
  185. if useconst then
  186. op := A_CMPDI
  187. else
  188. op := A_CMPD
  189. else if useconst then
  190. op := A_CMPLDI
  191. else
  192. op := A_CMPLD;
  193. if (right.location.loc = LOC_CONSTANT) then
  194. begin
  195. if useconst then
  196. exprasmlist.concat(taicpu.op_reg_const(op, left.location.register,
  197. longint(right.location.value)))
  198. else
  199. exprasmlist.concat(taicpu.op_reg_reg(op, left.location.register, tmpreg));
  200. end
  201. else
  202. exprasmlist.concat(taicpu.op_reg_reg(op,
  203. left.location.register, right.location.register));
  204. end;
  205. {*****************************************************************************
  206. AddBoolean
  207. *****************************************************************************}
  208. procedure tppcaddnode.second_addboolean;
  209. var
  210. cgop: TOpCg;
  211. cgsize: TCgSize;
  212. cmpop,
  213. isjump: boolean;
  214. otl, ofl: tasmlabel;
  215. begin
  216. { calculate the operator which is more difficult }
  217. firstcomplex(self);
  218. cmpop := false;
  219. if (torddef(left.resulttype.def).typ = bool8bit) or
  220. (torddef(right.resulttype.def).typ = bool8bit) then
  221. cgsize := OS_8
  222. else if (torddef(left.resulttype.def).typ = bool16bit) or
  223. (torddef(right.resulttype.def).typ = bool16bit) then
  224. cgsize := OS_16
  225. else
  226. cgsize := OS_32;
  227. if (cs_full_boolean_eval in aktlocalswitches) or
  228. (nodetype in [unequaln, ltn, lten, gtn, gten, equaln, xorn]) then
  229. begin
  230. if left.nodetype in [ordconstn, realconstn] then
  231. swapleftright;
  232. isjump := (left.expectloc = LOC_JUMP);
  233. if isjump then
  234. begin
  235. otl := truelabel;
  236. objectlibrary.getjumplabel(truelabel);
  237. ofl := falselabel;
  238. objectlibrary.getjumplabel(falselabel);
  239. end;
  240. secondpass(left);
  241. if left.location.loc in [LOC_FLAGS, LOC_JUMP] then
  242. location_force_reg(exprasmlist, left.location, cgsize, false);
  243. if isjump then
  244. begin
  245. truelabel := otl;
  246. falselabel := ofl;
  247. end
  248. else if left.location.loc = LOC_JUMP then
  249. internalerror(2003122901);
  250. isjump := (right.expectloc = LOC_JUMP);
  251. if isjump then
  252. begin
  253. otl := truelabel;
  254. objectlibrary.getjumplabel(truelabel);
  255. ofl := falselabel;
  256. objectlibrary.getjumplabel(falselabel);
  257. end;
  258. secondpass(right);
  259. if right.location.loc in [LOC_FLAGS, LOC_JUMP] then
  260. location_force_reg(exprasmlist, right.location, cgsize, false);
  261. if isjump then
  262. begin
  263. truelabel := otl;
  264. falselabel := ofl;
  265. end
  266. else if right.location.loc = LOC_JUMP then
  267. internalerror(200312292);
  268. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  269. { set result location }
  270. if not cmpop then
  271. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  272. else
  273. location_reset(location, LOC_FLAGS, OS_NO);
  274. load_left_right(cmpop, false);
  275. if (left.location.loc = LOC_CONSTANT) then
  276. swapleftright;
  277. { compare the }
  278. case nodetype of
  279. ltn, lten, gtn, gten,
  280. equaln, unequaln:
  281. begin
  282. if (right.location.loc <> LOC_CONSTANT) then
  283. exprasmlist.concat(taicpu.op_reg_reg(A_CMPLW,
  284. left.location.register, right.location.register))
  285. else
  286. exprasmlist.concat(taicpu.op_reg_const(A_CMPLWI,
  287. left.location.register, longint(right.location.value)));
  288. location.resflags := getresflags;
  289. end;
  290. else
  291. begin
  292. case nodetype of
  293. xorn:
  294. cgop := OP_XOR;
  295. orn:
  296. cgop := OP_OR;
  297. andn:
  298. cgop := OP_AND;
  299. else
  300. internalerror(200203247);
  301. end;
  302. if right.location.loc <> LOC_CONSTANT then
  303. cg.a_op_reg_reg_reg(exprasmlist, cgop, OS_INT,
  304. left.location.register, right.location.register,
  305. location.register)
  306. else
  307. cg.a_op_const_reg_reg(exprasmlist, cgop, OS_INT,
  308. right.location.value, left.location.register,
  309. location.register);
  310. end;
  311. end;
  312. end
  313. else
  314. begin
  315. // just to make sure we free the right registers
  316. cmpop := true;
  317. case nodetype of
  318. andn,
  319. orn:
  320. begin
  321. location_reset(location, LOC_JUMP, OS_NO);
  322. case nodetype of
  323. andn:
  324. begin
  325. otl := truelabel;
  326. objectlibrary.getjumplabel(truelabel);
  327. secondpass(left);
  328. maketojumpbool(exprasmlist, left, lr_load_regvars);
  329. cg.a_label(exprasmlist, truelabel);
  330. truelabel := otl;
  331. end;
  332. orn:
  333. begin
  334. ofl := falselabel;
  335. objectlibrary.getjumplabel(falselabel);
  336. secondpass(left);
  337. maketojumpbool(exprasmlist, left, lr_load_regvars);
  338. cg.a_label(exprasmlist, falselabel);
  339. falselabel := ofl;
  340. end;
  341. else
  342. internalerror(200403181);
  343. end;
  344. secondpass(right);
  345. maketojumpbool(exprasmlist, right, lr_load_regvars);
  346. end;
  347. end;
  348. end;
  349. end;
  350. {*****************************************************************************
  351. AddFloat
  352. *****************************************************************************}
  353. procedure tppcaddnode.second_addfloat;
  354. var
  355. op: TAsmOp;
  356. cmpop: boolean;
  357. begin
  358. pass_left_and_right;
  359. cmpop := false;
  360. case nodetype of
  361. addn:
  362. op := A_FADD;
  363. muln:
  364. op := A_FMUL;
  365. subn:
  366. op := A_FSUB;
  367. slashn:
  368. op := A_FDIV;
  369. ltn, lten, gtn, gten,
  370. equaln, unequaln:
  371. begin
  372. op := A_FCMPO;
  373. cmpop := true;
  374. end;
  375. else
  376. internalerror(200403182);
  377. end;
  378. // get the operands in the correct order, there are no special cases
  379. // here, everything is register-based
  380. if nf_swaped in flags then
  381. swapleftright;
  382. // put both operands in a register
  383. location_force_fpureg(exprasmlist, right.location, true);
  384. location_force_fpureg(exprasmlist, left.location, true);
  385. // initialize de result
  386. if not cmpop then
  387. begin
  388. location_reset(location, LOC_FPUREGISTER, def_cgsize(resulttype.def));
  389. if left.location.loc = LOC_FPUREGISTER then
  390. location.register := left.location.register
  391. else if right.location.loc = LOC_FPUREGISTER then
  392. location.register := right.location.register
  393. else
  394. location.register := cg.getfpuregister(exprasmlist, location.size);
  395. end
  396. else
  397. begin
  398. location_reset(location, LOC_FLAGS, OS_NO);
  399. location.resflags := getresflags;
  400. end;
  401. // emit the actual operation
  402. if not cmpop then
  403. begin
  404. exprasmlist.concat(taicpu.op_reg_reg_reg(op,
  405. location.register, left.location.register,
  406. right.location.register))
  407. end
  408. else
  409. begin
  410. exprasmlist.concat(taicpu.op_reg_reg_reg(op,
  411. newreg(R_SPECIALREGISTER, location.resflags.cr, R_SUBNONE),
  412. left.location.register, right.location.register))
  413. end;
  414. end;
  415. {*****************************************************************************
  416. AddSmallSet
  417. *****************************************************************************}
  418. procedure tppcaddnode.second_addsmallset;
  419. var
  420. cgop: TOpCg;
  421. tmpreg: tregister;
  422. opdone,
  423. cmpop: boolean;
  424. astring : string;
  425. // ts: todo - speed up by using 32 bit compares/adds/ands here
  426. begin
  427. pass_left_and_right;
  428. { when a setdef is passed, it has to be a smallset }
  429. if ((left.resulttype.def.deftype = setdef) and
  430. (tsetdef(left.resulttype.def).settype <> smallset)) or
  431. ((right.resulttype.def.deftype = setdef) and
  432. (tsetdef(right.resulttype.def).settype <> smallset)) then
  433. internalerror(200203301);
  434. opdone := false;
  435. cmpop := nodetype in [equaln, unequaln, lten, gten];
  436. { set result location }
  437. if not cmpop then
  438. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  439. else
  440. location_reset(location, LOC_FLAGS, OS_NO);
  441. load_left_right(cmpop, false);
  442. if not (cmpop) and
  443. (location.register = NR_NO) then
  444. location.register := cg.getintregister(exprasmlist, OS_64);
  445. astring := 'addsmallset0 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  446. exprasmlist.concat(tai_comment.create(strpnew(astring)));
  447. case nodetype of
  448. addn:
  449. begin
  450. if (nf_swaped in flags) and (left.nodetype = setelementn) then
  451. swapleftright;
  452. { are we adding set elements ? }
  453. if right.nodetype = setelementn then begin
  454. { no range support for smallsets! }
  455. if assigned(tsetelementnode(right).right) then
  456. internalerror(43244);
  457. if (right.location.loc = LOC_CONSTANT) then begin
  458. astring := 'addsmallset1 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  459. exprasmlist.concat(tai_comment.create(strpnew(astring)));
  460. cg.a_op_const_reg_reg(exprasmlist, OP_OR, OS_64,
  461. aint(1) shl aint(right.location.value),
  462. left.location.register, location.register)
  463. end else
  464. begin
  465. tmpreg := cg.getintregister(exprasmlist, OS_64);
  466. cg.a_load_const_reg(exprasmlist, OS_64, 1, tmpreg);
  467. cg.a_op_reg_reg(exprasmlist, OP_SHL, OS_64,
  468. right.location.register, tmpreg);
  469. if left.location.loc <> LOC_CONSTANT then begin
  470. cg.a_op_reg_reg_reg(exprasmlist, OP_OR, OS_64, tmpreg,
  471. left.location.register, location.register)
  472. end else begin
  473. astring := 'addsmallset2 ' + inttostr(left.location.value);
  474. exprasmlist.concat(tai_comment.create(strpnew(astring)));
  475. cg.a_op_const_reg_reg(exprasmlist, OP_OR, OS_64,
  476. left.location.value, tmpreg, location.register);
  477. end;
  478. end;
  479. opdone := true;
  480. end else begin
  481. cgop := OP_OR;
  482. end;
  483. end;
  484. symdifn:
  485. cgop := OP_XOR;
  486. muln:
  487. cgop := OP_AND;
  488. subn:
  489. begin
  490. cgop := OP_AND;
  491. if (not (nf_swaped in flags)) then
  492. if (right.location.loc = LOC_CONSTANT) then
  493. right.location.value := not (right.location.value)
  494. else
  495. opdone := true
  496. else if (left.location.loc = LOC_CONSTANT) then
  497. left.location.value := not (left.location.value)
  498. else begin
  499. swapleftright;
  500. opdone := true;
  501. end;
  502. if opdone then begin
  503. if left.location.loc = LOC_CONSTANT then
  504. begin
  505. tmpreg := cg.getintregister(exprasmlist, OS_64);
  506. cg.a_load_const_reg(exprasmlist, OS_64,
  507. left.location.value, tmpreg);
  508. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC,
  509. location.register, tmpreg, right.location.register));
  510. end
  511. else
  512. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC,
  513. location.register, left.location.register,
  514. right.location.register));
  515. end;
  516. end;
  517. equaln,
  518. unequaln:
  519. begin
  520. emit_compare(true);
  521. opdone := true;
  522. end;
  523. lten, gten:
  524. begin
  525. if (not (nf_swaped in flags) and
  526. (nodetype = lten)) or
  527. ((nf_swaped in flags) and
  528. (nodetype = gten)) then
  529. swapleftright;
  530. // now we have to check whether left >= right
  531. tmpreg := cg.getintregister(exprasmlist, OS_64);
  532. if left.location.loc = LOC_CONSTANT then begin
  533. cg.a_op_const_reg_reg(exprasmlist, OP_AND, OS_64,
  534. not (left.location.value), right.location.register, tmpreg);
  535. exprasmlist.concat(taicpu.op_reg_const(A_CMPDI, tmpreg, 0));
  536. // the two instructions above should be folded together by
  537. // the peepholeoptimizer
  538. end else begin
  539. if right.location.loc = LOC_CONSTANT then begin
  540. cg.a_load_const_reg(exprasmlist, OS_64,
  541. right.location.value, tmpreg);
  542. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  543. tmpreg, left.location.register));
  544. end else
  545. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  546. right.location.register, left.location.register));
  547. end;
  548. location.resflags.cr := RS_CR0;
  549. location.resflags.flag := F_EQ;
  550. opdone := true;
  551. end;
  552. else
  553. internalerror(2002072701);
  554. end;
  555. if not opdone then begin
  556. // these are all commutative operations
  557. if (left.location.loc = LOC_CONSTANT) then
  558. swapleftright;
  559. if (right.location.loc = LOC_CONSTANT) then begin
  560. astring := 'addsmallset4 ' + inttostr(right.location.value);
  561. exprasmlist.concat(tai_comment.create(strpnew(astring)));
  562. cg.a_op_const_reg_reg(exprasmlist, cgop, OS_64,
  563. right.location.value, left.location.register,
  564. location.register)
  565. end else begin
  566. cg.a_op_reg_reg_reg(exprasmlist, cgop, OS_64,
  567. right.location.register, left.location.register,
  568. location.register);
  569. end;
  570. end;
  571. end;
  572. {*****************************************************************************
  573. pass_2
  574. *****************************************************************************}
  575. procedure tppcaddnode.pass_2;
  576. { is also being used for xor, and "mul", "sub, or and comparative }
  577. { operators }
  578. var
  579. cgop: topcg;
  580. op: tasmop;
  581. tmpreg: tregister;
  582. hl: tasmlabel;
  583. cmpop: boolean;
  584. { true, if unsigned types are compared }
  585. unsigned: boolean;
  586. begin
  587. { to make it more readable, string and set (not smallset!) have their
  588. own procedures }
  589. case left.resulttype.def.deftype of
  590. orddef:
  591. begin
  592. { handling boolean expressions }
  593. if is_boolean(left.resulttype.def) and
  594. is_boolean(right.resulttype.def) then
  595. begin
  596. second_addboolean;
  597. exit;
  598. end;
  599. end;
  600. stringdef:
  601. begin
  602. internalerror(2002072402);
  603. exit;
  604. end;
  605. setdef:
  606. begin
  607. { normalsets are already handled in pass1 }
  608. if (tsetdef(left.resulttype.def).settype <> smallset) then
  609. internalerror(200109041);
  610. second_addsmallset;
  611. exit;
  612. end;
  613. arraydef:
  614. begin
  615. {$IFDEF SUPPORT_MMX}
  616. if is_mmx_able_array(left.resulttype.def) then
  617. begin
  618. second_addmmx;
  619. exit;
  620. end;
  621. {$ENDIF SUPPORT_MMX}
  622. end;
  623. floatdef:
  624. begin
  625. second_addfloat;
  626. exit;
  627. end;
  628. end;
  629. { defaults }
  630. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  631. unsigned := not (is_signed(left.resulttype.def)) or
  632. not (is_signed(right.resulttype.def));
  633. pass_left_and_right;
  634. { Convert flags to register first }
  635. { can any of these things be in the flags actually?? (JM) }
  636. if (left.location.loc = LOC_FLAGS) or
  637. (right.location.loc = LOC_FLAGS) then
  638. internalerror(2002072602);
  639. { set result location }
  640. if not cmpop then
  641. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  642. else
  643. location_reset(location, LOC_FLAGS, OS_NO);
  644. load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  645. (nodetype in [addn, subn, muln]));
  646. if (location.register = NR_NO) and
  647. not (cmpop) then
  648. location.register := cg.getintregister(exprasmlist, OS_INT);
  649. if not (cs_check_overflow in aktlocalswitches) or
  650. (cmpop) or
  651. (nodetype in [orn, andn, xorn]) then
  652. begin
  653. case nodetype of
  654. addn, muln, xorn, orn, andn:
  655. begin
  656. case nodetype of
  657. addn:
  658. cgop := OP_ADD;
  659. muln:
  660. if unsigned then
  661. cgop := OP_MUL
  662. else
  663. cgop := OP_IMUL;
  664. xorn:
  665. cgop := OP_XOR;
  666. orn:
  667. cgop := OP_OR;
  668. andn:
  669. cgop := OP_AND;
  670. end;
  671. if (left.location.loc = LOC_CONSTANT) then
  672. swapleftright;
  673. if (right.location.loc <> LOC_CONSTANT) then
  674. cg.a_op_reg_reg_reg(exprasmlist, cgop, OS_INT,
  675. left.location.register, right.location.register,
  676. location.register)
  677. else
  678. cg.a_op_const_reg_reg(exprasmlist, cgop, OS_INT,
  679. right.location.value, left.location.register,
  680. location.register);
  681. end;
  682. subn:
  683. begin
  684. if (nf_swaped in flags) then
  685. swapleftright;
  686. if left.location.loc <> LOC_CONSTANT then
  687. if right.location.loc <> LOC_CONSTANT then begin
  688. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT,
  689. right.location.register, left.location.register,
  690. location.register);
  691. end else begin
  692. cg.a_op_const_reg_reg(exprasmlist, OP_SUB, OS_INT,
  693. right.location.value, left.location.register,
  694. location.register);
  695. end
  696. else
  697. begin
  698. tmpreg := cg.getintregister(exprasmlist, OS_INT);
  699. cg.a_load_const_reg(exprasmlist, OS_INT,
  700. left.location.value, tmpreg);
  701. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT,
  702. right.location.register, tmpreg, location.register);
  703. end;
  704. end;
  705. ltn, lten, gtn, gten, equaln, unequaln:
  706. begin
  707. emit_compare(unsigned);
  708. end;
  709. end;
  710. end
  711. else
  712. // overflow checking is on and we have an addn, subn or muln
  713. begin
  714. if is_signed(resulttype.def) then
  715. begin
  716. case nodetype of
  717. addn:
  718. op := A_ADDO;
  719. subn:
  720. begin
  721. op := A_SUBO;
  722. if (nf_swaped in flags) then
  723. swapleftright;
  724. end;
  725. muln:
  726. op := A_MULLDO;
  727. else
  728. internalerror(2002072601);
  729. end;
  730. exprasmlist.concat(taicpu.op_reg_reg_reg(op, location.register,
  731. left.location.register, right.location.register));
  732. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  733. end
  734. else
  735. begin
  736. case nodetype of
  737. addn:
  738. begin
  739. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ADD, location.register,
  740. left.location.register, right.location.register));
  741. exprasmlist.concat(taicpu.op_reg_reg(A_CMPLD, location.register,
  742. left.location.register));
  743. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  744. end;
  745. subn:
  746. begin
  747. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB, location.register,
  748. left.location.register, right.location.register));
  749. exprasmlist.concat(taicpu.op_reg_reg(A_CMPLD,
  750. left.location.register, location.register));
  751. cg.g_overflowcheck(exprasmlist, location, resulttype.def);
  752. end;
  753. muln:
  754. begin
  755. { calculate the upper 64 bits of the product, = 0 if no overflow }
  756. cg.a_reg_alloc(exprasmlist, NR_R0);
  757. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULHDU_, NR_R0,
  758. left.location.register, right.location.register));
  759. cg.a_reg_dealloc(exprasmlist, NR_R0);
  760. { calculate the real result }
  761. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLD, location.register,
  762. left.location.register, right.location.register));
  763. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  764. objectlibrary.getjumplabel(hl);
  765. tcgppc(cg).a_jmp_cond(exprasmlist, OC_EQ, hl);
  766. cg.a_call_name(exprasmlist, 'FPC_OVERFLOW');
  767. cg.a_label(exprasmlist, hl);
  768. end;
  769. end;
  770. end;
  771. end;
  772. end;
  773. begin
  774. caddnode := tppcaddnode;
  775. end.