psub.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. {$ifdef powerpc}
  21. {$define newcg}
  22. {$endif powerpc}
  23. interface
  24. procedure compile_proc_body(make_global,parent_has_class:boolean);
  25. { reads the declaration blocks }
  26. procedure read_declarations(islibrary : boolean);
  27. { reads declarations in the interface part of a unit }
  28. procedure read_interface_declarations;
  29. implementation
  30. uses
  31. { common }
  32. cutils,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,comphook,
  35. systems,
  36. { aasm }
  37. cpubase,cpuinfo,aasm,
  38. { symtable }
  39. symconst,symbase,symdef,symsym,symtype,symtable,types,
  40. ppu,fmodule,
  41. { pass 1 }
  42. node,
  43. nbas,
  44. { pass 2 }
  45. {$ifndef NOPASS2}
  46. pass_1,pass_2,
  47. {$endif}
  48. { parser }
  49. scanner,
  50. pbase,pstatmnt,pdecl,pdecsub,pexports,
  51. { codegen }
  52. tgobj,cgbase,rgobj,
  53. rgcpu,
  54. cga
  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 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 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. { insert symtables for the class, by only if it is no nested function }
  225. if assigned(procinfo^._class) and not(parent_has_class) then
  226. begin
  227. { insert them in the reverse order ! }
  228. hp:=nil;
  229. repeat
  230. _class:=procinfo^._class;
  231. while _class.childof<>hp do
  232. _class:=_class.childof;
  233. hp:=_class;
  234. _class.symtable.next:=symtablestack;
  235. symtablestack:=_class.symtable;
  236. until hp=procinfo^._class;
  237. end;
  238. { insert parasymtable in symtablestack}
  239. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  240. for checking of same names used in interface and implementation !! }
  241. if lexlevel>=normal_function_level then
  242. begin
  243. aktprocdef.parast.next:=symtablestack;
  244. symtablestack:=aktprocdef.parast;
  245. symtablestack.symtablelevel:=lexlevel;
  246. end;
  247. { insert localsymtable in symtablestack}
  248. aktprocdef.localst.next:=symtablestack;
  249. symtablestack:=aktprocdef.localst;
  250. symtablestack.symtablelevel:=lexlevel;
  251. { constant symbols are inserted in this symboltable }
  252. constsymtable:=symtablestack;
  253. { reset the temporary memory }
  254. rg.cleartempgen;
  255. rg.usedinproc:=[];
  256. { save entry info }
  257. entrypos:=aktfilepos;
  258. entryswitches:=aktlocalswitches;
  259. localmaxfpuregisters:=aktmaxfpuregisters;
  260. { parse the code ... }
  261. code:=block(current_module.islibrary);
  262. { get a better entry point }
  263. if assigned(code) then
  264. entrypos:=code.fileinfo;
  265. { save exit info }
  266. exitswitches:=aktlocalswitches;
  267. exitpos:=last_endtoken_filepos;
  268. { save current filepos }
  269. savepos:=aktfilepos;
  270. {When we are called to compile the body of a unit, aktprocsym should
  271. point to the unit initialization. If the unit has no initialization,
  272. aktprocsym=nil. But in that case code=nil. hus we should check for
  273. code=nil, when we use aktprocsym.}
  274. { set the framepointer to esp for assembler functions }
  275. { but only if the are no local variables }
  276. { already done in assembler_block }
  277. tg.setfirsttemp(procinfo^.firsttemp_offset);
  278. { ... and generate assembler }
  279. { but set the right switches for entry !! }
  280. aktlocalswitches:=entryswitches;
  281. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  282. aktmaxfpuregisters:=localmaxfpuregisters;
  283. if assigned(code) then
  284. begin
  285. { the procedure is now defined }
  286. aktprocdef.forwarddef:=false;
  287. { only generate the code if no type errors are found, else
  288. finish at least the type checking pass }
  289. {$ifndef NOPASS2}
  290. if (status.errorcount=0) then
  291. begin
  292. generatecode(code);
  293. aktprocdef.code:=code;
  294. stackframe:=tg.gettempsize;
  295. { first generate entry code with the correct position and switches }
  296. aktfilepos:=entrypos;
  297. aktlocalswitches:=entryswitches;
  298. {$ifdef newcg}
  299. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  300. {$else newcg}
  301. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  302. {$endif newcg}
  303. { FPC_POPADDRSTACK destroys all registers (JM) }
  304. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  305. begin
  306. rg.usedinproc := ALL_REGISTERS;
  307. end;
  308. { now generate exit code with the correct position and switches }
  309. aktfilepos:=exitpos;
  310. aktlocalswitches:=exitswitches;
  311. {$ifdef newcg}
  312. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  313. {$else newcg}
  314. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  315. {$endif newcg}
  316. { now all the registers used are known }
  317. aktprocdef.usedregisters:=rg.usedinproc;
  318. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  319. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  320. {$ifdef i386}
  321. {$ifndef NoOpt}
  322. if (cs_optimize in aktglobalswitches) and
  323. { do not optimize pure assembler procedures }
  324. ((procinfo^.flags and pi_is_assembler)=0) then
  325. Optimize(procinfo^.aktproccode);
  326. {$endif NoOpt}
  327. {$endif i386}
  328. { save local data (casetable) also in the same file }
  329. if assigned(procinfo^.aktlocaldata) and
  330. (not procinfo^.aktlocaldata.empty) then
  331. begin
  332. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  333. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  334. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  335. end;
  336. { add the procedure to the codesegment }
  337. if (cs_create_smart in aktmoduleswitches) then
  338. codeSegment.concat(Tai_cut.Create);
  339. codeSegment.concatlist(procinfo^.aktproccode);
  340. end
  341. else
  342. do_resulttypepass(code);
  343. {$else NOPASS2}
  344. do_resulttypepass(code);
  345. {$endif NOPASS2}
  346. end;
  347. { ... remove symbol tables }
  348. if lexlevel>=normal_function_level then
  349. symtablestack:=symtablestack.next.next
  350. else
  351. symtablestack:=symtablestack.next;
  352. { ... check for unused symbols }
  353. { but only if there is no asm block }
  354. if assigned(code) then
  355. begin
  356. if (Errorcount=0) then
  357. begin
  358. { check if forwards are resolved }
  359. tstoredsymtable(aktprocdef.localst).check_forwards;
  360. { check if all labels are used }
  361. tstoredsymtable(aktprocdef.localst).checklabels;
  362. { remove cross unit overloads }
  363. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  364. end;
  365. if (procinfo^.flags and pi_uses_asm)=0 then
  366. begin
  367. { not for unit init, becuase the var can be used in finalize,
  368. it will be done in proc_unit }
  369. if not(aktprocdef.proctypeoption
  370. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  371. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  372. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  373. end;
  374. end;
  375. { the local symtables can be deleted, but the parast }
  376. { doesn't, (checking definitons when calling a }
  377. { function }
  378. { not for a inline procedure !! (PM) }
  379. { at lexlevel = 1 localst is the staticsymtable itself }
  380. { so no dispose here !! }
  381. if assigned(code) and
  382. not(cs_browser in aktmoduleswitches) and
  383. (aktprocdef.proccalloption<>pocall_inline) then
  384. begin
  385. if lexlevel>=normal_function_level then
  386. aktprocdef.localst.free;
  387. aktprocdef.localst:=nil;
  388. end;
  389. { all registers can be used again }
  390. rg.resetusableregisters;
  391. { only now we can remove the temps }
  392. tg.resettempgen;
  393. { remove code tree, if not inline procedure }
  394. if assigned(code) and (aktprocdef.proccalloption<>pocall_inline) then
  395. code.free;
  396. { remove class member symbol tables }
  397. while symtablestack.symtabletype=objectsymtable do
  398. symtablestack:=symtablestack.next;
  399. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  400. { restore filepos, the switches are already set }
  401. aktfilepos:=savepos;
  402. { restore labels }
  403. aktexitlabel:=oldexitlabel;
  404. aktexit2label:=oldexit2label;
  405. quickexitlabel:=oldquickexitlabel;
  406. faillabel:=oldfaillabel;
  407. { reset to normal non static function }
  408. if (lexlevel=normal_function_level) then
  409. allow_only_static:=false;
  410. { previous lexlevel }
  411. dec(lexlevel);
  412. end;
  413. {****************************************************************************
  414. PROCEDURE/FUNCTION PARSING
  415. ****************************************************************************}
  416. procedure checkvaluepara(p:tnamedindexitem);
  417. var
  418. vs : tvarsym;
  419. s : string;
  420. begin
  421. with tvarsym(p) do
  422. begin
  423. if copy(name,1,3)='val' then
  424. begin
  425. s:=Copy(name,4,255);
  426. if not(po_assembler in aktprocdef.procoptions) then
  427. begin
  428. vs:=tvarsym.create(s,vartype);
  429. vs.fileinfo:=fileinfo;
  430. vs.varspez:=varspez;
  431. aktprocdef.localst.insert(vs);
  432. include(vs.varoptions,vo_is_local_copy);
  433. vs.varstate:=vs_assigned;
  434. localvarsym:=vs;
  435. inc(refs); { the para was used to set the local copy ! }
  436. { warnings only on local copy ! }
  437. varstate:=vs_used;
  438. end
  439. else
  440. begin
  441. aktprocdef.parast.rename(name,s);
  442. end;
  443. end;
  444. end;
  445. end;
  446. procedure read_proc;
  447. {
  448. Parses the procedure directives, then parses the procedure body, then
  449. generates the code for it
  450. }
  451. var
  452. oldprefix : string;
  453. oldprocsym : tprocsym;
  454. oldprocdef : tprocdef;
  455. oldprocinfo : pprocinfo;
  456. oldconstsymtable : tsymtable;
  457. oldfilepos : tfileposinfo;
  458. pdflags : word;
  459. begin
  460. { save old state }
  461. oldprocdef:=aktprocdef;
  462. oldprocsym:=aktprocsym;
  463. oldprefix:=procprefix;
  464. oldconstsymtable:=constsymtable;
  465. oldprocinfo:=procinfo;
  466. { create a new procedure }
  467. codegen_newprocedure;
  468. with procinfo^ do
  469. begin
  470. parent:=oldprocinfo;
  471. { clear flags }
  472. flags:=0;
  473. { standard frame pointer }
  474. framepointer:=frame_pointer;
  475. { is this a nested function of a method ? }
  476. if assigned(oldprocinfo) then
  477. _class:=oldprocinfo^._class;
  478. end;
  479. parse_proc_dec;
  480. procinfo^.procdef:=aktprocdef;
  481. { set the default function options }
  482. if parse_only then
  483. begin
  484. aktprocdef.forwarddef:=true;
  485. { set also the interface flag, for better error message when the
  486. implementation doesn't much this header }
  487. aktprocdef.interfacedef:=true;
  488. pdflags:=pd_interface;
  489. end
  490. else
  491. begin
  492. pdflags:=pd_body;
  493. if current_module.in_implementation then
  494. pdflags:=pdflags or pd_implemen;
  495. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  496. pdflags:=pdflags or pd_global;
  497. procinfo^.exported:=false;
  498. aktprocdef.forwarddef:=false;
  499. end;
  500. { parse the directives that may follow }
  501. inc(lexlevel);
  502. parse_proc_directives(pdflags);
  503. dec(lexlevel);
  504. { hint directives, these can be separated by semicolons here,
  505. that need to be handled here with a loop (PFV) }
  506. while try_consume_hintdirective(aktprocsym.symoptions) do
  507. Consume(_SEMICOLON);
  508. { set aktfilepos to the beginning of the function declaration }
  509. oldfilepos:=aktfilepos;
  510. aktfilepos:=aktprocdef.fileinfo;
  511. { For varargs directive also cdecl and external must be defined }
  512. if (po_varargs in aktprocdef.procoptions) then
  513. begin
  514. { check first for external in the interface, if available there
  515. then the cdecl must also be there since there is no implementation
  516. available to contain it }
  517. if parse_only then
  518. begin
  519. { if external is available, then cdecl must also be available }
  520. if (po_external in aktprocdef.procoptions) and
  521. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  522. Message(parser_e_varargs_need_cdecl_and_external);
  523. end
  524. else
  525. begin
  526. { both must be defined now }
  527. if not(po_external in aktprocdef.procoptions) or
  528. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  529. Message(parser_e_varargs_need_cdecl_and_external);
  530. end;
  531. end;
  532. { search for forward declarations }
  533. if not proc_add_definition(aktprocsym,aktprocdef) then
  534. begin
  535. { A method must be forward defined (in the object declaration) }
  536. if assigned(procinfo^._class) and
  537. (not assigned(oldprocinfo^._class)) then
  538. begin
  539. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  540. aktprocsym.write_parameter_lists(aktprocdef);
  541. end
  542. else
  543. begin
  544. { Give a better error if there is a forward def in the interface and only
  545. a single implementation }
  546. if (not aktprocdef.forwarddef) and
  547. assigned(aktprocsym.defs^.next) and
  548. aktprocsym.defs^.def.forwarddef and
  549. aktprocsym.defs^.def.interfacedef and
  550. not(assigned(aktprocsym.defs^.next^.next)) then
  551. begin
  552. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  553. aktprocsym.write_parameter_lists(aktprocdef);
  554. end
  555. else
  556. begin
  557. { check the global flag, for delphi this is not
  558. required }
  559. if not(m_delphi in aktmodeswitches) and
  560. ((procinfo^.flags and pi_is_global)<>0) then
  561. Message(parser_e_overloaded_must_be_all_global);
  562. end;
  563. end;
  564. end;
  565. { update procinfo, because the aktprocdef can be
  566. changed by check_identical_proc (PFV) }
  567. procinfo^.procdef:=aktprocdef;
  568. {$ifdef i386}
  569. { add implicit pushes for interrupt routines }
  570. if (po_interrupt in aktprocdef.procoptions) then
  571. begin
  572. { we push Flags and CS as long
  573. to cope with the IRETD
  574. and we save 6 register + 4 selectors }
  575. inc(procinfo^.para_offset,8+6*4+4*2);
  576. end;
  577. {$endif i386}
  578. { pointer to the return value ? }
  579. if ret_in_param(aktprocdef.rettype.def) then
  580. begin
  581. procinfo^.return_offset:=procinfo^.para_offset;
  582. inc(procinfo^.para_offset,target_info.size_of_pointer);
  583. end;
  584. { allows to access the parameters of main functions in nested functions }
  585. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  586. { when it is a value para and it needs a local copy then rename
  587. the parameter and insert a copy in the localst. This is not done
  588. for assembler procedures }
  589. if (not parse_only) and (not aktprocdef.forwarddef) then
  590. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  591. { restore file pos }
  592. aktfilepos:=oldfilepos;
  593. { compile procedure when a body is needed }
  594. if (pdflags and pd_body)<>0 then
  595. begin
  596. Message1(parser_p_procedure_start,
  597. aktprocdef.fullprocname);
  598. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  599. { set _FAIL as keyword if constructor }
  600. if (aktprocdef.proctypeoption=potype_constructor) then
  601. tokeninfo^[_FAIL].keyword:=m_all;
  602. if assigned(aktprocdef._class) then
  603. tokeninfo^[_SELF].keyword:=m_all;
  604. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  605. { reset _FAIL as normal }
  606. if (aktprocdef.proctypeoption=potype_constructor) then
  607. tokeninfo^[_FAIL].keyword:=m_none;
  608. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  609. tokeninfo^[_SELF].keyword:=m_none;
  610. consume(_SEMICOLON);
  611. end;
  612. { close }
  613. codegen_doneprocedure;
  614. { Restore old state }
  615. constsymtable:=oldconstsymtable;
  616. { from now on all refernece to mangledname means
  617. that the function is already used }
  618. aktprocdef.count:=true;
  619. {$ifdef notused}
  620. { restore the interface order to maintain CRC values PM }
  621. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  622. begin
  623. stdef:=aktprocdef;
  624. aktprocdef:=stdef.nextoverloaded;
  625. stdef.nextoverloaded:=prevdef.nextoverloaded;
  626. prevdef.nextoverloaded:=stdef;
  627. end;
  628. {$endif notused}
  629. aktprocsym:=oldprocsym;
  630. aktprocdef:=oldprocdef;
  631. procprefix:=oldprefix;
  632. procinfo:=oldprocinfo;
  633. otsym:=nil;
  634. end;
  635. {****************************************************************************
  636. DECLARATION PARSING
  637. ****************************************************************************}
  638. { search in symtablestack for not complete classes }
  639. procedure check_forward_class(p : tnamedindexitem);
  640. begin
  641. if (tsym(p).typ=typesym) and
  642. (ttypesym(p).restype.def.deftype=objectdef) and
  643. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  644. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  645. end;
  646. procedure read_declarations(islibrary : boolean);
  647. procedure Not_supported_for_inline(t : ttoken);
  648. begin
  649. if assigned(aktprocsym) and
  650. (aktprocdef.proccalloption=pocall_inline) then
  651. Begin
  652. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  653. Message(parser_w_inlining_disabled);
  654. aktprocdef.proccalloption:=pocall_fpccall;
  655. End;
  656. end;
  657. begin
  658. repeat
  659. case token of
  660. _LABEL:
  661. begin
  662. Not_supported_for_inline(token);
  663. label_dec;
  664. end;
  665. _CONST:
  666. begin
  667. Not_supported_for_inline(token);
  668. const_dec;
  669. end;
  670. _TYPE:
  671. begin
  672. Not_supported_for_inline(token);
  673. type_dec;
  674. end;
  675. _VAR:
  676. var_dec;
  677. _THREADVAR:
  678. threadvar_dec;
  679. _CONSTRUCTOR,_DESTRUCTOR,
  680. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  681. begin
  682. Not_supported_for_inline(token);
  683. read_proc;
  684. end;
  685. _RESOURCESTRING:
  686. resourcestring_dec;
  687. _EXPORTS:
  688. begin
  689. Not_supported_for_inline(token);
  690. { here we should be at lexlevel 1, no ? PM }
  691. if (lexlevel<>main_program_level) or
  692. (current_module.is_unit) then
  693. begin
  694. Message(parser_e_syntax_error);
  695. consume_all_until(_SEMICOLON);
  696. end
  697. else if islibrary or (target_info.target=target_i386_WIN32)
  698. or (target_info.target=target_i386_Netware) then // AD
  699. read_exports;
  700. end
  701. else break;
  702. end;
  703. until false;
  704. { check for incomplete class definitions, this is only required
  705. for fpc modes }
  706. if (m_fpc in aktmodeswitches) then
  707. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  708. end;
  709. procedure read_interface_declarations;
  710. begin
  711. {Since the body is now parsed at lexlevel 1, and the declarations
  712. must be parsed at the same lexlevel we increase the lexlevel.}
  713. inc(lexlevel);
  714. repeat
  715. case token of
  716. _CONST :
  717. const_dec;
  718. _TYPE :
  719. type_dec;
  720. _VAR :
  721. var_dec;
  722. _THREADVAR :
  723. threadvar_dec;
  724. _RESOURCESTRING:
  725. resourcestring_dec;
  726. _FUNCTION,
  727. _PROCEDURE,
  728. _OPERATOR :
  729. read_proc;
  730. else
  731. break;
  732. end;
  733. until false;
  734. dec(lexlevel);
  735. { check for incomplete class definitions, this is only required
  736. for fpc modes }
  737. if (m_fpc in aktmodeswitches) then
  738. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  739. end;
  740. end.
  741. {
  742. $Log$
  743. Revision 1.45 2002-03-31 20:26:36 jonas
  744. + a_loadfpu_* and a_loadmm_* methods in tcg
  745. * register allocation is now handled by a class and is mostly processor
  746. independent (+rgobj.pas and i386/rgcpu.pas)
  747. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  748. * some small improvements and fixes to the optimizer
  749. * some register allocation fixes
  750. * some fpuvaroffset fixes in the unary minus node
  751. * push/popusedregisters is now called rg.save/restoreusedregisters and
  752. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  753. also better optimizable)
  754. * fixed and optimized register saving/restoring for new/dispose nodes
  755. * LOC_FPU locations now also require their "register" field to be set to
  756. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  757. - list field removed of the tnode class because it's not used currently
  758. and can cause hard-to-find bugs
  759. Revision 1.44 2002/01/19 15:37:24 peter
  760. * commited the wrong file :(
  761. Revision 1.43 2002/01/19 15:20:09 peter
  762. * also check at the end of the implementation for incomplete classes
  763. Revision 1.42 2002/01/19 15:12:34 peter
  764. * check for unresolved forward classes in the interface
  765. Revision 1.41 2001/11/02 22:58:06 peter
  766. * procsym definition rewrite
  767. Revision 1.40 2001/10/25 21:22:37 peter
  768. * calling convention rewrite
  769. Revision 1.39 2001/10/22 21:20:46 peter
  770. * overloaded functions don't need to be global in kylix
  771. Revision 1.38 2001/10/01 13:38:45 jonas
  772. * allow self parameter for normal procedures again (because Kylix allows
  773. it too) ("merged")
  774. Revision 1.37 2001/09/10 10:26:26 jonas
  775. * fixed web bug 1593
  776. * writing of procvar headers is more complete (mention var/const/out for
  777. paras, add "of object" if applicable)
  778. + error if declaring explicit self para as var/const
  779. * fixed mangled name of procedures which contain an explicit self para
  780. * parsing para's should be slightly faster because mangled name of
  781. procedure is only updated once instead of after parsing each para
  782. (all merged from fixes)
  783. Revision 1.36 2001/08/26 13:36:46 florian
  784. * some cg reorganisation
  785. * some PPC updates
  786. Revision 1.35 2001/08/06 21:40:47 peter
  787. * funcret moved from tprocinfo to tprocdef
  788. Revision 1.34 2001/06/04 11:53:13 peter
  789. + varargs directive
  790. Revision 1.33 2001/06/03 21:57:37 peter
  791. + hint directive parsing support
  792. Revision 1.32 2001/04/21 12:03:12 peter
  793. * m68k updates merged from fixes branch
  794. Revision 1.31 2001/04/18 22:01:57 peter
  795. * registration of targets and assemblers
  796. Revision 1.30 2001/04/14 14:05:47 peter
  797. * better skipping of secondpass if error
  798. Revision 1.29 2001/04/13 23:49:24 peter
  799. * when errors are found don't generate code, but still run the
  800. resulttype pass
  801. Revision 1.28 2001/04/13 17:59:03 peter
  802. * don't generate code when there is already an error
  803. Revision 1.27 2001/04/13 01:22:13 peter
  804. * symtable change to classes
  805. * range check generation and errors fixed, make cycle DEBUG=1 works
  806. * memory leaks fixed
  807. Revision 1.26 2001/04/02 21:20:34 peter
  808. * resulttype rewrite
  809. Revision 1.25 2001/02/26 19:44:53 peter
  810. * merged generic m68k updates from fixes branch
  811. Revision 1.24 2000/12/25 00:07:27 peter
  812. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  813. tlinkedlist objects)
  814. Revision 1.23 2000/11/29 00:30:37 florian
  815. * unused units removed from uses clause
  816. * some changes for widestrings
  817. Revision 1.22 2000/11/08 16:38:24 jonas
  818. * if a procedure uses exceptions (be it implicit or explicit), the
  819. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  820. any registers) ("merged", fixes make cycle woth -Or)
  821. Revision 1.21 2000/11/01 23:04:38 peter
  822. * tprocdef.fullprocname added for better casesensitve writing of
  823. procedures
  824. Revision 1.20 2000/10/31 22:02:50 peter
  825. * symtable splitted, no real code changes
  826. Revision 1.19 2000/10/24 22:21:25 peter
  827. * set usedregisters after writing entry and exit code (merged)
  828. Revision 1.18 2000/10/21 18:16:12 florian
  829. * a lot of changes:
  830. - basic dyn. array support
  831. - basic C++ support
  832. - some work for interfaces done
  833. ....
  834. Revision 1.17 2000/10/15 07:47:51 peter
  835. * unit names and procedure names are stored mixed case
  836. Revision 1.16 2000/10/14 10:14:52 peter
  837. * moehrendorf oct 2000 rewrite
  838. Revision 1.15 2000/09/24 21:33:47 peter
  839. * message updates merges
  840. Revision 1.14 2000/09/24 21:19:51 peter
  841. * delphi compile fixes
  842. Revision 1.13 2000/09/24 15:06:24 peter
  843. * use defines.inc
  844. Revision 1.12 2000/09/10 20:11:07 peter
  845. * overload checking in implementation removed (merged)
  846. Revision 1.11 2000/09/04 20:15:19 peter
  847. * fixed operator overloading
  848. Revision 1.10 2000/08/27 16:11:52 peter
  849. * moved some util functions from globals,cobjects to cutils
  850. * splitted files into finput,fmodule
  851. Revision 1.9 2000/08/16 18:33:54 peter
  852. * splitted namedobjectitem.next into indexnext and listnext so it
  853. can be used in both lists
  854. * don't allow "word = word" type definitions (merged)
  855. Revision 1.8 2000/08/13 12:54:56 peter
  856. * class member decl wrong then no other error after it
  857. * -vb has now also line numbering
  858. * -vb is also used for interface/implementation different decls and
  859. doesn't list the current function (merged)
  860. Revision 1.7 2000/08/08 19:28:57 peter
  861. * memdebug/memory patches (merged)
  862. * only once illegal directive (merged)
  863. Revision 1.6 2000/08/06 19:39:28 peter
  864. * default parameters working !
  865. Revision 1.5 2000/08/06 14:17:15 peter
  866. * overload fixes (merged)
  867. Revision 1.4 2000/07/30 17:04:43 peter
  868. * merged fixes
  869. Revision 1.3 2000/07/13 12:08:27 michael
  870. + patched to 1.1.0 with former 1.09patch from peter
  871. Revision 1.2 2000/07/13 11:32:46 michael
  872. + removed logs
  873. }