psub.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing and codegeneration at subroutine level
  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 psub;
  19. {$i fpcdefs.inc}
  20. interface
  21. procedure printnode_reset;
  22. procedure compile_proc_body(make_global,parent_has_class:boolean);
  23. { reads the declaration blocks }
  24. procedure read_declarations(islibrary : boolean);
  25. { reads declarations in the interface part of a unit }
  26. procedure read_interface_declarations;
  27. implementation
  28. uses
  29. { common }
  30. cutils,cclasses,
  31. { global }
  32. globtype,globals,tokens,verbose,comphook,
  33. systems,
  34. { aasm }
  35. cpubase,cpuinfo,aasmbase,aasmtai,
  36. { symtable }
  37. symconst,symbase,symdef,symsym,symtype,symtable,defutil,
  38. paramgr,
  39. ppu,fmodule,
  40. { pass 1 }
  41. node,
  42. nbas,nld,
  43. pass_1,
  44. {$ifdef state_tracking}
  45. nstate,
  46. {$endif state_tracking}
  47. { pass 2 }
  48. {$ifndef NOPASS2}
  49. pass_2,
  50. {$endif}
  51. { parser }
  52. scanner,
  53. pbase,pstatmnt,pdecl,pdecsub,pexports,
  54. { codegen }
  55. tgobj,cgbase,rgobj,rgcpu,
  56. ncgutil
  57. {$ifndef NOOPT}
  58. {$ifdef i386}
  59. ,aopt386
  60. {$else i386}
  61. ,aoptcpu
  62. {$endif i386}
  63. {$endif}
  64. ;
  65. {****************************************************************************
  66. PROCEDURE/FUNCTION BODY PARSING
  67. ****************************************************************************}
  68. procedure initializevars(p:tnamedindexitem;arg:pointer);
  69. var
  70. b : tblocknode;
  71. begin
  72. if tsym(p).typ<>varsym then
  73. exit;
  74. with tvarsym(p) do
  75. begin
  76. if assigned(defaultconstsym) then
  77. begin
  78. b:=tblocknode(arg);
  79. b.left:=cstatementnode.create(
  80. cassignmentnode.create(
  81. cloadnode.create(tsym(p),tsym(p).owner),
  82. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  83. b.left);
  84. end;
  85. end;
  86. end;
  87. function block(islibrary : boolean) : tnode;
  88. begin
  89. { parse const,types and vars }
  90. read_declarations(islibrary);
  91. procinfo.handle_body_start;
  92. { do we have an assembler block without the po_assembler?
  93. we should allow this for Delphi compatibility (PFV) }
  94. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  95. include(aktprocdef.procoptions,po_assembler);
  96. { Handle assembler block different }
  97. if (po_assembler in aktprocdef.procoptions) then
  98. begin
  99. block:=assembler_block;
  100. exit;
  101. end;
  102. {Unit initialization?.}
  103. if (lexlevel=unit_init_level) and (current_module.is_unit)
  104. or islibrary then
  105. begin
  106. if (token=_END) then
  107. begin
  108. consume(_END);
  109. { We need at least a node, else the entry/exit code is not
  110. generated and thus no PASCALMAIN symbol which we need (PFV) }
  111. if islibrary then
  112. block:=cnothingnode.create
  113. else
  114. block:=nil;
  115. end
  116. else
  117. begin
  118. if token=_INITIALIZATION then
  119. begin
  120. { The library init code is already called and does not
  121. need to be in the initfinal table (PFV) }
  122. if not islibrary then
  123. current_module.flags:=current_module.flags or uf_init;
  124. block:=statement_block(_INITIALIZATION);
  125. end
  126. else if (token=_FINALIZATION) then
  127. begin
  128. if (current_module.flags and uf_finalize)<>0 then
  129. block:=statement_block(_FINALIZATION)
  130. else
  131. begin
  132. { can we allow no INITIALIZATION for DLL ??
  133. I think it should work PM }
  134. block:=nil;
  135. exit;
  136. end;
  137. end
  138. else
  139. begin
  140. { The library init code is already called and does not
  141. need to be in the initfinal table (PFV) }
  142. if not islibrary then
  143. current_module.flags:=current_module.flags or uf_init;
  144. block:=statement_block(_BEGIN);
  145. end;
  146. end;
  147. end
  148. else
  149. begin
  150. block:=statement_block(_BEGIN);
  151. if symtablestack.symtabletype=localsymtable then
  152. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  153. end;
  154. end;
  155. {****************************************************************************
  156. PROCEDURE/FUNCTION COMPILING
  157. ****************************************************************************}
  158. procedure printnode_reset;
  159. begin
  160. assign(printnodefile,treelogfilename);
  161. {$I-}
  162. rewrite(printnodefile);
  163. {$I+}
  164. if ioresult<>0 then
  165. begin
  166. Comment(V_Error,'Error creating '+treelogfilename);
  167. exit;
  168. end;
  169. close(printnodefile);
  170. end;
  171. procedure printnode_procdef(pd:tprocdef);
  172. begin
  173. assign(printnodefile,treelogfilename);
  174. {$I-}
  175. append(printnodefile);
  176. if ioresult<>0 then
  177. rewrite(printnodefile);
  178. {$I+}
  179. if ioresult<>0 then
  180. begin
  181. Comment(V_Error,'Error creating '+treelogfilename);
  182. exit;
  183. end;
  184. writeln(printnodefile);
  185. writeln(printnodefile,'*******************************************************************************');
  186. writeln(printnodefile,aktprocdef.fullprocname(false));
  187. writeln(printnodefile,'*******************************************************************************');
  188. printnode(printnodefile,pd.code);
  189. close(printnodefile);
  190. end;
  191. procedure compile_proc_body(make_global,parent_has_class:boolean);
  192. {
  193. Compile the body of a procedure
  194. }
  195. var
  196. oldexitlabel,oldexit2label : tasmlabel;
  197. oldfaillabel,oldquickexitlabel:tasmlabel;
  198. _class,hp:tobjectdef;
  199. { switches can change inside the procedure }
  200. entryswitches, exitswitches : tlocalswitches;
  201. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  202. { code for the subroutine as tree }
  203. code:tnode;
  204. { true when no stackframe is required }
  205. nostackframe:boolean;
  206. { number of bytes which have to be cleared by RET }
  207. parasize:longint;
  208. { filepositions }
  209. entrypos,
  210. savepos,
  211. exitpos : tfileposinfo;
  212. begin
  213. { calculate the lexical level }
  214. inc(lexlevel);
  215. if lexlevel>maxnesting then
  216. Message(parser_e_too_much_lexlevel);
  217. { static is also important for local procedures !! }
  218. if (po_staticmethod in aktprocdef.procoptions) then
  219. allow_only_static:=true
  220. else if (lexlevel=normal_function_level) then
  221. allow_only_static:=false;
  222. { save old labels }
  223. oldexitlabel:=aktexitlabel;
  224. oldexit2label:=aktexit2label;
  225. oldquickexitlabel:=quickexitlabel;
  226. oldfaillabel:=faillabel;
  227. { get new labels }
  228. objectlibrary.getlabel(aktexitlabel);
  229. objectlibrary.getlabel(aktexit2label);
  230. { exit for fail in constructors }
  231. if (aktprocdef.proctypeoption=potype_constructor) then
  232. begin
  233. objectlibrary.getlabel(faillabel);
  234. objectlibrary.getlabel(quickexitlabel);
  235. end;
  236. { reset break and continue labels }
  237. block_type:=bt_general;
  238. aktbreaklabel:=nil;
  239. aktcontinuelabel:=nil;
  240. {$ifdef state_tracking}
  241. { aktstate:=Tstate_storage.create;}
  242. {$endif state_tracking}
  243. { insert symtables for the class, by only if it is no nested function }
  244. if assigned(procinfo._class) and not(parent_has_class) then
  245. begin
  246. { insert them in the reverse order ! }
  247. hp:=nil;
  248. repeat
  249. _class:=procinfo._class;
  250. while _class.childof<>hp do
  251. _class:=_class.childof;
  252. hp:=_class;
  253. _class.symtable.next:=symtablestack;
  254. symtablestack:=_class.symtable;
  255. until hp=procinfo._class;
  256. end;
  257. { insert parasymtable in symtablestack}
  258. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  259. for checking of same names used in interface and implementation !! }
  260. if lexlevel>=normal_function_level then
  261. begin
  262. aktprocdef.parast.next:=symtablestack;
  263. symtablestack:=aktprocdef.parast;
  264. symtablestack.symtablelevel:=lexlevel;
  265. end;
  266. { create a local symbol table for this routine }
  267. if not assigned(aktprocdef.localst) then
  268. aktprocdef.insert_localst;
  269. { insert localsymtable in symtablestack}
  270. aktprocdef.localst.next:=symtablestack;
  271. symtablestack:=aktprocdef.localst;
  272. symtablestack.symtablelevel:=lexlevel;
  273. { constant symbols are inserted in this symboltable }
  274. constsymtable:=symtablestack;
  275. { reset the temporary memory }
  276. rg.cleartempgen;
  277. rg.usedinproc:=[];
  278. rg.usedbyproc:=[];
  279. { save entry info }
  280. entrypos:=aktfilepos;
  281. entryswitches:=aktlocalswitches;
  282. localmaxfpuregisters:=aktmaxfpuregisters;
  283. { parse the code ... }
  284. code:=block(current_module.islibrary);
  285. { store a copy of the original tree for inline, for
  286. normal procedures only store a reference to the
  287. current tree }
  288. if (aktprocdef.proccalloption=pocall_inline) then
  289. aktprocdef.code:=code.getcopy
  290. else
  291. aktprocdef.code:=code;
  292. { get a better entry point }
  293. if assigned(code) then
  294. entrypos:=code.fileinfo;
  295. { save exit info }
  296. exitswitches:=aktlocalswitches;
  297. exitpos:=last_endtoken_filepos;
  298. { save current filepos }
  299. savepos:=aktfilepos;
  300. {When we are called to compile the body of a unit, aktprocsym should
  301. point to the unit initialization. If the unit has no initialization,
  302. aktprocsym=nil. But in that case code=nil. Thus we should check for
  303. code=nil, when we use aktprocsym.}
  304. { set the start offset to the start of the temp area in the stack }
  305. tg.setfirsttemp(procinfo.firsttemp_offset);
  306. { ... and generate assembler }
  307. { but set the right switches for entry !! }
  308. aktlocalswitches:=entryswitches;
  309. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  310. aktmaxfpuregisters:=localmaxfpuregisters;
  311. if assigned(code) then
  312. begin
  313. { the procedure is now defined }
  314. aktprocdef.forwarddef:=false;
  315. if paraprintnodetree=1 then
  316. printnode_procdef(aktprocdef);
  317. { only generate the code if no type errors are found, else
  318. finish at least the type checking pass }
  319. {$ifndef NOPASS2}
  320. if (status.errorcount=0) then
  321. begin
  322. generatecode(code);
  323. { first generate entry code with the correct position and switches }
  324. aktfilepos:=entrypos;
  325. aktlocalswitches:=entryswitches;
  326. genentrycode(procinfo.aktentrycode,make_global,0,parasize,nostackframe,false);
  327. { FPC_POPADDRSTACK destroys all registers (JM) }
  328. if (procinfo.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  329. begin
  330. rg.usedinproc := ALL_REGISTERS;
  331. end;
  332. { now generate exit code with the correct position and switches }
  333. aktfilepos:=exitpos;
  334. aktlocalswitches:=exitswitches;
  335. genexitcode(procinfo.aktexitcode,parasize,nostackframe,false);
  336. { now all the registers used are known }
  337. aktprocdef.usedintregisters:=rg.usedintinproc;
  338. aktprocdef.usedotherregisters:=rg.usedinproc;
  339. procinfo.aktproccode.insertlist(procinfo.aktentrycode);
  340. procinfo.aktproccode.concatlist(procinfo.aktexitcode);
  341. {$ifdef newra}
  342. { rg.writegraph;}
  343. {$endif}
  344. if not(cs_no_regalloc in aktglobalswitches) then
  345. begin
  346. {$ifdef newra}
  347. {Do register allocation.}
  348. repeat
  349. rg.prepare_colouring;
  350. rg.colour_registers;
  351. rg.epilogue_colouring;
  352. {Are there spilled registers? We cannot do that yet.}
  353. if rg.spillednodes<>'' then
  354. internalerror(200304221);
  355. {if not try_fast_spill(rg) then
  356. slow_spill(rg);
  357. }
  358. until rg.spillednodes='';
  359. procinfo.aktproccode.translate_registers(rg.colour);
  360. procinfo.aktproccode.convert_registers;
  361. {$else newra}
  362. procinfo.aktproccode.convert_registers;
  363. {$ifndef NoOpt}
  364. if (cs_optimize in aktglobalswitches) and
  365. { do not optimize pure assembler procedures }
  366. ((procinfo.flags and pi_is_assembler)=0) then
  367. optimize(procinfo.aktproccode);
  368. {$endif NoOpt}
  369. {$endif newra}
  370. end;
  371. { save local data (casetable) also in the same file }
  372. if assigned(procinfo.aktlocaldata) and
  373. (not procinfo.aktlocaldata.empty) then
  374. begin
  375. procinfo.aktproccode.concat(Tai_section.Create(sec_data));
  376. procinfo.aktproccode.concatlist(procinfo.aktlocaldata);
  377. procinfo.aktproccode.concat(Tai_section.Create(sec_code));
  378. end;
  379. { add the procedure to the codesegment }
  380. if (cs_create_smart in aktmoduleswitches) then
  381. codeSegment.concat(Tai_cut.Create);
  382. codeSegment.concatlist(procinfo.aktproccode);
  383. end
  384. else
  385. do_resulttypepass(code);
  386. {$else NOPASS2}
  387. do_resulttypepass(code);
  388. {$endif NOPASS2}
  389. end;
  390. { ... remove symbol tables }
  391. if lexlevel>=normal_function_level then
  392. symtablestack:=symtablestack.next.next
  393. else
  394. symtablestack:=symtablestack.next;
  395. { ... check for unused symbols }
  396. { but only if there is no asm block }
  397. if assigned(code) then
  398. begin
  399. if (Errorcount=0) then
  400. begin
  401. { check if forwards are resolved }
  402. tstoredsymtable(aktprocdef.localst).check_forwards;
  403. { check if all labels are used }
  404. tstoredsymtable(aktprocdef.localst).checklabels;
  405. { remove cross unit overloads }
  406. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  407. end;
  408. if (procinfo.flags and pi_uses_asm)=0 then
  409. begin
  410. { not for unit init, becuase the var can be used in finalize,
  411. it will be done in proc_unit }
  412. if not(aktprocdef.proctypeoption
  413. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  414. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  415. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  416. end;
  417. end;
  418. { the local symtables can be deleted, but the parast }
  419. { doesn't, (checking definitons when calling a }
  420. { function }
  421. { not for a inline procedure !! (PM) }
  422. { at lexlevel = 1 localst is the staticsymtable itself }
  423. { so no dispose here !! }
  424. if assigned(code) and
  425. not(cs_browser in aktmoduleswitches) and
  426. (aktprocdef.proccalloption<>pocall_inline) then
  427. begin
  428. if lexlevel>=normal_function_level then
  429. begin
  430. aktprocdef.localst.free;
  431. end;
  432. aktprocdef.localst:=nil;
  433. end;
  434. { all registers can be used again }
  435. rg.resetusableregisters;
  436. { only now we can remove the temps }
  437. tg.resettempgen;
  438. { remove code tree, if not inline procedure }
  439. if assigned(code) then
  440. begin
  441. { the inline procedure has already got a copy of the tree
  442. stored in aktprocdef.code }
  443. code.free;
  444. if (aktprocdef.proccalloption<>pocall_inline) then
  445. aktprocdef.code:=nil;
  446. end;
  447. { remove class member symbol tables }
  448. while symtablestack.symtabletype=objectsymtable do
  449. symtablestack:=symtablestack.next;
  450. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  451. {$ifdef state_tracking}
  452. { aktstate.destroy;}
  453. {$endif state_tracking}
  454. { restore filepos, the switches are already set }
  455. aktfilepos:=savepos;
  456. { restore labels }
  457. aktexitlabel:=oldexitlabel;
  458. aktexit2label:=oldexit2label;
  459. quickexitlabel:=oldquickexitlabel;
  460. faillabel:=oldfaillabel;
  461. { reset to normal non static function }
  462. if (lexlevel=normal_function_level) then
  463. allow_only_static:=false;
  464. { previous lexlevel }
  465. dec(lexlevel);
  466. end;
  467. {****************************************************************************
  468. PROCEDURE/FUNCTION PARSING
  469. ****************************************************************************}
  470. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  471. var
  472. vs : tvarsym;
  473. s : string;
  474. begin
  475. if tsym(p).typ<>varsym then
  476. exit;
  477. with tvarsym(p) do
  478. begin
  479. if copy(name,1,3)='val' then
  480. begin
  481. s:=Copy(name,4,255);
  482. if not(po_assembler in aktprocdef.procoptions) then
  483. begin
  484. vs:=tvarsym.create(s,vartype);
  485. vs.fileinfo:=fileinfo;
  486. vs.varspez:=varspez;
  487. if not assigned(aktprocdef.localst) then
  488. aktprocdef.insert_localst;
  489. aktprocdef.localst.insert(vs);
  490. aktprocdef.localst.insertvardata(vs);
  491. include(vs.varoptions,vo_is_local_copy);
  492. vs.varstate:=vs_assigned;
  493. localvarsym:=vs;
  494. inc(refs); { the para was used to set the local copy ! }
  495. { warnings only on local copy ! }
  496. varstate:=vs_used;
  497. end
  498. else
  499. begin
  500. aktprocdef.parast.rename(name,s);
  501. end;
  502. end;
  503. end;
  504. end;
  505. procedure read_proc;
  506. {
  507. Parses the procedure directives, then parses the procedure body, then
  508. generates the code for it
  509. }
  510. var
  511. oldprocsym : tprocsym;
  512. oldprocdef : tprocdef;
  513. oldprocinfo : tprocinfo;
  514. oldconstsymtable : tsymtable;
  515. oldfilepos : tfileposinfo;
  516. oldselftokenmode,
  517. oldfailtokenmode : tmodeswitch;
  518. pdflags : word;
  519. begin
  520. { save old state }
  521. oldprocdef:=aktprocdef;
  522. oldprocsym:=aktprocsym;
  523. oldconstsymtable:=constsymtable;
  524. oldprocinfo:=procinfo;
  525. { create a new procedure }
  526. codegen_newprocedure;
  527. with procinfo do
  528. begin
  529. parent:=oldprocinfo;
  530. { clear flags }
  531. flags:=0;
  532. { standard frame pointer }
  533. framepointer.enum:=R_INTREGISTER;
  534. framepointer.number:=NR_FRAME_POINTER_REG;
  535. { is this a nested function of a method ? }
  536. if assigned(oldprocinfo) then
  537. _class:=oldprocinfo._class;
  538. end;
  539. parse_proc_dec;
  540. procinfo.procdef:=aktprocdef;
  541. { set the default function options }
  542. if parse_only then
  543. begin
  544. aktprocdef.forwarddef:=true;
  545. { set also the interface flag, for better error message when the
  546. implementation doesn't much this header }
  547. aktprocdef.interfacedef:=true;
  548. pdflags:=pd_interface;
  549. end
  550. else
  551. begin
  552. pdflags:=pd_body;
  553. if (not current_module.in_interface) then
  554. pdflags:=pdflags or pd_implemen;
  555. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  556. pdflags:=pdflags or pd_global;
  557. procinfo.exported:=false;
  558. aktprocdef.forwarddef:=false;
  559. end;
  560. { parse the directives that may follow }
  561. inc(lexlevel);
  562. parse_proc_directives(pdflags);
  563. dec(lexlevel);
  564. { hint directives, these can be separated by semicolons here,
  565. that need to be handled here with a loop (PFV) }
  566. while try_consume_hintdirective(aktprocsym.symoptions) do
  567. Consume(_SEMICOLON);
  568. { set aktfilepos to the beginning of the function declaration }
  569. oldfilepos:=aktfilepos;
  570. aktfilepos:=aktprocdef.fileinfo;
  571. { For varargs directive also cdecl and external must be defined }
  572. if (po_varargs in aktprocdef.procoptions) then
  573. begin
  574. { check first for external in the interface, if available there
  575. then the cdecl must also be there since there is no implementation
  576. available to contain it }
  577. if parse_only then
  578. begin
  579. { if external is available, then cdecl must also be available }
  580. if (po_external in aktprocdef.procoptions) and
  581. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  582. Message(parser_e_varargs_need_cdecl_and_external);
  583. end
  584. else
  585. begin
  586. { both must be defined now }
  587. if not(po_external in aktprocdef.procoptions) or
  588. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  589. Message(parser_e_varargs_need_cdecl_and_external);
  590. end;
  591. end;
  592. { search for forward declarations }
  593. if not proc_add_definition(aktprocsym,aktprocdef) then
  594. begin
  595. { A method must be forward defined (in the object declaration) }
  596. if assigned(procinfo._class) and
  597. (not assigned(oldprocinfo._class)) then
  598. begin
  599. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname(false));
  600. aktprocsym.write_parameter_lists(aktprocdef);
  601. end
  602. else
  603. begin
  604. { Give a better error if there is a forward def in the interface and only
  605. a single implementation }
  606. if (not aktprocdef.forwarddef) and
  607. (not aktprocdef.interfacedef) and
  608. (aktprocsym.procdef_count>1) and
  609. aktprocsym.first_procdef.forwarddef and
  610. aktprocsym.first_procdef.interfacedef and
  611. not(aktprocsym.procdef_count>2) then
  612. begin
  613. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname(false));
  614. aktprocsym.write_parameter_lists(aktprocdef);
  615. end
  616. else
  617. begin
  618. { check the global flag, for delphi this is not
  619. required }
  620. if not(m_delphi in aktmodeswitches) and
  621. ((procinfo.flags and pi_is_global)<>0) then
  622. Message(parser_e_overloaded_must_be_all_global);
  623. end;
  624. end;
  625. end;
  626. { restore file pos }
  627. aktfilepos:=oldfilepos;
  628. { update procinfo, because the aktprocdef can be
  629. changed by check_identical_proc (PFV) }
  630. procinfo.procdef:=aktprocdef;
  631. { compile procedure when a body is needed }
  632. if (pdflags and pd_body)<>0 then
  633. begin
  634. Message1(parser_d_procedure_start,aktprocdef.fullprocname(false));
  635. if assigned(aktprocsym.owner) then
  636. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  637. { Insert result variables in the localst }
  638. insert_funcret_local(aktprocdef);
  639. { when it is a value para and it needs a local copy then rename
  640. the parameter and insert a copy in the localst. This is not done
  641. for assembler procedures }
  642. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  643. { calculate addresses in parasymtable }
  644. aktprocdef.parast.address_fixup:=procinfo.para_offset;
  645. calc_parasymtable_addresses(aktprocdef);
  646. {$ifdef i386}
  647. { add implicit pushes for interrupt routines }
  648. if (po_interrupt in aktprocdef.procoptions) then
  649. procinfo.allocate_interrupt_stackframe;
  650. {$endif i386}
  651. procinfo.after_header;
  652. { set _FAIL as keyword if constructor }
  653. if (aktprocdef.proctypeoption=potype_constructor) then
  654. begin
  655. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  656. tokeninfo^[_FAIL].keyword:=m_all;
  657. end;
  658. { set _SELF as keyword if methods }
  659. if assigned(aktprocdef._class) then
  660. begin
  661. oldselftokenmode:=tokeninfo^[_SELF].keyword;
  662. tokeninfo^[_SELF].keyword:=m_all;
  663. end;
  664. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo._class));
  665. { reset _FAIL as _SELF normal }
  666. if (aktprocdef.proctypeoption=potype_constructor) then
  667. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  668. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  669. tokeninfo^[_SELF].keyword:=oldselftokenmode;
  670. consume(_SEMICOLON);
  671. end;
  672. { close }
  673. codegen_doneprocedure;
  674. { Restore old state }
  675. constsymtable:=oldconstsymtable;
  676. { release procsym when it was not stored in the symtable }
  677. if not assigned(aktprocsym.owner) then
  678. begin
  679. aktprocsym.free;
  680. aktprocdef.procsym:=nil;
  681. end;
  682. aktprocsym:=oldprocsym;
  683. aktprocdef:=oldprocdef;
  684. procinfo:=oldprocinfo;
  685. end;
  686. {****************************************************************************
  687. DECLARATION PARSING
  688. ****************************************************************************}
  689. { search in symtablestack for not complete classes }
  690. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  691. begin
  692. if (tsym(p).typ=typesym) and
  693. (ttypesym(p).restype.def.deftype=objectdef) and
  694. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  695. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  696. end;
  697. procedure read_declarations(islibrary : boolean);
  698. procedure Not_supported_for_inline(t : ttoken);
  699. begin
  700. if assigned(aktprocsym) and
  701. (aktprocdef.proccalloption=pocall_inline) then
  702. Begin
  703. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  704. Message(parser_w_inlining_disabled);
  705. aktprocdef.proccalloption:=pocall_fpccall;
  706. End;
  707. end;
  708. begin
  709. repeat
  710. case token of
  711. _LABEL:
  712. begin
  713. Not_supported_for_inline(token);
  714. label_dec;
  715. end;
  716. _CONST:
  717. begin
  718. Not_supported_for_inline(token);
  719. const_dec;
  720. end;
  721. _TYPE:
  722. begin
  723. Not_supported_for_inline(token);
  724. type_dec;
  725. end;
  726. _VAR:
  727. var_dec;
  728. _THREADVAR:
  729. threadvar_dec;
  730. _CONSTRUCTOR,_DESTRUCTOR,
  731. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  732. begin
  733. Not_supported_for_inline(token);
  734. read_proc;
  735. end;
  736. _RESOURCESTRING:
  737. resourcestring_dec;
  738. _EXPORTS:
  739. begin
  740. Not_supported_for_inline(token);
  741. { here we should be at lexlevel 1, no ? PM }
  742. if (lexlevel<>main_program_level) or
  743. (current_module.is_unit) then
  744. begin
  745. Message(parser_e_syntax_error);
  746. consume_all_until(_SEMICOLON);
  747. end
  748. else if islibrary or
  749. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  750. read_exports
  751. else
  752. begin
  753. Message(parser_w_unsupported_feature);
  754. consume(_BEGIN);
  755. end;
  756. end
  757. else break;
  758. end;
  759. until false;
  760. { check for incomplete class definitions, this is only required
  761. for fpc modes }
  762. if (m_fpc in aktmodeswitches) then
  763. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  764. end;
  765. procedure read_interface_declarations;
  766. begin
  767. {Since the body is now parsed at lexlevel 1, and the declarations
  768. must be parsed at the same lexlevel we increase the lexlevel.}
  769. inc(lexlevel);
  770. repeat
  771. case token of
  772. _CONST :
  773. const_dec;
  774. _TYPE :
  775. type_dec;
  776. _VAR :
  777. var_dec;
  778. _THREADVAR :
  779. threadvar_dec;
  780. _RESOURCESTRING:
  781. resourcestring_dec;
  782. _FUNCTION,
  783. _PROCEDURE,
  784. _OPERATOR :
  785. read_proc;
  786. else
  787. break;
  788. end;
  789. until false;
  790. dec(lexlevel);
  791. { check for incomplete class definitions, this is only required
  792. for fpc modes }
  793. if (m_fpc in aktmodeswitches) then
  794. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  795. end;
  796. end.
  797. {
  798. $Log$
  799. Revision 1.105 2003-04-26 00:31:42 peter
  800. * set return_offset moved to after_header
  801. Revision 1.104 2003/04/25 20:59:34 peter
  802. * removed funcretn,funcretsym, function result is now in varsym
  803. and aliases for result and function name are added using absolutesym
  804. * vs_hidden parameter for funcret passed in parameter
  805. * vs_hidden fixes
  806. * writenode changed to printnode and released from extdebug
  807. * -vp option added to generate a tree.log with the nodetree
  808. * nicer printnode for statements, callnode
  809. Revision 1.103 2003/04/24 13:03:01 florian
  810. * comp is now written with its bit pattern to the ppu instead as an extended
  811. Revision 1.102 2003/04/23 12:35:34 florian
  812. * fixed several issues with powerpc
  813. + applied a patch from Jonas for nested function calls (PowerPC only)
  814. * ...
  815. Revision 1.101 2003/04/22 14:33:38 peter
  816. * removed some notes/hints
  817. Revision 1.100 2003/04/22 13:47:08 peter
  818. * fixed C style array of const
  819. * fixed C array passing
  820. * fixed left to right with high parameters
  821. Revision 1.99 2003/04/22 10:09:35 daniel
  822. + Implemented the actual register allocator
  823. + Scratch registers unavailable when new register allocator used
  824. + maybe_save/maybe_restore unavailable when new register allocator used
  825. Revision 1.98 2003/04/17 07:50:24 daniel
  826. * Some work on interference graph construction
  827. Revision 1.97 2003/04/16 09:26:55 jonas
  828. * assembler procedures now again get a stackframe if they have local
  829. variables. No space is reserved for a function result however.
  830. Also, the register parameters aren't automatically saved on the stack
  831. anymore in assembler procedures.
  832. Revision 1.96 2003/04/05 21:09:31 jonas
  833. * several ppc/generic result offset related fixes. The "normal" result
  834. offset seems now to be calculated correctly and a lot of duplicate
  835. calculations have been removed. Nested functions accessing the parent's
  836. function result don't work at all though :(
  837. Revision 1.95 2003/04/02 16:11:34 peter
  838. * give error when exports is not supported
  839. Revision 1.94 2003/03/12 22:43:38 jonas
  840. * more powerpc and generic fixes related to the new register allocator
  841. Revision 1.93 2003/03/08 08:59:07 daniel
  842. + $define newra will enable new register allocator
  843. + getregisterint will return imaginary registers with $newra
  844. + -sr switch added, will skip register allocation so you can see
  845. the direct output of the code generator before register allocation
  846. Revision 1.92 2003/02/19 22:00:14 daniel
  847. * Code generator converted to new register notation
  848. - Horribily outdated todo.txt removed
  849. Revision 1.91 2003/01/09 21:52:37 peter
  850. * merged some verbosity options.
  851. * V_LineInfo is a verbosity flag to include line info
  852. Revision 1.90 2003/01/09 20:40:59 daniel
  853. * Converted some code in cgx86.pas to new register numbering
  854. Revision 1.89 2003/01/09 15:49:56 daniel
  855. * Added register conversion
  856. Revision 1.88 2003/01/08 18:43:56 daniel
  857. * Tregister changed into a record
  858. Revision 1.87 2003/01/03 20:35:08 peter
  859. * check also interfacedef when checking for matching forwarddef
  860. Revision 1.86 2003/01/02 11:14:02 michael
  861. + Patch from peter to support initial values for local variables
  862. Revision 1.85 2002/12/29 18:59:34 peter
  863. * fixed parsing of declarations before asm statement
  864. Revision 1.84 2002/12/29 18:25:18 peter
  865. * parse declarations before check _ASM token
  866. Revision 1.83 2002/12/29 14:57:50 peter
  867. * unit loading changed to first register units and load them
  868. afterwards. This is needed to support uses xxx in yyy correctly
  869. * unit dependency check fixed
  870. Revision 1.82 2002/12/25 01:26:56 peter
  871. * duplicate procsym-unitsym fix
  872. Revision 1.81 2002/12/15 13:37:15 peter
  873. * don't include uf_init for library. The code is already called and
  874. does not need to be in the initfinal table
  875. Revision 1.80 2002/12/07 14:27:09 carl
  876. * 3% memory optimization
  877. * changed some types
  878. + added type checking with different size for call node and for
  879. parameters
  880. Revision 1.79 2002/11/25 18:43:32 carl
  881. - removed the invalid if <> checking (Delphi is strange on this)
  882. + implemented abstract warning on instance creation of class with
  883. abstract methods.
  884. * some error message cleanups
  885. Revision 1.78 2002/11/25 17:43:23 peter
  886. * splitted defbase in defutil,symutil,defcmp
  887. * merged isconvertable and is_equal into compare_defs(_ext)
  888. * made operator search faster by walking the list only once
  889. Revision 1.77 2002/11/23 22:50:06 carl
  890. * some small speed optimizations
  891. + added several new warnings/hints
  892. Revision 1.76 2002/11/18 17:31:58 peter
  893. * pass proccalloption to ret_in_xxx and push_xxx functions
  894. Revision 1.75 2002/11/17 16:31:57 carl
  895. * memory optimization (3-4%) : cleanup of tai fields,
  896. cleanup of tdef and tsym fields.
  897. * make it work for m68k
  898. Revision 1.74 2002/11/15 01:58:53 peter
  899. * merged changes from 1.0.7 up to 04-11
  900. - -V option for generating bug report tracing
  901. - more tracing for option parsing
  902. - errors for cdecl and high()
  903. - win32 import stabs
  904. - win32 records<=8 are returned in eax:edx (turned off by default)
  905. - heaptrc update
  906. - more info for temp management in .s file with EXTDEBUG
  907. Revision 1.73 2002/11/09 15:32:30 carl
  908. * noopt for non-i386 targets
  909. Revision 1.72 2002/09/10 20:31:48 florian
  910. * call to procinfo.after_header added
  911. Revision 1.71 2002/09/07 15:25:07 peter
  912. * old logs removed and tabs fixed
  913. Revision 1.70 2002/09/03 16:26:27 daniel
  914. * Make Tprocdef.defs protected
  915. Revision 1.69 2002/08/25 19:25:20 peter
  916. * sym.insert_in_data removed
  917. * symtable.insertvardata/insertconstdata added
  918. * removed insert_in_data call from symtable.insert, it needs to be
  919. called separatly. This allows to deref the address calculation
  920. * procedures now calculate the parast addresses after the procedure
  921. directives are parsed. This fixes the cdecl parast problem
  922. * push_addr_param has an extra argument that specifies if cdecl is used
  923. or not
  924. Revision 1.68 2002/08/17 09:23:41 florian
  925. * first part of procinfo rewrite
  926. Revision 1.67 2002/08/16 14:24:59 carl
  927. * issameref() to test if two references are the same (then emit no opcodes)
  928. + ret_in_reg to replace ret_in_acc
  929. (fix some register allocation bugs at the same time)
  930. + save_std_register now has an extra parameter which is the
  931. usedinproc registers
  932. Revision 1.66 2002/08/11 14:32:27 peter
  933. * renamed current_library to objectlibrary
  934. Revision 1.65 2002/08/11 13:24:13 peter
  935. * saving of asmsymbols in ppu supported
  936. * asmsymbollist global is removed and moved into a new class
  937. tasmlibrarydata that will hold the info of a .a file which
  938. corresponds with a single module. Added librarydata to tmodule
  939. to keep the library info stored for the module. In the future the
  940. objectfiles will also be stored to the tasmlibrarydata class
  941. * all getlabel/newasmsymbol and friends are moved to the new class
  942. Revision 1.64 2002/08/09 19:14:28 carl
  943. * fixed stackframe parameter (should only contain local size),
  944. set to zero currently
  945. Revision 1.63 2002/08/06 20:55:22 florian
  946. * first part of ppc calling conventions fix
  947. Revision 1.62 2002/07/26 21:15:41 florian
  948. * rewrote the system handling
  949. Revision 1.61 2002/07/20 11:57:56 florian
  950. * types.pas renamed to defbase.pas because D6 contains a types
  951. unit so this would conflicts if D6 programms are compiled
  952. + Willamette/SSE2 instructions to assembler added
  953. Revision 1.60 2002/07/19 11:41:36 daniel
  954. * State tracker work
  955. * The whilen and repeatn are now completely unified into whilerepeatn. This
  956. allows the state tracker to change while nodes automatically into
  957. repeat nodes.
  958. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  959. 'not(a>b)' is optimized into 'a<=b'.
  960. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  961. by removing the notn and later switchting the true and falselabels. The
  962. same is done with 'repeat until not a'.
  963. Revision 1.59 2002/07/15 18:03:15 florian
  964. * readded removed changes
  965. Revision 1.57 2002/07/11 14:41:28 florian
  966. * start of the new generic parameter handling
  967. Revision 1.58 2002/07/14 18:00:44 daniel
  968. + Added the beginning of a state tracker. This will track the values of
  969. variables through procedures and optimize things away.
  970. Revision 1.56 2002/07/07 09:52:32 florian
  971. * powerpc target fixed, very simple units can be compiled
  972. * some basic stuff for better callparanode handling, far from being finished
  973. Revision 1.55 2002/07/04 20:43:01 florian
  974. * first x86-64 patches
  975. Revision 1.54 2002/07/01 18:46:25 peter
  976. * internal linker
  977. * reorganized aasm layer
  978. Revision 1.53 2002/05/18 13:34:14 peter
  979. * readded missing revisions
  980. Revision 1.52 2002/05/16 19:46:44 carl
  981. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  982. + try to fix temp allocation (still in ifdef)
  983. + generic constructor calls
  984. + start of tassembler / tmodulebase class cleanup
  985. Revision 1.51 2002/05/14 19:34:49 peter
  986. * removed old logs and updated copyright year
  987. }