cgcpu.pas 9.4 KB

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