psub.pas 35 KB

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