cgbase.pas 13 KB

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