psub.pas 39 KB

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