cgbase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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,symconst,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 = object
  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. returntype : ttype;
  48. { symbol of the function, and the sym for result variable }
  49. resultfuncretsym,
  50. funcretsym : pfuncretsym;
  51. funcret_state : tvarstate;
  52. { the definition of the proc itself }
  53. def : pprocdef;
  54. sym : pprocsym;
  55. { frame pointer offset }
  56. framepointer_offset : longint;
  57. { self pointer offset }
  58. selfpointer_offset : longint;
  59. { result value offset }
  60. return_offset : longint;
  61. { firsttemp position }
  62. firsttemp_offset : longint;
  63. { parameter offset }
  64. call_offset : longint;
  65. { every register which must be saved by the entry code }
  66. { (and restored by the exit code) must be in that set }
  67. registerstosave : tregisterset;
  68. { some collected informations about the procedure }
  69. { see pi_xxxx above }
  70. flags : longint;
  71. { register used as frame pointer }
  72. framepointer : tregister;
  73. { true, if the procedure is exported by an unit }
  74. globalsymbol : boolean;
  75. { true, if the procedure should be exported (only OS/2) }
  76. exported : boolean;
  77. { code for the current procedure }
  78. aktproccode,aktentrycode,
  79. aktexitcode,aktlocaldata : paasmoutput;
  80. { local data is used for smartlink }
  81. constructor init;
  82. destructor done;
  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. TProcInfo
  261. ****************************************************************************}
  262. constructor tprocinfo.init;
  263. begin
  264. parent:=nil;
  265. _class:=nil;
  266. returntype.reset;
  267. resultfuncretsym:=nil;
  268. funcretsym:=nil;
  269. funcret_state:=vs_none;
  270. def:=nil;
  271. sym:=nil;
  272. framepointer_offset:=0;
  273. selfpointer_offset:=0;
  274. return_offset:=0;
  275. firsttemp_offset:=0;
  276. call_offset:=0;
  277. registerstosave:=[];
  278. flags:=0;
  279. framepointer:=R_NO;
  280. globalsymbol:=false;
  281. exported:=false;
  282. aktentrycode:=new(paasmoutput,init);
  283. aktexitcode:=new(paasmoutput,init);
  284. aktproccode:=new(paasmoutput,init);
  285. aktlocaldata:=new(paasmoutput,init);
  286. end;
  287. destructor tprocinfo.done;
  288. begin
  289. dispose(aktentrycode,done);
  290. dispose(aktexitcode,done);
  291. dispose(aktproccode,done);
  292. dispose(aktlocaldata,done);
  293. end;
  294. {*****************************************************************************
  295. initialize/terminate the codegen for procedure and modules
  296. *****************************************************************************}
  297. procedure codegen_newprocedure;
  298. begin
  299. aktbreaklabel:=nil;
  300. aktcontinuelabel:=nil;
  301. new(procinfo,init);
  302. { aktexitlabel:=0; is store in oldaktexitlabel
  303. so it must not be reset to zero before this storage !}
  304. end;
  305. procedure codegen_doneprocedure;
  306. begin
  307. dispose(procinfo,done);
  308. procinfo:=nil;
  309. end;
  310. procedure codegen_newmodule;
  311. begin
  312. exprasmlist:=new(paasmoutput,init);
  313. datasegment:=new(paasmoutput,init);
  314. codesegment:=new(paasmoutput,init);
  315. bsssegment:=new(paasmoutput,init);
  316. debuglist:=new(paasmoutput,init);
  317. consts:=new(paasmoutput,init);
  318. rttilist:=new(paasmoutput,init);
  319. importssection:=nil;
  320. exportssection:=nil;
  321. resourcesection:=nil;
  322. asmsymbollist:=new(pasmsymbollist,init);
  323. asmsymbollist^.usehash;
  324. end;
  325. procedure codegen_donemodule;
  326. begin
  327. dispose(exprasmlist,done);
  328. dispose(codesegment,done);
  329. dispose(bsssegment,done);
  330. dispose(datasegment,done);
  331. dispose(debuglist,done);
  332. dispose(consts,done);
  333. dispose(rttilist,done);
  334. if assigned(importssection) then
  335. dispose(importssection,done);
  336. if assigned(exportssection) then
  337. dispose(exportssection,done);
  338. if assigned(resourcesection) then
  339. dispose(resourcesection,done);
  340. if assigned(resourcestringlist) then
  341. dispose(resourcestringlist,done);
  342. dispose(asmsymbollist,done);
  343. end;
  344. {*****************************************************************************
  345. Case Helpers
  346. *****************************************************************************}
  347. function case_count_labels(root : pcaserecord) : longint;
  348. var
  349. _l : longint;
  350. procedure count(p : pcaserecord);
  351. begin
  352. inc(_l);
  353. if assigned(p^.less) then
  354. count(p^.less);
  355. if assigned(p^.greater) then
  356. count(p^.greater);
  357. end;
  358. begin
  359. _l:=0;
  360. count(root);
  361. case_count_labels:=_l;
  362. end;
  363. function case_get_max(root : pcaserecord) : longint;
  364. var
  365. hp : pcaserecord;
  366. begin
  367. hp:=root;
  368. while assigned(hp^.greater) do
  369. hp:=hp^.greater;
  370. case_get_max:=hp^._high;
  371. end;
  372. function case_get_min(root : pcaserecord) : longint;
  373. var
  374. hp : pcaserecord;
  375. begin
  376. hp:=root;
  377. while assigned(hp^.less) do
  378. hp:=hp^.less;
  379. case_get_min:=hp^._low;
  380. end;
  381. {*****************************************************************************
  382. TTempToDestroy
  383. *****************************************************************************}
  384. constructor ttemptodestroy.init(const a : treference;p : pdef);
  385. begin
  386. inherited init;
  387. address:=a;
  388. typ:=p;
  389. end;
  390. {*****************************************************************************
  391. some helper routines to handle locations
  392. *****************************************************************************}
  393. procedure clear_location(var loc : tlocation);
  394. begin
  395. if ((loc.loc=LOC_MEM) or (loc.loc=LOC_REFERENCE)) and
  396. assigned(loc.reference.symbol) then
  397. dispose(loc.reference.symbol,done);
  398. loc.loc:=LOC_INVALID;
  399. end;
  400. procedure set_location(var destloc,sourceloc : tlocation);
  401. begin
  402. { this is needed if you want to be able to delete }
  403. { the string with the nodes }
  404. if assigned(destloc.reference.symbol) then
  405. dispose(destloc.reference.symbol,done);
  406. destloc:= sourceloc;
  407. if sourceloc.loc in [LOC_MEM,LOC_REFERENCE] then
  408. begin
  409. if assigned(sourceloc.reference.symbol) then
  410. destloc.reference.symbol:=
  411. sourceloc.reference.symbol;
  412. end
  413. else
  414. destloc.reference.symbol:=nil;
  415. end;
  416. procedure swap_location(var destloc,sourceloc : tlocation);
  417. var
  418. swapl : tlocation;
  419. begin
  420. swapl:=destloc;
  421. destloc:=sourceloc;
  422. sourceloc:=swapl;
  423. end;
  424. end.
  425. {
  426. $Log$
  427. Revision 1.13 1999-12-01 12:42:33 peter
  428. * fixed bug 698
  429. * removed some notes about unused vars
  430. Revision 1.12 1999/11/05 13:15:00 florian
  431. * some fixes to get the new cg compiling again
  432. Revision 1.11 1999/10/14 14:57:54 florian
  433. - removed the hcodegen use in the new cg, use cgbase instead
  434. Revision 1.10 1999/10/12 21:20:46 florian
  435. * new codegenerator compiles again
  436. Revision 1.9 1999/09/10 18:48:11 florian
  437. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  438. * most things for stored properties fixed
  439. Revision 1.8 1999/08/06 13:26:49 florian
  440. * more changes ...
  441. Revision 1.7 1999/08/05 14:58:10 florian
  442. * some fixes for the floating point registers
  443. * more things for the new code generator
  444. Revision 1.6 1999/08/04 00:23:51 florian
  445. * renamed i386asm and i386base to cpuasm and cpubase
  446. Revision 1.5 1999/08/01 18:22:32 florian
  447. * made it again compilable
  448. Revision 1.4 1999/01/23 23:29:45 florian
  449. * first running version of the new code generator
  450. * when compiling exceptions under Linux fixed
  451. Revision 1.3 1999/01/06 22:58:48 florian
  452. + some stuff for the new code generator
  453. Revision 1.2 1998/12/26 15:20:28 florian
  454. + more changes for the new version
  455. Revision 1.1 1998/12/15 22:18:55 florian
  456. * some code added
  457. }