nppcadd.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the PowerPC64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nppcadd;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. node, nadd, ncgadd, cpubase;
  22. type
  23. tppcaddnode = class(tcgaddnode)
  24. function pass_1: tnode; override;
  25. procedure pass_2; override;
  26. private
  27. procedure pass_left_and_right;
  28. procedure load_left_right(cmpop, load_constants: boolean);
  29. function getresflags: tresflags;
  30. procedure emit_compare(unsigned: boolean);
  31. procedure second_addfloat; override;
  32. procedure second_addboolean; override;
  33. procedure second_addsmallset; override;
  34. end;
  35. implementation
  36. uses
  37. sysutils,
  38. globtype, systems,
  39. cutils, verbose, globals,
  40. symconst, symdef, paramgr,
  41. aasmbase, aasmtai,aasmdata, aasmcpu, defutil, htypechk,
  42. cgbase, cpuinfo, pass_1, pass_2, regvars,
  43. cpupara, cgcpu, cgutils,procinfo,
  44. ncon, nset,
  45. ncgutil, tgobj, rgobj, rgcpu, cgobj;
  46. {*****************************************************************************
  47. Pass 1
  48. *****************************************************************************}
  49. function tppcaddnode.pass_1: tnode;
  50. begin
  51. resulttypepass(left);
  52. if (nodetype in [equaln, unequaln]) and
  53. (left.resulttype.def.deftype = orddef) {and
  54. is_64bit(left.resulttype.def)}then
  55. begin
  56. result := nil;
  57. firstpass(left);
  58. firstpass(right);
  59. expectloc := LOC_FLAGS;
  60. calcregisters(self, 2, 0, 0);
  61. exit;
  62. end;
  63. result := inherited pass_1;
  64. end;
  65. {*****************************************************************************
  66. Helpers
  67. *****************************************************************************}
  68. procedure tppcaddnode.pass_left_and_right;
  69. begin
  70. { calculate the operator which is more difficult }
  71. firstcomplex(self);
  72. { in case of constant put it to the left }
  73. if (left.nodetype = ordconstn) then
  74. swapleftright;
  75. secondpass(left);
  76. secondpass(right);
  77. end;
  78. procedure tppcaddnode.load_left_right(cmpop, load_constants: boolean);
  79. procedure load_node(var n: tnode);
  80. begin
  81. case n.location.loc of
  82. LOC_CREGISTER:
  83. ;
  84. LOC_REGISTER:
  85. if not cmpop then
  86. begin
  87. location.register := n.location.register;
  88. end;
  89. LOC_REFERENCE, LOC_CREFERENCE:
  90. begin
  91. location_force_reg(current_asmdata.CurrAsmList, n.location,
  92. def_cgsize(n.resulttype.def), false);
  93. end;
  94. LOC_CONSTANT:
  95. begin
  96. if load_constants then begin
  97. location_force_reg(current_asmdata.CurrAsmList, n.location,
  98. def_cgsize(n.resulttype.def), false);
  99. end;
  100. end;
  101. else
  102. location_force_reg(current_asmdata.CurrAsmList,n.location,def_cgsize(n.resulttype.def),false);
  103. end;
  104. end;
  105. begin
  106. load_node(left);
  107. load_node(right);
  108. if not (cmpop) then begin
  109. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  110. end;
  111. end;
  112. function tppcaddnode.getresflags: tresflags;
  113. begin
  114. if (left.resulttype.def.deftype <> floatdef) then
  115. result.cr := RS_CR0
  116. else
  117. result.cr := RS_CR1;
  118. case nodetype of
  119. equaln: result.flag := F_EQ;
  120. unequaln: result.flag := F_NE;
  121. else
  122. if nf_swaped in flags then
  123. case nodetype of
  124. ltn: result.flag := F_GT;
  125. lten: result.flag := F_GE;
  126. gtn: result.flag := F_LT;
  127. gten: result.flag := F_LE;
  128. end
  129. else
  130. case nodetype of
  131. ltn: result.flag := F_LT;
  132. lten: result.flag := F_LE;
  133. gtn: result.flag := F_GT;
  134. gten: result.flag := F_GE;
  135. end;
  136. end
  137. end;
  138. procedure tppcaddnode.emit_compare(unsigned: boolean);
  139. var
  140. op: tasmop;
  141. tmpreg: tregister;
  142. useconst: boolean;
  143. {$IFDEF EXTDEBUG}
  144. opsize : TCgSize;
  145. {$ENDIF EXTDEBUG}
  146. begin
  147. // get the constant on the right if there is one
  148. if (left.location.loc = LOC_CONSTANT) then
  149. swapleftright;
  150. {$IFDEF EXTDEBUG}
  151. opsize := def_cgsize(left.resulttype.def);
  152. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('tppcaddnode.emit_compare ' + inttostr(ord(opsize)) + ' ' + inttostr(tcgsize2size[opsize]))));
  153. {$ENDIF EXTDEBUG}
  154. // can we use an immediate, or do we have to load the
  155. // constant in a register first?
  156. if (right.location.loc = LOC_CONSTANT) then 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 begin
  173. useconst := false;
  174. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  175. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, right.location.value, tmpreg);
  176. end
  177. end else
  178. useconst := false;
  179. location.loc := LOC_FLAGS;
  180. location.resflags := getresflags;
  181. if not unsigned then
  182. if useconst then
  183. op := A_CMPDI
  184. else
  185. op := A_CMPD
  186. else if useconst then
  187. op := A_CMPLDI
  188. else
  189. op := A_CMPLD;
  190. if (right.location.loc = LOC_CONSTANT) then begin
  191. if useconst then
  192. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op, left.location.register,
  193. longint(right.location.value)))
  194. else
  195. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op, left.location.register, tmpreg));
  196. end else
  197. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  198. left.location.register, right.location.register));
  199. end;
  200. {*****************************************************************************
  201. AddBoolean
  202. *****************************************************************************}
  203. procedure tppcaddnode.second_addboolean;
  204. var
  205. cgop: TOpCg;
  206. cgsize: TCgSize;
  207. cmpop,
  208. isjump: boolean;
  209. otl, ofl: tasmlabel;
  210. begin
  211. { calculate the operator which is more difficult }
  212. firstcomplex(self);
  213. cmpop := false;
  214. if (torddef(left.resulttype.def).typ = bool8bit) or
  215. (torddef(right.resulttype.def).typ = bool8bit) then
  216. cgsize := OS_8
  217. else if (torddef(left.resulttype.def).typ = bool16bit) or
  218. (torddef(right.resulttype.def).typ = bool16bit) then
  219. cgsize := OS_16
  220. else
  221. cgsize := OS_32;
  222. if (cs_full_boolean_eval in aktlocalswitches) or
  223. (nodetype in [unequaln, ltn, lten, gtn, gten, equaln, xorn]) then
  224. begin
  225. if left.nodetype in [ordconstn, realconstn] then
  226. swapleftright;
  227. isjump := (left.expectloc = LOC_JUMP);
  228. if isjump then
  229. begin
  230. otl := current_procinfo.CurrTrueLabel;
  231. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  232. ofl := current_procinfo.CurrFalseLabel;
  233. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  234. end;
  235. secondpass(left);
  236. if left.location.loc in [LOC_FLAGS, LOC_JUMP] then
  237. location_force_reg(current_asmdata.CurrAsmList, left.location, cgsize, false);
  238. if isjump then
  239. begin
  240. current_procinfo.CurrTrueLabel := otl;
  241. current_procinfo.CurrFalseLabel := ofl;
  242. end
  243. else if left.location.loc = LOC_JUMP then
  244. internalerror(2003122901);
  245. isjump := (right.expectloc = LOC_JUMP);
  246. if isjump then
  247. begin
  248. otl := current_procinfo.CurrTrueLabel;
  249. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  250. ofl := current_procinfo.CurrFalseLabel;
  251. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  252. end;
  253. secondpass(right);
  254. if right.location.loc in [LOC_FLAGS, LOC_JUMP] then
  255. location_force_reg(current_asmdata.CurrAsmList, right.location, cgsize, false);
  256. if isjump then
  257. begin
  258. current_procinfo.CurrTrueLabel := otl;
  259. current_procinfo.CurrFalseLabel := ofl;
  260. end
  261. else if right.location.loc = LOC_JUMP then
  262. internalerror(200312292);
  263. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  264. { set result location }
  265. if not cmpop then
  266. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  267. else
  268. location_reset(location, LOC_FLAGS, OS_NO);
  269. load_left_right(cmpop, false);
  270. if (left.location.loc = LOC_CONSTANT) then
  271. swapleftright;
  272. { compare the }
  273. case nodetype of
  274. ltn, lten, gtn, gten,
  275. equaln, unequaln:
  276. begin
  277. if (right.location.loc <> LOC_CONSTANT) then
  278. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLW,
  279. left.location.register, right.location.register))
  280. else
  281. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPLWI,
  282. left.location.register, longint(right.location.value)));
  283. location.resflags := getresflags;
  284. end;
  285. else
  286. begin
  287. case nodetype of
  288. xorn:
  289. cgop := OP_XOR;
  290. orn:
  291. cgop := OP_OR;
  292. andn:
  293. cgop := OP_AND;
  294. else
  295. internalerror(200203247);
  296. end;
  297. if right.location.loc <> LOC_CONSTANT then
  298. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  299. left.location.register, right.location.register,
  300. location.register)
  301. else
  302. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  303. right.location.value, left.location.register,
  304. location.register);
  305. end;
  306. end;
  307. end
  308. else
  309. begin
  310. // just to make sure we free the right registers
  311. cmpop := true;
  312. case nodetype of
  313. andn,
  314. orn:
  315. begin
  316. location_reset(location, LOC_JUMP, OS_NO);
  317. case nodetype of
  318. andn:
  319. begin
  320. otl := current_procinfo.CurrTrueLabel;
  321. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  322. secondpass(left);
  323. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  324. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrTrueLabel);
  325. current_procinfo.CurrTrueLabel := otl;
  326. end;
  327. orn:
  328. begin
  329. ofl := current_procinfo.CurrFalseLabel;
  330. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  331. secondpass(left);
  332. maketojumpbool(current_asmdata.CurrAsmList, left, lr_load_regvars);
  333. cg.a_label(current_asmdata.CurrAsmList, current_procinfo.CurrFalseLabel);
  334. current_procinfo.CurrFalseLabel := ofl;
  335. end;
  336. else
  337. internalerror(200403181);
  338. end;
  339. secondpass(right);
  340. maketojumpbool(current_asmdata.CurrAsmList, right, lr_load_regvars);
  341. end;
  342. end;
  343. end;
  344. end;
  345. {*****************************************************************************
  346. AddFloat
  347. *****************************************************************************}
  348. procedure tppcaddnode.second_addfloat;
  349. var
  350. op: TAsmOp;
  351. cmpop: boolean;
  352. begin
  353. pass_left_and_right;
  354. cmpop := false;
  355. case nodetype of
  356. addn:
  357. op := A_FADD;
  358. muln:
  359. op := A_FMUL;
  360. subn:
  361. op := A_FSUB;
  362. slashn:
  363. op := A_FDIV;
  364. ltn, lten, gtn, gten,
  365. equaln, unequaln:
  366. begin
  367. op := A_FCMPO;
  368. cmpop := true;
  369. end;
  370. else
  371. internalerror(200403182);
  372. end;
  373. // get the operands in the correct order, there are no special cases
  374. // here, everything is register-based
  375. if nf_swaped in flags then
  376. swapleftright;
  377. // put both operands in a register
  378. location_force_fpureg(current_asmdata.CurrAsmList, right.location, true);
  379. location_force_fpureg(current_asmdata.CurrAsmList, left.location, true);
  380. // initialize the result
  381. if not cmpop then begin
  382. location_reset(location, LOC_FPUREGISTER, def_cgsize(resulttype.def));
  383. location.register := cg.getfpuregister(current_asmdata.CurrAsmList, location.size);
  384. end else begin
  385. location_reset(location, LOC_FLAGS, OS_NO);
  386. location.resflags := getresflags;
  387. end;
  388. // emit the actual operation
  389. if not cmpop then begin
  390. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  391. location.register, left.location.register,
  392. right.location.register))
  393. end else begin
  394. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  395. newreg(R_SPECIALREGISTER, location.resflags.cr, R_SUBNONE),
  396. left.location.register, right.location.register))
  397. end;
  398. end;
  399. {*****************************************************************************
  400. AddSmallSet
  401. *****************************************************************************}
  402. procedure tppcaddnode.second_addsmallset;
  403. var
  404. cgop: TOpCg;
  405. tmpreg: tregister;
  406. opdone,
  407. cmpop: boolean;
  408. astring : string;
  409. // ts: todo - speed up by using 32 bit compares/adds/ands here
  410. begin
  411. pass_left_and_right;
  412. { when a setdef is passed, it has to be a smallset }
  413. if ((left.resulttype.def.deftype = setdef) and
  414. (tsetdef(left.resulttype.def).settype <> smallset)) or
  415. ((right.resulttype.def.deftype = setdef) and
  416. (tsetdef(right.resulttype.def).settype <> smallset)) then
  417. internalerror(200203301);
  418. opdone := false;
  419. cmpop := nodetype in [equaln, unequaln, lten, gten];
  420. { set result location }
  421. if not cmpop then
  422. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  423. else
  424. location_reset(location, LOC_FLAGS, OS_NO);
  425. load_left_right(cmpop, false);
  426. if not (cmpop) then
  427. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  428. {$ifdef extdebug}
  429. astring := 'addsmallset0 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  430. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  431. {$endif extdebug}
  432. case nodetype of
  433. addn:
  434. begin
  435. if (nf_swaped in flags) and (left.nodetype = setelementn) then
  436. swapleftright;
  437. { are we adding set elements ? }
  438. if right.nodetype = setelementn then begin
  439. { no range support for smallsets! }
  440. if assigned(tsetelementnode(right).right) then
  441. internalerror(43244);
  442. if (right.location.loc = LOC_CONSTANT) then begin
  443. {$ifdef extdebug}
  444. astring := 'addsmallset1 ' + inttostr(aword(1) shl aword(right.location.value)) + ' ' + inttostr(right.location.value);
  445. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  446. {$endif extdebug}
  447. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64,
  448. aint(1) shl aint(right.location.value),
  449. left.location.register, location.register)
  450. end else
  451. begin
  452. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  453. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64, 1, tmpreg);
  454. cg.a_op_reg_reg(current_asmdata.CurrAsmList, OP_SHL, OS_64,
  455. right.location.register, tmpreg);
  456. if left.location.loc <> LOC_CONSTANT then begin
  457. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_64, tmpreg,
  458. left.location.register, location.register)
  459. end else begin
  460. {$ifdef extdebug}
  461. astring := 'addsmallset2 ' + inttostr(left.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. left.location.value, tmpreg, location.register);
  466. end;
  467. end;
  468. opdone := true;
  469. end else begin
  470. cgop := OP_OR;
  471. end;
  472. end;
  473. symdifn:
  474. cgop := OP_XOR;
  475. muln:
  476. cgop := OP_AND;
  477. subn:
  478. begin
  479. cgop := OP_AND;
  480. if (not (nf_swaped in flags)) then
  481. if (right.location.loc = LOC_CONSTANT) then
  482. right.location.value := not (right.location.value)
  483. else
  484. opdone := true
  485. else if (left.location.loc = LOC_CONSTANT) then
  486. left.location.value := not (left.location.value)
  487. else begin
  488. swapleftright;
  489. opdone := true;
  490. end;
  491. if opdone then begin
  492. if left.location.loc = LOC_CONSTANT then
  493. begin
  494. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  495. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  496. left.location.value, tmpreg);
  497. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  498. location.register, tmpreg, right.location.register));
  499. end
  500. else
  501. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC,
  502. location.register, left.location.register,
  503. right.location.register));
  504. end;
  505. end;
  506. equaln,
  507. unequaln:
  508. begin
  509. emit_compare(true);
  510. opdone := true;
  511. end;
  512. lten, gten:
  513. begin
  514. if (not (nf_swaped in flags) and
  515. (nodetype = lten)) or
  516. ((nf_swaped in flags) and
  517. (nodetype = gten)) then
  518. swapleftright;
  519. // now we have to check whether left >= right
  520. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_64);
  521. if left.location.loc = LOC_CONSTANT then begin
  522. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_64,
  523. not (left.location.value), right.location.register, tmpreg);
  524. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMPDI, tmpreg, 0));
  525. // the two instructions above should be folded together by
  526. // the peepholeoptimizer
  527. end else begin
  528. if right.location.loc = LOC_CONSTANT then begin
  529. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_64,
  530. right.location.value, tmpreg);
  531. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  532. tmpreg, left.location.register));
  533. end else
  534. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC_, tmpreg,
  535. right.location.register, left.location.register));
  536. end;
  537. location.resflags.cr := RS_CR0;
  538. location.resflags.flag := F_EQ;
  539. opdone := true;
  540. end;
  541. else
  542. internalerror(2002072701);
  543. end;
  544. if not opdone then begin
  545. // these are all commutative operations
  546. if (left.location.loc = LOC_CONSTANT) then
  547. swapleftright;
  548. if (right.location.loc = LOC_CONSTANT) then begin
  549. astring := 'addsmallset4 ' + inttostr(right.location.value);
  550. current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew(astring)));
  551. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  552. right.location.value, left.location.register,
  553. location.register)
  554. end else begin
  555. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_64,
  556. right.location.register, left.location.register,
  557. location.register);
  558. end;
  559. end;
  560. end;
  561. {*****************************************************************************
  562. pass_2
  563. *****************************************************************************}
  564. procedure tppcaddnode.pass_2;
  565. { is also being used for xor, and "mul", "sub, or and comparative }
  566. { operators }
  567. var
  568. cgop: topcg;
  569. op: tasmop;
  570. tmpreg: tregister;
  571. hl: tasmlabel;
  572. cmpop: boolean;
  573. { true, if unsigned types are compared }
  574. unsigned: boolean;
  575. begin
  576. { to make it more readable, string and set (not smallset!) have their
  577. own procedures }
  578. case left.resulttype.def.deftype of
  579. orddef:
  580. begin
  581. { handling boolean expressions }
  582. if is_boolean(left.resulttype.def) and
  583. is_boolean(right.resulttype.def) then
  584. begin
  585. second_addboolean;
  586. exit;
  587. end;
  588. end;
  589. stringdef:
  590. begin
  591. internalerror(2002072402);
  592. exit;
  593. end;
  594. setdef:
  595. begin
  596. { normalsets are already handled in pass1 }
  597. if (tsetdef(left.resulttype.def).settype <> smallset) then
  598. internalerror(200109041);
  599. second_addsmallset;
  600. exit;
  601. end;
  602. arraydef:
  603. begin
  604. {$IFDEF SUPPORT_MMX}
  605. if is_mmx_able_array(left.resulttype.def) then
  606. begin
  607. second_addmmx;
  608. exit;
  609. end;
  610. {$ENDIF SUPPORT_MMX}
  611. end;
  612. floatdef:
  613. begin
  614. second_addfloat;
  615. exit;
  616. end;
  617. end;
  618. { defaults }
  619. cmpop := nodetype in [ltn, lten, gtn, gten, equaln, unequaln];
  620. unsigned := not (is_signed(left.resulttype.def)) or
  621. not (is_signed(right.resulttype.def));
  622. pass_left_and_right;
  623. { Convert flags to register first }
  624. { can any of these things be in the flags actually?? (JM) }
  625. if (left.location.loc = LOC_FLAGS) or
  626. (right.location.loc = LOC_FLAGS) then
  627. internalerror(2002072602);
  628. { set result location }
  629. if not cmpop then
  630. location_reset(location, LOC_REGISTER, def_cgsize(resulttype.def))
  631. else
  632. location_reset(location, LOC_FLAGS, OS_NO);
  633. load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  634. (nodetype in [addn, subn, muln]));
  635. if not (cmpop) then
  636. location.register := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  637. if not (cs_check_overflow in aktlocalswitches) or (cmpop) or
  638. (nodetype in [orn, andn, xorn]) then begin
  639. case nodetype of
  640. addn, muln, xorn, orn, andn:
  641. begin
  642. case nodetype of
  643. addn:
  644. cgop := OP_ADD;
  645. muln:
  646. if unsigned then
  647. cgop := OP_MUL
  648. else
  649. cgop := OP_IMUL;
  650. xorn:
  651. cgop := OP_XOR;
  652. orn:
  653. cgop := OP_OR;
  654. andn:
  655. cgop := OP_AND;
  656. end;
  657. if (left.location.loc = LOC_CONSTANT) then
  658. swapleftright;
  659. if (right.location.loc <> LOC_CONSTANT) then
  660. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  661. left.location.register, right.location.register,
  662. location.register)
  663. else
  664. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, cgop, OS_INT,
  665. right.location.value, left.location.register,
  666. location.register);
  667. end;
  668. subn:
  669. begin
  670. if (nf_swaped in flags) then
  671. swapleftright;
  672. if left.location.loc <> LOC_CONSTANT then
  673. if right.location.loc <> LOC_CONSTANT then begin
  674. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  675. right.location.register, left.location.register,
  676. location.register);
  677. end else begin
  678. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  679. right.location.value, left.location.register,
  680. location.register);
  681. end
  682. else
  683. begin
  684. tmpreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  685. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT,
  686. left.location.value, tmpreg);
  687. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT,
  688. right.location.register, tmpreg, location.register);
  689. end;
  690. end;
  691. ltn, lten, gtn, gten, equaln, unequaln:
  692. begin
  693. {$ifdef extdebug}
  694. current_asmdata.CurrAsmList.concat(tai_comment.create('tppcaddnode.pass2'));
  695. {$endif extdebug}
  696. emit_compare(unsigned);
  697. end;
  698. end;
  699. end
  700. else
  701. // overflow checking is on and we have an addn, subn or muln
  702. begin
  703. if is_signed(resulttype.def) then
  704. begin
  705. case nodetype of
  706. addn:
  707. op := A_ADDO;
  708. subn:
  709. begin
  710. op := A_SUBO;
  711. if (nf_swaped in flags) then
  712. swapleftright;
  713. end;
  714. muln:
  715. op := A_MULLDO;
  716. else
  717. internalerror(2002072601);
  718. end;
  719. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op, location.register,
  720. left.location.register, right.location.register));
  721. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  722. end
  723. else
  724. begin
  725. case nodetype of
  726. addn:
  727. begin
  728. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD, location.register,
  729. left.location.register, right.location.register));
  730. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD, location.register,
  731. left.location.register));
  732. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  733. end;
  734. subn:
  735. begin
  736. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUB, location.register,
  737. left.location.register, right.location.register));
  738. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMPLD,
  739. left.location.register, location.register));
  740. cg.g_overflowcheck(current_asmdata.CurrAsmList, location, resulttype.def);
  741. end;
  742. muln:
  743. begin
  744. { calculate the upper 64 bits of the product, = 0 if no overflow }
  745. cg.a_reg_alloc(current_asmdata.CurrAsmList, NR_R0);
  746. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU_, NR_R0,
  747. left.location.register, right.location.register));
  748. cg.a_reg_dealloc(current_asmdata.CurrAsmList, NR_R0);
  749. { calculate the real result }
  750. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULLD, location.register,
  751. left.location.register, right.location.register));
  752. { g_overflowcheck generates a OC_AE instead of OC_EQ :/ }
  753. current_asmdata.getjumplabel(hl);
  754. tcgppc(cg).a_jmp_cond(current_asmdata.CurrAsmList, OC_EQ, hl);
  755. cg.a_call_name(current_asmdata.CurrAsmList, 'FPC_OVERFLOW');
  756. cg.a_label(current_asmdata.CurrAsmList, hl);
  757. end;
  758. end;
  759. end;
  760. end;
  761. end;
  762. begin
  763. caddnode := tppcaddnode;
  764. end.