psub.pas 34 KB

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