psub.pas 37 KB

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