psub.pas 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. {$ifdef powerpc}
  21. {$define newcg}
  22. {$endif powerpc}
  23. interface
  24. procedure compile_proc_body(make_global,parent_has_class:boolean);
  25. { reads the declaration blocks }
  26. procedure read_declarations(islibrary : boolean);
  27. { reads declarations in the interface part of a unit }
  28. procedure read_interface_declarations;
  29. implementation
  30. uses
  31. { common }
  32. cutils,cclasses,
  33. { global }
  34. globtype,globals,tokens,verbose,comphook,
  35. systems,
  36. { aasm }
  37. cpubase,cpuinfo,aasm,
  38. { symtable }
  39. symconst,symbase,symdef,symsym,symtype,symtable,types,
  40. ppu,fmodule,
  41. { pass 1 }
  42. node,
  43. nbas,
  44. { pass 2 }
  45. {$ifndef NOPASS2}
  46. pass_1,pass_2,
  47. {$endif}
  48. { parser }
  49. scanner,
  50. pbase,pstatmnt,pdecl,pdecsub,pexports,
  51. { codegen }
  52. tgobj,cgbase,rgobj,
  53. rgcpu,
  54. cga
  55. {$ifndef NOOPT}
  56. {$ifdef i386}
  57. ,aopt386
  58. {$else i386}
  59. ,aoptcpu
  60. {$endif i386}
  61. {$endif}
  62. ;
  63. {****************************************************************************
  64. PROCEDURE/FUNCTION BODY PARSING
  65. ****************************************************************************}
  66. function block(islibrary : boolean) : tnode;
  67. var
  68. storepos : tfileposinfo;
  69. begin
  70. { do we have an assembler block without the po_assembler?
  71. we should allow this for Delphi compatibility (PFV) }
  72. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  73. include(aktprocdef.procoptions,po_assembler);
  74. { Handle assembler block different }
  75. if (po_assembler in aktprocdef.procoptions) then
  76. begin
  77. read_declarations(false);
  78. block:=assembler_block;
  79. exit;
  80. end;
  81. if not is_void(aktprocdef.rettype.def) then
  82. begin
  83. { if the current is a function aktprocsym is non nil }
  84. { and there is a local symtable set }
  85. storepos:=akttokenpos;
  86. akttokenpos:=aktprocsym.fileinfo;
  87. aktprocdef.funcretsym:=tfuncretsym.create(aktprocsym.name,aktprocdef.rettype);
  88. { insert in local symtable }
  89. symtablestack.insert(aktprocdef.funcretsym);
  90. akttokenpos:=storepos;
  91. if ret_in_acc(aktprocdef.rettype.def) or
  92. (aktprocdef.rettype.def.deftype=floatdef) then
  93. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  94. { insert result also if support is on }
  95. if (m_result in aktmodeswitches) then
  96. begin
  97. aktprocdef.resultfuncretsym:=tfuncretsym.create('RESULT',aktprocdef.rettype);
  98. symtablestack.insert(aktprocdef.resultfuncretsym);
  99. end;
  100. end;
  101. read_declarations(islibrary);
  102. { temporary space is set, while the BEGIN of the procedure }
  103. if (symtablestack.symtabletype=localsymtable) then
  104. procinfo^.firsttemp_offset := -symtablestack.datasize
  105. else
  106. procinfo^.firsttemp_offset := 0;
  107. { space for the return value }
  108. { !!!!! this means that we can not set the return value
  109. in a subfunction !!!!! }
  110. { because we don't know yet where the address is }
  111. if not is_void(aktprocdef.rettype.def) then
  112. begin
  113. if ret_in_acc(aktprocdef.rettype.def) or (aktprocdef.rettype.def.deftype=floatdef) then
  114. begin
  115. { the space has been set in the local symtable }
  116. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  117. if ((procinfo^.flags and pi_operator)<>0) and
  118. assigned(otsym) then
  119. otsym.address:=-procinfo^.return_offset;
  120. { eax is modified by a function }
  121. include(rg.usedinproc,accumulator);
  122. if (sizeof(aword) < 8) and
  123. (is_64bitint(aktprocdef.rettype.def)) then
  124. include(rg.usedinproc,accumulatorhigh);
  125. end;
  126. end;
  127. {Unit initialization?.}
  128. if (lexlevel=unit_init_level) and (current_module.is_unit)
  129. or islibrary then
  130. begin
  131. if (token=_END) then
  132. begin
  133. consume(_END);
  134. { We need at least a node, else the entry/exit code is not
  135. generated and thus no PASCALMAIN symbol which we need (PFV) }
  136. if islibrary then
  137. block:=cnothingnode.create
  138. else
  139. block:=nil;
  140. end
  141. else
  142. begin
  143. if token=_INITIALIZATION then
  144. begin
  145. current_module.flags:=current_module.flags or uf_init;
  146. block:=statement_block(_INITIALIZATION);
  147. end
  148. else if (token=_FINALIZATION) then
  149. begin
  150. if (current_module.flags and uf_finalize)<>0 then
  151. block:=statement_block(_FINALIZATION)
  152. else
  153. begin
  154. { can we allow no INITIALIZATION for DLL ??
  155. I think it should work PM }
  156. block:=nil;
  157. exit;
  158. end;
  159. end
  160. else
  161. begin
  162. current_module.flags:=current_module.flags or uf_init;
  163. block:=statement_block(_BEGIN);
  164. end;
  165. end;
  166. end
  167. else
  168. block:=statement_block(_BEGIN);
  169. end;
  170. {****************************************************************************
  171. PROCEDURE/FUNCTION COMPILING
  172. ****************************************************************************}
  173. procedure compile_proc_body(make_global,parent_has_class:boolean);
  174. {
  175. Compile the body of a procedure
  176. }
  177. var
  178. oldexitlabel,oldexit2label : tasmlabel;
  179. oldfaillabel,oldquickexitlabel:tasmlabel;
  180. _class,hp:tobjectdef;
  181. { switches can change inside the procedure }
  182. entryswitches, exitswitches : tlocalswitches;
  183. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  184. { code for the subroutine as tree }
  185. code:tnode;
  186. { size of the local strackframe }
  187. stackframe:longint;
  188. { true when no stackframe is required }
  189. nostackframe:boolean;
  190. { number of bytes which have to be cleared by RET }
  191. parasize:longint;
  192. { filepositions }
  193. entrypos,
  194. savepos,
  195. exitpos : tfileposinfo;
  196. begin
  197. { calculate the lexical level }
  198. inc(lexlevel);
  199. if lexlevel>32 then
  200. Message(parser_e_too_much_lexlevel);
  201. { static is also important for local procedures !! }
  202. if (po_staticmethod in aktprocdef.procoptions) then
  203. allow_only_static:=true
  204. else if (lexlevel=normal_function_level) then
  205. allow_only_static:=false;
  206. { save old labels }
  207. oldexitlabel:=aktexitlabel;
  208. oldexit2label:=aktexit2label;
  209. oldquickexitlabel:=quickexitlabel;
  210. oldfaillabel:=faillabel;
  211. { get new labels }
  212. getlabel(aktexitlabel);
  213. getlabel(aktexit2label);
  214. { exit for fail in constructors }
  215. if (aktprocdef.proctypeoption=potype_constructor) then
  216. begin
  217. getlabel(faillabel);
  218. getlabel(quickexitlabel);
  219. end;
  220. { reset break and continue labels }
  221. block_type:=bt_general;
  222. aktbreaklabel:=nil;
  223. aktcontinuelabel:=nil;
  224. { insert symtables for the class, by only if it is no nested function }
  225. if assigned(procinfo^._class) and not(parent_has_class) then
  226. begin
  227. { insert them in the reverse order ! }
  228. hp:=nil;
  229. repeat
  230. _class:=procinfo^._class;
  231. while _class.childof<>hp do
  232. _class:=_class.childof;
  233. hp:=_class;
  234. _class.symtable.next:=symtablestack;
  235. symtablestack:=_class.symtable;
  236. until hp=procinfo^._class;
  237. end;
  238. { insert parasymtable in symtablestack}
  239. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  240. for checking of same names used in interface and implementation !! }
  241. if lexlevel>=normal_function_level then
  242. begin
  243. aktprocdef.parast.next:=symtablestack;
  244. symtablestack:=aktprocdef.parast;
  245. symtablestack.symtablelevel:=lexlevel;
  246. end;
  247. { insert localsymtable in symtablestack}
  248. aktprocdef.localst.next:=symtablestack;
  249. symtablestack:=aktprocdef.localst;
  250. symtablestack.symtablelevel:=lexlevel;
  251. { constant symbols are inserted in this symboltable }
  252. constsymtable:=symtablestack;
  253. { reset the temporary memory }
  254. rg.cleartempgen;
  255. rg.usedinproc:=[];
  256. { save entry info }
  257. entrypos:=aktfilepos;
  258. entryswitches:=aktlocalswitches;
  259. localmaxfpuregisters:=aktmaxfpuregisters;
  260. { parse the code ... }
  261. code:=block(current_module.islibrary);
  262. { get a better entry point }
  263. if assigned(code) then
  264. entrypos:=code.fileinfo;
  265. { save exit info }
  266. exitswitches:=aktlocalswitches;
  267. exitpos:=last_endtoken_filepos;
  268. { save current filepos }
  269. savepos:=aktfilepos;
  270. {When we are called to compile the body of a unit, aktprocsym should
  271. point to the unit initialization. If the unit has no initialization,
  272. aktprocsym=nil. But in that case code=nil. hus we should check for
  273. code=nil, when we use aktprocsym.}
  274. { set the start offset to the start of the temp area in the stack }
  275. tg.setfirsttemp(procinfo^.firsttemp_offset);
  276. { ... and generate assembler }
  277. { but set the right switches for entry !! }
  278. aktlocalswitches:=entryswitches;
  279. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  280. aktmaxfpuregisters:=localmaxfpuregisters;
  281. if assigned(code) then
  282. begin
  283. { the procedure is now defined }
  284. aktprocdef.forwarddef:=false;
  285. { only generate the code if no type errors are found, else
  286. finish at least the type checking pass }
  287. {$ifndef NOPASS2}
  288. if (status.errorcount=0) then
  289. begin
  290. generatecode(code);
  291. aktprocdef.code:=code;
  292. stackframe:=tg.gettempsize;
  293. { first generate entry code with the correct position and switches }
  294. aktfilepos:=entrypos;
  295. aktlocalswitches:=entryswitches;
  296. {$ifdef newcg}
  297. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  298. {$else newcg}
  299. genentrycode(procinfo^.aktentrycode,make_global,stackframe,parasize,nostackframe,false);
  300. {$endif newcg}
  301. { FPC_POPADDRSTACK destroys all registers (JM) }
  302. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  303. begin
  304. rg.usedinproc := ALL_REGISTERS;
  305. end;
  306. { now generate exit code with the correct position and switches }
  307. aktfilepos:=exitpos;
  308. aktlocalswitches:=exitswitches;
  309. {$ifdef newcg}
  310. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  311. {$else newcg}
  312. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  313. {$endif newcg}
  314. { now all the registers used are known }
  315. aktprocdef.usedregisters:=rg.usedinproc;
  316. procinfo^.aktproccode.insertlist(procinfo^.aktentrycode);
  317. procinfo^.aktproccode.concatlist(procinfo^.aktexitcode);
  318. {$ifdef i386}
  319. {$ifndef NoOpt}
  320. if (cs_optimize in aktglobalswitches) and
  321. { do not optimize pure assembler procedures }
  322. ((procinfo^.flags and pi_is_assembler)=0) then
  323. Optimize(procinfo^.aktproccode);
  324. {$endif NoOpt}
  325. {$endif i386}
  326. { save local data (casetable) also in the same file }
  327. if assigned(procinfo^.aktlocaldata) and
  328. (not procinfo^.aktlocaldata.empty) then
  329. begin
  330. procinfo^.aktproccode.concat(Tai_section.Create(sec_data));
  331. procinfo^.aktproccode.concatlist(procinfo^.aktlocaldata);
  332. procinfo^.aktproccode.concat(Tai_section.Create(sec_code));
  333. end;
  334. { add the procedure to the codesegment }
  335. if (cs_create_smart in aktmoduleswitches) then
  336. codeSegment.concat(Tai_cut.Create);
  337. codeSegment.concatlist(procinfo^.aktproccode);
  338. end
  339. else
  340. do_resulttypepass(code);
  341. {$else NOPASS2}
  342. do_resulttypepass(code);
  343. {$endif NOPASS2}
  344. end;
  345. { ... remove symbol tables }
  346. if lexlevel>=normal_function_level then
  347. symtablestack:=symtablestack.next.next
  348. else
  349. symtablestack:=symtablestack.next;
  350. { ... check for unused symbols }
  351. { but only if there is no asm block }
  352. if assigned(code) then
  353. begin
  354. if (Errorcount=0) then
  355. begin
  356. { check if forwards are resolved }
  357. tstoredsymtable(aktprocdef.localst).check_forwards;
  358. { check if all labels are used }
  359. tstoredsymtable(aktprocdef.localst).checklabels;
  360. { remove cross unit overloads }
  361. tstoredsymtable(aktprocdef.localst).unchain_overloaded;
  362. end;
  363. if (procinfo^.flags and pi_uses_asm)=0 then
  364. begin
  365. { not for unit init, becuase the var can be used in finalize,
  366. it will be done in proc_unit }
  367. if not(aktprocdef.proctypeoption
  368. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  369. tstoredsymtable(aktprocdef.localst).allsymbolsused;
  370. tstoredsymtable(aktprocdef.parast).allsymbolsused;
  371. end;
  372. end;
  373. { the local symtables can be deleted, but the parast }
  374. { doesn't, (checking definitons when calling a }
  375. { function }
  376. { not for a inline procedure !! (PM) }
  377. { at lexlevel = 1 localst is the staticsymtable itself }
  378. { so no dispose here !! }
  379. if assigned(code) and
  380. not(cs_browser in aktmoduleswitches) and
  381. (aktprocdef.proccalloption<>pocall_inline) then
  382. begin
  383. if lexlevel>=normal_function_level then
  384. aktprocdef.localst.free;
  385. aktprocdef.localst:=nil;
  386. end;
  387. { all registers can be used again }
  388. rg.resetusableregisters;
  389. { only now we can remove the temps }
  390. tg.resettempgen;
  391. { remove code tree, if not inline procedure }
  392. if assigned(code) and (aktprocdef.proccalloption<>pocall_inline) then
  393. code.free;
  394. { remove class member symbol tables }
  395. while symtablestack.symtabletype=objectsymtable do
  396. symtablestack:=symtablestack.next;
  397. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  398. { restore filepos, the switches are already set }
  399. aktfilepos:=savepos;
  400. { restore labels }
  401. aktexitlabel:=oldexitlabel;
  402. aktexit2label:=oldexit2label;
  403. quickexitlabel:=oldquickexitlabel;
  404. faillabel:=oldfaillabel;
  405. { reset to normal non static function }
  406. if (lexlevel=normal_function_level) then
  407. allow_only_static:=false;
  408. { previous lexlevel }
  409. dec(lexlevel);
  410. end;
  411. {****************************************************************************
  412. PROCEDURE/FUNCTION PARSING
  413. ****************************************************************************}
  414. procedure checkvaluepara(p:tnamedindexitem);
  415. var
  416. vs : tvarsym;
  417. s : string;
  418. begin
  419. with tvarsym(p) do
  420. begin
  421. if copy(name,1,3)='val' then
  422. begin
  423. s:=Copy(name,4,255);
  424. if not(po_assembler in aktprocdef.procoptions) then
  425. begin
  426. vs:=tvarsym.create(s,vartype);
  427. vs.fileinfo:=fileinfo;
  428. vs.varspez:=varspez;
  429. aktprocdef.localst.insert(vs);
  430. include(vs.varoptions,vo_is_local_copy);
  431. vs.varstate:=vs_assigned;
  432. localvarsym:=vs;
  433. inc(refs); { the para was used to set the local copy ! }
  434. { warnings only on local copy ! }
  435. varstate:=vs_used;
  436. end
  437. else
  438. begin
  439. aktprocdef.parast.rename(name,s);
  440. end;
  441. end;
  442. end;
  443. end;
  444. procedure read_proc;
  445. {
  446. Parses the procedure directives, then parses the procedure body, then
  447. generates the code for it
  448. }
  449. var
  450. oldprocsym : tprocsym;
  451. oldprocdef : tprocdef;
  452. oldprocinfo : pprocinfo;
  453. oldconstsymtable : tsymtable;
  454. oldfilepos : tfileposinfo;
  455. pdflags : word;
  456. begin
  457. { save old state }
  458. oldprocdef:=aktprocdef;
  459. oldprocsym:=aktprocsym;
  460. oldconstsymtable:=constsymtable;
  461. oldprocinfo:=procinfo;
  462. { create a new procedure }
  463. codegen_newprocedure;
  464. with procinfo^ do
  465. begin
  466. parent:=oldprocinfo;
  467. { clear flags }
  468. flags:=0;
  469. { standard frame pointer }
  470. framepointer:=frame_pointer;
  471. { is this a nested function of a method ? }
  472. if assigned(oldprocinfo) then
  473. _class:=oldprocinfo^._class;
  474. end;
  475. parse_proc_dec;
  476. procinfo^.procdef:=aktprocdef;
  477. { set the default function options }
  478. if parse_only then
  479. begin
  480. aktprocdef.forwarddef:=true;
  481. { set also the interface flag, for better error message when the
  482. implementation doesn't much this header }
  483. aktprocdef.interfacedef:=true;
  484. pdflags:=pd_interface;
  485. end
  486. else
  487. begin
  488. pdflags:=pd_body;
  489. if current_module.in_implementation then
  490. pdflags:=pdflags or pd_implemen;
  491. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  492. pdflags:=pdflags or pd_global;
  493. procinfo^.exported:=false;
  494. aktprocdef.forwarddef:=false;
  495. end;
  496. { parse the directives that may follow }
  497. inc(lexlevel);
  498. parse_proc_directives(pdflags);
  499. dec(lexlevel);
  500. { hint directives, these can be separated by semicolons here,
  501. that need to be handled here with a loop (PFV) }
  502. while try_consume_hintdirective(aktprocsym.symoptions) do
  503. Consume(_SEMICOLON);
  504. { set aktfilepos to the beginning of the function declaration }
  505. oldfilepos:=aktfilepos;
  506. aktfilepos:=aktprocdef.fileinfo;
  507. { For varargs directive also cdecl and external must be defined }
  508. if (po_varargs in aktprocdef.procoptions) then
  509. begin
  510. { check first for external in the interface, if available there
  511. then the cdecl must also be there since there is no implementation
  512. available to contain it }
  513. if parse_only then
  514. begin
  515. { if external is available, then cdecl must also be available }
  516. if (po_external in aktprocdef.procoptions) and
  517. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  518. Message(parser_e_varargs_need_cdecl_and_external);
  519. end
  520. else
  521. begin
  522. { both must be defined now }
  523. if not(po_external in aktprocdef.procoptions) or
  524. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  525. Message(parser_e_varargs_need_cdecl_and_external);
  526. end;
  527. end;
  528. { search for forward declarations }
  529. if not proc_add_definition(aktprocsym,aktprocdef) then
  530. begin
  531. { A method must be forward defined (in the object declaration) }
  532. if assigned(procinfo^._class) and
  533. (not assigned(oldprocinfo^._class)) then
  534. begin
  535. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  536. aktprocsym.write_parameter_lists(aktprocdef);
  537. end
  538. else
  539. begin
  540. { Give a better error if there is a forward def in the interface and only
  541. a single implementation }
  542. if (not aktprocdef.forwarddef) and
  543. assigned(aktprocsym.defs^.next) and
  544. aktprocsym.defs^.def.forwarddef and
  545. aktprocsym.defs^.def.interfacedef and
  546. not(assigned(aktprocsym.defs^.next^.next)) then
  547. begin
  548. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  549. aktprocsym.write_parameter_lists(aktprocdef);
  550. end
  551. else
  552. begin
  553. { check the global flag, for delphi this is not
  554. required }
  555. if not(m_delphi in aktmodeswitches) and
  556. ((procinfo^.flags and pi_is_global)<>0) then
  557. Message(parser_e_overloaded_must_be_all_global);
  558. end;
  559. end;
  560. end;
  561. { update procinfo, because the aktprocdef can be
  562. changed by check_identical_proc (PFV) }
  563. procinfo^.procdef:=aktprocdef;
  564. {$ifdef i386}
  565. { add implicit pushes for interrupt routines }
  566. if (po_interrupt in aktprocdef.procoptions) then
  567. begin
  568. { we push Flags and CS as long
  569. to cope with the IRETD
  570. and we save 6 register + 4 selectors }
  571. inc(procinfo^.para_offset,8+6*4+4*2);
  572. end;
  573. {$endif i386}
  574. { pointer to the return value ? }
  575. if ret_in_param(aktprocdef.rettype.def) then
  576. begin
  577. procinfo^.return_offset:=procinfo^.para_offset;
  578. inc(procinfo^.para_offset,pointer_size);
  579. end;
  580. { allows to access the parameters of main functions in nested functions }
  581. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  582. { when it is a value para and it needs a local copy then rename
  583. the parameter and insert a copy in the localst. This is not done
  584. for assembler procedures }
  585. if (not parse_only) and (not aktprocdef.forwarddef) then
  586. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara);
  587. { restore file pos }
  588. aktfilepos:=oldfilepos;
  589. { compile procedure when a body is needed }
  590. if (pdflags and pd_body)<>0 then
  591. begin
  592. Message1(parser_p_procedure_start,
  593. aktprocdef.fullprocname);
  594. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  595. { set _FAIL as keyword if constructor }
  596. if (aktprocdef.proctypeoption=potype_constructor) then
  597. tokeninfo^[_FAIL].keyword:=m_all;
  598. if assigned(aktprocdef._class) then
  599. tokeninfo^[_SELF].keyword:=m_all;
  600. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  601. { reset _FAIL as normal }
  602. if (aktprocdef.proctypeoption=potype_constructor) then
  603. tokeninfo^[_FAIL].keyword:=m_none;
  604. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  605. tokeninfo^[_SELF].keyword:=m_none;
  606. consume(_SEMICOLON);
  607. end;
  608. { close }
  609. codegen_doneprocedure;
  610. { Restore old state }
  611. constsymtable:=oldconstsymtable;
  612. {$ifdef notused}
  613. { restore the interface order to maintain CRC values PM }
  614. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  615. begin
  616. stdef:=aktprocdef;
  617. aktprocdef:=stdef.nextoverloaded;
  618. stdef.nextoverloaded:=prevdef.nextoverloaded;
  619. prevdef.nextoverloaded:=stdef;
  620. end;
  621. {$endif notused}
  622. aktprocsym:=oldprocsym;
  623. aktprocdef:=oldprocdef;
  624. procinfo:=oldprocinfo;
  625. otsym:=nil;
  626. end;
  627. {****************************************************************************
  628. DECLARATION PARSING
  629. ****************************************************************************}
  630. { search in symtablestack for not complete classes }
  631. procedure check_forward_class(p : tnamedindexitem);
  632. begin
  633. if (tsym(p).typ=typesym) and
  634. (ttypesym(p).restype.def.deftype=objectdef) and
  635. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  636. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  637. end;
  638. procedure read_declarations(islibrary : boolean);
  639. procedure Not_supported_for_inline(t : ttoken);
  640. begin
  641. if assigned(aktprocsym) and
  642. (aktprocdef.proccalloption=pocall_inline) then
  643. Begin
  644. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  645. Message(parser_w_inlining_disabled);
  646. aktprocdef.proccalloption:=pocall_fpccall;
  647. End;
  648. end;
  649. begin
  650. repeat
  651. case token of
  652. _LABEL:
  653. begin
  654. Not_supported_for_inline(token);
  655. label_dec;
  656. end;
  657. _CONST:
  658. begin
  659. Not_supported_for_inline(token);
  660. const_dec;
  661. end;
  662. _TYPE:
  663. begin
  664. Not_supported_for_inline(token);
  665. type_dec;
  666. end;
  667. _VAR:
  668. var_dec;
  669. _THREADVAR:
  670. threadvar_dec;
  671. _CONSTRUCTOR,_DESTRUCTOR,
  672. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  673. begin
  674. Not_supported_for_inline(token);
  675. read_proc;
  676. end;
  677. _RESOURCESTRING:
  678. resourcestring_dec;
  679. _EXPORTS:
  680. begin
  681. Not_supported_for_inline(token);
  682. { here we should be at lexlevel 1, no ? PM }
  683. if (lexlevel<>main_program_level) or
  684. (current_module.is_unit) then
  685. begin
  686. Message(parser_e_syntax_error);
  687. consume_all_until(_SEMICOLON);
  688. end
  689. else if islibrary or (target_info.target in [target_i386_WIN32,target_i386_wdosx,target_i386_Netware])
  690. then // AD
  691. read_exports;
  692. end
  693. else break;
  694. end;
  695. until false;
  696. { check for incomplete class definitions, this is only required
  697. for fpc modes }
  698. if (m_fpc in aktmodeswitches) then
  699. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  700. end;
  701. procedure read_interface_declarations;
  702. begin
  703. {Since the body is now parsed at lexlevel 1, and the declarations
  704. must be parsed at the same lexlevel we increase the lexlevel.}
  705. inc(lexlevel);
  706. repeat
  707. case token of
  708. _CONST :
  709. const_dec;
  710. _TYPE :
  711. type_dec;
  712. _VAR :
  713. var_dec;
  714. _THREADVAR :
  715. threadvar_dec;
  716. _RESOURCESTRING:
  717. resourcestring_dec;
  718. _FUNCTION,
  719. _PROCEDURE,
  720. _OPERATOR :
  721. read_proc;
  722. else
  723. break;
  724. end;
  725. until false;
  726. dec(lexlevel);
  727. { check for incomplete class definitions, this is only required
  728. for fpc modes }
  729. if (m_fpc in aktmodeswitches) then
  730. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class);
  731. end;
  732. end.
  733. {
  734. $Log$
  735. Revision 1.48 2002-04-19 15:46:02 peter
  736. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  737. in most cases and not written to the ppu
  738. * add mangeledname_prefix() routine to generate the prefix of
  739. manglednames depending on the current procedure, object and module
  740. * removed static procprefix since the mangledname is now build only
  741. on demand from tprocdef.mangledname
  742. Revision 1.47 2002/04/15 19:01:28 carl
  743. + target_info.size_of_pointer -> pointer_Size
  744. Revision 1.46 2002/04/04 18:45:19 carl
  745. + added wdosx support (patch from Pavel)
  746. Revision 1.45 2002/03/31 20:26:36 jonas
  747. + a_loadfpu_* and a_loadmm_* methods in tcg
  748. * register allocation is now handled by a class and is mostly processor
  749. independent (+rgobj.pas and i386/rgcpu.pas)
  750. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  751. * some small improvements and fixes to the optimizer
  752. * some register allocation fixes
  753. * some fpuvaroffset fixes in the unary minus node
  754. * push/popusedregisters is now called rg.save/restoreusedregisters and
  755. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  756. also better optimizable)
  757. * fixed and optimized register saving/restoring for new/dispose nodes
  758. * LOC_FPU locations now also require their "register" field to be set to
  759. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  760. - list field removed of the tnode class because it's not used currently
  761. and can cause hard-to-find bugs
  762. Revision 1.44 2002/01/19 15:37:24 peter
  763. * commited the wrong file :(
  764. Revision 1.43 2002/01/19 15:20:09 peter
  765. * also check at the end of the implementation for incomplete classes
  766. Revision 1.42 2002/01/19 15:12:34 peter
  767. * check for unresolved forward classes in the interface
  768. Revision 1.41 2001/11/02 22:58:06 peter
  769. * procsym definition rewrite
  770. Revision 1.40 2001/10/25 21:22:37 peter
  771. * calling convention rewrite
  772. Revision 1.39 2001/10/22 21:20:46 peter
  773. * overloaded functions don't need to be global in kylix
  774. Revision 1.38 2001/10/01 13:38:45 jonas
  775. * allow self parameter for normal procedures again (because Kylix allows
  776. it too) ("merged")
  777. Revision 1.37 2001/09/10 10:26:26 jonas
  778. * fixed web bug 1593
  779. * writing of procvar headers is more complete (mention var/const/out for
  780. paras, add "of object" if applicable)
  781. + error if declaring explicit self para as var/const
  782. * fixed mangled name of procedures which contain an explicit self para
  783. * parsing para's should be slightly faster because mangled name of
  784. procedure is only updated once instead of after parsing each para
  785. (all merged from fixes)
  786. Revision 1.36 2001/08/26 13:36:46 florian
  787. * some cg reorganisation
  788. * some PPC updates
  789. Revision 1.35 2001/08/06 21:40:47 peter
  790. * funcret moved from tprocinfo to tprocdef
  791. Revision 1.34 2001/06/04 11:53:13 peter
  792. + varargs directive
  793. Revision 1.33 2001/06/03 21:57:37 peter
  794. + hint directive parsing support
  795. Revision 1.32 2001/04/21 12:03:12 peter
  796. * m68k updates merged from fixes branch
  797. Revision 1.31 2001/04/18 22:01:57 peter
  798. * registration of targets and assemblers
  799. Revision 1.30 2001/04/14 14:05:47 peter
  800. * better skipping of secondpass if error
  801. Revision 1.29 2001/04/13 23:49:24 peter
  802. * when errors are found don't generate code, but still run the
  803. resulttype pass
  804. Revision 1.28 2001/04/13 17:59:03 peter
  805. * don't generate code when there is already an error
  806. Revision 1.27 2001/04/13 01:22:13 peter
  807. * symtable change to classes
  808. * range check generation and errors fixed, make cycle DEBUG=1 works
  809. * memory leaks fixed
  810. Revision 1.26 2001/04/02 21:20:34 peter
  811. * resulttype rewrite
  812. Revision 1.25 2001/02/26 19:44:53 peter
  813. * merged generic m68k updates from fixes branch
  814. Revision 1.24 2000/12/25 00:07:27 peter
  815. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  816. tlinkedlist objects)
  817. Revision 1.23 2000/11/29 00:30:37 florian
  818. * unused units removed from uses clause
  819. * some changes for widestrings
  820. Revision 1.22 2000/11/08 16:38:24 jonas
  821. * if a procedure uses exceptions (be it implicit or explicit), the
  822. usedregisters are set to all (because FPC_POPADDRSTACK doesn't save
  823. any registers) ("merged", fixes make cycle woth -Or)
  824. Revision 1.21 2000/11/01 23:04:38 peter
  825. * tprocdef.fullprocname added for better casesensitve writing of
  826. procedures
  827. Revision 1.20 2000/10/31 22:02:50 peter
  828. * symtable splitted, no real code changes
  829. Revision 1.19 2000/10/24 22:21:25 peter
  830. * set usedregisters after writing entry and exit code (merged)
  831. Revision 1.18 2000/10/21 18:16:12 florian
  832. * a lot of changes:
  833. - basic dyn. array support
  834. - basic C++ support
  835. - some work for interfaces done
  836. ....
  837. Revision 1.17 2000/10/15 07:47:51 peter
  838. * unit names and procedure names are stored mixed case
  839. Revision 1.16 2000/10/14 10:14:52 peter
  840. * moehrendorf oct 2000 rewrite
  841. Revision 1.15 2000/09/24 21:33:47 peter
  842. * message updates merges
  843. Revision 1.14 2000/09/24 21:19:51 peter
  844. * delphi compile fixes
  845. Revision 1.13 2000/09/24 15:06:24 peter
  846. * use defines.inc
  847. Revision 1.12 2000/09/10 20:11:07 peter
  848. * overload checking in implementation removed (merged)
  849. Revision 1.11 2000/09/04 20:15:19 peter
  850. * fixed operator overloading
  851. Revision 1.10 2000/08/27 16:11:52 peter
  852. * moved some util functions from globals,cobjects to cutils
  853. * splitted files into finput,fmodule
  854. Revision 1.9 2000/08/16 18:33:54 peter
  855. * splitted namedobjectitem.next into indexnext and listnext so it
  856. can be used in both lists
  857. * don't allow "word = word" type definitions (merged)
  858. Revision 1.8 2000/08/13 12:54:56 peter
  859. * class member decl wrong then no other error after it
  860. * -vb has now also line numbering
  861. * -vb is also used for interface/implementation different decls and
  862. doesn't list the current function (merged)
  863. Revision 1.7 2000/08/08 19:28:57 peter
  864. * memdebug/memory patches (merged)
  865. * only once illegal directive (merged)
  866. Revision 1.6 2000/08/06 19:39:28 peter
  867. * default parameters working !
  868. Revision 1.5 2000/08/06 14:17:15 peter
  869. * overload fixes (merged)
  870. Revision 1.4 2000/07/30 17:04:43 peter
  871. * merged fixes
  872. Revision 1.3 2000/07/13 12:08:27 michael
  873. + patched to 1.1.0 with former 1.09patch from peter
  874. Revision 1.2 2000/07/13 11:32:46 michael
  875. + removed logs
  876. }