hcodegen.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 data segment }
  109. procedure generate_ascii(hs : string);
  110. { inserts the ASCII string to the data segment }
  111. procedure generate_ascii_insert(hs : string);
  112. { concates the ASCII string from pchar to the data segment }
  113. { WARNING : if hs has no #0 and strlen(hs)=length }
  114. { the terminal zero is not written }
  115. procedure generate_pascii(hs : pchar;length : longint);
  116. { inserts the ASCII string from pchar to the data segment }
  117. { see WARNING above }
  118. procedure generate_pascii_insert(hs : pchar;length : longint);
  119. procedure generate_interrupt_stackframe_entry;
  120. procedure generate_interrupt_stackframe_exit;
  121. implementation
  122. {$ifdef i386}
  123. procedure generate_interrupt_stackframe_entry;
  124. begin
  125. { save the registers of an interrupt procedure }
  126. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  127. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EBX)));
  128. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  129. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  130. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_ESI)));
  131. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_L,R_EDI)));
  132. { .... also the segment registers }
  133. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_DS)));
  134. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_ES)));
  135. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_FS)));
  136. procinfo.aktentrycode^.insert(new(pai386,op_reg(A_PUSH,S_W,R_GS)));
  137. end;
  138. procedure generate_interrupt_stackframe_exit;
  139. begin
  140. { restore the registers of an interrupt procedure }
  141. { this was all with entrycode instead of exitcode !!}
  142. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  143. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EBX)));
  144. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  145. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  146. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_ESI)));
  147. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_L,R_EDI)));
  148. { .... also the segment registers }
  149. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_DS)));
  150. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_ES)));
  151. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_FS)));
  152. procinfo.aktexitcode^.concat(new(pai386,op_reg(A_POP,S_W,R_GS)));
  153. { this restores the flags }
  154. procinfo.aktexitcode^.concat(new(pai386,op_none(A_IRET,S_NO)));
  155. end;
  156. {$endif}
  157. {$ifdef m68k}
  158. procedure generate_interrupt_stackframe_entry;
  159. begin
  160. { save the registers of an interrupt procedure }
  161. { .... also the segment registers }
  162. end;
  163. procedure generate_interrupt_stackframe_exit;
  164. begin
  165. { restore the registers of an interrupt procedure }
  166. end;
  167. {$endif}
  168. procedure generate_ascii(hs : string);
  169. begin
  170. while length(hs)>32 do
  171. begin
  172. datasegment^.concat(new(pai_string,init(copy(hs,1,32))));
  173. delete(hs,1,32);
  174. end;
  175. datasegment^.concat(new(pai_string,init(hs)))
  176. end;
  177. procedure generate_ascii_insert(hs : string);
  178. begin
  179. while length(hs)>32 do
  180. begin
  181. datasegment^.insert(new(pai_string,init(copy(hs,length(hs)-32+1,length(hs)))));
  182. { should be avoid very slow }
  183. delete(hs,length(hs)-32+1,length(hs));
  184. end;
  185. datasegment^.insert(new(pai_string,init(hs)));
  186. end;
  187. function strnew(p : pchar;length : longint) : pchar;
  188. var
  189. pc : pchar;
  190. begin
  191. getmem(pc,length);
  192. move(p^,pc^,length);
  193. strnew:=pc;
  194. end;
  195. { concates the ASCII string from pchar to the const segment }
  196. procedure generate_pascii(hs : pchar;length : longint);
  197. var
  198. real_end,current_begin,current_end : pchar;
  199. c :char;
  200. begin
  201. if assigned(hs) then
  202. begin
  203. current_begin:=hs;
  204. real_end:=strend(hs);
  205. c:=hs[0];
  206. while length>32 do
  207. begin
  208. { restore the char displaced }
  209. current_begin[0]:=c;
  210. current_end:=current_begin+32;
  211. { store the char for next loop }
  212. c:=current_end[0];
  213. current_end[0]:=#0;
  214. datasegment^.concat(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  215. length:=length-32;
  216. end;
  217. datasegment^.concat(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  218. end;
  219. end;
  220. { inserts the ASCII string from pchar to the const segment }
  221. procedure generate_pascii_insert(hs : pchar;length : longint);
  222. var
  223. real_end,current_begin,current_end : pchar;
  224. c :char;
  225. begin
  226. if assigned(hs) then
  227. begin
  228. current_begin:=hs;
  229. real_end:=strend(hs);
  230. c:=hs[0];
  231. length:=longint(real_end)-longint(hs);
  232. while length>32 do
  233. begin
  234. { restore the char displaced }
  235. current_begin[0]:=c;
  236. current_end:=current_begin+32;
  237. { store the char for next loop }
  238. c:=current_end[0];
  239. current_end[0]:=#0;
  240. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,32),32)));
  241. length:=length-32;
  242. end;
  243. datasegment^.insert(new(pai_string,init_length_pchar(strnew(current_begin,length),length)));
  244. end;
  245. end;
  246. function case_count_labels(root : pcaserecord) : longint;
  247. var
  248. _l : longint;
  249. procedure count(p : pcaserecord);
  250. begin
  251. inc(_l);
  252. if assigned(p^.less) then
  253. count(p^.less);
  254. if assigned(p^.greater) then
  255. count(p^.greater);
  256. end;
  257. begin
  258. _l:=0;
  259. count(root);
  260. case_count_labels:=_l;
  261. end;
  262. function case_get_max(root : pcaserecord) : longint;
  263. var
  264. hp : pcaserecord;
  265. begin
  266. hp:=root;
  267. while assigned(hp^.greater) do
  268. hp:=hp^.greater;
  269. case_get_max:=hp^._high;
  270. end;
  271. function case_get_min(root : pcaserecord) : longint;
  272. var
  273. hp : pcaserecord;
  274. begin
  275. hp:=root;
  276. while assigned(hp^.less) do
  277. hp:=hp^.less;
  278. case_get_min:=hp^._low;
  279. end;
  280. end.
  281. {
  282. $Log$
  283. Revision 1.2 1998-04-29 10:33:53 pierre
  284. + added some code for ansistring (not complete nor working yet)
  285. * corrected operator overloading
  286. * corrected nasm output
  287. + started inline procedures
  288. + added starstarn : use ** for exponentiation (^ gave problems)
  289. + started UseTokenInfo cond to get accurate positions
  290. Revision 1.1.1.1 1998/03/25 11:18:13 root
  291. * Restored version
  292. Revision 1.6 1998/03/10 16:27:38 pierre
  293. * better line info in stabs debug
  294. * symtabletype and lexlevel separated into two fields of tsymtable
  295. + ifdef MAKELIB for direct library output, not complete
  296. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  297. working
  298. + ifdef TESTFUNCRET for setting func result in underfunction, not
  299. working
  300. Revision 1.5 1998/03/10 01:17:19 peter
  301. * all files have the same header
  302. * messages are fully implemented, EXTDEBUG uses Comment()
  303. + AG... files for the Assembler generation
  304. Revision 1.4 1998/03/02 01:48:37 peter
  305. * renamed target_DOS to target_GO32V1
  306. + new verbose system, merged old errors and verbose units into one new
  307. verbose.pas, so errors.pas is obsolete
  308. Revision 1.3 1998/02/13 10:35:03 daniel
  309. * Made Motorola version compilable.
  310. * Fixed optimizer
  311. Revision 1.2 1998/01/16 18:03:15 florian
  312. * small bug fixes, some stuff of delphi styled constructores added
  313. Revision 1.1.1.1 1997/11/27 08:32:56 michael
  314. FPC Compiler CVS start
  315. Pre-CVS log:
  316. CEC Carl-Eric Codere
  317. FK Florian Klaempfl
  318. PM Pierre Muller
  319. + feature added
  320. - removed
  321. * bug fixed or changed
  322. History:
  323. 5th september 1997:
  324. + added support for MC68000 (CEC)
  325. 22th september 1997:
  326. + added tprocinfo member parent (FK)
  327. }