cgbase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globals,verbose,
  26. { symtable }
  27. symconst,symtype,symdef,symsym,
  28. { aasm }
  29. cpubase,cpuinfo,cginfo,aasmbase,aasmtai
  30. ;
  31. const
  32. {# bitmask indicating if the procedure uses asm }
  33. pi_uses_asm = $1;
  34. {# bitmask indicating if the procedure is exported by an unit }
  35. pi_is_global = $2;
  36. {# bitmask indicating if the procedure does a call }
  37. pi_do_call = $4;
  38. {# bitmask indicating if the procedure is an operator }
  39. pi_operator = $8;
  40. {# bitmask indicating if the procedure is an external C function }
  41. pi_c_import = $10;
  42. {# bitmask indicating if the procedure has a try statement = no register optimization }
  43. pi_uses_exceptions = $20;
  44. {# bitmask indicating if the procedure is declared as @var(assembler), don't optimize}
  45. pi_is_assembler = $40;
  46. {# bitmask indicating if the procedure contains data which needs to be finalized }
  47. pi_needs_implicit_finally = $80;
  48. type
  49. pprocinfo = ^tprocinfo;
  50. tprocinfo = object
  51. {# pointer to parent in nested procedures }
  52. parent : pprocinfo;
  53. {# current class, if we are in a method }
  54. _class : tobjectdef;
  55. {# the definition of the routine itself }
  56. procdef : tprocdef;
  57. {# frame pointer offset??? }
  58. framepointer_offset : longint;
  59. { self pointer offset???? }
  60. selfpointer_offset : longint;
  61. {# result value offset in stack (functions only) }
  62. return_offset : longint;
  63. {# firsttemp position }
  64. firsttemp_offset : longint;
  65. {# parameter offset in stack }
  66. para_offset : longint;
  67. {# some collected informations about the procedure
  68. see pi_xxxx above }
  69. flags : longint;
  70. {# register used as frame pointer }
  71. framepointer : tregister;
  72. {# true, if the procedure is exported by a unit }
  73. globalsymbol : boolean;
  74. {# true, if the procedure should be exported (only OS/2) }
  75. exported : boolean;
  76. {# true, if we can not use fast exit code }
  77. no_fast_exit : boolean;
  78. aktproccode,aktentrycode,
  79. aktexitcode,aktlocaldata : taasmoutput;
  80. constructor init;
  81. destructor done;
  82. end;
  83. pregvarinfo = ^tregvarinfo;
  84. tregvarinfo = record
  85. regvars : array[1..maxvarregs] of tvarsym;
  86. regvars_para : array[1..maxvarregs] of boolean;
  87. regvars_refs : array[1..maxvarregs] of longint;
  88. fpuregvars : array[1..maxfpuvarregs] of tvarsym;
  89. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  90. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  91. end;
  92. var
  93. {# information about the current sub routine being parsed (@var(pprocinfo))}
  94. procinfo : pprocinfo;
  95. { labels for BREAK and CONTINUE }
  96. aktbreaklabel,aktcontinuelabel : tasmlabel;
  97. { label when the result is true or false }
  98. truelabel,falselabel : tasmlabel;
  99. { label to leave the sub routine }
  100. aktexitlabel : tasmlabel;
  101. { also an exit label, only used we need to clear only the stack }
  102. aktexit2label : tasmlabel;
  103. {# only used in constructor for fail keyword or if getmem fails }
  104. faillabel : tasmlabel;
  105. quickexitlabel : tasmlabel;
  106. {# true, if there was an error while code generation occurs }
  107. codegenerror : boolean;
  108. { save the size of pushed parameter, needed for aligning }
  109. pushedparasize : longint;
  110. { message calls with codegenerror support }
  111. procedure cgmessage(t : longint);
  112. procedure cgmessage1(t : longint;const s : string);
  113. procedure cgmessage2(t : longint;const s1,s2 : string);
  114. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  115. procedure CGMessagePos(const pos:tfileposinfo;t:longint);
  116. procedure CGMessagePos1(const pos:tfileposinfo;t:longint;const s1:string);
  117. procedure CGMessagePos2(const pos:tfileposinfo;t:longint;const s1,s2:string);
  118. procedure CGMessagePos3(const pos:tfileposinfo;t:longint;const s1,s2,s3:string);
  119. { initialize respectively terminates the code generator }
  120. { for a new module or procedure }
  121. procedure codegen_doneprocedure;
  122. procedure codegen_donemodule;
  123. procedure codegen_newmodule;
  124. procedure codegen_newprocedure;
  125. {# From a definition return the abstract code generator size (@var(tcgsize) enum). It is
  126. to note that the value returned can be @var(OS_NO) }
  127. function def_cgsize(def: tdef): tcgsize;
  128. function int_cgsize(const l: aword): tcgsize;
  129. {# return the inverse condition of opcmp }
  130. function inverse_opcmp(opcmp: topcmp): topcmp;
  131. {# return whether op is commutative }
  132. function commutativeop(op: topcg): boolean;
  133. implementation
  134. uses
  135. systems,
  136. cresstr,
  137. defbase
  138. {$ifdef fixLeaksOnError}
  139. ,comphook
  140. {$endif fixLeaksOnError}
  141. ;
  142. {$ifdef fixLeaksOnError}
  143. var procinfoStack: TStack;
  144. hcodegen_old_do_stop: tstopprocedure;
  145. {$endif fixLeaksOnError}
  146. {*****************************************************************************
  147. override the message calls to set codegenerror
  148. *****************************************************************************}
  149. procedure cgmessage(t : longint);
  150. var
  151. olderrorcount : longint;
  152. begin
  153. if not(codegenerror) then
  154. begin
  155. olderrorcount:=Errorcount;
  156. verbose.Message(t);
  157. codegenerror:=olderrorcount<>Errorcount;
  158. end;
  159. end;
  160. procedure cgmessage1(t : longint;const s : string);
  161. var
  162. olderrorcount : longint;
  163. begin
  164. if not(codegenerror) then
  165. begin
  166. olderrorcount:=Errorcount;
  167. verbose.Message1(t,s);
  168. codegenerror:=olderrorcount<>Errorcount;
  169. end;
  170. end;
  171. procedure cgmessage2(t : longint;const s1,s2 : string);
  172. var
  173. olderrorcount : longint;
  174. begin
  175. if not(codegenerror) then
  176. begin
  177. olderrorcount:=Errorcount;
  178. verbose.Message2(t,s1,s2);
  179. codegenerror:=olderrorcount<>Errorcount;
  180. end;
  181. end;
  182. procedure cgmessage3(t : longint;const s1,s2,s3 : string);
  183. var
  184. olderrorcount : longint;
  185. begin
  186. if not(codegenerror) then
  187. begin
  188. olderrorcount:=Errorcount;
  189. verbose.Message3(t,s1,s2,s3);
  190. codegenerror:=olderrorcount<>Errorcount;
  191. end;
  192. end;
  193. procedure cgmessagepos(const pos:tfileposinfo;t : longint);
  194. var
  195. olderrorcount : longint;
  196. begin
  197. if not(codegenerror) then
  198. begin
  199. olderrorcount:=Errorcount;
  200. verbose.MessagePos(pos,t);
  201. codegenerror:=olderrorcount<>Errorcount;
  202. end;
  203. end;
  204. procedure cgmessagepos1(const pos:tfileposinfo;t : longint;const s1 : string);
  205. var
  206. olderrorcount : longint;
  207. begin
  208. if not(codegenerror) then
  209. begin
  210. olderrorcount:=Errorcount;
  211. verbose.MessagePos1(pos,t,s1);
  212. codegenerror:=olderrorcount<>Errorcount;
  213. end;
  214. end;
  215. procedure cgmessagepos2(const pos:tfileposinfo;t : longint;const s1,s2 : string);
  216. var
  217. olderrorcount : longint;
  218. begin
  219. if not(codegenerror) then
  220. begin
  221. olderrorcount:=Errorcount;
  222. verbose.MessagePos2(pos,t,s1,s2);
  223. codegenerror:=olderrorcount<>Errorcount;
  224. end;
  225. end;
  226. procedure cgmessagepos3(const pos:tfileposinfo;t : longint;const s1,s2,s3 : string);
  227. var
  228. olderrorcount : longint;
  229. begin
  230. if not(codegenerror) then
  231. begin
  232. olderrorcount:=Errorcount;
  233. verbose.MessagePos3(pos,t,s1,s2,s3);
  234. codegenerror:=olderrorcount<>Errorcount;
  235. end;
  236. end;
  237. {****************************************************************************
  238. TProcInfo
  239. ****************************************************************************}
  240. constructor tprocinfo.init;
  241. begin
  242. parent:=nil;
  243. _class:=nil;
  244. procdef:=nil;
  245. framepointer_offset:=0;
  246. selfpointer_offset:=0;
  247. return_offset:=0;
  248. firsttemp_offset:=0;
  249. para_offset:=0;
  250. flags:=0;
  251. framepointer:=R_NO;
  252. globalsymbol:=false;
  253. exported:=false;
  254. no_fast_exit:=false;
  255. aktentrycode:=Taasmoutput.Create;
  256. aktexitcode:=Taasmoutput.Create;
  257. aktproccode:=Taasmoutput.Create;
  258. aktlocaldata:=Taasmoutput.Create;
  259. end;
  260. destructor tprocinfo.done;
  261. begin
  262. aktentrycode.free;
  263. aktexitcode.free;
  264. aktproccode.free;
  265. aktlocaldata.free;
  266. end;
  267. {*****************************************************************************
  268. initialize/terminate the codegen for procedure and modules
  269. *****************************************************************************}
  270. procedure codegen_newprocedure;
  271. begin
  272. aktbreaklabel:=nil;
  273. aktcontinuelabel:=nil;
  274. { aktexitlabel:=0; is store in oldaktexitlabel
  275. so it must not be reset to zero before this storage !}
  276. { new procinfo }
  277. new(procinfo,init);
  278. {$ifdef fixLeaksOnError}
  279. procinfoStack.push(procinfo);
  280. {$endif fixLeaksOnError}
  281. end;
  282. procedure codegen_doneprocedure;
  283. begin
  284. {$ifdef fixLeaksOnError}
  285. if procinfo <> procinfoStack.pop then
  286. writeln('problem with procinfoStack!');
  287. {$endif fixLeaksOnError}
  288. dispose(procinfo,done);
  289. procinfo:=nil;
  290. end;
  291. procedure codegen_newmodule;
  292. begin
  293. exprasmlist:=taasmoutput.create;
  294. datasegment:=taasmoutput.create;
  295. codesegment:=taasmoutput.create;
  296. bsssegment:=taasmoutput.create;
  297. debuglist:=taasmoutput.create;
  298. withdebuglist:=taasmoutput.create;
  299. consts:=taasmoutput.create;
  300. rttilist:=taasmoutput.create;
  301. ResourceStringList:=Nil;
  302. importssection:=nil;
  303. exportssection:=nil;
  304. resourcesection:=nil;
  305. { assembler symbols }
  306. asmsymbollist:=tdictionary.create;
  307. asmsymbollist.usehash;
  308. { resourcestrings }
  309. ResourceStrings:=TResourceStrings.Create;
  310. end;
  311. procedure codegen_donemodule;
  312. {$ifdef MEMDEBUG}
  313. var
  314. d : tmemdebug;
  315. {$endif}
  316. begin
  317. {$ifdef MEMDEBUG}
  318. d:=tmemdebug.create('asmlist');
  319. {$endif}
  320. exprasmlist.free;
  321. codesegment.free;
  322. bsssegment.free;
  323. datasegment.free;
  324. debuglist.free;
  325. withdebuglist.free;
  326. consts.free;
  327. rttilist.free;
  328. if assigned(ResourceStringList) then
  329. ResourceStringList.free;
  330. if assigned(importssection) then
  331. importssection.free;
  332. if assigned(exportssection) then
  333. exportssection.free;
  334. if assigned(resourcesection) then
  335. resourcesection.free;
  336. {$ifdef MEMDEBUG}
  337. d.free;
  338. {$endif}
  339. { assembler symbols }
  340. {$ifdef MEMDEBUG}
  341. d:=tmemdebug.create('asmsymbol');
  342. {$endif}
  343. asmsymbollist.free;
  344. {$ifdef MEMDEBUG}
  345. d.free;
  346. {$endif}
  347. { resource strings }
  348. ResourceStrings.free;
  349. end;
  350. function def_cgsize(def: tdef): tcgsize;
  351. begin
  352. case def.deftype of
  353. orddef,
  354. enumdef,
  355. setdef:
  356. begin
  357. result := int_cgsize(def.size);
  358. if is_signed(def) then
  359. result := tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  360. end;
  361. classrefdef,
  362. pointerdef,
  363. procvardef:
  364. result := OS_ADDR;
  365. stringdef :
  366. begin
  367. if is_ansistring(def) or is_widestring(def) then
  368. result := OS_ADDR
  369. else
  370. result := OS_NO;
  371. end;
  372. objectdef :
  373. begin
  374. if is_class_or_interface(def) then
  375. result := OS_ADDR
  376. else
  377. result := OS_NO;
  378. end;
  379. floatdef:
  380. result := tfloat2tcgsize[tfloatdef(def).typ];
  381. recorddef :
  382. result:=int_cgsize(def.size);
  383. arraydef :
  384. begin
  385. if not is_special_array(def) then
  386. result := int_cgsize(def.size)
  387. else
  388. result := OS_NO;
  389. end;
  390. else
  391. begin
  392. { undefined size }
  393. result:=OS_NO;
  394. end;
  395. end;
  396. end;
  397. function int_cgsize(const l: aword): tcgsize;
  398. begin
  399. case l of
  400. 1 :
  401. result := OS_8;
  402. 2 :
  403. result := OS_16;
  404. 3,4 :
  405. result := OS_32;
  406. 5..8 :
  407. result := OS_64;
  408. else
  409. result:=OS_NO;
  410. end;
  411. end;
  412. function inverse_opcmp(opcmp: topcmp): topcmp;
  413. const
  414. list: array[TOpCmp] of TOpCmp =
  415. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  416. OC_B,OC_BE);
  417. begin
  418. inverse_opcmp := list[opcmp];
  419. end;
  420. function commutativeop(op: topcg): boolean;
  421. const
  422. list: array[topcg] of boolean =
  423. (true,true,true,false,false,true,true,false,false,
  424. true,false,false,false,false,true);
  425. begin
  426. commutativeop := list[op];
  427. end;
  428. {$ifdef fixLeaksOnError}
  429. procedure hcodegen_do_stop;
  430. var p: pprocinfo;
  431. begin
  432. p := pprocinfo(procinfoStack.pop);
  433. while p <> nil Do
  434. begin
  435. dispose(p,done);
  436. p := pprocinfo(procinfoStack.pop);
  437. end;
  438. procinfoStack.done;
  439. do_stop := hcodegen_old_do_stop;
  440. do_stop{$ifdef FPCPROCVAR}(){$endif};
  441. end;
  442. begin
  443. hcodegen_old_do_stop := do_stop;
  444. do_stop := {$ifdef FPCPROCVAR}@{$endif}hcodegen_do_stop;
  445. procinfoStack.init;
  446. {$endif fixLeaksOnError}
  447. end.
  448. {
  449. $Log$
  450. Revision 1.19 2002-07-20 11:57:53 florian
  451. * types.pas renamed to defbase.pas because D6 contains a types
  452. unit so this would conflicts if D6 programms are compiled
  453. + Willamette/SSE2 instructions to assembler added
  454. Revision 1.18 2002/07/01 18:46:22 peter
  455. * internal linker
  456. * reorganized aasm layer
  457. Revision 1.17 2002/05/20 13:30:40 carl
  458. * bugfix of hdisponen (base must be set, not index)
  459. * more portability fixes
  460. Revision 1.16 2002/05/18 13:34:05 peter
  461. * readded missing revisions
  462. Revision 1.15 2002/05/16 19:46:35 carl
  463. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  464. + try to fix temp allocation (still in ifdef)
  465. + generic constructor calls
  466. + start of tassembler / tmodulebase class cleanup
  467. Revision 1.13 2002/04/25 20:16:38 peter
  468. * moved more routines from cga/n386util
  469. Revision 1.12 2002/04/21 15:28:06 carl
  470. - remove duplicate constants
  471. - move some constants to cginfo
  472. Revision 1.11 2002/04/20 21:32:23 carl
  473. + generic FPC_CHECKPOINTER
  474. + first parameter offset in stack now portable
  475. * rename some constants
  476. + move some cpu stuff to other units
  477. - remove unused constents
  478. * fix stacksize for some targets
  479. * fix generic size problems which depend now on EXTEND_SIZE constant
  480. Revision 1.10 2002/04/07 09:13:39 carl
  481. + documentation
  482. - remove unused variables
  483. Revision 1.9 2002/04/04 19:05:54 peter
  484. * removed unused units
  485. * use tlocation.size in cg.a_*loc*() routines
  486. Revision 1.8 2002/04/02 17:11:27 peter
  487. * tlocation,treference update
  488. * LOC_CONSTANT added for better constant handling
  489. * secondadd splitted in multiple routines
  490. * location_force_reg added for loading a location to a register
  491. of a specified size
  492. * secondassignment parses now first the right and then the left node
  493. (this is compatible with Kylix). This saves a lot of push/pop especially
  494. with string operations
  495. * adapted some routines to use the new cg methods
  496. Revision 1.7 2002/03/31 20:26:33 jonas
  497. + a_loadfpu_* and a_loadmm_* methods in tcg
  498. * register allocation is now handled by a class and is mostly processor
  499. independent (+rgobj.pas and i386/rgcpu.pas)
  500. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  501. * some small improvements and fixes to the optimizer
  502. * some register allocation fixes
  503. * some fpuvaroffset fixes in the unary minus node
  504. * push/popusedregisters is now called rg.save/restoreusedregisters and
  505. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  506. also better optimizable)
  507. * fixed and optimized register saving/restoring for new/dispose nodes
  508. * LOC_FPU locations now also require their "register" field to be set to
  509. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  510. - list field removed of the tnode class because it's not used currently
  511. and can cause hard-to-find bugs
  512. Revision 1.6 2002/03/04 19:10:11 peter
  513. * removed compiler warnings
  514. }