psub.pas 37 KB

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