nppcadd.pas 25 KB

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