n386cnv.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 assembler for type converting 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 n386cnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncgcnv,defbase;
  23. type
  24. ti386typeconvnode = class(tcgtypeconvnode)
  25. protected
  26. { procedure second_int_to_int;override; }
  27. { procedure second_string_to_string;override; }
  28. { procedure second_cstring_to_pchar;override; }
  29. { procedure second_string_to_chararray;override; }
  30. { procedure second_array_to_pointer;override; }
  31. { procedure second_pointer_to_array;override; }
  32. { procedure second_chararray_to_string;override; }
  33. { procedure second_char_to_string;override; }
  34. function first_int_to_real: tnode; override;
  35. procedure second_int_to_real;override;
  36. { procedure second_real_to_real;override; }
  37. { procedure second_cord_to_pointer;override; }
  38. { procedure second_proc_to_procvar;override; }
  39. { procedure second_bool_to_int;override; }
  40. procedure second_int_to_bool;override;
  41. { procedure second_load_smallset;override; }
  42. { procedure second_ansistring_to_pchar;override; }
  43. { procedure second_pchar_to_string;override; }
  44. { procedure second_class_to_intf;override; }
  45. { procedure second_char_to_char;override; }
  46. {$ifdef TESTOBJEXT2}
  47. procedure checkobject;override;
  48. {$endif TESTOBJEXT2}
  49. procedure second_call_helper(c : tconverttype);override;
  50. end;
  51. implementation
  52. uses
  53. verbose,systems,
  54. symconst,symdef,aasmbase,aasmtai,aasmcpu,
  55. cginfo,cgbase,pass_2,
  56. ncon,ncal,ncnv,
  57. cpubase,
  58. cgobj,cga,tgobj,rgobj,rgcpu,ncgutil;
  59. {*****************************************************************************
  60. SecondTypeConv
  61. *****************************************************************************}
  62. function ti386typeconvnode.first_int_to_real : tnode;
  63. begin
  64. first_int_to_real:=nil;
  65. if registersfpu<1 then
  66. registersfpu:=1;
  67. location.loc:=LOC_FPUREGISTER;
  68. end;
  69. procedure ti386typeconvnode.second_int_to_real;
  70. var
  71. href : treference;
  72. hregister : tregister;
  73. l1,l2 : tasmlabel;
  74. freereg : boolean;
  75. begin
  76. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  77. hregister:=R_NO;
  78. freereg:=false;
  79. { for u32bit a solution is to push $0 and to load a comp }
  80. { does this first, it destroys maybe EDI }
  81. if torddef(left.resulttype.def).typ=u32bit then
  82. exprasmlist.concat(taicpu.op_const(A_PUSH,S_L,0));
  83. case left.location.loc of
  84. LOC_REGISTER,
  85. LOC_CREGISTER :
  86. begin
  87. case left.location.size of
  88. OS_64,OS_S64 :
  89. begin
  90. exprasmlist.concat(taicpu.op_reg(A_PUSH,S_L,left.location.registerhigh));
  91. hregister:=left.location.registerlow;
  92. end;
  93. OS_32,OS_S32 :
  94. hregister:=left.location.register;
  95. else
  96. begin
  97. hregister:=cg.get_scratch_reg_int(exprasmlist);
  98. freereg:=true;
  99. cg.a_load_reg_reg(exprasmlist,left.location.size,left.location.register,hregister);
  100. end;
  101. end;
  102. end;
  103. LOC_REFERENCE,
  104. LOC_CREFERENCE :
  105. begin
  106. hregister:=cg.get_scratch_reg_int(exprasmlist);
  107. freereg:=true;
  108. if left.location.size in [OS_64,OS_S64] then
  109. begin
  110. href:=left.location.reference;
  111. inc(href.offset,4);
  112. cg.a_load_ref_reg(exprasmlist,OS_32,href,hregister);
  113. exprasmlist.concat(taicpu.op_reg(A_PUSH,S_L,hregister));
  114. cg.a_load_ref_reg(exprasmlist,OS_32,left.location.reference,hregister);
  115. end
  116. else
  117. cg.a_load_ref_reg(exprasmlist,left.location.size,left.location.reference,hregister);
  118. end;
  119. else
  120. internalerror(2002032218);
  121. end;
  122. location_release(exprasmlist,left.location);
  123. location_freetemp(exprasmlist,left.location);
  124. { for 64 bit integers, the high dword is already pushed }
  125. exprasmlist.concat(taicpu.op_reg(A_PUSH,S_L,hregister));
  126. if freereg then
  127. cg.free_scratch_reg(exprasmlist,hregister);
  128. reference_reset_base(href,R_ESP,0);
  129. case torddef(left.resulttype.def).typ of
  130. u32bit:
  131. begin
  132. emit_ref(A_FILD,S_IQ,href);
  133. emit_const_reg(A_ADD,S_L,8,R_ESP);
  134. end;
  135. s64bit:
  136. begin
  137. emit_ref(A_FILD,S_IQ,href);
  138. emit_const_reg(A_ADD,S_L,8,R_ESP);
  139. end;
  140. u64bit:
  141. begin
  142. { unsigned 64 bit ints are harder to handle: }
  143. { we load bits 0..62 and then check bit 63: }
  144. { if it is 1 then we add $80000000 000000000 }
  145. { as double }
  146. inc(href.offset,4);
  147. rg.getexplicitregisterint(exprasmlist,R_EDI);
  148. emit_ref_reg(A_MOV,S_L,href,R_EDI);
  149. reference_reset_base(href,R_ESP,4);
  150. emit_const_ref(A_AND,S_L,$7fffffff,href);
  151. emit_const_reg(A_TEST,S_L,longint($80000000),R_EDI);
  152. rg.ungetregisterint(exprasmlist,R_EDI);
  153. reference_reset_base(href,R_ESP,0);
  154. emit_ref(A_FILD,S_IQ,href);
  155. objectlibrary.getdatalabel(l1);
  156. objectlibrary.getlabel(l2);
  157. emitjmp(C_Z,l2);
  158. Consts.concat(Tai_label.Create(l1));
  159. { I got this constant from a test progtram (FK) }
  160. Consts.concat(Tai_const.Create_32bit(0));
  161. Consts.concat(Tai_const.Create_32bit(1138753536));
  162. reference_reset_symbol(href,l1,0);
  163. emit_ref(A_FADD,S_FL,href);
  164. cg.a_label(exprasmlist,l2);
  165. emit_const_reg(A_ADD,S_L,8,R_ESP);
  166. end
  167. else
  168. begin
  169. emit_ref(A_FILD,S_IL,href);
  170. rg.getexplicitregisterint(exprasmlist,R_EDI);
  171. emit_reg(A_POP,S_L,R_EDI);
  172. rg.ungetregisterint(exprasmlist,R_EDI);
  173. end;
  174. end;
  175. inc(trgcpu(rg).fpuvaroffset);
  176. location.register:=R_ST;
  177. end;
  178. procedure ti386typeconvnode.second_int_to_bool;
  179. var
  180. hregister : tregister;
  181. pref : treference;
  182. resflags : tresflags;
  183. hlabel,oldtruelabel,oldfalselabel : tasmlabel;
  184. begin
  185. oldtruelabel:=truelabel;
  186. oldfalselabel:=falselabel;
  187. objectlibrary.getlabel(truelabel);
  188. objectlibrary.getlabel(falselabel);
  189. secondpass(left);
  190. if codegenerror then
  191. exit;
  192. { byte(boolean) or word(wordbool) or longint(longbool) must }
  193. { be accepted for var parameters }
  194. if (nf_explizit in flags) and
  195. (left.resulttype.def.size=resulttype.def.size) and
  196. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  197. begin
  198. location_copy(location,left.location);
  199. truelabel:=oldtruelabel;
  200. falselabel:=oldfalselabel;
  201. exit;
  202. end;
  203. { Load left node into flag F_NE/F_E }
  204. resflags:=F_NE;
  205. location_release(exprasmlist,left.location);
  206. case left.location.loc of
  207. LOC_CREFERENCE,
  208. LOC_REFERENCE :
  209. begin
  210. if left.location.size in [OS_64,OS_S64] then
  211. begin
  212. hregister:=rg.getregisterint(exprasmlist);
  213. emit_ref_reg(A_MOV,S_L,left.location.reference,hregister);
  214. pref:=left.location.reference;
  215. inc(pref.offset,4);
  216. emit_ref_reg(A_OR,S_L,pref,hregister);
  217. end
  218. else
  219. begin
  220. location_force_reg(exprasmlist,left.location,left.location.size,true);
  221. cg.a_op_reg_reg(exprasmlist,OP_OR,left.location.size,left.location.register,left.location.register);
  222. end;
  223. end;
  224. LOC_FLAGS :
  225. begin
  226. resflags:=left.location.resflags;
  227. end;
  228. LOC_REGISTER,LOC_CREGISTER :
  229. begin
  230. if left.location.size in [OS_64,OS_S64] then
  231. begin
  232. hregister:=cg.get_scratch_reg_int(exprasmlist);
  233. cg.a_load_reg_reg(exprasmlist,OS_32,left.location.registerlow,hregister);
  234. cg.a_op_reg_reg(exprasmlist,OP_OR,OS_32,left.location.registerhigh,hregister);
  235. cg.free_scratch_reg(exprasmlist,hregister);
  236. end
  237. else
  238. cg.a_op_reg_reg(exprasmlist,OP_OR,left.location.size,left.location.register,left.location.register);
  239. end;
  240. LOC_JUMP :
  241. begin
  242. hregister:=rg.getregisterint(exprasmlist);
  243. objectlibrary.getlabel(hlabel);
  244. cg.a_label(exprasmlist,truelabel);
  245. cg.a_load_const_reg(exprasmlist,OS_INT,1,hregister);
  246. cg.a_jmp_always(exprasmlist,hlabel);
  247. cg.a_label(exprasmlist,falselabel);
  248. cg.a_load_const_reg(exprasmlist,OS_INT,0,hregister);
  249. cg.a_label(exprasmlist,hlabel);
  250. cg.a_op_reg_reg(exprasmlist,OP_OR,OS_INT,hregister,hregister);
  251. end;
  252. else
  253. internalerror(10062);
  254. end;
  255. { load flags to register }
  256. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  257. location.register:=def_getreg(resulttype.def);
  258. cg.g_flags2reg(exprasmlist,location.size,resflags,location.register);
  259. truelabel:=oldtruelabel;
  260. falselabel:=oldfalselabel;
  261. end;
  262. {$ifdef TESTOBJEXT2}
  263. procedure ti386typeconvnode.checkobject;
  264. var
  265. r : preference;
  266. nillabel : plabel;
  267. begin
  268. new(r);
  269. reset_reference(r^);
  270. if p^.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  271. r^.base:=p^.location.register
  272. else
  273. begin
  274. rg.getexplicitregisterint(exprasmlist,R_EDI);
  275. emit_mov_loc_reg(p^.location,R_EDI);
  276. r^.base:=R_EDI;
  277. end;
  278. { NIL must be accepted !! }
  279. emit_reg_reg(A_OR,S_L,r^.base,r^.base);
  280. rg.ungetregisterint(exprasmlist,R_EDI);
  281. objectlibrary.getlabel(nillabel);
  282. emitjmp(C_E,nillabel);
  283. { this is one point where we need vmt_offset (PM) }
  284. r^.offset:= tobjectdef(tpointerdef(p^.resulttype.def).definition).vmt_offset;
  285. rg.getexplicitregisterint(exprasmlist,R_EDI);
  286. emit_ref_reg(A_MOV,S_L,r,R_EDI);
  287. emit_sym(A_PUSH,S_L,
  288. objectlibrary.newasmsymbol(tobjectdef(tpointerdef(p^.resulttype.def).definition).vmt_mangledname));
  289. emit_reg(A_PUSH,S_L,R_EDI);
  290. rg.ungetregister32(exprasmlist,R_EDI);
  291. emitcall('FPC_CHECK_OBJECT_EXT');
  292. emitlab(nillabel);
  293. end;
  294. {$endif TESTOBJEXT2}
  295. procedure ti386typeconvnode.second_call_helper(c : tconverttype);
  296. const
  297. secondconvert : array[tconverttype] of pointer = (
  298. @second_nothing, {equal}
  299. @second_nothing, {not_possible}
  300. @second_nothing, {second_string_to_string, handled in resulttype pass }
  301. @second_char_to_string,
  302. @second_nothing, {char_to_charray}
  303. @second_nothing, { pchar_to_string, handled in resulttype pass }
  304. @second_nothing, {cchar_to_pchar}
  305. @second_cstring_to_pchar,
  306. @second_ansistring_to_pchar,
  307. @second_string_to_chararray,
  308. @second_nothing, { chararray_to_string, handled in resulttype pass }
  309. @second_array_to_pointer,
  310. @second_pointer_to_array,
  311. @second_int_to_int,
  312. @second_int_to_bool,
  313. @second_bool_to_bool,
  314. @second_bool_to_int,
  315. @second_real_to_real,
  316. @second_int_to_real,
  317. @second_proc_to_procvar,
  318. @second_nothing, { arrayconstructor_to_set }
  319. @second_nothing, { second_load_smallset, handled in first pass }
  320. @second_cord_to_pointer,
  321. @second_nothing, { interface 2 string }
  322. @second_nothing, { interface 2 guid }
  323. @second_class_to_intf,
  324. @second_char_to_char,
  325. @second_nothing, { normal_2_smallset }
  326. @second_nothing { dynarray_2_openarray }
  327. );
  328. type
  329. tprocedureofobject = procedure of object;
  330. var
  331. r : packed record
  332. proc : pointer;
  333. obj : pointer;
  334. end;
  335. begin
  336. { this is a little bit dirty but it works }
  337. { and should be quite portable too }
  338. r.proc:=secondconvert[c];
  339. r.obj:=self;
  340. tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  341. end;
  342. begin
  343. ctypeconvnode:=ti386typeconvnode;
  344. end.
  345. {
  346. $Log$
  347. Revision 1.48 2002-08-14 19:19:14 carl
  348. * first_int_to_real moved to i386 (other one is generic)
  349. Revision 1.47 2002/08/11 14:32:30 peter
  350. * renamed current_library to objectlibrary
  351. Revision 1.46 2002/08/11 13:24:16 peter
  352. * saving of asmsymbols in ppu supported
  353. * asmsymbollist global is removed and moved into a new class
  354. tasmlibrarydata that will hold the info of a .a file which
  355. corresponds with a single module. Added librarydata to tmodule
  356. to keep the library info stored for the module. In the future the
  357. objectfiles will also be stored to the tasmlibrarydata class
  358. * all getlabel/newasmsymbol and friends are moved to the new class
  359. Revision 1.45 2002/07/27 19:53:51 jonas
  360. + generic implementation of tcg.g_flags2ref()
  361. * tcg.flags2xxx() now also needs a size parameter
  362. Revision 1.44 2002/07/20 11:58:01 florian
  363. * types.pas renamed to defbase.pas because D6 contains a types
  364. unit so this would conflicts if D6 programms are compiled
  365. + Willamette/SSE2 instructions to assembler added
  366. Revision 1.43 2002/07/01 18:46:31 peter
  367. * internal linker
  368. * reorganized aasm layer
  369. Revision 1.42 2002/05/20 13:30:40 carl
  370. * bugfix of hdisponen (base must be set, not index)
  371. * more portability fixes
  372. Revision 1.41 2002/05/18 13:34:24 peter
  373. * readded missing revisions
  374. Revision 1.40 2002/05/16 19:46:51 carl
  375. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  376. + try to fix temp allocation (still in ifdef)
  377. + generic constructor calls
  378. + start of tassembler / tmodulebase class cleanup
  379. Revision 1.38 2002/05/12 16:53:17 peter
  380. * moved entry and exitcode to ncgutil and cgobj
  381. * foreach gets extra argument for passing local data to the
  382. iterator function
  383. * -CR checks also class typecasts at runtime by changing them
  384. into as
  385. * fixed compiler to cycle with the -CR option
  386. * fixed stabs with elf writer, finally the global variables can
  387. be watched
  388. * removed a lot of routines from cga unit and replaced them by
  389. calls to cgobj
  390. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  391. u32bit then the other is typecasted also to u32bit without giving
  392. a rangecheck warning/error.
  393. * fixed pascal calling method with reversing also the high tree in
  394. the parast, detected by tcalcst3 test
  395. Revision 1.37 2002/04/21 19:02:07 peter
  396. * removed newn and disposen nodes, the code is now directly
  397. inlined from pexpr
  398. * -an option that will write the secondpass nodes to the .s file, this
  399. requires EXTDEBUG define to actually write the info
  400. * fixed various internal errors and crashes due recent code changes
  401. Revision 1.36 2002/04/21 15:35:23 carl
  402. * changeregsize -> rg.makeregsize
  403. Revision 1.35 2002/04/19 15:39:35 peter
  404. * removed some more routines from cga
  405. * moved location_force_reg/mem to ncgutil
  406. * moved arrayconstructnode secondpass to ncgld
  407. Revision 1.34 2002/04/15 19:44:21 peter
  408. * fixed stackcheck that would be called recursively when a stack
  409. error was found
  410. * generic changeregsize(reg,size) for i386 register resizing
  411. * removed some more routines from cga unit
  412. * fixed returnvalue handling
  413. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  414. Revision 1.33 2002/04/04 19:06:10 peter
  415. * removed unused units
  416. * use tlocation.size in cg.a_*loc*() routines
  417. Revision 1.32 2002/04/02 17:11:36 peter
  418. * tlocation,treference update
  419. * LOC_CONSTANT added for better constant handling
  420. * secondadd splitted in multiple routines
  421. * location_force_reg added for loading a location to a register
  422. of a specified size
  423. * secondassignment parses now first the right and then the left node
  424. (this is compatible with Kylix). This saves a lot of push/pop especially
  425. with string operations
  426. * adapted some routines to use the new cg methods
  427. Revision 1.31 2002/03/31 20:26:38 jonas
  428. + a_loadfpu_* and a_loadmm_* methods in tcg
  429. * register allocation is now handled by a class and is mostly processor
  430. independent (+rgobj.pas and i386/rgcpu.pas)
  431. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  432. * some small improvements and fixes to the optimizer
  433. * some register allocation fixes
  434. * some fpuvaroffset fixes in the unary minus node
  435. * push/popusedregisters is now called rg.save/restoreusedregisters and
  436. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  437. also better optimizable)
  438. * fixed and optimized register saving/restoring for new/dispose nodes
  439. * LOC_FPU locations now also require their "register" field to be set to
  440. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  441. - list field removed of the tnode class because it's not used currently
  442. and can cause hard-to-find bugs
  443. Revision 1.30 2002/03/04 19:10:13 peter
  444. * removed compiler warnings
  445. }