hcodegen.pas 15 KB

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