cgbase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit exports some help routines for the code generation
  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. {$i defines.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globals,verbose,
  26. { symtable }
  27. symconst,symtype,symdef,symsym,
  28. { aasm }
  29. aasm,cpubase
  30. ;
  31. type
  32. TOpCg = (OP_ADD,OP_AND,OP_DIV,OP_IDIV,OP_IMUL,OP_MUL,OP_NEG,OP_NOT,
  33. OP_OR,OP_SAR,OP_SHL,OP_SHR,OP_SUB,OP_XOR);
  34. TOpCmp = (OC_NONE,OC_EQ,OC_GT,OC_LT,OC_GTE,OC_LTE,OC_NE,OC_BE,OC_B,
  35. OC_AE,OC_A);
  36. TCgSize = (OS_NO,OS_8,OS_16,OS_32,OS_64,OS_S8,OS_S16,OS_S32,OS_S64);
  37. const
  38. pi_uses_asm = $1; { set, if the procedure uses asm }
  39. pi_is_global = $2; { set, if the procedure is exported by an unit }
  40. pi_do_call = $4; { set, if the procedure does a call }
  41. pi_operator = $8; { set, if the procedure is an operator }
  42. pi_C_import = $10; { set, if the procedure is an external C function }
  43. pi_uses_exceptions = $20;{ set, if the procedure has a try statement => }
  44. { no register variables }
  45. pi_is_assembler = $40; { set if the procedure is declared as ASSEMBLER
  46. => don't optimize}
  47. pi_needs_implicit_finally = $80; { set, if the procedure contains data which }
  48. { needs to be finalized }
  49. { defines the default address size for a processor }
  50. { and defines the natural int size for a processor }
  51. {$ifdef i386}
  52. OS_ADDR = OS_32;
  53. OS_INT = OS_32;
  54. {$endif i386}
  55. {$ifdef alpha}
  56. OS_ADDR = OS_64;
  57. OS_INT = OS_64;
  58. {$endif alpha}
  59. {$ifdef powerpc}
  60. OS_ADDR = OS_32;
  61. OS_INT = OS_32;
  62. {$endif powercc}
  63. {$ifdef ia64}
  64. OS_ADDR = OS_64;
  65. OS_INT = OS_64;
  66. {$endif ia64}
  67. type
  68. pprocinfo = ^tprocinfo;
  69. tprocinfo = object
  70. { pointer to parent in nested procedures }
  71. parent : pprocinfo;
  72. { current class, if we are in a method }
  73. _class : tobjectdef;
  74. { the definition of the proc itself }
  75. procdef : tprocdef;
  76. { frame pointer offset }
  77. framepointer_offset : longint;
  78. { self pointer offset }
  79. selfpointer_offset : longint;
  80. { result value offset }
  81. return_offset : longint;
  82. { firsttemp position }
  83. firsttemp_offset : longint;
  84. { parameter offset }
  85. para_offset : longint;
  86. { some collected informations about the procedure }
  87. { see pi_xxxx above }
  88. flags : longint;
  89. { register used as frame pointer }
  90. framepointer : tregister;
  91. { true, if the procedure is exported by an unit }
  92. globalsymbol : boolean;
  93. { true, if the procedure should be exported (only OS/2) }
  94. exported : boolean;
  95. { true, if we can not use fast exit code }
  96. no_fast_exit : boolean;
  97. { code for the current procedure }
  98. aktproccode,aktentrycode,
  99. aktexitcode,aktlocaldata : taasmoutput;
  100. { local data is used for smartlink }
  101. constructor init;
  102. destructor done;
  103. end;
  104. pregvarinfo = ^tregvarinfo;
  105. tregvarinfo = record
  106. regvars : array[1..maxvarregs] of tvarsym;
  107. regvars_para : array[1..maxvarregs] of boolean;
  108. regvars_refs : array[1..maxvarregs] of longint;
  109. fpuregvars : array[1..maxfpuvarregs] of tvarsym;
  110. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  111. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  112. end;
  113. var
  114. { info about the current sub routine }
  115. procinfo : pprocinfo;
  116. { labels for BREAK and CONTINUE }
  117. aktbreaklabel,aktcontinuelabel : tasmlabel;
  118. { label when the result is true or false }
  119. truelabel,falselabel : tasmlabel;
  120. { label to leave the sub routine }
  121. aktexitlabel : tasmlabel;
  122. { also an exit label, only used we need to clear only the stack }
  123. aktexit2label : tasmlabel;
  124. { only used in constructor for fail or if getmem fails }
  125. faillabel,quickexitlabel : tasmlabel;
  126. { Boolean, wenn eine loadn kein Assembler erzeugt hat }
  127. simple_loadn : boolean;
  128. { true, if an error while code generation occurs }
  129. codegenerror : boolean;
  130. { save the size of pushed parameter, needed for aligning }
  131. pushedparasize : longint;
  132. make_const_global : boolean;
  133. { message calls with codegenerror support }
  134. procedure cgmessage(t : longint);
  135. procedure cgmessage1(t : longint;const s : string);
  136. procedure cgmessage2(t : longint;const s1,s2 : string);
  137. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  138. procedure CGMessagePos(const pos:tfileposinfo;t:longint);
  139. procedure CGMessagePos1(const pos:tfileposinfo;t:longint;const s1:string);
  140. procedure CGMessagePos2(const pos:tfileposinfo;t:longint;const s1,s2:string);
  141. procedure CGMessagePos3(const pos:tfileposinfo;t:longint;const s1,s2,s3:string);
  142. { initialize respectively terminates the code generator }
  143. { for a new module or procedure }
  144. procedure codegen_doneprocedure;
  145. procedure codegen_donemodule;
  146. procedure codegen_newmodule;
  147. procedure codegen_newprocedure;
  148. function def_cgsize(const p1: tdef): tcgsize;
  149. { return the inverse condition of opcmp }
  150. function inverse_opcmp(opcmp: topcmp): topcmp;
  151. { return whether op is commutative }
  152. function commutativeop(op: topcg): boolean;
  153. implementation
  154. uses
  155. systems,
  156. cresstr,
  157. types
  158. {$ifdef fixLeaksOnError}
  159. ,comphook
  160. {$endif fixLeaksOnError}
  161. ;
  162. {$ifdef fixLeaksOnError}
  163. var procinfoStack: TStack;
  164. hcodegen_old_do_stop: tstopprocedure;
  165. {$endif fixLeaksOnError}
  166. {*****************************************************************************
  167. override the message calls to set codegenerror
  168. *****************************************************************************}
  169. procedure cgmessage(t : longint);
  170. var
  171. olderrorcount : longint;
  172. begin
  173. if not(codegenerror) then
  174. begin
  175. olderrorcount:=Errorcount;
  176. verbose.Message(t);
  177. codegenerror:=olderrorcount<>Errorcount;
  178. end;
  179. end;
  180. procedure cgmessage1(t : longint;const s : string);
  181. var
  182. olderrorcount : longint;
  183. begin
  184. if not(codegenerror) then
  185. begin
  186. olderrorcount:=Errorcount;
  187. verbose.Message1(t,s);
  188. codegenerror:=olderrorcount<>Errorcount;
  189. end;
  190. end;
  191. procedure cgmessage2(t : longint;const s1,s2 : string);
  192. var
  193. olderrorcount : longint;
  194. begin
  195. if not(codegenerror) then
  196. begin
  197. olderrorcount:=Errorcount;
  198. verbose.Message2(t,s1,s2);
  199. codegenerror:=olderrorcount<>Errorcount;
  200. end;
  201. end;
  202. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  203. var
  204. olderrorcount : longint;
  205. begin
  206. if not(codegenerror) then
  207. begin
  208. olderrorcount:=Errorcount;
  209. verbose.Message3(t,s1,s2,s3);
  210. codegenerror:=olderrorcount<>Errorcount;
  211. end;
  212. end;
  213. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  214. var
  215. olderrorcount : longint;
  216. begin
  217. if not(codegenerror) then
  218. begin
  219. olderrorcount:=Errorcount;
  220. verbose.MessagePos(pos,t);
  221. codegenerror:=olderrorcount<>Errorcount;
  222. end;
  223. end;
  224. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : string);
  225. var
  226. olderrorcount : longint;
  227. begin
  228. if not(codegenerror) then
  229. begin
  230. olderrorcount:=Errorcount;
  231. verbose.MessagePos1(pos,t,s1);
  232. codegenerror:=olderrorcount<>Errorcount;
  233. end;
  234. end;
  235. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : string);
  236. var
  237. olderrorcount : longint;
  238. begin
  239. if not(codegenerror) then
  240. begin
  241. olderrorcount:=Errorcount;
  242. verbose.MessagePos2(pos,t,s1,s2);
  243. codegenerror:=olderrorcount<>Errorcount;
  244. end;
  245. end;
  246. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : string);
  247. var
  248. olderrorcount : longint;
  249. begin
  250. if not(codegenerror) then
  251. begin
  252. olderrorcount:=Errorcount;
  253. verbose.MessagePos3(pos,t,s1,s2,s3);
  254. codegenerror:=olderrorcount<>Errorcount;
  255. end;
  256. end;
  257. {****************************************************************************
  258. TProcInfo
  259. ****************************************************************************}
  260. constructor tprocinfo.init;
  261. begin
  262. parent:=nil;
  263. _class:=nil;
  264. procdef:=nil;
  265. framepointer_offset:=0;
  266. selfpointer_offset:=0;
  267. return_offset:=0;
  268. firsttemp_offset:=0;
  269. para_offset:=0;
  270. flags:=0;
  271. framepointer:=R_NO;
  272. globalsymbol:=false;
  273. exported:=false;
  274. no_fast_exit:=false;
  275. aktentrycode:=Taasmoutput.Create;
  276. aktexitcode:=Taasmoutput.Create;
  277. aktproccode:=Taasmoutput.Create;
  278. aktlocaldata:=Taasmoutput.Create;
  279. end;
  280. destructor tprocinfo.done;
  281. begin
  282. aktentrycode.free;
  283. aktexitcode.free;
  284. aktproccode.free;
  285. aktlocaldata.free;
  286. end;
  287. {*****************************************************************************
  288. initialize/terminate the codegen for procedure and modules
  289. *****************************************************************************}
  290. procedure codegen_newprocedure;
  291. begin
  292. aktbreaklabel:=nil;
  293. aktcontinuelabel:=nil;
  294. { aktexitlabel:=0; is store in oldaktexitlabel
  295. so it must not be reset to zero before this storage !}
  296. { new procinfo }
  297. new(procinfo,init);
  298. {$ifdef fixLeaksOnError}
  299. procinfoStack.push(procinfo);
  300. {$endif fixLeaksOnError}
  301. end;
  302. procedure codegen_doneprocedure;
  303. begin
  304. {$ifdef fixLeaksOnError}
  305. if procinfo <> procinfoStack.pop then
  306. writeln('problem with procinfoStack!');
  307. {$endif fixLeaksOnError}
  308. dispose(procinfo,done);
  309. procinfo:=nil;
  310. end;
  311. procedure codegen_newmodule;
  312. begin
  313. exprasmlist:=taasmoutput.create;
  314. datasegment:=taasmoutput.create;
  315. codesegment:=taasmoutput.create;
  316. bsssegment:=taasmoutput.create;
  317. debuglist:=taasmoutput.create;
  318. withdebuglist:=taasmoutput.create;
  319. consts:=taasmoutput.create;
  320. rttilist:=taasmoutput.create;
  321. ResourceStringList:=Nil;
  322. importssection:=nil;
  323. exportssection:=nil;
  324. resourcesection:=nil;
  325. { assembler symbols }
  326. asmsymbollist:=tdictionary.create;
  327. asmsymbollist.usehash;
  328. { resourcestrings }
  329. ResourceStrings:=TResourceStrings.Create;
  330. end;
  331. procedure codegen_donemodule;
  332. {$ifdef MEMDEBUG}
  333. var
  334. d : tmemdebug;
  335. {$endif}
  336. begin
  337. {$ifdef MEMDEBUG}
  338. d.init('asmlist');
  339. {$endif}
  340. exprasmlist.free;
  341. codesegment.free;
  342. bsssegment.free;
  343. datasegment.free;
  344. debuglist.free;
  345. withdebuglist.free;
  346. consts.free;
  347. rttilist.free;
  348. if assigned(ResourceStringList) then
  349. ResourceStringList.free;
  350. if assigned(importssection) then
  351. importssection.free;
  352. if assigned(exportssection) then
  353. exportssection.free;
  354. if assigned(resourcesection) then
  355. resourcesection.free;
  356. {$ifdef MEMDEBUG}
  357. d.free;
  358. {$endif}
  359. { assembler symbols }
  360. {$ifdef MEMDEBUG}
  361. d.init('asmsymbol');
  362. {$endif}
  363. asmsymbollist.free;
  364. {$ifdef MEMDEBUG}
  365. d.free;
  366. {$endif}
  367. { resource strings }
  368. ResourceStrings.free;
  369. end;
  370. function def_cgsize(const p1: tdef): tcgsize;
  371. begin
  372. case p1.size of
  373. 1: result := OS_8;
  374. 2: result := OS_16;
  375. 4: result := OS_32;
  376. else
  377. internalerror(2001092311);
  378. end;
  379. if is_signed(p1) then
  380. result := tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  381. end;
  382. function inverse_opcmp(opcmp: topcmp): topcmp;
  383. const
  384. list: array[TOpCmp] of TOpCmp =
  385. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  386. OC_B,OC_BE);
  387. begin
  388. inverse_opcmp := list[opcmp];
  389. end;
  390. function commutativeop(op: topcg): boolean;
  391. const
  392. list: array[topcg] of boolean =
  393. (true,true,false,false,true,true,false,false,
  394. true,false,false,false,false,true);
  395. begin
  396. commutativeop := list[op];
  397. end;
  398. {$ifdef fixLeaksOnError}
  399. procedure hcodegen_do_stop;
  400. var p: pprocinfo;
  401. begin
  402. p := pprocinfo(procinfoStack.pop);
  403. while p <> nil Do
  404. begin
  405. dispose(p,done);
  406. p := pprocinfo(procinfoStack.pop);
  407. end;
  408. procinfoStack.done;
  409. do_stop := hcodegen_old_do_stop;
  410. do_stop{$ifdef FPCPROCVAR}(){$endif};
  411. end;
  412. begin
  413. hcodegen_old_do_stop := do_stop;
  414. do_stop := {$ifdef FPCPROCVAR}@{$endif}hcodegen_do_stop;
  415. procinfoStack.init;
  416. {$endif fixLeaksOnError}
  417. end.
  418. {
  419. $Log$
  420. Revision 1.2 2001-09-28 20:39:33 jonas
  421. * changed all flow control structures (except for exception handling
  422. related things) to processor independent code (in new ncgflw unit)
  423. + generic cgobj unit which contains lots of code generator helpers with
  424. global "cg" class instance variable
  425. + cgcpu unit for i386 (implements processor specific routines of the above
  426. unit)
  427. * updated cgbase and cpubase for the new code generator units
  428. * include ncgflw unit in cpunode unit
  429. Revision 1.1 2001/08/26 13:36:36 florian
  430. * some cg reorganisation
  431. * some PPC updates
  432. Revision 1.11 2001/08/06 21:40:46 peter
  433. * funcret moved from tprocinfo to tprocdef
  434. Revision 1.10 2001/04/13 01:22:07 peter
  435. * symtable change to classes
  436. * range check generation and errors fixed, make cycle DEBUG=1 works
  437. * memory leaks fixed
  438. Revision 1.9 2000/12/25 00:07:26 peter
  439. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  440. tlinkedlist objects)
  441. Revision 1.8 2000/11/30 22:16:49 florian
  442. * moved to i386
  443. Revision 1.7 2000/10/31 22:02:47 peter
  444. * symtable splitted, no real code changes
  445. Revision 1.6 2000/09/24 15:06:17 peter
  446. * use defines.inc
  447. Revision 1.5 2000/08/27 16:11:51 peter
  448. * moved some util functions from globals,cobjects to cutils
  449. * splitted files into finput,fmodule
  450. Revision 1.4 2000/08/12 15:34:22 peter
  451. + usedasmsymbollist to check and reset only the used symbols (merged)
  452. Revision 1.3 2000/08/03 13:17:26 jonas
  453. + allow regvars to be used inside inlined procs, which required the
  454. following changes:
  455. + load regvars in genentrycode/free them in genexitcode (cgai386)
  456. * moved all regvar related code to new regvars unit
  457. + added pregvarinfo type to hcodegen
  458. + added regvarinfo field to tprocinfo (symdef/symdefh)
  459. * deallocate the regvars of the caller in secondprocinline before
  460. inlining the called procedure and reallocate them afterwards
  461. Revision 1.2 2000/07/13 11:32:41 michael
  462. + removed logs
  463. }