cgbase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. TOpCg = (OP_ADD,OP_AND,OP_DIV,OP_IDIV,OP_IMUL,OP_MUL,OP_NEG,OP_NOT,
  36. OP_OR,OP_SAR,OP_SHL,OP_SHR,OP_SUB,OP_XOR);
  37. TOpCmp = (OC_EQ,OC_GT,OC_LT,OC_GTE,OC_LTE,OC_NE,OC_BE,OC_B,
  38. OC_AE,OC_A);
  39. TCgSize = (OS_NO,OS_8,OS_16,OS_32,OS_64);
  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. selfpointer_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. { every register which must be saved by the entry code }
  67. { (and restored by the exit code) must be in that set }
  68. registerstosave : tregisterset;
  69. { some collected informations about the procedure }
  70. { see pi_xxxx above }
  71. flags : longint;
  72. { register used as frame pointer }
  73. framepointer : tregister;
  74. { true, if the procedure is exported by an unit }
  75. globalsymbol : boolean;
  76. { true, if the procedure should be exported (only OS/2) }
  77. exported : boolean;
  78. { code for the current procedure }
  79. aktproccode,aktentrycode,
  80. aktexitcode,aktlocaldata : paasmoutput;
  81. { local data is used for smartlink }
  82. end;
  83. { some kind of temp. types needs to be destructed }
  84. { for example ansistring, this is done using this }
  85. { list }
  86. ptemptodestroy = ^ttemptodestroy;
  87. ttemptodestroy = object(tlinkedlist_item)
  88. typ : pdef;
  89. address : treference;
  90. constructor init(const a : treference;p : pdef);
  91. end;
  92. const
  93. { defines the default address size for a processor }
  94. { and defines the natural int size for a processor }
  95. {$ifdef i386}
  96. OS_ADDR = OS_32;
  97. OS_INT = OS_32;
  98. {$endif i386}
  99. {$ifdef alpha}
  100. OS_ADDR = OS_64;
  101. OS_INT = OS_64;
  102. {$endif alpha}
  103. {$ifdef powerpc}
  104. OS_ADDR = OS_32;
  105. OS_INT = OS_32;
  106. {$endif powercc}
  107. var
  108. { info about the current sub routine }
  109. procinfo : tprocinfo;
  110. { labels for BREAK and CONTINUE }
  111. aktbreaklabel,aktcontinuelabel : pasmlabel;
  112. { label when the result is true or false }
  113. truelabel,falselabel : pasmlabel;
  114. { label to leave the sub routine }
  115. aktexitlabel : pasmlabel;
  116. { also an exit label, only used we need to clear only the stack }
  117. aktexit2label : pasmlabel;
  118. { only used in constructor for fail or if getmem fails }
  119. quickexitlabel : pasmlabel;
  120. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  121. simple_loadn : boolean;
  122. { tries to hold the amount of times which the current tree is processed }
  123. t_times : longint;
  124. { true, if an error while code generation occurs }
  125. codegenerror : boolean;
  126. { this is for open arrays and strings }
  127. { but be careful, this data is in the }
  128. { generated code destroyed quick, and also }
  129. { the next call of secondload destroys this }
  130. { data }
  131. { So be careful using the informations }
  132. { provided by this variables }
  133. highframepointer : tregister;
  134. highoffset : longint;
  135. make_const_global : boolean;
  136. temptoremove : plinkedlist;
  137. { message calls with codegenerror support }
  138. procedure cgmessage(const t : tmsgconst);
  139. procedure cgmessage1(const t : tmsgconst;const s : string);
  140. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  141. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  142. { initialize respectively terminates the code generator }
  143. { for a new module or procedure }
  144. procedure codegen_doneprocedure;
  145. procedure codegen_donemodule;
  146. procedure codegen_newmodule;
  147. procedure codegen_newprocedure;
  148. { counts the labels }
  149. function case_count_labels(root : pcaserecord) : longint;
  150. { searches the highest label }
  151. function case_get_max(root : pcaserecord) : longint;
  152. { searches the lowest label }
  153. function case_get_min(root : pcaserecord) : longint;
  154. { clears a location record }
  155. procedure clear_location(var loc : tlocation);
  156. { copies a location, takes care of the symbol }
  157. procedure set_location(var destloc,sourceloc : tlocation);
  158. { swaps two locations }
  159. procedure swap_location(var destloc,sourceloc : tlocation);
  160. implementation
  161. uses
  162. comphook;
  163. {*****************************************************************************
  164. override the message calls to set codegenerror
  165. *****************************************************************************}
  166. procedure cgmessage(const t : tmsgconst);
  167. var
  168. olderrorcount : longint;
  169. begin
  170. if not(codegenerror) then
  171. begin
  172. olderrorcount:=status.errorcount;
  173. verbose.Message(t);
  174. codegenerror:=olderrorcount<>status.errorcount;
  175. end;
  176. end;
  177. procedure cgmessage1(const t : tmsgconst;const s : string);
  178. var
  179. olderrorcount : longint;
  180. begin
  181. if not(codegenerror) then
  182. begin
  183. olderrorcount:=status.errorcount;
  184. verbose.Message1(t,s);
  185. codegenerror:=olderrorcount<>status.errorcount;
  186. end;
  187. end;
  188. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  189. var
  190. olderrorcount : longint;
  191. begin
  192. if not(codegenerror) then
  193. begin
  194. olderrorcount:=status.errorcount;
  195. verbose.Message2(t,s1,s2);
  196. codegenerror:=olderrorcount<>status.errorcount;
  197. end;
  198. end;
  199. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  200. var
  201. olderrorcount : longint;
  202. begin
  203. if not(codegenerror) then
  204. begin
  205. olderrorcount:=status.errorcount;
  206. verbose.Message3(t,s1,s2,s3);
  207. codegenerror:=olderrorcount<>status.errorcount;
  208. end;
  209. end;
  210. {*****************************************************************************
  211. initialize/terminate the codegen for procedure and modules
  212. *****************************************************************************}
  213. procedure codegen_newprocedure;
  214. begin
  215. aktbreaklabel:=nil;
  216. aktcontinuelabel:=nil;
  217. { aktexitlabel:=0; is store in oldaktexitlabel
  218. so it must not be reset to zero before this storage !}
  219. { the type of this lists isn't important }
  220. { because the code of this lists is }
  221. { copied to the code segment }
  222. procinfo.aktentrycode:=new(paasmoutput,init);
  223. procinfo.aktexitcode:=new(paasmoutput,init);
  224. procinfo.aktproccode:=new(paasmoutput,init);
  225. procinfo.aktlocaldata:=new(paasmoutput,init);
  226. end;
  227. procedure codegen_doneprocedure;
  228. begin
  229. dispose(procinfo.aktentrycode,done);
  230. dispose(procinfo.aktexitcode,done);
  231. dispose(procinfo.aktproccode,done);
  232. dispose(procinfo.aktlocaldata,done);
  233. end;
  234. procedure codegen_newmodule;
  235. begin
  236. exprasmlist:=new(paasmoutput,init);
  237. datasegment:=new(paasmoutput,init);
  238. codesegment:=new(paasmoutput,init);
  239. bsssegment:=new(paasmoutput,init);
  240. debuglist:=new(paasmoutput,init);
  241. consts:=new(paasmoutput,init);
  242. rttilist:=new(paasmoutput,init);
  243. importssection:=nil;
  244. exportssection:=nil;
  245. resourcesection:=nil;
  246. asmsymbollist:=new(pasmsymbollist,init);
  247. asmsymbollist^.usehash;
  248. end;
  249. procedure codegen_donemodule;
  250. begin
  251. dispose(exprasmlist,done);
  252. dispose(codesegment,done);
  253. dispose(bsssegment,done);
  254. dispose(datasegment,done);
  255. dispose(debuglist,done);
  256. dispose(consts,done);
  257. dispose(rttilist,done);
  258. if assigned(importssection) then
  259. dispose(importssection,done);
  260. if assigned(exportssection) then
  261. dispose(exportssection,done);
  262. if assigned(resourcesection) then
  263. dispose(resourcesection,done);
  264. if assigned(resourcestringlist) then
  265. dispose(resourcestringlist,done);
  266. dispose(asmsymbollist,done);
  267. end;
  268. {*****************************************************************************
  269. Case Helpers
  270. *****************************************************************************}
  271. function case_count_labels(root : pcaserecord) : longint;
  272. var
  273. _l : longint;
  274. procedure count(p : pcaserecord);
  275. begin
  276. inc(_l);
  277. if assigned(p^.less) then
  278. count(p^.less);
  279. if assigned(p^.greater) then
  280. count(p^.greater);
  281. end;
  282. begin
  283. _l:=0;
  284. count(root);
  285. case_count_labels:=_l;
  286. end;
  287. function case_get_max(root : pcaserecord) : longint;
  288. var
  289. hp : pcaserecord;
  290. begin
  291. hp:=root;
  292. while assigned(hp^.greater) do
  293. hp:=hp^.greater;
  294. case_get_max:=hp^._high;
  295. end;
  296. function case_get_min(root : pcaserecord) : longint;
  297. var
  298. hp : pcaserecord;
  299. begin
  300. hp:=root;
  301. while assigned(hp^.less) do
  302. hp:=hp^.less;
  303. case_get_min:=hp^._low;
  304. end;
  305. {*****************************************************************************
  306. TTempToDestroy
  307. *****************************************************************************}
  308. constructor ttemptodestroy.init(const a : treference;p : pdef);
  309. begin
  310. inherited init;
  311. address:=a;
  312. typ:=p;
  313. end;
  314. {*****************************************************************************
  315. some helper routines to handle locations
  316. *****************************************************************************}
  317. procedure clear_location(var loc : tlocation);
  318. begin
  319. if ((loc.loc=LOC_MEM) or (loc.loc=LOC_REFERENCE)) and
  320. assigned(loc.reference.symbol) then
  321. dispose(loc.reference.symbol,done);
  322. loc.loc:=LOC_INVALID;
  323. end;
  324. procedure set_location(var destloc,sourceloc : tlocation);
  325. begin
  326. { this is needed if you want to be able to delete }
  327. { the string with the nodes }
  328. if assigned(destloc.reference.symbol) then
  329. dispose(destloc.reference.symbol,done);
  330. destloc:= sourceloc;
  331. if sourceloc.loc in [LOC_MEM,LOC_REFERENCE] then
  332. begin
  333. if assigned(sourceloc.reference.symbol) then
  334. destloc.reference.symbol:=
  335. sourceloc.reference.symbol;
  336. end
  337. else
  338. destloc.reference.symbol:=nil;
  339. end;
  340. procedure swap_location(var destloc,sourceloc : tlocation);
  341. var
  342. swapl : tlocation;
  343. begin
  344. swapl:=destloc;
  345. destloc:=sourceloc;
  346. sourceloc:=swapl;
  347. end;
  348. end.
  349. {
  350. $Log$
  351. Revision 1.9 1999-09-10 18:48:11 florian
  352. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  353. * most things for stored properties fixed
  354. Revision 1.8 1999/08/06 13:26:49 florian
  355. * more changes ...
  356. Revision 1.7 1999/08/05 14:58:10 florian
  357. * some fixes for the floating point registers
  358. * more things for the new code generator
  359. Revision 1.6 1999/08/04 00:23:51 florian
  360. * renamed i386asm and i386base to cpuasm and cpubase
  361. Revision 1.5 1999/08/01 18:22:32 florian
  362. * made it again compilable
  363. Revision 1.4 1999/01/23 23:29:45 florian
  364. * first running version of the new code generator
  365. * when compiling exceptions under Linux fixed
  366. Revision 1.3 1999/01/06 22:58:48 florian
  367. + some stuff for the new code generator
  368. Revision 1.2 1998/12/26 15:20:28 florian
  369. + more changes for the new version
  370. Revision 1.1 1998/12/15 22:18:55 florian
  371. * some code added
  372. }