ncgmat.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate generic mathematical nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgmat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat,cpubase,cgbase;
  23. type
  24. tcgunaryminusnode = class(tunaryminusnode)
  25. protected
  26. { This routine is called to change the sign of the
  27. floating point value in the floating point
  28. register r.
  29. This routine should be overriden, since
  30. the generic version is not optimal at all. The
  31. generic version assumes that floating
  32. point values are stored in the register
  33. in IEEE-754 format.
  34. }
  35. procedure emit_float_sign_change(r: tregister; _size : tcgsize);virtual;
  36. {$ifdef SUPPORT_MMX}
  37. procedure second_mmx;virtual;abstract;
  38. {$endif SUPPORT_MMX}
  39. {$ifndef cpu64bit}
  40. procedure second_64bit;virtual;
  41. {$endif cpu64bit}
  42. procedure second_integer;virtual;
  43. procedure second_float;virtual;
  44. public
  45. procedure pass_2;override;
  46. end;
  47. tcgmoddivnode = class(tmoddivnode)
  48. procedure pass_2;override;
  49. protected
  50. { This routine must do an actual 32-bit division, be it
  51. signed or unsigned. The result must set into the the
  52. @var(num) register.
  53. @param(signed Indicates if the division must be signed)
  54. @param(denum Register containing the denominator
  55. @param(num Register containing the numerator, will also receive result)
  56. The actual optimizations regarding shifts have already
  57. been done and emitted, so this should really a do a divide.
  58. }
  59. procedure emit_div_reg_reg(signed: boolean;denum,num : tregister);virtual;abstract;
  60. { This routine must do an actual 32-bit modulo, be it
  61. signed or unsigned. The result must set into the the
  62. @var(num) register.
  63. @param(signed Indicates if the modulo must be signed)
  64. @param(denum Register containing the denominator
  65. @param(num Register containing the numerator, will also receive result)
  66. The actual optimizations regarding shifts have already
  67. been done and emitted, so this should really a do a modulo.
  68. }
  69. procedure emit_mod_reg_reg(signed: boolean;denum,num : tregister);virtual;abstract;
  70. {$ifndef cpu64bit}
  71. { This routine must do an actual 64-bit division, be it
  72. signed or unsigned. The result must set into the the
  73. @var(num) register.
  74. @param(signed Indicates if the division must be signed)
  75. @param(denum Register containing the denominator
  76. @param(num Register containing the numerator, will also receive result)
  77. The actual optimizations regarding shifts have already
  78. been done and emitted, so this should really a do a divide.
  79. Currently, this routine should only be implemented on
  80. 64-bit systems, otherwise a helper is called in 1st pass.
  81. }
  82. procedure emit64_div_reg_reg(signed: boolean;denum,num : tregister64);virtual;
  83. {$endif cpu64bit}
  84. end;
  85. tcgshlshrnode = class(tshlshrnode)
  86. {$ifndef cpu64bit}
  87. procedure second_64bit;virtual;
  88. {$endif cpu64bit}
  89. procedure second_integer;virtual;
  90. procedure pass_2;override;
  91. end;
  92. tcgnotnode = class(tnotnode)
  93. protected
  94. procedure second_boolean;virtual;abstract;
  95. {$ifdef SUPPORT_MMX}
  96. procedure second_mmx;virtual;abstract;
  97. {$endif SUPPORT_MMX}
  98. {$ifndef cpu64bit}
  99. procedure second_64bit;virtual;
  100. {$endif cpu64bit}
  101. procedure second_integer;virtual;
  102. public
  103. procedure pass_2;override;
  104. end;
  105. implementation
  106. uses
  107. globtype,systems,
  108. cutils,verbose,globals,
  109. symconst,aasmbase,aasmtai,aasmcpu,defutil,
  110. parabase,
  111. pass_2,
  112. ncon,
  113. tgobj,ncgutil,cgobj,paramgr
  114. {$ifndef cpu64bit}
  115. ,cg64f32
  116. {$endif cpu64bit}
  117. ;
  118. {*****************************************************************************
  119. TCGUNARYMINUSNODE
  120. *****************************************************************************}
  121. procedure tcgunaryminusnode.emit_float_sign_change(r: tregister; _size : tcgsize);
  122. var
  123. href,
  124. href2 : treference;
  125. begin
  126. { get a temporary memory reference to store the floating
  127. point value
  128. }
  129. tg.gettemp(exprasmlist,tcgsize2size[_size],tt_normal,href);
  130. { store the floating point value in the temporary memory area }
  131. cg.a_loadfpu_reg_ref(exprasmlist,_size,r,href);
  132. { only single and double ieee are supported, for little endian
  133. the signed bit is in the second dword }
  134. href2:=href;
  135. case _size of
  136. OS_F64 :
  137. if target_info.endian = endian_little then
  138. inc(href2.offset,4);
  139. OS_F32 :
  140. ;
  141. else
  142. internalerror(200406021);
  143. end;
  144. { flip sign-bit (bit 31/63) of single/double }
  145. cg.a_op_const_ref(exprasmlist,OP_XOR,OS_32,aint($80000000),href2);
  146. cg.a_loadfpu_ref_reg(exprasmlist,_size,href,r);
  147. tg.ungetiftemp(exprasmlist,href);
  148. end;
  149. {$ifndef cpu64bit}
  150. procedure tcgunaryminusnode.second_64bit;
  151. begin
  152. secondpass(left);
  153. { load left operator in a register }
  154. location_copy(location,left.location);
  155. location_force_reg(exprasmlist,location,OS_64,false);
  156. cg64.a_op64_loc_reg(exprasmlist,OP_NEG,
  157. location,joinreg64(location.registerlow,location.registerhigh));
  158. end;
  159. {$endif cpu64bit}
  160. procedure tcgunaryminusnode.second_float;
  161. begin
  162. secondpass(left);
  163. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  164. case left.location.loc of
  165. LOC_REFERENCE,
  166. LOC_CREFERENCE :
  167. begin
  168. location.register:=cg.getfpuregister(exprasmlist,location.size);
  169. cg.a_loadfpu_ref_reg(exprasmlist,
  170. def_cgsize(left.resulttype.def),
  171. left.location.reference,location.register);
  172. emit_float_sign_change(location.register,def_cgsize(left.resulttype.def));
  173. end;
  174. LOC_FPUREGISTER:
  175. begin
  176. location.register:=left.location.register;
  177. emit_float_sign_change(location.register,def_cgsize(left.resulttype.def));
  178. end;
  179. LOC_CFPUREGISTER:
  180. begin
  181. location.register:=cg.getfpuregister(exprasmlist,location.size);
  182. cg.a_loadfpu_reg_reg(exprasmlist,left.location.size,left.location.register,location.register);
  183. emit_float_sign_change(location.register,def_cgsize(left.resulttype.def));
  184. end;
  185. else
  186. internalerror(200306021);
  187. end;
  188. end;
  189. procedure tcgunaryminusnode.second_integer;
  190. begin
  191. secondpass(left);
  192. { load left operator in a register }
  193. location_copy(location,left.location);
  194. location_force_reg(exprasmlist,location,OS_INT,false);
  195. cg.a_op_reg_reg(exprasmlist,OP_NEG,OS_INT,location.register,location.register);
  196. end;
  197. procedure tcgunaryminusnode.pass_2;
  198. begin
  199. {$ifndef cpu64bit}
  200. if is_64bit(left.resulttype.def) then
  201. second_64bit
  202. else
  203. {$endif cpu64bit}
  204. {$ifdef SUPPORT_MMX}
  205. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype.def) then
  206. second_mmx
  207. else
  208. {$endif SUPPORT_MMX}
  209. if (left.resulttype.def.deftype=floatdef) then
  210. second_float
  211. else
  212. second_integer;
  213. end;
  214. {*****************************************************************************
  215. TCGMODDIVNODE
  216. *****************************************************************************}
  217. {$ifndef cpu64bit}
  218. procedure tcgmoddivnode.emit64_div_reg_reg(signed: boolean; denum,num:tregister64);
  219. begin
  220. { handled in pass_1 already, unless pass_1 is
  221. overriden
  222. }
  223. { should be handled in pass_1 (JM) }
  224. internalerror(200109052);
  225. end;
  226. {$endif cpu64bit}
  227. procedure tcgmoddivnode.pass_2;
  228. var
  229. hreg1 : tregister;
  230. hdenom : tregister;
  231. power : longint;
  232. hl : tasmlabel;
  233. paraloc1 : tcgpara;
  234. begin
  235. secondpass(left);
  236. if codegenerror then
  237. exit;
  238. secondpass(right);
  239. if codegenerror then
  240. exit;
  241. location_copy(location,left.location);
  242. {$ifndef cpu64bit}
  243. if is_64bit(resulttype.def) then
  244. begin
  245. { this code valid for 64-bit cpu's only ,
  246. otherwise helpers are called in pass_1
  247. }
  248. location_force_reg(exprasmlist,location,OS_64,false);
  249. location_copy(location,left.location);
  250. location_force_reg(exprasmlist,right.location,OS_64,false);
  251. emit64_div_reg_reg(is_signed(left.resulttype.def),
  252. joinreg64(right.location.registerlow,right.location.registerhigh),
  253. joinreg64(location.registerlow,location.registerhigh));
  254. end
  255. else
  256. {$endif cpu64bit}
  257. begin
  258. { put numerator in register }
  259. location_force_reg(exprasmlist,left.location,OS_INT,false);
  260. hreg1:=left.location.register;
  261. if (nodetype=divn) and
  262. (right.nodetype=ordconstn) and
  263. ispowerof2(tordconstnode(right).value,power) then
  264. Begin
  265. { for signed numbers, the numerator must be adjusted before the
  266. shift instruction, but not wih unsigned numbers! Otherwise,
  267. "Cardinal($ffffffff) div 16" overflows! (JM) }
  268. If is_signed(left.resulttype.def) Then
  269. Begin
  270. objectlibrary.getlabel(hl);
  271. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_GT,0,hreg1,hl);
  272. if power=1 then
  273. cg.a_op_const_reg(exprasmlist,OP_ADD,OS_INT,1,hreg1)
  274. else
  275. cg.a_op_const_reg(exprasmlist,OP_ADD,OS_INT,tordconstnode(right).value-1,hreg1);
  276. cg.a_label(exprasmlist,hl);
  277. cg.a_op_const_reg(exprasmlist,OP_SAR,OS_INT,power,hreg1);
  278. End
  279. Else { not signed }
  280. cg.a_op_const_reg(exprasmlist,OP_SHR,OS_INT,power,hreg1);
  281. End
  282. else
  283. begin
  284. { bring denominator to hdenom }
  285. { hdenom is always free, it's }
  286. { only used for temporary }
  287. { purposes }
  288. hdenom := cg.getintregister(exprasmlist,OS_INT);
  289. cg.a_load_loc_reg(exprasmlist,right.location.size,right.location,hdenom);
  290. { verify if the divisor is zero, if so return an error
  291. immediately
  292. }
  293. objectlibrary.getlabel(hl);
  294. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_NE,0,hdenom,hl);
  295. paraloc1.init;
  296. paramanager.getintparaloc(pocall_default,1,paraloc1);
  297. paramanager.allocparaloc(exprasmlist,paraloc1);
  298. cg.a_param_const(exprasmlist,OS_S32,200,paraloc1);
  299. paramanager.freeparaloc(exprasmlist,paraloc1);
  300. cg.a_call_name(exprasmlist,'FPC_HANDLERROR');
  301. paraloc1.done;
  302. cg.a_label(exprasmlist,hl);
  303. if nodetype = modn then
  304. emit_mod_reg_reg(is_signed(left.resulttype.def),hdenom,hreg1)
  305. else
  306. emit_div_reg_reg(is_signed(left.resulttype.def),hdenom,hreg1);
  307. end;
  308. location_reset(location,LOC_REGISTER,OS_INT);
  309. location.register:=hreg1;
  310. end;
  311. cg.g_overflowcheck(exprasmlist,location,resulttype.def);
  312. end;
  313. {*****************************************************************************
  314. TCGSHLRSHRNODE
  315. *****************************************************************************}
  316. {$ifndef cpu64bit}
  317. procedure tcgshlshrnode.second_64bit;
  318. begin
  319. { already hanled in 1st pass }
  320. internalerror(2002081501);
  321. end;
  322. {$endif cpu64bit}
  323. procedure tcgshlshrnode.second_integer;
  324. var
  325. op : topcg;
  326. hcountreg : tregister;
  327. begin
  328. { determine operator }
  329. case nodetype of
  330. shln: op:=OP_SHL;
  331. shrn: op:=OP_SHR;
  332. end;
  333. { load left operators in a register }
  334. location_copy(location,left.location);
  335. location_force_reg(exprasmlist,location,OS_INT,false);
  336. { shifting by a constant directly coded: }
  337. if (right.nodetype=ordconstn) then
  338. begin
  339. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)
  340. if right.value<=31 then
  341. }
  342. cg.a_op_const_reg(exprasmlist,op,location.size,
  343. tordconstnode(right).value and 31,location.register);
  344. {
  345. else
  346. emit_reg_reg(A_XOR,S_L,hregister1,
  347. hregister1);
  348. }
  349. end
  350. else
  351. begin
  352. { load right operators in a register - this
  353. is done since most target cpu which will use this
  354. node do not support a shift count in a mem. location (cec)
  355. }
  356. if right.location.loc<>LOC_REGISTER then
  357. begin
  358. hcountreg:=cg.getintregister(exprasmlist,OS_INT);
  359. cg.a_load_loc_reg(exprasmlist,right.location.size,right.location,hcountreg);
  360. end
  361. else
  362. hcountreg:=right.location.register;
  363. cg.a_op_reg_reg(exprasmlist,op,OS_INT,hcountreg,location.register);
  364. end;
  365. end;
  366. procedure tcgshlshrnode.pass_2;
  367. begin
  368. secondpass(left);
  369. secondpass(right);
  370. {$ifndef cpu64bit}
  371. if is_64bit(left.resulttype.def) then
  372. second_64bit
  373. else
  374. {$endif cpu64bit}
  375. second_integer;
  376. end;
  377. {*****************************************************************************
  378. TCGNOTNODE
  379. *****************************************************************************}
  380. {$ifndef cpu64bit}
  381. procedure tcgnotnode.second_64bit;
  382. begin
  383. secondpass(left);
  384. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  385. location_copy(location,left.location);
  386. { perform the NOT operation }
  387. cg64.a_op64_reg_reg(exprasmlist,OP_NOT,left.location.register64,location.register64);
  388. end;
  389. {$endif cpu64bit}
  390. procedure tcgnotnode.second_integer;
  391. begin
  392. secondpass(left);
  393. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),false);
  394. location_copy(location,left.location);
  395. { perform the NOT operation }
  396. cg.a_op_reg_reg(exprasmlist,OP_NOT,location.size,location.register,location.register);
  397. end;
  398. procedure tcgnotnode.pass_2;
  399. begin
  400. if is_boolean(resulttype.def) then
  401. second_boolean
  402. {$ifdef SUPPORT_MMX}
  403. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype.def) then
  404. second_mmx
  405. {$endif SUPPORT_MMX}
  406. {$ifndef cpu64bit}
  407. else if is_64bit(left.resulttype.def) then
  408. second_64bit
  409. {$endif cpu64bit}
  410. else
  411. second_integer;
  412. end;
  413. begin
  414. cmoddivnode:=tcgmoddivnode;
  415. cunaryminusnode:=tcgunaryminusnode;
  416. cshlshrnode:=tcgshlshrnode;
  417. cnotnode:=tcgnotnode;
  418. end.
  419. {
  420. $Log$
  421. Revision 1.29 2004-09-25 14:23:54 peter
  422. * ungetregister is now only used for cpuregisters, renamed to
  423. ungetcpuregister
  424. * renamed (get|unget)explicitregister(s) to ..cpuregister
  425. * removed location-release/reference_release
  426. Revision 1.28 2004/09/21 17:25:12 peter
  427. * paraloc branch merged
  428. Revision 1.27.4.1 2004/08/31 20:43:06 peter
  429. * paraloc patch
  430. Revision 1.27 2004/06/20 08:55:29 florian
  431. * logs truncated
  432. Revision 1.26 2004/06/16 20:07:08 florian
  433. * dwarf branch merged
  434. Revision 1.25.2.4 2004/06/02 19:04:51 peter
  435. * fixed minusunary for float
  436. Revision 1.25.2.3 2004/05/31 16:39:42 peter
  437. * add ungetiftemp in a few locations
  438. Revision 1.25.2.2 2004/05/30 17:07:07 peter
  439. * fix shl shr for sparc
  440. Revision 1.25.2.1 2004/04/27 18:18:25 peter
  441. * aword -> aint
  442. }