cgbase.pas 14 KB

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