hcodegen.pas 15 KB

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