psub.pas 80 KB

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