cgcpu.pas 10.0 KB

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