README 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. $Id$
  2. Module CPUBASE
  3. --------------
  4. CONSTANTS used throughout the code generator
  5. --------------------------------------------
  6. frame_pointer equals the register used as frame pointer
  7. stack_pointer equals the register used as stack pointer
  8. self_pointer equals the register used as self pointer
  9. accumulator equals the register which will be used
  10. as function return values
  11. unusedregsint set of Currently available integer registers
  12. unusedregsfpu set of Currently available fpu registers
  13. unusedregsmm set of Currently available mm registers
  14. availabletempregsint set of maximally available integer registers
  15. availabletempregsfpu set of maximally available fpu registers
  16. availabletempregsmm set of maximally available mm registers
  17. countusableregsint count of currently available integer registers
  18. countusableregsfpu count of currently available fpu registers
  19. countusableregsmm count of currently available mm registers
  20. c_countusableregsint count of max. available int registers (in the current procedure)
  21. c_countusableregsfpu count of max. available fpu registers (in the current procedure)
  22. c_countusableregsmm count of max. available mm registers (in the current procedure)
  23. intregs all!! available integer register
  24. fpuregs all!! available fpu register
  25. mmregs all!! available multimedia register
  26. lvaluelocations a set of all locations which can be an l-value
  27. Locations
  28. ---------
  29. The first pass assigns these location types which are then
  30. used by the code generator to write out the correct instructions:
  31. LOC_INVALID = This is an error and should never occur
  32. LOC_REGISTER = Location is in a register
  33. LOC_MEM = Memory reference (symbolic or register address?)
  34. LOC_REFERENCE = Memory reference (symbolic or register address?)
  35. LOC_JUMP = ????
  36. LOC_FLAGS = Value is in the flags (Florian, this will give problems!)
  37. LOC_CREGISTER = Value is in a constant register (across calls -
  38. used for optimizations) - Constant registers
  39. should not be directly modified????
  40. LOC_CONST = Value is a numeric constant
  41. Operand Sizes
  42. -------------
  43. OS_NO = No operand size.
  44. OS_8 = 8-bit signed or unsigned value
  45. OS_16 = 16-bit signed or unsigned value
  46. OS_32 = 32-bit signed or unsigned value
  47. OS_64 = 64-bit signed or unsigned value
  48. Intel specific
  49. --------------
  50. unusedregssse
  51. availabletempregssse
  52. countusableregssse
  53. Jonas Maebe schrieb:
  54. >
  55. > Hello,
  56. >
  57. > Is there any difference between the localsize parameter of
  58. > g_stackframe_entry and the parasize parameter of g_return_from_proc, or
  59. > are they both the same value?
  60. They are different, I think the value of g_return_from_proc doesn't matter
  61. for the PowerPC. It's the size of parameters passed on the stack
  62. and only important for the i386/m68k probably.
  63. >
  64. > And for the PowerPC, what will they contain? Just the size of the local
  65. > variables and parameters, or also the maximum needed size for parameters
  66. > of any procedure called by the current one (the caller must reserve space
  67. > for the callee's parameters on it's own stack because you can't push
  68. > values on the stack in the middle of a procedure (no frame pointer))
  69. >
  70. > Jonas
  71. the parameter passed to g_stackframe_entry contains the size of the all local space which is
  72. needed
  73. except
  74. that one for saving registers: the set procinfo.registerstosave (not yet implemented,
  75. I'll commit it soon) will contain
  76. all registers which must be saved by the entry and restored by the exit code of a procedure
  77. and you have to add extra space to do that.
  78. The code generation
  79. -------------------
  80. The code generation can be seperated into 3 layers:
  81. 1. the method secondpass of the tnode childs
  82. 2. the procedure variables p2_
  83. 3. the code generator object
  84. 1.: This procedure does very high level stuff, if the code generation
  85. is processor independent, it calls the appropriate procedures of the
  86. code generator object to generate the code, but in most cases, it
  87. calls procedure variables of the second layer
  88. 2. This procedure variables must be initialized to match the
  89. current processor, these variables are used to optimize
  90. existing processor instructions(? CEC).
  91. The following procedure variables are currently used
  92. Name Purpose Alternatives
  93. -----------------------------------------------------------------------------
  94. p2_assignment
  95. p2_assignment_int64_reg Do an assignment of a int64
  96. 3. The code generator object does very basic operations like generating
  97. move code etc, which is called by the p2_ functions and by the
  98. secondpass procedures.
  99. Alignment
  100. ---------
  101. The alignment is handled very easily: treference contains a field
  102. alignment which describes the ensured alignment for the node, possible
  103. values: 1,2,4,8,16 (1 means unligned). The code generator must update
  104. that field at the appropriate places and take care of it when
  105. generating the code
  106. MODULE CGOBJ (The code generator object)
  107. ------------
  108. This is the basis of the code generator, it includes several
  109. template instructions which are used to create a processor
  110. independant code generator.
  111. Fields:
  112. scratch_register_array_pointer : aword;
  113. ?????????????????????
  114. Indicates the free scratch registers?
  115. unusedscratchregisters : tregisterset;
  116. This holds the currently unused registers which can
  117. be used as temporary placeholders.
  118. alignment : talignment; ?? Why is this in cg object, should not
  119. this be a constant instead?
  120. Template instructions
  121. ---------------------
  122. procedure a_call_name
  123. Call a routine by symbolic name with a possible
  124. numeric offset value.
  125. ???? WE ASSUME UNSIGNED???
  126. { move instructions }
  127. procedure a_load_const_reg
  128. --------------------------
  129. Move a constant value to a register
  130. procedure a_load_reg_ref
  131. ------------------------
  132. Move a register value to a memory reference
  133. procedure a_load_ref_reg
  134. ------------------------
  135. Move the value at a specified address into a register
  136. procedure a_load_reg_reg
  137. ------------------------
  138. Move from register to register
  139. WE NEED !!!!MOVE WITH SIGN EXTENSION??????????????????????
  140. { comparison operations }
  141. ????????????? WHAT DOES THE LABELS MEAN????????
  142. procedure a_cmp_reg_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  143. l : pasmlabel);virtual;
  144. procedure a_cmp_reg_reg_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  145. procedure a_cmp_reg_ref_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;l : pasmlabel);
  146. procedure a_cmp_ref_const_label(list : paasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  147. l : pasmlabel);
  148. procedure a_jmp_cond(list : paasmoutput;cond : TOpCmp;l: pasmlabel);
  149. procedure a_loadaddress_ref_reg(list : paasmoutput;const ref : treference;r : tregister);virtual;
  150. ??????????????
  151. { allocates register r by inserting a pai_realloc record }
  152. procedure a_reg_alloc(list : paasmoutput;r : tregister);
  153. { deallocates register r by inserting a pa_regdealloc record}
  154. procedure a_reg_dealloc(list : paasmoutput;r : tregister);
  155. { returns a register for use as scratch register }
  156. function get_scratch_reg(list : paasmoutput) : tregister;
  157. { releases a scratch register }
  158. procedure free_scratch_reg(list : paasmoutput;r : tregister);
  159. {************************************************}
  160. { code generation for subroutine entry/exit code }
  161. { initilizes data of type t }
  162. { if is_already_ref is true then the routines assumes }
  163. { that r points to the data to initialize ???? }
  164. procedure g_initialize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  165. { finalizes data of type t }
  166. { if is_already_ref is true then the routines assumes }
  167. { that r points to the data to finalizes ???? }
  168. procedure g_finalize(list : paasmoutput;t : pdef;const ref : treference;is_already_ref : boolean);
  169. { helper routines }
  170. procedure g_initialize_data(list : paasmoutput;p : psym);
  171. procedure g_incr_data(list : paasmoutput;p : psym);
  172. procedure g_finalize_data(list : paasmoutput;p : pnamedindexobject);
  173. procedure g_copyvalueparas(list : paasmoutput;p : pnamedindexobject);
  174. procedure g_finalizetempansistrings(list : paasmoutput);
  175. procedure g_entrycode(list : paasmoutput;
  176. const proc_names : tstringcontainer;make_global : boolean;
  177. stackframe : longint;var parasize : longint;
  178. var nostackframe : boolean;inlined : boolean);
  179. procedure g_exitcode(list : paasmoutput;parasize : longint;
  180. nostackframe,inlined : boolean);
  181. { string helper routines }
  182. procedure g_decrstrref(list : paasmoutput;const ref : treference;t : pdef);
  183. procedure g_removetemps(list : paasmoutput;p : plinkedlist);
  184. { passing parameters, per default the parameter is pushed }
  185. { nr gives the number of the parameter (enumerated from }
  186. { left to right), this allows to move the parameter to }
  187. { register, if the cpu supports register calling }
  188. { conventions }
  189. procedure a_param_reg(list : paasmoutput;size : tcgsize;r : tregister;nr : longint);virtual;
  190. procedure a_param_const(list : paasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  191. procedure a_param_ref(list : paasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  192. procedure a_paramaddr_ref(list : paasmoutput;const r : treference;nr : longint);virtual;
  193. {**********************************}
  194. { these methods must be overriden: }
  195. { Remarks:
  196. * If a method specifies a size you have only to take care
  197. of that number of bits, i.e. load_const_reg with OP_8 must
  198. only load the lower 8 bit of the specified register
  199. the rest of the register can be undefined
  200. if necessary the compiler will call a method
  201. to zero or sign extend the register
  202. * The a_load_XX_XX with OP_64 needn't to be
  203. implemented for 32 bit
  204. processors, the code generator takes care of that
  205. * the addr size is for work with the natural pointer
  206. size
  207. * the procedures without fpu/mm are only for integer usage
  208. * normally the first location is the source and the
  209. second the destination
  210. }
  211. Virtual instruction templates:
  212. procedure g_stackframe_entry(list : paasmoutput;localsize : longint);virtual;
  213. { restores the frame pointer at procedure exit, for the }
  214. { i386 it generates a simple leave }
  215. procedure g_restore_frame_pointer(list : paasmoutput);virtual;
  216. { some processors like the PPC doesn't allow to change the stack in }
  217. { a procedure, so we need to maintain an extra stack for the }
  218. { result values of setjmp in exception code }
  219. { this two procedures are for pushing an exception value, }
  220. { they can use the scratch registers }
  221. procedure g_push_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  222. procedure g_push_exception_value_const(list : paasmoutput;reg : tregister);virtual;
  223. { that procedure pops a exception value }
  224. procedure g_pop_exception_value_reg(list : paasmoutput;reg : tregister);virtual;
  225. procedure g_return_from_proc(list : paasmoutput;parasize : aword);virtual;
  226. {********************************************************}
  227. { these methods can be overriden for extra functionality }
  228. { the following methods do nothing: }
  229. procedure g_interrupt_stackframe_entry(list : paasmoutput);virtual;
  230. procedure g_interrupt_stackframe_exit(list : paasmoutput);virtual;
  231. procedure g_profilecode(list : paasmoutput);virtual;
  232. procedure g_stackcheck(list : paasmoutput;stackframesize : longint);virtual;
  233. procedure a_load_const_ref(list : paasmoutput;size : tcgsize;a : aword;const ref : treference);virtual;
  234. procedure g_maybe_loadself(list : paasmoutput);virtual;
  235. { copies len bytes from the source to destination, if }
  236. { loadref is true, it assumes that it first must load }
  237. { the source address from the memory location where }
  238. { source points to }
  239. procedure g_concatcopy(list : paasmoutput;const source,dest : treference;len : aword;loadref : boolean);virtual;
  240. { uses the addr of ref as param, was emitpushreferenceaddr }
  241. procedure a_param_ref_addr(list : paasmoutput;r : treference;nr : longint);virtual;
  242. CVS Log
  243. -------
  244. $Log$
  245. Revision 1.6 2000-03-02 03:22:16 carl
  246. + More information and suggestions
  247. Revision 1.5 2000/03/01 15:36:12 florian
  248. * some new stuff for the new cg
  249. Revision 1.4 1999/10/14 14:57:54 florian
  250. - removed the hcodegen use in the new cg, use cgbase instead