cgcpu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.number<>regdst.reglo.number) then
  100. a_load64_reg_reg(list,regsrc,regdst{$ifdef newra},false{$endif});
  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.number<>regdst.reglo.number) then
  109. a_load64_reg_reg(list,regsrc,regdst{$ifdef newra},false{$endif});
  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.36 2003-06-12 18:31:18 peter
  173. * fix newra cycle for i386
  174. Revision 1.35 2003/06/03 21:11:09 peter
  175. * cg.a_load_* get a from and to size specifier
  176. * makeregsize only accepts newregister
  177. * i386 uses generic tcgnotnode,tcgunaryminus
  178. Revision 1.34 2003/06/01 21:38:06 peter
  179. * getregisterfpu size parameter added
  180. * op_const_reg size parameter added
  181. * sparc updates
  182. Revision 1.33 2003/05/22 21:32:28 peter
  183. * removed some unit dependencies
  184. Revision 1.32 2002/11/25 17:43:26 peter
  185. * splitted defbase in defutil,symutil,defcmp
  186. * merged isconvertable and is_equal into compare_defs(_ext)
  187. * made operator search faster by walking the list only once
  188. Revision 1.31 2002/10/05 12:43:29 carl
  189. * fixes for Delphi 6 compilation
  190. (warning : Some features do not work under Delphi)
  191. Revision 1.30 2002/09/07 15:25:10 peter
  192. * old logs removed and tabs fixed
  193. Revision 1.29 2002/07/20 19:28:47 florian
  194. * splitting of i386\cgcpu.pas into x86\cgx86.pas and i386\cgcpu.pas
  195. cgx86.pas will contain the common code for i386 and x86_64
  196. Revision 1.28 2002/07/20 11:58:00 florian
  197. * types.pas renamed to defbase.pas because D6 contains a types
  198. unit so this would conflicts if D6 programms are compiled
  199. + Willamette/SSE2 instructions to assembler added
  200. Revision 1.27 2002/07/11 14:41:32 florian
  201. * start of the new generic parameter handling
  202. Revision 1.26 2002/07/07 09:52:33 florian
  203. * powerpc target fixed, very simple units can be compiled
  204. * some basic stuff for better callparanode handling, far from being finished
  205. Revision 1.25 2002/07/01 18:46:30 peter
  206. * internal linker
  207. * reorganized aasm layer
  208. Revision 1.24 2002/07/01 16:23:55 peter
  209. * cg64 patch
  210. * basics for currency
  211. * asnode updates for class and interface (not finished)
  212. Revision 1.23 2002/06/16 08:16:59 carl
  213. * bugfix of missing popecx for shift operations
  214. Revision 1.22 2002/05/22 19:02:16 carl
  215. + generic FPC_HELP_FAIL
  216. + generic FPC_HELP_DESTRUCTOR instated (original from Pierre)
  217. + generic FPC_DISPOSE_CLASS
  218. + TEST_GENERIC define
  219. Revision 1.21 2002/05/20 13:30:40 carl
  220. * bugfix of hdisponen (base must be set, not index)
  221. * more portability fixes
  222. Revision 1.20 2002/05/18 13:34:22 peter
  223. * readded missing revisions
  224. Revision 1.19 2002/05/16 19:46:50 carl
  225. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  226. + try to fix temp allocation (still in ifdef)
  227. + generic constructor calls
  228. + start of tassembler / tmodulebase class cleanup
  229. Revision 1.17 2002/05/13 19:54:37 peter
  230. * removed n386ld and n386util units
  231. * maybe_save/maybe_restore added instead of the old maybe_push
  232. Revision 1.16 2002/05/12 19:59:05 carl
  233. * some small portability fixes
  234. Revision 1.15 2002/05/12 16:53:16 peter
  235. * moved entry and exitcode to ncgutil and cgobj
  236. * foreach gets extra argument for passing local data to the
  237. iterator function
  238. * -CR checks also class typecasts at runtime by changing them
  239. into as
  240. * fixed compiler to cycle with the -CR option
  241. * fixed stabs with elf writer, finally the global variables can
  242. be watched
  243. * removed a lot of routines from cga unit and replaced them by
  244. calls to cgobj
  245. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  246. u32bit then the other is typecasted also to u32bit without giving
  247. a rangecheck warning/error.
  248. * fixed pascal calling method with reversing also the high tree in
  249. the parast, detected by tcalcst3 test
  250. Revision 1.14 2002/04/25 20:16:40 peter
  251. * moved more routines from cga/n386util
  252. Revision 1.13 2002/04/21 15:31:05 carl
  253. * changeregsize -> rg.makeregsize
  254. + a_jmp_always added
  255. Revision 1.12 2002/04/15 19:44:20 peter
  256. * fixed stackcheck that would be called recursively when a stack
  257. error was found
  258. * generic changeregsize(reg,size) for i386 register resizing
  259. * removed some more routines from cga unit
  260. * fixed returnvalue handling
  261. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  262. Revision 1.11 2002/04/04 19:06:10 peter
  263. * removed unused units
  264. * use tlocation.size in a_*loc*() routines
  265. Revision 1.10 2002/04/02 20:29:02 jonas
  266. * optimized the code generated by the a_op_const_* and a_op64_const
  267. methods
  268. Revision 1.9 2002/04/02 17:11:33 peter
  269. * tlocation,treference update
  270. * LOC_CONSTANT added for better constant handling
  271. * secondadd splitted in multiple routines
  272. * location_force_reg added for loading a location to a register
  273. of a specified size
  274. * secondassignment parses now first the right and then the left node
  275. (this is compatible with Kylix). This saves a lot of push/pop especially
  276. with string operations
  277. * adapted some routines to use the new cg methods
  278. }