hcodegen.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$i defines.inc}
  20. {$ifdef newcg}
  21. interface
  22. implementation
  23. {$else newcg}
  24. interface
  25. uses
  26. cobjects,
  27. tokens,verbose,
  28. aasm,symconst,symtable,cpubase;
  29. const
  30. pi_uses_asm = $1; { set, if the procedure uses asm }
  31. pi_is_global = $2; { set, if the procedure is exported by an unit }
  32. pi_do_call = $4; { set, if the procedure does a call }
  33. pi_operator = $8; { set, if the procedure is an operator }
  34. pi_C_import = $10; { set, if the procedure is an external C function }
  35. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  36. { no register variables }
  37. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  38. => don't optimize}
  39. pi_needs_implicit_finally = $80; { set, if the procedure contains data which }
  40. { needs to be finalized }
  41. type
  42. pprocinfo = ^tprocinfo;
  43. tprocinfo = object
  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. returntype : ttype;
  50. { symbol of the function, and the sym for result variable }
  51. resultfuncretsym,
  52. funcretsym : pfuncretsym;
  53. funcret_state : tvarstate;
  54. { the definition of the proc itself }
  55. def : pprocdef;
  56. sym : pprocsym;
  57. { frame pointer offset }
  58. framepointer_offset : longint;
  59. { self pointer offset }
  60. selfpointer_offset : longint;
  61. { result value offset }
  62. return_offset : longint;
  63. { firsttemp position }
  64. firsttemp_offset : longint;
  65. { parameter offset }
  66. para_offset : longint;
  67. { some collected informations about the procedure }
  68. { see pi_xxxx above }
  69. flags : longint;
  70. { register used as frame pointer }
  71. framepointer : tregister;
  72. { true, if the procedure is exported by an unit }
  73. globalsymbol : boolean;
  74. { true, if the procedure should be exported (only OS/2) }
  75. exported : boolean;
  76. { true, if we can not use fast exit code }
  77. no_fast_exit : boolean;
  78. { code for the current procedure }
  79. aktproccode,aktentrycode,
  80. aktexitcode,aktlocaldata : paasmoutput;
  81. { local data is used for smartlink }
  82. constructor init;
  83. destructor done;
  84. end;
  85. { some kind of temp. types needs to be destructed }
  86. { for example ansistring, this is done using this }
  87. { list }
  88. ptemptodestroy = ^ttemptodestroy;
  89. ttemptodestroy = object(tlinkedlist_item)
  90. typ : pdef;
  91. address : treference;
  92. constructor init(const a : treference;p : pdef);
  93. end;
  94. pregvarinfo = ^tregvarinfo;
  95. tregvarinfo = record
  96. regvars : array[1..maxvarregs] of pvarsym;
  97. regvars_para : array[1..maxvarregs] of boolean;
  98. regvars_refs : array[1..maxvarregs] of longint;
  99. fpuregvars : array[1..maxfpuvarregs] of pvarsym;
  100. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  101. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  102. end;
  103. var
  104. { info about the current sub routine }
  105. procinfo : pprocinfo;
  106. { labels for BREAK and CONTINUE }
  107. aktbreaklabel,aktcontinuelabel : pasmlabel;
  108. { label when the result is true or false }
  109. truelabel,falselabel : pasmlabel;
  110. { label to leave the sub routine }
  111. aktexitlabel : pasmlabel;
  112. { also an exit label, only used we need to clear only the stack }
  113. aktexit2label : pasmlabel;
  114. { only used in constructor for fail or if getmem fails }
  115. faillabel,quickexitlabel : pasmlabel;
  116. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  117. simple_loadn : boolean;
  118. { true, if an error while code generation occurs }
  119. codegenerror : boolean;
  120. { save the size of pushed parameter, needed for aligning }
  121. pushedparasize : longint;
  122. make_const_global : boolean;
  123. { message calls with codegenerror support }
  124. procedure cgmessage(t : longint);
  125. procedure cgmessage1(t : longint;const s : string);
  126. procedure cgmessage2(t : longint;const s1,s2 : string);
  127. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  128. procedure CGMessagePos(const pos:tfileposinfo;t:longint);
  129. procedure CGMessagePos1(const pos:tfileposinfo;t:longint;const s1:string);
  130. procedure CGMessagePos2(const pos:tfileposinfo;t:longint;const s1,s2:string);
  131. procedure CGMessagePos3(const pos:tfileposinfo;t:longint;const s1,s2,s3:string);
  132. { initialize respectively terminates the code generator }
  133. { for a new module or procedure }
  134. procedure codegen_doneprocedure;
  135. procedure codegen_donemodule;
  136. procedure codegen_newmodule;
  137. procedure codegen_newprocedure;
  138. implementation
  139. uses
  140. systems,globals,cresstr
  141. {$ifdef fixLeaksOnError}
  142. ,comphook
  143. {$endif fixLeaksOnError}
  144. ;
  145. {$ifdef fixLeaksOnError}
  146. var procinfoStack: TStack;
  147. hcodegen_old_do_stop: tstopprocedure;
  148. {$endif fixLeaksOnError}
  149. {*****************************************************************************
  150. override the message calls to set codegenerror
  151. *****************************************************************************}
  152. procedure cgmessage(t : longint);
  153. var
  154. olderrorcount : longint;
  155. begin
  156. if not(codegenerror) then
  157. begin
  158. olderrorcount:=Errorcount;
  159. verbose.Message(t);
  160. codegenerror:=olderrorcount<>Errorcount;
  161. end;
  162. end;
  163. procedure cgmessage1(t : longint;const s : string);
  164. var
  165. olderrorcount : longint;
  166. begin
  167. if not(codegenerror) then
  168. begin
  169. olderrorcount:=Errorcount;
  170. verbose.Message1(t,s);
  171. codegenerror:=olderrorcount<>Errorcount;
  172. end;
  173. end;
  174. procedure cgmessage2(t : longint;const s1,s2 : string);
  175. var
  176. olderrorcount : longint;
  177. begin
  178. if not(codegenerror) then
  179. begin
  180. olderrorcount:=Errorcount;
  181. verbose.Message2(t,s1,s2);
  182. codegenerror:=olderrorcount<>Errorcount;
  183. end;
  184. end;
  185. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  186. var
  187. olderrorcount : longint;
  188. begin
  189. if not(codegenerror) then
  190. begin
  191. olderrorcount:=Errorcount;
  192. verbose.Message3(t,s1,s2,s3);
  193. codegenerror:=olderrorcount<>Errorcount;
  194. end;
  195. end;
  196. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  197. var
  198. olderrorcount : longint;
  199. begin
  200. if not(codegenerror) then
  201. begin
  202. olderrorcount:=Errorcount;
  203. verbose.MessagePos(pos,t);
  204. codegenerror:=olderrorcount<>Errorcount;
  205. end;
  206. end;
  207. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : string);
  208. var
  209. olderrorcount : longint;
  210. begin
  211. if not(codegenerror) then
  212. begin
  213. olderrorcount:=Errorcount;
  214. verbose.MessagePos1(pos,t,s1);
  215. codegenerror:=olderrorcount<>Errorcount;
  216. end;
  217. end;
  218. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : string);
  219. var
  220. olderrorcount : longint;
  221. begin
  222. if not(codegenerror) then
  223. begin
  224. olderrorcount:=Errorcount;
  225. verbose.MessagePos2(pos,t,s1,s2);
  226. codegenerror:=olderrorcount<>Errorcount;
  227. end;
  228. end;
  229. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : string);
  230. var
  231. olderrorcount : longint;
  232. begin
  233. if not(codegenerror) then
  234. begin
  235. olderrorcount:=Errorcount;
  236. verbose.MessagePos3(pos,t,s1,s2,s3);
  237. codegenerror:=olderrorcount<>Errorcount;
  238. end;
  239. end;
  240. {****************************************************************************
  241. TProcInfo
  242. ****************************************************************************}
  243. constructor tprocinfo.init;
  244. begin
  245. parent:=nil;
  246. _class:=nil;
  247. returntype.reset;
  248. resultfuncretsym:=nil;
  249. funcretsym:=nil;
  250. funcret_state:=vs_none;
  251. def:=nil;
  252. sym:=nil;
  253. framepointer_offset:=0;
  254. selfpointer_offset:=0;
  255. return_offset:=0;
  256. firsttemp_offset:=0;
  257. para_offset:=0;
  258. flags:=0;
  259. framepointer:=R_NO;
  260. globalsymbol:=false;
  261. exported:=false;
  262. no_fast_exit:=false;
  263. aktentrycode:=new(paasmoutput,init);
  264. aktexitcode:=new(paasmoutput,init);
  265. aktproccode:=new(paasmoutput,init);
  266. aktlocaldata:=new(paasmoutput,init);
  267. end;
  268. destructor tprocinfo.done;
  269. begin
  270. dispose(aktentrycode,done);
  271. dispose(aktexitcode,done);
  272. dispose(aktproccode,done);
  273. dispose(aktlocaldata,done);
  274. end;
  275. {*****************************************************************************
  276. initialize/terminate the codegen for procedure and modules
  277. *****************************************************************************}
  278. procedure codegen_newprocedure;
  279. begin
  280. aktbreaklabel:=nil;
  281. aktcontinuelabel:=nil;
  282. { aktexitlabel:=0; is store in oldaktexitlabel
  283. so it must not be reset to zero before this storage !}
  284. { new procinfo }
  285. new(procinfo,init);
  286. {$ifdef fixLeaksOnError}
  287. procinfoStack.push(procinfo);
  288. {$endif fixLeaksOnError}
  289. end;
  290. procedure codegen_doneprocedure;
  291. begin
  292. {$ifdef fixLeaksOnError}
  293. if procinfo <> procinfoStack.pop then
  294. writeln('problem with procinfoStack!');
  295. {$endif fixLeaksOnError}
  296. dispose(procinfo,done);
  297. procinfo:=nil;
  298. end;
  299. procedure codegen_newmodule;
  300. begin
  301. exprasmlist:=new(paasmoutput,init);
  302. datasegment:=new(paasmoutput,init);
  303. codesegment:=new(paasmoutput,init);
  304. bsssegment:=new(paasmoutput,init);
  305. debuglist:=new(paasmoutput,init);
  306. withdebuglist:=new(paasmoutput,init);
  307. consts:=new(paasmoutput,init);
  308. rttilist:=new(paasmoutput,init);
  309. ResourceStringList:=Nil;
  310. importssection:=nil;
  311. exportssection:=nil;
  312. resourcesection:=nil;
  313. { assembler symbols }
  314. asmsymbollist:=new(pdictionary,init);
  315. asmsymbollist^.usehash;
  316. { resourcestrings }
  317. new(ResourceStrings,Init);
  318. end;
  319. procedure codegen_donemodule;
  320. {$ifdef MEMDEBUG}
  321. var
  322. d : tmemdebug;
  323. {$endif}
  324. begin
  325. {$ifdef MEMDEBUG}
  326. d.init('asmlist');
  327. {$endif}
  328. dispose(exprasmlist,done);
  329. dispose(codesegment,done);
  330. dispose(bsssegment,done);
  331. dispose(datasegment,done);
  332. dispose(debuglist,done);
  333. dispose(withdebuglist,done);
  334. dispose(consts,done);
  335. dispose(rttilist,done);
  336. if assigned(ResourceStringList) then
  337. dispose(ResourceStringList,done);
  338. if assigned(importssection) then
  339. dispose(importssection,done);
  340. if assigned(exportssection) then
  341. dispose(exportssection,done);
  342. if assigned(resourcesection) then
  343. dispose(resourcesection,done);
  344. {$ifdef MEMDEBUG}
  345. d.done;
  346. {$endif}
  347. { assembler symbols }
  348. {$ifdef MEMDEBUG}
  349. d.init('asmsymbol');
  350. {$endif}
  351. dispose(asmsymbollist,done);
  352. {$ifdef MEMDEBUG}
  353. d.done;
  354. {$endif}
  355. { resource strings }
  356. dispose(ResourceStrings,done);
  357. end;
  358. {*****************************************************************************
  359. TTempToDestroy
  360. *****************************************************************************}
  361. constructor ttemptodestroy.init(const a : treference;p : pdef);
  362. begin
  363. inherited init;
  364. address:=a;
  365. typ:=p;
  366. end;
  367. {$endif newcg}
  368. {$ifdef fixLeaksOnError}
  369. procedure hcodegen_do_stop;
  370. var p: pprocinfo;
  371. begin
  372. p := pprocinfo(procinfoStack.pop);
  373. while p <> nil Do
  374. begin
  375. dispose(p,done);
  376. p := pprocinfo(procinfoStack.pop);
  377. end;
  378. procinfoStack.done;
  379. do_stop := hcodegen_old_do_stop;
  380. do_stop{$ifdef FPCPROCVAR}(){$endif};
  381. end;
  382. begin
  383. hcodegen_old_do_stop := do_stop;
  384. do_stop := {$ifdef FPCPROCVAR}@{$endif}hcodegen_do_stop;
  385. procinfoStack.init;
  386. {$endif fixLeaksOnError}
  387. end.
  388. {
  389. $Log$
  390. Revision 1.6 2000-09-24 15:06:17 peter
  391. * use defines.inc
  392. Revision 1.5 2000/08/27 16:11:51 peter
  393. * moved some util functions from globals,cobjects to cutils
  394. * splitted files into finput,fmodule
  395. Revision 1.4 2000/08/12 15:34:22 peter
  396. + usedasmsymbollist to check and reset only the used symbols (merged)
  397. Revision 1.3 2000/08/03 13:17:26 jonas
  398. + allow regvars to be used inside inlined procs, which required the
  399. following changes:
  400. + load regvars in genentrycode/free them in genexitcode (cgai386)
  401. * moved all regvar related code to new regvars unit
  402. + added pregvarinfo type to hcodegen
  403. + added regvarinfo field to tprocinfo (symdef/symdefh)
  404. * deallocate the regvars of the caller in secondprocinline before
  405. inlining the called procedure and reallocate them afterwards
  406. Revision 1.2 2000/07/13 11:32:41 michael
  407. + removed logs
  408. }