hcodegen.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. pregvarinfo = ^tregvarinfo;
  94. tregvarinfo = record
  95. regvars : array[1..maxvarregs] of pvarsym;
  96. regvars_para : array[1..maxvarregs] of boolean;
  97. regvars_refs : array[1..maxvarregs] of longint;
  98. fpuregvars : array[1..maxfpuvarregs] of pvarsym;
  99. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  100. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  101. end;
  102. var
  103. { info about the current sub routine }
  104. procinfo : pprocinfo;
  105. { labels for BREAK and CONTINUE }
  106. aktbreaklabel,aktcontinuelabel : pasmlabel;
  107. { label when the result is true or false }
  108. truelabel,falselabel : pasmlabel;
  109. { label to leave the sub routine }
  110. aktexitlabel : pasmlabel;
  111. { also an exit label, only used we need to clear only the stack }
  112. aktexit2label : pasmlabel;
  113. { only used in constructor for fail or if getmem fails }
  114. faillabel,quickexitlabel : pasmlabel;
  115. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  116. simple_loadn : boolean;
  117. { true, if an error while code generation occurs }
  118. codegenerror : boolean;
  119. { save the size of pushed parameter, needed for aligning }
  120. pushedparasize : longint;
  121. make_const_global : boolean;
  122. { message calls with codegenerror support }
  123. procedure cgmessage(t : longint);
  124. procedure cgmessage1(t : longint;const s : string);
  125. procedure cgmessage2(t : longint;const s1,s2 : string);
  126. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  127. procedure CGMessagePos(const pos:tfileposinfo;t:longint);
  128. procedure CGMessagePos1(const pos:tfileposinfo;t:longint;const s1:string);
  129. procedure CGMessagePos2(const pos:tfileposinfo;t:longint;const s1,s2:string);
  130. procedure CGMessagePos3(const pos:tfileposinfo;t:longint;const s1,s2,s3:string);
  131. { initialize respectively terminates the code generator }
  132. { for a new module or procedure }
  133. procedure codegen_doneprocedure;
  134. procedure codegen_donemodule;
  135. procedure codegen_newmodule;
  136. procedure codegen_newprocedure;
  137. implementation
  138. uses
  139. systems,globals,strings,cresstr
  140. {$ifdef fixLeaksOnError}
  141. ,comphook
  142. {$endif fixLeaksOnError}
  143. ;
  144. {$ifdef fixLeaksOnError}
  145. var procinfoStack: TStack;
  146. hcodegen_old_do_stop: tstopprocedure;
  147. {$endif fixLeaksOnError}
  148. {*****************************************************************************
  149. override the message calls to set codegenerror
  150. *****************************************************************************}
  151. procedure cgmessage(t : longint);
  152. var
  153. olderrorcount : longint;
  154. begin
  155. if not(codegenerror) then
  156. begin
  157. olderrorcount:=Errorcount;
  158. verbose.Message(t);
  159. codegenerror:=olderrorcount<>Errorcount;
  160. end;
  161. end;
  162. procedure cgmessage1(t : longint;const s : string);
  163. var
  164. olderrorcount : longint;
  165. begin
  166. if not(codegenerror) then
  167. begin
  168. olderrorcount:=Errorcount;
  169. verbose.Message1(t,s);
  170. codegenerror:=olderrorcount<>Errorcount;
  171. end;
  172. end;
  173. procedure cgmessage2(t : longint;const s1,s2 : string);
  174. var
  175. olderrorcount : longint;
  176. begin
  177. if not(codegenerror) then
  178. begin
  179. olderrorcount:=Errorcount;
  180. verbose.Message2(t,s1,s2);
  181. codegenerror:=olderrorcount<>Errorcount;
  182. end;
  183. end;
  184. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  185. var
  186. olderrorcount : longint;
  187. begin
  188. if not(codegenerror) then
  189. begin
  190. olderrorcount:=Errorcount;
  191. verbose.Message3(t,s1,s2,s3);
  192. codegenerror:=olderrorcount<>Errorcount;
  193. end;
  194. end;
  195. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  196. var
  197. olderrorcount : longint;
  198. begin
  199. if not(codegenerror) then
  200. begin
  201. olderrorcount:=Errorcount;
  202. verbose.MessagePos(pos,t);
  203. codegenerror:=olderrorcount<>Errorcount;
  204. end;
  205. end;
  206. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : string);
  207. var
  208. olderrorcount : longint;
  209. begin
  210. if not(codegenerror) then
  211. begin
  212. olderrorcount:=Errorcount;
  213. verbose.MessagePos1(pos,t,s1);
  214. codegenerror:=olderrorcount<>Errorcount;
  215. end;
  216. end;
  217. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : string);
  218. var
  219. olderrorcount : longint;
  220. begin
  221. if not(codegenerror) then
  222. begin
  223. olderrorcount:=Errorcount;
  224. verbose.MessagePos2(pos,t,s1,s2);
  225. codegenerror:=olderrorcount<>Errorcount;
  226. end;
  227. end;
  228. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : string);
  229. var
  230. olderrorcount : longint;
  231. begin
  232. if not(codegenerror) then
  233. begin
  234. olderrorcount:=Errorcount;
  235. verbose.MessagePos3(pos,t,s1,s2,s3);
  236. codegenerror:=olderrorcount<>Errorcount;
  237. end;
  238. end;
  239. {****************************************************************************
  240. TProcInfo
  241. ****************************************************************************}
  242. constructor tprocinfo.init;
  243. begin
  244. parent:=nil;
  245. _class:=nil;
  246. returntype.reset;
  247. resultfuncretsym:=nil;
  248. funcretsym:=nil;
  249. funcret_state:=vs_none;
  250. def:=nil;
  251. sym:=nil;
  252. framepointer_offset:=0;
  253. selfpointer_offset:=0;
  254. return_offset:=0;
  255. firsttemp_offset:=0;
  256. para_offset:=0;
  257. flags:=0;
  258. framepointer:=R_NO;
  259. globalsymbol:=false;
  260. exported:=false;
  261. no_fast_exit:=false;
  262. aktentrycode:=new(paasmoutput,init);
  263. aktexitcode:=new(paasmoutput,init);
  264. aktproccode:=new(paasmoutput,init);
  265. aktlocaldata:=new(paasmoutput,init);
  266. end;
  267. destructor tprocinfo.done;
  268. begin
  269. dispose(aktentrycode,done);
  270. dispose(aktexitcode,done);
  271. dispose(aktproccode,done);
  272. dispose(aktlocaldata,done);
  273. end;
  274. {*****************************************************************************
  275. initialize/terminate the codegen for procedure and modules
  276. *****************************************************************************}
  277. procedure codegen_newprocedure;
  278. begin
  279. aktbreaklabel:=nil;
  280. aktcontinuelabel:=nil;
  281. { aktexitlabel:=0; is store in oldaktexitlabel
  282. so it must not be reset to zero before this storage !}
  283. { new procinfo }
  284. new(procinfo,init);
  285. {$ifdef fixLeaksOnError}
  286. procinfoStack.push(procinfo);
  287. {$endif fixLeaksOnError}
  288. end;
  289. procedure codegen_doneprocedure;
  290. begin
  291. {$ifdef fixLeaksOnError}
  292. if procinfo <> procinfoStack.pop then
  293. writeln('problem with procinfoStack!');
  294. {$endif fixLeaksOnError}
  295. dispose(procinfo,done);
  296. procinfo:=nil;
  297. end;
  298. procedure codegen_newmodule;
  299. begin
  300. exprasmlist:=new(paasmoutput,init);
  301. datasegment:=new(paasmoutput,init);
  302. codesegment:=new(paasmoutput,init);
  303. bsssegment:=new(paasmoutput,init);
  304. debuglist:=new(paasmoutput,init);
  305. withdebuglist:=new(paasmoutput,init);
  306. consts:=new(paasmoutput,init);
  307. rttilist:=new(paasmoutput,init);
  308. ResourceStringList:=Nil;
  309. importssection:=nil;
  310. exportssection:=nil;
  311. resourcesection:=nil;
  312. { assembler symbols }
  313. asmsymbollist:=new(pdictionary,init);
  314. asmsymbollist^.usehash;
  315. { resourcestrings }
  316. new(ResourceStrings,Init);
  317. end;
  318. procedure codegen_donemodule;
  319. {$ifdef MEMDEBUG}
  320. var
  321. d : tmemdebug;
  322. {$endif}
  323. begin
  324. {$ifdef MEMDEBUG}
  325. d.init('asmlist');
  326. {$endif}
  327. dispose(exprasmlist,done);
  328. dispose(codesegment,done);
  329. dispose(bsssegment,done);
  330. dispose(datasegment,done);
  331. dispose(debuglist,done);
  332. dispose(withdebuglist,done);
  333. dispose(consts,done);
  334. dispose(rttilist,done);
  335. if assigned(ResourceStringList) then
  336. dispose(ResourceStringList,done);
  337. if assigned(importssection) then
  338. dispose(importssection,done);
  339. if assigned(exportssection) then
  340. dispose(exportssection,done);
  341. if assigned(resourcesection) then
  342. dispose(resourcesection,done);
  343. {$ifdef MEMDEBUG}
  344. d.done;
  345. {$endif}
  346. { assembler symbols }
  347. {$ifdef MEMDEBUG}
  348. d.init('asmsymbol');
  349. {$endif}
  350. dispose(asmsymbollist,done);
  351. {$ifdef MEMDEBUG}
  352. d.done;
  353. {$endif}
  354. { resource strings }
  355. dispose(ResourceStrings,done);
  356. end;
  357. {*****************************************************************************
  358. TTempToDestroy
  359. *****************************************************************************}
  360. constructor ttemptodestroy.init(const a : treference;p : pdef);
  361. begin
  362. inherited init;
  363. address:=a;
  364. typ:=p;
  365. end;
  366. {$endif newcg}
  367. {$ifdef fixLeaksOnError}
  368. procedure hcodegen_do_stop; {$ifdef tp} far; {$endif tp}
  369. var p: pprocinfo;
  370. begin
  371. p := pprocinfo(procinfoStack.pop);
  372. while p <> nil Do
  373. begin
  374. dispose(p,done);
  375. p := pprocinfo(procinfoStack.pop);
  376. end;
  377. procinfoStack.done;
  378. do_stop := hcodegen_old_do_stop;
  379. {$ifdef tp}
  380. do_stop;
  381. {$else tp}
  382. do_stop();
  383. {$endif tp}
  384. end;
  385. begin
  386. hcodegen_old_do_stop := do_stop;
  387. do_stop := {$ifdef tp}@{$endif}hcodegen_do_stop;
  388. procinfoStack.init;
  389. {$endif fixLeaksOnError}
  390. end.
  391. {
  392. $Log$
  393. Revision 1.5 2000-08-27 16:11:51 peter
  394. * moved some util functions from globals,cobjects to cutils
  395. * splitted files into finput,fmodule
  396. Revision 1.4 2000/08/12 15:34:22 peter
  397. + usedasmsymbollist to check and reset only the used symbols (merged)
  398. Revision 1.3 2000/08/03 13:17:26 jonas
  399. + allow regvars to be used inside inlined procs, which required the
  400. following changes:
  401. + load regvars in genentrycode/free them in genexitcode (cgai386)
  402. * moved all regvar related code to new regvars unit
  403. + added pregvarinfo type to hcodegen
  404. + added regvarinfo field to tprocinfo (symdef/symdefh)
  405. * deallocate the regvars of the caller in secondprocinline before
  406. inlining the called procedure and reallocate them afterwards
  407. Revision 1.2 2000/07/13 11:32:41 michael
  408. + removed logs
  409. }