n386inl.pas 18 KB

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