hcodegen.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. call_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. { tries to hold the amount of times which the current tree is processed }
  109. t_times : longint;
  110. { true, if an error while code generation occurs }
  111. codegenerror : boolean;
  112. { save the size of pushed parameter, needed for aligning }
  113. pushedparasize : longint;
  114. make_const_global : boolean;
  115. { message calls with codegenerror support }
  116. procedure cgmessage(t : tmsgconst);
  117. procedure cgmessage1(t : tmsgconst;const s : string);
  118. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  119. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  120. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  121. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  122. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  123. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  124. { initialize respectively terminates the code generator }
  125. { for a new module or procedure }
  126. procedure codegen_doneprocedure;
  127. procedure codegen_donemodule;
  128. procedure codegen_newmodule;
  129. procedure codegen_newprocedure;
  130. implementation
  131. uses
  132. systems,globals,files,strings,cresstr;
  133. {*****************************************************************************
  134. override the message calls to set codegenerror
  135. *****************************************************************************}
  136. procedure cgmessage(t : tmsgconst);
  137. var
  138. olderrorcount : longint;
  139. begin
  140. if not(codegenerror) then
  141. begin
  142. olderrorcount:=Errorcount;
  143. verbose.Message(t);
  144. codegenerror:=olderrorcount<>Errorcount;
  145. end;
  146. end;
  147. procedure cgmessage1(t : tmsgconst;const s : string);
  148. var
  149. olderrorcount : longint;
  150. begin
  151. if not(codegenerror) then
  152. begin
  153. olderrorcount:=Errorcount;
  154. verbose.Message1(t,s);
  155. codegenerror:=olderrorcount<>Errorcount;
  156. end;
  157. end;
  158. procedure cgmessage2(t : tmsgconst;const s1,s2 : string);
  159. var
  160. olderrorcount : longint;
  161. begin
  162. if not(codegenerror) then
  163. begin
  164. olderrorcount:=Errorcount;
  165. verbose.Message2(t,s1,s2);
  166. codegenerror:=olderrorcount<>Errorcount;
  167. end;
  168. end;
  169. procedure cgmessage3(t : tmsgconst;const s1,s2,s3 : string);
  170. var
  171. olderrorcount : longint;
  172. begin
  173. if not(codegenerror) then
  174. begin
  175. olderrorcount:=Errorcount;
  176. verbose.Message3(t,s1,s2,s3);
  177. codegenerror:=olderrorcount<>Errorcount;
  178. end;
  179. end;
  180. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  181. var
  182. olderrorcount : longint;
  183. begin
  184. if not(codegenerror) then
  185. begin
  186. olderrorcount:=Errorcount;
  187. verbose.MessagePos(pos,t);
  188. codegenerror:=olderrorcount<>Errorcount;
  189. end;
  190. end;
  191. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  192. var
  193. olderrorcount : longint;
  194. begin
  195. if not(codegenerror) then
  196. begin
  197. olderrorcount:=Errorcount;
  198. verbose.MessagePos1(pos,t,s1);
  199. codegenerror:=olderrorcount<>Errorcount;
  200. end;
  201. end;
  202. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  203. var
  204. olderrorcount : longint;
  205. begin
  206. if not(codegenerror) then
  207. begin
  208. olderrorcount:=Errorcount;
  209. verbose.MessagePos2(pos,t,s1,s2);
  210. codegenerror:=olderrorcount<>Errorcount;
  211. end;
  212. end;
  213. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  214. var
  215. olderrorcount : longint;
  216. begin
  217. if not(codegenerror) then
  218. begin
  219. olderrorcount:=Errorcount;
  220. verbose.MessagePos3(pos,t,s1,s2,s3);
  221. codegenerror:=olderrorcount<>Errorcount;
  222. end;
  223. end;
  224. {****************************************************************************
  225. TProcInfo
  226. ****************************************************************************}
  227. constructor tprocinfo.init;
  228. begin
  229. parent:=nil;
  230. _class:=nil;
  231. returntype.reset;
  232. resultfuncretsym:=nil;
  233. funcretsym:=nil;
  234. funcret_state:=vs_none;
  235. def:=nil;
  236. sym:=nil;
  237. framepointer_offset:=0;
  238. selfpointer_offset:=0;
  239. return_offset:=0;
  240. firsttemp_offset:=0;
  241. call_offset:=0;
  242. flags:=0;
  243. framepointer:=R_NO;
  244. globalsymbol:=false;
  245. exported:=false;
  246. no_fast_exit:=false;
  247. aktentrycode:=new(paasmoutput,init);
  248. aktexitcode:=new(paasmoutput,init);
  249. aktproccode:=new(paasmoutput,init);
  250. aktlocaldata:=new(paasmoutput,init);
  251. end;
  252. destructor tprocinfo.done;
  253. begin
  254. dispose(aktentrycode,done);
  255. dispose(aktexitcode,done);
  256. dispose(aktproccode,done);
  257. dispose(aktlocaldata,done);
  258. end;
  259. {*****************************************************************************
  260. initialize/terminate the codegen for procedure and modules
  261. *****************************************************************************}
  262. procedure codegen_newprocedure;
  263. begin
  264. aktbreaklabel:=nil;
  265. aktcontinuelabel:=nil;
  266. { aktexitlabel:=0; is store in oldaktexitlabel
  267. so it must not be reset to zero before this storage !}
  268. { new procinfo }
  269. new(procinfo,init);
  270. end;
  271. procedure codegen_doneprocedure;
  272. begin
  273. dispose(procinfo,done);
  274. procinfo:=nil;
  275. end;
  276. procedure codegen_newmodule;
  277. begin
  278. exprasmlist:=new(paasmoutput,init);
  279. datasegment:=new(paasmoutput,init);
  280. codesegment:=new(paasmoutput,init);
  281. bsssegment:=new(paasmoutput,init);
  282. debuglist:=new(paasmoutput,init);
  283. consts:=new(paasmoutput,init);
  284. rttilist:=new(paasmoutput,init);
  285. importssection:=nil;
  286. exportssection:=nil;
  287. resourcesection:=nil;
  288. { assembler symbols }
  289. asmsymbollist:=new(pasmsymbollist,init);
  290. asmsymbollist^.usehash;
  291. end;
  292. procedure codegen_donemodule;
  293. {$ifdef MEMDEBUG}
  294. var
  295. d : tmemdebug;
  296. {$endif}
  297. begin
  298. {$ifdef MEMDEBUG}
  299. d.init('asmlist');
  300. {$endif}
  301. dispose(exprasmlist,done);
  302. dispose(codesegment,done);
  303. dispose(bsssegment,done);
  304. dispose(datasegment,done);
  305. dispose(debuglist,done);
  306. dispose(consts,done);
  307. dispose(rttilist,done);
  308. if assigned(importssection) then
  309. dispose(importssection,done);
  310. if assigned(exportssection) then
  311. dispose(exportssection,done);
  312. if assigned(resourcesection) then
  313. dispose(resourcesection,done);
  314. {$ifdef MEMDEBUG}
  315. d.done;
  316. {$endif}
  317. { assembler symbols }
  318. {$ifdef MEMDEBUG}
  319. d.init('asmsymbol');
  320. {$endif}
  321. dispose(asmsymbollist,done);
  322. {$ifdef MEMDEBUG}
  323. d.done;
  324. {$endif}
  325. { resourcestrings }
  326. ResetResourceStrings;
  327. end;
  328. {*****************************************************************************
  329. TTempToDestroy
  330. *****************************************************************************}
  331. constructor ttemptodestroy.init(const a : treference;p : pdef);
  332. begin
  333. inherited init;
  334. address:=a;
  335. typ:=p;
  336. end;
  337. {$endif newcg}
  338. end.
  339. {
  340. $Log$
  341. Revision 1.53 2000-01-07 01:14:27 peter
  342. * updated copyright to 2000
  343. Revision 1.52 1999/12/09 23:18:04 pierre
  344. * no_fast_exit if procedure contains implicit termination code
  345. Revision 1.51 1999/12/01 12:42:32 peter
  346. * fixed bug 698
  347. * removed some notes about unused vars
  348. Revision 1.50 1999/11/30 10:40:43 peter
  349. + ttype, tsymlist
  350. Revision 1.49 1999/11/17 17:04:59 pierre
  351. * Notes/hints changes
  352. Revision 1.48 1999/11/09 23:06:45 peter
  353. * esi_offset -> selfpointer_offset to be newcg compatible
  354. * hcogegen -> cgbase fixes for newcg
  355. Revision 1.47 1999/11/06 14:34:21 peter
  356. * truncated log to 20 revs
  357. Revision 1.46 1999/10/21 14:18:54 peter
  358. * tp7 fix
  359. Revision 1.45 1999/10/14 14:57:52 florian
  360. - removed the hcodegen use in the new cg, use cgbase instead
  361. Revision 1.44 1999/10/13 10:42:15 peter
  362. * cgmessagepos functions
  363. Revision 1.43 1999/09/27 23:44:51 peter
  364. * procinfo is now a pointer
  365. * support for result setting in sub procedure
  366. Revision 1.42 1999/08/26 20:24:40 michael
  367. + Hopefuly last fixes for resourcestrings
  368. Revision 1.41 1999/08/24 13:14:03 peter
  369. * MEMDEBUG to see the sizes of asmlist,asmsymbols,symtables
  370. Revision 1.40 1999/08/24 12:01:32 michael
  371. + changes for resourcestrings
  372. Revision 1.39 1999/08/19 13:10:18 pierre
  373. + faillabel for _FAIL
  374. Revision 1.38 1999/08/16 18:23:56 peter
  375. * reset resourcestringlist in newmodule.
  376. Revision 1.37 1999/08/04 00:23:02 florian
  377. * renamed i386asm and i386base to cpuasm and cpubase
  378. Revision 1.36 1999/08/01 23:09:26 michael
  379. * procbase -> cpubase
  380. Revision 1.35 1999/08/01 23:04:49 michael
  381. + Changes for Alpha
  382. Revision 1.34 1999/07/22 09:37:42 florian
  383. + resourcestring implemented
  384. + start of longstring support
  385. Revision 1.33 1999/05/27 19:44:31 peter
  386. * removed oldasm
  387. * plabel -> pasmlabel
  388. * -a switches to source writing automaticly
  389. * assembler readers OOPed
  390. * asmsymbol automaticly external
  391. * jumptables and other label fixes for asm readers
  392. Revision 1.32 1999/05/21 13:55:01 peter
  393. * NEWLAB for label as symbol
  394. Revision 1.31 1999/05/17 21:57:08 florian
  395. * new temporary ansistring handling
  396. Revision 1.30 1999/05/01 13:24:22 peter
  397. * merged nasm compiler
  398. * old asm moved to oldasm/
  399. Revision 1.29 1999/04/21 09:43:38 peter
  400. * storenumber works
  401. * fixed some typos in double_checksum
  402. + incompatible types type1 and type2 message (with storenumber)
  403. Revision 1.28 1999/03/24 23:17:00 peter
  404. * fixed bugs 212,222,225,227,229,231,233
  405. Revision 1.27 1999/02/25 21:02:37 peter
  406. * ag386bin updates
  407. + coff writer
  408. }