hcodegen.pas 15 KB

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