hcodegen.pas 15 KB

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