psub.pas 40 KB

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