psub.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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.usedintregisters:=rg.usedintinproc;
  323. aktprocdef.usedotherregisters:=rg.usedinproc;
  324. procinfo.aktproccode.insertlist(procinfo.aktentrycode);
  325. procinfo.aktproccode.concatlist(procinfo.aktexitcode);
  326. {$ifdef i386}
  327. procinfo.aktproccode.convert_registers;
  328. {$endif}
  329. {$ifndef NoOpt}
  330. if (cs_optimize in aktglobalswitches) and
  331. { do not optimize pure assembler procedures }
  332. ((procinfo.flags and pi_is_assembler)=0) then
  333. optimize(procinfo.aktproccode);
  334. {$endif NoOpt}
  335. { save local data (casetable) also in the same file }
  336. if assigned(procinfo.aktlocaldata) and
  337. (not procinfo.aktlocaldata.empty) then
  338. begin
  339. procinfo.aktproccode.concat(Tai_section.Create(sec_data));
  340. procinfo.aktproccode.concatlist(procinfo.aktlocaldata);
  341. procinfo.aktproccode.concat(Tai_section.Create(sec_code));
  342. end;
  343. { add the procedure to the codesegment }
  344. if (cs_create_smart in aktmoduleswitches) then
  345. codeSegment.concat(Tai_cut.Create);
  346. codeSegment.concatlist(procinfo.aktproccode);
  347. end
  348. else
  349. do_resulttypepass(code);
  350. {$else NOPASS2}
  351. do_resulttypepass(code);
  352. {$endif NOPASS2}
  353. end;
  354. { ... remove symbol tables }
  355. if lexlevel>=normal_function_level then
  356. symtablestack:=symtablestack.next.next
  357. else
  358. symtablestack:=symtablestack.next;
  359. { ... check for unused symbols }
  360. { but only if there is no asm block }
  361. if assigned(code) then
  362. begin
  363. if (Errorcount=0) then
  364. begin
  365. { check if forwards are resolved }
  366. tstoredsymtable(aktprocdef.localst).check_forwards;
  367. { check if all labels are used }
  368. tstoredsymtable(aktprocdef.localst).checklabels;
  369. { remove cross unit overloads }
  370. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  371. end;
  372. if (procinfo.flags and pi_uses_asm)=0 then
  373. begin
  374. { not for unit init, becuase the var can be used in finalize,
  375. it will be done in proc_unit }
  376. if not(aktprocdef.proctypeoption
  377. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  378. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  379. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  380. end;
  381. end;
  382. { the local symtables can be deleted, but the parast }
  383. { doesn't, (checking definitons when calling a }
  384. { function }
  385. { not for a inline procedure !! (PM) }
  386. { at lexlevel = 1 localst is the staticsymtable itself }
  387. { so no dispose here !! }
  388. if assigned(code) and
  389. not(cs_browser in aktmoduleswitches) and
  390. (aktprocdef.proccalloption<>pocall_inline) then
  391. begin
  392. if lexlevel>=normal_function_level then
  393. begin
  394. aktprocdef.localst.free;
  395. end;
  396. aktprocdef.localst:=nil;
  397. end;
  398. { all registers can be used again }
  399. rg.resetusableregisters;
  400. { only now we can remove the temps }
  401. tg.resettempgen;
  402. { remove code tree, if not inline procedure }
  403. if assigned(code) then
  404. begin
  405. { the inline procedure has already got a copy of the tree
  406. stored in aktprocdef.code }
  407. code.free;
  408. if (aktprocdef.proccalloption<>pocall_inline) then
  409. aktprocdef.code:=nil;
  410. end;
  411. { remove class member symbol tables }
  412. while symtablestack.symtabletype=objectsymtable do
  413. symtablestack:=symtablestack.next;
  414. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  415. {$ifdef state_tracking}
  416. { aktstate.destroy;}
  417. {$endif state_tracking}
  418. { restore filepos, the switches are already set }
  419. aktfilepos:=savepos;
  420. { restore labels }
  421. aktexitlabel:=oldexitlabel;
  422. aktexit2label:=oldexit2label;
  423. quickexitlabel:=oldquickexitlabel;
  424. faillabel:=oldfaillabel;
  425. { reset to normal non static function }
  426. if (lexlevel=normal_function_level) then
  427. allow_only_static:=false;
  428. { previous lexlevel }
  429. dec(lexlevel);
  430. end;
  431. {****************************************************************************
  432. PROCEDURE/FUNCTION PARSING
  433. ****************************************************************************}
  434. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  435. var
  436. vs : tvarsym;
  437. s : string;
  438. begin
  439. if tsym(p).typ<>varsym then
  440. exit;
  441. with tvarsym(p) do
  442. begin
  443. if copy(name,1,3)='val' then
  444. begin
  445. s:=Copy(name,4,255);
  446. if not(po_assembler in aktprocdef.procoptions) then
  447. begin
  448. vs:=tvarsym.create(s,vartype);
  449. vs.fileinfo:=fileinfo;
  450. vs.varspez:=varspez;
  451. if not assigned(aktprocdef.localst) then
  452. aktprocdef.insert_localst;
  453. aktprocdef.localst.insert(vs);
  454. aktprocdef.localst.insertvardata(vs);
  455. include(vs.varoptions,vo_is_local_copy);
  456. vs.varstate:=vs_assigned;
  457. localvarsym:=vs;
  458. inc(refs); { the para was used to set the local copy ! }
  459. { warnings only on local copy ! }
  460. varstate:=vs_used;
  461. end
  462. else
  463. begin
  464. aktprocdef.parast.rename(name,s);
  465. end;
  466. end;
  467. end;
  468. end;
  469. procedure read_proc;
  470. {
  471. Parses the procedure directives, then parses the procedure body, then
  472. generates the code for it
  473. }
  474. var
  475. oldprocsym : tprocsym;
  476. oldprocdef : tprocdef;
  477. oldprocinfo : tprocinfo;
  478. oldconstsymtable : tsymtable;
  479. oldfilepos : tfileposinfo;
  480. pdflags : word;
  481. begin
  482. { save old state }
  483. oldprocdef:=aktprocdef;
  484. oldprocsym:=aktprocsym;
  485. oldconstsymtable:=constsymtable;
  486. oldprocinfo:=procinfo;
  487. { create a new procedure }
  488. codegen_newprocedure;
  489. with procinfo do
  490. begin
  491. parent:=oldprocinfo;
  492. { clear flags }
  493. flags:=0;
  494. { standard frame pointer }
  495. framepointer.enum:=R_INTREGISTER;
  496. framepointer.number:=NR_FRAME_POINTER_REG;
  497. { is this a nested function of a method ? }
  498. if assigned(oldprocinfo) then
  499. _class:=oldprocinfo._class;
  500. end;
  501. parse_proc_dec;
  502. procinfo.procdef:=aktprocdef;
  503. { set the default function options }
  504. if parse_only then
  505. begin
  506. aktprocdef.forwarddef:=true;
  507. { set also the interface flag, for better error message when the
  508. implementation doesn't much this header }
  509. aktprocdef.interfacedef:=true;
  510. pdflags:=pd_interface;
  511. end
  512. else
  513. begin
  514. pdflags:=pd_body;
  515. if (not current_module.in_interface) then
  516. pdflags:=pdflags or pd_implemen;
  517. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  518. pdflags:=pdflags or pd_global;
  519. procinfo.exported:=false;
  520. aktprocdef.forwarddef:=false;
  521. end;
  522. { parse the directives that may follow }
  523. inc(lexlevel);
  524. parse_proc_directives(pdflags);
  525. dec(lexlevel);
  526. { hint directives, these can be separated by semicolons here,
  527. that need to be handled here with a loop (PFV) }
  528. while try_consume_hintdirective(aktprocsym.symoptions) do
  529. Consume(_SEMICOLON);
  530. { set aktfilepos to the beginning of the function declaration }
  531. oldfilepos:=aktfilepos;
  532. aktfilepos:=aktprocdef.fileinfo;
  533. { For varargs directive also cdecl and external must be defined }
  534. if (po_varargs in aktprocdef.procoptions) then
  535. begin
  536. { check first for external in the interface, if available there
  537. then the cdecl must also be there since there is no implementation
  538. available to contain it }
  539. if parse_only then
  540. begin
  541. { if external is available, then cdecl must also be available }
  542. if (po_external in aktprocdef.procoptions) and
  543. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  544. Message(parser_e_varargs_need_cdecl_and_external);
  545. end
  546. else
  547. begin
  548. { both must be defined now }
  549. if not(po_external in aktprocdef.procoptions) or
  550. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  551. Message(parser_e_varargs_need_cdecl_and_external);
  552. end;
  553. end;
  554. { search for forward declarations }
  555. if not proc_add_definition(aktprocsym,aktprocdef) then
  556. begin
  557. { A method must be forward defined (in the object declaration) }
  558. if assigned(procinfo._class) and
  559. (not assigned(oldprocinfo._class)) then
  560. begin
  561. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  562. aktprocsym.write_parameter_lists(aktprocdef);
  563. end
  564. else
  565. begin
  566. { Give a better error if there is a forward def in the interface and only
  567. a single implementation }
  568. if (not aktprocdef.forwarddef) and
  569. (not aktprocdef.interfacedef) and
  570. (aktprocsym.procdef_count>1) and
  571. aktprocsym.first_procdef.forwarddef and
  572. aktprocsym.first_procdef.interfacedef and
  573. not(aktprocsym.procdef_count>2) then
  574. begin
  575. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  576. aktprocsym.write_parameter_lists(aktprocdef);
  577. end
  578. else
  579. begin
  580. { check the global flag, for delphi this is not
  581. required }
  582. if not(m_delphi in aktmodeswitches) and
  583. ((procinfo.flags and pi_is_global)<>0) then
  584. Message(parser_e_overloaded_must_be_all_global);
  585. end;
  586. end;
  587. end;
  588. { update procinfo, because the aktprocdef can be
  589. changed by check_identical_proc (PFV) }
  590. procinfo.procdef:=aktprocdef;
  591. {$ifdef i386}
  592. { add implicit pushes for interrupt routines }
  593. if (po_interrupt in aktprocdef.procoptions) then
  594. procinfo.allocate_interrupt_stackframe;
  595. {$endif i386}
  596. { pointer to the return value ? }
  597. if paramanager.ret_in_param(aktprocdef.rettype.def,aktprocdef.proccalloption)
  598. {$ifdef m68k}
  599. and not (aktprocdef.proccalloption in [pocall_cdecl])
  600. {$endif m68k}
  601. then
  602. begin
  603. procinfo.return_offset:=procinfo.para_offset;
  604. inc(procinfo.para_offset,pointer_size);
  605. end;
  606. { allows to access the parameters of main functions in nested functions }
  607. aktprocdef.parast.address_fixup:=procinfo.para_offset;
  608. { when it is a value para and it needs a local copy then rename
  609. the parameter and insert a copy in the localst. This is not done
  610. for assembler procedures }
  611. if (not parse_only) and (not aktprocdef.forwarddef) then
  612. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  613. procinfo.after_header;
  614. { restore file pos }
  615. aktfilepos:=oldfilepos;
  616. { compile procedure when a body is needed }
  617. if (pdflags and pd_body)<>0 then
  618. begin
  619. Message1(parser_d_procedure_start,aktprocdef.fullprocname);
  620. if assigned(aktprocsym.owner) then
  621. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  622. { set _FAIL as keyword if constructor }
  623. if (aktprocdef.proctypeoption=potype_constructor) then
  624. tokeninfo^[_FAIL].keyword:=m_all;
  625. if assigned(aktprocdef._class) then
  626. tokeninfo^[_SELF].keyword:=m_all;
  627. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo._class));
  628. { reset _FAIL as normal }
  629. if (aktprocdef.proctypeoption=potype_constructor) then
  630. tokeninfo^[_FAIL].keyword:=m_none;
  631. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  632. tokeninfo^[_SELF].keyword:=m_none;
  633. consume(_SEMICOLON);
  634. end;
  635. { close }
  636. codegen_doneprocedure;
  637. { Restore old state }
  638. constsymtable:=oldconstsymtable;
  639. {$ifdef notused}
  640. { restore the interface order to maintain CRC values PM }
  641. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  642. begin
  643. stdef:=aktprocdef;
  644. aktprocdef:=stdef.nextoverloaded;
  645. stdef.nextoverloaded:=prevdef.nextoverloaded;
  646. prevdef.nextoverloaded:=stdef;
  647. end;
  648. {$endif notused}
  649. { release procsym when it was not stored in the symtable }
  650. if not assigned(aktprocsym.owner) then
  651. begin
  652. aktprocsym.free;
  653. aktprocdef.procsym:=nil;
  654. end;
  655. aktprocsym:=oldprocsym;
  656. aktprocdef:=oldprocdef;
  657. procinfo:=oldprocinfo;
  658. otsym:=nil;
  659. end;
  660. {****************************************************************************
  661. DECLARATION PARSING
  662. ****************************************************************************}
  663. { search in symtablestack for not complete classes }
  664. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  665. begin
  666. if (tsym(p).typ=typesym) and
  667. (ttypesym(p).restype.def.deftype=objectdef) and
  668. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  669. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  670. end;
  671. procedure read_declarations(islibrary : boolean);
  672. procedure Not_supported_for_inline(t : ttoken);
  673. begin
  674. if assigned(aktprocsym) and
  675. (aktprocdef.proccalloption=pocall_inline) then
  676. Begin
  677. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  678. Message(parser_w_inlining_disabled);
  679. aktprocdef.proccalloption:=pocall_fpccall;
  680. End;
  681. end;
  682. begin
  683. repeat
  684. case token of
  685. _LABEL:
  686. begin
  687. Not_supported_for_inline(token);
  688. label_dec;
  689. end;
  690. _CONST:
  691. begin
  692. Not_supported_for_inline(token);
  693. const_dec;
  694. end;
  695. _TYPE:
  696. begin
  697. Not_supported_for_inline(token);
  698. type_dec;
  699. end;
  700. _VAR:
  701. var_dec;
  702. _THREADVAR:
  703. threadvar_dec;
  704. _CONSTRUCTOR,_DESTRUCTOR,
  705. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  706. begin
  707. Not_supported_for_inline(token);
  708. read_proc;
  709. end;
  710. _RESOURCESTRING:
  711. resourcestring_dec;
  712. _EXPORTS:
  713. begin
  714. Not_supported_for_inline(token);
  715. { here we should be at lexlevel 1, no ? PM }
  716. if (lexlevel<>main_program_level) or
  717. (current_module.is_unit) then
  718. begin
  719. Message(parser_e_syntax_error);
  720. consume_all_until(_SEMICOLON);
  721. end
  722. else if islibrary or (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware])
  723. then // AD
  724. read_exports;
  725. end
  726. else break;
  727. end;
  728. until false;
  729. { check for incomplete class definitions, this is only required
  730. for fpc modes }
  731. if (m_fpc in aktmodeswitches) then
  732. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  733. end;
  734. procedure read_interface_declarations;
  735. begin
  736. {Since the body is now parsed at lexlevel 1, and the declarations
  737. must be parsed at the same lexlevel we increase the lexlevel.}
  738. inc(lexlevel);
  739. repeat
  740. case token of
  741. _CONST :
  742. const_dec;
  743. _TYPE :
  744. type_dec;
  745. _VAR :
  746. var_dec;
  747. _THREADVAR :
  748. threadvar_dec;
  749. _RESOURCESTRING:
  750. resourcestring_dec;
  751. _FUNCTION,
  752. _PROCEDURE,
  753. _OPERATOR :
  754. read_proc;
  755. else
  756. break;
  757. end;
  758. until false;
  759. dec(lexlevel);
  760. { check for incomplete class definitions, this is only required
  761. for fpc modes }
  762. if (m_fpc in aktmodeswitches) then
  763. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  764. end;
  765. end.
  766. {
  767. $Log$
  768. Revision 1.92 2003-02-19 22:00:14 daniel
  769. * Code generator converted to new register notation
  770. - Horribily outdated todo.txt removed
  771. Revision 1.91 2003/01/09 21:52:37 peter
  772. * merged some verbosity options.
  773. * V_LineInfo is a verbosity flag to include line info
  774. Revision 1.90 2003/01/09 20:40:59 daniel
  775. * Converted some code in cgx86.pas to new register numbering
  776. Revision 1.89 2003/01/09 15:49:56 daniel
  777. * Added register conversion
  778. Revision 1.88 2003/01/08 18:43:56 daniel
  779. * Tregister changed into a record
  780. Revision 1.87 2003/01/03 20:35:08 peter
  781. * check also interfacedef when checking for matching forwarddef
  782. Revision 1.86 2003/01/02 11:14:02 michael
  783. + Patch from peter to support initial values for local variables
  784. Revision 1.85 2002/12/29 18:59:34 peter
  785. * fixed parsing of declarations before asm statement
  786. Revision 1.84 2002/12/29 18:25:18 peter
  787. * parse declarations before check _ASM token
  788. Revision 1.83 2002/12/29 14:57:50 peter
  789. * unit loading changed to first register units and load them
  790. afterwards. This is needed to support uses xxx in yyy correctly
  791. * unit dependency check fixed
  792. Revision 1.82 2002/12/25 01:26:56 peter
  793. * duplicate procsym-unitsym fix
  794. Revision 1.81 2002/12/15 13:37:15 peter
  795. * don't include uf_init for library. The code is already called and
  796. does not need to be in the initfinal table
  797. Revision 1.80 2002/12/07 14:27:09 carl
  798. * 3% memory optimization
  799. * changed some types
  800. + added type checking with different size for call node and for
  801. parameters
  802. Revision 1.79 2002/11/25 18:43:32 carl
  803. - removed the invalid if <> checking (Delphi is strange on this)
  804. + implemented abstract warning on instance creation of class with
  805. abstract methods.
  806. * some error message cleanups
  807. Revision 1.78 2002/11/25 17:43:23 peter
  808. * splitted defbase in defutil,symutil,defcmp
  809. * merged isconvertable and is_equal into compare_defs(_ext)
  810. * made operator search faster by walking the list only once
  811. Revision 1.77 2002/11/23 22:50:06 carl
  812. * some small speed optimizations
  813. + added several new warnings/hints
  814. Revision 1.76 2002/11/18 17:31:58 peter
  815. * pass proccalloption to ret_in_xxx and push_xxx functions
  816. Revision 1.75 2002/11/17 16:31:57 carl
  817. * memory optimization (3-4%) : cleanup of tai fields,
  818. cleanup of tdef and tsym fields.
  819. * make it work for m68k
  820. Revision 1.74 2002/11/15 01:58:53 peter
  821. * merged changes from 1.0.7 up to 04-11
  822. - -V option for generating bug report tracing
  823. - more tracing for option parsing
  824. - errors for cdecl and high()
  825. - win32 import stabs
  826. - win32 records<=8 are returned in eax:edx (turned off by default)
  827. - heaptrc update
  828. - more info for temp management in .s file with EXTDEBUG
  829. Revision 1.73 2002/11/09 15:32:30 carl
  830. * noopt for non-i386 targets
  831. Revision 1.72 2002/09/10 20:31:48 florian
  832. * call to procinfo.after_header added
  833. Revision 1.71 2002/09/07 15:25:07 peter
  834. * old logs removed and tabs fixed
  835. Revision 1.70 2002/09/03 16:26:27 daniel
  836. * Make Tprocdef.defs protected
  837. Revision 1.69 2002/08/25 19:25:20 peter
  838. * sym.insert_in_data removed
  839. * symtable.insertvardata/insertconstdata added
  840. * removed insert_in_data call from symtable.insert, it needs to be
  841. called separatly. This allows to deref the address calculation
  842. * procedures now calculate the parast addresses after the procedure
  843. directives are parsed. This fixes the cdecl parast problem
  844. * push_addr_param has an extra argument that specifies if cdecl is used
  845. or not
  846. Revision 1.68 2002/08/17 09:23:41 florian
  847. * first part of procinfo rewrite
  848. Revision 1.67 2002/08/16 14:24:59 carl
  849. * issameref() to test if two references are the same (then emit no opcodes)
  850. + ret_in_reg to replace ret_in_acc
  851. (fix some register allocation bugs at the same time)
  852. + save_std_register now has an extra parameter which is the
  853. usedinproc registers
  854. Revision 1.66 2002/08/11 14:32:27 peter
  855. * renamed current_library to objectlibrary
  856. Revision 1.65 2002/08/11 13:24:13 peter
  857. * saving of asmsymbols in ppu supported
  858. * asmsymbollist global is removed and moved into a new class
  859. tasmlibrarydata that will hold the info of a .a file which
  860. corresponds with a single module. Added librarydata to tmodule
  861. to keep the library info stored for the module. In the future the
  862. objectfiles will also be stored to the tasmlibrarydata class
  863. * all getlabel/newasmsymbol and friends are moved to the new class
  864. Revision 1.64 2002/08/09 19:14:28 carl
  865. * fixed stackframe parameter (should only contain local size),
  866. set to zero currently
  867. Revision 1.63 2002/08/06 20:55:22 florian
  868. * first part of ppc calling conventions fix
  869. Revision 1.62 2002/07/26 21:15:41 florian
  870. * rewrote the system handling
  871. Revision 1.61 2002/07/20 11:57:56 florian
  872. * types.pas renamed to defbase.pas because D6 contains a types
  873. unit so this would conflicts if D6 programms are compiled
  874. + Willamette/SSE2 instructions to assembler added
  875. Revision 1.60 2002/07/19 11:41:36 daniel
  876. * State tracker work
  877. * The whilen and repeatn are now completely unified into whilerepeatn. This
  878. allows the state tracker to change while nodes automatically into
  879. repeat nodes.
  880. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  881. 'not(a>b)' is optimized into 'a<=b'.
  882. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  883. by removing the notn and later switchting the true and falselabels. The
  884. same is done with 'repeat until not a'.
  885. Revision 1.59 2002/07/15 18:03:15 florian
  886. * readded removed changes
  887. Revision 1.57 2002/07/11 14:41:28 florian
  888. * start of the new generic parameter handling
  889. Revision 1.58 2002/07/14 18:00:44 daniel
  890. + Added the beginning of a state tracker. This will track the values of
  891. variables through procedures and optimize things away.
  892. Revision 1.56 2002/07/07 09:52:32 florian
  893. * powerpc target fixed, very simple units can be compiled
  894. * some basic stuff for better callparanode handling, far from being finished
  895. Revision 1.55 2002/07/04 20:43:01 florian
  896. * first x86-64 patches
  897. Revision 1.54 2002/07/01 18:46:25 peter
  898. * internal linker
  899. * reorganized aasm layer
  900. Revision 1.53 2002/05/18 13:34:14 peter
  901. * readded missing revisions
  902. Revision 1.52 2002/05/16 19:46:44 carl
  903. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  904. + try to fix temp allocation (still in ifdef)
  905. + generic constructor calls
  906. + start of tassembler / tmodulebase class cleanup
  907. Revision 1.51 2002/05/14 19:34:49 peter
  908. * removed old logs and updated copyright year
  909. }