psub.pas 38 KB

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