cgbase.pas 15 KB

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