nppcmat.pas 31 KB

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