n386inl.pas 17 KB

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