hcodegen.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit exports some help routines for the code generation
  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 hcodegen;
  19. interface
  20. uses
  21. cobjects,systems,globals,tree,symtable,types,strings,aasm
  22. {$ifdef i386}
  23. ,i386
  24. {$endif}
  25. {$ifdef m68k}
  26. ,m68k
  27. {$endif}
  28. ;
  29. const
  30. { set, if the procedure uses asm }
  31. pi_uses_asm = $1;
  32. { set, if the procedure is exported by an unit }
  33. pi_is_global = $2;
  34. { set, if the procedure does a call }
  35. { this is for the optimizer }
  36. pi_do_call = $4;
  37. { if the procedure is an operator }
  38. pi_operator = $8;
  39. { set, if the procedure is an external C function }
  40. pi_C_import = $10;
  41. type
  42. pprocinfo = ^tprocinfo;
  43. tprocinfo = record
  44. { pointer to parent in nested procedures }
  45. parent : pprocinfo;
  46. { current class, if we are in a method }
  47. _class : pobjectdef;
  48. { return type }
  49. retdef : pdef;
  50. { frame pointer offset }
  51. framepointer_offset : longint;
  52. { self pointer offset }
  53. ESI_offset : longint;
  54. { result value offset }
  55. retoffset : longint;
  56. { firsttemp position }
  57. firsttemp : longint;
  58. funcret_is_valid : boolean;
  59. { parameter offset }
  60. call_offset : longint;
  61. { some collected informations about the procedure }
  62. { see pi_xxxx above }
  63. flags : longint;
  64. { register used as frame pointer }
  65. framepointer : tregister;
  66. {$ifdef GDB}
  67. { true, if the procedure is exported by an unit }
  68. globalsymbol : boolean;
  69. {$endif * GDB *}
  70. { true, if the procedure should be exported (only OS/2) }
  71. exported : boolean;
  72. { code for the current procedure }
  73. aktproccode,aktentrycode,aktexitcode : paasmoutput;
  74. end;
  75. var
  76. { info about the current sub routine }
  77. procinfo : tprocinfo;
  78. { Die Nummer der Label die bei BREAK bzw CONTINUE }
  79. { angesprungen werden sollen }
  80. aktbreaklabel,aktcontinuelabel : plabel;
  81. { truelabel wird angesprungen, wenn ein Ausdruck true ist, falselabel }
  82. { entsprechend }
  83. truelabel,falselabel : plabel;
  84. { Nr des Labels welches zum Verlassen eines Unterprogramm }
  85. { angesprungen wird }
  86. aktexitlabel : plabel;
  87. { also an exit label, only used we need to clear only the }
  88. { stack }
  89. aktexit2label : plabel;
  90. { only used in constructor for fail or if getmem fails }
  91. quickexitlabel : plabel;
  92. { this asm list contains the debug info }
  93. {debuginfos : paasmoutput; debuglist is enough }
  94. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  95. simple_loadn : boolean;
  96. { enth„lt die gesch„tzte Durchlaufanzahl*100 f�r den }
  97. { momentan bearbeiteten Baum }
  98. t_times : longint;
  99. { true, if an error while code generation occurs }
  100. codegenerror : boolean;
  101. { some support routines for the case instruction }
  102. { counts the labels }
  103. function case_count_labels(root : pcaserecord) : longint;
  104. { searches the highest label }
  105. function case_get_max(root : pcaserecord) : longint;
  106. { searches the lowest label }
  107. function case_get_min(root : pcaserecord) : longint;
  108. { concates the ASCII string to the const segment }
  109. procedure generate_ascii(hs : string);
  110. { inserts the ASCII string to the const segment }
  111. procedure generate_ascii_insert(hs : string);
  112. procedure generate_interrupt_stackframe_entry;
  113. procedure generate_interrupt_stackframe_exit;
  114. implementation
  115. {$ifdef i386}
  116. procedure generate_interrupt_stackframe_entry;
  117. begin
  118. { save the registers of an interrupt procedure }
  119. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  120. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  121. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  122. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  123. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  124. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  125. { .... also the segment registers }
  126. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_DS)));
  127. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_ES)));
  128. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_FS)));
  129. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_GS)));
  130. end;
  131. procedure generate_interrupt_stackframe_exit;
  132. begin
  133. { restore the registers of an interrupt procedure }
  134. { this was all with entrycode instead of exitcode !!}
  135. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  136. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  137. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  138. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  139. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  140. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  141. { .... also the segment registers }
  142. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_DS)));
  143. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_ES)));
  144. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_FS)));
  145. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_GS)));
  146. { this restores the flags }
  147. procinfo.aktexitcode^.concat(new(pai386,op_none(A_IRET,S_NO)));
  148. end;
  149. {$endif}
  150. {$ifdef m68k}
  151. procedure generate_interrupt_stackframe_entry;
  152. begin
  153. { save the registers of an interrupt procedure }
  154. { .... also the segment registers }
  155. end;
  156. procedure generate_interrupt_stackframe_exit;
  157. begin
  158. { restore the registers of an interrupt procedure }
  159. end;
  160. {$endif}
  161. procedure generate_ascii(hs : string);
  162. begin
  163. while length(hs)>32 do
  164. begin
  165. datasegment^.concat(new(pai_string,init(copy(hs,1,32))));
  166. delete(hs,1,32);
  167. end;
  168. datasegment^.concat(new(pai_string,init(hs)))
  169. end;
  170. procedure generate_ascii_insert(hs : string);
  171. begin
  172. while length(hs)>32 do
  173. begin
  174. datasegment^.insert(new(pai_string,init(copy(hs,length(hs)-32+1,length(hs)))));
  175. delete(hs,length(hs)-32+1,length(hs));
  176. end;
  177. datasegment^.insert(new(pai_string,init(hs)));
  178. end;
  179. function case_count_labels(root : pcaserecord) : longint;
  180. var
  181. _l : longint;
  182. procedure count(p : pcaserecord);
  183. begin
  184. inc(_l);
  185. if assigned(p^.less) then
  186. count(p^.less);
  187. if assigned(p^.greater) then
  188. count(p^.greater);
  189. end;
  190. begin
  191. _l:=0;
  192. count(root);
  193. case_count_labels:=_l;
  194. end;
  195. function case_get_max(root : pcaserecord) : longint;
  196. var
  197. hp : pcaserecord;
  198. begin
  199. hp:=root;
  200. while assigned(hp^.greater) do
  201. hp:=hp^.greater;
  202. case_get_max:=hp^._high;
  203. end;
  204. function case_get_min(root : pcaserecord) : longint;
  205. var
  206. hp : pcaserecord;
  207. begin
  208. hp:=root;
  209. while assigned(hp^.less) do
  210. hp:=hp^.less;
  211. case_get_min:=hp^._low;
  212. end;
  213. end.
  214. {
  215. $Log$
  216. Revision 1.1 1998-03-25 11:18:13 root
  217. Initial revision
  218. Revision 1.6 1998/03/10 16:27:38 pierre
  219. * better line info in stabs debug
  220. * symtabletype and lexlevel separated into two fields of tsymtable
  221. + ifdef MAKELIB for direct library output, not complete
  222. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  223. working
  224. + ifdef TESTFUNCRET for setting func result in underfunction, not
  225. working
  226. Revision 1.5 1998/03/10 01:17:19 peter
  227. * all files have the same header
  228. * messages are fully implemented, EXTDEBUG uses Comment()
  229. + AG... files for the Assembler generation
  230. Revision 1.4 1998/03/02 01:48:37 peter
  231. * renamed target_DOS to target_GO32V1
  232. + new verbose system, merged old errors and verbose units into one new
  233. verbose.pas, so errors.pas is obsolete
  234. Revision 1.3 1998/02/13 10:35:03 daniel
  235. * Made Motorola version compilable.
  236. * Fixed optimizer
  237. Revision 1.2 1998/01/16 18:03:15 florian
  238. * small bug fixes, some stuff of delphi styled constructores added
  239. Revision 1.1.1.1 1997/11/27 08:32:56 michael
  240. FPC Compiler CVS start
  241. Pre-CVS log:
  242. CEC Carl-Eric Codere
  243. FK Florian Klaempfl
  244. PM Pierre Muller
  245. + feature added
  246. - removed
  247. * bug fixed or changed
  248. History:
  249. 5th september 1997:
  250. + added support for MC68000 (CEC)
  251. 22th september 1997:
  252. + added tprocinfo member parent (FK)
  253. }