cgcpu.pas 9.0 KB

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