psub.pas 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing and codegeneration at subroutine level
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit psub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,globals,
  22. node,nbas,
  23. symdef,procinfo,optdfa;
  24. type
  25. tcgprocinfo = class(tprocinfo)
  26. private
  27. procedure add_entry_exit_code;
  28. public
  29. { code for the subroutine as tree }
  30. code : tnode;
  31. { positions in the tree for init/final }
  32. entry_asmnode,
  33. loadpara_asmnode,
  34. exitlabel_asmnode,
  35. stackcheck_asmnode,
  36. init_asmnode,
  37. final_asmnode : tasmnode;
  38. { list to store the procinfo's of the nested procedures }
  39. nestedprocs : tlinkedlist;
  40. dfabuilder : TDFABuilder;
  41. constructor create(aparent:tprocinfo);override;
  42. destructor destroy;override;
  43. procedure printproc;
  44. procedure generate_code;
  45. procedure resetprocdef;
  46. procedure add_to_symtablestack;
  47. procedure remove_from_symtablestack;
  48. procedure parse_body;
  49. function stack_tainting_parameter : boolean;
  50. function has_assembler_child : boolean;
  51. end;
  52. procedure printnode_reset;
  53. { reads the declaration blocks }
  54. procedure read_declarations(islibrary : boolean);
  55. { reads declarations in the interface part of a unit }
  56. procedure read_interface_declarations;
  57. procedure generate_specialization_procs;
  58. implementation
  59. uses
  60. sysutils,
  61. { common }
  62. cutils,
  63. { global }
  64. globtype,tokens,verbose,comphook,constexp,
  65. systems,
  66. { aasm }
  67. cpubase,aasmbase,aasmtai,aasmdata,
  68. { symtable }
  69. symconst,symbase,symsym,symtype,symtable,defutil,
  70. paramgr,
  71. ppu,fmodule,
  72. { pass 1 }
  73. nutils,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  74. pass_1,
  75. {$ifdef state_tracking}
  76. nstate,
  77. {$endif state_tracking}
  78. { pass 2 }
  79. {$ifndef NOPASS2}
  80. pass_2,
  81. {$endif}
  82. { parser }
  83. scanner,import,gendef,
  84. pbase,pstatmnt,pdecl,pdecsub,pexports,
  85. { codegen }
  86. tgobj,cgbase,cgobj,dbgbase,
  87. ncgutil,regvars,
  88. optbase,
  89. opttail,
  90. optcse,optloop,
  91. optutils
  92. {$if defined(arm) or defined(powerpc) or defined(powerpc64)}
  93. ,aasmcpu
  94. {$endif arm}
  95. {$ifndef NOOPT}
  96. {$ifdef i386}
  97. ,aopt386
  98. {$else i386}
  99. ,aopt
  100. {$endif i386}
  101. {$endif}
  102. ;
  103. {****************************************************************************
  104. PROCEDURE/FUNCTION BODY PARSING
  105. ****************************************************************************}
  106. procedure initializevars(p:TObject;arg:pointer);
  107. var
  108. b : tblocknode;
  109. begin
  110. if not (tsym(p).typ in [localvarsym,staticvarsym]) then
  111. exit;
  112. with tabstractnormalvarsym(p) do
  113. begin
  114. if assigned(defaultconstsym) then
  115. begin
  116. b:=tblocknode(arg);
  117. b.left:=cstatementnode.create(
  118. cassignmentnode.create(
  119. cloadnode.create(tsym(p),tsym(p).owner),
  120. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  121. b.left);
  122. end;
  123. end;
  124. end;
  125. procedure check_finalize_paras(p:TObject;arg:pointer);
  126. begin
  127. if (tsym(p).typ=paravarsym) and
  128. (tparavarsym(p).varspez=vs_value) and
  129. not is_class(tparavarsym(p).vardef) and
  130. tparavarsym(p).vardef.needs_inittable then
  131. include(current_procinfo.flags,pi_needs_implicit_finally);
  132. end;
  133. procedure check_finalize_locals(p:TObject;arg:pointer);
  134. begin
  135. { include the result: it needs to be finalized in case an exception }
  136. { occurs }
  137. if (tsym(p).typ=localvarsym) and
  138. (tlocalvarsym(p).refs>0) and
  139. not(is_class(tlocalvarsym(p).vardef)) and
  140. tlocalvarsym(p).vardef.needs_inittable then
  141. include(current_procinfo.flags,pi_needs_implicit_finally);
  142. end;
  143. function block(islibrary : boolean) : tnode;
  144. begin
  145. { parse const,types and vars }
  146. read_declarations(islibrary);
  147. { do we have an assembler block without the po_assembler?
  148. we should allow this for Delphi compatibility (PFV) }
  149. if (token=_ASM) and (m_delphi in current_settings.modeswitches) then
  150. include(current_procinfo.procdef.procoptions,po_assembler);
  151. { Handle assembler block different }
  152. if (po_assembler in current_procinfo.procdef.procoptions) then
  153. begin
  154. block:=assembler_block;
  155. exit;
  156. end;
  157. {Unit initialization?.}
  158. if (
  159. assigned(current_procinfo.procdef.localst) and
  160. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  161. (current_module.is_unit)
  162. ) or
  163. islibrary then
  164. begin
  165. if (token=_END) then
  166. begin
  167. consume(_END);
  168. { We need at least a node, else the entry/exit code is not
  169. generated and thus no PASCALMAIN symbol which we need (PFV) }
  170. if islibrary then
  171. block:=cnothingnode.create
  172. else
  173. block:=nil;
  174. end
  175. else
  176. begin
  177. if token=_INITIALIZATION then
  178. begin
  179. { The library init code is already called and does not
  180. need to be in the initfinal table (PFV) }
  181. if not islibrary then
  182. current_module.flags:=current_module.flags or uf_init;
  183. block:=statement_block(_INITIALIZATION);
  184. end
  185. else if (token=_FINALIZATION) then
  186. begin
  187. if (current_module.flags and uf_finalize)<>0 then
  188. block:=statement_block(_FINALIZATION)
  189. else
  190. begin
  191. { can we allow no INITIALIZATION for DLL ??
  192. I think it should work PM }
  193. block:=nil;
  194. exit;
  195. end;
  196. end
  197. else
  198. begin
  199. { The library init code is already called and does not
  200. need to be in the initfinal table (PFV) }
  201. if not islibrary then
  202. current_module.flags:=current_module.flags or uf_init;
  203. block:=statement_block(_BEGIN);
  204. end;
  205. end;
  206. end
  207. else
  208. begin
  209. block:=statement_block(_BEGIN);
  210. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  211. current_procinfo.procdef.localst.SymList.ForEachCall(@initializevars,block);
  212. end;
  213. end;
  214. {****************************************************************************
  215. PROCEDURE/FUNCTION COMPILING
  216. ****************************************************************************}
  217. procedure printnode_reset;
  218. begin
  219. assign(printnodefile,treelogfilename);
  220. {$I-}
  221. rewrite(printnodefile);
  222. {$I+}
  223. if ioresult<>0 then
  224. begin
  225. Comment(V_Error,'Error creating '+treelogfilename);
  226. exit;
  227. end;
  228. close(printnodefile);
  229. end;
  230. function generate_bodyentry_block:tnode;
  231. var
  232. srsym : tsym;
  233. para : tcallparanode;
  234. newstatement : tstatementnode;
  235. begin
  236. result:=internalstatements(newstatement);
  237. if assigned(current_objectdef) then
  238. begin
  239. { a constructor needs a help procedure }
  240. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  241. begin
  242. if is_class(current_objectdef) then
  243. begin
  244. include(current_procinfo.flags,pi_needs_implicit_finally);
  245. srsym:=search_class_member(current_objectdef,'NEWINSTANCE');
  246. if assigned(srsym) and
  247. (srsym.typ=procsym) then
  248. begin
  249. { if vmt>1 then newinstance }
  250. addstatement(newstatement,cifnode.create(
  251. caddnode.create(gtn,
  252. ctypeconvnode.create_internal(
  253. load_vmt_pointer_node,
  254. voidpointertype),
  255. cpointerconstnode.create(1,voidpointertype)),
  256. cassignmentnode.create(
  257. ctypeconvnode.create_internal(
  258. load_self_pointer_node,
  259. voidpointertype),
  260. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node,[])),
  261. nil));
  262. end
  263. else
  264. internalerror(200305108);
  265. end
  266. else
  267. if is_object(current_objectdef) then
  268. begin
  269. { parameter 3 : vmt_offset }
  270. { parameter 2 : address of pointer to vmt,
  271. this is required to allow setting the vmt to -1 to indicate
  272. that memory was allocated }
  273. { parameter 1 : self pointer }
  274. para:=ccallparanode.create(
  275. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  276. ccallparanode.create(
  277. ctypeconvnode.create_internal(
  278. load_vmt_pointer_node,
  279. voidpointertype),
  280. ccallparanode.create(
  281. ctypeconvnode.create_internal(
  282. load_self_pointer_node,
  283. voidpointertype),
  284. nil)));
  285. addstatement(newstatement,cassignmentnode.create(
  286. ctypeconvnode.create_internal(
  287. load_self_pointer_node,
  288. voidpointertype),
  289. ccallnode.createintern('fpc_help_constructor',para)));
  290. end
  291. else
  292. internalerror(200305103);
  293. { if self=nil then exit
  294. calling fail instead of exit is useless because
  295. there is nothing to dispose (PFV) }
  296. addstatement(newstatement,cifnode.create(
  297. caddnode.create(equaln,
  298. load_self_pointer_node,
  299. cnilnode.create),
  300. cexitnode.create(nil),
  301. nil));
  302. end;
  303. { maybe call BeforeDestruction for classes }
  304. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  305. is_class(current_objectdef) then
  306. begin
  307. srsym:=search_class_member(current_objectdef,'BEFOREDESTRUCTION');
  308. if assigned(srsym) and
  309. (srsym.typ=procsym) then
  310. begin
  311. { if vmt>0 then beforedestruction }
  312. addstatement(newstatement,cifnode.create(
  313. caddnode.create(gtn,
  314. ctypeconvnode.create_internal(
  315. load_vmt_pointer_node,ptrsinttype),
  316. ctypeconvnode.create_internal(
  317. cnilnode.create,ptrsinttype)),
  318. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  319. nil));
  320. end
  321. else
  322. internalerror(200305104);
  323. end;
  324. end;
  325. end;
  326. function generate_bodyexit_block:tnode;
  327. var
  328. srsym : tsym;
  329. para : tcallparanode;
  330. newstatement : tstatementnode;
  331. oldlocalswitches: tlocalswitches;
  332. begin
  333. result:=internalstatements(newstatement);
  334. if assigned(current_objectdef) then
  335. begin
  336. { Don't test self and the vmt here. The reason is that }
  337. { a constructor already checks whether these are valid }
  338. { before. Further, in case of TThread the thread may }
  339. { free the class instance right after AfterConstruction }
  340. { has been called, so it may no longer be valid (JM) }
  341. oldlocalswitches:=current_settings.localswitches;
  342. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  343. { maybe call AfterConstruction for classes }
  344. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  345. is_class(current_objectdef) then
  346. begin
  347. srsym:=search_class_member(current_objectdef,'AFTERCONSTRUCTION');
  348. if assigned(srsym) and
  349. (srsym.typ=procsym) then
  350. begin
  351. { Self can be nil when fail is called }
  352. { if self<>nil and vmt<>nil then afterconstruction }
  353. addstatement(newstatement,cifnode.create(
  354. caddnode.create(andn,
  355. caddnode.create(unequaln,
  356. load_self_pointer_node,
  357. cnilnode.create),
  358. caddnode.create(unequaln,
  359. load_vmt_pointer_node,
  360. cnilnode.create)),
  361. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  362. nil));
  363. end
  364. else
  365. internalerror(200305106);
  366. end;
  367. { a destructor needs a help procedure }
  368. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  369. begin
  370. if is_class(current_objectdef) then
  371. begin
  372. srsym:=search_class_member(current_objectdef,'FREEINSTANCE');
  373. if assigned(srsym) and
  374. (srsym.typ=procsym) then
  375. begin
  376. { if self<>0 and vmt<>0 then freeinstance }
  377. addstatement(newstatement,cifnode.create(
  378. caddnode.create(andn,
  379. caddnode.create(unequaln,
  380. load_self_pointer_node,
  381. cnilnode.create),
  382. caddnode.create(unequaln,
  383. ctypeconvnode.create(
  384. load_vmt_pointer_node,
  385. voidpointertype),
  386. cpointerconstnode.create(0,voidpointertype))),
  387. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  388. nil));
  389. end
  390. else
  391. internalerror(200305108);
  392. end
  393. else
  394. if is_object(current_objectdef) then
  395. begin
  396. { finalize object data }
  397. if current_objectdef.needs_inittable then
  398. addstatement(newstatement,finalize_data_node(load_self_node));
  399. { parameter 3 : vmt_offset }
  400. { parameter 2 : pointer to vmt }
  401. { parameter 1 : self pointer }
  402. para:=ccallparanode.create(
  403. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  404. ccallparanode.create(
  405. ctypeconvnode.create_internal(
  406. load_vmt_pointer_node,
  407. voidpointertype),
  408. ccallparanode.create(
  409. ctypeconvnode.create_internal(
  410. load_self_pointer_node,
  411. voidpointertype),
  412. nil)));
  413. addstatement(newstatement,
  414. ccallnode.createintern('fpc_help_destructor',para));
  415. end
  416. else
  417. internalerror(200305105);
  418. end;
  419. current_settings.localswitches:=oldlocalswitches;
  420. end;
  421. end;
  422. function generate_except_block:tnode;
  423. var
  424. pd : tprocdef;
  425. newstatement : tstatementnode;
  426. oldlocalswitches: tlocalswitches;
  427. begin
  428. generate_except_block:=internalstatements(newstatement);
  429. { a constructor needs call destructor (if available) when it
  430. is not inherited }
  431. if assigned(current_objectdef) and
  432. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  433. begin
  434. { Don't test self and the vmt here. See generate_bodyexit_block }
  435. { why (JM) }
  436. oldlocalswitches:=current_settings.localswitches;
  437. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  438. pd:=current_objectdef.Finddestructor;
  439. if assigned(pd) then
  440. begin
  441. { if vmt<>0 then call destructor }
  442. addstatement(newstatement,cifnode.create(
  443. caddnode.create(unequaln,
  444. load_vmt_pointer_node,
  445. cnilnode.create),
  446. { cnf_create_failed -> don't call BeforeDestruction }
  447. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed]),
  448. nil));
  449. end;
  450. current_settings.localswitches:=oldlocalswitches;
  451. end
  452. else
  453. begin
  454. { no constructor }
  455. { must be the return value finalized before reraising the exception? }
  456. if (not is_void(current_procinfo.procdef.returndef)) and
  457. (current_procinfo.procdef.returndef.needs_inittable) and
  458. (not paramanager.ret_in_param(current_procinfo.procdef.returndef, current_procinfo.procdef.proccalloption)) and
  459. (not is_class(current_procinfo.procdef.returndef)) then
  460. addstatement(newstatement,finalize_data_node(load_result_node));
  461. end;
  462. end;
  463. {****************************************************************************
  464. TCGProcInfo
  465. ****************************************************************************}
  466. constructor tcgprocinfo.create(aparent:tprocinfo);
  467. begin
  468. inherited Create(aparent);
  469. nestedprocs:=tlinkedlist.create;
  470. end;
  471. destructor tcgprocinfo.destroy;
  472. begin
  473. nestedprocs.free;
  474. if assigned(code) then
  475. code.free;
  476. inherited destroy;
  477. end;
  478. procedure tcgprocinfo.printproc;
  479. begin
  480. assign(printnodefile,treelogfilename);
  481. {$I-}
  482. append(printnodefile);
  483. if ioresult<>0 then
  484. rewrite(printnodefile);
  485. {$I+}
  486. if ioresult<>0 then
  487. begin
  488. Comment(V_Error,'Error creating '+treelogfilename);
  489. exit;
  490. end;
  491. writeln(printnodefile);
  492. writeln(printnodefile,'*******************************************************************************');
  493. writeln(printnodefile,procdef.fullprocname(false));
  494. writeln(printnodefile,'*******************************************************************************');
  495. printnode(printnodefile,code);
  496. close(printnodefile);
  497. end;
  498. procedure tcgprocinfo.add_entry_exit_code;
  499. var
  500. finalcode,
  501. bodyentrycode,
  502. bodyexitcode,
  503. exceptcode : tnode;
  504. newblock : tblocknode;
  505. codestatement,
  506. newstatement : tstatementnode;
  507. oldfilepos : tfileposinfo;
  508. begin
  509. oldfilepos:=current_filepos;
  510. { Generate code/locations used at start of proc }
  511. current_filepos:=entrypos;
  512. entry_asmnode:=casmnode.create_get_position;
  513. loadpara_asmnode:=casmnode.create_get_position;
  514. stackcheck_asmnode:=casmnode.create_get_position;
  515. init_asmnode:=casmnode.create_get_position;
  516. bodyentrycode:=generate_bodyentry_block;
  517. { Generate code/locations used at end of proc }
  518. current_filepos:=exitpos;
  519. exitlabel_asmnode:=casmnode.create_get_position;
  520. final_asmnode:=casmnode.create_get_position;
  521. bodyexitcode:=generate_bodyexit_block;
  522. { Generate procedure by combining init+body+final,
  523. depending on the implicit finally we need to add
  524. an try...finally...end wrapper }
  525. newblock:=internalstatements(newstatement);
  526. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  527. (pi_needs_implicit_finally in flags) and
  528. { but it's useless in init/final code of units }
  529. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  530. begin
  531. { Generate special exception block only needed when
  532. implicit finaly is used }
  533. current_filepos:=exitpos;
  534. exceptcode:=generate_except_block;
  535. { Generate code that will be in the try...finally }
  536. finalcode:=internalstatements(codestatement);
  537. addstatement(codestatement,final_asmnode);
  538. { Initialize before try...finally...end frame }
  539. addstatement(newstatement,loadpara_asmnode);
  540. addstatement(newstatement,stackcheck_asmnode);
  541. addstatement(newstatement,entry_asmnode);
  542. addstatement(newstatement,init_asmnode);
  543. addstatement(newstatement,bodyentrycode);
  544. current_filepos:=entrypos;
  545. addstatement(newstatement,ctryfinallynode.create_implicit(
  546. code,
  547. finalcode,
  548. exceptcode));
  549. addstatement(newstatement,exitlabel_asmnode);
  550. addstatement(newstatement,bodyexitcode);
  551. { set flag the implicit finally has been generated }
  552. include(flags,pi_has_implicit_finally);
  553. end
  554. else
  555. begin
  556. addstatement(newstatement,loadpara_asmnode);
  557. addstatement(newstatement,stackcheck_asmnode);
  558. addstatement(newstatement,entry_asmnode);
  559. addstatement(newstatement,init_asmnode);
  560. addstatement(newstatement,bodyentrycode);
  561. addstatement(newstatement,code);
  562. addstatement(newstatement,exitlabel_asmnode);
  563. addstatement(newstatement,bodyexitcode);
  564. addstatement(newstatement,final_asmnode);
  565. end;
  566. do_firstpass(newblock);
  567. code:=newblock;
  568. current_filepos:=oldfilepos;
  569. end;
  570. procedure clearrefs(p:TObject;arg:pointer);
  571. begin
  572. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  573. if tabstractvarsym(p).refs>1 then
  574. tabstractvarsym(p).refs:=1;
  575. end;
  576. procedure translate_registers(p:TObject;list:pointer);
  577. begin
  578. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  579. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  580. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  581. begin
  582. if not(cs_no_regalloc in current_settings.globalswitches) then
  583. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  584. if cs_asm_source in current_settings.globalswitches then
  585. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  586. std_regname(tabstractnormalvarsym(p).localloc.register))))
  587. end;
  588. end;
  589. procedure check_for_stack(p:TObject;arg:pointer);
  590. begin
  591. if tsym(p).typ=paravarsym then
  592. begin
  593. { check if there no parameter of the current procedure is stack dependend }
  594. if is_open_array(tparavarsym(p).vardef) or
  595. is_array_of_const(tparavarsym(p).vardef) then
  596. pboolean(arg)^:=true;
  597. if assigned(p) and
  598. assigned(tparavarsym(p).paraloc[calleeside].location) and
  599. (tparavarsym(p).paraloc[calleeside].location^.loc=LOC_REFERENCE) then
  600. pboolean(arg)^:=true;
  601. end;
  602. end;
  603. function tcgprocinfo.stack_tainting_parameter : boolean;
  604. begin
  605. result:=false;
  606. procdef.parast.SymList.ForEachCall(@check_for_stack,@result);
  607. end;
  608. function tcgprocinfo.has_assembler_child : boolean;
  609. var
  610. hp : tcgprocinfo;
  611. begin
  612. result:=false;
  613. hp:=tcgprocinfo(nestedprocs.first);
  614. while assigned(hp) do
  615. begin
  616. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  617. begin
  618. result:=true;
  619. exit;
  620. end;
  621. hp:=tcgprocinfo(hp.next);
  622. end;
  623. end;
  624. procedure tcgprocinfo.generate_code;
  625. var
  626. old_current_procinfo : tprocinfo;
  627. oldmaxfpuregisters : longint;
  628. oldfilepos : tfileposinfo;
  629. old_current_objectdef : tobjectdef;
  630. templist : TAsmList;
  631. headertai : tai;
  632. i : integer;
  633. varsym : tabstractnormalvarsym;
  634. RedoDFA : boolean;
  635. begin
  636. { the initialization procedure can be empty, then we
  637. don't need to generate anything. When it was an empty
  638. procedure there would be at least a blocknode }
  639. if not assigned(code) then
  640. exit;
  641. { We need valid code }
  642. if Errorcount<>0 then
  643. exit;
  644. { No code can be generated for generic template }
  645. if (df_generic in procdef.defoptions) then
  646. internalerror(200511152);
  647. { The RA and Tempgen shall not be available yet }
  648. if assigned(tg) then
  649. internalerror(200309201);
  650. old_current_procinfo:=current_procinfo;
  651. oldfilepos:=current_filepos;
  652. old_current_objectdef:=current_objectdef;
  653. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  654. current_procinfo:=self;
  655. current_filepos:=entrypos;
  656. current_objectdef:=procdef._class;
  657. templist:=TAsmList.create;
  658. { add parast/localst to symtablestack }
  659. add_to_symtablestack;
  660. { clear register count }
  661. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  662. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  663. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  664. if (procdef.localst.symtablelevel=main_program_level) and
  665. (not current_module.is_unit) then
  666. include(flags,pi_do_call);
  667. { set implicit_finally flag when there are locals/paras to be finalized }
  668. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  669. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  670. {$if defined(x86) or defined(arm)}
  671. { set implicit_finally flag for if procedure is safecall }
  672. if (target_info.system in system_all_windows) and
  673. (procdef.proccalloption=pocall_safecall) then
  674. include(flags, pi_needs_implicit_finally);
  675. {$endif}
  676. { firstpass everything }
  677. flowcontrol:=[];
  678. do_firstpass(code);
  679. {$ifdef i386}
  680. procdef.fpu_used:=node_resources_fpu(code);
  681. if procdef.fpu_used>0 then
  682. include(flags,pi_uses_fpu);
  683. {$endif i386}
  684. { do this before adding the entry code else the tail recursion recognition won't work,
  685. if this causes troubles, it must be if'ed
  686. }
  687. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  688. (pi_is_recursive in flags) then
  689. do_opttail(code,procdef);
  690. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  691. { creating dfa is not always possible }
  692. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  693. pi_needs_implicit_finally,pi_has_implicit_finally])=[]) then
  694. begin
  695. dfabuilder:=TDFABuilder.Create;
  696. dfabuilder.createdfainfo(code);
  697. { when life info is available, we can give more sophisticated warning about unintialized
  698. variables }
  699. { iterate through life info of the first node }
  700. for i:=0 to dfabuilder.nodemap.count-1 do
  701. begin
  702. if DFASetIn(code.optinfo^.life,i) then
  703. case tnode(dfabuilder.nodemap[i]).nodetype of
  704. loadn:
  705. begin
  706. varsym:=tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry);
  707. { Give warning/note for living locals }
  708. if assigned(varsym.owner) and
  709. not(vo_is_external in varsym.varoptions) then
  710. begin
  711. if (vo_is_funcret in varsym.varoptions) then
  712. CGMessage(sym_w_function_result_uninitialized)
  713. else
  714. begin
  715. if varsym.owner=procdef.localst then
  716. CGMessage1(sym_w_uninitialized_local_variable,varsym.realname);
  717. end;
  718. end;
  719. end;
  720. end;
  721. end;
  722. include(flags,pi_dfaavailable);
  723. end;
  724. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  725. { our induction variable strength reduction doesn't like
  726. for loops with more than one entry }
  727. and not(pi_has_goto in current_procinfo.flags) then
  728. begin
  729. RedoDFA:=OptimizeInductionVariables(code);
  730. end;
  731. if cs_opt_nodecse in current_settings.optimizerswitches then
  732. do_optcse(code);
  733. { add implicit entry and exit code }
  734. add_entry_exit_code;
  735. { only do secondpass if there are no errors }
  736. if (ErrorCount=0) then
  737. begin
  738. { set the start offset to the start of the temp area in the stack }
  739. tg:=ttgobj.create;
  740. {$if defined(x86) or defined(arm)}
  741. { try to strip the stack frame }
  742. { set the framepointer to esp if:
  743. - no assembler directive, those are handled in assembler_block
  744. in pstatment.pas (for cases not caught by the Delphi
  745. exception below)
  746. - no exceptions are used
  747. - no pushes are used/esp modifications, could be:
  748. * outgoing parameters on the stack
  749. * incoming parameters on the stack
  750. * open arrays
  751. - no inline assembler
  752. or
  753. - Delphi mode
  754. - assembler directive
  755. - no pushes are used/esp modifications, could be:
  756. * outgoing parameters on the stack
  757. * incoming parameters on the stack
  758. * open arrays
  759. - no local variables
  760. }
  761. if ((po_assembler in procdef.procoptions) and
  762. (m_delphi in current_settings.modeswitches) and
  763. { localst at main_program_level is a staticsymtable }
  764. (procdef.localst.symtablelevel<>main_program_level) and
  765. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  766. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  767. not(cs_generate_stackframes in current_settings.localswitches) and
  768. not(po_assembler in procdef.procoptions) and
  769. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler,
  770. pi_needs_implicit_finally,pi_has_implicit_finally,pi_has_stackparameter,
  771. pi_needs_stackframe])=[])
  772. )
  773. then
  774. begin
  775. { we need the parameter info here to determine if the procedure gets
  776. parameters on the stack
  777. calling generate_parameter_info doesn't hurt but it costs time
  778. }
  779. generate_parameter_info;
  780. if not(stack_tainting_parameter) and
  781. not(has_assembler_child) and (para_stack_size=0) then
  782. begin
  783. { Only need to set the framepointer }
  784. framepointer:=NR_STACK_POINTER_REG;
  785. tg.direction:=1;
  786. end;
  787. end;
  788. {$endif}
  789. { Create register allocator }
  790. cg.init_register_allocators;
  791. set_first_temp_offset;
  792. generate_parameter_info;
  793. { allocate got register if needed }
  794. current_procinfo.allocate_got_register(aktproccode);
  795. { Allocate space in temp/registers for parast and localst }
  796. current_filepos:=entrypos;
  797. gen_alloc_symtable(aktproccode,procdef.parast);
  798. gen_alloc_symtable(aktproccode,procdef.localst);
  799. { Store temp offset for information about 'real' temps }
  800. tempstart:=tg.lasttemp;
  801. { Generate code to load register parameters in temps and insert local
  802. copies for values parameters. This must be done before the code for the
  803. body is generated because the localloc is updated.
  804. Note: The generated code will be inserted after the code generation of
  805. the body is finished, because only then the position is known }
  806. {$ifdef oldregvars}
  807. assign_regvars(code);
  808. {$endif oldreg}
  809. current_filepos:=entrypos;
  810. { record which registers are allocated here, since all code }
  811. { allocating registers comes after it }
  812. cg.set_regalloc_live_range_direction(rad_backwards);
  813. gen_load_para_value(templist);
  814. cg.set_regalloc_live_range_direction(rad_forward);
  815. { caller paraloc info is also necessary in the stackframe_entry
  816. code of the ppc (and possibly other processors) }
  817. if not procdef.has_paraloc_info then
  818. begin
  819. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  820. procdef.has_paraloc_info:=true;
  821. end;
  822. { generate code for the node tree }
  823. do_secondpass(code);
  824. aktproccode.concatlist(current_asmdata.CurrAsmList);
  825. { The position of the loadpara_asmnode is now known }
  826. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  827. { first generate entry and initialize code with the correct
  828. position and switches }
  829. current_filepos:=entrypos;
  830. current_settings.localswitches:=entryswitches;
  831. cg.set_regalloc_live_range_direction(rad_backwards);
  832. gen_entry_code(templist);
  833. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  834. gen_initialize_code(templist);
  835. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  836. { now generate finalize and exit code with the correct position
  837. and switches }
  838. current_filepos:=exitpos;
  839. current_settings.localswitches:=exitswitches;
  840. cg.set_regalloc_live_range_direction(rad_forward);
  841. gen_finalize_code(templist);
  842. { the finalcode must be concated if there was no position available,
  843. using insertlistafter will result in an insert at the start
  844. when currentai=nil }
  845. if assigned(final_asmnode.currenttai) then
  846. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  847. else
  848. aktproccode.concatlist(templist);
  849. { insert exit label at the correct position }
  850. cg.a_label(templist,CurrExitLabel);
  851. if assigned(exitlabel_asmnode.currenttai) then
  852. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  853. else
  854. aktproccode.concatlist(templist);
  855. { exit code }
  856. gen_exit_code(templist);
  857. aktproccode.concatlist(templist);
  858. {$ifdef OLDREGVARS}
  859. { note: this must be done only after as much code as possible has }
  860. { been generated. The result is that when you ungetregister() a }
  861. { regvar, it will actually free the regvar (and alse free the }
  862. { the regvars at the same time). Doing this too early will }
  863. { confuse the register allocator, as the regvars will still be }
  864. { used. It should be done before loading the result regs (so }
  865. { they don't conflict with the regvars) and before }
  866. { gen_entry_code (that one has to be able to allocate the }
  867. { regvars again) (JM) }
  868. free_regvars(aktproccode);
  869. {$endif OLDREGVARS}
  870. { generate symbol and save end of header position }
  871. current_filepos:=entrypos;
  872. gen_proc_symbol(templist);
  873. headertai:=tai(templist.last);
  874. { insert symbol }
  875. aktproccode.insertlist(templist);
  876. { Free space in temp/registers for parast and localst, must be
  877. done after gen_entry_code }
  878. current_filepos:=exitpos;
  879. { make sure the got/pic register doesn't get freed in the }
  880. { middle of a loop }
  881. if (cs_create_pic in current_settings.moduleswitches) and
  882. (pi_needs_got in current_procinfo.flags) and
  883. (current_procinfo.got<>NR_NO) then
  884. cg.a_reg_sync(aktproccode,current_procinfo.got);
  885. gen_free_symtable(aktproccode,procdef.localst);
  886. gen_free_symtable(aktproccode,procdef.parast);
  887. { add code that will load the return value, this is not done
  888. for assembler routines when they didn't reference the result
  889. variable }
  890. gen_load_return_value(templist);
  891. aktproccode.concatlist(templist);
  892. { Already reserve all registers for stack checking code and
  893. generate the call to the helper function }
  894. if not(tf_no_generic_stackcheck in target_info.flags) and
  895. (cs_check_stack in entryswitches) and
  896. not(po_assembler in procdef.procoptions) and
  897. (procdef.proctypeoption<>potype_proginit) then
  898. begin
  899. current_filepos:=entrypos;
  900. gen_stack_check_call(templist);
  901. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  902. end;
  903. { this code (got loading) comes before everything which has }
  904. { already been generated, so reset the info about already }
  905. { backwards extended registers (so their live range can be }
  906. { extended backwards even further if needed) }
  907. { This code must be }
  908. { a) generated after do_secondpass has been called }
  909. { (because pi_needs_got may be set there) }
  910. { b) generated before register allocation, because the }
  911. { got/pic register can be a virtual one }
  912. { c) inserted before the entry code, because the entry }
  913. { code may need global symbols such as init rtti }
  914. { d) inserted after the stackframe allocation, because }
  915. { this register may have to be spilled }
  916. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  917. current_filepos:=entrypos;
  918. { load got if necessary }
  919. cg.g_maybe_got_init(templist);
  920. aktproccode.insertlistafter(headertai,templist);
  921. cg.set_regalloc_live_range_direction(rad_forward);
  922. { The procedure body is finished, we can now
  923. allocate the registers }
  924. cg.do_register_allocation(aktproccode,headertai);
  925. { translate imag. register to their real counter parts
  926. this is necessary for debuginfo and verbose assembler output
  927. when SSA will be implented, this will be more complicated because we've to
  928. maintain location lists }
  929. current_procinfo.procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  930. current_procinfo.procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  931. if (cs_create_pic in current_settings.moduleswitches) and
  932. (pi_needs_got in current_procinfo.flags) and
  933. not(cs_no_regalloc in current_settings.globalswitches) and
  934. (current_procinfo.got<>NR_NO) then
  935. cg.translate_register(current_procinfo.got);
  936. { Add save and restore of used registers }
  937. current_filepos:=entrypos;
  938. gen_save_used_regs(templist);
  939. aktproccode.insertlistafter(headertai,templist);
  940. current_filepos:=exitpos;
  941. gen_restore_used_regs(aktproccode);
  942. { We know the size of the stack, now we can generate the
  943. parameter that is passed to the stack checking code }
  944. if not(tf_no_generic_stackcheck in target_info.flags) and
  945. (cs_check_stack in entryswitches) and
  946. not(po_assembler in procdef.procoptions) and
  947. (procdef.proctypeoption<>potype_proginit) then
  948. begin
  949. current_filepos:=entrypos;
  950. gen_stack_check_size_para(templist);
  951. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  952. end;
  953. { Add entry code (stack allocation) after header }
  954. current_filepos:=entrypos;
  955. gen_proc_entry_code(templist);
  956. aktproccode.insertlistafter(headertai,templist);
  957. {$if defined(x86) or defined(arm)}
  958. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  959. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  960. (target_info.system in system_all_windows) and
  961. (procdef.proccalloption=pocall_safecall) then
  962. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  963. {$endif}
  964. { Add exit code at the end }
  965. current_filepos:=exitpos;
  966. gen_proc_exit_code(templist);
  967. aktproccode.concatlist(templist);
  968. { check if the implicit finally has been generated. The flag
  969. should already be set in pass1 }
  970. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  971. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  972. (pi_needs_implicit_finally in flags) and
  973. not(pi_has_implicit_finally in flags) then
  974. internalerror(200405231);
  975. {$ifndef NoOpt}
  976. if not(cs_no_regalloc in current_settings.globalswitches) then
  977. begin
  978. if (cs_opt_level1 in current_settings.optimizerswitches) and
  979. { do not optimize pure assembler procedures }
  980. not(pi_is_assembler in flags) then
  981. optimize(aktproccode);
  982. end;
  983. {$endif NoOpt}
  984. {$ifdef ARM}
  985. { because of the limited constant size of the arm, all data access is done pc relative }
  986. insertpcrelativedata(aktproccode,aktlocaldata);
  987. {$endif ARM}
  988. { Add end symbol and debug info }
  989. { this must be done after the pcrelativedata is appended else the distance calculation of
  990. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  991. so it should be inserted before the end symbol (FK)
  992. }
  993. current_filepos:=exitpos;
  994. gen_proc_symbol_end(templist);
  995. aktproccode.concatlist(templist);
  996. {$if defined(POWERPC) or defined(POWERPC64)}
  997. fixup_jmps(aktproccode);
  998. {$endif}
  999. { insert line debuginfo }
  1000. if (cs_debuginfo in current_settings.moduleswitches) or
  1001. (cs_use_lineinfo in current_settings.globalswitches) then
  1002. current_debuginfo.insertlineinfo(aktproccode);
  1003. { add the procedure to the al_procedures }
  1004. maybe_new_object_file(current_asmdata.asmlists[al_procedures]);
  1005. new_section(current_asmdata.asmlists[al_procedures],sec_code,lower(procdef.mangledname),getprocalign);
  1006. current_asmdata.asmlists[al_procedures].concatlist(aktproccode);
  1007. { save local data (casetable) also in the same file }
  1008. if assigned(aktlocaldata) and
  1009. (not aktlocaldata.empty) then
  1010. current_asmdata.asmlists[al_procedures].concatlist(aktlocaldata);
  1011. { only now we can remove the temps }
  1012. tg.resettempgen;
  1013. { stop tempgen and ra }
  1014. tg.free;
  1015. cg.done_register_allocators;
  1016. tg:=nil;
  1017. end;
  1018. dfabuilder.free;
  1019. { restore symtablestack }
  1020. remove_from_symtablestack;
  1021. { restore }
  1022. templist.free;
  1023. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1024. current_filepos:=oldfilepos;
  1025. current_objectdef:=old_current_objectdef;
  1026. current_procinfo:=old_current_procinfo;
  1027. end;
  1028. procedure tcgprocinfo.add_to_symtablestack;
  1029. var
  1030. _class,hp : tobjectdef;
  1031. begin
  1032. { insert symtables for the class, but only if it is no nested function }
  1033. if assigned(procdef._class) and
  1034. not(assigned(parent) and
  1035. assigned(parent.procdef) and
  1036. assigned(parent.procdef._class)) then
  1037. begin
  1038. { insert them in the reverse order }
  1039. hp:=nil;
  1040. repeat
  1041. _class:=procdef._class;
  1042. while _class.childof<>hp do
  1043. _class:=_class.childof;
  1044. hp:=_class;
  1045. symtablestack.push(_class.symtable);
  1046. until hp=procdef._class;
  1047. end;
  1048. { insert parasymtable in symtablestack when parsing
  1049. a function }
  1050. if procdef.parast.symtablelevel>=normal_function_level then
  1051. symtablestack.push(procdef.parast);
  1052. { insert localsymtable }
  1053. symtablestack.push(procdef.localst);
  1054. end;
  1055. procedure tcgprocinfo.remove_from_symtablestack;
  1056. var
  1057. _class : tobjectdef;
  1058. begin
  1059. { remove localsymtable }
  1060. symtablestack.pop(procdef.localst);
  1061. { remove parasymtable }
  1062. if procdef.parast.symtablelevel>=normal_function_level then
  1063. symtablestack.pop(procdef.parast);
  1064. { remove symtables for the class, but only if it is no nested function }
  1065. if assigned(procdef._class) and
  1066. not(assigned(parent) and
  1067. assigned(parent.procdef) and
  1068. assigned(parent.procdef._class)) then
  1069. begin
  1070. _class:=procdef._class;
  1071. while assigned(_class) do
  1072. begin
  1073. symtablestack.pop(_class.symtable);
  1074. _class:=_class.childof;
  1075. end;
  1076. end;
  1077. end;
  1078. procedure tcgprocinfo.resetprocdef;
  1079. begin
  1080. { remove code tree, if not inline procedure }
  1081. if assigned(code) then
  1082. begin
  1083. { the inline procedure has already got a copy of the tree
  1084. stored in procdef.inlininginfo }
  1085. code.free;
  1086. code:=nil;
  1087. end;
  1088. end;
  1089. function checknodeinlining(procdef: tprocdef): boolean;
  1090. var
  1091. i : integer;
  1092. currpara : tparavarsym;
  1093. begin
  1094. result := false;
  1095. if (pi_has_assembler_block in current_procinfo.flags) then
  1096. begin
  1097. Message1(parser_w_not_supported_for_inline,'assembler');
  1098. Message(parser_w_inlining_disabled);
  1099. exit;
  1100. end;
  1101. for i:=0 to procdef.paras.count-1 do
  1102. begin
  1103. currpara:=tparavarsym(procdef.paras[i]);
  1104. case currpara.vardef.typ of
  1105. formaldef :
  1106. begin
  1107. if (currpara.varspez in [vs_out,vs_var,vs_const]) then
  1108. begin
  1109. Message1(parser_w_not_supported_for_inline,'formal parameter');
  1110. Message(parser_w_inlining_disabled);
  1111. exit;
  1112. end;
  1113. end;
  1114. arraydef :
  1115. begin
  1116. if is_array_of_const(currpara.vardef) or
  1117. is_variant_array(currpara.vardef) then
  1118. begin
  1119. Message1(parser_w_not_supported_for_inline,'array of const');
  1120. Message(parser_w_inlining_disabled);
  1121. exit;
  1122. end;
  1123. { open arrays might need re-basing of the index, i.e. if you pass
  1124. an array[1..10] as open array, you have to add 1 to all index operations
  1125. if you directly inline it }
  1126. if is_open_array(currpara.vardef) then
  1127. begin
  1128. Message1(parser_w_not_supported_for_inline,'open array');
  1129. Message(parser_w_inlining_disabled);
  1130. exit;
  1131. end;
  1132. end;
  1133. end;
  1134. end;
  1135. result:=true;
  1136. end;
  1137. procedure tcgprocinfo.parse_body;
  1138. var
  1139. old_current_procinfo : tprocinfo;
  1140. old_block_type : tblock_type;
  1141. st : TSymtable;
  1142. old_current_objectdef : tobjectdef;
  1143. begin
  1144. old_current_procinfo:=current_procinfo;
  1145. old_block_type:=block_type;
  1146. old_current_objectdef:=current_objectdef;
  1147. current_procinfo:=self;
  1148. current_objectdef:=procdef._class;
  1149. { calculate the lexical level }
  1150. if procdef.parast.symtablelevel>maxnesting then
  1151. Message(parser_e_too_much_lexlevel);
  1152. block_type:=bt_body;
  1153. {$ifdef state_tracking}
  1154. { aktstate:=Tstate_storage.create;}
  1155. {$endif state_tracking}
  1156. { allocate the symbol for this procedure }
  1157. alloc_proc_symbol(procdef);
  1158. { add parast/localst to symtablestack }
  1159. add_to_symtablestack;
  1160. { save entry info }
  1161. entrypos:=current_filepos;
  1162. entryswitches:=current_settings.localswitches;
  1163. if (df_generic in procdef.defoptions) then
  1164. begin
  1165. { start token recorder for generic template }
  1166. procdef.initgeneric;
  1167. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1168. end;
  1169. { parse the code ... }
  1170. code:=block(current_module.islibrary);
  1171. if (df_generic in procdef.defoptions) then
  1172. begin
  1173. { stop token recorder for generic template }
  1174. current_scanner.stoprecordtokens;
  1175. { Give an error for accesses in the static symtable that aren't visible
  1176. outside the current unit }
  1177. st:=procdef.owner;
  1178. while (st.symtabletype=ObjectSymtable) do
  1179. st:=st.defowner.owner;
  1180. if (pi_uses_static_symtable in flags) and
  1181. (st.symtabletype<>staticsymtable) then
  1182. Comment(V_Error,'Global Generic template references static symtable');
  1183. end;
  1184. { save exit info }
  1185. exitswitches:=current_settings.localswitches;
  1186. exitpos:=last_endtoken_filepos;
  1187. { the procedure is now defined }
  1188. procdef.forwarddef:=false;
  1189. if assigned(code) then
  1190. begin
  1191. { get a better entry point }
  1192. entrypos:=code.fileinfo;
  1193. { Finish type checking pass }
  1194. do_typecheckpass(code);
  1195. end;
  1196. { Check for unused labels, forwards, symbols for procedures. Static
  1197. symtable is checked in pmodules.
  1198. The check must be done after the typecheckpass }
  1199. if (Errorcount=0) and
  1200. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1201. begin
  1202. { check if forwards are resolved }
  1203. tstoredsymtable(procdef.localst).check_forwards;
  1204. { check if all labels are used }
  1205. tstoredsymtable(procdef.localst).checklabels;
  1206. { check for unused symbols, but only if there is no asm block }
  1207. if not(pi_has_assembler_block in flags) then
  1208. begin
  1209. tstoredsymtable(procdef.localst).allsymbolsused;
  1210. tstoredsymtable(procdef.parast).allsymbolsused;
  1211. end;
  1212. end;
  1213. if (po_inline in procdef.procoptions) then
  1214. begin
  1215. { Can we inline this procedure? }
  1216. if checknodeinlining(procdef) then
  1217. begin
  1218. new(procdef.inlininginfo);
  1219. include(procdef.procoptions,po_has_inlininginfo);
  1220. procdef.inlininginfo^.code:=code.getcopy;
  1221. procdef.inlininginfo^.flags:=current_procinfo.flags;
  1222. { The blocknode needs to set an exit label }
  1223. if procdef.inlininginfo^.code.nodetype=blockn then
  1224. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1225. end;
  1226. end;
  1227. { Print the node to tree.log }
  1228. if paraprintnodetree=1 then
  1229. printproc;
  1230. { ... remove symbol tables }
  1231. remove_from_symtablestack;
  1232. {$ifdef state_tracking}
  1233. { aktstate.destroy;}
  1234. {$endif state_tracking}
  1235. current_objectdef:=old_current_objectdef;
  1236. current_procinfo:=old_current_procinfo;
  1237. { Restore old state }
  1238. block_type:=old_block_type;
  1239. end;
  1240. {****************************************************************************
  1241. PROCEDURE/FUNCTION PARSING
  1242. ****************************************************************************}
  1243. procedure check_init_paras(p:TObject;arg:pointer);
  1244. begin
  1245. if tsym(p).typ<>paravarsym then
  1246. exit;
  1247. with tparavarsym(p) do
  1248. if (not is_class(vardef) and
  1249. vardef.needs_inittable and
  1250. (varspez in [vs_value,vs_out])) then
  1251. include(current_procinfo.flags,pi_do_call);
  1252. end;
  1253. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1254. {
  1255. Parses the procedure directives, then parses the procedure body, then
  1256. generates the code for it
  1257. }
  1258. procedure do_generate_code(pi:tcgprocinfo);
  1259. var
  1260. hpi : tcgprocinfo;
  1261. begin
  1262. { generate code for this procedure }
  1263. pi.generate_code;
  1264. { process nested procs }
  1265. hpi:=tcgprocinfo(pi.nestedprocs.first);
  1266. while assigned(hpi) do
  1267. begin
  1268. do_generate_code(hpi);
  1269. hpi:=tcgprocinfo(hpi.next);
  1270. end;
  1271. pi.resetprocdef;
  1272. end;
  1273. var
  1274. oldfailtokenmode : tmodeswitch;
  1275. isnestedproc : boolean;
  1276. begin
  1277. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1278. { create a new procedure }
  1279. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1280. current_module.procinfo:=current_procinfo;
  1281. current_procinfo.procdef:=pd;
  1282. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1283. { Insert mangledname }
  1284. pd.aliasnames.insert(pd.mangledname);
  1285. { Handle Export of this procedure }
  1286. if (po_exports in pd.procoptions) and
  1287. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1288. begin
  1289. pd.aliasnames.insert(pd.procsym.realname);
  1290. if cs_link_deffile in current_settings.globalswitches then
  1291. deffile.AddExport(pd.mangledname);
  1292. end;
  1293. { Insert result variables in the localst }
  1294. insert_funcret_local(pd);
  1295. { check if there are para's which require initing -> set }
  1296. { pi_do_call (if not yet set) }
  1297. if not(pi_do_call in current_procinfo.flags) then
  1298. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1299. { set _FAIL as keyword if constructor }
  1300. if (pd.proctypeoption=potype_constructor) then
  1301. begin
  1302. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1303. tokeninfo^[_FAIL].keyword:=m_all;
  1304. end;
  1305. tcgprocinfo(current_procinfo).parse_body;
  1306. { When it's a nested procedure then defer the code generation,
  1307. when back at normal function level then generate the code
  1308. for all defered nested procedures and the current procedure }
  1309. if isnestedproc then
  1310. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1311. else
  1312. begin
  1313. { We can't support inlining for procedures that have nested
  1314. procedures because the nested procedures use a fixed offset
  1315. for accessing locals in the parent procedure (PFV) }
  1316. if (tcgprocinfo(current_procinfo).nestedprocs.count>0) then
  1317. begin
  1318. if (df_generic in current_procinfo.procdef.defoptions) then
  1319. Comment(V_Error,'Generic methods cannot have nested procedures')
  1320. else
  1321. if (po_inline in current_procinfo.procdef.procoptions) then
  1322. begin
  1323. Message1(parser_w_not_supported_for_inline,'nested procedures');
  1324. Message(parser_w_inlining_disabled);
  1325. exclude(current_procinfo.procdef.procoptions,po_inline);
  1326. end;
  1327. end;
  1328. if not(df_generic in current_procinfo.procdef.defoptions) then
  1329. do_generate_code(tcgprocinfo(current_procinfo));
  1330. end;
  1331. { reset _FAIL as _SELF normal }
  1332. if (pd.proctypeoption=potype_constructor) then
  1333. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1334. { release procinfo }
  1335. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1336. internalerror(200304274);
  1337. current_module.procinfo:=current_procinfo.parent;
  1338. { For specialization we didn't record the last semicolon. Moving this parsing
  1339. into the parse_body routine is not done because of having better file position
  1340. information available }
  1341. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1342. consume(_SEMICOLON);
  1343. if not isnestedproc then
  1344. { current_procinfo is checked for nil later on }
  1345. freeandnil(current_procinfo);
  1346. end;
  1347. procedure read_proc;
  1348. {
  1349. Parses the procedure directives, then parses the procedure body, then
  1350. generates the code for it
  1351. }
  1352. var
  1353. old_current_procinfo : tprocinfo;
  1354. old_current_objectdef : tobjectdef;
  1355. pdflags : tpdflags;
  1356. pd,firstpd : tprocdef;
  1357. s : string;
  1358. begin
  1359. { save old state }
  1360. old_current_procinfo:=current_procinfo;
  1361. old_current_objectdef:=current_objectdef;
  1362. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1363. to an other procdef }
  1364. current_procinfo:=nil;
  1365. current_objectdef:=nil;
  1366. { parse procedure declaration }
  1367. pd:=parse_proc_dec(old_current_objectdef);
  1368. { set the default function options }
  1369. if parse_only then
  1370. begin
  1371. pd.forwarddef:=true;
  1372. { set also the interface flag, for better error message when the
  1373. implementation doesn't much this header }
  1374. pd.interfacedef:=true;
  1375. include(pd.procoptions,po_global);
  1376. pdflags:=[pd_interface];
  1377. end
  1378. else
  1379. begin
  1380. pdflags:=[pd_body];
  1381. if (not current_module.in_interface) then
  1382. include(pdflags,pd_implemen);
  1383. if (not current_module.is_unit) or
  1384. create_smartlink or
  1385. {
  1386. taking addresses of static procedures goes wrong
  1387. if they aren't global when pic is used (FK)
  1388. }
  1389. (cs_create_pic in current_settings.moduleswitches) then
  1390. include(pd.procoptions,po_global);
  1391. pd.forwarddef:=false;
  1392. end;
  1393. { parse the directives that may follow }
  1394. parse_proc_directives(pd,pdflags);
  1395. { hint directives, these can be separated by semicolons here,
  1396. that needs to be handled here with a loop (PFV) }
  1397. while try_consume_hintdirective(pd.symoptions) do
  1398. Consume(_SEMICOLON);
  1399. { Set calling convention }
  1400. handle_calling_convention(pd);
  1401. { search for forward declarations }
  1402. if not proc_add_definition(pd) then
  1403. begin
  1404. { A method must be forward defined (in the object declaration) }
  1405. if assigned(pd._class) and
  1406. (not assigned(old_current_objectdef)) then
  1407. begin
  1408. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1409. tprocsym(pd.procsym).write_parameter_lists(pd);
  1410. end
  1411. else
  1412. begin
  1413. { Give a better error if there is a forward def in the interface and only
  1414. a single implementation }
  1415. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1416. if (not pd.forwarddef) and
  1417. (not pd.interfacedef) and
  1418. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1419. firstpd.forwarddef and
  1420. firstpd.interfacedef and
  1421. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1422. { don't give an error if it may be an overload }
  1423. not(m_fpc in current_settings.modeswitches) and
  1424. (not(po_overload in pd.procoptions) or
  1425. not(po_overload in firstpd.procoptions)) then
  1426. begin
  1427. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1428. tprocsym(pd.procsym).write_parameter_lists(pd);
  1429. end;
  1430. end;
  1431. end;
  1432. { Set mangled name }
  1433. proc_set_mangledname(pd);
  1434. { compile procedure when a body is needed }
  1435. if (pd_body in pdflags) then
  1436. begin
  1437. read_proc_body(old_current_procinfo,pd);
  1438. end
  1439. else
  1440. begin
  1441. { Handle imports }
  1442. if (po_external in pd.procoptions) then
  1443. begin
  1444. { External declared in implementation, and there was already a
  1445. forward (or interface) declaration then we need to generate
  1446. a stub that calls the external routine }
  1447. if (not pd.forwarddef) and
  1448. (pd.hasforward) and
  1449. not(
  1450. assigned(pd.import_dll) and
  1451. (target_info.system in [system_i386_wdosx,
  1452. system_arm_wince,system_i386_wince])
  1453. ) then
  1454. begin
  1455. s:=proc_get_importname(pd);
  1456. if s<>'' then
  1457. gen_external_stub(current_asmdata.asmlists[al_procedures],pd,s);
  1458. end;
  1459. { Import DLL specified? }
  1460. if assigned(pd.import_dll) then
  1461. current_module.AddExternalImport(pd.import_dll^,proc_get_importname(pd),pd.import_nr,false,pd.import_name=nil)
  1462. else
  1463. begin
  1464. { add import name to external list for DLL scanning }
  1465. if tf_has_dllscanner in target_info.flags then
  1466. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  1467. end;
  1468. end;
  1469. end;
  1470. { make sure that references to forward-declared functions are not }
  1471. { treated as references to external symbols, needed for darwin. }
  1472. { make sure we don't change the binding of real external symbols }
  1473. if not(po_external in pd.procoptions) then
  1474. begin
  1475. if (po_global in pd.procoptions) or
  1476. (cs_profile in current_settings.moduleswitches) then
  1477. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1478. else
  1479. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1480. end;
  1481. current_objectdef:=old_current_objectdef;
  1482. current_procinfo:=old_current_procinfo;
  1483. end;
  1484. {****************************************************************************
  1485. DECLARATION PARSING
  1486. ****************************************************************************}
  1487. { search in symtablestack for not complete classes }
  1488. procedure check_forward_class(p:TObject;arg:pointer);
  1489. begin
  1490. if (tsym(p).typ=typesym) and
  1491. (ttypesym(p).typedef.typ=objectdef) and
  1492. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  1493. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1494. end;
  1495. procedure read_declarations(islibrary : boolean);
  1496. begin
  1497. repeat
  1498. if not assigned(current_procinfo) then
  1499. internalerror(200304251);
  1500. case token of
  1501. _LABEL:
  1502. label_dec;
  1503. _CONST:
  1504. const_dec;
  1505. _TYPE:
  1506. type_dec;
  1507. _VAR:
  1508. var_dec;
  1509. _THREADVAR:
  1510. threadvar_dec;
  1511. _CONSTRUCTOR,
  1512. _DESTRUCTOR,
  1513. _FUNCTION,
  1514. _PROCEDURE,
  1515. _OPERATOR,
  1516. _CLASS:
  1517. read_proc;
  1518. _EXPORTS:
  1519. begin
  1520. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  1521. begin
  1522. Message(parser_e_syntax_error);
  1523. consume_all_until(_SEMICOLON);
  1524. end
  1525. else if islibrary or
  1526. (target_info.system in system_unit_program_exports) then
  1527. read_exports
  1528. else
  1529. begin
  1530. Message(parser_w_unsupported_feature);
  1531. consume(_BEGIN);
  1532. end;
  1533. end
  1534. else
  1535. begin
  1536. case idtoken of
  1537. _RESOURCESTRING :
  1538. begin
  1539. { m_class is needed, because the resourcestring
  1540. loading is in the ObjPas unit }
  1541. { if (m_class in current_settings.modeswitches) then}
  1542. resourcestring_dec
  1543. { else
  1544. break;}
  1545. end;
  1546. _PROPERTY:
  1547. begin
  1548. if (m_fpc in current_settings.modeswitches) then
  1549. property_dec
  1550. else
  1551. break;
  1552. end;
  1553. else
  1554. break;
  1555. end;
  1556. end;
  1557. end;
  1558. until false;
  1559. { check for incomplete class definitions, this is only required
  1560. for fpc modes }
  1561. if (m_fpc in current_settings.modeswitches) then
  1562. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  1563. end;
  1564. procedure read_interface_declarations;
  1565. begin
  1566. repeat
  1567. case token of
  1568. _CONST :
  1569. const_dec;
  1570. _TYPE :
  1571. type_dec;
  1572. _VAR :
  1573. var_dec;
  1574. _THREADVAR :
  1575. threadvar_dec;
  1576. _FUNCTION,
  1577. _PROCEDURE,
  1578. _OPERATOR :
  1579. read_proc;
  1580. else
  1581. begin
  1582. case idtoken of
  1583. _RESOURCESTRING :
  1584. resourcestring_dec;
  1585. _PROPERTY:
  1586. begin
  1587. if (m_fpc in current_settings.modeswitches) then
  1588. property_dec
  1589. else
  1590. break;
  1591. end;
  1592. else
  1593. break;
  1594. end;
  1595. end;
  1596. end;
  1597. until false;
  1598. { check for incomplete class definitions, this is only required
  1599. for fpc modes }
  1600. if (m_fpc in current_settings.modeswitches) then
  1601. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  1602. end;
  1603. {****************************************************************************
  1604. SPECIALIZATION BODY GENERATION
  1605. ****************************************************************************}
  1606. procedure specialize_objectdefs(p:TObject;arg:pointer);
  1607. var
  1608. i : longint;
  1609. hp : tdef;
  1610. oldcurrent_filepos : tfileposinfo;
  1611. oldsymtablestack : tsymtablestack;
  1612. pu : tused_unit;
  1613. hmodule : tmodule;
  1614. specobj : tobjectdef;
  1615. begin
  1616. if not((tsym(p).typ=typesym) and
  1617. (ttypesym(p).typedef.typesym=tsym(p)) and
  1618. (ttypesym(p).typedef.typ=objectdef) and
  1619. (df_specialization in ttypesym(p).typedef.defoptions)
  1620. ) then
  1621. exit;
  1622. { Setup symtablestack a definition time }
  1623. specobj:=tobjectdef(ttypesym(p).typedef);
  1624. oldsymtablestack:=symtablestack;
  1625. symtablestack:=tsymtablestack.create;
  1626. if not assigned(tobjectdef(ttypesym(p).typedef).genericdef) then
  1627. internalerror(200705151);
  1628. hmodule:=find_module_from_symtable(specobj.genericdef.owner);
  1629. if hmodule=nil then
  1630. internalerror(200705152);
  1631. pu:=tused_unit(hmodule.used_units.first);
  1632. while assigned(pu) do
  1633. begin
  1634. if not assigned(pu.u.globalsymtable) then
  1635. internalerror(200705153);
  1636. symtablestack.push(pu.u.globalsymtable);
  1637. pu:=tused_unit(pu.next);
  1638. end;
  1639. if assigned(hmodule.globalsymtable) then
  1640. symtablestack.push(hmodule.globalsymtable);
  1641. if assigned(hmodule.localsymtable) then
  1642. symtablestack.push(hmodule.localsymtable);
  1643. { procedure definitions for classes or objects }
  1644. if is_class(specobj) or is_object(specobj) then
  1645. begin
  1646. for i:=0 to specobj.symtable.DefList.Count-1 do
  1647. begin
  1648. hp:=tdef(specobj.symtable.DefList[i]);
  1649. if hp.typ=procdef then
  1650. begin
  1651. if assigned(tprocdef(hp).genericdef) and
  1652. (tprocdef(hp).genericdef.typ=procdef) and
  1653. assigned(tprocdef(tprocdef(hp).genericdef).generictokenbuf) then
  1654. begin
  1655. oldcurrent_filepos:=current_filepos;
  1656. current_filepos:=tprocdef(tprocdef(hp).genericdef).fileinfo;
  1657. { use the index the module got from the current compilation process }
  1658. current_filepos.moduleindex:=hmodule.unit_index;
  1659. current_tokenpos:=current_filepos;
  1660. current_scanner.startreplaytokens(tprocdef(tprocdef(hp).genericdef).generictokenbuf);
  1661. read_proc_body(nil,tprocdef(hp));
  1662. current_filepos:=oldcurrent_filepos;
  1663. end
  1664. else
  1665. MessagePos1(tprocdef(hp).fileinfo,sym_e_forward_not_resolved,tprocdef(hp).fullprocname(false));
  1666. end;
  1667. end;
  1668. end;
  1669. { Restore symtablestack }
  1670. symtablestack.free;
  1671. symtablestack:=oldsymtablestack;
  1672. end;
  1673. procedure generate_specialization_procs;
  1674. begin
  1675. if assigned(current_module.globalsymtable) then
  1676. current_module.globalsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1677. if assigned(current_module.localsymtable) then
  1678. current_module.localsymtable.SymList.ForEachCall(@specialize_objectdefs,nil);
  1679. end;
  1680. end.