n386inl.pas 16 KB

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