cgcpu.pas 12 KB

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