cgcpu.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. This unit implements the code generator for the x86-64.
  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. { This unit implements the code generator for the x86-64.
  19. }
  20. unit cgcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cgbase,cgobj,cg64f64,cgx86,
  25. aasmbase,aasmtai,aasmcpu,
  26. cpubase,cpuinfo,cpupara,
  27. node,symconst,rgx86;
  28. type
  29. tcgx86_64 = class(tcgx86)
  30. procedure init_register_allocators;override;
  31. class function reg_cgsize(const reg: tregister): tcgsize; override;
  32. procedure g_save_all_registers(list : taasmoutput);override;
  33. procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);override;
  34. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  35. end;
  36. implementation
  37. uses
  38. globtype,globals,verbose,systems,cutils,
  39. symdef,symsym,defutil,paramgr,
  40. rgobj,tgobj,rgcpu;
  41. procedure Tcgx86_64.init_register_allocators;
  42. begin
  43. inherited init_register_allocators;
  44. if cs_create_pic in aktmoduleswitches then
  45. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP,RS_EBX])
  46. else
  47. rg[R_INTREGISTER]:=trgcpu.create(R_INTREGISTER,R_SUBWHOLE,[RS_EAX,RS_EDX,RS_ECX,RS_EBX,RS_ESI,RS_EDI],first_int_imreg,[RS_EBP]);
  48. rg[R_MMXREGISTER]:=trgcpu.create(R_MMXREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_sse_imreg,[]);
  49. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,[RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7],first_sse_imreg,[]);
  50. rgfpu:=Trgx86fpu.create;
  51. end;
  52. class function tcgx86_64.reg_cgsize(const reg: tregister): tcgsize;
  53. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  54. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO);
  55. begin
  56. case getregtype(reg) of
  57. R_INTREGISTER :
  58. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  59. R_FPUREGISTER :
  60. reg_cgsize:=OS_F80;
  61. R_MMXREGISTER:
  62. reg_cgsize:=OS_M64;
  63. R_MMREGISTER:
  64. reg_cgsize:=OS_M128;
  65. R_SPECIALREGISTER :
  66. case reg of
  67. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  68. reg_cgsize:=OS_16
  69. else
  70. reg_cgsize:=OS_32
  71. end
  72. else
  73. internalerror(200303181);
  74. end;
  75. end;
  76. procedure tcgx86_64.g_save_all_registers(list : taasmoutput);
  77. begin
  78. {$warning todo tcgx86_64.g_save_all_registers}
  79. end;
  80. procedure tcgx86_64.g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);
  81. begin
  82. {$warning todo tcgx86_64.g_restore_all_registers}
  83. end;
  84. procedure tcgx86_64.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  85. var
  86. ecxpushed : boolean;
  87. helpsize : longint;
  88. i : byte;
  89. reg8,reg32 : tregister;
  90. srcref,dstref : treference;
  91. swap : boolean;
  92. {!!!
  93. procedure maybepushecx;
  94. begin
  95. if not(R_ECX in rg.unusedregsint) then
  96. begin
  97. list.concat(Taicpu.Op_reg(A_PUSH,S_L,R_ECX));
  98. ecxpushed:=true;
  99. end
  100. else rg.getexplicitregisterint(list,R_ECX);
  101. end;
  102. }
  103. begin
  104. {!!!
  105. if (not loadref) and
  106. ((len<=8) or
  107. (not(cs_littlesize in aktglobalswitches ) and (len<=12))) then
  108. begin
  109. helpsize:=len shr 3;
  110. rg.getexplicitregisterint(list,R_RDI);
  111. dstref:=dest;
  112. srcref:=source;
  113. for i:=1 to helpsize do
  114. begin
  115. a_load_ref_reg(list,OS_64,srcref,R_RDI);
  116. If (len=8) and delsource then
  117. reference_release(list,source);
  118. a_load_reg_ref(list,OS_64,R_RDI,dstref);
  119. inc(srcref.offset,8);
  120. inc(dstref.offset,8);
  121. dec(len,8);
  122. end;
  123. if len>1 then
  124. begin
  125. a_load_ref_reg(list,OS_16,srcref,R_EDI);
  126. If (len =4) and delsource then
  127. reference_release(list,source);
  128. a_load_reg_ref(list,OS_16,R_EDI,dstref);
  129. inc(srcref.offset,4);
  130. inc(dstref.offset,4);
  131. dec(len,4);
  132. end;
  133. if len>1 then
  134. begin
  135. a_load_ref_reg(list,OS_16,srcref,R_DI);
  136. If (len = 2) and delsource then
  137. reference_release(list,source);
  138. a_load_reg_ref(list,OS_16,R_DI,dstref);
  139. inc(srcref.offset,2);
  140. inc(dstref.offset,2);
  141. dec(len,2);
  142. end;
  143. if len>0 then
  144. begin
  145. a_load_ref_reg(list,OS_16,srcref,R_DIL);
  146. a_load_reg_ref(list,OS_16,R_DIL,dstref);
  147. end;
  148. rg.ungetregisterint(list,R_RDI);
  149. end
  150. else
  151. begin
  152. rg.getexplicitregisterint(list,R_RDI);
  153. a_loadaddr_ref_reg(list,dest,R_RDI);
  154. list.concat(tai_regalloc.Alloc(R_RSI));
  155. if loadref then
  156. a_load_ref_reg(list,OS_ADDR,source,R_RSI)
  157. else
  158. begin
  159. a_loadaddr_ref_reg(list,source,R_RSI);
  160. if delsource then
  161. reference_release(list,source);
  162. end;
  163. list.concat(Taicpu.Op_none(A_CLD,S_NO));
  164. ecxpushed:=false;
  165. if cs_littlesize in aktglobalswitches then
  166. begin
  167. maybepushecx;
  168. a_load_const_reg(list,OS_INT,len,R_RCX);
  169. list.concat(Taicpu.Op_none(A_REP,S_NO));
  170. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  171. end
  172. else
  173. begin
  174. helpsize:=len shr 2;
  175. len:=len and 3;
  176. if helpsize>1 then
  177. begin
  178. maybepushecx;
  179. a_load_const_reg(list,OS_INT,helpsize,R_RCX);
  180. list.concat(Taicpu.Op_none(A_REP,S_NO));
  181. end;
  182. if helpsize>0 then
  183. list.concat(Taicpu.Op_none(A_MOVSD,S_NO));
  184. if len>1 then
  185. begin
  186. dec(len,2);
  187. list.concat(Taicpu.Op_none(A_MOVSW,S_NO));
  188. end;
  189. if len=1 then
  190. list.concat(Taicpu.Op_none(A_MOVSB,S_NO));
  191. end;
  192. rg.ungetregisterint(list,R_RDI);
  193. list.concat(tai_regalloc.DeAlloc(R_RSI));
  194. if ecxpushed then
  195. list.concat(Taicpu.Op_reg(A_POP,S_L,R_RCX))
  196. else
  197. rg.ungetregisterint(list,R_RCX);
  198. { loading SELF-reference again }
  199. g_maybe_loadself(list);
  200. end;
  201. if delsource then
  202. tg.ungetiftemp(list,source);
  203. }
  204. end;
  205. begin
  206. cg:=tcgx86_64.create;
  207. cg64:=tcg64f64.create;
  208. end.
  209. {
  210. $Log$
  211. Revision 1.10 2004-02-04 22:01:13 peter
  212. * first try to get cpupara working for x86_64
  213. Revision 1.9 2004/01/14 23:39:05 florian
  214. * another bunch of x86-64 fixes mainly calling convention and
  215. assembler reader related
  216. Revision 1.8 2004/01/13 18:08:58 florian
  217. * x86-64 compilation fixed
  218. Revision 1.7 2003/12/24 01:47:23 florian
  219. * first fixes to compile the x86-64 system unit
  220. Revision 1.6 2003/12/22 19:00:17 florian
  221. * fixed some x86-64 issues
  222. Revision 1.5 2003/09/25 13:13:32 florian
  223. * more x86-64 fixes
  224. Revision 1.4 2003/04/30 15:45:35 florian
  225. * merged more x86-64/i386 code
  226. Revision 1.3 2003/01/05 13:36:54 florian
  227. * x86-64 compiles
  228. + very basic support for float128 type (x86-64 only)
  229. Revision 1.2 2002/07/25 22:55:33 florian
  230. * several fixes, small test units can be compiled
  231. Revision 1.1 2002/07/24 22:38:15 florian
  232. + initial release of x86-64 target code
  233. }