hcodegen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. {$ifdef newcg}
  20. interface
  21. implementation
  22. {$else newcg}
  23. interface
  24. uses
  25. cobjects,
  26. tokens,verbose,
  27. aasm,symconst,symtable,cpubase;
  28. const
  29. pi_uses_asm = $1; { set, if the procedure uses asm }
  30. pi_is_global = $2; { set, if the procedure is exported by an unit }
  31. pi_do_call = $4; { set, if the procedure does a call }
  32. pi_operator = $8; { set, if the procedure is an operator }
  33. pi_C_import = $10; { set, if the procedure is an external C function }
  34. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  35. { no register variables }
  36. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  37. => don't optimize}
  38. pi_needs_implicit_finally = $80; { set, if the procedure contains data which }
  39. { needs to be finalized }
  40. type
  41. pprocinfo = ^tprocinfo;
  42. tprocinfo = object
  43. { pointer to parent in nested procedures }
  44. parent : pprocinfo;
  45. { current class, if we are in a method }
  46. _class : pobjectdef;
  47. { return type }
  48. returntype : ttype;
  49. { symbol of the function, and the sym for result variable }
  50. resultfuncretsym,
  51. funcretsym : pfuncretsym;
  52. funcret_state : tvarstate;
  53. { the definition of the proc itself }
  54. def : pprocdef;
  55. sym : pprocsym;
  56. { frame pointer offset }
  57. framepointer_offset : longint;
  58. { self pointer offset }
  59. selfpointer_offset : longint;
  60. { result value offset }
  61. return_offset : longint;
  62. { firsttemp position }
  63. firsttemp_offset : longint;
  64. { parameter offset }
  65. para_offset : longint;
  66. { some collected informations about the procedure }
  67. { see pi_xxxx above }
  68. flags : longint;
  69. { register used as frame pointer }
  70. framepointer : tregister;
  71. { true, if the procedure is exported by an unit }
  72. globalsymbol : boolean;
  73. { true, if the procedure should be exported (only OS/2) }
  74. exported : boolean;
  75. { true, if we can not use fast exit code }
  76. no_fast_exit : boolean;
  77. { code for the current procedure }
  78. aktproccode,aktentrycode,
  79. aktexitcode,aktlocaldata : paasmoutput;
  80. { local data is used for smartlink }
  81. constructor init;
  82. destructor done;
  83. end;
  84. { some kind of temp. types needs to be destructed }
  85. { for example ansistring, this is done using this }
  86. { list }
  87. ptemptodestroy = ^ttemptodestroy;
  88. ttemptodestroy = object(tlinkedlist_item)
  89. typ : pdef;
  90. address : treference;
  91. constructor init(const a : treference;p : pdef);
  92. end;
  93. var
  94. { info about the current sub routine }
  95. procinfo : pprocinfo;
  96. { labels for BREAK and CONTINUE }
  97. aktbreaklabel,aktcontinuelabel : pasmlabel;
  98. { label when the result is true or false }
  99. truelabel,falselabel : pasmlabel;
  100. { label to leave the sub routine }
  101. aktexitlabel : pasmlabel;
  102. { also an exit label, only used we need to clear only the stack }
  103. aktexit2label : pasmlabel;
  104. { only used in constructor for fail or if getmem fails }
  105. faillabel,quickexitlabel : pasmlabel;
  106. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  107. simple_loadn : boolean;
  108. { tries to hold the amount of times which the current tree is processed }
  109. t_times : longint;
  110. { true, if an error while code generation occurs }
  111. codegenerror : boolean;
  112. { save the size of pushed parameter, needed for aligning }
  113. pushedparasize : longint;
  114. make_const_global : boolean;
  115. { message calls with codegenerror support }
  116. procedure cgmessage(t : tmsgconst);
  117. procedure cgmessage1(t : tmsgconst;const s : string);
  118. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  119. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  120. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  121. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  122. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  123. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  124. { initialize respectively terminates the code generator }
  125. { for a new module or procedure }
  126. procedure codegen_doneprocedure;
  127. procedure codegen_donemodule;
  128. procedure codegen_newmodule;
  129. procedure codegen_newprocedure;
  130. implementation
  131. uses
  132. systems,globals,files,strings,cresstr
  133. {$ifdef fixLeaksOnError}
  134. ,comphook
  135. {$endif fixLeaksOnError}
  136. ;
  137. {$ifdef fixLeaksOnError}
  138. var procinfoStack: TStack;
  139. hcodegen_old_do_stop: tstopprocedure;
  140. {$endif fixLeaksOnError}
  141. {*****************************************************************************
  142. override the message calls to set codegenerror
  143. *****************************************************************************}
  144. procedure cgmessage(t : tmsgconst);
  145. var
  146. olderrorcount : longint;
  147. begin
  148. if not(codegenerror) then
  149. begin
  150. olderrorcount:=Errorcount;
  151. verbose.Message(t);
  152. codegenerror:=olderrorcount<>Errorcount;
  153. end;
  154. end;
  155. procedure cgmessage1(t : tmsgconst;const s : string);
  156. var
  157. olderrorcount : longint;
  158. begin
  159. if not(codegenerror) then
  160. begin
  161. olderrorcount:=Errorcount;
  162. verbose.Message1(t,s);
  163. codegenerror:=olderrorcount<>Errorcount;
  164. end;
  165. end;
  166. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  167. var
  168. olderrorcount : longint;
  169. begin
  170. if not(codegenerror) then
  171. begin
  172. olderrorcount:=Errorcount;
  173. verbose.Message2(t,s1,s2);
  174. codegenerror:=olderrorcount<>Errorcount;
  175. end;
  176. end;
  177. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  178. var
  179. olderrorcount : longint;
  180. begin
  181. if not(codegenerror) then
  182. begin
  183. olderrorcount:=Errorcount;
  184. verbose.Message3(t,s1,s2,s3);
  185. codegenerror:=olderrorcount<>Errorcount;
  186. end;
  187. end;
  188. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  189. var
  190. olderrorcount : longint;
  191. begin
  192. if not(codegenerror) then
  193. begin
  194. olderrorcount:=Errorcount;
  195. verbose.MessagePos(pos,t);
  196. codegenerror:=olderrorcount<>Errorcount;
  197. end;
  198. end;
  199. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  200. var
  201. olderrorcount : longint;
  202. begin
  203. if not(codegenerror) then
  204. begin
  205. olderrorcount:=Errorcount;
  206. verbose.MessagePos1(pos,t,s1);
  207. codegenerror:=olderrorcount<>Errorcount;
  208. end;
  209. end;
  210. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  211. var
  212. olderrorcount : longint;
  213. begin
  214. if not(codegenerror) then
  215. begin
  216. olderrorcount:=Errorcount;
  217. verbose.MessagePos2(pos,t,s1,s2);
  218. codegenerror:=olderrorcount<>Errorcount;
  219. end;
  220. end;
  221. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  222. var
  223. olderrorcount : longint;
  224. begin
  225. if not(codegenerror) then
  226. begin
  227. olderrorcount:=Errorcount;
  228. verbose.MessagePos3(pos,t,s1,s2,s3);
  229. codegenerror:=olderrorcount<>Errorcount;
  230. end;
  231. end;
  232. {****************************************************************************
  233. TProcInfo
  234. ****************************************************************************}
  235. constructor tprocinfo.init;
  236. begin
  237. parent:=nil;
  238. _class:=nil;
  239. returntype.reset;
  240. resultfuncretsym:=nil;
  241. funcretsym:=nil;
  242. funcret_state:=vs_none;
  243. def:=nil;
  244. sym:=nil;
  245. framepointer_offset:=0;
  246. selfpointer_offset:=0;
  247. return_offset:=0;
  248. firsttemp_offset:=0;
  249. para_offset:=0;
  250. flags:=0;
  251. framepointer:=R_NO;
  252. globalsymbol:=false;
  253. exported:=false;
  254. no_fast_exit:=false;
  255. aktentrycode:=new(paasmoutput,init);
  256. aktexitcode:=new(paasmoutput,init);
  257. aktproccode:=new(paasmoutput,init);
  258. aktlocaldata:=new(paasmoutput,init);
  259. end;
  260. destructor tprocinfo.done;
  261. begin
  262. dispose(aktentrycode,done);
  263. dispose(aktexitcode,done);
  264. dispose(aktproccode,done);
  265. dispose(aktlocaldata,done);
  266. end;
  267. {*****************************************************************************
  268. initialize/terminate the codegen for procedure and modules
  269. *****************************************************************************}
  270. procedure codegen_newprocedure;
  271. begin
  272. aktbreaklabel:=nil;
  273. aktcontinuelabel:=nil;
  274. { aktexitlabel:=0; is store in oldaktexitlabel
  275. so it must not be reset to zero before this storage !}
  276. { new procinfo }
  277. new(procinfo,init);
  278. {$ifdef fixLeaksOnError}
  279. procinfoStack.push(procinfo);
  280. {$endif fixLeaksOnError}
  281. end;
  282. procedure codegen_doneprocedure;
  283. begin
  284. {$ifdef fixLeaksOnError}
  285. if procinfo <> procinfoStack.pop then
  286. writeln('problem with procinfoStack!');
  287. {$endif fixLeaksOnError}
  288. dispose(procinfo,done);
  289. procinfo:=nil;
  290. end;
  291. procedure codegen_newmodule;
  292. begin
  293. exprasmlist:=new(paasmoutput,init);
  294. datasegment:=new(paasmoutput,init);
  295. codesegment:=new(paasmoutput,init);
  296. bsssegment:=new(paasmoutput,init);
  297. debuglist:=new(paasmoutput,init);
  298. withdebuglist:=new(paasmoutput,init);
  299. consts:=new(paasmoutput,init);
  300. rttilist:=new(paasmoutput,init);
  301. importssection:=nil;
  302. exportssection:=nil;
  303. resourcesection:=nil;
  304. { assembler symbols }
  305. asmsymbollist:=new(pasmsymbollist,init);
  306. asmsymbollist^.usehash;
  307. end;
  308. procedure codegen_donemodule;
  309. {$ifdef MEMDEBUG}
  310. var
  311. d : tmemdebug;
  312. {$endif}
  313. begin
  314. {$ifdef MEMDEBUG}
  315. d.init('asmlist');
  316. {$endif}
  317. dispose(exprasmlist,done);
  318. dispose(codesegment,done);
  319. dispose(bsssegment,done);
  320. dispose(datasegment,done);
  321. dispose(debuglist,done);
  322. dispose(withdebuglist,done);
  323. dispose(consts,done);
  324. dispose(rttilist,done);
  325. if assigned(importssection) then
  326. dispose(importssection,done);
  327. if assigned(exportssection) then
  328. dispose(exportssection,done);
  329. if assigned(resourcesection) then
  330. dispose(resourcesection,done);
  331. {$ifdef MEMDEBUG}
  332. d.done;
  333. {$endif}
  334. { assembler symbols }
  335. {$ifdef MEMDEBUG}
  336. d.init('asmsymbol');
  337. {$endif}
  338. dispose(asmsymbollist,done);
  339. {$ifdef MEMDEBUG}
  340. d.done;
  341. {$endif}
  342. { resourcestrings }
  343. ResetResourceStrings;
  344. end;
  345. {*****************************************************************************
  346. TTempToDestroy
  347. *****************************************************************************}
  348. constructor ttemptodestroy.init(const a : treference;p : pdef);
  349. begin
  350. inherited init;
  351. address:=a;
  352. typ:=p;
  353. end;
  354. {$endif newcg}
  355. {$ifdef fixLeaksOnError}
  356. procedure hcodegen_do_stop; {$ifdef tp} far; {$endif tp}
  357. var p: pprocinfo;
  358. begin
  359. p := pprocinfo(procinfoStack.pop);
  360. while p <> nil Do
  361. begin
  362. dispose(p,done);
  363. p := pprocinfo(procinfoStack.pop);
  364. end;
  365. procinfoStack.done;
  366. do_stop := hcodegen_old_do_stop;
  367. {$ifdef tp}
  368. do_stop;
  369. {$else tp}
  370. do_stop();
  371. {$endif tp}
  372. end;
  373. begin
  374. hcodegen_old_do_stop := do_stop;
  375. do_stop := {$ifdef tp}@{$endif}hcodegen_do_stop;
  376. procinfoStack.init;
  377. {$endif fixLeaksOnError}
  378. end.
  379. {
  380. $Log$
  381. Revision 1.57 2000-02-18 20:53:14 pierre
  382. * fixes a stabs problem for functions
  383. + includes a stabs local var for with statements
  384. the name is with in lowercase followed by an index
  385. for nested with.
  386. + Withdebuglist added because the stabs declarations of local
  387. var are postponed to end of function.
  388. Revision 1.56 2000/02/09 13:22:53 peter
  389. * log truncated
  390. Revision 1.55 2000/01/16 22:17:11 peter
  391. * renamed call_offset to para_offset
  392. Revision 1.54 2000/01/11 17:16:04 jonas
  393. * removed a lot of memory leaks when an error is encountered (caused by
  394. procinfo and pstringcontainers). There are still plenty left though :)
  395. Revision 1.53 2000/01/07 01:14:27 peter
  396. * updated copyright to 2000
  397. Revision 1.52 1999/12/09 23:18:04 pierre
  398. * no_fast_exit if procedure contains implicit termination code
  399. Revision 1.51 1999/12/01 12:42:32 peter
  400. * fixed bug 698
  401. * removed some notes about unused vars
  402. Revision 1.50 1999/11/30 10:40:43 peter
  403. + ttype, tsymlist
  404. Revision 1.49 1999/11/17 17:04:59 pierre
  405. * Notes/hints changes
  406. Revision 1.48 1999/11/09 23:06:45 peter
  407. * esi_offset -> selfpointer_offset to be newcg compatible
  408. * hcogegen -> cgbase fixes for newcg
  409. Revision 1.47 1999/11/06 14:34:21 peter
  410. * truncated log to 20 revs
  411. Revision 1.46 1999/10/21 14:18:54 peter
  412. * tp7 fix
  413. Revision 1.45 1999/10/14 14:57:52 florian
  414. - removed the hcodegen use in the new cg, use cgbase instead
  415. Revision 1.44 1999/10/13 10:42:15 peter
  416. * cgmessagepos functions
  417. Revision 1.43 1999/09/27 23:44:51 peter
  418. * procinfo is now a pointer
  419. * support for result setting in sub procedure
  420. Revision 1.42 1999/08/26 20:24:40 michael
  421. + Hopefuly last fixes for resourcestrings
  422. Revision 1.41 1999/08/24 13:14:03 peter
  423. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  424. Revision 1.40 1999/08/24 12:01:32 michael
  425. + changes for resourcestrings
  426. Revision 1.39 1999/08/19 13:10:18 pierre
  427. + faillabel for _FAIL
  428. Revision 1.38 1999/08/16 18:23:56 peter
  429. * reset resourcestringlist in newmodule.
  430. Revision 1.37 1999/08/04 00:23:02 florian
  431. * renamed i386asm and i386base to cpuasm and cpubase
  432. Revision 1.36 1999/08/01 23:09:26 michael
  433. * procbase -> cpubase
  434. }