n386inl.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 inline 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 n386inl;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ninl,ncginl;
  23. type
  24. ti386inlinenode = class(tcginlinenode)
  25. { first pass override
  26. so that the code generator will actually generate
  27. these nodes.
  28. }
  29. function first_pi: tnode ; override;
  30. function first_arctan_real: tnode; override;
  31. function first_abs_real: tnode; override;
  32. function first_sqr_real: tnode; override;
  33. function first_sqrt_real: tnode; override;
  34. function first_ln_real: tnode; override;
  35. function first_cos_real: tnode; override;
  36. function first_sin_real: tnode; override;
  37. { second pass override to generate these nodes }
  38. procedure second_IncludeExclude;override;
  39. procedure second_pi; override;
  40. procedure second_arctan_real; override;
  41. procedure second_abs_real; override;
  42. procedure second_sqr_real; override;
  43. procedure second_sqrt_real; override;
  44. procedure second_ln_real; override;
  45. procedure second_cos_real; override;
  46. procedure second_sin_real; override;
  47. procedure second_prefetch;override;
  48. private
  49. procedure load_fpu_location;
  50. end;
  51. implementation
  52. uses
  53. systems,
  54. globals,
  55. cutils,verbose,
  56. defutil,
  57. aasmtai,aasmcpu,
  58. cgbase,pass_2,
  59. cpuinfo,cpubase,paramgr,
  60. nbas,ncon,ncal,ncnv,nld,
  61. cga,cgx86,cgobj;
  62. {*****************************************************************************
  63. TI386INLINENODE
  64. *****************************************************************************}
  65. function ti386inlinenode.first_pi : tnode;
  66. begin
  67. expectloc:=LOC_FPUREGISTER;
  68. registersfpu:=1;
  69. first_pi := nil;
  70. end;
  71. function ti386inlinenode.first_arctan_real : tnode;
  72. begin
  73. expectloc:=LOC_FPUREGISTER;
  74. registersint:=left.registersint;
  75. registersfpu:=max(left.registersfpu,2);
  76. {$ifdef SUPPORT_MMX}
  77. registersmmx:=left.registersmmx;
  78. {$endif SUPPORT_MMX}
  79. first_arctan_real := nil;
  80. end;
  81. function ti386inlinenode.first_abs_real : tnode;
  82. begin
  83. expectloc:=LOC_FPUREGISTER;
  84. registersint:=left.registersint;
  85. registersfpu:=max(left.registersfpu,1);
  86. {$ifdef SUPPORT_MMX}
  87. registersmmx:=left.registersmmx;
  88. {$endif SUPPORT_MMX}
  89. first_abs_real := nil;
  90. end;
  91. function ti386inlinenode.first_sqr_real : tnode;
  92. begin
  93. expectloc:=LOC_FPUREGISTER;
  94. registersint:=left.registersint;
  95. registersfpu:=max(left.registersfpu,1);
  96. {$ifdef SUPPORT_MMX}
  97. registersmmx:=left.registersmmx;
  98. {$endif SUPPORT_MMX}
  99. first_sqr_real := nil;
  100. end;
  101. function ti386inlinenode.first_sqrt_real : tnode;
  102. begin
  103. expectloc:=LOC_FPUREGISTER;
  104. registersint:=left.registersint;
  105. registersfpu:=max(left.registersfpu,1);
  106. {$ifdef SUPPORT_MMX}
  107. registersmmx:=left.registersmmx;
  108. {$endif SUPPORT_MMX}
  109. first_sqrt_real := nil;
  110. end;
  111. function ti386inlinenode.first_ln_real : tnode;
  112. begin
  113. expectloc:=LOC_FPUREGISTER;
  114. registersint:=left.registersint;
  115. registersfpu:=max(left.registersfpu,2);
  116. {$ifdef SUPPORT_MMX}
  117. registersmmx:=left.registersmmx;
  118. {$endif SUPPORT_MMX}
  119. first_ln_real := nil;
  120. end;
  121. function ti386inlinenode.first_cos_real : tnode;
  122. begin
  123. expectloc:=LOC_FPUREGISTER;
  124. registersint:=left.registersint;
  125. registersfpu:=max(left.registersfpu,1);
  126. {$ifdef SUPPORT_MMX}
  127. registersmmx:=left.registersmmx;
  128. {$endif SUPPORT_MMX}
  129. first_cos_real := nil;
  130. end;
  131. function ti386inlinenode.first_sin_real : tnode;
  132. begin
  133. expectloc:=LOC_FPUREGISTER;
  134. registersint:=left.registersint;
  135. registersfpu:=max(left.registersfpu,1);
  136. {$ifdef SUPPORT_MMX}
  137. registersmmx:=left.registersmmx;
  138. {$endif SUPPORT_MMX}
  139. first_sin_real := nil;
  140. end;
  141. procedure ti386inlinenode.second_Pi;
  142. begin
  143. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  144. emit_none(A_FLDPI,S_NO);
  145. tcgx86(cg).inc_fpu_stack;
  146. location.register:=NR_FPU_RESULT_REG;
  147. end;
  148. { load the FPU into the an fpu register }
  149. procedure ti386inlinenode.load_fpu_location;
  150. begin
  151. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  152. location.register:=NR_FPU_RESULT_REG;
  153. secondpass(left);
  154. case left.location.loc of
  155. LOC_FPUREGISTER:
  156. ;
  157. LOC_CFPUREGISTER:
  158. begin
  159. cg.a_loadfpu_reg_reg(exprasmlist,left.location.size,
  160. left.location.register,location.register);
  161. end;
  162. LOC_REFERENCE,LOC_CREFERENCE:
  163. begin
  164. cg.a_loadfpu_ref_reg(exprasmlist,
  165. def_cgsize(left.resulttype.def),
  166. left.location.reference,location.register);
  167. location_release(exprasmlist,left.location);
  168. end
  169. else
  170. internalerror(309991);
  171. end;
  172. end;
  173. procedure ti386inlinenode.second_arctan_real;
  174. begin
  175. load_fpu_location;
  176. emit_none(A_FLD1,S_NO);
  177. emit_none(A_FPATAN,S_NO);
  178. end;
  179. procedure ti386inlinenode.second_abs_real;
  180. begin
  181. load_fpu_location;
  182. emit_none(A_FABS,S_NO);
  183. end;
  184. procedure ti386inlinenode.second_sqr_real;
  185. begin
  186. load_fpu_location;
  187. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  188. end;
  189. procedure ti386inlinenode.second_sqrt_real;
  190. begin
  191. load_fpu_location;
  192. emit_none(A_FSQRT,S_NO);
  193. end;
  194. procedure ti386inlinenode.second_ln_real;
  195. begin
  196. load_fpu_location;
  197. emit_none(A_FLDLN2,S_NO);
  198. emit_none(A_FXCH,S_NO);
  199. emit_none(A_FYL2X,S_NO);
  200. end;
  201. procedure ti386inlinenode.second_cos_real;
  202. begin
  203. load_fpu_location;
  204. emit_none(A_FCOS,S_NO);
  205. end;
  206. procedure ti386inlinenode.second_sin_real;
  207. begin
  208. load_fpu_location;
  209. emit_none(A_FSIN,S_NO)
  210. end;
  211. procedure ti386inlinenode.second_prefetch;
  212. var
  213. ref : treference;
  214. r : tregister;
  215. begin
  216. if aktspecificoptprocessor>=ClassPentium3 then
  217. begin
  218. secondpass(left);
  219. case left.location.loc of
  220. LOC_CREFERENCE,
  221. LOC_REFERENCE:
  222. begin
  223. r:=cg.getintregister(exprasmlist,OS_ADDR);
  224. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,r);
  225. location_release(exprasmlist,left.location);
  226. reference_reset(ref);
  227. ref.base:=r;
  228. exprasmlist.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  229. cg.ungetregister(exprasmlist,r);
  230. end;
  231. else
  232. internalerror(200402021);
  233. end;
  234. end;
  235. end;
  236. {*****************************************************************************
  237. INCLUDE/EXCLUDE GENERIC HANDLING
  238. *****************************************************************************}
  239. procedure ti386inlinenode.second_IncludeExclude;
  240. var
  241. hregister : tregister;
  242. asmop : tasmop;
  243. L : cardinal;
  244. cgop : topcg;
  245. begin
  246. secondpass(tcallparanode(left).left);
  247. if tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn then
  248. begin
  249. { calculate bit position }
  250. l:=cardinal(1 shl (tordconstnode(tcallparanode(tcallparanode(left).right).left).value mod 32));
  251. { determine operator }
  252. if inlinenumber=in_include_x_y then
  253. cgop:=OP_OR
  254. else
  255. begin
  256. cgop:=OP_AND;
  257. l:=not(l);
  258. end;
  259. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  260. begin
  261. inc(tcallparanode(left).left.location.reference.offset,
  262. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value div 32)*4);
  263. cg.a_op_const_ref(exprasmlist,cgop,OS_INT,l,tcallparanode(left).left.location.reference);
  264. location_release(exprasmlist,tcallparanode(left).left.location);
  265. end
  266. else
  267. { LOC_CREGISTER }
  268. begin
  269. cg.a_op_const_reg(exprasmlist,cgop,tcallparanode(left).left.location.size,
  270. l,tcallparanode(left).left.location.register);
  271. end;
  272. end
  273. else
  274. begin
  275. { generate code for the element to set }
  276. secondpass(tcallparanode(tcallparanode(left).right).left);
  277. { determine asm operator }
  278. if inlinenumber=in_include_x_y then
  279. asmop:=A_BTS
  280. else
  281. asmop:=A_BTR;
  282. if tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  283. { we don't need a mod 32 because this is done automatically }
  284. { by the bts instruction. For proper checking we would }
  285. { note: bts doesn't do any mod'ing, that's why we can also use }
  286. { it for normalsets! (JM) }
  287. { need a cmp and jmp, but this should be done by the }
  288. { type cast code which does range checking if necessary (FK) }
  289. begin
  290. hregister:=cg.makeregsize(Tcallparanode(Tcallparanode(left).right).left.location.register,OS_INT);
  291. end
  292. else
  293. begin
  294. hregister:=cg.getintregister(exprasmlist,OS_INT);
  295. end;
  296. location_release(exprasmlist,tcallparanode(tcallparanode(left).right).left.location);
  297. cg.a_load_loc_reg(exprasmlist,OS_INT,tcallparanode(tcallparanode(left).right).left.location,hregister);
  298. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  299. emit_reg_ref(asmop,S_L,hregister,tcallparanode(left).left.location.reference)
  300. else
  301. emit_reg_reg(asmop,S_L,hregister,tcallparanode(left).left.location.register);
  302. cg.ungetregister(exprasmlist,hregister);
  303. location_release(exprasmlist,Tcallparanode(left).left.location);
  304. end;
  305. end;
  306. begin
  307. cinlinenode:=ti386inlinenode;
  308. end.
  309. {
  310. $Log$
  311. Revision 1.72 2004-02-03 22:32:54 peter
  312. * renamed xNNbittype to xNNinttype
  313. * renamed registers32 to registersint
  314. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  315. Revision 1.71 2004/02/02 20:41:59 florian
  316. + added prefetch(const mem) support
  317. Revision 1.70 2003/10/10 17:48:14 peter
  318. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  319. * tregisteralloctor renamed to trgobj
  320. * removed rgobj from a lot of units
  321. * moved location_* and reference_* to cgobj
  322. * first things for mmx register allocation
  323. Revision 1.69 2003/10/09 21:31:37 daniel
  324. * Register allocator splitted, ans abstract now
  325. Revision 1.68 2003/10/01 20:34:49 peter
  326. * procinfo unit contains tprocinfo
  327. * cginfo renamed to cgbase
  328. * moved cgmessage to verbose
  329. * fixed ppc and sparc compiles
  330. Revision 1.67 2003/09/28 21:48:20 peter
  331. * fix register leaks
  332. Revision 1.66 2003/09/07 22:09:35 peter
  333. * preparations for different default calling conventions
  334. * various RA fixes
  335. Revision 1.65 2003/09/03 15:55:01 peter
  336. * NEWRA branch merged
  337. Revision 1.64.2.2 2003/08/31 15:46:26 peter
  338. * more updates for tregister
  339. Revision 1.64.2.1 2003/08/29 17:29:00 peter
  340. * next batch of updates
  341. Revision 1.64 2003/07/02 22:18:04 peter
  342. * paraloc splitted in callerparaloc,calleeparaloc
  343. * sparc calling convention updates
  344. Revision 1.63 2003/06/03 13:01:59 daniel
  345. * Register allocator finished
  346. Revision 1.62 2003/06/01 21:38:06 peter
  347. * getregisterfpu size parameter added
  348. * op_const_reg size parameter added
  349. * sparc updates
  350. Revision 1.61 2003/05/30 23:49:18 jonas
  351. * a_load_loc_reg now has an extra size parameter for the destination
  352. register (properly fixes what I worked around in revision 1.106 of
  353. ncgutil.pas)
  354. Revision 1.60 2003/04/23 09:50:31 peter
  355. * wrong location_copy for include/exclude
  356. Revision 1.59 2003/04/22 23:50:23 peter
  357. * firstpass uses expectloc
  358. * checks if there are differences between the expectloc and
  359. location.loc from secondpass in EXTDEBUG
  360. Revision 1.58 2003/04/22 14:33:38 peter
  361. * removed some notes/hints
  362. Revision 1.57 2003/04/22 10:09:35 daniel
  363. + Implemented the actual register allocator
  364. + Scratch registers unavailable when new register allocator used
  365. + maybe_save/maybe_restore unavailable when new register allocator used
  366. Revision 1.56 2003/02/19 22:00:15 daniel
  367. * Code generator converted to new register notation
  368. - Horribily outdated todo.txt removed
  369. Revision 1.55 2003/01/08 18:43:57 daniel
  370. * Tregister changed into a record
  371. Revision 1.54 2002/11/25 17:43:26 peter
  372. * splitted defbase in defutil,symutil,defcmp
  373. * merged isconvertable and is_equal into compare_defs(_ext)
  374. * made operator search faster by walking the list only once
  375. Revision 1.53 2002/09/07 15:25:10 peter
  376. * old logs removed and tabs fixed
  377. Revision 1.52 2002/08/02 07:44:31 jonas
  378. * made assigned() handling generic
  379. * add nodes now can also evaluate constant expressions at compile time
  380. that contain nil nodes
  381. Revision 1.51 2002/07/26 11:16:35 jonas
  382. * fixed (actual and potential) range errors
  383. Revision 1.50 2002/07/25 18:02:33 carl
  384. + added generic inline nodes
  385. Revision 1.49 2002/07/20 11:58:02 florian
  386. * types.pas renamed to defbase.pas because D6 contains a types
  387. unit so this would conflicts if D6 programms are compiled
  388. + Willamette/SSE2 instructions to assembler added
  389. Revision 1.48 2002/07/11 14:41:33 florian
  390. * start of the new generic parameter handling
  391. Revision 1.47 2002/07/07 09:52:34 florian
  392. * powerpc target fixed, very simple units can be compiled
  393. * some basic stuff for better callparanode handling, far from being finished
  394. Revision 1.46 2002/07/01 18:46:33 peter
  395. * internal linker
  396. * reorganized aasm layer
  397. Revision 1.45 2002/07/01 16:23:56 peter
  398. * cg64 patch
  399. * basics for currency
  400. * asnode updates for class and interface (not finished)
  401. Revision 1.44 2002/05/18 13:34:25 peter
  402. * readded missing revisions
  403. Revision 1.43 2002/05/16 19:46:51 carl
  404. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  405. + try to fix temp allocation (still in ifdef)
  406. + generic constructor calls
  407. + start of tassembler / tmodulebase class cleanup
  408. Revision 1.41 2002/05/13 19:54:38 peter
  409. * removed n386ld and n386util units
  410. * maybe_save/maybe_restore added instead of the old maybe_push
  411. Revision 1.40 2002/05/12 16:53:17 peter
  412. * moved entry and exitcode to ncgutil and cgobj
  413. * foreach gets extra argument for passing local data to the
  414. iterator function
  415. * -CR checks also class typecasts at runtime by changing them
  416. into as
  417. * fixed compiler to cycle with the -CR option
  418. * fixed stabs with elf writer, finally the global variables can
  419. be watched
  420. * removed a lot of routines from cga unit and replaced them by
  421. calls to cgobj
  422. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  423. u32bit then the other is typecasted also to u32bit without giving
  424. a rangecheck warning/error.
  425. * fixed pascal calling method with reversing also the high tree in
  426. the parast, detected by tcalcst3 test
  427. Revision 1.39 2002/04/23 19:16:35 peter
  428. * add pinline unit that inserts compiler supported functions using
  429. one or more statements
  430. * moved finalize and setlength from ninl to pinline
  431. Revision 1.38 2002/04/21 15:35:54 carl
  432. * changeregsize -> rg.makeregsize
  433. Revision 1.37 2002/04/19 15:39:35 peter
  434. * removed some more routines from cga
  435. * moved location_force_reg/mem to ncgutil
  436. * moved arrayconstructnode secondpass to ncgld
  437. Revision 1.36 2002/04/15 19:44:21 peter
  438. * fixed stackcheck that would be called recursively when a stack
  439. error was found
  440. * generic changeregsize(reg,size) for i386 register resizing
  441. * removed some more routines from cga unit
  442. * fixed returnvalue handling
  443. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  444. Revision 1.35 2002/04/04 19:06:11 peter
  445. * removed unused units
  446. * use tlocation.size in cg.a_*loc*() routines
  447. Revision 1.34 2002/04/02 17:11:36 peter
  448. * tlocation,treference update
  449. * LOC_CONSTANT added for better constant handling
  450. * secondadd splitted in multiple routines
  451. * location_force_reg added for loading a location to a register
  452. of a specified size
  453. * secondassignment parses now first the right and then the left node
  454. (this is compatible with Kylix). This saves a lot of push/pop especially
  455. with string operations
  456. * adapted some routines to use the new cg methods
  457. Revision 1.33 2002/03/31 20:26:39 jonas
  458. + a_loadfpu_* and a_loadmm_* methods in tcg
  459. * register allocation is now second_d by a class and is mostly processor
  460. independent (+rgobj.pas and i386/rgcpu.pas)
  461. * temp allocation is now second_d by a class (+tgobj.pas, -i386\tgcpu.pas)
  462. * some small improvements and fixes to the optimizer
  463. * some register allocation fixes
  464. * some fpuvaroffset fixes in the unary minus node
  465. * push/popusedregisters is now called rg.save/restoreusedregisters and
  466. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  467. also better optimizable)
  468. * fixed and optimized register saving/restoring for new/dispose nodes
  469. * LOC_FPU locations now also require their "register" field to be set to
  470. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  471. - list field removed of the tnode class because it's not used currently
  472. and can cause hard-to-find bugs
  473. Revision 1.32 2002/03/04 19:10:14 peter
  474. * removed compiler warnings
  475. }