psub.pas 35 KB

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