n386inl.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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;
  23. type
  24. ti386inlinenode = class(tinlinenode)
  25. procedure pass_2;override;
  26. end;
  27. implementation
  28. uses
  29. globtype,systems,
  30. cutils,verbose,globals,fmodule,
  31. symconst,symdef,aasm,types,
  32. cginfo,cgbase,pass_1,pass_2,
  33. cpubase,
  34. nbas,ncon,ncal,ncnv,nld,
  35. cga,tgobj,ncgutil,cgobj,cg64f32,rgobj,rgcpu;
  36. {*****************************************************************************
  37. TI386INLINENODE
  38. *****************************************************************************}
  39. procedure ti386inlinenode.pass_2;
  40. const
  41. {tfloattype = (s32real,s64real,s80real,s64bit,f16bit,f32bit);}
  42. { float_name: array[tfloattype] of string[8]=
  43. ('S32REAL','S64REAL','S80REAL','S64BIT','F16BIT','F32BIT'); }
  44. addsubop:array[in_inc_x..in_dec_x] of TOpCG=(OP_ADD,OP_SUB);
  45. var
  46. asmop : tasmop;
  47. {inc/dec}
  48. addconstant : boolean;
  49. addvalue : longint;
  50. href : treference;
  51. hp2 : tstringconstnode;
  52. l : longint;
  53. pushedregs : tmaybesave;
  54. hregisterhi,
  55. hregister : tregister;
  56. lengthlab,
  57. otlabel,oflabel{,l1} : tasmlabel;
  58. oldpushedparasize : longint;
  59. cgop : TOpCG;
  60. cgsize : TCGSize;
  61. begin
  62. { save & reset pushedparasize }
  63. oldpushedparasize:=pushedparasize;
  64. pushedparasize:=0;
  65. case inlinenumber of
  66. in_assert_x_y:
  67. begin
  68. { the node should be removed in the firstpass }
  69. if not (cs_do_assertion in aktlocalswitches) then
  70. internalerror(7123458);
  71. otlabel:=truelabel;
  72. oflabel:=falselabel;
  73. getlabel(truelabel);
  74. getlabel(falselabel);
  75. secondpass(tcallparanode(left).left);
  76. maketojumpbool(exprasmlist,tcallparanode(left).left,lr_load_regvars);
  77. cg.a_label(exprasmlist,falselabel);
  78. { erroraddr }
  79. cg.a_param_reg(exprasmlist,OS_ADDR,R_EBP,4);
  80. { lineno }
  81. cg.a_param_const(exprasmlist,OS_INT,aktfilepos.line,3);
  82. { filename string }
  83. hp2:=cstringconstnode.createstr(current_module.sourcefiles.get_file_name(aktfilepos.fileindex),st_shortstring);
  84. firstpass(hp2);
  85. secondpass(hp2);
  86. if codegenerror then
  87. exit;
  88. cg.a_paramaddr_ref(exprasmlist,hp2.location.reference,2);
  89. hp2.free;
  90. { push msg }
  91. secondpass(tcallparanode(tcallparanode(left).right).left);
  92. cg.a_paramaddr_ref(exprasmlist,tcallparanode(tcallparanode(left).right).left.location.reference,1);
  93. { call }
  94. cg.a_call_name(exprasmlist,'FPC_ASSERT');
  95. cg.a_label(exprasmlist,truelabel);
  96. truelabel:=otlabel;
  97. falselabel:=oflabel;
  98. end;
  99. in_sizeof_x,
  100. in_typeof_x :
  101. begin
  102. location_reset(location,LOC_REGISTER,OS_ADDR);
  103. { for both cases load vmt }
  104. if left.nodetype=typen then
  105. begin
  106. hregister:=rg.getaddressregister(exprasmlist);
  107. reference_reset_symbol(href,newasmsymbol(tobjectdef(left.resulttype.def).vmt_mangledname),0);
  108. cg.a_loadaddr_ref_reg(exprasmlist,href,hregister);
  109. end
  110. else
  111. begin
  112. secondpass(left);
  113. location_release(exprasmlist,left.location);
  114. hregister:=rg.getaddressregister(exprasmlist);
  115. { load VMT pointer }
  116. inc(left.location.reference.offset,tobjectdef(left.resulttype.def).vmt_offset);
  117. cg.a_load_ref_reg(exprasmlist,OS_ADDR,left.location.reference,hregister);
  118. end;
  119. { in sizeof load size }
  120. if inlinenumber=in_sizeof_x then
  121. begin
  122. reference_reset_base(href,hregister,0);
  123. rg.ungetaddressregister(exprasmlist,hregister);
  124. hregister:=rg.getregisterint(exprasmlist);
  125. cg.a_load_ref_reg(exprasmlist,OS_INT,href,hregister);
  126. end;
  127. location.register:=hregister;
  128. end;
  129. in_length_x :
  130. begin
  131. secondpass(left);
  132. { length in ansi strings is at offset -8 }
  133. if is_ansistring(left.resulttype.def) or
  134. is_widestring(left.resulttype.def) then
  135. begin
  136. location_force_reg(exprasmlist,left.location,OS_ADDR,false);
  137. hregister:=left.location.register;
  138. getlabel(lengthlab);
  139. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,hregister,lengthlab);
  140. reference_reset_base(href,hregister,-8);
  141. cg.a_load_ref_reg(exprasmlist,OS_INT,href,hregister);
  142. cg.a_label(exprasmlist,lengthlab);
  143. location_reset(location,LOC_REGISTER,OS_INT);
  144. location.register:=hregister;
  145. end
  146. else
  147. begin
  148. location_copy(location,left.location);
  149. location.size:=OS_8;
  150. end;
  151. end;
  152. in_pred_x,
  153. in_succ_x:
  154. begin
  155. secondpass(left);
  156. if inlinenumber=in_pred_x then
  157. cgop:=OP_SUB
  158. else
  159. cgop:=OP_ADD;
  160. cgsize:=def_cgsize(resulttype.def);
  161. { we need a value in a register }
  162. location_copy(location,left.location);
  163. location_force_reg(exprasmlist,location,cgsize,false);
  164. if cgsize in [OS_64,OS_S64] then
  165. cg64.a_op64_const_reg(exprasmlist,cgop,1,
  166. location.register64)
  167. else
  168. cg.a_op_const_reg(exprasmlist,cgop,1,location.register);
  169. cg.g_overflowcheck(exprasmlist,self);
  170. cg.g_rangecheck(exprasmlist,self,resulttype.def);
  171. end;
  172. in_dec_x,
  173. in_inc_x :
  174. begin
  175. { set defaults }
  176. addconstant:=true;
  177. { load first parameter, must be a reference }
  178. secondpass(tcallparanode(left).left);
  179. cgsize:=def_cgsize(tcallparanode(left).left.resulttype.def);
  180. { get addvalue }
  181. case tcallparanode(left).left.resulttype.def.deftype of
  182. orddef,
  183. enumdef :
  184. addvalue:=1;
  185. pointerdef :
  186. begin
  187. if is_void(tpointerdef(tcallparanode(left).left.resulttype.def).pointertype.def) then
  188. addvalue:=1
  189. else
  190. addvalue:=tpointerdef(tcallparanode(left).left.resulttype.def).pointertype.def.size;
  191. end;
  192. else
  193. internalerror(10081);
  194. end;
  195. { second argument specified?, must be a s32bit in register }
  196. if assigned(tcallparanode(left).right) then
  197. begin
  198. maybe_save(exprasmlist,tcallparanode(tcallparanode(left).right).left.registers32,
  199. tcallparanode(left).left.location,pushedregs);
  200. secondpass(tcallparanode(tcallparanode(left).right).left);
  201. maybe_restore(exprasmlist,tcallparanode(left).left.location,pushedregs);
  202. { when constant, just multiply the addvalue }
  203. if is_constintnode(tcallparanode(tcallparanode(left).right).left) then
  204. addvalue:=addvalue*get_ordinal_value(tcallparanode(tcallparanode(left).right).left)
  205. else
  206. begin
  207. location_force_reg(exprasmlist,tcallparanode(tcallparanode(left).right).left.location,cgsize,false);
  208. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  209. hregisterhi:=tcallparanode(tcallparanode(left).right).left.location.registerhigh;
  210. { insert multiply with addvalue if its >1 }
  211. if addvalue>1 then
  212. cg.a_op_const_reg(exprasmlist,OP_IMUL,addvalue,hregister);
  213. addconstant:=false;
  214. end;
  215. end;
  216. { write the add instruction }
  217. if addconstant then
  218. begin
  219. if cgsize in [OS_64,OS_S64] then
  220. cg64.a_op64_const_loc(exprasmlist,addsubop[inlinenumber],
  221. addvalue,tcallparanode(left).left.location)
  222. else
  223. cg.a_op_const_loc(exprasmlist,addsubop[inlinenumber],
  224. addvalue,tcallparanode(left).left.location);
  225. end
  226. else
  227. begin
  228. if cgsize in [OS_64,OS_S64] then
  229. cg64.a_op64_reg_loc(exprasmlist,addsubop[inlinenumber],
  230. joinreg64(hregister,hregisterhi),tcallparanode(left).left.location)
  231. else
  232. cg.a_op_reg_loc(exprasmlist,addsubop[inlinenumber],
  233. hregister,tcallparanode(left).left.location);
  234. location_release(exprasmlist,tcallparanode(tcallparanode(left).right).left.location);
  235. end;
  236. cg.g_overflowcheck(exprasmlist,tcallparanode(left).left);
  237. cg.g_rangecheck(exprasmlist,tcallparanode(left).left,tcallparanode(left).left.resulttype.def);
  238. end;
  239. in_typeinfo_x:
  240. begin
  241. location_reset(location,LOC_REGISTER,OS_ADDR);
  242. location.register:=rg.getregisterint(exprasmlist);
  243. reference_reset_symbol(href,tstoreddef(ttypenode(tcallparanode(left).left).resulttype.def).get_rtti_label(fullrtti),0);
  244. emit_ref_reg(A_LEA,S_L,href,location.register);
  245. end;
  246. in_assigned_x :
  247. begin
  248. secondpass(tcallparanode(left).left);
  249. location_release(exprasmlist,tcallparanode(left).left.location);
  250. if (tcallparanode(left).left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  251. begin
  252. emit_reg_reg(A_OR,S_L,
  253. tcallparanode(left).left.location.register,
  254. tcallparanode(left).left.location.register);
  255. end
  256. else
  257. begin
  258. emit_const_ref(A_CMP,S_L,0,tcallparanode(left).left.location.reference);
  259. end;
  260. location_reset(location,LOC_FLAGS,OS_NO);
  261. location.resflags:=F_NE;
  262. end;
  263. in_include_x_y,
  264. in_exclude_x_y:
  265. begin
  266. location_copy(location,left.location);
  267. secondpass(tcallparanode(left).left);
  268. if tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn then
  269. begin
  270. { calculate bit position }
  271. l:=1 shl (tordconstnode(tcallparanode(tcallparanode(left).right).left).value mod 32);
  272. { determine operator }
  273. if inlinenumber=in_include_x_y then
  274. asmop:=A_OR
  275. else
  276. begin
  277. asmop:=A_AND;
  278. l:=not(l);
  279. end;
  280. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  281. begin
  282. inc(tcallparanode(left).left.location.reference.offset,
  283. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value div 32)*4);
  284. emit_const_ref(asmop,S_L,l,tcallparanode(left).left.location.reference);
  285. location_release(exprasmlist,tcallparanode(left).left.location);
  286. end
  287. else
  288. { LOC_CREGISTER }
  289. begin
  290. secondpass(tcallparanode(left).left);
  291. emit_const_reg(asmop,S_L,
  292. l,tcallparanode(left).left.location.register);
  293. end;
  294. end
  295. else
  296. begin
  297. { generate code for the element to set }
  298. maybe_save(exprasmlist,tcallparanode(tcallparanode(left).right).left.registers32,
  299. tcallparanode(left).left.location,pushedregs);
  300. secondpass(tcallparanode(tcallparanode(left).right).left);
  301. maybe_restore(exprasmlist,tcallparanode(left).left.location,pushedregs);
  302. { determine asm operator }
  303. if inlinenumber=in_include_x_y then
  304. asmop:=A_BTS
  305. else
  306. asmop:=A_BTR;
  307. if tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  308. { we don't need a mod 32 because this is done automatically }
  309. { by the bts instruction. For proper checking we would }
  310. { note: bts doesn't do any mod'ing, that's why we can also use }
  311. { it for normalsets! (JM) }
  312. { need a cmp and jmp, but this should be done by the }
  313. { type cast code which does range checking if necessary (FK) }
  314. begin
  315. hregister := rg.makeregsize(tcallparanode(tcallparanode(left).right).left.location.register,OS_INT);
  316. end
  317. else
  318. begin
  319. rg.getexplicitregisterint(exprasmlist,R_EDI);
  320. hregister:=R_EDI;
  321. end;
  322. cg.a_load_loc_reg(exprasmlist,tcallparanode(tcallparanode(left).right).left.location,hregister);
  323. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  324. emit_reg_ref(asmop,S_L,hregister,tcallparanode(left).left.location.reference)
  325. else
  326. emit_reg_reg(asmop,S_L,hregister,tcallparanode(left).left.location.register);
  327. if hregister = R_EDI then
  328. rg.ungetregisterint(exprasmlist,R_EDI);
  329. end;
  330. end;
  331. in_pi:
  332. begin
  333. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  334. emit_none(A_FLDPI,S_NO);
  335. inc(trgcpu(rg).fpuvaroffset);
  336. location.register:=R_ST;
  337. end;
  338. in_sin_extended,
  339. in_arctan_extended,
  340. in_abs_extended,
  341. in_sqr_extended,
  342. in_sqrt_extended,
  343. in_ln_extended,
  344. in_cos_extended:
  345. begin
  346. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  347. location.register:=R_ST;
  348. secondpass(left);
  349. case left.location.loc of
  350. LOC_FPUREGISTER:
  351. ;
  352. LOC_CFPUREGISTER:
  353. begin
  354. cg.a_loadfpu_reg_reg(exprasmlist,
  355. left.location.register,location.register);
  356. end;
  357. LOC_REFERENCE,LOC_CREFERENCE:
  358. begin
  359. cg.a_loadfpu_ref_reg(exprasmlist,
  360. def_cgsize(left.resulttype.def),
  361. left.location.reference,location.register);
  362. location_release(exprasmlist,left.location);
  363. end
  364. else
  365. internalerror(309991);
  366. end;
  367. case inlinenumber of
  368. in_sin_extended,
  369. in_cos_extended:
  370. begin
  371. if inlinenumber=in_sin_extended then
  372. emit_none(A_FSIN,S_NO)
  373. else
  374. emit_none(A_FCOS,S_NO);
  375. {
  376. getlabel(l1);
  377. emit_reg(A_FNSTSW,S_NO,R_AX);
  378. emit_none(A_SAHF,S_NO);
  379. emitjmp(C_NP,l1);
  380. emit_reg(A_FSTP,S_NO,R_ST0);
  381. emit_none(A_FLDZ,S_NO);
  382. cg.a_label(exprasmlist,l1);
  383. }
  384. end;
  385. in_arctan_extended:
  386. begin
  387. emit_none(A_FLD1,S_NO);
  388. emit_none(A_FPATAN,S_NO);
  389. end;
  390. in_abs_extended:
  391. emit_none(A_FABS,S_NO);
  392. in_sqr_extended:
  393. begin
  394. (* emit_reg(A_FLD,S_NO,R_ST0);
  395. { emit_none(A_FMULP,S_NO); nasm does not accept this PM }
  396. emit_reg_reg(A_FMULP,S_NO,R_ST0,R_ST1);
  397. can be shorten to *)
  398. emit_reg_reg(A_FMUL,S_NO,R_ST0,R_ST0);
  399. end;
  400. in_sqrt_extended:
  401. emit_none(A_FSQRT,S_NO);
  402. in_ln_extended:
  403. begin
  404. emit_none(A_FLDLN2,S_NO);
  405. emit_none(A_FXCH,S_NO);
  406. emit_none(A_FYL2X,S_NO);
  407. end;
  408. end;
  409. end;
  410. {$ifdef SUPPORT_MMX}
  411. in_mmx_pcmpeqb..in_mmx_pcmpgtw:
  412. begin
  413. location_reset(location,LOC_MMXREGISTER,OS_NO);
  414. if left.location.loc=LOC_REGISTER then
  415. begin
  416. {!!!!!!!}
  417. end
  418. else if tcallparanode(left).left.location.loc=LOC_REGISTER then
  419. begin
  420. {!!!!!!!}
  421. end
  422. else
  423. begin
  424. {!!!!!!!}
  425. end;
  426. end;
  427. {$endif SUPPORT_MMX}
  428. else internalerror(9);
  429. end;
  430. { reset pushedparasize }
  431. pushedparasize:=oldpushedparasize;
  432. end;
  433. begin
  434. cinlinenode:=ti386inlinenode;
  435. end.
  436. {
  437. $Log$
  438. Revision 1.45 2002-07-01 16:23:56 peter
  439. * cg64 patch
  440. * basics for currency
  441. * asnode updates for class and interface (not finished)
  442. Revision 1.44 2002/05/18 13:34:25 peter
  443. * readded missing revisions
  444. Revision 1.43 2002/05/16 19:46:51 carl
  445. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  446. + try to fix temp allocation (still in ifdef)
  447. + generic constructor calls
  448. + start of tassembler / tmodulebase class cleanup
  449. Revision 1.41 2002/05/13 19:54:38 peter
  450. * removed n386ld and n386util units
  451. * maybe_save/maybe_restore added instead of the old maybe_push
  452. Revision 1.40 2002/05/12 16:53:17 peter
  453. * moved entry and exitcode to ncgutil and cgobj
  454. * foreach gets extra argument for passing local data to the
  455. iterator function
  456. * -CR checks also class typecasts at runtime by changing them
  457. into as
  458. * fixed compiler to cycle with the -CR option
  459. * fixed stabs with elf writer, finally the global variables can
  460. be watched
  461. * removed a lot of routines from cga unit and replaced them by
  462. calls to cgobj
  463. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  464. u32bit then the other is typecasted also to u32bit without giving
  465. a rangecheck warning/error.
  466. * fixed pascal calling method with reversing also the high tree in
  467. the parast, detected by tcalcst3 test
  468. Revision 1.39 2002/04/23 19:16:35 peter
  469. * add pinline unit that inserts compiler supported functions using
  470. one or more statements
  471. * moved finalize and setlength from ninl to pinline
  472. Revision 1.38 2002/04/21 15:35:54 carl
  473. * changeregsize -> rg.makeregsize
  474. Revision 1.37 2002/04/19 15:39:35 peter
  475. * removed some more routines from cga
  476. * moved location_force_reg/mem to ncgutil
  477. * moved arrayconstructnode secondpass to ncgld
  478. Revision 1.36 2002/04/15 19:44:21 peter
  479. * fixed stackcheck that would be called recursively when a stack
  480. error was found
  481. * generic changeregsize(reg,size) for i386 register resizing
  482. * removed some more routines from cga unit
  483. * fixed returnvalue handling
  484. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  485. Revision 1.35 2002/04/04 19:06:11 peter
  486. * removed unused units
  487. * use tlocation.size in cg.a_*loc*() routines
  488. Revision 1.34 2002/04/02 17:11:36 peter
  489. * tlocation,treference update
  490. * LOC_CONSTANT added for better constant handling
  491. * secondadd splitted in multiple routines
  492. * location_force_reg added for loading a location to a register
  493. of a specified size
  494. * secondassignment parses now first the right and then the left node
  495. (this is compatible with Kylix). This saves a lot of push/pop especially
  496. with string operations
  497. * adapted some routines to use the new cg methods
  498. Revision 1.33 2002/03/31 20:26:39 jonas
  499. + a_loadfpu_* and a_loadmm_* methods in tcg
  500. * register allocation is now handled by a class and is mostly processor
  501. independent (+rgobj.pas and i386/rgcpu.pas)
  502. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  503. * some small improvements and fixes to the optimizer
  504. * some register allocation fixes
  505. * some fpuvaroffset fixes in the unary minus node
  506. * push/popusedregisters is now called rg.save/restoreusedregisters and
  507. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  508. also better optimizable)
  509. * fixed and optimized register saving/restoring for new/dispose nodes
  510. * LOC_FPU locations now also require their "register" field to be set to
  511. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  512. - list field removed of the tnode class because it's not used currently
  513. and can cause hard-to-find bugs
  514. Revision 1.32 2002/03/04 19:10:14 peter
  515. * removed compiler warnings
  516. }