nppcmat.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate PowerPC assembler for math nodes
  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 nppcmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat;
  22. type
  23. tppcmoddivnode = class(tmoddivnode)
  24. function pass_1: tnode;override;
  25. procedure pass_2;override;
  26. end;
  27. tppcshlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. { everything will be handled in pass_2 }
  30. function first_shlshr64bitint: tnode; override;
  31. end;
  32. tppcunaryminusnode = class(tunaryminusnode)
  33. procedure pass_2;override;
  34. end;
  35. tppcnotnode = class(tnotnode)
  36. procedure pass_2;override;
  37. end;
  38. implementation
  39. uses
  40. globtype,systems,
  41. cutils,verbose,globals,
  42. symconst,
  43. aasmbase,aasmcpu,aasmtai,
  44. defutil,
  45. cgbase,cgutils,cgobj,pass_2,
  46. ncon,procinfo,
  47. cpubase,
  48. ncgutil,cgcpu;
  49. { helper functions }
  50. procedure getmagic_unsigned32(d : dword; out magic_m : dword; out magic_add : boolean; out magic_shift : dword);
  51. var
  52. p : longint;
  53. nc, delta, q1, r1, q2, r2 : dword;
  54. begin
  55. assert(d > 0);
  56. magic_add := false;
  57. nc := - 1 - (-d) mod d;
  58. p := 31; { initialize p }
  59. q1 := $80000000 div nc; { initialize q1 = 2p/nc }
  60. r1 := $80000000 - q1*nc; { initialize r1 = rem(2p,nc) }
  61. q2 := $7FFFFFFF div d; { initialize q2 = (2p-1)/d }
  62. r2 := $7FFFFFFF - q2*d; { initialize r2 = rem((2p-1),d) }
  63. repeat
  64. inc(p);
  65. if (r1 >= (nc - r1)) then begin
  66. q1 := 2 * q1 + 1; { update q1 }
  67. r1 := 2*r1 - nc; { update r1 }
  68. end else begin
  69. q1 := 2*q1; { update q1 }
  70. r1 := 2*r1; { update r1 }
  71. end;
  72. if ((r2 + 1) >= (d - r2)) then begin
  73. if (q2 >= $7FFFFFFF) then
  74. magic_add := true;
  75. q2 := 2*q2 + 1; { update q2 }
  76. r2 := 2*r2 + 1 - d; { update r2 }
  77. end else begin
  78. if (q2 >= $80000000) then
  79. magic_add := true;
  80. q2 := 2*q2; { update q2 }
  81. r2 := 2*r2 + 1; { update r2 }
  82. end;
  83. delta := d - 1 - r2;
  84. until not ((p < 64) and ((q1 < delta) or ((q1 = delta) and (r1 = 0))));
  85. magic_m := q2 + 1; { resulting magic number }
  86. magic_shift := p - 32; { resulting shift }
  87. end;
  88. procedure getmagic_signed32(d : longint; out magic_m : longint; out magic_s : longint);
  89. const
  90. two_31 : DWord = high(longint)+1;
  91. var
  92. p : Longint;
  93. ad, anc, delta, q1, r1, q2, r2, t : DWord;
  94. begin
  95. assert((d < -1) or (d > 1));
  96. ad := abs(d);
  97. t := two_31 + (DWord(d) shr 31);
  98. anc := t - 1 - t mod ad; { absolute value of nc }
  99. p := 31; { initialize p }
  100. q1 := two_31 div anc; { initialize q1 = 2p/abs(nc) }
  101. r1 := two_31 - q1*anc; { initialize r1 = rem(2p,abs(nc)) }
  102. q2 := two_31 div ad; { initialize q2 = 2p/abs(d) }
  103. r2 := two_31 - q2*ad; { initialize r2 = rem(2p,abs(d)) }
  104. repeat
  105. inc(p);
  106. q1 := 2*q1; { update q1 = 2p/abs(nc) }
  107. r1 := 2*r1; { update r1 = rem(2p/abs(nc)) }
  108. if (r1 >= anc) then begin { must be unsigned comparison }
  109. inc(q1);
  110. dec(r1, anc);
  111. end;
  112. q2 := 2*q2; { update q2 = 2p/abs(d) }
  113. r2 := 2*r2; { update r2 = rem(2p/abs(d)) }
  114. if (r2 >= ad) then begin { must be unsigned comparison }
  115. inc(q2);
  116. dec(r2, ad);
  117. end;
  118. delta := ad - r2;
  119. until not ((q1 < delta) or ((q1 = delta) and (r1 = 0)));
  120. magic_m := q2 + 1;
  121. if (d < 0) then begin
  122. magic_m := -magic_m; { resulting magic number }
  123. end;
  124. magic_s := p - 32; { resulting shift }
  125. end;
  126. {*****************************************************************************
  127. TPPCMODDIVNODE
  128. *****************************************************************************}
  129. function tppcmoddivnode.pass_1: tnode;
  130. begin
  131. result := inherited pass_1;
  132. if not assigned(result) then
  133. include(current_procinfo.flags,pi_do_call);
  134. end;
  135. procedure tppcmoddivnode.pass_2;
  136. const
  137. { signed overflow }
  138. divops: array[boolean, boolean] of tasmop =
  139. ((A_DIVWU,A_DIVWU_),(A_DIVW,A_DIVWO_));
  140. zerocond: tasmcond = (dirhint: DH_Plus; simple: true; cond:C_NE; cr: RS_CR1);
  141. var
  142. power : longint;
  143. op : tasmop;
  144. numerator,
  145. divider,
  146. resultreg : tregister;
  147. size : Tcgsize;
  148. hl : tasmlabel;
  149. done: boolean;
  150. procedure genOrdConstNodeDiv;
  151. const
  152. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  153. var
  154. magic, shift : longint;
  155. u_magic, u_shift : dword;
  156. u_add : boolean;
  157. divreg : tregister;
  158. begin
  159. if (tordconstnode(right).value = 0) then begin
  160. internalerror(2005061701);
  161. end else if (tordconstnode(right).value = 1) then begin
  162. cg.a_load_reg_reg(exprasmlist, OS_INT, OS_INT, numerator, resultreg);
  163. end else if (tordconstnode(right).value = -1) then begin
  164. // note: only in the signed case possible..., may overflow
  165. exprasmlist.concat(taicpu.op_reg_reg(negops[cs_check_overflow in aktlocalswitches], resultreg, numerator));
  166. end else if (ispowerof2(tordconstnode(right).value, power)) then begin
  167. if (is_signed(right.resulttype.def)) then begin
  168. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  169. cg.a_op_const_reg_reg(exprasmlist, OP_SAR, OS_INT, power,
  170. numerator, resultreg);
  171. exprasmlist.concat(taicpu.op_reg_reg(A_ADDZE, resultreg, resultreg));
  172. end else begin
  173. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, power, numerator, resultreg)
  174. end;
  175. end else begin
  176. { replace division by multiplication, both implementations }
  177. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  178. divreg := cg.getintregister(exprasmlist, OS_INT);
  179. if (is_signed(right.resulttype.def)) then begin
  180. getmagic_signed32(tordconstnode(right).value, magic, shift);
  181. // load magic value
  182. cg.a_load_const_reg(exprasmlist, OS_INT, magic, divreg);
  183. // multiply
  184. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULHW, resultreg, numerator, divreg));
  185. // add/subtract numerator
  186. if (tordconstnode(right).value > 0) and (magic < 0) then begin
  187. cg.a_op_reg_reg_reg(exprasmlist, OP_ADD, OS_INT, numerator, resultreg, resultreg);
  188. end else if (tordconstnode(right).value < 0) and (magic > 0) then begin
  189. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT, numerator, resultreg, resultreg);
  190. end;
  191. // shift shift places to the right (arithmetic)
  192. cg.a_op_const_reg_reg(exprasmlist, OP_SAR, OS_INT, shift, resultreg, resultreg);
  193. // extract and add sign bit
  194. if (tordconstnode(right).value >= 0) then begin
  195. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, 31, numerator, divreg);
  196. end else begin
  197. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, 31, resultreg, divreg);
  198. end;
  199. cg.a_op_reg_reg_reg(exprasmlist, OP_ADD, OS_INT, resultreg, divreg, resultreg);
  200. end else begin
  201. getmagic_unsigned32(tordconstnode(right).value, u_magic, u_add, u_shift);
  202. // load magic in divreg
  203. cg.a_load_const_reg(exprasmlist, OS_INT, u_magic, divreg);
  204. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULHWU, resultreg, numerator, divreg));
  205. if (u_add) then begin
  206. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT, resultreg, numerator, divreg);
  207. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, 1, divreg, divreg);
  208. cg.a_op_reg_reg_reg(exprasmlist, OP_ADD, OS_INT, divreg, resultreg, divreg);
  209. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, u_shift-1, divreg, resultreg);
  210. end else begin
  211. cg.a_op_const_reg_reg(exprasmlist, OP_SHR, OS_INT, u_shift, resultreg, resultreg);
  212. end;
  213. end;
  214. end;
  215. done := true;
  216. end;
  217. procedure genOrdConstNodeMod;
  218. var
  219. modreg, maskreg, tempreg : tregister;
  220. begin
  221. if (tordconstnode(right).value = 0) then begin
  222. internalerror(2005061702);
  223. end else if (abs(tordconstnode(right).value) = 1) then begin
  224. // x mod +/-1 is always zero
  225. cg.a_load_const_reg(exprasmlist, OS_INT, 0, resultreg);
  226. end else if (ispowerof2(tordconstnode(right).value, power)) then begin
  227. if (is_signed(right.resulttype.def)) then begin
  228. tempreg := cg.getintregister(exprasmlist, OS_INT);
  229. maskreg := cg.getintregister(exprasmlist, OS_INT);
  230. modreg := cg.getintregister(exprasmlist, OS_INT);
  231. cg.a_load_const_reg(exprasmlist, OS_INT, abs(tordconstnode(right).value)-1, modreg);
  232. cg.a_op_const_reg_reg(exprasmlist, OP_SAR, OS_INT, 31, numerator, maskreg);
  233. cg.a_op_reg_reg_reg(exprasmlist, OP_AND, OS_INT, numerator, modreg, tempreg);
  234. exprasmlist.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  235. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  236. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  237. cg.a_op_reg_reg_reg(exprasmlist, OP_AND, OS_INT, modreg, maskreg, maskreg);
  238. cg.a_op_reg_reg_reg(exprasmlist, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  239. end else begin
  240. cg.a_op_const_reg_reg(exprasmlist, OP_AND, OS_INT, tordconstnode(right).value-1, numerator, resultreg);
  241. end;
  242. end else begin
  243. genOrdConstNodeDiv();
  244. cg.a_op_const_reg_reg(exprasmlist, OP_MUL, OS_INT, tordconstnode(right).value, resultreg, resultreg);
  245. cg.a_op_reg_reg_reg(exprasmlist, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  246. end;
  247. end;
  248. begin
  249. secondpass(left);
  250. secondpass(right);
  251. location_copy(location,left.location);
  252. { put numerator in register }
  253. size:=def_cgsize(left.resulttype.def);
  254. location_force_reg(exprasmlist,left.location,
  255. size,true);
  256. location_copy(location,left.location);
  257. numerator := location.register;
  258. resultreg := location.register;
  259. if (location.loc = LOC_CREGISTER) then begin
  260. location.loc := LOC_REGISTER;
  261. location.register := cg.getintregister(exprasmlist,size);
  262. resultreg := location.register;
  263. end else if (nodetype = modn) or (right.nodetype = ordconstn) then begin
  264. // for a modulus op, and for const nodes we need the result register
  265. // to be an extra register
  266. resultreg := cg.getintregister(exprasmlist,size);
  267. end;
  268. done := false;
  269. if (right.nodetype = ordconstn) then begin
  270. if (nodetype = divn) then
  271. genOrdConstNodeDiv
  272. else
  273. genOrdConstNodeMod;
  274. done := true;
  275. end;
  276. if (not done) then begin
  277. { load divider in a register if necessary }
  278. location_force_reg(exprasmlist,right.location,
  279. def_cgsize(right.resulttype.def),true);
  280. if (right.nodetype <> ordconstn) then
  281. exprasmlist.concat(taicpu.op_reg_reg_const(A_CMPWI,NR_CR1,
  282. right.location.register,0));
  283. divider := right.location.register;
  284. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  285. op := divops[is_signed(right.resulttype.def),
  286. cs_check_overflow in aktlocalswitches];
  287. exprasmlist.concat(taicpu.op_reg_reg_reg(op,resultreg,numerator,
  288. divider));
  289. if (nodetype = modn) then
  290. begin
  291. exprasmlist.concat(taicpu.op_reg_reg_reg(A_MULLW,resultreg,
  292. divider,resultreg));
  293. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,location.register,
  294. numerator,resultreg));
  295. resultreg := location.register;
  296. end;
  297. end;
  298. { set result location }
  299. location.loc:=LOC_REGISTER;
  300. location.register:=resultreg;
  301. if right.nodetype <> ordconstn then
  302. begin
  303. objectlibrary.getjumplabel(hl);
  304. exprasmlist.concat(taicpu.op_cond_sym(A_BC,zerocond,hl));
  305. cg.a_call_name(exprasmlist,'FPC_DIVBYZERO');
  306. cg.a_label(exprasmlist,hl);
  307. end;
  308. { unsigned division/module can only overflow in case of division by zero }
  309. { (but checking this overflow flag is more convoluted than performing a }
  310. { simple comparison with 0) }
  311. if is_signed(right.resulttype.def) then
  312. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  313. end;
  314. {*****************************************************************************
  315. TPPCSHLRSHRNODE
  316. *****************************************************************************}
  317. function tppcshlshrnode.first_shlshr64bitint: tnode;
  318. begin
  319. result := nil;
  320. end;
  321. procedure tppcshlshrnode.pass_2;
  322. var
  323. resultreg, hregister1,hregister2,
  324. hreg64hi,hreg64lo : tregister;
  325. op : topcg;
  326. asmop1, asmop2: tasmop;
  327. shiftval: aint;
  328. begin
  329. secondpass(left);
  330. secondpass(right);
  331. if is_64bitint(left.resulttype.def) then
  332. begin
  333. location_force_reg(exprasmlist,left.location,
  334. def_cgsize(left.resulttype.def),true);
  335. location_copy(location,left.location);
  336. hreg64hi := location.register64.reghi;
  337. hreg64lo := location.register64.reglo;
  338. if (location.loc = LOC_CREGISTER) then
  339. begin
  340. location.loc := LOC_REGISTER;
  341. location.register64.reghi := cg.getintregister(exprasmlist,OS_32);
  342. location.register64.reglo := cg.getintregister(exprasmlist,OS_32);
  343. end;
  344. if (right.nodetype = ordconstn) then
  345. begin
  346. shiftval := tordconstnode(right).value;
  347. shiftval := shiftval and 63;
  348. {
  349. I think the statements below is much more correct instead of the hack above,
  350. but then we fail tshlshr.pp :/
  351. if shiftval > 63 then
  352. begin
  353. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  354. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  355. end
  356. else } if shiftval > 31 then
  357. begin
  358. if nodetype = shln then
  359. begin
  360. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,
  361. shiftval and 31,hreg64lo,location.register64.reghi);
  362. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reglo);
  363. end
  364. else
  365. begin
  366. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,
  367. shiftval and 31,hreg64hi,location.register64.reglo);
  368. cg.a_load_const_reg(exprasmlist,OS_32,0,location.register64.reghi);
  369. end;
  370. end
  371. else
  372. begin
  373. if nodetype = shln then
  374. begin
  375. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  376. A_RLWINM,location.register64.reghi,hreg64hi,shiftval,
  377. 0,31-shiftval));
  378. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  379. A_RLWIMI,location.register64.reghi,hreg64lo,shiftval,
  380. 32-shiftval,31));
  381. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  382. A_RLWINM,location.register64.reglo,hreg64lo,shiftval,
  383. 0,31-shiftval));
  384. end
  385. else
  386. begin
  387. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  388. A_RLWINM,location.register64.reglo,hreg64lo,32-shiftval,
  389. shiftval,31));
  390. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  391. A_RLWIMI,location.register64.reglo,hreg64hi,32-shiftval,
  392. 0,shiftval-1));
  393. exprasmlist.concat(taicpu.op_reg_reg_const_const_const(
  394. A_RLWINM,location.register64.reghi,hreg64hi,32-shiftval,
  395. shiftval,31));
  396. end;
  397. end;
  398. end
  399. else
  400. { no constant shiftcount }
  401. begin
  402. location_force_reg(exprasmlist,right.location,OS_S32,true);
  403. hregister1 := right.location.register;
  404. if nodetype = shln then
  405. begin
  406. asmop1 := A_SLW;
  407. asmop2 := A_SRW;
  408. end
  409. else
  410. begin
  411. asmop1 := A_SRW;
  412. asmop2 := A_SLW;
  413. resultreg := hreg64hi;
  414. hreg64hi := hreg64lo;
  415. hreg64lo := resultreg;
  416. resultreg := location.register64.reghi;
  417. location.register64.reghi := location.register64.reglo;
  418. location.register64.reglo := resultreg;
  419. end;
  420. cg.getcpuregister(exprasmlist,NR_R0);
  421. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  422. NR_R0,hregister1,32));
  423. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  424. location.register64.reghi,hreg64hi,hregister1));
  425. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop2,
  426. NR_R0,hreg64lo,NR_R0));
  427. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  428. location.register64.reghi,location.register64.reghi,NR_R0));
  429. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBI,
  430. NR_R0,hregister1,32));
  431. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  432. NR_R0,hreg64lo,NR_R0));
  433. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,
  434. location.register64.reghi,location.register64.reghi,NR_R0));
  435. exprasmlist.concat(taicpu.op_reg_reg_reg(asmop1,
  436. location.register64.reglo,hreg64lo,hregister1));
  437. cg.ungetcpuregister(exprasmlist,NR_R0);
  438. if nodetype = shrn then
  439. begin
  440. resultreg := location.register64.reghi;
  441. location.register64.reghi := location.register64.reglo;
  442. location.register64.reglo := resultreg;
  443. end;
  444. end
  445. end
  446. else
  447. begin
  448. { load left operators in a register }
  449. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  450. location_copy(location,left.location);
  451. resultreg := location.register;
  452. hregister1 := location.register;
  453. if (location.loc = LOC_CREGISTER) then
  454. begin
  455. location.loc := LOC_REGISTER;
  456. resultreg := cg.getintregister(exprasmlist,OS_32);
  457. location.register := resultreg;
  458. end;
  459. { determine operator }
  460. if nodetype=shln then
  461. op:=OP_SHL
  462. else
  463. op:=OP_SHR;
  464. { shifting by a constant directly coded: }
  465. if (right.nodetype=ordconstn) then
  466. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,
  467. tordconstnode(right).value and 31,hregister1,resultreg)
  468. else
  469. begin
  470. { load shift count in a register if necessary }
  471. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  472. hregister2 := right.location.register;
  473. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,hregister2,
  474. hregister1,resultreg);
  475. end;
  476. end;
  477. end;
  478. {*****************************************************************************
  479. TPPCUNARYMINUSNODE
  480. *****************************************************************************}
  481. procedure tppcunaryminusnode.pass_2;
  482. var
  483. src1: tregister;
  484. op: tasmop;
  485. begin
  486. secondpass(left);
  487. if is_64bitint(left.resulttype.def) then
  488. begin
  489. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  490. location_copy(location,left.location);
  491. if (location.loc = LOC_CREGISTER) then
  492. begin
  493. location.register64.reglo := cg.getintregister(exprasmlist,OS_INT);
  494. location.register64.reghi := cg.getintregister(exprasmlist,OS_INT);
  495. location.loc := LOC_REGISTER;
  496. end;
  497. exprasmlist.concat(taicpu.op_reg_reg_const(A_SUBFIC,
  498. location.register64.reglo,left.location.register64.reglo,0));
  499. if not(cs_check_overflow in aktlocalswitches) then
  500. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZE,
  501. location.register64.reghi,left.location.register64.reghi))
  502. else
  503. exprasmlist.concat(taicpu.op_reg_reg(A_SUBFZEO_,
  504. location.register64.reghi,left.location.register64.reghi));
  505. end
  506. else
  507. begin
  508. location_copy(location,left.location);
  509. location.loc:=LOC_REGISTER;
  510. case left.location.loc of
  511. LOC_FPUREGISTER, LOC_REGISTER:
  512. begin
  513. src1 := left.location.register;
  514. location.register := src1;
  515. end;
  516. LOC_CFPUREGISTER, LOC_CREGISTER:
  517. begin
  518. src1 := left.location.register;
  519. if left.location.loc = LOC_CREGISTER then
  520. location.register := cg.getintregister(exprasmlist,OS_INT)
  521. else
  522. location.register := cg.getfpuregister(exprasmlist,location.size);
  523. end;
  524. LOC_REFERENCE,LOC_CREFERENCE:
  525. begin
  526. if (left.resulttype.def.deftype=floatdef) then
  527. begin
  528. src1 := cg.getfpuregister(exprasmlist,def_cgsize(left.resulttype.def));
  529. location.register := src1;
  530. cg.a_loadfpu_ref_reg(exprasmlist,
  531. def_cgsize(left.resulttype.def),
  532. left.location.reference,src1);
  533. end
  534. else
  535. begin
  536. src1 := cg.getintregister(exprasmlist,OS_32);
  537. location.register:= src1;
  538. cg.a_load_ref_reg(exprasmlist,OS_32,OS_32,
  539. left.location.reference,src1);
  540. end;
  541. end;
  542. end;
  543. { choose appropriate operand }
  544. if left.resulttype.def.deftype <> floatdef then
  545. begin
  546. if not(cs_check_overflow in aktlocalswitches) then
  547. op := A_NEG
  548. else
  549. op := A_NEGO_;
  550. location.loc := LOC_REGISTER;
  551. end
  552. else
  553. begin
  554. op := A_FNEG;
  555. location.loc := LOC_FPUREGISTER;
  556. end;
  557. { emit operation }
  558. exprasmlist.concat(taicpu.op_reg_reg(op,location.register,src1));
  559. end;
  560. { Here was a problem... }
  561. { Operand to be negated always }
  562. { seems to be converted to signed }
  563. { 32-bit before doing neg!! }
  564. { So this is useless... }
  565. { that's not true: -2^31 gives an overflow error if it is negated (FK) }
  566. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  567. end;
  568. {*****************************************************************************
  569. TPPCNOTNODE
  570. *****************************************************************************}
  571. procedure tppcnotnode.pass_2;
  572. var
  573. hl : tasmlabel;
  574. begin
  575. if is_boolean(resulttype.def) then
  576. begin
  577. { if the location is LOC_JUMP, we do the secondpass after the
  578. labels are allocated
  579. }
  580. if left.expectloc=LOC_JUMP then
  581. begin
  582. hl:=truelabel;
  583. truelabel:=falselabel;
  584. falselabel:=hl;
  585. secondpass(left);
  586. maketojumpbool(exprasmlist,left,lr_load_regvars);
  587. hl:=truelabel;
  588. truelabel:=falselabel;
  589. falselabel:=hl;
  590. location.loc:=LOC_JUMP;
  591. end
  592. else
  593. begin
  594. secondpass(left);
  595. case left.location.loc of
  596. LOC_FLAGS :
  597. begin
  598. location_copy(location,left.location);
  599. inverse_flags(location.resflags);
  600. end;
  601. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  602. begin
  603. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  604. exprasmlist.concat(taicpu.op_reg_const(A_CMPWI,left.location.register,0));
  605. location_reset(location,LOC_FLAGS,OS_NO);
  606. location.resflags.cr:=RS_CR0;
  607. location.resflags.flag:=F_EQ;
  608. end;
  609. else
  610. internalerror(2003042401);
  611. end;
  612. end;
  613. end
  614. else if is_64bitint(left.resulttype.def) then
  615. begin
  616. secondpass(left);
  617. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  618. location_copy(location,left.location);
  619. { perform the NOT operation }
  620. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reghi,
  621. location.register64.reghi));
  622. exprasmlist.concat(taicpu.op_reg_reg(A_NOT,location.register64.reglo,
  623. location.register64.reglo));
  624. end
  625. else
  626. begin
  627. secondpass(left);
  628. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  629. location_copy(location,left.location);
  630. location.loc := LOC_REGISTER;
  631. location.register := cg.getintregister(exprasmlist,OS_INT);
  632. { perform the NOT operation }
  633. cg.a_op_reg_reg(exprasmlist,OP_NOT,def_cgsize(resulttype.def),left.location.register,
  634. location.register);
  635. end;
  636. end;
  637. begin
  638. cmoddivnode:=tppcmoddivnode;
  639. cshlshrnode:=tppcshlshrnode;
  640. cunaryminusnode:=tppcunaryminusnode;
  641. cnotnode:=tppcnotnode;
  642. end.