cgcpu.pas 8.4 KB

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