cgbase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This units implements some code generator helper routines
  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 cgbase;
  19. interface
  20. uses
  21. globtype,cobjects,aasm,symtable,verbose,tree
  22. {$I cpuunit.inc}
  23. ;
  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. type
  33. pprocinfo = ^tprocinfo;
  34. tprocinfo = record
  35. { pointer to parent in nested procedures }
  36. parent : pprocinfo;
  37. { current class, if we are in a method }
  38. _class : pobjectdef;
  39. { return type }
  40. retdef : pdef;
  41. { return type }
  42. sym : pprocsym;
  43. { symbol of the function }
  44. funcretsym : pfuncretsym;
  45. { the definition of the proc itself }
  46. { why was this a pdef only ?? PM }
  47. def : pprocdef;
  48. { frame pointer offset }
  49. framepointer_offset : longint;
  50. { self pointer offset }
  51. selfpointer_offset : longint;
  52. { result value offset }
  53. retoffset : longint;
  54. { firsttemp position }
  55. firsttemp : longint;
  56. funcret_is_valid : boolean;
  57. { parameter offset }
  58. call_offset : longint;
  59. { some collected informations about the procedure }
  60. { see pi_xxxx above }
  61. flags : longint;
  62. { register used as frame pointer }
  63. framepointer : tregister;
  64. { true, if the procedure is exported by an unit }
  65. globalsymbol : boolean;
  66. { true, if the procedure should be exported (only OS/2) }
  67. exported : boolean;
  68. { code for the current procedure }
  69. aktproccode,aktentrycode,
  70. aktexitcode,aktlocaldata : paasmoutput;
  71. { local data is used for smartlink }
  72. end;
  73. { some kind of temp. types needs to be destructed }
  74. { for example ansistring, this is done using this }
  75. { list }
  76. ptemptodestroy = ^ttemptodestroy;
  77. ttemptodestroy = object(tlinkedlist_item)
  78. typ : pdef;
  79. address : treference;
  80. constructor init(const a : treference;p : pdef);
  81. end;
  82. var
  83. { info about the current sub routine }
  84. procinfo : tprocinfo;
  85. { labels for BREAK and CONTINUE }
  86. aktbreaklabel,aktcontinuelabel : plabel;
  87. { label when the result is true or false }
  88. truelabel,falselabel : plabel;
  89. { label to leave the sub routine }
  90. aktexitlabel : plabel;
  91. { also an exit label, only used we need to clear only the stack }
  92. aktexit2label : plabel;
  93. { only used in constructor for fail or if getmem fails }
  94. quickexitlabel : plabel;
  95. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  96. simple_loadn : boolean;
  97. { tries to hold the amount of times which the current tree is processed }
  98. t_times : longint;
  99. { true, if an error while code generation occurs }
  100. codegenerror : boolean;
  101. { this is for open arrays and strings }
  102. { but be careful, this data is in the }
  103. { generated code destroyed quick, and also }
  104. { the next call of secondload destroys this }
  105. { data }
  106. { So be careful using the informations }
  107. { provided by this variables }
  108. highframepointer : tregister;
  109. highoffset : longint;
  110. make_const_global : boolean;
  111. temptoremove : plinkedlist;
  112. { message calls with codegenerror support }
  113. procedure cgmessage(const t : tmsgconst);
  114. procedure cgmessage1(const t : tmsgconst;const s : string);
  115. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  116. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  117. { initialize respectively terminates the code generator }
  118. { for a new module or procedure }
  119. procedure codegen_doneprocedure;
  120. procedure codegen_donemodule;
  121. procedure codegen_newmodule;
  122. procedure codegen_newprocedure;
  123. { counts the labels }
  124. function case_count_labels(root : pcaserecord) : longint;
  125. { searches the highest label }
  126. function case_get_max(root : pcaserecord) : longint;
  127. { searches the lowest label }
  128. function case_get_min(root : pcaserecord) : longint;
  129. { clears a location record }
  130. procedure clear_location(var loc : tlocation);
  131. { copies a location, takes care of the symbol }
  132. procedure set_location(var destloc,sourceloc : tlocation);
  133. { swaps two locations }
  134. procedure swap_location(var destloc,sourceloc : tlocation);
  135. implementation
  136. uses
  137. comphook;
  138. {*****************************************************************************
  139. override the message calls to set codegenerror
  140. *****************************************************************************}
  141. procedure cgmessage(const t : tmsgconst);
  142. var
  143. olderrorcount : longint;
  144. begin
  145. if not(codegenerror) then
  146. begin
  147. olderrorcount:=status.errorcount;
  148. verbose.Message(t);
  149. codegenerror:=olderrorcount<>status.errorcount;
  150. end;
  151. end;
  152. procedure cgmessage1(const t : tmsgconst;const s : string);
  153. var
  154. olderrorcount : longint;
  155. begin
  156. if not(codegenerror) then
  157. begin
  158. olderrorcount:=status.errorcount;
  159. verbose.Message1(t,s);
  160. codegenerror:=olderrorcount<>status.errorcount;
  161. end;
  162. end;
  163. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  164. var
  165. olderrorcount : longint;
  166. begin
  167. if not(codegenerror) then
  168. begin
  169. olderrorcount:=status.errorcount;
  170. verbose.Message2(t,s1,s2);
  171. codegenerror:=olderrorcount<>status.errorcount;
  172. end;
  173. end;
  174. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  175. var
  176. olderrorcount : longint;
  177. begin
  178. if not(codegenerror) then
  179. begin
  180. olderrorcount:=status.errorcount;
  181. verbose.Message3(t,s1,s2,s3);
  182. codegenerror:=olderrorcount<>status.errorcount;
  183. end;
  184. end;
  185. {*****************************************************************************
  186. initialize/terminate the codegen for procedure and modules
  187. *****************************************************************************}
  188. procedure codegen_newprocedure;
  189. begin
  190. aktbreaklabel:=nil;
  191. aktcontinuelabel:=nil;
  192. { aktexitlabel:=0; is store in oldaktexitlabel
  193. so it must not be reset to zero before this storage !}
  194. { the type of this lists isn't important }
  195. { because the code of this lists is }
  196. { copied to the code segment }
  197. procinfo.aktentrycode:=new(paasmoutput,init);
  198. procinfo.aktexitcode:=new(paasmoutput,init);
  199. procinfo.aktproccode:=new(paasmoutput,init);
  200. procinfo.aktlocaldata:=new(paasmoutput,init);
  201. end;
  202. procedure codegen_doneprocedure;
  203. begin
  204. dispose(procinfo.aktentrycode,done);
  205. dispose(procinfo.aktexitcode,done);
  206. dispose(procinfo.aktproccode,done);
  207. dispose(procinfo.aktlocaldata,done);
  208. end;
  209. procedure codegen_newmodule;
  210. begin
  211. exprasmlist:=new(paasmoutput,init);
  212. datasegment:=new(paasmoutput,init);
  213. codesegment:=new(paasmoutput,init);
  214. bsssegment:=new(paasmoutput,init);
  215. debuglist:=new(paasmoutput,init);
  216. externals:=new(paasmoutput,init);
  217. internals:=new(paasmoutput,init);
  218. consts:=new(paasmoutput,init);
  219. rttilist:=new(paasmoutput,init);
  220. importssection:=nil;
  221. exportssection:=nil;
  222. resourcesection:=nil;
  223. end;
  224. procedure codegen_donemodule;
  225. begin
  226. dispose(exprasmlist,done);
  227. dispose(codesegment,done);
  228. dispose(bsssegment,done);
  229. dispose(datasegment,done);
  230. dispose(debuglist,done);
  231. dispose(externals,done);
  232. dispose(internals,done);
  233. dispose(consts,done);
  234. dispose(rttilist,done);
  235. if assigned(importssection) then
  236. dispose(importssection,done);
  237. if assigned(exportssection) then
  238. dispose(exportssection,done);
  239. if assigned(resourcesection) then
  240. dispose(resourcesection,done);
  241. end;
  242. {*****************************************************************************
  243. Case Helpers
  244. *****************************************************************************}
  245. function case_count_labels(root : pcaserecord) : longint;
  246. var
  247. _l : longint;
  248. procedure count(p : pcaserecord);
  249. begin
  250. inc(_l);
  251. if assigned(p^.less) then
  252. count(p^.less);
  253. if assigned(p^.greater) then
  254. count(p^.greater);
  255. end;
  256. begin
  257. _l:=0;
  258. count(root);
  259. case_count_labels:=_l;
  260. end;
  261. function case_get_max(root : pcaserecord) : longint;
  262. var
  263. hp : pcaserecord;
  264. begin
  265. hp:=root;
  266. while assigned(hp^.greater) do
  267. hp:=hp^.greater;
  268. case_get_max:=hp^._high;
  269. end;
  270. function case_get_min(root : pcaserecord) : longint;
  271. var
  272. hp : pcaserecord;
  273. begin
  274. hp:=root;
  275. while assigned(hp^.less) do
  276. hp:=hp^.less;
  277. case_get_min:=hp^._low;
  278. end;
  279. {*****************************************************************************
  280. TTempToDestroy
  281. *****************************************************************************}
  282. constructor ttemptodestroy.init(const a : treference;p : pdef);
  283. begin
  284. inherited init;
  285. address:=a;
  286. typ:=p;
  287. end;
  288. {*****************************************************************************
  289. some helper routines to handle locations
  290. *****************************************************************************}
  291. procedure clear_location(var loc : tlocation);
  292. begin
  293. if ((loc.loc=LOC_MEM) or (loc.loc=LOC_REFERENCE)) and
  294. assigned(loc.reference.symbol) then
  295. stringdispose(loc.reference.symbol);
  296. loc.loc:=LOC_INVALID;
  297. end;
  298. procedure set_location(var destloc,sourceloc : tlocation);
  299. begin
  300. { this is needed if you want to be able to delete }
  301. { the string with the nodes }
  302. if assigned(destloc.reference.symbol) then
  303. stringdispose(destloc.reference.symbol);
  304. destloc:= sourceloc;
  305. if sourceloc.loc in [LOC_MEM,LOC_REFERENCE] then
  306. begin
  307. if assigned(sourceloc.reference.symbol) then
  308. destloc.reference.symbol:=
  309. stringdup(sourceloc.reference.symbol^);
  310. end
  311. else
  312. destloc.reference.symbol:=nil;
  313. end;
  314. procedure swap_location(var destloc,sourceloc : tlocation);
  315. var
  316. swapl : tlocation;
  317. begin
  318. swapl:=destloc;
  319. destloc:=sourceloc;
  320. sourceloc:=swapl;
  321. end;
  322. end.
  323. {
  324. $Log$
  325. Revision 1.4 1999-01-23 23:29:45 florian
  326. * first running version of the new code generator
  327. * when compiling exceptions under Linux fixed
  328. Revision 1.3 1999/01/06 22:58:48 florian
  329. + some stuff for the new code generator
  330. Revision 1.2 1998/12/26 15:20:28 florian
  331. + more changes for the new version
  332. Revision 1.1 1998/12/15 22:18:55 florian
  333. * some code added
  334. }