cgbase.pas 13 KB

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