nppcmat.pas 32 KB

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