cgbase.pas 14 KB

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