hcodegen.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 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 = record
  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. retdef : pdef;
  49. { return type }
  50. sym : pprocsym;
  51. { symbol of the function, and the sym for result variable }
  52. resultfuncretsym,
  53. funcretsym : pfuncretsym;
  54. { the definition of the proc itself }
  55. { why was this a pdef only ?? PM }
  56. def : pprocdef;
  57. { frame pointer offset }
  58. framepointer_offset : longint;
  59. { self pointer offset }
  60. selfpointer_offset : longint;
  61. { result value offset }
  62. retoffset : longint;
  63. { firsttemp position }
  64. firsttemp : longint;
  65. { funcret_is_valid : boolean; }
  66. funcret_state : tvarstate;
  67. { parameter offset }
  68. call_offset : longint;
  69. { some collected informations about the procedure }
  70. { see pi_xxxx above }
  71. flags : longint;
  72. { register used as frame pointer }
  73. framepointer : tregister;
  74. { true, if the procedure is exported by an unit }
  75. globalsymbol : boolean;
  76. { true, if the procedure should be exported (only OS/2) }
  77. exported : boolean;
  78. { code for the current procedure }
  79. aktproccode,aktentrycode,
  80. aktexitcode,aktlocaldata : paasmoutput;
  81. { local data is used for smartlink }
  82. end;
  83. { some kind of temp. types needs to be destructed }
  84. { for example ansistring, this is done using this }
  85. { list }
  86. ptemptodestroy = ^ttemptodestroy;
  87. ttemptodestroy = object(tlinkedlist_item)
  88. typ : pdef;
  89. address : treference;
  90. constructor init(const a : treference;p : pdef);
  91. end;
  92. var
  93. { info about the current sub routine }
  94. procinfo : pprocinfo;
  95. { labels for BREAK and CONTINUE }
  96. aktbreaklabel,aktcontinuelabel : pasmlabel;
  97. { label when the result is true or false }
  98. truelabel,falselabel : pasmlabel;
  99. { label to leave the sub routine }
  100. aktexitlabel : pasmlabel;
  101. { also an exit label, only used we need to clear only the stack }
  102. aktexit2label : pasmlabel;
  103. { only used in constructor for fail or if getmem fails }
  104. faillabel,quickexitlabel : pasmlabel;
  105. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  106. simple_loadn : boolean;
  107. { tries to hold the amount of times which the current tree is processed }
  108. t_times : longint;
  109. { true, if an error while code generation occurs }
  110. codegenerror : boolean;
  111. { save the size of pushed parameter, needed for aligning }
  112. pushedparasize : longint;
  113. make_const_global : boolean;
  114. { message calls with codegenerror support }
  115. procedure cgmessage(t : tmsgconst);
  116. procedure cgmessage1(t : tmsgconst;const s : string);
  117. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  118. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  119. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  120. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  121. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  122. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  123. { initialize respectively terminates the code generator }
  124. { for a new module or procedure }
  125. procedure codegen_doneprocedure;
  126. procedure codegen_donemodule;
  127. procedure codegen_newmodule;
  128. procedure codegen_newprocedure;
  129. implementation
  130. uses
  131. systems,globals,files,strings,cresstr;
  132. {*****************************************************************************
  133. override the message calls to set codegenerror
  134. *****************************************************************************}
  135. procedure cgmessage(t : tmsgconst);
  136. var
  137. olderrorcount : longint;
  138. begin
  139. if not(codegenerror) then
  140. begin
  141. olderrorcount:=Errorcount;
  142. verbose.Message(t);
  143. codegenerror:=olderrorcount<>Errorcount;
  144. end;
  145. end;
  146. procedure cgmessage1(t : tmsgconst;const s : string);
  147. var
  148. olderrorcount : longint;
  149. begin
  150. if not(codegenerror) then
  151. begin
  152. olderrorcount:=Errorcount;
  153. verbose.Message1(t,s);
  154. codegenerror:=olderrorcount<>Errorcount;
  155. end;
  156. end;
  157. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  158. var
  159. olderrorcount : longint;
  160. begin
  161. if not(codegenerror) then
  162. begin
  163. olderrorcount:=Errorcount;
  164. verbose.Message2(t,s1,s2);
  165. codegenerror:=olderrorcount<>Errorcount;
  166. end;
  167. end;
  168. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  169. var
  170. olderrorcount : longint;
  171. begin
  172. if not(codegenerror) then
  173. begin
  174. olderrorcount:=Errorcount;
  175. verbose.Message3(t,s1,s2,s3);
  176. codegenerror:=olderrorcount<>Errorcount;
  177. end;
  178. end;
  179. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  180. var
  181. olderrorcount : longint;
  182. begin
  183. if not(codegenerror) then
  184. begin
  185. olderrorcount:=Errorcount;
  186. verbose.MessagePos(pos,t);
  187. codegenerror:=olderrorcount<>Errorcount;
  188. end;
  189. end;
  190. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  191. var
  192. olderrorcount : longint;
  193. begin
  194. if not(codegenerror) then
  195. begin
  196. olderrorcount:=Errorcount;
  197. verbose.MessagePos1(pos,t,s1);
  198. codegenerror:=olderrorcount<>Errorcount;
  199. end;
  200. end;
  201. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  202. var
  203. olderrorcount : longint;
  204. begin
  205. if not(codegenerror) then
  206. begin
  207. olderrorcount:=Errorcount;
  208. verbose.MessagePos2(pos,t,s1,s2);
  209. codegenerror:=olderrorcount<>Errorcount;
  210. end;
  211. end;
  212. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  213. var
  214. olderrorcount : longint;
  215. begin
  216. if not(codegenerror) then
  217. begin
  218. olderrorcount:=Errorcount;
  219. verbose.MessagePos3(pos,t,s1,s2,s3);
  220. codegenerror:=olderrorcount<>Errorcount;
  221. end;
  222. end;
  223. {*****************************************************************************
  224. initialize/terminate the codegen for procedure and modules
  225. *****************************************************************************}
  226. procedure codegen_newprocedure;
  227. begin
  228. aktbreaklabel:=nil;
  229. aktcontinuelabel:=nil;
  230. { aktexitlabel:=0; is store in oldaktexitlabel
  231. so it must not be reset to zero before this storage !}
  232. { new procinfo }
  233. new(procinfo);
  234. fillchar(procinfo^,sizeof(tprocinfo),0);
  235. { the type of this lists isn't important }
  236. { because the code of this lists is }
  237. { copied to the code segment }
  238. procinfo^.aktentrycode:=new(paasmoutput,init);
  239. procinfo^.aktexitcode:=new(paasmoutput,init);
  240. procinfo^.aktproccode:=new(paasmoutput,init);
  241. procinfo^.aktlocaldata:=new(paasmoutput,init);
  242. end;
  243. procedure codegen_doneprocedure;
  244. begin
  245. dispose(procinfo^.aktentrycode,done);
  246. dispose(procinfo^.aktexitcode,done);
  247. dispose(procinfo^.aktproccode,done);
  248. dispose(procinfo^.aktlocaldata,done);
  249. dispose(procinfo);
  250. procinfo:=nil;
  251. end;
  252. procedure codegen_newmodule;
  253. begin
  254. exprasmlist:=new(paasmoutput,init);
  255. datasegment:=new(paasmoutput,init);
  256. codesegment:=new(paasmoutput,init);
  257. bsssegment:=new(paasmoutput,init);
  258. debuglist:=new(paasmoutput,init);
  259. consts:=new(paasmoutput,init);
  260. rttilist:=new(paasmoutput,init);
  261. importssection:=nil;
  262. exportssection:=nil;
  263. resourcesection:=nil;
  264. { assembler symbols }
  265. asmsymbollist:=new(pasmsymbollist,init);
  266. asmsymbollist^.usehash;
  267. end;
  268. procedure codegen_donemodule;
  269. {$ifdef MEMDEBUG}
  270. var
  271. d : tmemdebug;
  272. {$endif}
  273. begin
  274. {$ifdef MEMDEBUG}
  275. d.init('asmlist');
  276. {$endif}
  277. dispose(exprasmlist,done);
  278. dispose(codesegment,done);
  279. dispose(bsssegment,done);
  280. dispose(datasegment,done);
  281. dispose(debuglist,done);
  282. dispose(consts,done);
  283. dispose(rttilist,done);
  284. if assigned(importssection) then
  285. dispose(importssection,done);
  286. if assigned(exportssection) then
  287. dispose(exportssection,done);
  288. if assigned(resourcesection) then
  289. dispose(resourcesection,done);
  290. {$ifdef MEMDEBUG}
  291. d.done;
  292. {$endif}
  293. { assembler symbols }
  294. {$ifdef MEMDEBUG}
  295. d.init('asmsymbol');
  296. {$endif}
  297. dispose(asmsymbollist,done);
  298. {$ifdef MEMDEBUG}
  299. d.done;
  300. {$endif}
  301. { resourcestrings }
  302. ResetResourceStrings;
  303. end;
  304. {*****************************************************************************
  305. TTempToDestroy
  306. *****************************************************************************}
  307. constructor ttemptodestroy.init(const a : treference;p : pdef);
  308. begin
  309. inherited init;
  310. address:=a;
  311. typ:=p;
  312. end;
  313. {$endif newcg}
  314. end.
  315. {
  316. $Log$
  317. Revision 1.49 1999-11-17 17:04:59 pierre
  318. * Notes/hints changes
  319. Revision 1.48 1999/11/09 23:06:45 peter
  320. * esi_offset -> selfpointer_offset to be newcg compatible
  321. * hcogegen -> cgbase fixes for newcg
  322. Revision 1.47 1999/11/06 14:34:21 peter
  323. * truncated log to 20 revs
  324. Revision 1.46 1999/10/21 14:18:54 peter
  325. * tp7 fix
  326. Revision 1.45 1999/10/14 14:57:52 florian
  327. - removed the hcodegen use in the new cg, use cgbase instead
  328. Revision 1.44 1999/10/13 10:42:15 peter
  329. * cgmessagepos functions
  330. Revision 1.43 1999/09/27 23:44:51 peter
  331. * procinfo is now a pointer
  332. * support for result setting in sub procedure
  333. Revision 1.42 1999/08/26 20:24:40 michael
  334. + Hopefuly last fixes for resourcestrings
  335. Revision 1.41 1999/08/24 13:14:03 peter
  336. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  337. Revision 1.40 1999/08/24 12:01:32 michael
  338. + changes for resourcestrings
  339. Revision 1.39 1999/08/19 13:10:18 pierre
  340. + faillabel for _FAIL
  341. Revision 1.38 1999/08/16 18:23:56 peter
  342. * reset resourcestringlist in newmodule.
  343. Revision 1.37 1999/08/04 00:23:02 florian
  344. * renamed i386asm and i386base to cpuasm and cpubase
  345. Revision 1.36 1999/08/01 23:09:26 michael
  346. * procbase -> cpubase
  347. Revision 1.35 1999/08/01 23:04:49 michael
  348. + Changes for Alpha
  349. Revision 1.34 1999/07/22 09:37:42 florian
  350. + resourcestring implemented
  351. + start of longstring support
  352. Revision 1.33 1999/05/27 19:44:31 peter
  353. * removed oldasm
  354. * plabel -> pasmlabel
  355. * -a switches to source writing automaticly
  356. * assembler readers OOPed
  357. * asmsymbol automaticly external
  358. * jumptables and other label fixes for asm readers
  359. Revision 1.32 1999/05/21 13:55:01 peter
  360. * NEWLAB for label as symbol
  361. Revision 1.31 1999/05/17 21:57:08 florian
  362. * new temporary ansistring handling
  363. Revision 1.30 1999/05/01 13:24:22 peter
  364. * merged nasm compiler
  365. * old asm moved to oldasm/
  366. Revision 1.29 1999/04/21 09:43:38 peter
  367. * storenumber works
  368. * fixed some typos in double_checksum
  369. + incompatible types type1 and type2 message (with storenumber)
  370. Revision 1.28 1999/03/24 23:17:00 peter
  371. * fixed bugs 212,222,225,227,229,231,233
  372. Revision 1.27 1999/02/25 21:02:37 peter
  373. * ag386bin updates
  374. + coff writer
  375. }