cgcpu.pas 9.6 KB

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