cgbase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  144. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  145. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  146. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  147. { initialize respectively terminates the code generator }
  148. { for a new module or procedure }
  149. procedure codegen_doneprocedure;
  150. procedure codegen_donemodule;
  151. procedure codegen_newmodule;
  152. procedure codegen_newprocedure;
  153. { counts the labels }
  154. function case_count_labels(root : pcaserecord) : longint;
  155. { searches the highest label }
  156. function case_get_max(root : pcaserecord) : longint;
  157. { searches the lowest label }
  158. function case_get_min(root : pcaserecord) : longint;
  159. { clears a location record }
  160. procedure clear_location(var loc : tlocation);
  161. { copies a location, takes care of the symbol }
  162. procedure set_location(var destloc,sourceloc : tlocation);
  163. { swaps two locations }
  164. procedure swap_location(var destloc,sourceloc : tlocation);
  165. implementation
  166. uses
  167. comphook;
  168. {*****************************************************************************
  169. override the message calls to set codegenerror
  170. *****************************************************************************}
  171. procedure cgmessage(const t : tmsgconst);
  172. var
  173. olderrorcount : longint;
  174. begin
  175. if not(codegenerror) then
  176. begin
  177. olderrorcount:=status.errorcount;
  178. verbose.Message(t);
  179. codegenerror:=olderrorcount<>status.errorcount;
  180. end;
  181. end;
  182. procedure cgmessage1(const t : tmsgconst;const s : string);
  183. var
  184. olderrorcount : longint;
  185. begin
  186. if not(codegenerror) then
  187. begin
  188. olderrorcount:=status.errorcount;
  189. verbose.Message1(t,s);
  190. codegenerror:=olderrorcount<>status.errorcount;
  191. end;
  192. end;
  193. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  194. var
  195. olderrorcount : longint;
  196. begin
  197. if not(codegenerror) then
  198. begin
  199. olderrorcount:=status.errorcount;
  200. verbose.Message2(t,s1,s2);
  201. codegenerror:=olderrorcount<>status.errorcount;
  202. end;
  203. end;
  204. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  205. var
  206. olderrorcount : longint;
  207. begin
  208. if not(codegenerror) then
  209. begin
  210. olderrorcount:=status.errorcount;
  211. verbose.Message3(t,s1,s2,s3);
  212. codegenerror:=olderrorcount<>status.errorcount;
  213. end;
  214. end;
  215. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  216. var
  217. olderrorcount : longint;
  218. begin
  219. if not(codegenerror) then
  220. begin
  221. olderrorcount:=Errorcount;
  222. verbose.MessagePos(pos,t);
  223. codegenerror:=olderrorcount<>Errorcount;
  224. end;
  225. end;
  226. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  227. var
  228. olderrorcount : longint;
  229. begin
  230. if not(codegenerror) then
  231. begin
  232. olderrorcount:=Errorcount;
  233. verbose.MessagePos1(pos,t,s1);
  234. codegenerror:=olderrorcount<>Errorcount;
  235. end;
  236. end;
  237. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  238. var
  239. olderrorcount : longint;
  240. begin
  241. if not(codegenerror) then
  242. begin
  243. olderrorcount:=Errorcount;
  244. verbose.MessagePos2(pos,t,s1,s2);
  245. codegenerror:=olderrorcount<>Errorcount;
  246. end;
  247. end;
  248. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  249. var
  250. olderrorcount : longint;
  251. begin
  252. if not(codegenerror) then
  253. begin
  254. olderrorcount:=Errorcount;
  255. verbose.MessagePos3(pos,t,s1,s2,s3);
  256. codegenerror:=olderrorcount<>Errorcount;
  257. end;
  258. end;
  259. {*****************************************************************************
  260. initialize/terminate the codegen for procedure and modules
  261. *****************************************************************************}
  262. procedure codegen_newprocedure;
  263. begin
  264. aktbreaklabel:=nil;
  265. aktcontinuelabel:=nil;
  266. new(procinfo);
  267. { aktexitlabel:=0; is store in oldaktexitlabel
  268. so it must not be reset to zero before this storage !}
  269. { the type of this lists isn't important }
  270. { because the code of this lists is }
  271. { copied to the code segment }
  272. procinfo^.aktentrycode:=new(paasmoutput,init);
  273. procinfo^.aktexitcode:=new(paasmoutput,init);
  274. procinfo^.aktproccode:=new(paasmoutput,init);
  275. procinfo^.aktlocaldata:=new(paasmoutput,init);
  276. end;
  277. procedure codegen_doneprocedure;
  278. begin
  279. dispose(procinfo^.aktentrycode,done);
  280. dispose(procinfo^.aktexitcode,done);
  281. dispose(procinfo^.aktproccode,done);
  282. dispose(procinfo^.aktlocaldata,done);
  283. dispose(procinfo);
  284. end;
  285. procedure codegen_newmodule;
  286. begin
  287. exprasmlist:=new(paasmoutput,init);
  288. datasegment:=new(paasmoutput,init);
  289. codesegment:=new(paasmoutput,init);
  290. bsssegment:=new(paasmoutput,init);
  291. debuglist:=new(paasmoutput,init);
  292. consts:=new(paasmoutput,init);
  293. rttilist:=new(paasmoutput,init);
  294. importssection:=nil;
  295. exportssection:=nil;
  296. resourcesection:=nil;
  297. asmsymbollist:=new(pasmsymbollist,init);
  298. asmsymbollist^.usehash;
  299. end;
  300. procedure codegen_donemodule;
  301. begin
  302. dispose(exprasmlist,done);
  303. dispose(codesegment,done);
  304. dispose(bsssegment,done);
  305. dispose(datasegment,done);
  306. dispose(debuglist,done);
  307. dispose(consts,done);
  308. dispose(rttilist,done);
  309. if assigned(importssection) then
  310. dispose(importssection,done);
  311. if assigned(exportssection) then
  312. dispose(exportssection,done);
  313. if assigned(resourcesection) then
  314. dispose(resourcesection,done);
  315. if assigned(resourcestringlist) then
  316. dispose(resourcestringlist,done);
  317. dispose(asmsymbollist,done);
  318. end;
  319. {*****************************************************************************
  320. Case Helpers
  321. *****************************************************************************}
  322. function case_count_labels(root : pcaserecord) : longint;
  323. var
  324. _l : longint;
  325. procedure count(p : pcaserecord);
  326. begin
  327. inc(_l);
  328. if assigned(p^.less) then
  329. count(p^.less);
  330. if assigned(p^.greater) then
  331. count(p^.greater);
  332. end;
  333. begin
  334. _l:=0;
  335. count(root);
  336. case_count_labels:=_l;
  337. end;
  338. function case_get_max(root : pcaserecord) : longint;
  339. var
  340. hp : pcaserecord;
  341. begin
  342. hp:=root;
  343. while assigned(hp^.greater) do
  344. hp:=hp^.greater;
  345. case_get_max:=hp^._high;
  346. end;
  347. function case_get_min(root : pcaserecord) : longint;
  348. var
  349. hp : pcaserecord;
  350. begin
  351. hp:=root;
  352. while assigned(hp^.less) do
  353. hp:=hp^.less;
  354. case_get_min:=hp^._low;
  355. end;
  356. {*****************************************************************************
  357. TTempToDestroy
  358. *****************************************************************************}
  359. constructor ttemptodestroy.init(const a : treference;p : pdef);
  360. begin
  361. inherited init;
  362. address:=a;
  363. typ:=p;
  364. end;
  365. {*****************************************************************************
  366. some helper routines to handle locations
  367. *****************************************************************************}
  368. procedure clear_location(var loc : tlocation);
  369. begin
  370. if ((loc.loc=LOC_MEM) or (loc.loc=LOC_REFERENCE)) and
  371. assigned(loc.reference.symbol) then
  372. dispose(loc.reference.symbol,done);
  373. loc.loc:=LOC_INVALID;
  374. end;
  375. procedure set_location(var destloc,sourceloc : tlocation);
  376. begin
  377. { this is needed if you want to be able to delete }
  378. { the string with the nodes }
  379. if assigned(destloc.reference.symbol) then
  380. dispose(destloc.reference.symbol,done);
  381. destloc:= sourceloc;
  382. if sourceloc.loc in [LOC_MEM,LOC_REFERENCE] then
  383. begin
  384. if assigned(sourceloc.reference.symbol) then
  385. destloc.reference.symbol:=
  386. sourceloc.reference.symbol;
  387. end
  388. else
  389. destloc.reference.symbol:=nil;
  390. end;
  391. procedure swap_location(var destloc,sourceloc : tlocation);
  392. var
  393. swapl : tlocation;
  394. begin
  395. swapl:=destloc;
  396. destloc:=sourceloc;
  397. sourceloc:=swapl;
  398. end;
  399. end.
  400. {
  401. $Log$
  402. Revision 1.12 1999-11-05 13:15:00 florian
  403. * some fixes to get the new cg compiling again
  404. Revision 1.11 1999/10/14 14:57:54 florian
  405. - removed the hcodegen use in the new cg, use cgbase instead
  406. Revision 1.10 1999/10/12 21:20:46 florian
  407. * new codegenerator compiles again
  408. Revision 1.9 1999/09/10 18:48:11 florian
  409. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  410. * most things for stored properties fixed
  411. Revision 1.8 1999/08/06 13:26:49 florian
  412. * more changes ...
  413. Revision 1.7 1999/08/05 14:58:10 florian
  414. * some fixes for the floating point registers
  415. * more things for the new code generator
  416. Revision 1.6 1999/08/04 00:23:51 florian
  417. * renamed i386asm and i386base to cpuasm and cpubase
  418. Revision 1.5 1999/08/01 18:22:32 florian
  419. * made it again compilable
  420. Revision 1.4 1999/01/23 23:29:45 florian
  421. * first running version of the new code generator
  422. * when compiling exceptions under Linux fixed
  423. Revision 1.3 1999/01/06 22:58:48 florian
  424. + some stuff for the new code generator
  425. Revision 1.2 1998/12/26 15:20:28 florian
  426. + more changes for the new version
  427. Revision 1.1 1998/12/15 22:18:55 florian
  428. * some code added
  429. }