nppcadd.pas 26 KB

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