hcodegen.pas 15 KB

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