hcodegen.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. { true, if an error while code generation occurs }
  109. codegenerror : boolean;
  110. { save the size of pushed parameter, needed for aligning }
  111. pushedparasize : longint;
  112. make_const_global : boolean;
  113. { message calls with codegenerror support }
  114. procedure cgmessage(t : tmsgconst);
  115. procedure cgmessage1(t : tmsgconst;const s : string);
  116. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  117. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  118. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  119. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  120. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  121. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  122. { initialize respectively terminates the code generator }
  123. { for a new module or procedure }
  124. procedure codegen_doneprocedure;
  125. procedure codegen_donemodule;
  126. procedure codegen_newmodule;
  127. procedure codegen_newprocedure;
  128. implementation
  129. uses
  130. systems,globals,files,strings,cresstr
  131. {$ifdef fixLeaksOnError}
  132. ,comphook
  133. {$endif fixLeaksOnError}
  134. ;
  135. {$ifdef fixLeaksOnError}
  136. var procinfoStack: TStack;
  137. hcodegen_old_do_stop: tstopprocedure;
  138. {$endif fixLeaksOnError}
  139. {*****************************************************************************
  140. override the message calls to set codegenerror
  141. *****************************************************************************}
  142. procedure cgmessage(t : tmsgconst);
  143. var
  144. olderrorcount : longint;
  145. begin
  146. if not(codegenerror) then
  147. begin
  148. olderrorcount:=Errorcount;
  149. verbose.Message(t);
  150. codegenerror:=olderrorcount<>Errorcount;
  151. end;
  152. end;
  153. procedure cgmessage1(t : tmsgconst;const s : string);
  154. var
  155. olderrorcount : longint;
  156. begin
  157. if not(codegenerror) then
  158. begin
  159. olderrorcount:=Errorcount;
  160. verbose.Message1(t,s);
  161. codegenerror:=olderrorcount<>Errorcount;
  162. end;
  163. end;
  164. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  165. var
  166. olderrorcount : longint;
  167. begin
  168. if not(codegenerror) then
  169. begin
  170. olderrorcount:=Errorcount;
  171. verbose.Message2(t,s1,s2);
  172. codegenerror:=olderrorcount<>Errorcount;
  173. end;
  174. end;
  175. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  176. var
  177. olderrorcount : longint;
  178. begin
  179. if not(codegenerror) then
  180. begin
  181. olderrorcount:=Errorcount;
  182. verbose.Message3(t,s1,s2,s3);
  183. codegenerror:=olderrorcount<>Errorcount;
  184. end;
  185. end;
  186. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  187. var
  188. olderrorcount : longint;
  189. begin
  190. if not(codegenerror) then
  191. begin
  192. olderrorcount:=Errorcount;
  193. verbose.MessagePos(pos,t);
  194. codegenerror:=olderrorcount<>Errorcount;
  195. end;
  196. end;
  197. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  198. var
  199. olderrorcount : longint;
  200. begin
  201. if not(codegenerror) then
  202. begin
  203. olderrorcount:=Errorcount;
  204. verbose.MessagePos1(pos,t,s1);
  205. codegenerror:=olderrorcount<>Errorcount;
  206. end;
  207. end;
  208. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  209. var
  210. olderrorcount : longint;
  211. begin
  212. if not(codegenerror) then
  213. begin
  214. olderrorcount:=Errorcount;
  215. verbose.MessagePos2(pos,t,s1,s2);
  216. codegenerror:=olderrorcount<>Errorcount;
  217. end;
  218. end;
  219. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  220. var
  221. olderrorcount : longint;
  222. begin
  223. if not(codegenerror) then
  224. begin
  225. olderrorcount:=Errorcount;
  226. verbose.MessagePos3(pos,t,s1,s2,s3);
  227. codegenerror:=olderrorcount<>Errorcount;
  228. end;
  229. end;
  230. {****************************************************************************
  231. TProcInfo
  232. ****************************************************************************}
  233. constructor tprocinfo.init;
  234. begin
  235. parent:=nil;
  236. _class:=nil;
  237. returntype.reset;
  238. resultfuncretsym:=nil;
  239. funcretsym:=nil;
  240. funcret_state:=vs_none;
  241. def:=nil;
  242. sym:=nil;
  243. framepointer_offset:=0;
  244. selfpointer_offset:=0;
  245. return_offset:=0;
  246. firsttemp_offset:=0;
  247. para_offset:=0;
  248. flags:=0;
  249. framepointer:=R_NO;
  250. globalsymbol:=false;
  251. exported:=false;
  252. no_fast_exit:=false;
  253. aktentrycode:=new(paasmoutput,init);
  254. aktexitcode:=new(paasmoutput,init);
  255. aktproccode:=new(paasmoutput,init);
  256. aktlocaldata:=new(paasmoutput,init);
  257. end;
  258. destructor tprocinfo.done;
  259. begin
  260. dispose(aktentrycode,done);
  261. dispose(aktexitcode,done);
  262. dispose(aktproccode,done);
  263. dispose(aktlocaldata,done);
  264. end;
  265. {*****************************************************************************
  266. initialize/terminate the codegen for procedure and modules
  267. *****************************************************************************}
  268. procedure codegen_newprocedure;
  269. begin
  270. aktbreaklabel:=nil;
  271. aktcontinuelabel:=nil;
  272. { aktexitlabel:=0; is store in oldaktexitlabel
  273. so it must not be reset to zero before this storage !}
  274. { new procinfo }
  275. new(procinfo,init);
  276. {$ifdef fixLeaksOnError}
  277. procinfoStack.push(procinfo);
  278. {$endif fixLeaksOnError}
  279. end;
  280. procedure codegen_doneprocedure;
  281. begin
  282. {$ifdef fixLeaksOnError}
  283. if procinfo <> procinfoStack.pop then
  284. writeln('problem with procinfoStack!');
  285. {$endif fixLeaksOnError}
  286. dispose(procinfo,done);
  287. procinfo:=nil;
  288. end;
  289. procedure codegen_newmodule;
  290. begin
  291. exprasmlist:=new(paasmoutput,init);
  292. datasegment:=new(paasmoutput,init);
  293. codesegment:=new(paasmoutput,init);
  294. bsssegment:=new(paasmoutput,init);
  295. debuglist:=new(paasmoutput,init);
  296. withdebuglist:=new(paasmoutput,init);
  297. consts:=new(paasmoutput,init);
  298. rttilist:=new(paasmoutput,init);
  299. ResourceStringList:=Nil;
  300. importssection:=nil;
  301. exportssection:=nil;
  302. resourcesection:=nil;
  303. { assembler symbols }
  304. asmsymbollist:=new(pasmsymbollist,init);
  305. asmsymbollist^.usehash;
  306. { resourcestrings }
  307. new(ResourceStrings,Init);
  308. end;
  309. procedure codegen_donemodule;
  310. {$ifdef MEMDEBUG}
  311. var
  312. d : tmemdebug;
  313. {$endif}
  314. begin
  315. {$ifdef MEMDEBUG}
  316. d.init('asmlist');
  317. {$endif}
  318. dispose(exprasmlist,done);
  319. dispose(codesegment,done);
  320. dispose(bsssegment,done);
  321. dispose(datasegment,done);
  322. dispose(debuglist,done);
  323. dispose(withdebuglist,done);
  324. dispose(consts,done);
  325. dispose(rttilist,done);
  326. if assigned(ResourceStringList) then
  327. dispose(ResourceStringList,done);
  328. if assigned(importssection) then
  329. dispose(importssection,done);
  330. if assigned(exportssection) then
  331. dispose(exportssection,done);
  332. if assigned(resourcesection) then
  333. dispose(resourcesection,done);
  334. {$ifdef MEMDEBUG}
  335. d.done;
  336. {$endif}
  337. { assembler symbols }
  338. {$ifdef MEMDEBUG}
  339. d.init('asmsymbol');
  340. {$endif}
  341. dispose(asmsymbollist,done);
  342. {$ifdef MEMDEBUG}
  343. d.done;
  344. {$endif}
  345. { resource strings }
  346. dispose(ResourceStrings,done);
  347. end;
  348. {*****************************************************************************
  349. TTempToDestroy
  350. *****************************************************************************}
  351. constructor ttemptodestroy.init(const a : treference;p : pdef);
  352. begin
  353. inherited init;
  354. address:=a;
  355. typ:=p;
  356. end;
  357. {$endif newcg}
  358. {$ifdef fixLeaksOnError}
  359. procedure hcodegen_do_stop; {$ifdef tp} far; {$endif tp}
  360. var p: pprocinfo;
  361. begin
  362. p := pprocinfo(procinfoStack.pop);
  363. while p <> nil Do
  364. begin
  365. dispose(p,done);
  366. p := pprocinfo(procinfoStack.pop);
  367. end;
  368. procinfoStack.done;
  369. do_stop := hcodegen_old_do_stop;
  370. {$ifdef tp}
  371. do_stop;
  372. {$else tp}
  373. do_stop();
  374. {$endif tp}
  375. end;
  376. begin
  377. hcodegen_old_do_stop := do_stop;
  378. do_stop := {$ifdef tp}@{$endif}hcodegen_do_stop;
  379. procinfoStack.init;
  380. {$endif fixLeaksOnError}
  381. end.
  382. {
  383. $Log$
  384. Revision 1.59 2000-06-01 19:09:57 peter
  385. * made resourcestrings OOP so it's easier to handle it per module
  386. Revision 1.58 2000/04/02 18:30:12 florian
  387. * fixed another problem with readln(<floating point register variable>);
  388. * the register allocator takes now care of necessary pushes/pops for
  389. readln/writeln
  390. Revision 1.57 2000/02/18 20:53:14 pierre
  391. * fixes a stabs problem for functions
  392. + includes a stabs local var for with statements
  393. the name is with in lowercase followed by an index
  394. for nested with.
  395. + Withdebuglist added because the stabs declarations of local
  396. var are postponed to end of function.
  397. Revision 1.56 2000/02/09 13:22:53 peter
  398. * log truncated
  399. Revision 1.55 2000/01/16 22:17:11 peter
  400. * renamed call_offset to para_offset
  401. Revision 1.54 2000/01/11 17:16:04 jonas
  402. * removed a lot of memory leaks when an error is encountered (caused by
  403. procinfo and pstringcontainers). There are still plenty left though :)
  404. Revision 1.53 2000/01/07 01:14:27 peter
  405. * updated copyright to 2000
  406. Revision 1.52 1999/12/09 23:18:04 pierre
  407. * no_fast_exit if procedure contains implicit termination code
  408. Revision 1.51 1999/12/01 12:42:32 peter
  409. * fixed bug 698
  410. * removed some notes about unused vars
  411. Revision 1.50 1999/11/30 10:40:43 peter
  412. + ttype, tsymlist
  413. Revision 1.49 1999/11/17 17:04:59 pierre
  414. * Notes/hints changes
  415. Revision 1.48 1999/11/09 23:06:45 peter
  416. * esi_offset -> selfpointer_offset to be newcg compatible
  417. * hcogegen -> cgbase fixes for newcg
  418. Revision 1.47 1999/11/06 14:34:21 peter
  419. * truncated log to 20 revs
  420. Revision 1.46 1999/10/21 14:18:54 peter
  421. * tp7 fix
  422. Revision 1.45 1999/10/14 14:57:52 florian
  423. - removed the hcodegen use in the new cg, use cgbase instead
  424. Revision 1.44 1999/10/13 10:42:15 peter
  425. * cgmessagepos functions
  426. Revision 1.43 1999/09/27 23:44:51 peter
  427. * procinfo is now a pointer
  428. * support for result setting in sub procedure
  429. Revision 1.42 1999/08/26 20:24:40 michael
  430. + Hopefuly last fixes for resourcestrings
  431. Revision 1.41 1999/08/24 13:14:03 peter
  432. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  433. Revision 1.40 1999/08/24 12:01:32 michael
  434. + changes for resourcestrings
  435. Revision 1.39 1999/08/19 13:10:18 pierre
  436. + faillabel for _FAIL
  437. Revision 1.38 1999/08/16 18:23:56 peter
  438. * reset resourcestringlist in newmodule.
  439. Revision 1.37 1999/08/04 00:23:02 florian
  440. * renamed i386asm and i386base to cpuasm and cpubase
  441. Revision 1.36 1999/08/01 23:09:26 michael
  442. * procbase -> cpubase
  443. }