cga.pas 11 KB

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