hcodegen.pas 15 KB

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