psub.pas 41 KB

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