cgbase.pas 21 KB

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