cga.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Helper routines for the i386 code generator
  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. unit cga;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpuinfo,cpubase,cgbase,
  23. symconst,symtype,symdef,aasmbase,aasmtai,aasmcpu;
  24. {$define TESTGETTEMP to store const that
  25. are written into temps for later release PM }
  26. function def_opsize(p1:tdef):topsize;
  27. procedure emit_none(i : tasmop;s : topsize);
  28. procedure emit_const(i : tasmop;s : topsize;c : longint);
  29. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  30. procedure emit_ref(i : tasmop;s : topsize;const ref : treference);
  31. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  32. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;const ref : treference);
  33. procedure emit_ref_reg(i : tasmop;s : topsize;const ref : treference;reg : tregister);
  34. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;const ref : treference);
  35. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  36. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  37. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  38. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  39. implementation
  40. uses
  41. cutils,
  42. systems,globals,verbose,
  43. cgobj,tgobj,rgobj;
  44. {*****************************************************************************
  45. Helpers
  46. *****************************************************************************}
  47. function def_opsize(p1:tdef):topsize;
  48. begin
  49. case p1.size of
  50. 1 : def_opsize:=S_B;
  51. 2 : def_opsize:=S_W;
  52. 4 : def_opsize:=S_L;
  53. {$ifdef x86_64}
  54. 8 : def_opsize:=S_Q;
  55. {$else x86_64}
  56. { I don't know if we need it (FK) }
  57. 8 : def_opsize:=S_L;
  58. {$endif x86_64}
  59. else
  60. internalerror(130820001);
  61. end;
  62. end;
  63. {*****************************************************************************
  64. Emit Assembler
  65. *****************************************************************************}
  66. procedure emit_none(i : tasmop;s : topsize);
  67. begin
  68. exprasmList.concat(Taicpu.Op_none(i,s));
  69. end;
  70. procedure emit_reg(i : tasmop;s : topsize;reg : tregister);
  71. begin
  72. exprasmList.concat(Taicpu.Op_reg(i,s,reg));
  73. end;
  74. procedure emit_ref(i : tasmop;s : topsize;const ref : treference);
  75. begin
  76. exprasmList.concat(Taicpu.Op_ref(i,s,ref));
  77. end;
  78. procedure emit_const(i : tasmop;s : topsize;c : longint);
  79. begin
  80. exprasmList.concat(Taicpu.Op_const(i,s,aword(c)));
  81. end;
  82. procedure emit_const_reg(i : tasmop;s : topsize;c : longint;reg : tregister);
  83. begin
  84. exprasmList.concat(Taicpu.Op_const_reg(i,s,aword(c),reg));
  85. end;
  86. procedure emit_const_ref(i : tasmop;s : topsize;c : longint;const ref : treference);
  87. begin
  88. exprasmList.concat(Taicpu.Op_const_ref(i,s,aword(c),ref));
  89. end;
  90. procedure emit_ref_reg(i : tasmop;s : topsize;const ref : treference;reg : tregister);
  91. begin
  92. exprasmList.concat(Taicpu.Op_ref_reg(i,s,ref,reg));
  93. end;
  94. procedure emit_reg_ref(i : tasmop;s : topsize;reg : tregister;const ref : treference);
  95. begin
  96. exprasmList.concat(Taicpu.Op_reg_ref(i,s,reg,ref));
  97. end;
  98. procedure emit_reg_reg(i : tasmop;s : topsize;reg1,reg2 : tregister);
  99. var instr:Taicpu;
  100. begin
  101. if not ((reg1=reg2) and (i=A_MOV)) then
  102. begin
  103. instr:=Taicpu.op_reg_reg(i,s,reg1,reg2);
  104. exprasmlist.concat(instr);
  105. if i=A_MOV then
  106. cg.add_move_instruction(instr);
  107. end;
  108. end;
  109. procedure emit_const_reg_reg(i : tasmop;s : topsize;c : longint;reg1,reg2 : tregister);
  110. begin
  111. exprasmList.concat(Taicpu.Op_const_reg_reg(i,s,c,reg1,reg2));
  112. end;
  113. procedure emit_reg_reg_reg(i : tasmop;s : topsize;reg1,reg2,reg3 : tregister);
  114. begin
  115. exprasmList.concat(Taicpu.Op_reg_reg_reg(i,s,reg1,reg2,reg3));
  116. end;
  117. procedure emit_sym(i : tasmop;s : topsize;op : tasmsymbol);
  118. begin
  119. exprasmList.concat(Taicpu.Op_sym(i,s,op));
  120. end;
  121. end.
  122. {
  123. $Log$
  124. Revision 1.6 2003-10-09 21:31:37 daniel
  125. * Register allocator splitted, ans abstract now
  126. Revision 1.5 2003/10/01 20:34:50 peter
  127. * procinfo unit contains tprocinfo
  128. * cginfo renamed to cgbase
  129. * moved cgmessage to verbose
  130. * fixed ppc and sparc compiles
  131. Revision 1.4 2003/09/28 21:49:39 peter
  132. * removed emitjmp
  133. Revision 1.3 2003/09/03 15:55:02 peter
  134. * NEWRA branch merged
  135. Revision 1.2.2.1 2003/08/29 17:29:00 peter
  136. * next batch of updates
  137. Revision 1.2 2003/05/22 21:33:31 peter
  138. * removed some unit dependencies
  139. Revision 1.1 2003/04/30 20:53:32 florian
  140. * error when address of an abstract method is taken
  141. * fixed some x86-64 problems
  142. * merged some more x86-64 and i386 code
  143. Revision 1.38 2003/04/17 16:48:21 daniel
  144. * Added some code to keep track of move instructions in register
  145. allocator
  146. Revision 1.37 2003/03/08 08:59:07 daniel
  147. + $define newra will enable new register allocator
  148. + getregisterint will return imaginary registers with $newra
  149. + -sr switch added, will skip register allocation so you can see
  150. the direct output of the code generator before register allocation
  151. Revision 1.36 2003/02/19 22:00:15 daniel
  152. * Code generator converted to new register notation
  153. - Horribily outdated todo.txt removed
  154. Revision 1.35 2003/01/13 14:54:34 daniel
  155. * Further work to convert codegenerator register convention;
  156. internalerror bug fixed.
  157. Revision 1.34 2003/01/08 18:43:57 daniel
  158. * Tregister changed into a record
  159. Revision 1.33 2002/07/01 18:46:29 peter
  160. * internal linker
  161. * reorganized aasm layer
  162. Revision 1.32 2002/05/18 13:34:21 peter
  163. * readded missing revisions
  164. Revision 1.31 2002/05/16 19:46:50 carl
  165. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  166. + try to fix temp allocation (still in ifdef)
  167. + generic constructor calls
  168. + start of tassembler / tmodulebase class cleanup
  169. Revision 1.29 2002/05/13 19:54:37 peter
  170. * removed n386ld and n386util units
  171. * maybe_save/maybe_restore added instead of the old maybe_push
  172. Revision 1.28 2002/05/12 16:53:16 peter
  173. * moved entry and exitcode to ncgutil and cgobj
  174. * foreach gets extra argument for passing local data to the
  175. iterator function
  176. * -CR checks also class typecasts at runtime by changing them
  177. into as
  178. * fixed compiler to cycle with the -CR option
  179. * fixed stabs with elf writer, finally the global variables can
  180. be watched
  181. * removed a lot of routines from cga unit and replaced them by
  182. calls to cgobj
  183. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  184. u32bit then the other is typecasted also to u32bit without giving
  185. a rangecheck warning/error.
  186. * fixed pascal calling method with reversing also the high tree in
  187. the parast, detected by tcalcst3 test
  188. Revision 1.27 2002/04/25 20:16:39 peter
  189. * moved more routines from cga/n386util
  190. Revision 1.26 2002/04/21 15:29:53 carl
  191. * changeregsize -> rg.makeregsize
  192. Revision 1.25 2002/04/20 21:37:07 carl
  193. + generic FPC_CHECKPOINTER
  194. + first parameter offset in stack now portable
  195. * rename some constants
  196. + move some cpu stuff to other units
  197. - remove unused constents
  198. * fix stacksize for some targets
  199. * fix generic size problems which depend now on EXTEND_SIZE constant
  200. * removing frame pointer in routines is only available for : i386,m68k and vis targets
  201. Revision 1.24 2002/04/19 15:39:34 peter
  202. * removed some more routines from cga
  203. * moved location_force_reg/mem to ncgutil
  204. * moved arrayconstructnode secondpass to ncgld
  205. Revision 1.23 2002/04/15 19:44:20 peter
  206. * fixed stackcheck that would be called recursively when a stack
  207. error was found
  208. * generic changeregsize(reg,size) for i386 register resizing
  209. * removed some more routines from cga unit
  210. * fixed returnvalue handling
  211. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  212. Revision 1.22 2002/04/14 20:54:17 carl
  213. + stack checking enabled for all targets (it is simulated now)
  214. Revision 1.21 2002/04/04 19:06:08 peter
  215. * removed unused units
  216. * use tlocation.size in cg.a_*loc*() routines
  217. Revision 1.20 2002/04/04 18:30:22 carl
  218. + added wdosx support (patch from Pavel)
  219. Revision 1.19 2002/04/02 17:11:33 peter
  220. * tlocation,treference update
  221. * LOC_CONSTANT added for better constant handling
  222. * secondadd splitted in multiple routines
  223. * location_force_reg added for loading a location to a register
  224. of a specified size
  225. * secondassignment parses now first the right and then the left node
  226. (this is compatible with Kylix). This saves a lot of push/pop especially
  227. with string operations
  228. * adapted some routines to use the new cg methods
  229. Revision 1.18 2002/03/31 20:26:37 jonas
  230. + a_loadfpu_* and a_loadmm_* methods in tcg
  231. * register allocation is now handled by a class and is mostly processor
  232. independent (+rgobj.pas and i386/rgcpu.pas)
  233. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  234. * some small improvements and fixes to the optimizer
  235. * some register allocation fixes
  236. * some fpuvaroffset fixes in the unary minus node
  237. * push/popusedregisters is now called rg.save/restoreusedregisters and
  238. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  239. also better optimizable)
  240. * fixed and optimized register saving/restoring for new/dispose nodes
  241. * LOC_FPU locations now also require their "register" field to be set to
  242. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  243. - list field removed of the tnode class because it's not used currently
  244. and can cause hard-to-find bugs
  245. Revision 1.17 2002/03/28 16:07:52 armin
  246. + initialize threadvars defined local in units
  247. Revision 1.16 2002/03/04 19:10:12 peter
  248. * removed compiler warnings
  249. Revision 1.15 2002/01/24 18:25:53 peter
  250. * implicit result variable generation for assembler routines
  251. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  252. Revision 1.14 2002/01/19 14:21:17 peter
  253. * fixed init/final for value parameters
  254. }