cgbase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,
  22. cpuasm,cpubase
  23. {$IFDEF NEWST}
  24. ,defs,symbols
  25. {$ENDIF NEWST};
  26. const
  27. pi_uses_asm = $1; { set, if the procedure uses asm }
  28. pi_is_global = $2; { set, if the procedure is exported by an unit }
  29. pi_do_call = $4; { set, if the procedure does a call }
  30. pi_operator = $8; { set, if the procedure is an operator }
  31. pi_C_import = $10; { set, if the procedure is an external C function }
  32. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  33. { no register variables }
  34. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  35. => don't optimize}
  36. pi_needs_implicit_finally = $80; { set, if the procedure contains data which }
  37. { needs to be finalized }
  38. type
  39. TOpCg = (OP_ADD,OP_AND,OP_DIV,OP_IDIV,OP_IMUL,OP_MUL,OP_NEG,OP_NOT,
  40. OP_OR,OP_SAR,OP_SHL,OP_SHR,OP_SUB,OP_XOR);
  41. TOpCmp = (OC_NONE,OC_EQ,OC_GT,OC_LT,OC_GTE,OC_LTE,OC_NE,OC_BE,OC_B,
  42. OC_AE,OC_A);
  43. TCgSize = (OS_NO,OS_8,OS_16,OS_32,OS_64);
  44. pprocinfo = ^tprocinfo;
  45. tprocinfo = object
  46. { pointer to parent in nested procedures }
  47. parent : pprocinfo;
  48. { current class, if we are in a method }
  49. _class : pobjectdef;
  50. { return type }
  51. {$IFNDEF NEWST}
  52. returntype : ttype;
  53. {$ENDIF NEWST}
  54. { symbol of the function, and the sym for result variable }
  55. resultfuncretsym,
  56. funcretsym : pfuncretsym;
  57. funcret_state : tvarstate;
  58. { the definition of the proc itself }
  59. def : pprocdef;
  60. sym : pprocsym;
  61. { frame pointer offset }
  62. framepointer_offset : longint;
  63. { self pointer offset }
  64. selfpointer_offset : longint;
  65. { result value offset }
  66. return_offset : longint;
  67. { firsttemp position }
  68. firsttemp_offset : longint;
  69. { parameter offset }
  70. para_offset : longint;
  71. { every register which must be saved by the entry code }
  72. { (and restored by the exit code) must be in that set }
  73. registerstosave : tregisterset;
  74. { some collected informations about the procedure }
  75. { see pi_xxxx above }
  76. flags : longint;
  77. { register used as frame pointer }
  78. framepointer : tregister;
  79. { true, if the procedure is exported by an unit }
  80. globalsymbol : boolean;
  81. { true, if the procedure should be exported (only OS/2) }
  82. exported : boolean;
  83. { true, if we can not use fast exit code }
  84. no_fast_exit : boolean;
  85. { code for the current procedure }
  86. aktproccode,aktentrycode,
  87. aktexitcode,aktlocaldata : paasmoutput;
  88. { local data is used for smartlink }
  89. constructor init;
  90. destructor done;
  91. end;
  92. { some kind of temp. types needs to be destructed }
  93. { for example ansistring, this is done using this }
  94. { list }
  95. ptemptodestroy = ^ttemptodestroy;
  96. ttemptodestroy = object(tlinkedlist_item)
  97. typ : pdef;
  98. address : treference;
  99. constructor init(const a : treference;p : pdef);
  100. end;
  101. const
  102. { defines the default address size for a processor }
  103. { and defines the natural int size for a processor }
  104. {$ifdef i386}
  105. OS_ADDR = OS_32;
  106. OS_INT = OS_32;
  107. {$endif i386}
  108. {$ifdef alpha}
  109. OS_ADDR = OS_64;
  110. OS_INT = OS_64;
  111. {$endif alpha}
  112. {$ifdef powerpc}
  113. OS_ADDR = OS_32;
  114. OS_INT = OS_32;
  115. {$endif powercc}
  116. var
  117. { info about the current sub routine }
  118. procinfo : pprocinfo;
  119. { labels for BREAK and CONTINUE }
  120. aktbreaklabel,aktcontinuelabel : pasmlabel;
  121. { label when the result is true or false }
  122. truelabel,falselabel : pasmlabel;
  123. { label to leave the sub routine }
  124. aktexitlabel : pasmlabel;
  125. { also an exit label, only used we need to clear only the stack }
  126. aktexit2label : pasmlabel;
  127. { only used in constructor for fail or if getmem fails }
  128. faillabel,quickexitlabel : pasmlabel;
  129. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  130. simple_loadn : boolean;
  131. { tries to hold the amount of times which the current tree is processed }
  132. t_times : longint;
  133. { true, if an error while code generation occurs }
  134. codegenerror : boolean;
  135. { this is for open arrays and strings }
  136. { but be careful, this data is in the }
  137. { generated code destroyed quick, and also }
  138. { the next call of secondload destroys this }
  139. { data }
  140. { So be careful using the informations }
  141. { provided by this variables }
  142. highframepointer : tregister;
  143. highoffset : longint;
  144. make_const_global : boolean;
  145. temptoremove : plinkedlist;
  146. { message calls with codegenerror support }
  147. procedure cgmessage(const t : tmsgconst);
  148. procedure cgmessage1(const t : tmsgconst;const s : string);
  149. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  150. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  151. procedure CGMessagePos(const pos:tfileposinfo;t:tmsgconst);
  152. procedure CGMessagePos1(const pos:tfileposinfo;t:tmsgconst;const s1:string);
  153. procedure CGMessagePos2(const pos:tfileposinfo;t:tmsgconst;const s1,s2:string);
  154. procedure CGMessagePos3(const pos:tfileposinfo;t:tmsgconst;const s1,s2,s3:string);
  155. { initialize respectively terminates the code generator }
  156. { for a new module or procedure }
  157. procedure codegen_doneprocedure;
  158. procedure codegen_donemodule;
  159. procedure codegen_newmodule;
  160. procedure codegen_newprocedure;
  161. { counts the labels }
  162. function case_count_labels(root : pcaserecord) : longint;
  163. { searches the highest label }
  164. function case_get_max(root : pcaserecord) : longint;
  165. { searches the lowest label }
  166. function case_get_min(root : pcaserecord) : longint;
  167. { clears a location record }
  168. procedure clear_location(var loc : tlocation);
  169. { copies a location, takes care of the symbol }
  170. procedure set_location(var destloc,sourceloc : tlocation);
  171. { swaps two locations }
  172. procedure swap_location(var destloc,sourceloc : tlocation);
  173. implementation
  174. uses
  175. comphook;
  176. {*****************************************************************************
  177. override the message calls to set codegenerror
  178. *****************************************************************************}
  179. procedure cgmessage(const t : tmsgconst);
  180. var
  181. olderrorcount : longint;
  182. begin
  183. if not(codegenerror) then
  184. begin
  185. olderrorcount:=status.errorcount;
  186. verbose.Message(t);
  187. codegenerror:=olderrorcount<>status.errorcount;
  188. end;
  189. end;
  190. procedure cgmessage1(const t : tmsgconst;const s : string);
  191. var
  192. olderrorcount : longint;
  193. begin
  194. if not(codegenerror) then
  195. begin
  196. olderrorcount:=status.errorcount;
  197. verbose.Message1(t,s);
  198. codegenerror:=olderrorcount<>status.errorcount;
  199. end;
  200. end;
  201. procedure cgmessage2(const t : tmsgconst;const s1,s2 : string);
  202. var
  203. olderrorcount : longint;
  204. begin
  205. if not(codegenerror) then
  206. begin
  207. olderrorcount:=status.errorcount;
  208. verbose.Message2(t,s1,s2);
  209. codegenerror:=olderrorcount<>status.errorcount;
  210. end;
  211. end;
  212. procedure cgmessage3(const t : tmsgconst;const s1,s2,s3 : string);
  213. var
  214. olderrorcount : longint;
  215. begin
  216. if not(codegenerror) then
  217. begin
  218. olderrorcount:=status.errorcount;
  219. verbose.Message3(t,s1,s2,s3);
  220. codegenerror:=olderrorcount<>status.errorcount;
  221. end;
  222. end;
  223. procedure cgmessagepos(const pos:tfileposinfo;t : tmsgconst);
  224. var
  225. olderrorcount : longint;
  226. begin
  227. if not(codegenerror) then
  228. begin
  229. olderrorcount:=Errorcount;
  230. verbose.MessagePos(pos,t);
  231. codegenerror:=olderrorcount<>Errorcount;
  232. end;
  233. end;
  234. procedure cgmessagepos1(const pos:tfileposinfo;t : tmsgconst;const s1 : string);
  235. var
  236. olderrorcount : longint;
  237. begin
  238. if not(codegenerror) then
  239. begin
  240. olderrorcount:=Errorcount;
  241. verbose.MessagePos1(pos,t,s1);
  242. codegenerror:=olderrorcount<>Errorcount;
  243. end;
  244. end;
  245. procedure cgmessagepos2(const pos:tfileposinfo;t : tmsgconst;const s1,s2 : string);
  246. var
  247. olderrorcount : longint;
  248. begin
  249. if not(codegenerror) then
  250. begin
  251. olderrorcount:=Errorcount;
  252. verbose.MessagePos2(pos,t,s1,s2);
  253. codegenerror:=olderrorcount<>Errorcount;
  254. end;
  255. end;
  256. procedure cgmessagepos3(const pos:tfileposinfo;t : tmsgconst;const s1,s2,s3 : string);
  257. var
  258. olderrorcount : longint;
  259. begin
  260. if not(codegenerror) then
  261. begin
  262. olderrorcount:=Errorcount;
  263. verbose.MessagePos3(pos,t,s1,s2,s3);
  264. codegenerror:=olderrorcount<>Errorcount;
  265. end;
  266. end;
  267. {****************************************************************************
  268. TProcInfo
  269. ****************************************************************************}
  270. constructor tprocinfo.init;
  271. begin
  272. parent:=nil;
  273. _class:=nil;
  274. {$IFNDEF NEWST}
  275. returntype.reset;
  276. {$ENDIF NEWST}
  277. resultfuncretsym:=nil;
  278. funcretsym:=nil;
  279. funcret_state:=vs_none;
  280. def:=nil;
  281. sym:=nil;
  282. framepointer_offset:=0;
  283. selfpointer_offset:=0;
  284. return_offset:=0;
  285. firsttemp_offset:=0;
  286. para_offset:=0;
  287. registerstosave:=[];
  288. flags:=0;
  289. framepointer:=R_NO;
  290. globalsymbol:=false;
  291. exported:=false;
  292. aktentrycode:=new(paasmoutput,init);
  293. aktexitcode:=new(paasmoutput,init);
  294. aktproccode:=new(paasmoutput,init);
  295. aktlocaldata:=new(paasmoutput,init);
  296. end;
  297. destructor tprocinfo.done;
  298. begin
  299. dispose(aktentrycode,done);
  300. dispose(aktexitcode,done);
  301. dispose(aktproccode,done);
  302. dispose(aktlocaldata,done);
  303. end;
  304. {*****************************************************************************
  305. initialize/terminate the codegen for procedure and modules
  306. *****************************************************************************}
  307. procedure codegen_newprocedure;
  308. begin
  309. aktbreaklabel:=nil;
  310. aktcontinuelabel:=nil;
  311. new(procinfo,init);
  312. { aktexitlabel:=0; is store in oldaktexitlabel
  313. so it must not be reset to zero before this storage !}
  314. end;
  315. procedure codegen_doneprocedure;
  316. begin
  317. dispose(procinfo,done);
  318. procinfo:=nil;
  319. end;
  320. procedure codegen_newmodule;
  321. begin
  322. exprasmlist:=new(paasmoutput,init);
  323. datasegment:=new(paasmoutput,init);
  324. codesegment:=new(paasmoutput,init);
  325. bsssegment:=new(paasmoutput,init);
  326. debuglist:=new(paasmoutput,init);
  327. consts:=new(paasmoutput,init);
  328. rttilist:=new(paasmoutput,init);
  329. importssection:=nil;
  330. exportssection:=nil;
  331. resourcesection:=nil;
  332. asmsymbollist:=new(pasmsymbollist,init);
  333. asmsymbollist^.usehash;
  334. end;
  335. procedure codegen_donemodule;
  336. begin
  337. dispose(exprasmlist,done);
  338. dispose(codesegment,done);
  339. dispose(bsssegment,done);
  340. dispose(datasegment,done);
  341. dispose(debuglist,done);
  342. dispose(consts,done);
  343. dispose(rttilist,done);
  344. if assigned(importssection) then
  345. dispose(importssection,done);
  346. if assigned(exportssection) then
  347. dispose(exportssection,done);
  348. if assigned(resourcesection) then
  349. dispose(resourcesection,done);
  350. if assigned(resourcestringlist) then
  351. dispose(resourcestringlist,done);
  352. dispose(asmsymbollist,done);
  353. end;
  354. {*****************************************************************************
  355. Case Helpers
  356. *****************************************************************************}
  357. function case_count_labels(root : pcaserecord) : longint;
  358. var
  359. _l : longint;
  360. procedure count(p : pcaserecord);
  361. begin
  362. inc(_l);
  363. if assigned(p^.less) then
  364. count(p^.less);
  365. if assigned(p^.greater) then
  366. count(p^.greater);
  367. end;
  368. begin
  369. _l:=0;
  370. count(root);
  371. case_count_labels:=_l;
  372. end;
  373. function case_get_max(root : pcaserecord) : longint;
  374. var
  375. hp : pcaserecord;
  376. begin
  377. hp:=root;
  378. while assigned(hp^.greater) do
  379. hp:=hp^.greater;
  380. case_get_max:=hp^._high;
  381. end;
  382. function case_get_min(root : pcaserecord) : longint;
  383. var
  384. hp : pcaserecord;
  385. begin
  386. hp:=root;
  387. while assigned(hp^.less) do
  388. hp:=hp^.less;
  389. case_get_min:=hp^._low;
  390. end;
  391. {*****************************************************************************
  392. TTempToDestroy
  393. *****************************************************************************}
  394. constructor ttemptodestroy.init(const a : treference;p : pdef);
  395. begin
  396. inherited init;
  397. address:=a;
  398. typ:=p;
  399. end;
  400. {*****************************************************************************
  401. some helper routines to handle locations
  402. *****************************************************************************}
  403. procedure clear_location(var loc : tlocation);
  404. begin
  405. if ((loc.loc=LOC_MEM) or (loc.loc=LOC_REFERENCE)) and
  406. assigned(loc.reference.symbol) then
  407. dispose(loc.reference.symbol,done);
  408. loc.loc:=LOC_INVALID;
  409. end;
  410. procedure set_location(var destloc,sourceloc : tlocation);
  411. begin
  412. { this is needed if you want to be able to delete }
  413. { the string with the nodes }
  414. if assigned(destloc.reference.symbol) then
  415. dispose(destloc.reference.symbol,done);
  416. destloc:= sourceloc;
  417. if sourceloc.loc in [LOC_MEM,LOC_REFERENCE] then
  418. begin
  419. if assigned(sourceloc.reference.symbol) then
  420. destloc.reference.symbol:=
  421. sourceloc.reference.symbol;
  422. end
  423. else
  424. destloc.reference.symbol:=nil;
  425. end;
  426. procedure swap_location(var destloc,sourceloc : tlocation);
  427. var
  428. swapl : tlocation;
  429. begin
  430. swapl:=destloc;
  431. destloc:=sourceloc;
  432. sourceloc:=swapl;
  433. end;
  434. end.
  435. {
  436. $Log$
  437. Revision 1.18 2000-02-28 17:23:58 daniel
  438. * Current work of symtable integration committed. The symtable can be
  439. activated by defining 'newst', but doesn't compile yet. Changes in type
  440. checking and oop are completed. What is left is to write a new
  441. symtablestack and adapt the parser to use it.
  442. Revision 1.17 2000/02/20 20:49:46 florian
  443. * newcg is compiling
  444. * fixed the dup id problem reported by Paul Y.
  445. Revision 1.16 2000/02/17 14:48:36 florian
  446. * updated to use old firstpass
  447. Revision 1.15 2000/01/07 01:14:52 peter
  448. * updated copyright to 2000
  449. Revision 1.14 1999/12/24 22:47:42 jonas
  450. * added OC_NONE to the compare forms (to allow unconditional jumps)
  451. Revision 1.13 1999/12/01 12:42:33 peter
  452. * fixed bug 698
  453. * removed some notes about unused vars
  454. Revision 1.12 1999/11/05 13:15:00 florian
  455. * some fixes to get the new cg compiling again
  456. Revision 1.11 1999/10/14 14:57:54 florian
  457. - removed the hcodegen use in the new cg, use cgbase instead
  458. Revision 1.10 1999/10/12 21:20:46 florian
  459. * new codegenerator compiles again
  460. Revision 1.9 1999/09/10 18:48:11 florian
  461. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  462. * most things for stored properties fixed
  463. Revision 1.8 1999/08/06 13:26:49 florian
  464. * more changes ...
  465. Revision 1.7 1999/08/05 14:58:10 florian
  466. * some fixes for the floating point registers
  467. * more things for the new code generator
  468. Revision 1.6 1999/08/04 00:23:51 florian
  469. * renamed i386asm and i386base to cpuasm and cpubase
  470. Revision 1.5 1999/08/01 18:22:32 florian
  471. * made it again compilable
  472. Revision 1.4 1999/01/23 23:29:45 florian
  473. * first running version of the new code generator
  474. * when compiling exceptions under Linux fixed
  475. Revision 1.3 1999/01/06 22:58:48 florian
  476. + some stuff for the new code generator
  477. Revision 1.2 1998/12/26 15:20:28 florian
  478. + more changes for the new version
  479. Revision 1.1 1998/12/15 22:18:55 florian
  480. * some code added
  481. }