hcodegen.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. interface
  20. uses
  21. cobjects,
  22. tokens,verbose,
  23. aasm,symtable
  24. {$ifdef i386}
  25. {$ifdef ag386bin}
  26. ,i386base
  27. {$else}
  28. ,i386
  29. {$endif}
  30. {$endif}
  31. {$ifdef m68k}
  32. ,m68k
  33. {$endif}
  34. ;
  35. const
  36. pi_uses_asm = $1; { set, if the procedure uses asm }
  37. pi_is_global = $2; { set, if the procedure is exported by an unit }
  38. pi_do_call = $4; { set, if the procedure does a call }
  39. pi_operator = $8; { set, if the procedure is an operator }
  40. pi_C_import = $10; { set, if the procedure is an external C function }
  41. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  42. { no register variables }
  43. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  44. => don't optimize}
  45. type
  46. pprocinfo = ^tprocinfo;
  47. tprocinfo = record
  48. { pointer to parent in nested procedures }
  49. parent : pprocinfo;
  50. { current class, if we are in a method }
  51. _class : pobjectdef;
  52. { return type }
  53. retdef : pdef;
  54. { return type }
  55. sym : pprocsym;
  56. { symbol of the function }
  57. funcretsym : pfuncretsym;
  58. { the definition of the proc itself }
  59. { why was this a pdef only ?? PM }
  60. def : pprocdef;
  61. { frame pointer offset }
  62. framepointer_offset : longint;
  63. { self pointer offset }
  64. ESI_offset : longint;
  65. { result value offset }
  66. retoffset : longint;
  67. { firsttemp position }
  68. firsttemp : longint;
  69. funcret_is_valid : boolean;
  70. { parameter offset }
  71. call_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. { code for the current procedure }
  82. aktproccode,aktentrycode,
  83. aktexitcode,aktlocaldata : paasmoutput;
  84. { local data is used for smartlink }
  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. var
  96. { info about the current sub routine }
  97. procinfo : tprocinfo;
  98. { labels for BREAK and CONTINUE }
  99. aktbreaklabel,aktcontinuelabel : plabel;
  100. { label when the result is true or false }
  101. truelabel,falselabel : plabel;
  102. { label to leave the sub routine }
  103. aktexitlabel : plabel;
  104. { also an exit label, only used we need to clear only the stack }
  105. aktexit2label : plabel;
  106. { only used in constructor for fail or if getmem fails }
  107. quickexitlabel : plabel;
  108. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  109. simple_loadn : boolean;
  110. { tries to hold the amount of times which the current tree is processed }
  111. t_times : longint;
  112. { true, if an error while code generation occurs }
  113. codegenerror : boolean;
  114. { save the size of pushed parameter, needed for aligning }
  115. pushedparasize : longint;
  116. make_const_global : boolean;
  117. temptoremove : plinkedlist;
  118. { message calls with codegenerror support }
  119. procedure cgmessage(const t : tmsgconst);
  120. procedure cgmessage1(const t : tmsgconst;const s : string);
  121. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  122. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  123. { helpers }
  124. procedure maybe_concat_external(symt : psymtable;const name : string);
  125. { initialize respectively terminates the code generator }
  126. { for a new module or procedure }
  127. procedure codegen_doneprocedure;
  128. procedure codegen_donemodule;
  129. procedure codegen_newmodule;
  130. procedure codegen_newprocedure;
  131. implementation
  132. uses
  133. systems,globals,files,strings;
  134. {*****************************************************************************
  135. override the message calls to set codegenerror
  136. *****************************************************************************}
  137. procedure cgmessage(const t : tmsgconst);
  138. var
  139. olderrorcount : longint;
  140. begin
  141. if not(codegenerror) then
  142. begin
  143. olderrorcount:=Errorcount;
  144. verbose.Message(t);
  145. codegenerror:=olderrorcount<>Errorcount;
  146. end;
  147. end;
  148. procedure cgmessage1(const t : tmsgconst;const s : string);
  149. var
  150. olderrorcount : longint;
  151. begin
  152. if not(codegenerror) then
  153. begin
  154. olderrorcount:=Errorcount;
  155. verbose.Message1(t,s);
  156. codegenerror:=olderrorcount<>Errorcount;
  157. end;
  158. end;
  159. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  160. var
  161. olderrorcount : longint;
  162. begin
  163. if not(codegenerror) then
  164. begin
  165. olderrorcount:=Errorcount;
  166. verbose.Message2(t,s1,s2);
  167. codegenerror:=olderrorcount<>Errorcount;
  168. end;
  169. end;
  170. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  171. var
  172. olderrorcount : longint;
  173. begin
  174. if not(codegenerror) then
  175. begin
  176. olderrorcount:=Errorcount;
  177. verbose.Message3(t,s1,s2,s3);
  178. codegenerror:=olderrorcount<>Errorcount;
  179. end;
  180. end;
  181. {*****************************************************************************
  182. Helpers
  183. *****************************************************************************}
  184. procedure maybe_concat_external(symt : psymtable;const name : string);
  185. begin
  186. if (symt^.symtabletype=unitsymtable) or
  187. ((symt^.symtabletype in [recordsymtable,objectsymtable]) and
  188. (symt^.defowner^.owner^.symtabletype=unitsymtable)) or
  189. ((symt^.symtabletype=withsymtable) and
  190. (symt^.defowner^.owner^.symtabletype=unitsymtable)) then
  191. concat_external(name,EXT_NEAR);
  192. end;
  193. {*****************************************************************************
  194. initialize/terminate the codegen for procedure and modules
  195. *****************************************************************************}
  196. procedure codegen_newprocedure;
  197. begin
  198. aktbreaklabel:=nil;
  199. aktcontinuelabel:=nil;
  200. { aktexitlabel:=0; is store in oldaktexitlabel
  201. so it must not be reset to zero before this storage !}
  202. { the type of this lists isn't important }
  203. { because the code of this lists is }
  204. { copied to the code segment }
  205. procinfo.aktentrycode:=new(paasmoutput,init);
  206. procinfo.aktexitcode:=new(paasmoutput,init);
  207. procinfo.aktproccode:=new(paasmoutput,init);
  208. procinfo.aktlocaldata:=new(paasmoutput,init);
  209. end;
  210. procedure codegen_doneprocedure;
  211. begin
  212. dispose(procinfo.aktentrycode,done);
  213. dispose(procinfo.aktexitcode,done);
  214. dispose(procinfo.aktproccode,done);
  215. dispose(procinfo.aktlocaldata,done);
  216. end;
  217. procedure codegen_newmodule;
  218. begin
  219. exprasmlist:=new(paasmoutput,init);
  220. datasegment:=new(paasmoutput,init);
  221. codesegment:=new(paasmoutput,init);
  222. bsssegment:=new(paasmoutput,init);
  223. debuglist:=new(paasmoutput,init);
  224. externals:=new(paasmoutput,init);
  225. internals:=new(paasmoutput,init);
  226. consts:=new(paasmoutput,init);
  227. rttilist:=new(paasmoutput,init);
  228. importssection:=nil;
  229. exportssection:=nil;
  230. resourcesection:=nil;
  231. asmsymbollist:=new(pasmsymbollist,init);
  232. asmsymbollist^.usehash;
  233. end;
  234. procedure codegen_donemodule;
  235. begin
  236. dispose(exprasmlist,done);
  237. dispose(codesegment,done);
  238. dispose(bsssegment,done);
  239. dispose(datasegment,done);
  240. dispose(debuglist,done);
  241. dispose(externals,done);
  242. dispose(internals,done);
  243. dispose(consts,done);
  244. dispose(rttilist,done);
  245. if assigned(importssection) then
  246. dispose(importssection,done);
  247. if assigned(exportssection) then
  248. dispose(exportssection,done);
  249. if assigned(resourcesection) then
  250. dispose(resourcesection,done);
  251. dispose(asmsymbollist,done);
  252. end;
  253. {*****************************************************************************
  254. TTempToDestroy
  255. *****************************************************************************}
  256. constructor ttemptodestroy.init(const a : treference;p : pdef);
  257. begin
  258. inherited init;
  259. address:=a;
  260. typ:=p;
  261. end;
  262. end.
  263. {
  264. $Log$
  265. Revision 1.29 1999-04-21 09:43:38 peter
  266. * storenumber works
  267. * fixed some typos in double_checksum
  268. + incompatible types type1 and type2 message (with storenumber)
  269. Revision 1.28 1999/03/24 23:17:00 peter
  270. * fixed bugs 212,222,225,227,229,231,233
  271. Revision 1.27 1999/02/25 21:02:37 peter
  272. * ag386bin updates
  273. + coff writer
  274. Revision 1.26 1999/02/22 02:15:21 peter
  275. * updates for ag386bin
  276. Revision 1.25 1999/01/21 22:10:45 peter
  277. * fixed array of const
  278. * generic platform independent high() support
  279. Revision 1.24 1998/12/29 18:48:18 jonas
  280. + optimize pascal code surrounding assembler blocks
  281. Revision 1.23 1998/11/27 14:50:38 peter
  282. + open strings, $P switch support
  283. Revision 1.22 1998/11/16 12:12:21 peter
  284. - generate_pascii which is obsolete
  285. Revision 1.21 1998/11/04 10:11:38 peter
  286. * ansistring fixes
  287. Revision 1.20 1998/10/29 15:42:48 florian
  288. + partial disposing of temp. ansistrings
  289. Revision 1.19 1998/10/26 22:58:18 florian
  290. * new introduded problem with classes fix, the parent class wasn't set
  291. correct, if the class was defined forward before
  292. Revision 1.18 1998/10/06 17:16:50 pierre
  293. * some memory leaks fixed (thanks to Peter for heaptrc !)
  294. Revision 1.17 1998/09/17 09:42:37 peter
  295. + pass_2 for cg386
  296. * Message() -> CGMessage() for pass_1/pass_2
  297. Revision 1.16 1998/09/07 18:46:04 peter
  298. * update smartlinking, uses getdatalabel
  299. * renamed ptree.value vars to value_str,value_real,value_set
  300. Revision 1.15 1998/09/01 09:02:51 peter
  301. * moved message() to hcodegen, so pass_2 also uses them
  302. Revision 1.14 1998/08/21 14:08:43 pierre
  303. + TEST_FUNCRET now default (old code removed)
  304. works also for m68k (at least compiles)
  305. Revision 1.13 1998/08/20 09:26:38 pierre
  306. + funcret setting in underproc testing
  307. compile with _dTEST_FUNCRET
  308. Revision 1.12 1998/08/10 14:50:01 peter
  309. + localswitches, moduleswitches, globalswitches splitting
  310. Revision 1.11 1998/07/28 21:52:51 florian
  311. + implementation of raise and try..finally
  312. + some misc. exception stuff
  313. Revision 1.10 1998/07/20 18:40:13 florian
  314. * handling of ansi string constants should now work
  315. Revision 1.9 1998/06/05 16:13:34 pierre
  316. * fix for real and string consts inside inlined procs
  317. Revision 1.8 1998/06/04 23:51:40 peter
  318. * m68k compiles
  319. + .def file creation moved to gendef.pas so it could also be used
  320. for win32
  321. Revision 1.7 1998/06/04 09:55:38 pierre
  322. * demangled name of procsym reworked to become independant of the mangling scheme
  323. Revision 1.6 1998/05/23 01:21:08 peter
  324. + aktasmmode, aktoptprocessor, aktoutputformat
  325. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  326. + $LIBNAME to set the library name where the unit will be put in
  327. * splitted cgi386 a bit (codeseg to large for bp7)
  328. * nasm, tasm works again. nasm moved to ag386nsm.pas
  329. Revision 1.5 1998/05/20 09:42:34 pierre
  330. + UseTokenInfo now default
  331. * unit in interface uses and implementation uses gives error now
  332. * only one error for unknown symbol (uses lastsymknown boolean)
  333. the problem came from the label code !
  334. + first inlined procedures and function work
  335. (warning there might be allowed cases were the result is still wrong !!)
  336. * UseBrower updated gives a global list of all position of all used symbols
  337. with switch -gb
  338. Revision 1.4 1998/05/07 00:17:01 peter
  339. * smartlinking for sets
  340. + consts labels are now concated/generated in hcodegen
  341. * moved some cpu code to cga and some none cpu depended code from cga
  342. to tree and hcodegen and cleanup of hcodegen
  343. * assembling .. output reduced for smartlinking ;)
  344. Revision 1.3 1998/05/06 08:38:40 pierre
  345. * better position info with UseTokenInfo
  346. UseTokenInfo greatly simplified
  347. + added check for changed tree after first time firstpass
  348. (if we could remove all the cases were it happen
  349. we could skip all firstpass if firstpasscount > 1)
  350. Only with ExtDebug
  351. Revision 1.2 1998/04/29 10:33:53 pierre
  352. + added some code for ansistring (not complete nor working yet)
  353. * corrected operator overloading
  354. * corrected nasm output
  355. + started inline procedures
  356. + added starstarn : use ** for exponentiation (^ gave problems)
  357. + started UseTokenInfo cond to get accurate positions
  358. }