psub.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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,defbase,paramgr,
  37. ppu,fmodule,
  38. { pass 1 }
  39. node,
  40. nbas,
  41. pass_1,
  42. {$ifdef state_tracking}
  43. nstate,
  44. {$endif state_tracking}
  45. { pass 2 }
  46. {$ifndef NOPASS2}
  47. pass_2,
  48. {$endif}
  49. { parser }
  50. scanner,
  51. pbase,pstatmnt,pdecl,pdecsub,pexports,
  52. { codegen }
  53. tgobj,cgbase,rgobj,rgcpu,
  54. ncgutil
  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. { the result will be returned in a register, then setup
  92. the temp. memory for the result
  93. }
  94. if paramanager.ret_in_reg(aktprocdef.rettype.def) then
  95. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  96. { insert result also if support is on }
  97. if (m_result in aktmodeswitches) then
  98. begin
  99. aktprocdef.resultfuncretsym:=tfuncretsym.create('RESULT',aktprocdef.rettype);
  100. symtablestack.insert(aktprocdef.resultfuncretsym);
  101. end;
  102. end;
  103. read_declarations(islibrary);
  104. { temporary space is set, while the BEGIN of the procedure }
  105. if (symtablestack.symtabletype=localsymtable) then
  106. procinfo^.firsttemp_offset := -symtablestack.datasize
  107. else
  108. procinfo^.firsttemp_offset := 0;
  109. { space for the return value }
  110. { !!!!! this means that we can not set the return value
  111. in a subfunction !!!!! }
  112. { because we don't know yet where the address is }
  113. if not is_void(aktprocdef.rettype.def) then
  114. begin
  115. if paramanager.ret_in_reg(aktprocdef.rettype.def) then
  116. begin
  117. { the space has been set in the local symtable }
  118. procinfo^.return_offset:=-tfuncretsym(aktprocdef.funcretsym).address;
  119. if ((procinfo^.flags and pi_operator)<>0) and
  120. assigned(otsym) then
  121. otsym.address:=-procinfo^.return_offset;
  122. { is the return result in registers? The
  123. set them as used in the routine
  124. }
  125. rg.usedinproc := rg.usedinproc +
  126. getfuncusedregisters(aktprocdef.rettype.def);
  127. end;
  128. end;
  129. {Unit initialization?.}
  130. if (lexlevel=unit_init_level) and (current_module.is_unit)
  131. or islibrary then
  132. begin
  133. if (token=_END) then
  134. begin
  135. consume(_END);
  136. { We need at least a node, else the entry/exit code is not
  137. generated and thus no PASCALMAIN symbol which we need (PFV) }
  138. if islibrary then
  139. block:=cnothingnode.create
  140. else
  141. block:=nil;
  142. end
  143. else
  144. begin
  145. if token=_INITIALIZATION then
  146. begin
  147. current_module.flags:=current_module.flags or uf_init;
  148. block:=statement_block(_INITIALIZATION);
  149. end
  150. else if (token=_FINALIZATION) then
  151. begin
  152. if (current_module.flags and uf_finalize)<>0 then
  153. block:=statement_block(_FINALIZATION)
  154. else
  155. begin
  156. { can we allow no INITIALIZATION for DLL ??
  157. I think it should work PM }
  158. block:=nil;
  159. exit;
  160. end;
  161. end
  162. else
  163. begin
  164. current_module.flags:=current_module.flags or uf_init;
  165. block:=statement_block(_BEGIN);
  166. end;
  167. end;
  168. end
  169. else
  170. block:=statement_block(_BEGIN);
  171. end;
  172. {****************************************************************************
  173. PROCEDURE/FUNCTION COMPILING
  174. ****************************************************************************}
  175. procedure compile_proc_body(make_global,parent_has_class:boolean);
  176. {
  177. Compile the body of a procedure
  178. }
  179. var
  180. oldexitlabel,oldexit2label : tasmlabel;
  181. oldfaillabel,oldquickexitlabel:tasmlabel;
  182. _class,hp:tobjectdef;
  183. { switches can change inside the procedure }
  184. entryswitches, exitswitches : tlocalswitches;
  185. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  186. { code for the subroutine as tree }
  187. code:tnode;
  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. objectlibrary.getlabel(aktexitlabel);
  213. objectlibrary.getlabel(aktexit2label);
  214. { exit for fail in constructors }
  215. if (aktprocdef.proctypeoption=potype_constructor) then
  216. begin
  217. objectlibrary.getlabel(faillabel);
  218. objectlibrary.getlabel(quickexitlabel);
  219. end;
  220. { reset break and continue labels }
  221. block_type:=bt_general;
  222. aktbreaklabel:=nil;
  223. aktcontinuelabel:=nil;
  224. {$ifdef state_tracking}
  225. { aktstate:=Tstate_storage.create;}
  226. {$endif state_tracking}
  227. { insert symtables for the class, by only if it is no nested function }
  228. if assigned(procinfo^._class) and not(parent_has_class) then
  229. begin
  230. { insert them in the reverse order ! }
  231. hp:=nil;
  232. repeat
  233. _class:=procinfo^._class;
  234. while _class.childof<>hp do
  235. _class:=_class.childof;
  236. hp:=_class;
  237. _class.symtable.next:=symtablestack;
  238. symtablestack:=_class.symtable;
  239. until hp=procinfo^._class;
  240. end;
  241. { insert parasymtable in symtablestack}
  242. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  243. for checking of same names used in interface and implementation !! }
  244. if lexlevel>=normal_function_level then
  245. begin
  246. aktprocdef.parast.next:=symtablestack;
  247. symtablestack:=aktprocdef.parast;
  248. symtablestack.symtablelevel:=lexlevel;
  249. end;
  250. { insert localsymtable in symtablestack}
  251. aktprocdef.localst.next:=symtablestack;
  252. symtablestack:=aktprocdef.localst;
  253. symtablestack.symtablelevel:=lexlevel;
  254. { constant symbols are inserted in this symboltable }
  255. constsymtable:=symtablestack;
  256. { reset the temporary memory }
  257. rg.cleartempgen;
  258. rg.usedinproc:=[];
  259. rg.usedbyproc:=[];
  260. { save entry info }
  261. entrypos:=aktfilepos;
  262. entryswitches:=aktlocalswitches;
  263. localmaxfpuregisters:=aktmaxfpuregisters;
  264. { parse the code ... }
  265. code:=block(current_module.islibrary);
  266. { get a better entry point }
  267. if assigned(code) then
  268. entrypos:=code.fileinfo;
  269. { save exit info }
  270. exitswitches:=aktlocalswitches;
  271. exitpos:=last_endtoken_filepos;
  272. { save current filepos }
  273. savepos:=aktfilepos;
  274. {When we are called to compile the body of a unit, aktprocsym should
  275. point to the unit initialization. If the unit has no initialization,
  276. aktprocsym=nil. But in that case code=nil. hus we should check for
  277. code=nil, when we use aktprocsym.}
  278. { set the start offset to the start of the temp area in the stack }
  279. tg.setfirsttemp(procinfo^.firsttemp_offset);
  280. { ... and generate assembler }
  281. { but set the right switches for entry !! }
  282. aktlocalswitches:=entryswitches;
  283. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  284. aktmaxfpuregisters:=localmaxfpuregisters;
  285. if assigned(code) then
  286. begin
  287. { the procedure is now defined }
  288. aktprocdef.forwarddef:=false;
  289. {$ifdef state_tracking}
  290. { writenode(code);
  291. do_track_state_pass(code);
  292. writenode(code);}
  293. {$endif}
  294. { only generate the code if no type errors are found, else
  295. finish at least the type checking pass }
  296. {$ifndef NOPASS2}
  297. if (status.errorcount=0) then
  298. begin
  299. generatecode(code);
  300. aktprocdef.code:=code;
  301. { first generate entry code with the correct position and switches }
  302. aktfilepos:=entrypos;
  303. aktlocalswitches:=entryswitches;
  304. genentrycode(procinfo^.aktentrycode,make_global,0,parasize,nostackframe,false);
  305. { FPC_POPADDRSTACK destroys all registers (JM) }
  306. if (procinfo^.flags and (pi_needs_implicit_finally or pi_uses_exceptions)) <> 0 then
  307. begin
  308. rg.usedinproc := ALL_REGISTERS;
  309. end;
  310. { now generate exit code with the correct position and switches }
  311. aktfilepos:=exitpos;
  312. aktlocalswitches:=exitswitches;
  313. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  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. {$ifdef state_tracking}
  399. { aktstate.destroy;}
  400. {$endif state_tracking}
  401. { restore filepos, the switches are already set }
  402. aktfilepos:=savepos;
  403. { restore labels }
  404. aktexitlabel:=oldexitlabel;
  405. aktexit2label:=oldexit2label;
  406. quickexitlabel:=oldquickexitlabel;
  407. faillabel:=oldfaillabel;
  408. { reset to normal non static function }
  409. if (lexlevel=normal_function_level) then
  410. allow_only_static:=false;
  411. { previous lexlevel }
  412. dec(lexlevel);
  413. end;
  414. {****************************************************************************
  415. PROCEDURE/FUNCTION PARSING
  416. ****************************************************************************}
  417. procedure checkvaluepara(p:tnamedindexitem;arg:pointer);
  418. var
  419. vs : tvarsym;
  420. s : string;
  421. begin
  422. if tsym(p).typ<>varsym then
  423. exit;
  424. with tvarsym(p) do
  425. begin
  426. if copy(name,1,3)='val' then
  427. begin
  428. s:=Copy(name,4,255);
  429. if not(po_assembler in aktprocdef.procoptions) then
  430. begin
  431. vs:=tvarsym.create(s,vartype);
  432. vs.fileinfo:=fileinfo;
  433. vs.varspez:=varspez;
  434. aktprocdef.localst.insert(vs);
  435. include(vs.varoptions,vo_is_local_copy);
  436. vs.varstate:=vs_assigned;
  437. localvarsym:=vs;
  438. inc(refs); { the para was used to set the local copy ! }
  439. { warnings only on local copy ! }
  440. varstate:=vs_used;
  441. end
  442. else
  443. begin
  444. aktprocdef.parast.rename(name,s);
  445. end;
  446. end;
  447. end;
  448. end;
  449. procedure read_proc;
  450. {
  451. Parses the procedure directives, then parses the procedure body, then
  452. generates the code for it
  453. }
  454. var
  455. oldprocsym : tprocsym;
  456. oldprocdef : tprocdef;
  457. oldprocinfo : pprocinfo;
  458. oldconstsymtable : tsymtable;
  459. oldfilepos : tfileposinfo;
  460. pdflags : word;
  461. begin
  462. { save old state }
  463. oldprocdef:=aktprocdef;
  464. oldprocsym:=aktprocsym;
  465. oldconstsymtable:=constsymtable;
  466. oldprocinfo:=procinfo;
  467. { create a new procedure }
  468. codegen_newprocedure;
  469. with procinfo^ do
  470. begin
  471. parent:=oldprocinfo;
  472. { clear flags }
  473. flags:=0;
  474. { standard frame pointer }
  475. framepointer:=frame_pointer_reg;
  476. { is this a nested function of a method ? }
  477. if assigned(oldprocinfo) then
  478. _class:=oldprocinfo^._class;
  479. end;
  480. parse_proc_dec;
  481. procinfo^.procdef:=aktprocdef;
  482. { set the default function options }
  483. if parse_only then
  484. begin
  485. aktprocdef.forwarddef:=true;
  486. { set also the interface flag, for better error message when the
  487. implementation doesn't much this header }
  488. aktprocdef.interfacedef:=true;
  489. pdflags:=pd_interface;
  490. end
  491. else
  492. begin
  493. pdflags:=pd_body;
  494. if current_module.in_implementation then
  495. pdflags:=pdflags or pd_implemen;
  496. if (not current_module.is_unit) or (cs_create_smart in aktmoduleswitches) then
  497. pdflags:=pdflags or pd_global;
  498. procinfo^.exported:=false;
  499. aktprocdef.forwarddef:=false;
  500. end;
  501. { parse the directives that may follow }
  502. inc(lexlevel);
  503. parse_proc_directives(pdflags);
  504. dec(lexlevel);
  505. { hint directives, these can be separated by semicolons here,
  506. that need to be handled here with a loop (PFV) }
  507. while try_consume_hintdirective(aktprocsym.symoptions) do
  508. Consume(_SEMICOLON);
  509. { set aktfilepos to the beginning of the function declaration }
  510. oldfilepos:=aktfilepos;
  511. aktfilepos:=aktprocdef.fileinfo;
  512. { For varargs directive also cdecl and external must be defined }
  513. if (po_varargs in aktprocdef.procoptions) then
  514. begin
  515. { check first for external in the interface, if available there
  516. then the cdecl must also be there since there is no implementation
  517. available to contain it }
  518. if parse_only then
  519. begin
  520. { if external is available, then cdecl must also be available }
  521. if (po_external in aktprocdef.procoptions) and
  522. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  523. Message(parser_e_varargs_need_cdecl_and_external);
  524. end
  525. else
  526. begin
  527. { both must be defined now }
  528. if not(po_external in aktprocdef.procoptions) or
  529. not(aktprocdef.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  530. Message(parser_e_varargs_need_cdecl_and_external);
  531. end;
  532. end;
  533. { search for forward declarations }
  534. if not proc_add_definition(aktprocsym,aktprocdef) then
  535. begin
  536. { A method must be forward defined (in the object declaration) }
  537. if assigned(procinfo^._class) and
  538. (not assigned(oldprocinfo^._class)) then
  539. begin
  540. Message1(parser_e_header_dont_match_any_member,aktprocdef.fullprocname);
  541. aktprocsym.write_parameter_lists(aktprocdef);
  542. end
  543. else
  544. begin
  545. { Give a better error if there is a forward def in the interface and only
  546. a single implementation }
  547. if (not aktprocdef.forwarddef) and
  548. assigned(aktprocsym.defs^.next) and
  549. aktprocsym.defs^.def.forwarddef and
  550. aktprocsym.defs^.def.interfacedef and
  551. not(assigned(aktprocsym.defs^.next^.next)) then
  552. begin
  553. Message1(parser_e_header_dont_match_forward,aktprocdef.fullprocname);
  554. aktprocsym.write_parameter_lists(aktprocdef);
  555. end
  556. else
  557. begin
  558. { check the global flag, for delphi this is not
  559. required }
  560. if not(m_delphi in aktmodeswitches) and
  561. ((procinfo^.flags and pi_is_global)<>0) then
  562. Message(parser_e_overloaded_must_be_all_global);
  563. end;
  564. end;
  565. end;
  566. { update procinfo, because the aktprocdef can be
  567. changed by check_identical_proc (PFV) }
  568. procinfo^.procdef:=aktprocdef;
  569. {$ifdef i386}
  570. { add implicit pushes for interrupt routines }
  571. if (po_interrupt in aktprocdef.procoptions) then
  572. begin
  573. { we push Flags and CS as long
  574. to cope with the IRETD
  575. and we save 6 register + 4 selectors }
  576. inc(procinfo^.para_offset,8+6*4+4*2);
  577. end;
  578. {$endif i386}
  579. { pointer to the return value ? }
  580. if paramanager.ret_in_param(aktprocdef.rettype.def) then
  581. begin
  582. procinfo^.return_offset:=procinfo^.para_offset;
  583. inc(procinfo^.para_offset,pointer_size);
  584. end;
  585. { allows to access the parameters of main functions in nested functions }
  586. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  587. { when it is a value para and it needs a local copy then rename
  588. the parameter and insert a copy in the localst. This is not done
  589. for assembler procedures }
  590. if (not parse_only) and (not aktprocdef.forwarddef) then
  591. aktprocdef.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}checkvaluepara,nil);
  592. { restore file pos }
  593. aktfilepos:=oldfilepos;
  594. { compile procedure when a body is needed }
  595. if (pdflags and pd_body)<>0 then
  596. begin
  597. Message1(parser_p_procedure_start,
  598. aktprocdef.fullprocname);
  599. aktprocdef.aliasnames.insert(aktprocdef.mangledname);
  600. { set _FAIL as keyword if constructor }
  601. if (aktprocdef.proctypeoption=potype_constructor) then
  602. tokeninfo^[_FAIL].keyword:=m_all;
  603. if assigned(aktprocdef._class) then
  604. tokeninfo^[_SELF].keyword:=m_all;
  605. compile_proc_body(((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  606. { reset _FAIL as normal }
  607. if (aktprocdef.proctypeoption=potype_constructor) then
  608. tokeninfo^[_FAIL].keyword:=m_none;
  609. if assigned(aktprocdef._class) and (lexlevel=main_program_level) then
  610. tokeninfo^[_SELF].keyword:=m_none;
  611. consume(_SEMICOLON);
  612. end;
  613. { close }
  614. codegen_doneprocedure;
  615. { Restore old state }
  616. constsymtable:=oldconstsymtable;
  617. {$ifdef notused}
  618. { restore the interface order to maintain CRC values PM }
  619. if assigned(prevdef) and assigned(aktprocdef.nextoverloaded) then
  620. begin
  621. stdef:=aktprocdef;
  622. aktprocdef:=stdef.nextoverloaded;
  623. stdef.nextoverloaded:=prevdef.nextoverloaded;
  624. prevdef.nextoverloaded:=stdef;
  625. end;
  626. {$endif notused}
  627. aktprocsym:=oldprocsym;
  628. aktprocdef:=oldprocdef;
  629. procinfo:=oldprocinfo;
  630. otsym:=nil;
  631. end;
  632. {****************************************************************************
  633. DECLARATION PARSING
  634. ****************************************************************************}
  635. { search in symtablestack for not complete classes }
  636. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  637. begin
  638. if (tsym(p).typ=typesym) and
  639. (ttypesym(p).restype.def.deftype=objectdef) and
  640. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  641. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  642. end;
  643. procedure read_declarations(islibrary : boolean);
  644. procedure Not_supported_for_inline(t : ttoken);
  645. begin
  646. if assigned(aktprocsym) and
  647. (aktprocdef.proccalloption=pocall_inline) then
  648. Begin
  649. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  650. Message(parser_w_inlining_disabled);
  651. aktprocdef.proccalloption:=pocall_fpccall;
  652. End;
  653. end;
  654. begin
  655. repeat
  656. case token of
  657. _LABEL:
  658. begin
  659. Not_supported_for_inline(token);
  660. label_dec;
  661. end;
  662. _CONST:
  663. begin
  664. Not_supported_for_inline(token);
  665. const_dec;
  666. end;
  667. _TYPE:
  668. begin
  669. Not_supported_for_inline(token);
  670. type_dec;
  671. end;
  672. _VAR:
  673. var_dec;
  674. _THREADVAR:
  675. threadvar_dec;
  676. _CONSTRUCTOR,_DESTRUCTOR,
  677. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  678. begin
  679. Not_supported_for_inline(token);
  680. read_proc;
  681. end;
  682. _RESOURCESTRING:
  683. resourcestring_dec;
  684. _EXPORTS:
  685. begin
  686. Not_supported_for_inline(token);
  687. { here we should be at lexlevel 1, no ? PM }
  688. if (lexlevel<>main_program_level) or
  689. (current_module.is_unit) then
  690. begin
  691. Message(parser_e_syntax_error);
  692. consume_all_until(_SEMICOLON);
  693. end
  694. else if islibrary or (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware])
  695. then // AD
  696. read_exports;
  697. end
  698. else break;
  699. end;
  700. until false;
  701. { check for incomplete class definitions, this is only required
  702. for fpc modes }
  703. if (m_fpc in aktmodeswitches) then
  704. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  705. end;
  706. procedure read_interface_declarations;
  707. begin
  708. {Since the body is now parsed at lexlevel 1, and the declarations
  709. must be parsed at the same lexlevel we increase the lexlevel.}
  710. inc(lexlevel);
  711. repeat
  712. case token of
  713. _CONST :
  714. const_dec;
  715. _TYPE :
  716. type_dec;
  717. _VAR :
  718. var_dec;
  719. _THREADVAR :
  720. threadvar_dec;
  721. _RESOURCESTRING:
  722. resourcestring_dec;
  723. _FUNCTION,
  724. _PROCEDURE,
  725. _OPERATOR :
  726. read_proc;
  727. else
  728. break;
  729. end;
  730. until false;
  731. dec(lexlevel);
  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. end.
  738. {
  739. $Log$
  740. Revision 1.67 2002-08-16 14:24:59 carl
  741. * issameref() to test if two references are the same (then emit no opcodes)
  742. + ret_in_reg to replace ret_in_acc
  743. (fix some register allocation bugs at the same time)
  744. + save_std_register now has an extra parameter which is the
  745. usedinproc registers
  746. Revision 1.66 2002/08/11 14:32:27 peter
  747. * renamed current_library to objectlibrary
  748. Revision 1.65 2002/08/11 13:24:13 peter
  749. * saving of asmsymbols in ppu supported
  750. * asmsymbollist global is removed and moved into a new class
  751. tasmlibrarydata that will hold the info of a .a file which
  752. corresponds with a single module. Added librarydata to tmodule
  753. to keep the library info stored for the module. In the future the
  754. objectfiles will also be stored to the tasmlibrarydata class
  755. * all getlabel/newasmsymbol and friends are moved to the new class
  756. Revision 1.64 2002/08/09 19:14:28 carl
  757. * fixed stackframe parameter (should only contain local size),
  758. set to zero currently
  759. Revision 1.63 2002/08/06 20:55:22 florian
  760. * first part of ppc calling conventions fix
  761. Revision 1.62 2002/07/26 21:15:41 florian
  762. * rewrote the system handling
  763. Revision 1.61 2002/07/20 11:57:56 florian
  764. * types.pas renamed to defbase.pas because D6 contains a types
  765. unit so this would conflicts if D6 programms are compiled
  766. + Willamette/SSE2 instructions to assembler added
  767. Revision 1.60 2002/07/19 11:41:36 daniel
  768. * State tracker work
  769. * The whilen and repeatn are now completely unified into whilerepeatn. This
  770. allows the state tracker to change while nodes automatically into
  771. repeat nodes.
  772. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  773. 'not(a>b)' is optimized into 'a<=b'.
  774. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  775. by removing the notn and later switchting the true and falselabels. The
  776. same is done with 'repeat until not a'.
  777. Revision 1.59 2002/07/15 18:03:15 florian
  778. * readded removed changes
  779. Revision 1.57 2002/07/11 14:41:28 florian
  780. * start of the new generic parameter handling
  781. Revision 1.58 2002/07/14 18:00:44 daniel
  782. + Added the beginning of a state tracker. This will track the values of
  783. variables through procedures and optimize things away.
  784. Revision 1.56 2002/07/07 09:52:32 florian
  785. * powerpc target fixed, very simple units can be compiled
  786. * some basic stuff for better callparanode handling, far from being finished
  787. Revision 1.55 2002/07/04 20:43:01 florian
  788. * first x86-64 patches
  789. Revision 1.54 2002/07/01 18:46:25 peter
  790. * internal linker
  791. * reorganized aasm layer
  792. Revision 1.53 2002/05/18 13:34:14 peter
  793. * readded missing revisions
  794. Revision 1.52 2002/05/16 19:46:44 carl
  795. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  796. + try to fix temp allocation (still in ifdef)
  797. + generic constructor calls
  798. + start of tassembler / tmodulebase class cleanup
  799. Revision 1.51 2002/05/14 19:34:49 peter
  800. * removed old logs and updated copyright year
  801. Revision 1.50 2002/05/12 16:53:09 peter
  802. * moved entry and exitcode to ncgutil and cgobj
  803. * foreach gets extra argument for passing local data to the
  804. iterator function
  805. * -CR checks also class typecasts at runtime by changing them
  806. into as
  807. * fixed compiler to cycle with the -CR option
  808. * fixed stabs with elf writer, finally the global variables can
  809. be watched
  810. * removed a lot of routines from cga unit and replaced them by
  811. calls to cgobj
  812. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  813. u32bit then the other is typecasted also to u32bit without giving
  814. a rangecheck warning/error.
  815. * fixed pascal calling method with reversing also the high tree in
  816. the parast, detected by tcalcst3 test
  817. Revision 1.49 2002/04/20 21:32:24 carl
  818. + generic FPC_CHECKPOINTER
  819. + first parameter offset in stack now portable
  820. * rename some constants
  821. + move some cpu stuff to other units
  822. - remove unused constents
  823. * fix stacksize for some targets
  824. * fix generic size problems which depend now on EXTEND_SIZE constant
  825. Revision 1.48 2002/04/19 15:46:02 peter
  826. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  827. in most cases and not written to the ppu
  828. * add mangeledname_prefix() routine to generate the prefix of
  829. manglednames depending on the current procedure, object and module
  830. * removed static procprefix since the mangledname is now build only
  831. on demand from tprocdef.mangledname
  832. Revision 1.47 2002/04/15 19:01:28 carl
  833. + target_info.size_of_pointer -> pointer_Size
  834. Revision 1.46 2002/04/04 18:45:19 carl
  835. + added wdosx support (patch from Pavel)
  836. Revision 1.45 2002/03/31 20:26:36 jonas
  837. + a_loadfpu_* and a_loadmm_* methods in tcg
  838. * register allocation is now handled by a class and is mostly processor
  839. independent (+rgobj.pas and i386/rgcpu.pas)
  840. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  841. * some small improvements and fixes to the optimizer
  842. * some register allocation fixes
  843. * some fpuvaroffset fixes in the unary minus node
  844. * push/popusedregisters is now called rg.save/restoreusedregisters and
  845. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  846. also better optimizable)
  847. * fixed and optimized register saving/restoring for new/dispose nodes
  848. * LOC_FPU locations now also require their "register" field to be set to
  849. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  850. - list field removed of the tnode class because it's not used currently
  851. and can cause hard-to-find bugs
  852. Revision 1.44 2002/01/19 15:37:24 peter
  853. * commited the wrong file :(
  854. Revision 1.43 2002/01/19 15:20:09 peter
  855. * also check at the end of the implementation for incomplete classes
  856. Revision 1.42 2002/01/19 15:12:34 peter
  857. * check for unresolved forward classes in the interface
  858. }