psub.pas 35 KB

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