ncgmat.pas 18 KB

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