nppcadd.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. if not cmpop then
  94. begin
  95. location.register := n.location.register;
  96. end;
  97. end;
  98. LOC_CONSTANT:
  99. begin
  100. if load_constants then
  101. begin
  102. location_force_reg(current_asmdata.CurrAsmList, n.location,
  103. def_cgsize(n.resulttype.def), false);
  104. if not cmpop then
  105. location.register := n.location.register;
  106. end;
  107. end;
  108. else
  109. location_force_reg(current_asmdata.CurrAsmList,n.location,def_cgsize(n.resulttype.def),false);
  110. end;
  111. end;
  112. begin
  113. load_node(left);
  114. load_node(right);
  115. if not (cmpop) and
  116. (location.register = NR_NO) then
  117. begin
  118. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  119. end;
  120. end;
  121. function tppcaddnode.getresflags: tresflags;
  122. begin
  123. if (left.resulttype.def.deftype <> floatdef) then
  124. result.cr := RS_CR0
  125. else
  126. result.cr := RS_CR1;
  127. case nodetype of
  128. equaln: result.flag := F_EQ;
  129. unequaln: result.flag := F_NE;
  130. else
  131. if nf_swaped in flags then
  132. case nodetype of
  133. ltn: result.flag := F_GT;
  134. lten: result.flag := F_GE;
  135. gtn: result.flag := F_LT;
  136. gten: result.flag := F_LE;
  137. end
  138. else
  139. case nodetype of
  140. ltn: result.flag := F_LT;
  141. lten: result.flag := F_LE;
  142. gtn: result.flag := F_GT;
  143. gten: result.flag := F_GE;
  144. end;
  145. end
  146. end;
  147. procedure tppcaddnode.emit_compare(unsigned: boolean);
  148. var
  149. op: tasmop;
  150. tmpreg: tregister;
  151. useconst: boolean;
  152. begin
  153. // get the constant on the right if there is one
  154. if (left.location.loc = LOC_CONSTANT) then
  155. swapleftright;
  156. {$IFDEF EXTDEBUG}
  157. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('tppcaddnode.emit_compare ' + inttostr(ord(opsize)) + ' ' + inttostr(tcgsize2size[opsize]))));
  158. {$ENDIF EXTDEBUG}
  159. // can we use an immediate, or do we have to load the
  160. // constant in a register first?
  161. if (right.location.loc = LOC_CONSTANT) then begin
  162. if (nodetype in [equaln, unequaln]) then
  163. if (unsigned and
  164. (aword(right.location.value) > high(word))) or
  165. (not unsigned and
  166. (aint(right.location.value) < low(smallint)) or
  167. (aint(right.location.value) > high(smallint))) then
  168. { we can then maybe use a constant in the 'othersigned' case
  169. (the sign doesn't matter for // equal/unequal)}
  170. unsigned := not unsigned;
  171. if (unsigned and
  172. (aword(right.location.value) <= high(word))) or
  173. (not (unsigned) and
  174. (aint(right.location.value) >= low(smallint)) and
  175. (aint(right.location.value) <= high(smallint))) then
  176. useconst := true
  177. else begin
  178. useconst := false;
  179. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  180. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, right.location.value, tmpreg);
  181. end
  182. end else
  183. useconst := false;
  184. location.loc := LOC_FLAGS;
  185. location.resflags := getresflags;
  186. if not unsigned then
  187. if useconst then
  188. op := A_CMPDI
  189. else
  190. op := A_CMPD
  191. else if useconst then
  192. op := A_CMPLDI
  193. else
  194. op := A_CMPLD;
  195. if (right.location.loc = LOC_CONSTANT) then begin
  196. if useconst then
  197. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op, left.location.register,
  198. longint(right.location.value)))
  199. else
  200. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op, left.location.register, tmpreg));
  201. end else
  202. current_asmdata.CurrAsmList.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 := current_procinfo.CurrTrueLabel;
  236. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  237. ofl := current_procinfo.CurrFalseLabel;
  238. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  239. end;
  240. secondpass(left);
  241. if left.location.loc in [LOC_FLAGS, LOC_JUMP] then
  242. location_force_reg(current_asmdata.CurrAsmList, left.location, cgsize, false);
  243. if isjump then
  244. begin
  245. current_procinfo.CurrTrueLabel := otl;
  246. current_procinfo.CurrFalseLabel := 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 := current_procinfo.CurrTrueLabel;
  254. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  255. ofl := current_procinfo.CurrFalseLabel;
  256. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  257. end;
  258. secondpass(right);
  259. if right.location.loc in [LOC_FLAGS, LOC_JUMP] then
  260. location_force_reg(current_asmdata.CurrAsmList, right.location, cgsize, false);
  261. if isjump then
  262. begin
  263. current_procinfo.CurrTrueLabel := otl;
  264. current_procinfo.CurrFalseLabel := 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. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,
  284. left.location.register, right.location.register))
  285. else
  286. current_asmdata.CurrAsmList.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(current_asmdata.CurrAsmList, cgop, OS_INT,
  304. left.location.register, right.location.register,
  305. location.register)
  306. else
  307. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, 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 := current_procinfo.CurrTrueLabel;
  326. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  327. secondpass(left);
  328. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  329. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrTrueLabel);
  330. current_procinfo.CurrTrueLabel := otl;
  331. end;
  332. orn:
  333. begin
  334. ofl := current_procinfo.CurrFalseLabel;
  335. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  336. secondpass(left);
  337. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  338. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrFalseLabel);
  339. current_procinfo.CurrFalseLabel := ofl;
  340. end;
  341. else
  342. internalerror(200403181);
  343. end;
  344. secondpass(right);
  345. maketojumpbool(current_asmdata.CurrAsmList, 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(current_asmdata.CurrAsmList, right.location, true);
  384. location_force_fpureg(current_asmdata.CurrAsmList, 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(current_asmdata.CurrAsmList, 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. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  405. location.register, left.location.register,
  406. right.location.register))
  407. end
  408. else
  409. begin
  410. current_asmdata.CurrAsmList.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(current_asmdata.CurrAsmList, OS_64);
  445. {$ifdef extdebug}
  446. astring := 'addsmallset0 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  447. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  448. {$endif extdebug}
  449. case nodetype of
  450. addn:
  451. begin
  452. if (nf_swaped in flags) and (left.nodetype = setelementn) then
  453. swapleftright;
  454. { are we adding set elements ? }
  455. if right.nodetype = setelementn then begin
  456. { no range support for smallsets! }
  457. if assigned(tsetelementnode(right).right) then
  458. internalerror(43244);
  459. if (right.location.loc = LOC_CONSTANT) then begin
  460. {$ifdef extdebug}
  461. astring := 'addsmallset1 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  462. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  463. {$endif extdebug}
  464. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64,
  465. aint(1) shl aint(right.location.value),
  466. left.location.register, location.register)
  467. end else
  468. begin
  469. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  470. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64, 1, tmpreg);
  471. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_64,
  472. right.location.register, tmpreg);
  473. if left.location.loc <> LOC_CONSTANT then begin
  474. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64, tmpreg,
  475. left.location.register, location.register)
  476. end else begin
  477. {$ifdef extdebug}
  478. astring := 'addsmallset2 ' + inttostr(left.location.value);
  479. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  480. {$endif extdebug}
  481. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64,
  482. left.location.value, tmpreg, location.register);
  483. end;
  484. end;
  485. opdone := true;
  486. end else begin
  487. cgop := OP_OR;
  488. end;
  489. end;
  490. symdifn:
  491. cgop := OP_XOR;
  492. muln:
  493. cgop := OP_AND;
  494. subn:
  495. begin
  496. cgop := OP_AND;
  497. if (not (nf_swaped in flags)) then
  498. if (right.location.loc = LOC_CONSTANT) then
  499. right.location.value := not (right.location.value)
  500. else
  501. opdone := true
  502. else if (left.location.loc = LOC_CONSTANT) then
  503. left.location.value := not (left.location.value)
  504. else begin
  505. swapleftright;
  506. opdone := true;
  507. end;
  508. if opdone then begin
  509. if left.location.loc = LOC_CONSTANT then
  510. begin
  511. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  512. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  513. left.location.value, tmpreg);
  514. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  515. location.register, tmpreg, right.location.register));
  516. end
  517. else
  518. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  519. location.register, left.location.register,
  520. right.location.register));
  521. end;
  522. end;
  523. equaln,
  524. unequaln:
  525. begin
  526. emit_compare(true);
  527. opdone := true;
  528. end;
  529. lten, gten:
  530. begin
  531. if (not (nf_swaped in flags) and
  532. (nodetype = lten)) or
  533. ((nf_swaped in flags) and
  534. (nodetype = gten)) then
  535. swapleftright;
  536. // now we have to check whether left >= right
  537. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  538. if left.location.loc = LOC_CONSTANT then begin
  539. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_64,
  540. not (left.location.value), right.location.register, tmpreg);
  541. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPDI, tmpreg, 0));
  542. // the two instructions above should be folded together by
  543. // the peepholeoptimizer
  544. end else begin
  545. if right.location.loc = LOC_CONSTANT then begin
  546. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  547. right.location.value, tmpreg);
  548. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  549. tmpreg, left.location.register));
  550. end else
  551. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  552. right.location.register, left.location.register));
  553. end;
  554. location.resflags.cr := RS_CR0;
  555. location.resflags.flag := F_EQ;
  556. opdone := true;
  557. end;
  558. else
  559. internalerror(2002072701);
  560. end;
  561. if not opdone then begin
  562. // these are all commutative operations
  563. if (left.location.loc = LOC_CONSTANT) then
  564. swapleftright;
  565. if (right.location.loc = LOC_CONSTANT) then begin
  566. astring := 'addsmallset4 ' + inttostr(right.location.value);
  567. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  568. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  569. right.location.value, left.location.register,
  570. location.register)
  571. end else begin
  572. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  573. right.location.register, left.location.register,
  574. location.register);
  575. end;
  576. end;
  577. end;
  578. {*****************************************************************************
  579. pass_2
  580. *****************************************************************************}
  581. procedure tppcaddnode.pass_2;
  582. { is also being used for xor, and "mul", "sub, or and comparative }
  583. { operators }
  584. var
  585. cgop: topcg;
  586. op: tasmop;
  587. tmpreg: tregister;
  588. hl: tasmlabel;
  589. cmpop: boolean;
  590. { true, if unsigned types are compared }
  591. unsigned: boolean;
  592. begin
  593. { to make it more readable, string and set (not smallset!) have their
  594. own procedures }
  595. case left.resulttype.def.deftype of
  596. orddef:
  597. begin
  598. { handling boolean expressions }
  599. if is_boolean(left.resulttype.def) and
  600. is_boolean(right.resulttype.def) then
  601. begin
  602. second_addboolean;
  603. exit;
  604. end;
  605. end;
  606. stringdef:
  607. begin
  608. internalerror(2002072402);
  609. exit;
  610. end;
  611. setdef:
  612. begin
  613. { normalsets are already handled in pass1 }
  614. if (tsetdef(left.resulttype.def).settype <> smallset) then
  615. internalerror(200109041);
  616. second_addsmallset;
  617. exit;
  618. end;
  619. arraydef:
  620. begin
  621. {$IFDEF SUPPORT_MMX}
  622. if is_mmx_able_array(left.resulttype.def) then
  623. begin
  624. second_addmmx;
  625. exit;
  626. end;
  627. {$ENDIF SUPPORT_MMX}
  628. end;
  629. floatdef:
  630. begin
  631. second_addfloat;
  632. exit;
  633. end;
  634. end;
  635. { defaults }
  636. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  637. unsigned := not (is_signed(left.resulttype.def)) or
  638. not (is_signed(right.resulttype.def));
  639. pass_left_and_right;
  640. { Convert flags to register first }
  641. { can any of these things be in the flags actually?? (JM) }
  642. if (left.location.loc = LOC_FLAGS) or
  643. (right.location.loc = LOC_FLAGS) then
  644. internalerror(2002072602);
  645. { set result location }
  646. if not cmpop then
  647. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  648. else
  649. location_reset(location, LOC_FLAGS, OS_NO);
  650. load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  651. (nodetype in [addn, subn, muln]));
  652. if (location.register = NR_NO) and
  653. not (cmpop) then
  654. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  655. if not (cs_check_overflow in aktlocalswitches) or (cmpop) or
  656. (nodetype in [orn, andn, xorn]) then
  657. begin
  658. case nodetype of
  659. addn, muln, xorn, orn, andn:
  660. begin
  661. case nodetype of
  662. addn:
  663. cgop := OP_ADD;
  664. muln:
  665. if unsigned then
  666. cgop := OP_MUL
  667. else
  668. cgop := OP_IMUL;
  669. xorn:
  670. cgop := OP_XOR;
  671. orn:
  672. cgop := OP_OR;
  673. andn:
  674. cgop := OP_AND;
  675. end;
  676. if (left.location.loc = LOC_CONSTANT) then
  677. swapleftright;
  678. if (right.location.loc <> LOC_CONSTANT) then
  679. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  680. left.location.register, right.location.register,
  681. location.register)
  682. else
  683. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  684. right.location.value, left.location.register,
  685. location.register);
  686. end;
  687. subn:
  688. begin
  689. if (nf_swaped in flags) then
  690. swapleftright;
  691. if left.location.loc <> LOC_CONSTANT then
  692. if right.location.loc <> LOC_CONSTANT then begin
  693. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  694. right.location.register, left.location.register,
  695. location.register);
  696. end else begin
  697. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  698. right.location.value, left.location.register,
  699. location.register);
  700. end
  701. else
  702. begin
  703. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  704. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT,
  705. left.location.value, tmpreg);
  706. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  707. right.location.register, tmpreg, location.register);
  708. end;
  709. end;
  710. ltn, lten, gtn, gten, equaln, unequaln:
  711. begin
  712. {$ifdef extdebug}
  713. current_asmdata.CurrAsmList.concat(tai_comment.create('tppcaddnode.pass2'));
  714. {$endif extdebug}
  715. emit_compare(unsigned);
  716. end;
  717. end;
  718. end
  719. else
  720. // overflow checking is on and we have an addn, subn or muln
  721. begin
  722. if is_signed(resulttype.def) then
  723. begin
  724. case nodetype of
  725. addn:
  726. op := A_ADDO;
  727. subn:
  728. begin
  729. op := A_SUBO;
  730. if (nf_swaped in flags) then
  731. swapleftright;
  732. end;
  733. muln:
  734. op := A_MULLDO;
  735. else
  736. internalerror(2002072601);
  737. end;
  738. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op, location.register,
  739. left.location.register, right.location.register));
  740. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  741. end
  742. else
  743. begin
  744. case nodetype of
  745. addn:
  746. begin
  747. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD, location.register,
  748. left.location.register, right.location.register));
  749. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD, location.register,
  750. left.location.register));
  751. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  752. end;
  753. subn:
  754. begin
  755. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB, location.register,
  756. left.location.register, right.location.register));
  757. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD,
  758. left.location.register, location.register));
  759. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  760. end;
  761. muln:
  762. begin
  763. { calculate the upper 64 bits of the product, = 0 if no overflow }
  764. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_R0);
  765. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU_, NR_R0,
  766. left.location.register, right.location.register));
  767. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_R0);
  768. { calculate the real result }
  769. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLD, location.register,
  770. left.location.register, right.location.register));
  771. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  772. current_asmdata.getjumplabel(hl);
  773. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, OC_EQ, hl);
  774. cg.a_call_name(current_asmdata.CurrAsmList, 'FPC_OVERFLOW');
  775. cg.a_label(current_asmdata.CurrAsmList, hl);
  776. end;
  777. end;
  778. end;
  779. end;
  780. end;
  781. begin
  782. caddnode := tppcaddnode;
  783. end.