cgcpu.pas 11 KB

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