psub.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. {$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_1,
  45. { pass 2 }
  46. {$ifndef NOPASS2}
  47. pass_2,
  48. {$endif}
  49. { parser }
  50. scanner,
  51. pbase,pstatmnt,pdecl,pdecsub,pexports,
  52. { codegen }
  53. tgobj,cgbase,rgobj,rgcpu,
  54. ncgutil
  55. {$ifndef NOOPT}
  56. {$ifdef i386}
  57. ,aopt386
  58. {$else i386}
  59. ,aoptcpu
  60. {$endif i386}
  61. {$endif}
  62. ;
  63. {****************************************************************************
  64. PROCEDURE/FUNCTION BODY PARSING
  65. ****************************************************************************}
  66. function block(islibrary : boolean) : tnode;
  67. var
  68. storepos : tfileposinfo;
  69. begin
  70. { do we have an assembler block without the po_assembler?
  71. we should allow this for Delphi compatibility (PFV) }
  72. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  73. include(aktprocdef.procoptions,po_assembler);
  74. { Handle assembler block different }
  75. if (po_assembler in aktprocdef.procoptions) then
  76. begin
  77. read_declarations(false);
  78. block:=assembler_block;
  79. exit;
  80. end;
  81. if not is_void(aktprocdef.rettype.def) then
  82. begin
  83. { if the current is a function aktprocsym is non nil }
  84. { and there is a local symtable set }
  85. storepos:=akttokenpos;
  86. akttokenpos:=aktprocsym.fileinfo;
  87. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  88. { insert in local symtable }
  89. symtablestack.insert(aktprocdef.funcretsym);
  90. akttokenpos:=storepos;
  91. if 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 start offset to the start of the temp area in the stack }
  275. tg.setfirsttemp(procinfo^.firsttemp_offset);
  276. { ... and generate assembler }
  277. { but set the right switches for entry !! }
  278. aktlocalswitches:=entryswitches;
  279. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  280. aktmaxfpuregisters:=localmaxfpuregisters;
  281. if assigned(code) then
  282. begin
  283. { the procedure is now defined }
  284. aktprocdef.forwarddef:=false;
  285. { only generate the code if no type errors are found, else
  286. finish at least the type checking pass }
  287. {$ifndef NOPASS2}
  288. if (status.errorcount=0) then
  289. begin
  290. generatecode(code);
  291. aktprocdef.code:=code;
  292. {$ifdef testtemp}
  293. if assigned(aktprocdef) and assigned(aktprocdef.localst) then
  294. begin
  295. stackframe:=align(tg.gettempsize+aktprocdef.localst.datasize,4);
  296. end
  297. else
  298. begin
  299. stackframe:=tg.gettempsize;
  300. end;
  301. if lexlevel = 1 then
  302. WriteLn(stackframe);
  303. {$else}
  304. stackframe:=tg.gettempsize;
  305. { if lexlevel = 1 then
  306. WriteLn(stackframe);}
  307. {$endif}
  308. { first generate entry code with the correct position and switches }
  309. aktfilepos:=entrypos;
  310. aktlocalswitches:=entryswitches;
  311. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  312. { FPC_POPADDRSTACK destroys all registers (JM) }
  313. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  314. begin
  315. rg.usedinproc := ALL_REGISTERS;
  316. end;
  317. { now generate exit code with the correct position and switches }
  318. aktfilepos:=exitpos;
  319. aktlocalswitches:=exitswitches;
  320. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  321. { now all the registers used are known }
  322. aktprocdef.usedregisters:=rg.usedinproc;
  323. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  324. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  325. {$ifdef i386}
  326. {$ifndef NoOpt}
  327. if (cs_optimize in aktglobalswitches) and
  328. { do not optimize pure assembler procedures }
  329. ((procinfo^.flags and pi_is_assembler)=0) then
  330. Optimize(procinfo^.aktproccode);
  331. {$endif NoOpt}
  332. {$endif i386}
  333. { save local data (casetable) also in the same file }
  334. if assigned(procinfo^.aktlocaldata) and
  335. (not procinfo^.aktlocaldata.empty) then
  336. begin
  337. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  338. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  339. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  340. end;
  341. { add the procedure to the codesegment }
  342. if (cs_create_smart in aktmoduleswitches) then
  343. codeSegment.concat(Tai_cut.Create);
  344. codeSegment.concatlist(procinfo^.aktproccode);
  345. end
  346. else
  347. do_resulttypepass(code);
  348. {$else NOPASS2}
  349. do_resulttypepass(code);
  350. {$endif NOPASS2}
  351. end;
  352. { ... remove symbol tables }
  353. if lexlevel>=normal_function_level then
  354. symtablestack:=symtablestack.next.next
  355. else
  356. symtablestack:=symtablestack.next;
  357. { ... check for unused symbols }
  358. { but only if there is no asm block }
  359. if assigned(code) then
  360. begin
  361. if (Errorcount=0) then
  362. begin
  363. { check if forwards are resolved }
  364. tstoredsymtable(aktprocdef.localst).check_forwards;
  365. { check if all labels are used }
  366. tstoredsymtable(aktprocdef.localst).checklabels;
  367. { remove cross unit overloads }
  368. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  369. end;
  370. if (procinfo^.flags and pi_uses_asm)=0 then
  371. begin
  372. { not for unit init, becuase the var can be used in finalize,
  373. it will be done in proc_unit }
  374. if not(aktprocdef.proctypeoption
  375. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  376. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  377. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  378. end;
  379. end;
  380. { the local symtables can be deleted, but the parast }
  381. { doesn't, (checking definitons when calling a }
  382. { function }
  383. { not for a inline procedure !! (PM) }
  384. { at lexlevel = 1 localst is the staticsymtable itself }
  385. { so no dispose here !! }
  386. if assigned(code) and
  387. not(cs_browser in aktmoduleswitches) and
  388. (aktprocdef.proccalloption<>pocall_inline) then
  389. begin
  390. if lexlevel>=normal_function_level then
  391. aktprocdef.localst.free;
  392. aktprocdef.localst:=nil;
  393. end;
  394. { all registers can be used again }
  395. rg.resetusableregisters;
  396. { only now we can remove the temps }
  397. tg.resettempgen;
  398. { remove code tree, if not inline procedure }
  399. if assigned(code) and (aktprocdef.proccalloption<>pocall_inline) then
  400. code.free;
  401. { remove class member symbol tables }
  402. while symtablestack.symtabletype=objectsymtable do
  403. symtablestack:=symtablestack.next;
  404. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  405. { restore filepos, the switches are already set }
  406. aktfilepos:=savepos;
  407. { restore labels }
  408. aktexitlabel:=oldexitlabel;
  409. aktexit2label:=oldexit2label;
  410. quickexitlabel:=oldquickexitlabel;
  411. faillabel:=oldfaillabel;
  412. { reset to normal non static function }
  413. if (lexlevel=normal_function_level) then
  414. allow_only_static:=false;
  415. { previous lexlevel }
  416. dec(lexlevel);
  417. end;
  418. {****************************************************************************
  419. PROCEDURE/FUNCTION PARSING
  420. ****************************************************************************}
  421. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  422. var
  423. vs : tvarsym;
  424. s : string;
  425. begin
  426. if tsym(p).typ<>varsym then
  427. exit;
  428. with tvarsym(p) do
  429. begin
  430. if copy(name,1,3)='val' then
  431. begin
  432. s:=Copy(name,4,255);
  433. if not(po_assembler in aktprocdef.procoptions) then
  434. begin
  435. vs:=tvarsym.create(s,vartype);
  436. vs.fileinfo:=fileinfo;
  437. vs.varspez:=varspez;
  438. aktprocdef.localst.insert(vs);
  439. include(vs.varoptions,vo_is_local_copy);
  440. vs.varstate:=vs_assigned;
  441. localvarsym:=vs;
  442. inc(refs); { the para was used to set the local copy ! }
  443. { warnings only on local copy ! }
  444. varstate:=vs_used;
  445. end
  446. else
  447. begin
  448. aktprocdef.parast.rename(name,s);
  449. end;
  450. end;
  451. end;
  452. end;
  453. procedure read_proc;
  454. {
  455. Parses the procedure directives, then parses the procedure body, then
  456. generates the code for it
  457. }
  458. var
  459. oldprocsym : tprocsym;
  460. oldprocdef : tprocdef;
  461. oldprocinfo : pprocinfo;
  462. oldconstsymtable : tsymtable;
  463. oldfilepos : tfileposinfo;
  464. pdflags : word;
  465. begin
  466. { save old state }
  467. oldprocdef:=aktprocdef;
  468. oldprocsym:=aktprocsym;
  469. oldconstsymtable:=constsymtable;
  470. oldprocinfo:=procinfo;
  471. { create a new procedure }
  472. codegen_newprocedure;
  473. with procinfo^ do
  474. begin
  475. parent:=oldprocinfo;
  476. { clear flags }
  477. flags:=0;
  478. { standard frame pointer }
  479. framepointer:=frame_pointer_reg;
  480. { is this a nested function of a method ? }
  481. if assigned(oldprocinfo) then
  482. _class:=oldprocinfo^._class;
  483. end;
  484. parse_proc_dec;
  485. procinfo^.procdef:=aktprocdef;
  486. { set the default function options }
  487. if parse_only then
  488. begin
  489. aktprocdef.forwarddef:=true;
  490. { set also the interface flag, for better error message when the
  491. implementation doesn't much this header }
  492. aktprocdef.interfacedef:=true;
  493. pdflags:=pd_interface;
  494. end
  495. else
  496. begin
  497. pdflags:=pd_body;
  498. if current_module.in_implementation then
  499. pdflags:=pdflags or pd_implemen;
  500. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  501. pdflags:=pdflags or pd_global;
  502. procinfo^.exported:=false;
  503. aktprocdef.forwarddef:=false;
  504. end;
  505. { parse the directives that may follow }
  506. inc(lexlevel);
  507. parse_proc_directives(pdflags);
  508. dec(lexlevel);
  509. { hint directives, these can be separated by semicolons here,
  510. that need to be handled here with a loop (PFV) }
  511. while try_consume_hintdirective(aktprocsym.symoptions) do
  512. Consume(_SEMICOLON);
  513. { set aktfilepos to the beginning of the function declaration }
  514. oldfilepos:=aktfilepos;
  515. aktfilepos:=aktprocdef.fileinfo;
  516. { For varargs directive also cdecl and external must be defined }
  517. if (po_varargs in aktprocdef.procoptions) then
  518. begin
  519. { check first for external in the interface, if available there
  520. then the cdecl must also be there since there is no implementation
  521. available to contain it }
  522. if parse_only then
  523. begin
  524. { if external is available, then cdecl must also be available }
  525. if (po_external in aktprocdef.procoptions) and
  526. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  527. Message(parser_e_varargs_need_cdecl_and_external);
  528. end
  529. else
  530. begin
  531. { both must be defined now }
  532. if not(po_external in aktprocdef.procoptions) or
  533. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  534. Message(parser_e_varargs_need_cdecl_and_external);
  535. end;
  536. end;
  537. { search for forward declarations }
  538. if not proc_add_definition(aktprocsym,aktprocdef) then
  539. begin
  540. { A method must be forward defined (in the object declaration) }
  541. if assigned(procinfo^._class) and
  542. (not assigned(oldprocinfo^._class)) then
  543. begin
  544. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  545. aktprocsym.write_parameter_lists(aktprocdef);
  546. end
  547. else
  548. begin
  549. { Give a better error if there is a forward def in the interface and only
  550. a single implementation }
  551. if (not aktprocdef.forwarddef) and
  552. assigned(aktprocsym.defs^.next) and
  553. aktprocsym.defs^.def.forwarddef and
  554. aktprocsym.defs^.def.interfacedef and
  555. not(assigned(aktprocsym.defs^.next^.next)) then
  556. begin
  557. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  558. aktprocsym.write_parameter_lists(aktprocdef);
  559. end
  560. else
  561. begin
  562. { check the global flag, for delphi this is not
  563. required }
  564. if not(m_delphi in aktmodeswitches) and
  565. ((procinfo^.flags and pi_is_global)<>0) then
  566. Message(parser_e_overloaded_must_be_all_global);
  567. end;
  568. end;
  569. end;
  570. { update procinfo, because the aktprocdef can be
  571. changed by check_identical_proc (PFV) }
  572. procinfo^.procdef:=aktprocdef;
  573. {$ifdef i386}
  574. { add implicit pushes for interrupt routines }
  575. if (po_interrupt in aktprocdef.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(aktprocdef.rettype.def) then
  585. begin
  586. procinfo^.return_offset:=procinfo^.para_offset;
  587. inc(procinfo^.para_offset,pointer_size);
  588. end;
  589. { allows to access the parameters of main functions in nested functions }
  590. aktprocdef.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 aktprocdef.forwarddef) then
  595. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  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. aktprocdef.fullprocname);
  603. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  604. { set _FAIL as keyword if constructor }
  605. if (aktprocdef.proctypeoption=potype_constructor) then
  606. tokeninfo^[_FAIL].keyword:=m_all;
  607. if assigned(aktprocdef._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 (aktprocdef.proctypeoption=potype_constructor) then
  612. tokeninfo^[_FAIL].keyword:=m_none;
  613. if assigned(aktprocdef._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. {$ifdef notused}
  622. { restore the interface order to maintain CRC values PM }
  623. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  624. begin
  625. stdef:=aktprocdef;
  626. aktprocdef:=stdef.nextoverloaded;
  627. stdef.nextoverloaded:=prevdef.nextoverloaded;
  628. prevdef.nextoverloaded:=stdef;
  629. end;
  630. {$endif notused}
  631. aktprocsym:=oldprocsym;
  632. aktprocdef:=oldprocdef;
  633. procinfo:=oldprocinfo;
  634. otsym:=nil;
  635. end;
  636. {****************************************************************************
  637. DECLARATION PARSING
  638. ****************************************************************************}
  639. { search in symtablestack for not complete classes }
  640. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  641. begin
  642. if (tsym(p).typ=typesym) and
  643. (ttypesym(p).restype.def.deftype=objectdef) and
  644. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  645. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  646. end;
  647. procedure read_declarations(islibrary : boolean);
  648. procedure Not_supported_for_inline(t : ttoken);
  649. begin
  650. if assigned(aktprocsym) and
  651. (aktprocdef.proccalloption=pocall_inline) then
  652. Begin
  653. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  654. Message(parser_w_inlining_disabled);
  655. aktprocdef.proccalloption:=pocall_fpccall;
  656. End;
  657. end;
  658. begin
  659. repeat
  660. case token of
  661. _LABEL:
  662. begin
  663. Not_supported_for_inline(token);
  664. label_dec;
  665. end;
  666. _CONST:
  667. begin
  668. Not_supported_for_inline(token);
  669. const_dec;
  670. end;
  671. _TYPE:
  672. begin
  673. Not_supported_for_inline(token);
  674. type_dec;
  675. end;
  676. _VAR:
  677. var_dec;
  678. _THREADVAR:
  679. threadvar_dec;
  680. _CONSTRUCTOR,_DESTRUCTOR,
  681. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  682. begin
  683. Not_supported_for_inline(token);
  684. read_proc;
  685. end;
  686. _RESOURCESTRING:
  687. resourcestring_dec;
  688. _EXPORTS:
  689. begin
  690. Not_supported_for_inline(token);
  691. { here we should be at lexlevel 1, no ? PM }
  692. if (lexlevel<>main_program_level) or
  693. (current_module.is_unit) then
  694. begin
  695. Message(parser_e_syntax_error);
  696. consume_all_until(_SEMICOLON);
  697. end
  698. else if islibrary or (target_info.target in [target_i386_WIN32,target_i386_wdosx,target_i386_Netware])
  699. then // AD
  700. read_exports;
  701. end
  702. else break;
  703. end;
  704. until false;
  705. { check for incomplete class definitions, this is only required
  706. for fpc modes }
  707. if (m_fpc in aktmodeswitches) then
  708. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  709. end;
  710. procedure read_interface_declarations;
  711. begin
  712. {Since the body is now parsed at lexlevel 1, and the declarations
  713. must be parsed at the same lexlevel we increase the lexlevel.}
  714. inc(lexlevel);
  715. repeat
  716. case token of
  717. _CONST :
  718. const_dec;
  719. _TYPE :
  720. type_dec;
  721. _VAR :
  722. var_dec;
  723. _THREADVAR :
  724. threadvar_dec;
  725. _RESOURCESTRING:
  726. resourcestring_dec;
  727. _FUNCTION,
  728. _PROCEDURE,
  729. _OPERATOR :
  730. read_proc;
  731. else
  732. break;
  733. end;
  734. until false;
  735. dec(lexlevel);
  736. { check for incomplete class definitions, this is only required
  737. for fpc modes }
  738. if (m_fpc in aktmodeswitches) then
  739. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  740. end;
  741. end.
  742. {
  743. $Log$
  744. Revision 1.52 2002-05-16 19:46:44 carl
  745. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  746. + try to fix temp allocation (still in ifdef)
  747. + generic constructor calls
  748. + start of tassembler / tmodulebase class cleanup
  749. Revision 1.51 2002/05/14 19:34:49 peter
  750. * removed old logs and updated copyright year
  751. Revision 1.50 2002/05/12 16:53:09 peter
  752. * moved entry and exitcode to ncgutil and cgobj
  753. * foreach gets extra argument for passing local data to the
  754. iterator function
  755. * -CR checks also class typecasts at runtime by changing them
  756. into as
  757. * fixed compiler to cycle with the -CR option
  758. * fixed stabs with elf writer, finally the global variables can
  759. be watched
  760. * removed a lot of routines from cga unit and replaced them by
  761. calls to cgobj
  762. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  763. u32bit then the other is typecasted also to u32bit without giving
  764. a rangecheck warning/error.
  765. * fixed pascal calling method with reversing also the high tree in
  766. the parast, detected by tcalcst3 test
  767. Revision 1.49 2002/04/20 21:32:24 carl
  768. + generic FPC_CHECKPOINTER
  769. + first parameter offset in stack now portable
  770. * rename some constants
  771. + move some cpu stuff to other units
  772. - remove unused constents
  773. * fix stacksize for some targets
  774. * fix generic size problems which depend now on EXTEND_SIZE constant
  775. Revision 1.48 2002/04/19 15:46:02 peter
  776. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  777. in most cases and not written to the ppu
  778. * add mangeledname_prefix() routine to generate the prefix of
  779. manglednames depending on the current procedure, object and module
  780. * removed static procprefix since the mangledname is now build only
  781. on demand from tprocdef.mangledname
  782. Revision 1.47 2002/04/15 19:01:28 carl
  783. + target_info.size_of_pointer -> pointer_Size
  784. Revision 1.46 2002/04/04 18:45:19 carl
  785. + added wdosx support (patch from Pavel)
  786. Revision 1.45 2002/03/31 20:26:36 jonas
  787. + a_loadfpu_* and a_loadmm_* methods in tcg
  788. * register allocation is now handled by a class and is mostly processor
  789. independent (+rgobj.pas and i386/rgcpu.pas)
  790. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  791. * some small improvements and fixes to the optimizer
  792. * some register allocation fixes
  793. * some fpuvaroffset fixes in the unary minus node
  794. * push/popusedregisters is now called rg.save/restoreusedregisters and
  795. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  796. also better optimizable)
  797. * fixed and optimized register saving/restoring for new/dispose nodes
  798. * LOC_FPU locations now also require their "register" field to be set to
  799. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  800. - list field removed of the tnode class because it's not used currently
  801. and can cause hard-to-find bugs
  802. Revision 1.44 2002/01/19 15:37:24 peter
  803. * commited the wrong file :(
  804. Revision 1.43 2002/01/19 15:20:09 peter
  805. * also check at the end of the implementation for incomplete classes
  806. Revision 1.42 2002/01/19 15:12:34 peter
  807. * check for unresolved forward classes in the interface
  808. }