cgcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the code generator for the i386
  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 i386.
  19. }
  20. unit cgcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. cgbase,cgobj,cg64f32,cgx86,
  25. aasmbase,aasmtai,aasmcpu,
  26. cpubase,cpuinfo,
  27. node,symconst
  28. {$ifdef delphi}
  29. ,dmisc
  30. {$endif}
  31. ;
  32. type
  33. tcg386 = class(tcgx86)
  34. procedure init_register_allocators;override;
  35. class function reg_cgsize(const reg: tregister): tcgsize; override;
  36. procedure g_save_all_registers(list : taasmoutput);override;
  37. procedure g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);override;
  38. end;
  39. tcg64f386 = class(tcg64f32)
  40. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);override;
  41. procedure a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);override;
  42. procedure a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);override;
  43. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);override;
  44. private
  45. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  46. end;
  47. implementation
  48. uses
  49. globtype,globals,verbose,systems,cutils,
  50. symdef,symsym,defutil,paramgr,procinfo,
  51. rgcpu,rgx86,tgobj;
  52. procedure Tcg386.init_register_allocators;
  53. begin
  54. inherited init_register_allocators;
  55. if cs_create_pic in aktmoduleswitches then
  56. 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])
  57. else
  58. 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]);
  59. 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,[]);
  60. 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,[]);
  61. rgfpu:=Trgx86fpu.create;
  62. end;
  63. class function tcg386.reg_cgsize(const reg: tregister): tcgsize;
  64. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  65. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO);
  66. begin
  67. case getregtype(reg) of
  68. R_INTREGISTER :
  69. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  70. R_FPUREGISTER :
  71. reg_cgsize:=OS_F80;
  72. R_MMXREGISTER,
  73. R_MMREGISTER :
  74. reg_cgsize:=OS_M64;
  75. R_SPECIALREGISTER :
  76. case reg of
  77. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  78. reg_cgsize:=OS_16
  79. else
  80. reg_cgsize:=OS_32
  81. end
  82. else
  83. internalerror(200303181);
  84. end;
  85. end;
  86. { const
  87. opsize_2_cgsize: array[topsize] of tcgsize = (OS_NO,
  88. OS_8,OS_16,OS_32,OS_NO,OS_NO,OS_NO,
  89. OS_32,OS_64,OS_64,
  90. OS_F32,OS_F64,OS_F80,OS_F32,OS_F64,OS_M64,OS_NO,
  91. OS_NO,OS_NO,OS_NO
  92. );
  93. begin
  94. result := opsize_2_cgsize[reg2opsize(reg)];
  95. end;}
  96. procedure tcg386.g_save_all_registers(list : taasmoutput);
  97. begin
  98. list.concat(Taicpu.Op_none(A_PUSHA,S_L));
  99. tg.GetTemp(list,POINTER_SIZE,tt_noreuse,current_procinfo.save_regs_ref);
  100. a_load_reg_ref(list,OS_ADDR,OS_ADDR,NR_STACK_POINTER_REG,current_procinfo.save_regs_ref);
  101. end;
  102. procedure tcg386.g_restore_all_registers(list : taasmoutput;const funcretparaloc:tparalocation);
  103. var
  104. href : treference;
  105. begin
  106. a_load_ref_reg(list,OS_ADDR,OS_ADDR,current_procinfo.save_regs_ref,NR_STACK_POINTER_REG);
  107. tg.UnGetTemp(list,current_procinfo.save_regs_ref);
  108. if funcretparaloc.loc=LOC_REGISTER then
  109. begin
  110. if funcretparaloc.size in [OS_64,OS_S64] then
  111. begin
  112. reference_reset_base(href,NR_STACK_POINTER_REG,20);
  113. a_load_reg_ref(list,OS_32,OS_32,NR_FUNCTION_RETURN64_HIGH_REG,href);
  114. reference_reset_base(href,NR_STACK_POINTER_REG,28);
  115. a_load_reg_ref(list,OS_32,OS_32,NR_FUNCTION_RETURN64_LOW_REG,href);
  116. end
  117. else
  118. begin
  119. reference_reset_base(href,NR_STACK_POINTER_REG,28);
  120. a_load_reg_ref(list,OS_32,OS_32,NR_FUNCTION_RETURN_REG,href);
  121. end;
  122. end;
  123. list.concat(Taicpu.Op_none(A_POPA,S_L));
  124. { We add a NOP because of the 386DX CPU bugs with POPAD }
  125. list.concat(taicpu.op_none(A_NOP,S_L));
  126. end;
  127. { ************* 64bit operations ************ }
  128. procedure tcg64f386.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  129. begin
  130. case op of
  131. OP_ADD :
  132. begin
  133. op1:=A_ADD;
  134. op2:=A_ADC;
  135. end;
  136. OP_SUB :
  137. begin
  138. op1:=A_SUB;
  139. op2:=A_SBB;
  140. end;
  141. OP_XOR :
  142. begin
  143. op1:=A_XOR;
  144. op2:=A_XOR;
  145. end;
  146. OP_OR :
  147. begin
  148. op1:=A_OR;
  149. op2:=A_OR;
  150. end;
  151. OP_AND :
  152. begin
  153. op1:=A_AND;
  154. op2:=A_AND;
  155. end;
  156. else
  157. internalerror(200203241);
  158. end;
  159. end;
  160. procedure tcg64f386.a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);
  161. var
  162. op1,op2 : TAsmOp;
  163. tempref : treference;
  164. begin
  165. get_64bit_ops(op,op1,op2);
  166. list.concat(taicpu.op_ref_reg(op1,S_L,ref,reg.reglo));
  167. tempref:=ref;
  168. inc(tempref.offset,4);
  169. list.concat(taicpu.op_ref_reg(op2,S_L,tempref,reg.reghi));
  170. end;
  171. procedure tcg64f386.a_op64_reg_reg(list : taasmoutput;op:TOpCG;regsrc,regdst : tregister64);
  172. var
  173. op1,op2 : TAsmOp;
  174. begin
  175. case op of
  176. OP_NEG :
  177. begin
  178. if (regsrc.reglo<>regdst.reglo) then
  179. a_load64_reg_reg(list,regsrc,regdst);
  180. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  181. list.concat(taicpu.op_reg(A_NEG,S_L,regdst.reglo));
  182. list.concat(taicpu.op_const_reg(A_SBB,S_L,aword(-1),regdst.reghi));
  183. exit;
  184. end;
  185. OP_NOT :
  186. begin
  187. if (regsrc.reglo<>regdst.reglo) then
  188. a_load64_reg_reg(list,regsrc,regdst);
  189. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reghi));
  190. list.concat(taicpu.op_reg(A_NOT,S_L,regdst.reglo));
  191. exit;
  192. end;
  193. end;
  194. get_64bit_ops(op,op1,op2);
  195. list.concat(taicpu.op_reg_reg(op1,S_L,regsrc.reglo,regdst.reglo));
  196. list.concat(taicpu.op_reg_reg(op2,S_L,regsrc.reghi,regdst.reghi));
  197. end;
  198. procedure tcg64f386.a_op64_const_reg(list : taasmoutput;op:TOpCG;value : qword;reg : tregister64);
  199. var
  200. op1,op2 : TAsmOp;
  201. begin
  202. case op of
  203. OP_AND,OP_OR,OP_XOR:
  204. begin
  205. cg.a_op_const_reg(list,op,OS_32,lo(value),reg.reglo);
  206. cg.a_op_const_reg(list,op,OS_32,hi(value),reg.reghi);
  207. end;
  208. OP_ADD, OP_SUB:
  209. begin
  210. // can't use a_op_const_ref because this may use dec/inc
  211. get_64bit_ops(op,op1,op2);
  212. list.concat(taicpu.op_const_reg(op1,S_L,lo(value),reg.reglo));
  213. list.concat(taicpu.op_const_reg(op2,S_L,hi(value),reg.reghi));
  214. end;
  215. else
  216. internalerror(200204021);
  217. end;
  218. end;
  219. procedure tcg64f386.a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);
  220. var
  221. op1,op2 : TAsmOp;
  222. tempref : treference;
  223. begin
  224. case op of
  225. OP_AND,OP_OR,OP_XOR:
  226. begin
  227. cg.a_op_const_ref(list,op,OS_32,lo(value),ref);
  228. tempref:=ref;
  229. inc(tempref.offset,4);
  230. cg.a_op_const_ref(list,op,OS_32,hi(value),tempref);
  231. end;
  232. OP_ADD, OP_SUB:
  233. begin
  234. get_64bit_ops(op,op1,op2);
  235. // can't use a_op_const_ref because this may use dec/inc
  236. list.concat(taicpu.op_const_ref(op1,S_L,lo(value),ref));
  237. tempref:=ref;
  238. inc(tempref.offset,4);
  239. list.concat(taicpu.op_const_ref(op2,S_L,hi(value),tempref));
  240. end;
  241. else
  242. internalerror(200204022);
  243. end;
  244. end;
  245. begin
  246. cg := tcg386.create;
  247. cg64 := tcg64f386.create;
  248. end.
  249. {
  250. $Log$
  251. Revision 1.45 2004-02-04 22:01:13 peter
  252. * first try to get cpupara working for x86_64
  253. Revision 1.44 2004/01/14 23:39:05 florian
  254. * another bunch of x86-64 fixes mainly calling convention and
  255. assembler reader related
  256. Revision 1.43 2004/01/12 16:39:40 peter
  257. * sparc updates, mostly float related
  258. Revision 1.42 2003/12/24 00:10:02 florian
  259. - delete parameter in cg64 methods removed
  260. Revision 1.41 2003/12/19 22:08:44 daniel
  261. * Some work to restore the MMX capabilities
  262. Revision 1.40 2003/10/10 17:48:14 peter
  263. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  264. * tregisteralloctor renamed to trgobj
  265. * removed rgobj from a lot of units
  266. * moved location_* and reference_* to cgobj
  267. * first things for mmx register allocation
  268. Revision 1.39 2003/10/01 20:34:49 peter
  269. * procinfo unit contains tprocinfo
  270. * cginfo renamed to cgbase
  271. * moved cgmessage to verbose
  272. * fixed ppc and sparc compiles
  273. Revision 1.38 2003/09/25 13:13:32 florian
  274. * more x86-64 fixes
  275. Revision 1.37 2003/09/03 15:55:01 peter
  276. * NEWRA branch merged
  277. Revision 1.36.2.1 2003/08/29 17:28:59 peter
  278. * next batch of updates
  279. Revision 1.36 2003/06/12 18:31:18 peter
  280. * fix newra cycle for i386
  281. Revision 1.35 2003/06/03 21:11:09 peter
  282. * cg.a_load_* get a from and to size specifier
  283. * makeregsize only accepts newregister
  284. * i386 uses generic tcgnotnode,tcgunaryminus
  285. Revision 1.34 2003/06/01 21:38:06 peter
  286. * getregisterfpu size parameter added
  287. * op_const_reg size parameter added
  288. * sparc updates
  289. Revision 1.33 2003/05/22 21:32:28 peter
  290. * removed some unit dependencies
  291. Revision 1.32 2002/11/25 17:43:26 peter
  292. * splitted defbase in defutil,symutil,defcmp
  293. * merged isconvertable and is_equal into compare_defs(_ext)
  294. * made operator search faster by walking the list only once
  295. Revision 1.31 2002/10/05 12:43:29 carl
  296. * fixes for Delphi 6 compilation
  297. (warning : Some features do not work under Delphi)
  298. Revision 1.30 2002/09/07 15:25:10 peter
  299. * old logs removed and tabs fixed
  300. Revision 1.29 2002/07/20 19:28:47 florian
  301. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  302. cgx86.pas will contain the common code for i386 and x86_64
  303. Revision 1.28 2002/07/20 11:58:00 florian
  304. * types.pas renamed to defbase.pas because D6 contains a types
  305. unit so this would conflicts if D6 programms are compiled
  306. + Willamette/SSE2 instructions to assembler added
  307. Revision 1.27 2002/07/11 14:41:32 florian
  308. * start of the new generic parameter handling
  309. Revision 1.26 2002/07/07 09:52:33 florian
  310. * powerpc target fixed, very simple units can be compiled
  311. * some basic stuff for better callparanode handling, far from being finished
  312. Revision 1.25 2002/07/01 18:46:30 peter
  313. * internal linker
  314. * reorganized aasm layer
  315. Revision 1.24 2002/07/01 16:23:55 peter
  316. * cg64 patch
  317. * basics for currency
  318. * asnode updates for class and interface (not finished)
  319. Revision 1.23 2002/06/16 08:16:59 carl
  320. * bugfix of missing popecx for shift operations
  321. Revision 1.22 2002/05/22 19:02:16 carl
  322. + generic FPC_HELP_FAIL
  323. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  324. + generic FPC_DISPOSE_CLASS
  325. + TEST_GENERIC define
  326. Revision 1.21 2002/05/20 13:30:40 carl
  327. * bugfix of hdisponen (base must be set, not index)
  328. * more portability fixes
  329. Revision 1.20 2002/05/18 13:34:22 peter
  330. * readded missing revisions
  331. Revision 1.19 2002/05/16 19:46:50 carl
  332. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  333. + try to fix temp allocation (still in ifdef)
  334. + generic constructor calls
  335. + start of tassembler / tmodulebase class cleanup
  336. Revision 1.17 2002/05/13 19:54:37 peter
  337. * removed n386ld and n386util units
  338. * maybe_save/maybe_restore added instead of the old maybe_push
  339. Revision 1.16 2002/05/12 19:59:05 carl
  340. * some small portability fixes
  341. Revision 1.15 2002/05/12 16:53:16 peter
  342. * moved entry and exitcode to ncgutil and cgobj
  343. * foreach gets extra argument for passing local data to the
  344. iterator function
  345. * -CR checks also class typecasts at runtime by changing them
  346. into as
  347. * fixed compiler to cycle with the -CR option
  348. * fixed stabs with elf writer, finally the global variables can
  349. be watched
  350. * removed a lot of routines from cga unit and replaced them by
  351. calls to cgobj
  352. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  353. u32bit then the other is typecasted also to u32bit without giving
  354. a rangecheck warning/error.
  355. * fixed pascal calling method with reversing also the high tree in
  356. the parast, detected by tcalcst3 test
  357. Revision 1.14 2002/04/25 20:16:40 peter
  358. * moved more routines from cga/n386util
  359. Revision 1.13 2002/04/21 15:31:05 carl
  360. * changeregsize -> rg.makeregsize
  361. + a_jmp_always added
  362. Revision 1.12 2002/04/15 19:44:20 peter
  363. * fixed stackcheck that would be called recursively when a stack
  364. error was found
  365. * generic changeregsize(reg,size) for i386 register resizing
  366. * removed some more routines from cga unit
  367. * fixed returnvalue handling
  368. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  369. Revision 1.11 2002/04/04 19:06:10 peter
  370. * removed unused units
  371. * use tlocation.size in a_*loc*() routines
  372. Revision 1.10 2002/04/02 20:29:02 jonas
  373. * optimized the code generated by the a_op_const_* and a_op64_const
  374. methods
  375. Revision 1.9 2002/04/02 17:11:33 peter
  376. * tlocation,treference update
  377. * LOC_CONSTANT added for better constant handling
  378. * secondadd splitted in multiple routines
  379. * location_force_reg added for loading a location to a register
  380. of a specified size
  381. * secondassignment parses now first the right and then the left node
  382. (this is compatible with Kylix). This saves a lot of push/pop especially
  383. with string operations
  384. * adapted some routines to use the new cg methods
  385. }