psub.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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,aasm,
  35. { symtable }
  36. symconst,symbase,symdef,symsym,symtable,types,
  37. ppu,fmodule,
  38. { pass 1 }
  39. node,
  40. nbas,
  41. { pass 2 }
  42. {$ifndef NOPASS2}
  43. pass_1,pass_2,
  44. {$endif}
  45. { parser }
  46. scanner,
  47. pbase,pstatmnt,pdecl,pdecsub,pexports,
  48. { codegen }
  49. tgcpu,hcodegen
  50. {$ifdef newcg}
  51. ,cgbase
  52. ,cgobj
  53. {$ifndef NOOPT}
  54. ,aopt
  55. {$endif}
  56. {$else}
  57. ,temp_gen
  58. {$ifdef i386}
  59. ,cgai386
  60. {$ifndef NOOPT}
  61. ,aopt386
  62. {$endif}
  63. {$endif}
  64. {$endif}
  65. ;
  66. {****************************************************************************
  67. PROCEDURE/FUNCTION BODY PARSING
  68. ****************************************************************************}
  69. function block(islibrary : boolean) : tnode;
  70. var
  71. funcretsym : tfuncretsym;
  72. storepos : tfileposinfo;
  73. begin
  74. { do we have an assembler block without the po_assembler?
  75. we should allow this for Delphi compatibility (PFV) }
  76. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  77. include(aktprocsym.definition.procoptions,po_assembler);
  78. { Handle assembler block different }
  79. if (po_assembler in aktprocsym.definition.procoptions) then
  80. begin
  81. read_declarations(false);
  82. block:=assembler_block;
  83. exit;
  84. end;
  85. if not is_void(procinfo^.returntype.def) then
  86. begin
  87. { if the current is a function aktprocsym is non nil }
  88. { and there is a local symtable set }
  89. storepos:=akttokenpos;
  90. akttokenpos:=aktprocsym.fileinfo;
  91. funcretsym:=tfuncretsym.create(aktprocsym.name,procinfo);
  92. { insert in local symtable }
  93. symtablestack.insert(funcretsym);
  94. akttokenpos:=storepos;
  95. if ret_in_acc(procinfo^.returntype.def) or (procinfo^.returntype.def.deftype=floatdef) then
  96. procinfo^.return_offset:=-funcretsym.address;
  97. procinfo^.funcretsym:=funcretsym;
  98. { insert result also if support is on }
  99. if (m_result in aktmodeswitches) then
  100. begin
  101. procinfo^.resultfuncretsym:=tfuncretsym.create('RESULT',procinfo);
  102. symtablestack.insert(procinfo^.resultfuncretsym);
  103. end;
  104. end;
  105. read_declarations(islibrary);
  106. { temporary space is set, while the BEGIN of the procedure }
  107. if (symtablestack.symtabletype=localsymtable) then
  108. procinfo^.firsttemp_offset := -symtablestack.datasize
  109. else
  110. procinfo^.firsttemp_offset := 0;
  111. { space for the return value }
  112. { !!!!! this means that we can not set the return value
  113. in a subfunction !!!!! }
  114. { because we don't know yet where the address is }
  115. if not is_void(procinfo^.returntype.def) then
  116. begin
  117. if ret_in_acc(procinfo^.returntype.def) or (procinfo^.returntype.def.deftype=floatdef) then
  118. begin
  119. { the space has been set in the local symtable }
  120. procinfo^.return_offset:=-funcretsym.address;
  121. if ((procinfo^.flags and pi_operator)<>0) and
  122. assigned(otsym) then
  123. otsym.address:=-procinfo^.return_offset;
  124. { eax is modified by a function }
  125. {$ifndef newcg}
  126. {$ifdef i386}
  127. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  128. if is_64bitint(procinfo^.returntype.def) then
  129. usedinproc:=usedinproc or ($80 shr byte(R_EDX))
  130. {$endif}
  131. {$ifdef m68k}
  132. usedinproc:=usedinproc or ($800 shr word(R_D0));
  133. if is_64bitint(procinfo^.returntype.def) then
  134. usedinproc:=usedinproc or ($800 shr byte(R_D1))
  135. {$endif}
  136. {$endif newcg}
  137. end;
  138. end;
  139. {Unit initialization?.}
  140. if (lexlevel=unit_init_level) and (current_module.is_unit)
  141. or islibrary then
  142. begin
  143. if (token=_END) then
  144. begin
  145. consume(_END);
  146. { We need at least a node, else the entry/exit code is not
  147. generated and thus no PASCALMAIN symbol which we need (PFV) }
  148. if islibrary then
  149. block:=cnothingnode.create
  150. else
  151. block:=nil;
  152. end
  153. else
  154. begin
  155. if token=_INITIALIZATION then
  156. begin
  157. current_module.flags:=current_module.flags or uf_init;
  158. block:=statement_block(_INITIALIZATION);
  159. end
  160. else if (token=_FINALIZATION) then
  161. begin
  162. if (current_module.flags and uf_finalize)<>0 then
  163. block:=statement_block(_FINALIZATION)
  164. else
  165. begin
  166. { can we allow no INITIALIZATION for DLL ??
  167. I think it should work PM }
  168. block:=nil;
  169. exit;
  170. end;
  171. end
  172. else
  173. begin
  174. current_module.flags:=current_module.flags or uf_init;
  175. block:=statement_block(_BEGIN);
  176. end;
  177. end;
  178. end
  179. else
  180. block:=statement_block(_BEGIN);
  181. end;
  182. {****************************************************************************
  183. PROCEDURE/FUNCTION COMPILING
  184. ****************************************************************************}
  185. procedure compile_proc_body(make_global,parent_has_class:boolean);
  186. {
  187. Compile the body of a procedure
  188. }
  189. var
  190. oldexitlabel,oldexit2label : tasmlabel;
  191. oldfaillabel,oldquickexitlabel:tasmlabel;
  192. _class,hp:tobjectdef;
  193. { switches can change inside the procedure }
  194. entryswitches, exitswitches : tlocalswitches;
  195. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  196. { code for the subroutine as tree }
  197. code:tnode;
  198. { size of the local strackframe }
  199. stackframe:longint;
  200. { true when no stackframe is required }
  201. nostackframe:boolean;
  202. { number of bytes which have to be cleared by RET }
  203. parasize:longint;
  204. { filepositions }
  205. entrypos,
  206. savepos,
  207. exitpos : tfileposinfo;
  208. begin
  209. { calculate the lexical level }
  210. inc(lexlevel);
  211. if lexlevel>32 then
  212. Message(parser_e_too_much_lexlevel);
  213. { static is also important for local procedures !! }
  214. if (po_staticmethod in aktprocsym.definition.procoptions) then
  215. allow_only_static:=true
  216. else if (lexlevel=normal_function_level) then
  217. allow_only_static:=false;
  218. { save old labels }
  219. oldexitlabel:=aktexitlabel;
  220. oldexit2label:=aktexit2label;
  221. oldquickexitlabel:=quickexitlabel;
  222. oldfaillabel:=faillabel;
  223. { get new labels }
  224. getlabel(aktexitlabel);
  225. getlabel(aktexit2label);
  226. { exit for fail in constructors }
  227. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  228. begin
  229. getlabel(faillabel);
  230. getlabel(quickexitlabel);
  231. end;
  232. { reset break and continue labels }
  233. block_type:=bt_general;
  234. aktbreaklabel:=nil;
  235. aktcontinuelabel:=nil;
  236. { insert symtables for the class, by only if it is no nested function }
  237. if assigned(procinfo^._class) and not(parent_has_class) then
  238. begin
  239. { insert them in the reverse order ! }
  240. hp:=nil;
  241. repeat
  242. _class:=procinfo^._class;
  243. while _class.childof<>hp do
  244. _class:=_class.childof;
  245. hp:=_class;
  246. _class.symtable.next:=symtablestack;
  247. symtablestack:=_class.symtable;
  248. until hp=procinfo^._class;
  249. end;
  250. { insert parasymtable in symtablestack}
  251. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  252. for checking of same names used in interface and implementation !! }
  253. if lexlevel>=normal_function_level then
  254. begin
  255. aktprocsym.definition.parast.next:=symtablestack;
  256. symtablestack:=aktprocsym.definition.parast;
  257. symtablestack.symtablelevel:=lexlevel;
  258. end;
  259. { insert localsymtable in symtablestack}
  260. aktprocsym.definition.localst.next:=symtablestack;
  261. symtablestack:=aktprocsym.definition.localst;
  262. symtablestack.symtablelevel:=lexlevel;
  263. { constant symbols are inserted in this symboltable }
  264. constsymtable:=symtablestack;
  265. { reset the temporary memory }
  266. cleartempgen;
  267. {$ifdef newcg}
  268. tg.usedinproc:=[];
  269. {$else newcg}
  270. { no registers are used }
  271. usedinproc:=0;
  272. {$endif newcg}
  273. { save entry info }
  274. entrypos:=aktfilepos;
  275. entryswitches:=aktlocalswitches;
  276. localmaxfpuregisters:=aktmaxfpuregisters;
  277. { parse the code ... }
  278. code:=block(current_module.islibrary);
  279. { get a better entry point }
  280. if assigned(code) then
  281. entrypos:=code.fileinfo;
  282. { save exit info }
  283. exitswitches:=aktlocalswitches;
  284. exitpos:=last_endtoken_filepos;
  285. { save current filepos }
  286. savepos:=aktfilepos;
  287. {When we are called to compile the body of a unit, aktprocsym should
  288. point to the unit initialization. If the unit has no initialization,
  289. aktprocsym=nil. But in that case code=nil. hus we should check for
  290. code=nil, when we use aktprocsym.}
  291. { set the framepointer to esp for assembler functions }
  292. { but only if the are no local variables }
  293. { already done in assembler_block }
  294. {$ifdef newcg}
  295. tg.setfirsttemp(procinfo^.firsttemp_offset);
  296. {$else newcg}
  297. setfirsttemp(procinfo^.firsttemp_offset);
  298. {$endif newcg}
  299. { ... and generate assembler }
  300. { but set the right switches for entry !! }
  301. aktlocalswitches:=entryswitches;
  302. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  303. aktmaxfpuregisters:=localmaxfpuregisters;
  304. if assigned(code) then
  305. begin
  306. { the procedure is now defined }
  307. aktprocsym.definition.forwarddef:=false;
  308. { only generate the code if no type errors are found, else
  309. finish at least the type checking pass }
  310. {$ifndef NOPASS2}
  311. if (status.errorcount=0) then
  312. begin
  313. generatecode(code);
  314. aktprocsym.definition.code:=code;
  315. {$ifdef newcg}
  316. stackframe:=tg.gettempsize;
  317. {$else newcg}
  318. stackframe:=gettempsize;
  319. {$endif newcg}
  320. { first generate entry code with the correct position and switches }
  321. aktfilepos:=entrypos;
  322. aktlocalswitches:=entryswitches;
  323. {$ifdef newcg}
  324. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  325. {$else newcg}
  326. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  327. {$endif newcg}
  328. { FPC_POPADDRSTACK destroys all registers (JM) }
  329. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  330. usedinproc := $ff;
  331. { now generate exit code with the correct position and switches }
  332. aktfilepos:=exitpos;
  333. aktlocalswitches:=exitswitches;
  334. {$ifdef newcg}
  335. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  336. {$else newcg}
  337. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  338. {$endif newcg}
  339. { now all the registers used are known }
  340. {$ifdef newcg}
  341. aktprocsym.definition.usedregisters:=tg.usedinproc;
  342. {$else newcg}
  343. aktprocsym.definition.usedregisters:=usedinproc;
  344. {$endif newcg}
  345. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  346. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  347. {$ifdef i386}
  348. {$ifndef NoOpt}
  349. if (cs_optimize in aktglobalswitches) and
  350. { do not optimize pure assembler procedures }
  351. ((procinfo^.flags and pi_is_assembler)=0) then
  352. Optimize(procinfo^.aktproccode);
  353. {$endif NoOpt}
  354. {$endif i386}
  355. { save local data (casetable) also in the same file }
  356. if assigned(procinfo^.aktlocaldata) and
  357. (not procinfo^.aktlocaldata.empty) then
  358. begin
  359. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  360. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  361. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  362. end;
  363. { add the procedure to the codesegment }
  364. if (cs_create_smart in aktmoduleswitches) then
  365. codeSegment.concat(Tai_cut.Create);
  366. codeSegment.concatlist(procinfo^.aktproccode);
  367. end
  368. else
  369. do_resulttypepass(code);
  370. {$else NOPASS2}
  371. do_resulttypepass(code);
  372. {$endif NOPASS2}
  373. end;
  374. { ... remove symbol tables }
  375. if lexlevel>=normal_function_level then
  376. symtablestack:=symtablestack.next.next
  377. else
  378. symtablestack:=symtablestack.next;
  379. { ... check for unused symbols }
  380. { but only if there is no asm block }
  381. if assigned(code) then
  382. begin
  383. if (Errorcount=0) then
  384. begin
  385. tstoredsymtable(aktprocsym.definition.localst).check_forwards;
  386. tstoredsymtable(aktprocsym.definition.localst).checklabels;
  387. end;
  388. if (procinfo^.flags and pi_uses_asm)=0 then
  389. begin
  390. { not for unit init, becuase the var can be used in finalize,
  391. it will be done in proc_unit }
  392. if not(aktprocsym.definition.proctypeoption
  393. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  394. tstoredsymtable(aktprocsym.definition.localst).allsymbolsused;
  395. tstoredsymtable(aktprocsym.definition.parast).allsymbolsused;
  396. end;
  397. end;
  398. { the local symtables can be deleted, but the parast }
  399. { doesn't, (checking definitons when calling a }
  400. { function }
  401. { not for a inline procedure !! (PM) }
  402. { at lexlevel = 1 localst is the staticsymtable itself }
  403. { so no dispose here !! }
  404. if assigned(code) and
  405. not(cs_browser in aktmoduleswitches) and
  406. not(pocall_inline in aktprocsym.definition.proccalloptions) then
  407. begin
  408. if lexlevel>=normal_function_level then
  409. aktprocsym.definition.localst.free;
  410. aktprocsym.definition.localst:=nil;
  411. end;
  412. {$ifdef newcg}
  413. { all registers can be used again }
  414. tg.resetusableregisters;
  415. { only now we can remove the temps }
  416. tg.resettempgen;
  417. {$else newcg}
  418. { all registers can be used again }
  419. resetusableregisters;
  420. { only now we can remove the temps }
  421. resettempgen;
  422. {$endif newcg}
  423. { remove code tree, if not inline procedure }
  424. if assigned(code) and not(pocall_inline in aktprocsym.definition.proccalloptions) then
  425. code.free;
  426. { remove class member symbol tables }
  427. while symtablestack.symtabletype=objectsymtable do
  428. symtablestack:=symtablestack.next;
  429. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  430. { restore filepos, the switches are already set }
  431. aktfilepos:=savepos;
  432. { restore labels }
  433. aktexitlabel:=oldexitlabel;
  434. aktexit2label:=oldexit2label;
  435. quickexitlabel:=oldquickexitlabel;
  436. faillabel:=oldfaillabel;
  437. { reset to normal non static function }
  438. if (lexlevel=normal_function_level) then
  439. allow_only_static:=false;
  440. { previous lexlevel }
  441. dec(lexlevel);
  442. end;
  443. {****************************************************************************
  444. PROCEDURE/FUNCTION PARSING
  445. ****************************************************************************}
  446. procedure checkvaluepara(p:tnamedindexitem);
  447. var
  448. vs : tvarsym;
  449. s : string;
  450. begin
  451. with tvarsym(p) do
  452. begin
  453. if copy(name,1,3)='val' then
  454. begin
  455. s:=Copy(name,4,255);
  456. if not(po_assembler in aktprocsym.definition.procoptions) then
  457. begin
  458. vs:=tvarsym.create(s,vartype);
  459. vs.fileinfo:=fileinfo;
  460. vs.varspez:=varspez;
  461. aktprocsym.definition.localst.insert(vs);
  462. include(vs.varoptions,vo_is_local_copy);
  463. vs.varstate:=vs_assigned;
  464. localvarsym:=vs;
  465. inc(refs); { the para was used to set the local copy ! }
  466. { warnings only on local copy ! }
  467. varstate:=vs_used;
  468. end
  469. else
  470. begin
  471. aktprocsym.definition.parast.rename(name,s);
  472. end;
  473. end;
  474. end;
  475. end;
  476. procedure read_proc;
  477. {
  478. Parses the procedure directives, then parses the procedure body, then
  479. generates the code for it
  480. }
  481. var
  482. oldprefix : string;
  483. oldprocsym : tprocsym;
  484. oldprocinfo : pprocinfo;
  485. oldconstsymtable : tsymtable;
  486. oldfilepos : tfileposinfo;
  487. pdflags : word;
  488. prevdef,stdef : tprocdef;
  489. begin
  490. { save old state }
  491. oldprocsym:=aktprocsym;
  492. oldprefix:=procprefix;
  493. oldconstsymtable:=constsymtable;
  494. oldprocinfo:=procinfo;
  495. { create a new procedure }
  496. codegen_newprocedure;
  497. with procinfo^ do
  498. begin
  499. parent:=oldprocinfo;
  500. { clear flags }
  501. flags:=0;
  502. { standard frame pointer }
  503. framepointer:=frame_pointer;
  504. { funcret_is_valid:=false; }
  505. funcret_state:=vs_declared;
  506. { is this a nested function of a method ? }
  507. if assigned(oldprocinfo) then
  508. _class:=oldprocinfo^._class;
  509. end;
  510. parse_proc_dec;
  511. procinfo^.sym:=aktprocsym;
  512. procinfo^.def:=aktprocsym.definition;
  513. { set the default function options }
  514. if parse_only then
  515. begin
  516. aktprocsym.definition.forwarddef:=true;
  517. { set also the interface flag, for better error message when the
  518. implementation doesn't much this header }
  519. aktprocsym.definition.interfacedef:=true;
  520. pdflags:=pd_interface;
  521. end
  522. else
  523. begin
  524. pdflags:=pd_body;
  525. if current_module.in_implementation then
  526. pdflags:=pdflags or pd_implemen;
  527. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  528. pdflags:=pdflags or pd_global;
  529. procinfo^.exported:=false;
  530. aktprocsym.definition.forwarddef:=false;
  531. end;
  532. { parse the directives that may follow }
  533. inc(lexlevel);
  534. parse_proc_directives(pdflags);
  535. dec(lexlevel);
  536. { set aktfilepos to the beginning of the function declaration }
  537. oldfilepos:=aktfilepos;
  538. aktfilepos:=aktprocsym.definition.fileinfo;
  539. { search for forward declarations }
  540. if not check_identical_proc(prevdef) then
  541. begin
  542. { A method must be forward defined (in the object declaration) }
  543. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  544. begin
  545. Message1(parser_e_header_dont_match_any_member,
  546. aktprocsym.definition.fullprocname);
  547. aktprocsym.write_parameter_lists(aktprocsym.definition);
  548. end
  549. else
  550. begin
  551. { Give a better error if there is a forward def in the interface and only
  552. a single implementation }
  553. if (not aktprocsym.definition.forwarddef) and
  554. assigned(aktprocsym.definition.nextoverloaded) and
  555. aktprocsym.definition.nextoverloaded.forwarddef and
  556. aktprocsym.definition.nextoverloaded.interfacedef and
  557. not(assigned(aktprocsym.definition.nextoverloaded.nextoverloaded)) then
  558. begin
  559. Message1(parser_e_header_dont_match_forward,
  560. aktprocsym.definition.fullprocname);
  561. aktprocsym.write_parameter_lists(aktprocsym.definition);
  562. end
  563. else
  564. begin
  565. { check the global flag }
  566. if (procinfo^.flags and pi_is_global)<>0 then
  567. Message(parser_e_overloaded_must_be_all_global);
  568. end;
  569. end;
  570. end;
  571. { set return type here, becuase the aktprocsym.definition can be
  572. changed by check_identical_proc (PFV) }
  573. procinfo^.returntype.def:=aktprocsym.definition.rettype.def;
  574. {$ifdef i386}
  575. if (po_interrupt in aktprocsym.definition.procoptions) then
  576. begin
  577. { we push Flags and CS as long
  578. to cope with the IRETD
  579. and we save 6 register + 4 selectors }
  580. inc(procinfo^.para_offset,8+6*4+4*2);
  581. end;
  582. {$endif i386}
  583. { pointer to the return value ? }
  584. if ret_in_param(procinfo^.returntype.def) then
  585. begin
  586. procinfo^.return_offset:=procinfo^.para_offset;
  587. inc(procinfo^.para_offset,target_info.size_of_pointer);
  588. end;
  589. { allows to access the parameters of main functions in nested functions }
  590. aktprocsym.definition.parast.address_fixup:=procinfo^.para_offset;
  591. { when it is a value para and it needs a local copy then rename
  592. the parameter and insert a copy in the localst. This is not done
  593. for assembler procedures }
  594. if (not parse_only) and (not aktprocsym.definition.forwarddef) then
  595. aktprocsym.definition.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  596. { restore file pos }
  597. aktfilepos:=oldfilepos;
  598. { compile procedure when a body is needed }
  599. if (pdflags and pd_body)<>0 then
  600. begin
  601. Message1(parser_p_procedure_start,
  602. aktprocsym.definition.fullprocname);
  603. aktprocsym.definition.aliasnames.insert(aktprocsym.definition.mangledname);
  604. { set _FAIL as keyword if constructor }
  605. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  606. tokeninfo^[_FAIL].keyword:=m_all;
  607. if assigned(aktprocsym.definition._class) then
  608. tokeninfo^[_SELF].keyword:=m_all;
  609. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  610. { reset _FAIL as normal }
  611. if (aktprocsym.definition.proctypeoption=potype_constructor) then
  612. tokeninfo^[_FAIL].keyword:=m_none;
  613. if assigned(aktprocsym.definition._class) and (lexlevel=main_program_level) then
  614. tokeninfo^[_SELF].keyword:=m_none;
  615. consume(_SEMICOLON);
  616. end;
  617. { close }
  618. codegen_doneprocedure;
  619. { Restore old state }
  620. constsymtable:=oldconstsymtable;
  621. { from now on all refernece to mangledname means
  622. that the function is already used }
  623. aktprocsym.definition.count:=true;
  624. { restore the interface order to maintain CRC values PM }
  625. if assigned(prevdef) and assigned(aktprocsym.definition.nextoverloaded) then
  626. begin
  627. stdef:=aktprocsym.definition;
  628. aktprocsym.definition:=stdef.nextoverloaded;
  629. stdef.nextoverloaded:=prevdef.nextoverloaded;
  630. prevdef.nextoverloaded:=stdef;
  631. end;
  632. aktprocsym:=oldprocsym;
  633. procprefix:=oldprefix;
  634. procinfo:=oldprocinfo;
  635. otsym:=nil;
  636. end;
  637. {****************************************************************************
  638. DECLARATION PARSING
  639. ****************************************************************************}
  640. procedure read_declarations(islibrary : boolean);
  641. procedure Not_supported_for_inline(t : ttoken);
  642. begin
  643. if assigned(aktprocsym) and
  644. (pocall_inline in aktprocsym.definition.proccalloptions) then
  645. Begin
  646. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  647. Message(parser_w_inlining_disabled);
  648. exclude(aktprocsym.definition.proccalloptions,pocall_inline);
  649. End;
  650. end;
  651. begin
  652. repeat
  653. case token of
  654. _LABEL:
  655. begin
  656. Not_supported_for_inline(token);
  657. label_dec;
  658. end;
  659. _CONST:
  660. begin
  661. Not_supported_for_inline(token);
  662. const_dec;
  663. end;
  664. _TYPE:
  665. begin
  666. Not_supported_for_inline(token);
  667. type_dec;
  668. end;
  669. _VAR:
  670. var_dec;
  671. _THREADVAR:
  672. threadvar_dec;
  673. _CONSTRUCTOR,_DESTRUCTOR,
  674. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  675. begin
  676. Not_supported_for_inline(token);
  677. read_proc;
  678. end;
  679. _RESOURCESTRING:
  680. resourcestring_dec;
  681. _EXPORTS:
  682. begin
  683. Not_supported_for_inline(token);
  684. { here we should be at lexlevel 1, no ? PM }
  685. if (lexlevel<>main_program_level) or
  686. (current_module.is_unit) then
  687. begin
  688. Message(parser_e_syntax_error);
  689. consume_all_until(_SEMICOLON);
  690. end
  691. else if islibrary or (target_info.target=target_i386_WIN32)
  692. or (target_info.target=target_i386_Netware) then // AD
  693. read_exports;
  694. end
  695. else break;
  696. end;
  697. until false;
  698. end;
  699. procedure read_interface_declarations;
  700. begin
  701. {Since the body is now parsed at lexlevel 1, and the declarations
  702. must be parsed at the same lexlevel we increase the lexlevel.}
  703. inc(lexlevel);
  704. repeat
  705. case token of
  706. _CONST :
  707. const_dec;
  708. _TYPE :
  709. type_dec;
  710. _VAR :
  711. var_dec;
  712. _THREADVAR :
  713. threadvar_dec;
  714. _RESOURCESTRING:
  715. resourcestring_dec;
  716. _FUNCTION,
  717. _PROCEDURE,
  718. _OPERATOR :
  719. read_proc;
  720. else
  721. break;
  722. end;
  723. until false;
  724. dec(lexlevel);
  725. end;
  726. end.
  727. {
  728. $Log$
  729. Revision 1.31 2001-04-18 22:01:57 peter
  730. * registration of targets and assemblers
  731. Revision 1.30 2001/04/14 14:05:47 peter
  732. * better skipping of secondpass if error
  733. Revision 1.29 2001/04/13 23:49:24 peter
  734. * when errors are found don't generate code, but still run the
  735. resulttype pass
  736. Revision 1.28 2001/04/13 17:59:03 peter
  737. * don't generate code when there is already an error
  738. Revision 1.27 2001/04/13 01:22:13 peter
  739. * symtable change to classes
  740. * range check generation and errors fixed, make cycle DEBUG=1 works
  741. * memory leaks fixed
  742. Revision 1.26 2001/04/02 21:20:34 peter
  743. * resulttype rewrite
  744. Revision 1.25 2001/02/26 19:44:53 peter
  745. * merged generic m68k updates from fixes branch
  746. Revision 1.24 2000/12/25 00:07:27 peter
  747. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  748. tlinkedlist objects)
  749. Revision 1.23 2000/11/29 00:30:37 florian
  750. * unused units removed from uses clause
  751. * some changes for widestrings
  752. Revision 1.22 2000/11/08 16:38:24 jonas
  753. * if a procedure uses exceptions (be it implicit or explicit), the
  754. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  755. any registers) ("merged", fixes make cycle woth -Or)
  756. Revision 1.21 2000/11/01 23:04:38 peter
  757. * tprocdef.fullprocname added for better casesensitve writing of
  758. procedures
  759. Revision 1.20 2000/10/31 22:02:50 peter
  760. * symtable splitted, no real code changes
  761. Revision 1.19 2000/10/24 22:21:25 peter
  762. * set usedregisters after writing entry and exit code (merged)
  763. Revision 1.18 2000/10/21 18:16:12 florian
  764. * a lot of changes:
  765. - basic dyn. array support
  766. - basic C++ support
  767. - some work for interfaces done
  768. ....
  769. Revision 1.17 2000/10/15 07:47:51 peter
  770. * unit names and procedure names are stored mixed case
  771. Revision 1.16 2000/10/14 10:14:52 peter
  772. * moehrendorf oct 2000 rewrite
  773. Revision 1.15 2000/09/24 21:33:47 peter
  774. * message updates merges
  775. Revision 1.14 2000/09/24 21:19:51 peter
  776. * delphi compile fixes
  777. Revision 1.13 2000/09/24 15:06:24 peter
  778. * use defines.inc
  779. Revision 1.12 2000/09/10 20:11:07 peter
  780. * overload checking in implementation removed (merged)
  781. Revision 1.11 2000/09/04 20:15:19 peter
  782. * fixed operator overloading
  783. Revision 1.10 2000/08/27 16:11:52 peter
  784. * moved some util functions from globals,cobjects to cutils
  785. * splitted files into finput,fmodule
  786. Revision 1.9 2000/08/16 18:33:54 peter
  787. * splitted namedobjectitem.next into indexnext and listnext so it
  788. can be used in both lists
  789. * don't allow "word = word" type definitions (merged)
  790. Revision 1.8 2000/08/13 12:54:56 peter
  791. * class member decl wrong then no other error after it
  792. * -vb has now also line numbering
  793. * -vb is also used for interface/implementation different decls and
  794. doesn't list the current function (merged)
  795. Revision 1.7 2000/08/08 19:28:57 peter
  796. * memdebug/memory patches (merged)
  797. * only once illegal directive (merged)
  798. Revision 1.6 2000/08/06 19:39:28 peter
  799. * default parameters working !
  800. Revision 1.5 2000/08/06 14:17:15 peter
  801. * overload fixes (merged)
  802. Revision 1.4 2000/07/30 17:04:43 peter
  803. * merged fixes
  804. Revision 1.3 2000/07/13 12:08:27 michael
  805. + patched to 1.1.0 with former 1.09patch from peter
  806. Revision 1.2 2000/07/13 11:32:46 michael
  807. + removed logs
  808. }