psub.pas 34 KB

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