nppcadd.pas 27 KB

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