pmodules.pas 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  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 pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses fmodule;
  21. function proc_unit(curr: tmodule):boolean;
  22. procedure proc_package(curr: tmodule);
  23. procedure proc_program(curr: tmodule; islibrary : boolean);
  24. implementation
  25. uses
  26. SysUtils,
  27. globtype,systems,tokens,
  28. cutils,cfileutl,cclasses,comphook,
  29. globals,verbose,finput,fppu,globstat,fpcp,fpkg,
  30. symconst,symbase,symtype,symdef,symsym,symtable,defutil,symcreat,
  31. wpoinfo,
  32. aasmtai,aasmdata,aasmbase,aasmcpu,
  33. cgbase,ngenutil,
  34. nbas,nutils,ncgutil,
  35. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  36. cresstr,procinfo,
  37. objcgutl,
  38. pkgutil,
  39. wpobase,
  40. scanner,pbase,pexpr,psystem,psub,pgenutil,pparautl,ncgvmt,ncgrtti,
  41. cpuinfo;
  42. procedure create_objectfile(curr : tmodule);
  43. var
  44. DLLScanner : TDLLScanner;
  45. s : string;
  46. KeepShared : TCmdStrList;
  47. begin
  48. { try to create import entries from system dlls }
  49. if (tf_has_dllscanner in target_info.flags) and
  50. (not curr.linkOtherSharedLibs.Empty) then
  51. begin
  52. { Init DLLScanner }
  53. if assigned(CDLLScanner[target_info.system]) then
  54. DLLScanner:=CDLLScanner[target_info.system].Create
  55. else
  56. internalerror(200104121);
  57. KeepShared:=TCmdStrList.Create;
  58. { Walk all shared libs }
  59. While not curr.linkOtherSharedLibs.Empty do
  60. begin
  61. S:=curr.linkOtherSharedLibs.Getusemask(link_always);
  62. if not DLLScanner.scan(s) then
  63. KeepShared.Concat(s);
  64. end;
  65. DLLscanner.Free;
  66. { Recreate import section }
  67. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  68. begin
  69. if assigned(current_asmdata.asmlists[al_imports]) then
  70. current_asmdata.asmlists[al_imports].clear
  71. else
  72. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  73. importlib.generatelib;
  74. end;
  75. { Readd the not processed files }
  76. while not KeepShared.Empty do
  77. begin
  78. s:=KeepShared.GetFirst;
  79. curr.linkOtherSharedLibs.add(s,link_always);
  80. end;
  81. KeepShared.Free;
  82. end;
  83. { allow a target-specific pass over all assembler code (used by LLVM
  84. to insert type definitions }
  85. cnodeutils.InsertObjectInfo;
  86. { Start and end module debuginfo, at least required for stabs
  87. to insert n_sourcefile lines }
  88. if (cs_debuginfo in current_settings.moduleswitches) or
  89. (cs_use_lineinfo in current_settings.globalswitches) then
  90. current_debuginfo.insertmoduleinfo;
  91. { create the .s file and assemble it }
  92. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  93. GenerateAsm(false);
  94. { Also create a smartlinked version ? }
  95. if create_smartlink_library then
  96. begin
  97. GenerateAsm(true);
  98. if (af_needar in target_asm.flags) then
  99. Linker.MakeStaticLibrary;
  100. end;
  101. { resource files }
  102. CompileResourceFiles;
  103. end;
  104. procedure insertobjectfile(curr : tmodule);
  105. { Insert the used object file for this unit in the used list for this unit }
  106. begin
  107. curr.linkunitofiles.add(curr.objfilename,link_static);
  108. curr.headerflags:=curr.headerflags or uf_static_linked;
  109. if create_smartlink_library then
  110. begin
  111. curr.linkunitstaticlibs.add(curr.staticlibfilename ,link_smart);
  112. curr.headerflags:=curr.headerflags or uf_smart_linked;
  113. end;
  114. if cs_lto in current_settings.moduleswitches then
  115. begin
  116. curr.linkunitofiles.add(ChangeFileExt(curr.objfilename,LTOExt),link_lto);
  117. curr.headerflags:=curr.headerflags or uf_lto_linked;
  118. end;
  119. end;
  120. procedure create_dwarf_frame;
  121. begin
  122. { Dwarf conflicts with smartlinking in separate .a files }
  123. if create_smartlink_library then
  124. exit;
  125. { Call frame information }
  126. { MWE: we write our own info, so dwarf asm support is not really needed }
  127. { if (af_supports_dwarf in target_asm.flags) and }
  128. { CFI is currently broken for Darwin }
  129. if not(target_info.system in systems_darwin) and
  130. (
  131. (tf_needs_dwarf_cfi in target_info.flags) or
  132. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  133. ) then
  134. begin
  135. current_asmdata.asmlists[al_dwarf_frame].Free;
  136. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  137. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  138. end;
  139. end;
  140. Function CheckResourcesUsed(curr : tmodule) : boolean;
  141. var
  142. hp : tused_unit;
  143. found : Boolean;
  144. begin
  145. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  146. if not CheckResourcesUsed then exit;
  147. hp:=tused_unit(usedunits.first);
  148. found:=mf_has_resourcefiles in curr.moduleflags;
  149. while Assigned(hp) and not found do
  150. begin
  151. found:=mf_has_resourcefiles in hp.u.moduleflags;
  152. hp:=tused_unit(hp.next);
  153. end;
  154. CheckResourcesUsed:=found;
  155. end;
  156. function AddUnit(curr : tmodule; const s:string;addasused:boolean): tppumodule;
  157. var
  158. hp : tppumodule;
  159. unitsym : tunitsym;
  160. begin
  161. { load unit }
  162. hp:=registerunit(curr,s,'');
  163. hp.loadppu;
  164. hp.adddependency(curr);
  165. { add to symtable stack }
  166. symtablestack.push(hp.globalsymtable);
  167. if (m_mac in current_settings.modeswitches) and
  168. assigned(hp.globalmacrosymtable) then
  169. macrosymtablestack.push(hp.globalmacrosymtable);
  170. { insert unitsym }
  171. unitsym:=cunitsym.create(hp.modulename^,hp);
  172. inc(unitsym.refs);
  173. tabstractunitsymtable(curr.localsymtable).insertunit(unitsym);
  174. if addasused then
  175. { add to used units }
  176. curr.addusedunit(hp,false,unitsym);
  177. result:=hp;
  178. end;
  179. function AddUnit(curr :tmodule; const s:string):tppumodule;
  180. begin
  181. result:=AddUnit(curr,s,true);
  182. end;
  183. procedure maybeloadvariantsunit(curr : tmodule);
  184. var
  185. hp : tmodule;
  186. addsystemnamespace : Boolean;
  187. begin
  188. { Do we need the variants unit? Skip this
  189. for VarUtils unit for bootstrapping }
  190. if not(mf_uses_variants in curr.moduleflags) or
  191. (curr.modulename^='VARUTILS') or
  192. (curr.modulename^='SYSTEM.VARUTILS') then
  193. exit;
  194. { Variants unit already loaded? }
  195. hp:=tmodule(loaded_units.first);
  196. while assigned(hp) do
  197. begin
  198. if (hp.modulename^='VARIANTS') or (hp.modulename^='SYSTEM.VARIANTS') then
  199. exit;
  200. hp:=tmodule(hp.next);
  201. end;
  202. { Variants unit is not loaded yet, load it now }
  203. Message(parser_w_implicit_uses_of_variants_unit);
  204. addsystemnamespace:=namespacelist.Find('System')=Nil;
  205. if addsystemnamespace then
  206. namespacelist.concat('System');
  207. AddUnit(curr,'variants');
  208. if addsystemnamespace then
  209. namespacelist.Remove('System');
  210. end;
  211. function MaybeRemoveResUnit(curr : tmodule) : boolean;
  212. var
  213. resources_used : boolean;
  214. hp : tmodule;
  215. uu : tused_unit;
  216. unitname : shortstring;
  217. begin
  218. { We simply remove the unit from:
  219. - usedunit list, so that things like init/finalization table won't
  220. contain references to this unit
  221. - loaded_units list, so that the unit object file doesn't get linked
  222. with the executable. }
  223. { Note: on windows we always need resources! }
  224. resources_used:=(target_info.system in systems_all_windows)
  225. or CheckResourcesUsed(curr);
  226. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  227. begin
  228. { resources aren't used, so we don't need this unit }
  229. if target_res.id=res_ext then
  230. unitname:='FPEXTRES'
  231. else
  232. unitname:='FPINTRES';
  233. Message1(unit_u_unload_resunit,unitname);
  234. { find the module }
  235. hp:=tmodule(loaded_units.first);
  236. while assigned(hp) do
  237. begin
  238. if hp.is_unit and (hp.modulename^=unitname) then break;
  239. hp:=tmodule(hp.next);
  240. end;
  241. if not assigned(hp) then
  242. internalerror(200801071);
  243. { find its tused_unit in the global list }
  244. uu:=tused_unit(usedunits.first);
  245. while assigned(uu) do
  246. begin
  247. if uu.u=hp then break;
  248. uu:=tused_unit(uu.next);
  249. end;
  250. if not assigned(uu) then
  251. internalerror(200801072);
  252. { remove the tused_unit }
  253. usedunits.Remove(uu);
  254. uu.Free;
  255. { remove the module }
  256. loaded_units.Remove(hp);
  257. unloaded_units.Concat(hp);
  258. end;
  259. MaybeRemoveResUnit:=resources_used;
  260. end;
  261. procedure loadsystemunit(curr : tmodule);
  262. begin
  263. { we are going to rebuild the symtablestack, clear it first }
  264. symtablestack.clear;
  265. macrosymtablestack.clear;
  266. { macro symtable }
  267. macrosymtablestack.push(initialmacrosymtable);
  268. { are we compiling the system unit? }
  269. if (cs_compilesystem in current_settings.moduleswitches) then
  270. begin
  271. systemunit:=tglobalsymtable(curr.localsymtable);
  272. { create system defines }
  273. create_intern_types;
  274. create_intern_symbols;
  275. { Set the owner of errorsym and errortype to symtable to
  276. prevent crashes when accessing .owner }
  277. generrorsym.owner:=systemunit;
  278. generrordef.owner:=systemunit;
  279. exit;
  280. end;
  281. { insert the system unit, it is allways the first. Load also the
  282. internal types from the system unit }
  283. AddUnit(curr,'system');
  284. systemunit:=tglobalsymtable(symtablestack.top);
  285. load_intern_types;
  286. { Set the owner of errorsym and errortype to symtable to
  287. prevent crashes when accessing .owner }
  288. generrorsym.owner:=systemunit;
  289. generrordef.owner:=systemunit;
  290. // Implicitly enable unicode strings in unicode RTL in modes objfpc/delphi.
  291. { TODO: Check if we should also do this for mode macpas }
  292. if not (cs_compilesystem in current_settings.moduleswitches) then
  293. if ([m_objfpc,m_delphi] * current_settings.modeswitches)<>[] then
  294. if is_systemunit_unicode then
  295. Include(current_settings.modeswitches,m_default_unicodestring)
  296. end;
  297. procedure loaddefaultunits(curr :tmodule);
  298. begin
  299. { Units only required for main module }
  300. if not(curr.is_unit) then
  301. begin
  302. { Heaptrc unit, load heaptrace before any other units especially objpas }
  303. if (cs_use_heaptrc in current_settings.globalswitches) then
  304. AddUnit(curr,'heaptrc');
  305. { Valgrind requires c memory manager }
  306. if (cs_gdb_valgrind in current_settings.globalswitches) or
  307. (([cs_sanitize_address]*current_settings.moduleswitches)<>[]) then
  308. AddUnit(curr,'cmem');
  309. { Lineinfo unit }
  310. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  311. case target_dbg.id of
  312. dbg_stabs:
  313. AddUnit(curr,'lineinfo');
  314. dbg_stabx:
  315. AddUnit(curr,'lnfogdb');
  316. else
  317. AddUnit(curr,'lnfodwrf');
  318. end;
  319. end;
  320. {$ifdef cpufpemu}
  321. { Floating point emulation unit?
  322. softfpu must be in the system unit anyways (FK)
  323. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  324. AddUnit('softfpu');
  325. }
  326. {$endif cpufpemu}
  327. { Which kind of resource support?
  328. Note: if resources aren't used this unit will be removed later,
  329. otherwise we need it here since it must be loaded quite early }
  330. if (tf_has_winlike_resources in target_info.flags) then
  331. if target_res.id=res_ext then
  332. AddUnit(curr,'fpextres')
  333. else
  334. AddUnit(curr,'fpintres');
  335. end
  336. else if (cs_checkpointer in current_settings.localswitches) then
  337. AddUnit(curr,'heaptrc');
  338. { Objpas unit? }
  339. if m_objpas in current_settings.modeswitches then
  340. AddUnit(curr,'objpas');
  341. { Macpas unit? }
  342. if m_mac in current_settings.modeswitches then
  343. AddUnit(curr,'macpas');
  344. if m_iso in current_settings.modeswitches then
  345. AddUnit(curr,'iso7185');
  346. if m_extpas in current_settings.modeswitches then
  347. begin
  348. { basic procedures for Extended Pascal are for now provided by the iso unit }
  349. AddUnit(curr,'iso7185');
  350. AddUnit(curr,'extpas');
  351. end;
  352. { blocks support? }
  353. if m_blocks in current_settings.modeswitches then
  354. AddUnit(curr,'blockrtl');
  355. { Determine char size. }
  356. // Ansi RTL ?
  357. if not is_systemunit_unicode then
  358. begin
  359. if m_default_unicodestring in current_settings.modeswitches then
  360. AddUnit(curr,'uuchar'); // redefines char as widechar
  361. end
  362. else
  363. begin
  364. // Unicode RTL
  365. if not (m_default_ansistring in current_settings.modeswitches) then
  366. if not (curr.modulename^<>'UACHAR') then
  367. AddUnit(curr,'uachar'); // redefines char as ansichar
  368. end;
  369. { Objective-C support unit? }
  370. if (m_objectivec1 in current_settings.modeswitches) then
  371. begin
  372. { interface to Objective-C run time }
  373. AddUnit(curr,'objc');
  374. loadobjctypes;
  375. { NSObject }
  376. if not(curr.is_unit) or
  377. (curr.modulename^<>'OBJCBASE') then
  378. AddUnit(curr,'objcbase');
  379. end;
  380. { Profile unit? Needed for go32v2 only }
  381. if (cs_profile in current_settings.moduleswitches) and
  382. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  383. AddUnit(curr,'profile');
  384. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  385. begin
  386. AddUnit(curr,'fpcylix');
  387. AddUnit(curr,'dynlibs');
  388. end;
  389. {$push}
  390. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  391. { CPU targets with microcontroller support can add a controller specific unit }
  392. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  393. (current_settings.controllertype<>ct_none) and
  394. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') and
  395. (embedded_controllers[current_settings.controllertype].controllerunitstr<>curr.modulename^) then
  396. AddUnit(curr,embedded_controllers[current_settings.controllertype].controllerunitstr);
  397. {$pop}
  398. {$ifdef XTENSA}
  399. if not(curr.is_unit) and (target_info.system=system_xtensa_freertos) then
  400. if (current_settings.controllertype=ct_esp32) then
  401. begin
  402. if (idf_version>=40100) and (idf_version<40200) then
  403. AddUnit(curr,'espidf_40100')
  404. else if (curr,idf_version>=40200) and (idf_version<40400) then
  405. AddUnit(curr,'espidf_40200')
  406. else if idf_version>=40400 then
  407. AddUnit(curr,'espidf_40400')
  408. else
  409. Comment(V_Warning, 'Unsupported esp-idf version');
  410. end
  411. else if (current_settings.controllertype=ct_esp8266) then
  412. begin
  413. if (idf_version>=30300) and (idf_version<30400) then
  414. AddUnit(curr,'esp8266rtos_30300')
  415. else if idf_version>=30400 then
  416. AddUnit(curr,'esp8266rtos_30400')
  417. else
  418. Comment(V_Warning, 'Unsupported esp-rtos version');
  419. end;
  420. {$endif XTENSA}
  421. end;
  422. procedure loadautounits(curr: tmodule);
  423. var
  424. hs,s : string;
  425. begin
  426. hs:=autoloadunits;
  427. repeat
  428. s:=GetToken(hs,',');
  429. if s='' then
  430. break;
  431. AddUnit(curr,s);
  432. until false;
  433. end;
  434. procedure parseusesclause(curr: tmodule);
  435. var
  436. s,sorg : ansistring;
  437. fn : string;
  438. pu : tused_unit;
  439. hp2 : tmodule;
  440. unitsym : tunitsym;
  441. filepos : tfileposinfo;
  442. begin
  443. consume(_USES);
  444. repeat
  445. s:=pattern;
  446. sorg:=orgpattern;
  447. filepos:=current_tokenpos;
  448. consume(_ID);
  449. while token=_POINT do
  450. begin
  451. consume(_POINT);
  452. s:=s+'.'+pattern;
  453. sorg:=sorg+'.'+orgpattern;
  454. consume(_ID);
  455. end;
  456. { support "<unit> in '<file>'" construct, but not for tp7 }
  457. fn:='';
  458. if not(m_tp7 in current_settings.modeswitches) and
  459. try_to_consume(_OP_IN) then
  460. fn:=FixFileName(get_stringconst);
  461. { Give a warning if lineinfo is loaded }
  462. if s='LINEINFO' then
  463. begin
  464. Message(parser_w_no_lineinfo_use_switch);
  465. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  466. s := 'LNFODWRF';
  467. sorg := s;
  468. end;
  469. { Give a warning if objpas is loaded }
  470. if s='OBJPAS' then
  471. Message(parser_w_no_objpas_use_mode);
  472. { Using the unit itself is not possible }
  473. if (s<>curr.modulename^) then
  474. begin
  475. { check if the unit is already used }
  476. hp2:=nil;
  477. pu:=tused_unit(curr.used_units.first);
  478. while assigned(pu) do
  479. begin
  480. if (pu.u.modulename^=s) then
  481. begin
  482. hp2:=pu.u;
  483. break;
  484. end;
  485. pu:=tused_unit(pu.next);
  486. end;
  487. if not assigned(hp2) then
  488. hp2:=registerunit(curr,sorg,fn)
  489. else
  490. Message1(sym_e_duplicate_id,s);
  491. { Create unitsym, we need to use the name as specified, we
  492. can not use the modulename because that can be different
  493. when -Un is used }
  494. current_tokenpos:=filepos;
  495. unitsym:=cunitsym.create(sorg,nil);
  496. { the current module uses the unit hp2 }
  497. curr.addusedunit(hp2,true,unitsym);
  498. end
  499. else
  500. Message1(sym_e_duplicate_id,s);
  501. if token=_COMMA then
  502. begin
  503. pattern:='';
  504. consume(_COMMA);
  505. end
  506. else
  507. break;
  508. until false;
  509. end;
  510. procedure loadunits(curr: tmodule; preservest:tsymtable);
  511. var
  512. s,sorg : ansistring;
  513. pu,pu2 : tused_unit;
  514. hp2 : tmodule;
  515. begin
  516. parseusesclause(curr);
  517. { Load the units }
  518. pu:=tused_unit(curr.used_units.first);
  519. while assigned(pu) do
  520. begin
  521. { Only load the units that are in the current
  522. (interface/implementation) uses clause }
  523. if pu.in_uses and
  524. (pu.in_interface=curr.in_interface) then
  525. begin
  526. tppumodule(pu.u).loadppu;
  527. { is our module compiled? then we can stop }
  528. if curr.state=ms_compiled then
  529. exit;
  530. { add this unit to the dependencies }
  531. pu.u.adddependency(curr);
  532. { save crc values }
  533. pu.checksum:=pu.u.crc;
  534. pu.interface_checksum:=pu.u.interface_crc;
  535. pu.indirect_checksum:=pu.u.indirect_crc;
  536. if tppumodule(pu.u).nsprefix<>'' then
  537. begin
  538. { use the name as declared in the uses section for -Un }
  539. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  540. s:=upper(sorg);
  541. { check whether the module was already loaded }
  542. hp2:=nil;
  543. pu2:=tused_unit(curr.used_units.first);
  544. while assigned(pu2) and (pu2<>pu) do
  545. begin
  546. if (pu2.u.modulename^=s) then
  547. begin
  548. hp2:=pu.u;
  549. break;
  550. end;
  551. pu2:=tused_unit(pu2.next);
  552. end;
  553. if assigned(hp2) then
  554. begin
  555. MessagePos1(pu.unitsym.fileinfo,sym_e_duplicate_id,s);
  556. pu:=tused_unit(pu.next);
  557. continue;
  558. end;
  559. { update unitsym now that we have access to the full name }
  560. pu.unitsym.free;
  561. pu.unitsym:=cunitsym.create(sorg,pu.u);
  562. end
  563. else
  564. begin
  565. { connect unitsym to the module }
  566. pu.unitsym.module:=pu.u;
  567. pu.unitsym.register_sym;
  568. end;
  569. tabstractunitsymtable(curr.localsymtable).insertunit(pu.unitsym);
  570. { add to symtable stack }
  571. if assigned(preservest) then
  572. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  573. else
  574. symtablestack.push(pu.u.globalsymtable);
  575. if (m_mac in current_settings.modeswitches) and
  576. assigned(pu.u.globalmacrosymtable) then
  577. macrosymtablestack.push(pu.u.globalmacrosymtable);
  578. { check hints }
  579. pu.check_hints;
  580. end;
  581. pu:=tused_unit(pu.next);
  582. end;
  583. end;
  584. procedure reset_all_defs(curr: tmodule);
  585. begin
  586. if assigned(curr.wpoinfo) then
  587. curr.wpoinfo.resetdefs;
  588. end;
  589. procedure free_localsymtables(st:TSymtable);
  590. var
  591. i : longint;
  592. def : tstoreddef;
  593. pd : tprocdef;
  594. begin
  595. for i:=0 to st.DefList.Count-1 do
  596. begin
  597. def:=tstoreddef(st.DefList[i]);
  598. if def.typ=procdef then
  599. begin
  600. pd:=tprocdef(def);
  601. if assigned(pd.localst) and
  602. (pd.localst.symtabletype<>staticsymtable) and
  603. not(po_inline in pd.procoptions) then
  604. begin
  605. free_localsymtables(pd.localst);
  606. pd.localst.free;
  607. pd.localst:=nil;
  608. end;
  609. pd.freeimplprocdefinfo;
  610. pd.done_paraloc_info(calleeside);
  611. end;
  612. end;
  613. end;
  614. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  615. var
  616. i: longint;
  617. def: tdef;
  618. sym: tsym;
  619. begin
  620. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  621. begin
  622. def:=tdef(curr.localsymtable.deflist[i]);
  623. { since commit 48986 deflist might have NIL entries }
  624. if not assigned(def) then
  625. continue;
  626. { this also frees def, as the defs are owned by the symtable }
  627. if not def.is_registered and
  628. not(df_not_registered_no_free in def.defoptions) then
  629. begin
  630. { if it's a procdef, unregister it from its procsym first,
  631. unless that sym hasn't been registered either (it's possible
  632. to have one overload in the interface and another in the
  633. implementation) }
  634. if (def.typ=procdef) and
  635. tprocdef(def).procsym.is_registered then
  636. tprocsym(tprocdef(def).procsym).ProcdefList.Remove(def);
  637. curr.localsymtable.deletedef(def);
  638. end;
  639. end;
  640. { from high to low so we hopefully have moves of less data }
  641. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  642. begin
  643. sym:=tsym(curr.localsymtable.symlist[i]);
  644. { this also frees sym, as the symbols are owned by the symtable }
  645. if not sym.is_registered then
  646. curr.localsymtable.DeleteSym(sym);
  647. end;
  648. end;
  649. procedure setupglobalswitches;
  650. begin
  651. if (cs_create_pic in current_settings.moduleswitches) then
  652. begin
  653. def_system_macro('FPC_PIC');
  654. def_system_macro('PIC');
  655. end;
  656. end;
  657. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  658. var
  659. ps : tprocsym;
  660. pd : tprocdef;
  661. begin
  662. { there should be no current_procinfo available }
  663. if assigned(current_procinfo) then
  664. internalerror(200304275);
  665. {Generate a procsym for main}
  666. ps:=cprocsym.create('$'+name);
  667. { always register the symbol }
  668. ps.register_sym;
  669. { main are allways used }
  670. inc(ps.refs);
  671. st.insertsym(ps);
  672. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  673. { We don't need a local symtable, change it into the static symtable }
  674. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  675. begin
  676. pd.localst.free;
  677. pd.localst:=st;
  678. end
  679. else if (potype=potype_pkgstub) and
  680. (target_info.system in systems_all_windows+systems_nativent) then
  681. pd.proccalloption:=pocall_stdcall
  682. else
  683. pd.proccalloption:=pocall_cdecl;
  684. handle_calling_convention(pd,hcc_default_actions_impl);
  685. { set procinfo and current_procinfo.procdef }
  686. result:=tcgprocinfo(cprocinfo.create(nil));
  687. result.procdef:=pd;
  688. { main proc does always a call e.g. to init system unit }
  689. if potype<>potype_pkgstub then
  690. include(result.flags,pi_do_call);
  691. end;
  692. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  693. begin
  694. { remove localst as it was replaced by staticsymtable }
  695. pi.procdef.localst:=nil;
  696. { remove procinfo }
  697. curr.procinfo:=nil;
  698. pi.free;
  699. pi:=nil;
  700. end;
  701. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  702. procedure maybe_load_got;
  703. {$if defined(i386) or defined (sparcgen)}
  704. var
  705. gotvarsym : tstaticvarsym;
  706. {$endif i386 or sparcgen}
  707. begin
  708. {$if defined(i386) or defined(sparcgen)}
  709. if (cs_create_pic in current_settings.moduleswitches) and
  710. (tf_pic_uses_got in target_info.flags) then
  711. begin
  712. { insert symbol for got access in assembler code}
  713. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  714. vs_value,voidpointertype,[vo_is_external]);
  715. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  716. curr.localsymtable.insertsym(gotvarsym);
  717. { avoid unnecessary warnings }
  718. gotvarsym.varstate:=vs_read;
  719. gotvarsym.refs:=1;
  720. end;
  721. {$endif i386 or sparcgen}
  722. end;
  723. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  724. begin
  725. { create procdef }
  726. case flag of
  727. mf_init :
  728. begin
  729. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  730. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  731. end;
  732. mf_finalize :
  733. begin
  734. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  735. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  736. if (not curr.is_unit) then
  737. result.procdef.aliasnames.concat('PASCALFINALIZE');
  738. end;
  739. else
  740. internalerror(200304253);
  741. end;
  742. result.code:=cnothingnode.create;
  743. end;
  744. procedure copy_macro(p:TObject; arg:pointer);
  745. begin
  746. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  747. end;
  748. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  749. var
  750. deprecated_seen,
  751. last_is_deprecated:boolean;
  752. begin
  753. try_consume_hintdirective:=false;
  754. deprecated_seen:=false;
  755. repeat
  756. last_is_deprecated:=false;
  757. case idtoken of
  758. _LIBRARY :
  759. begin
  760. include(moduleopt,mo_hint_library);
  761. try_consume_hintdirective:=true;
  762. end;
  763. _DEPRECATED :
  764. begin
  765. { allow deprecated only once }
  766. if deprecated_seen then
  767. break;
  768. include(moduleopt,mo_hint_deprecated);
  769. try_consume_hintdirective:=true;
  770. last_is_deprecated:=true;
  771. deprecated_seen:=true;
  772. end;
  773. _EXPERIMENTAL :
  774. begin
  775. include(moduleopt,mo_hint_experimental);
  776. try_consume_hintdirective:=true;
  777. end;
  778. _PLATFORM :
  779. begin
  780. include(moduleopt,mo_hint_platform);
  781. try_consume_hintdirective:=true;
  782. end;
  783. _UNIMPLEMENTED :
  784. begin
  785. include(moduleopt,mo_hint_unimplemented);
  786. try_consume_hintdirective:=true;
  787. end;
  788. else
  789. break;
  790. end;
  791. consume(Token);
  792. { handle deprecated message }
  793. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  794. begin
  795. if deprecatedmsg<>nil then
  796. internalerror(201001221);
  797. if token=_CSTRING then
  798. deprecatedmsg:=stringdup(cstringpattern)
  799. else
  800. deprecatedmsg:=stringdup(pattern);
  801. consume(token);
  802. include(moduleopt,mo_has_deprecated_msg);
  803. end;
  804. until false;
  805. end;
  806. {$ifdef jvm}
  807. procedure addmoduleclass;
  808. var
  809. def: tobjectdef;
  810. typesym: ttypesym;
  811. begin
  812. { java_jlobject may not have been parsed yet (system unit); in any
  813. case, we only use this to refer to the class type, so inheritance
  814. does not matter }
  815. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  816. include(def.objectoptions,oo_is_external);
  817. include(def.objectoptions,oo_is_sealed);
  818. def.objextname:=stringdup(curr.realmodulename^);
  819. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  820. symtablestack.top.insertsym(typesym);
  821. end;
  822. {$endif jvm}
  823. type
  824. tfinishstate=record
  825. init_procinfo:tcgprocinfo;
  826. finalize_procinfo:tcgprocinfo;
  827. end;
  828. pfinishstate=^tfinishstate;
  829. procedure finish_unit(module:tmodule;immediate:boolean);forward;
  830. function proc_unit_implementation(curr: tmodule):boolean;
  831. var
  832. init_procinfo,
  833. finalize_procinfo : tcgprocinfo;
  834. i,j : integer;
  835. finishstate:pfinishstate;
  836. globalstate:pglobalstate;
  837. begin
  838. result:=true;
  839. init_procinfo:=nil;
  840. finalize_procinfo:=nil;
  841. finishstate:=nil;
  842. globalstate:=nil;
  843. { All units are read, now give them a number }
  844. curr.updatemaps;
  845. { further, changing the globalsymtable is not allowed anymore }
  846. curr.globalsymtable.sealed:=true;
  847. symtablestack.push(curr.localsymtable);
  848. if not curr.interface_only then
  849. begin
  850. Message1(parser_u_parsing_implementation,curr.modulename^);
  851. if curr.in_interface then
  852. internalerror(200212285);
  853. { Compile the unit }
  854. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  855. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  856. init_procinfo.parse_body;
  857. { save file pos for debuginfo }
  858. curr.mainfilepos:=init_procinfo.entrypos;
  859. { parse finalization section }
  860. if token=_FINALIZATION then
  861. begin
  862. { Compile the finalize }
  863. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  864. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  865. finalize_procinfo.parse_body;
  866. end
  867. end;
  868. { remove all units that we are waiting for that are already waiting for
  869. us => breaking up circles }
  870. for i:=0 to curr.waitingunits.count-1 do
  871. for j:=curr.waitingforunit.count-1 downto 0 do
  872. if curr.waitingunits[i]=curr.waitingforunit[j] then
  873. curr.waitingforunit.delete(j);
  874. {$ifdef DEBUG_UNITWAITING}
  875. Writeln('Units waiting for ', curr.modulename^, ': ',
  876. curr.waitingforunit.Count);
  877. {$endif}
  878. result:=curr.waitingforunit.count=0;
  879. { save all information that is needed for finishing the unit }
  880. New(finishstate);
  881. finishstate^.init_procinfo:=init_procinfo;
  882. finishstate^.finalize_procinfo:=finalize_procinfo;
  883. curr.finishstate:=finishstate;
  884. if result then
  885. finish_unit(curr,true)
  886. else
  887. begin
  888. { save the current state, so the parsing can continue where we left
  889. of here }
  890. New(globalstate);
  891. save_global_state(globalstate^,true);
  892. curr.globalstate:=globalstate;
  893. end;
  894. end;
  895. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  896. begin
  897. { create whole program optimisation information (may already be
  898. updated in the interface, e.g., in case of classrefdef typed
  899. constants }
  900. curr.wpoinfo:=tunitwpoinfo.create;
  901. { ... parse the declarations }
  902. Message1(parser_u_parsing_interface,curr.realmodulename^);
  903. symtablestack.push(curr.globalsymtable);
  904. {$ifdef jvm}
  905. { fake classdef to represent the class corresponding to the unit }
  906. addmoduleclass;
  907. {$endif}
  908. read_interface_declarations;
  909. { Export macros defined in the interface for macpas. The macros
  910. are put in the globalmacrosymtable that will only be used by other
  911. units. The current unit continues to use the localmacrosymtable }
  912. if (m_mac in current_settings.modeswitches) then
  913. begin
  914. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  915. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  916. end;
  917. { leave when we got an error }
  918. if (Errorcount>0) and not status.skip_error then
  919. begin
  920. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  921. status.skip_error:=true;
  922. symtablestack.pop(curr.globalsymtable);
  923. {$ifdef DEBUG_NODE_XML}
  924. XMLFinalizeNodeFile('unit');
  925. {$endif DEBUG_NODE_XML}
  926. exit;
  927. end;
  928. { Our interface is compiled, generate CRC and switch to implementation }
  929. if not(cs_compilesystem in current_settings.moduleswitches) and
  930. (Errorcount=0) then
  931. tppumodule(curr).getppucrc;
  932. curr.in_interface:=false;
  933. curr.interface_compiled:=true;
  934. { First reload all units depending on our interface, we need to do this
  935. in the implementation part to prevent erroneous circular references }
  936. tppumodule(curr).setdefgeneration;
  937. tppumodule(curr).reload_flagged_units;
  938. { Parse the implementation section }
  939. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  940. curr.interface_only:=true
  941. else
  942. curr.interface_only:=false;
  943. parse_only:=false;
  944. { create static symbol table }
  945. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  946. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  947. maybe_load_got;
  948. if not curr.interface_only then
  949. begin
  950. consume(_IMPLEMENTATION);
  951. Message1(unit_u_loading_implementation_units,curr.modulename^);
  952. { Read the implementation units }
  953. if token=_USES then
  954. begin
  955. loadunits(curr,curr.globalsymtable);
  956. consume(_SEMICOLON);
  957. end;
  958. end;
  959. if curr.state=ms_compiled then
  960. begin
  961. symtablestack.pop(curr.globalsymtable);
  962. exit;
  963. end;
  964. result:=proc_unit_implementation(curr);
  965. end;
  966. function proc_unit(curr: tmodule):boolean;
  967. var
  968. main_file: tinputfile;
  969. s1,s2 : ^string; {Saves stack space}
  970. unitname : ansistring;
  971. unitname8 : string[8];
  972. consume_semicolon_after_uses:boolean;
  973. feature : tfeature;
  974. begin
  975. result:=true;
  976. if m_mac in current_settings.modeswitches then
  977. curr.mode_switch_allowed:= false;
  978. consume(_UNIT);
  979. if compile_level=1 then
  980. Status.IsExe:=false;
  981. unitname:=orgpattern;
  982. consume(_ID);
  983. while token=_POINT do
  984. begin
  985. consume(_POINT);
  986. unitname:=unitname+'.'+orgpattern;
  987. consume(_ID);
  988. end;
  989. { create filenames and unit name }
  990. main_file := current_scanner.inputfile;
  991. while assigned(main_file.next) do
  992. main_file := main_file.next;
  993. new(s1);
  994. s1^:=curr.modulename^;
  995. curr.SetFileName(main_file.path+main_file.name,true);
  996. curr.SetModuleName(unitname);
  997. {$ifdef DEBUG_NODE_XML}
  998. XMLInitializeNodeFile('unit', unitname);
  999. {$endif DEBUG_NODE_XML}
  1000. { check for system unit }
  1001. new(s2);
  1002. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1003. unitname8:=copy(curr.modulename^,1,8);
  1004. if (cs_check_unit_name in current_settings.globalswitches) and
  1005. (
  1006. not(
  1007. (curr.modulename^=s2^) or
  1008. (
  1009. (length(curr.modulename^)>8) and
  1010. (unitname8=s2^)
  1011. )
  1012. )
  1013. or
  1014. (
  1015. (length(s1^)>8) and
  1016. (s1^<>curr.modulename^)
  1017. )
  1018. ) then
  1019. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1020. if (curr.modulename^='SYSTEM') then
  1021. include(current_settings.moduleswitches,cs_compilesystem);
  1022. dispose(s2);
  1023. dispose(s1);
  1024. if (target_info.system in systems_unit_program_exports) then
  1025. exportlib.preparelib(curr.realmodulename^);
  1026. { parse hint directives }
  1027. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1028. consume(_SEMICOLON);
  1029. { handle the global switches, do this before interface, because after interface has been
  1030. read, all following directives are parsed as well }
  1031. setupglobalswitches;
  1032. { generate now the global symboltable,
  1033. define first as local to overcome dependency conflicts }
  1034. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1035. { insert unitsym of this unit to prevent other units having
  1036. the same name }
  1037. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1038. { load default system unit, it must be loaded before interface is parsed
  1039. else we cannot use e.g. feature switches before the next real token }
  1040. loadsystemunit(curr);
  1041. { system unit is loaded, now insert feature defines }
  1042. for feature:=low(tfeature) to high(tfeature) do
  1043. if feature in features then
  1044. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1045. consume(_INTERFACE);
  1046. { global switches are read, so further changes aren't allowed }
  1047. curr.in_global:=false;
  1048. message1(unit_u_loading_interface_units,curr.modulename^);
  1049. { update status }
  1050. status.currentmodule:=curr.realmodulename^;
  1051. { maybe turn off m_objpas if we are compiling objpas }
  1052. if (curr.modulename^='OBJPAS') then
  1053. exclude(current_settings.modeswitches,m_objpas);
  1054. { maybe turn off m_mac if we are compiling macpas }
  1055. if (curr.modulename^='MACPAS') then
  1056. exclude(current_settings.modeswitches,m_mac);
  1057. parse_only:=true;
  1058. { load default units, like language mode units }
  1059. if not(cs_compilesystem in current_settings.moduleswitches) then
  1060. loaddefaultunits(curr);
  1061. { insert qualifier for the system unit (allows system.writeln) }
  1062. if not(cs_compilesystem in current_settings.moduleswitches) and
  1063. (token=_USES) then
  1064. begin
  1065. // We do this as late as possible.
  1066. if Assigned(curr) then
  1067. curr.Loadlocalnamespacelist
  1068. else
  1069. current_namespacelist:=Nil;
  1070. loadunits(curr, nil);
  1071. { has it been compiled at a higher level ?}
  1072. if curr.state=ms_compiled then
  1073. begin
  1074. Message1(parser_u_already_compiled,curr.realmodulename^);
  1075. exit;
  1076. end;
  1077. consume_semicolon_after_uses:=true;
  1078. end
  1079. else
  1080. consume_semicolon_after_uses:=false;
  1081. { move the global symtable from the temporary local to global }
  1082. curr.globalsymtable:=curr.localsymtable;
  1083. curr.localsymtable:=nil;
  1084. { number all units, so we know if a unit is used by this unit or
  1085. needs to be added implicitly }
  1086. curr.updatemaps;
  1087. { consume the semicolon after maps have been updated else conditional compiling expressions
  1088. might cause internal errors, see tw8611 }
  1089. if consume_semicolon_after_uses then
  1090. consume(_SEMICOLON);
  1091. result:=parse_unit_interface_declarations(curr);
  1092. end;
  1093. procedure finish_unit(module:tmodule;immediate:boolean);
  1094. function is_assembler_generated:boolean;
  1095. var
  1096. hal : tasmlisttype;
  1097. begin
  1098. result:=false;
  1099. if Errorcount=0 then
  1100. begin
  1101. for hal:=low(TasmlistType) to high(TasmlistType) do
  1102. if not current_asmdata.asmlists[hal].empty then
  1103. begin
  1104. result:=true;
  1105. exit;
  1106. end;
  1107. end;
  1108. end;
  1109. procedure module_is_done(curr: tmodule);inline;
  1110. begin
  1111. dispose(pglobalstate(curr.globalstate));
  1112. curr.globalstate:=nil;
  1113. dispose(pfinishstate(curr.finishstate));
  1114. curr.finishstate:=nil;
  1115. end;
  1116. var
  1117. {$ifdef EXTDEBUG}
  1118. store_crc,
  1119. {$endif EXTDEBUG}
  1120. store_interface_crc,
  1121. store_indirect_crc: cardinal;
  1122. force_init_final : boolean;
  1123. init_procinfo,
  1124. finalize_procinfo : tcgprocinfo;
  1125. i : longint;
  1126. ag : boolean;
  1127. finishstate : tfinishstate;
  1128. globalstate : tglobalstate;
  1129. waitingmodule : tmodule;
  1130. begin
  1131. globalstate:=default(tglobalstate);
  1132. if not immediate then
  1133. begin
  1134. {$ifdef DEBUG_UNITWAITING}
  1135. writeln('finishing waiting unit ''', module.modulename^, '''');
  1136. {$endif DEBUG_UNITWAITING}
  1137. { restore the state when we stopped working on the unit }
  1138. save_global_state(globalstate,true);
  1139. if not assigned(module.globalstate) then
  1140. internalerror(2012091802);
  1141. restore_global_state(pglobalstate(module.globalstate)^,true);
  1142. end;
  1143. { curr is now module }
  1144. if not assigned(module.finishstate) then
  1145. internalerror(2012091801);
  1146. finishstate:=pfinishstate(module.finishstate)^;
  1147. finalize_procinfo:=finishstate.finalize_procinfo;
  1148. init_procinfo:=finishstate.init_procinfo;
  1149. { Generate specializations of objectdefs methods }
  1150. generate_specialization_procs;
  1151. // This needs to be done before we generate the VMTs
  1152. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1153. begin
  1154. add_synthetic_interface_classes_for_st(module.globalsymtable);
  1155. add_synthetic_interface_classes_for_st(module.localsymtable);
  1156. end;
  1157. { generate construction functions for all attributes in the unit:
  1158. this must be done before writing the VMTs because
  1159. during VMT writing the extended field info is written }
  1160. generate_attr_constrs(current_module.used_rtti_attrs);
  1161. { Generate VMTs }
  1162. if Errorcount=0 then
  1163. begin
  1164. write_vmts(module.globalsymtable,true);
  1165. write_vmts(module.localsymtable,false);
  1166. end;
  1167. { add implementations for synthetic method declarations added by
  1168. the compiler }
  1169. add_synthetic_method_implementations(module.globalsymtable);
  1170. add_synthetic_method_implementations(module.localsymtable);
  1171. { if the unit contains ansi/widestrings, initialization and
  1172. finalization code must be forced }
  1173. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1174. tstaticsymtable(module.localsymtable).needs_init_final;
  1175. { should we force unit initialization? }
  1176. { this is a hack, but how can it be done better ? }
  1177. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1178. is it needed at all? (Sergei) }
  1179. { it's needed in case cnodeutils.force_init = true }
  1180. if (force_init_final or cnodeutils.force_init) and
  1181. (
  1182. not assigned(init_procinfo) or
  1183. has_no_code(init_procinfo.code)
  1184. ) then
  1185. begin
  1186. { first release the not used init procinfo }
  1187. if assigned(init_procinfo) then
  1188. begin
  1189. release_proc_symbol(init_procinfo.procdef);
  1190. release_main_proc(module,init_procinfo);
  1191. end;
  1192. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1193. end;
  1194. if (force_init_final or cnodeutils.force_final) and
  1195. (
  1196. not assigned(finalize_procinfo) or
  1197. has_no_code(finalize_procinfo.code)
  1198. ) then
  1199. begin
  1200. { first release the not used finalize procinfo }
  1201. if assigned(finalize_procinfo) then
  1202. begin
  1203. release_proc_symbol(finalize_procinfo.procdef);
  1204. release_main_proc(module,finalize_procinfo);
  1205. end;
  1206. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1207. end;
  1208. { Now both init and finalize bodies are read and it is known
  1209. which variables are used in both init and finalize we can now
  1210. generate the code. This is required to prevent putting a variable in
  1211. a register that is also used in the finalize body (PFV) }
  1212. if assigned(init_procinfo) then
  1213. begin
  1214. if (force_init_final or cnodeutils.force_init) or
  1215. not(has_no_code(init_procinfo.code)) then
  1216. begin
  1217. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1218. init_procinfo.generate_code_tree;
  1219. include(module.moduleflags,mf_init);
  1220. end
  1221. else
  1222. release_proc_symbol(init_procinfo.procdef);
  1223. init_procinfo.resetprocdef;
  1224. release_main_proc(module,init_procinfo);
  1225. end;
  1226. if assigned(finalize_procinfo) then
  1227. begin
  1228. if force_init_final or
  1229. cnodeutils.force_init or
  1230. not(has_no_code(finalize_procinfo.code)) then
  1231. begin
  1232. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1233. finalize_procinfo.generate_code_tree;
  1234. include(module.moduleflags,mf_finalize);
  1235. end
  1236. else
  1237. release_proc_symbol(finalize_procinfo.procdef);
  1238. finalize_procinfo.resetprocdef;
  1239. release_main_proc(module,finalize_procinfo);
  1240. end;
  1241. symtablestack.pop(module.localsymtable);
  1242. symtablestack.pop(module.globalsymtable);
  1243. { the last char should always be a point }
  1244. consume(_POINT);
  1245. { reset wpo flags for all defs }
  1246. reset_all_defs(module);
  1247. if (Errorcount=0) then
  1248. begin
  1249. { tests, if all (interface) forwards are resolved }
  1250. tstoredsymtable(module.globalsymtable).check_forwards;
  1251. { check if all private fields are used }
  1252. tstoredsymtable(module.globalsymtable).allprivatesused;
  1253. { test static symtable }
  1254. tstoredsymtable(module.localsymtable).allsymbolsused;
  1255. tstoredsymtable(module.localsymtable).allprivatesused;
  1256. tstoredsymtable(module.localsymtable).check_forwards;
  1257. tstoredsymtable(module.localsymtable).checklabels;
  1258. { used units }
  1259. module.allunitsused;
  1260. end;
  1261. { leave when we got an error }
  1262. if (Errorcount>0) and not status.skip_error then
  1263. begin
  1264. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1265. status.skip_error:=true;
  1266. module_is_done(module);
  1267. if not immediate then
  1268. restore_global_state(globalstate,true);
  1269. {$ifdef DEBUG_NODE_XML}
  1270. XMLFinalizeNodeFile('unit');
  1271. {$endif DEBUG_NODE_XML}
  1272. exit;
  1273. end;
  1274. { if an Objective-C module, generate rtti and module info }
  1275. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1276. { do we need to add the variants unit? }
  1277. maybeloadvariantsunit(module);
  1278. { generate rtti/init tables }
  1279. write_persistent_type_info(module.globalsymtable,true);
  1280. write_persistent_type_info(module.localsymtable,false);
  1281. { Tables }
  1282. cnodeutils.InsertThreadvars;
  1283. { Resource strings }
  1284. GenerateResourceStrings;
  1285. { Widestring typed constants }
  1286. cnodeutils.InsertWideInits;
  1287. { Resourcestring references }
  1288. cnodeutils.InsertResStrInits;
  1289. { generate debuginfo }
  1290. if (cs_debuginfo in current_settings.moduleswitches) then
  1291. current_debuginfo.inserttypeinfo;
  1292. { generate imports }
  1293. if module.ImportLibraryList.Count>0 then
  1294. importlib.generatelib;
  1295. { insert own objectfile, or say that it's in a library
  1296. (no check for an .o when loading) }
  1297. ag:=is_assembler_generated;
  1298. if ag then
  1299. insertobjectfile(module)
  1300. else
  1301. begin
  1302. module.headerflags:=module.headerflags or uf_no_link;
  1303. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1304. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1305. end;
  1306. if ag then
  1307. begin
  1308. { create callframe info }
  1309. create_dwarf_frame;
  1310. { assemble }
  1311. create_objectfile(module);
  1312. end;
  1313. { Write out the ppufile after the object file has been created }
  1314. store_interface_crc:=module.interface_crc;
  1315. store_indirect_crc:=module.indirect_crc;
  1316. {$ifdef EXTDEBUG}
  1317. store_crc:=curr.crc;
  1318. {$endif EXTDEBUG}
  1319. if (Errorcount=0) then
  1320. tppumodule(module).writeppu;
  1321. if not(cs_compilesystem in current_settings.moduleswitches) then
  1322. begin
  1323. if store_interface_crc<>module.interface_crc then
  1324. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1325. if store_indirect_crc<>module.indirect_crc then
  1326. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1327. end;
  1328. {$ifdef EXTDEBUG}
  1329. if not(cs_compilesystem in current_settings.moduleswitches) then
  1330. if (store_crc<>curr.crc) then
  1331. Message1(unit_u_implementation_crc_changed,curr.ppufilename);
  1332. {$endif EXTDEBUG}
  1333. { release unregistered defs/syms from the localsymtable }
  1334. free_unregistered_localsymtable_elements(module);
  1335. { release local symtables that are not needed anymore }
  1336. free_localsymtables(module.globalsymtable);
  1337. free_localsymtables(module.localsymtable);
  1338. { leave when we got an error }
  1339. if (Errorcount>0) and not status.skip_error then
  1340. begin
  1341. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1342. status.skip_error:=true;
  1343. module_is_done(module);
  1344. if not immediate then
  1345. restore_global_state(globalstate,true);
  1346. {$ifdef DEBUG_NODE_XML}
  1347. XMLFinalizeNodeFile('unit');
  1348. {$endif DEBUG_NODE_XML}
  1349. exit;
  1350. end;
  1351. {$ifdef debug_devirt}
  1352. { print out all instantiated class/object types }
  1353. writeln('constructed object/class/classreftypes in ',curr.realmodulename^);
  1354. for i := 0 to curr.wpoinfo.createdobjtypes.count-1 do
  1355. begin
  1356. write(' ',tdef(curr.wpoinfo.createdobjtypes[i]).GetTypeName);
  1357. case tdef(curr.wpoinfo.createdobjtypes[i]).typ of
  1358. objectdef:
  1359. case tobjectdef(curr.wpoinfo.createdobjtypes[i]).objecttype of
  1360. odt_object:
  1361. writeln(' (object)');
  1362. odt_class:
  1363. writeln(' (class)');
  1364. else
  1365. internalerror(2008101103);
  1366. end;
  1367. else
  1368. internalerror(2008101104);
  1369. end;
  1370. end;
  1371. for i := 0 to curr.wpoinfo.createdclassrefobjtypes.count-1 do
  1372. begin
  1373. write(' Class Of ',tdef(curr.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1374. case tdef(curr.wpoinfo.createdclassrefobjtypes[i]).typ of
  1375. objectdef:
  1376. case tobjectdef(curr.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1377. odt_class:
  1378. writeln(' (classrefdef)');
  1379. else
  1380. internalerror(2008101105);
  1381. end
  1382. else
  1383. internalerror(2008101102);
  1384. end;
  1385. end;
  1386. {$endif debug_devirt}
  1387. Message1(unit_u_finished_compiling,module.modulename^);
  1388. module_is_done(module);
  1389. if not immediate then
  1390. restore_global_state(globalstate,true);
  1391. for i:=0 to module.waitingunits.count-1 do
  1392. begin
  1393. waitingmodule:=tmodule(module.waitingunits[i]);
  1394. waitingmodule.waitingforunit.remove(module);
  1395. { only finish the module if it isn't already finished }
  1396. if (waitingmodule.waitingforunit.count=0) and
  1397. assigned(waitingmodule.finishstate) then
  1398. begin
  1399. finish_unit(waitingmodule,false);
  1400. waitingmodule.end_of_parsing;
  1401. end;
  1402. end;
  1403. {$ifdef DEBUG_NODE_XML}
  1404. XMLFinalizeNodeFile('unit');
  1405. {$endif DEBUG_NODE_XML}
  1406. end;
  1407. procedure proc_package(curr: tmodule);
  1408. var
  1409. main_file : tinputfile;
  1410. hp,hp2 : tmodule;
  1411. pkg : tpcppackage;
  1412. {finalize_procinfo,
  1413. init_procinfo,}
  1414. main_procinfo : tcgprocinfo;
  1415. force_init_final : boolean;
  1416. uu : tused_unit;
  1417. module_name: ansistring;
  1418. pentry: ppackageentry;
  1419. feature : tfeature;
  1420. begin
  1421. Status.IsPackage:=true;
  1422. Status.IsExe:=true;
  1423. parse_only:=false;
  1424. main_procinfo:=nil;
  1425. {init_procinfo:=nil;
  1426. finalize_procinfo:=nil;}
  1427. if not (tf_supports_packages in target_info.flags) then
  1428. message1(parser_e_packages_not_supported,target_info.name);
  1429. if not RelocSectionSetExplicitly then
  1430. RelocSection:=true;
  1431. { Relocation works only without stabs under Windows when }
  1432. { external linker (LD) is used. LD generates relocs for }
  1433. { stab sections which is not loaded in memory. It causes }
  1434. { AV error when DLL is loaded and relocation is needed. }
  1435. { Internal linker does not have this problem. }
  1436. if RelocSection and
  1437. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1438. (cs_link_extern in current_settings.globalswitches) then
  1439. begin
  1440. include(current_settings.globalswitches,cs_link_strip);
  1441. { Warning stabs info does not work with reloc section !! }
  1442. if (cs_debuginfo in current_settings.moduleswitches) and
  1443. (target_dbg.id=dbg_stabs) then
  1444. begin
  1445. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1446. Message(parser_w_parser_win32_debug_needs_WN);
  1447. exclude(current_settings.moduleswitches,cs_debuginfo);
  1448. end;
  1449. end;
  1450. { get correct output names }
  1451. main_file := current_scanner.inputfile;
  1452. while assigned(main_file.next) do
  1453. main_file := main_file.next;
  1454. curr.SetFileName(main_file.path+main_file.name,true);
  1455. { consume _PACKAGE word }
  1456. consume(_ID);
  1457. module_name:=orgpattern;
  1458. consume(_ID);
  1459. while token=_POINT do
  1460. begin
  1461. consume(_POINT);
  1462. module_name:=module_name+'.'+orgpattern;
  1463. consume(_ID);
  1464. end;
  1465. curr.setmodulename(module_name);
  1466. curr.ispackage:=true;
  1467. exportlib.preparelib(module_name);
  1468. pkg:=tpcppackage.create(module_name);
  1469. if tf_library_needs_pic in target_info.flags then
  1470. include(current_settings.moduleswitches,cs_create_pic);
  1471. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1472. read, all following directives are parsed as well }
  1473. setupglobalswitches;
  1474. {$ifdef DEBUG_NODE_XML}
  1475. XMLInitializeNodeFile('package', module_name);
  1476. {$endif DEBUG_NODE_XML}
  1477. consume(_SEMICOLON);
  1478. { global switches are read, so further changes aren't allowed }
  1479. curr.in_global:=false;
  1480. { set implementation flag }
  1481. curr.in_interface:=false;
  1482. curr.interface_compiled:=true;
  1483. { insert after the unit symbol tables the static symbol table }
  1484. { of the program }
  1485. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1486. { ensure that no packages are picked up from the options }
  1487. packagelist.clear;
  1488. // There should always be a requires, except for the system package. So we load here
  1489. if Assigned(curr) then
  1490. curr.Loadlocalnamespacelist
  1491. else
  1492. current_namespacelist:=Nil;
  1493. {Read the packages used by the package we compile.}
  1494. if (token=_ID) and (idtoken=_REQUIRES) then
  1495. begin
  1496. { consume _REQUIRES word }
  1497. consume(_ID);
  1498. while true do
  1499. begin
  1500. if token=_ID then
  1501. begin
  1502. module_name:=orgpattern;
  1503. consume(_ID);
  1504. while token=_POINT do
  1505. begin
  1506. consume(_POINT);
  1507. module_name:=module_name+'.'+orgpattern;
  1508. consume(_ID);
  1509. end;
  1510. add_package(module_name,false,true);
  1511. end
  1512. else
  1513. consume(_ID);
  1514. if token=_COMMA then
  1515. consume(_COMMA)
  1516. else
  1517. break;
  1518. end;
  1519. consume(_SEMICOLON);
  1520. end;
  1521. { now load all packages, so that we can determine whether a unit is
  1522. already provided by one of the loaded packages }
  1523. load_packages;
  1524. if packagelist.Count>0 then
  1525. begin
  1526. { this means the SYSTEM unit *must* be part of one of the required
  1527. packages, so load it }
  1528. AddUnit(curr,'system',false);
  1529. systemunit:=tglobalsymtable(symtablestack.top);
  1530. load_intern_types;
  1531. { system unit is loaded, now insert feature defines }
  1532. for feature:=low(tfeature) to high(tfeature) do
  1533. if feature in features then
  1534. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1535. end;
  1536. {Load the units used by the program we compile.}
  1537. if (token=_ID) and (idtoken=_CONTAINS) then
  1538. begin
  1539. { consume _CONTAINS word }
  1540. consume(_ID);
  1541. while true do
  1542. begin
  1543. if token=_ID then
  1544. begin
  1545. module_name:=orgpattern;
  1546. consume(_ID);
  1547. while token=_POINT do
  1548. begin
  1549. consume(_POINT);
  1550. module_name:=module_name+'.'+orgpattern;
  1551. consume(_ID);
  1552. end;
  1553. hp:=AddUnit(curr,module_name);
  1554. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1555. begin
  1556. systemunit:=tglobalsymtable(hp.globalsymtable);
  1557. load_intern_types;
  1558. end;
  1559. end
  1560. else
  1561. consume(_ID);
  1562. if token=_COMMA then
  1563. consume(_COMMA)
  1564. else break;
  1565. end;
  1566. consume(_SEMICOLON);
  1567. end;
  1568. { All units are read, now give them a number }
  1569. curr.updatemaps;
  1570. hp:=tmodule(loaded_units.first);
  1571. while assigned(hp) do
  1572. begin
  1573. if (hp<>curr) and not assigned(hp.package) then
  1574. begin
  1575. if mf_package_deny in hp.moduleflags then
  1576. message1(package_e_unit_deny_package,hp.realmodulename^);
  1577. { part of the package's used, aka contained units? }
  1578. uu:=tused_unit(curr.used_units.first);
  1579. while assigned(uu) do
  1580. begin
  1581. if uu.u=hp then
  1582. break;
  1583. uu:=tused_unit(uu.next);
  1584. end;
  1585. if not assigned(uu) then
  1586. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1587. end;
  1588. { was this unit listed as a contained unit? If so => error }
  1589. if (hp<>curr) and assigned(hp.package) then
  1590. begin
  1591. uu:=tused_unit(curr.used_units.first);
  1592. while assigned(uu) do
  1593. begin
  1594. if uu.u=hp then
  1595. break;
  1596. uu:=tused_unit(uu.next);
  1597. end;
  1598. if assigned(uu) then
  1599. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1600. end;
  1601. hp:=tmodule(hp.next);
  1602. end;
  1603. {Insert the name of the main program into the symbol table.}
  1604. if curr.realmodulename^<>'' then
  1605. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1606. Message1(parser_u_parsing_implementation,curr.mainsource);
  1607. symtablestack.push(curr.localsymtable);
  1608. { create whole program optimisation information }
  1609. curr.wpoinfo:=tunitwpoinfo.create;
  1610. { should we force unit initialization? }
  1611. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1612. if force_init_final or cnodeutils.force_init then
  1613. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1614. { Add symbol to the exports section for win32 so smartlinking a
  1615. DLL will include the edata section }
  1616. if assigned(exportlib) and
  1617. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1618. (mf_has_exports in curr.moduleflags) then
  1619. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1620. { all labels must be defined before generating code }
  1621. if Errorcount=0 then
  1622. tstoredsymtable(curr.localsymtable).checklabels;
  1623. symtablestack.pop(curr.localsymtable);
  1624. { consume the last point }
  1625. consume(_END);
  1626. consume(_POINT);
  1627. if (Errorcount=0) then
  1628. begin
  1629. { test static symtable }
  1630. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1631. tstoredsymtable(curr.localsymtable).allprivatesused;
  1632. tstoredsymtable(curr.localsymtable).check_forwards;
  1633. { Note: all contained units are considered as used }
  1634. end;
  1635. if target_info.system in systems_all_windows+systems_nativent then
  1636. begin
  1637. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1638. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1639. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1640. main_procinfo.generate_code;
  1641. end;
  1642. {$ifdef DEBUG_NODE_XML}
  1643. XMLFinalizeNodeFile('package');
  1644. {$endif DEBUG_NODE_XML}
  1645. { leave when we got an error }
  1646. if (Errorcount>0) and not status.skip_error then
  1647. begin
  1648. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1649. status.skip_error:=true;
  1650. pkg.free;
  1651. exit;
  1652. end;
  1653. { remove all unused units, this happends when units are removed
  1654. from the uses clause in the source and the ppu was already being loaded }
  1655. hp:=tmodule(loaded_units.first);
  1656. while assigned(hp) do
  1657. begin
  1658. hp2:=hp;
  1659. hp:=tmodule(hp.next);
  1660. if assigned(hp2.package) then
  1661. add_package_unit_ref(hp2.package);
  1662. if hp2.is_unit and
  1663. not assigned(hp2.globalsymtable) then
  1664. loaded_units.remove(hp2);
  1665. end;
  1666. exportlib.ignoreduplicates:=true;
  1667. { force exports }
  1668. uu:=tused_unit(usedunits.first);
  1669. while assigned(uu) do
  1670. begin
  1671. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1672. begin
  1673. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1674. load_intern_types;
  1675. end;
  1676. if not assigned(uu.u.package) then
  1677. export_unit(uu.u);
  1678. uu:=tused_unit(uu.next);
  1679. end;
  1680. {$ifdef arm}
  1681. { Insert .pdata section for arm-wince.
  1682. It is needed for exception handling. }
  1683. if target_info.system in [system_arm_wince] then
  1684. InsertPData;
  1685. {$endif arm}
  1686. { generate debuginfo }
  1687. if (cs_debuginfo in current_settings.moduleswitches) then
  1688. current_debuginfo.inserttypeinfo;
  1689. exportlib.generatelib;
  1690. exportlib.ignoreduplicates:=false;
  1691. { create import libraries for all packages }
  1692. if packagelist.count>0 then
  1693. createimportlibfromexternals;
  1694. { generate imports }
  1695. if curr.ImportLibraryList.Count>0 then
  1696. importlib.generatelib;
  1697. { Reference all DEBUGINFO sections from the main .fpc section }
  1698. if (cs_debuginfo in current_settings.moduleswitches) then
  1699. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1700. { insert own objectfile }
  1701. insertobjectfile(curr);
  1702. { assemble and link }
  1703. create_objectfile(curr);
  1704. { We might need the symbols info if not using
  1705. the default do_extractsymbolinfo
  1706. which is a dummy function PM }
  1707. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1708. { release all local symtables that are not needed anymore }
  1709. if (not needsymbolinfo) then
  1710. free_localsymtables(curr.localsymtable);
  1711. { leave when we got an error }
  1712. if (Errorcount>0) and not status.skip_error then
  1713. begin
  1714. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1715. status.skip_error:=true;
  1716. pkg.free;
  1717. exit;
  1718. end;
  1719. if (not curr.is_unit) then
  1720. begin
  1721. { we add all loaded units that are not part of a package to the
  1722. package; this includes units in the "contains" section as well
  1723. as implicitely imported ones }
  1724. hp:=tmodule(loaded_units.first);
  1725. while assigned(hp) do
  1726. begin
  1727. if (hp<>curr) then
  1728. begin
  1729. if not assigned(hp.package) then
  1730. begin
  1731. pkg.addunit(hp);
  1732. check_for_indirect_package_usages(hp.used_units);
  1733. end
  1734. else
  1735. begin
  1736. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1737. if not assigned(pentry) then
  1738. internalerror(2015112301);
  1739. pkg.add_required_package(hp.package);
  1740. end;
  1741. end;
  1742. hp:=tmodule(hp.next);
  1743. end;
  1744. pkg.initmoduleinfo(curr);
  1745. { create the executable when we are at level 1 }
  1746. if (compile_level=1) then
  1747. begin
  1748. { create global resource file by collecting all resource files }
  1749. CollectResourceFiles;
  1750. { write .def file }
  1751. if (cs_link_deffile in current_settings.globalswitches) then
  1752. deffile.writefile;
  1753. { generate the pcp file }
  1754. pkg.savepcp;
  1755. { insert all .o files from all loaded units and
  1756. unload the units, we don't need them anymore.
  1757. Keep the curr because that is still needed }
  1758. hp:=tmodule(loaded_units.first);
  1759. while assigned(hp) do
  1760. begin
  1761. { only link in those units which should become part of this
  1762. package }
  1763. if not assigned(hp.package) then
  1764. linker.AddModuleFiles(hp);
  1765. hp2:=tmodule(hp.next);
  1766. if (hp<>curr) and
  1767. (not needsymbolinfo) then
  1768. begin
  1769. loaded_units.remove(hp);
  1770. hp.free;
  1771. end;
  1772. hp:=hp2;
  1773. end;
  1774. { add the library of directly used packages }
  1775. add_package_libs(linker);
  1776. { and now link the package library }
  1777. linker.MakeSharedLibrary
  1778. end;
  1779. { Give Fatal with error count for linker errors }
  1780. if (Errorcount>0) and not status.skip_error then
  1781. begin
  1782. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1783. status.skip_error:=true;
  1784. end;
  1785. pkg.free;
  1786. end;
  1787. end;
  1788. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  1789. var
  1790. program_uses_checkpointer : boolean;
  1791. hp,hp2 : tmodule;
  1792. begin
  1793. { create global resource file by collecting all resource files }
  1794. CollectResourceFiles;
  1795. { write .def file }
  1796. if (cs_link_deffile in current_settings.globalswitches) then
  1797. deffile.writefile;
  1798. { link SysInit (if any) first, to have behavior consistent with
  1799. assembler startup files }
  1800. if assigned(sysinitmod) then
  1801. linker.AddModuleFiles(sysinitmod);
  1802. { Does any unit use checkpointer function }
  1803. program_uses_checkpointer:=false;
  1804. { insert all .o files from all loaded units and
  1805. unload the units, we don't need them anymore.
  1806. Keep the curr because that is still needed }
  1807. hp:=tmodule(loaded_units.first);
  1808. while assigned(hp) do
  1809. begin
  1810. if (hp<>sysinitmod) and not assigned(hp.package) then
  1811. begin
  1812. linker.AddModuleFiles(hp);
  1813. if mf_checkpointer_called in hp.moduleflags then
  1814. program_uses_checkpointer:=true;
  1815. end;
  1816. hp2:=tmodule(hp.next);
  1817. if assigned(hp.package) then
  1818. add_package_unit_ref(hp.package);
  1819. if (hp<>curr) and
  1820. (not needsymbolinfo) then
  1821. begin
  1822. loaded_units.remove(hp);
  1823. hp.free;
  1824. end;
  1825. hp:=hp2;
  1826. end;
  1827. { free also unneeded units we didn't free before }
  1828. if not needsymbolinfo then
  1829. unloaded_units.Clear;
  1830. { Does any unit use checkpointer function }
  1831. if program_uses_checkpointer then
  1832. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  1833. { add all directly used packages as libraries }
  1834. add_package_libs(linker);
  1835. { finally we can create an executable }
  1836. if curr.islibrary then
  1837. linker.MakeSharedLibrary
  1838. else
  1839. linker.MakeExecutable;
  1840. { collect all necessary information for whole-program optimization }
  1841. wpoinfomanager.extractwpoinfofromprogram;
  1842. end;
  1843. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  1844. var
  1845. sysinitmod, hp,hp2 : tmodule;
  1846. resources_used : boolean;
  1847. begin
  1848. sysinitmod:=nil;
  1849. hp:=nil;
  1850. hp2:=nil;
  1851. resources_used:=false;
  1852. {$ifdef DEBUG_NODE_XML}
  1853. if IsLibrary then
  1854. XMLFinalizeNodeFile('library')
  1855. else
  1856. XMLFinalizeNodeFile('program');
  1857. {$endif DEBUG_NODE_XML}
  1858. { reset wpo flags for all defs }
  1859. reset_all_defs(curr);
  1860. if (Errorcount=0) then
  1861. begin
  1862. { test static symtable }
  1863. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1864. tstoredsymtable(curr.localsymtable).allprivatesused;
  1865. tstoredsymtable(curr.localsymtable).check_forwards;
  1866. curr.allunitsused;
  1867. end;
  1868. { leave when we got an error }
  1869. if (Errorcount>0) and not status.skip_error then
  1870. begin
  1871. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1872. status.skip_error:=true;
  1873. exit;
  1874. end;
  1875. { remove all unused units, this happens when units are removed
  1876. from the uses clause in the source and the ppu was already being loaded }
  1877. hp:=tmodule(loaded_units.first);
  1878. while assigned(hp) do
  1879. begin
  1880. hp2:=hp;
  1881. hp:=tmodule(hp.next);
  1882. if hp2.is_unit and
  1883. not assigned(hp2.globalsymtable) then
  1884. begin
  1885. loaded_units.remove(hp2);
  1886. unloaded_units.concat(hp2);
  1887. end;
  1888. end;
  1889. { do we need to add the variants unit? }
  1890. maybeloadvariantsunit(curr);
  1891. { Now that everything has been compiled we know if we need resource
  1892. support. If not, remove the unit. }
  1893. resources_used:=MaybeRemoveResUnit(curr);
  1894. linker.initsysinitunitname;
  1895. if target_info.system in systems_internal_sysinit then
  1896. begin
  1897. { add start/halt unit }
  1898. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  1899. end
  1900. else
  1901. sysinitmod:=nil;
  1902. {$ifdef arm}
  1903. { Insert .pdata section for arm-wince.
  1904. It is needed for exception handling. }
  1905. if target_info.system in [system_arm_wince] then
  1906. InsertPData;
  1907. {$endif arm}
  1908. cnodeutils.InsertThreadvars;
  1909. { generate rtti/init tables }
  1910. write_persistent_type_info(curr.localsymtable,false);
  1911. { if an Objective-C module, generate rtti and module info }
  1912. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  1913. { generate debuginfo }
  1914. if (cs_debuginfo in current_settings.moduleswitches) then
  1915. current_debuginfo.inserttypeinfo;
  1916. if islibrary or (target_info.system in systems_unit_program_exports) then
  1917. exportlib.generatelib;
  1918. { Reference all DEBUGINFO sections from the main .fpc section }
  1919. if (cs_debuginfo in current_settings.moduleswitches) then
  1920. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1921. { Resource strings }
  1922. GenerateResourceStrings;
  1923. { Windows widestring needing initialization }
  1924. cnodeutils.InsertWideInits;
  1925. { Resourcestring references (const foo:string=someresourcestring) }
  1926. cnodeutils.InsertResStrInits;
  1927. { insert Tables and StackLength }
  1928. cnodeutils.InsertInitFinalTable;
  1929. cnodeutils.InsertThreadvarTablesTable;
  1930. cnodeutils.InsertResourceTablesTable;
  1931. cnodeutils.InsertWideInitsTablesTable;
  1932. cnodeutils.InsertResStrTablesTable;
  1933. cnodeutils.InsertMemorySizes;
  1934. { Insert symbol to resource info }
  1935. cnodeutils.InsertResourceInfo(resources_used);
  1936. { create callframe info }
  1937. create_dwarf_frame;
  1938. { create import library for all packages }
  1939. if packagelist.count>0 then
  1940. createimportlibfromexternals;
  1941. { generate imports }
  1942. if curr.ImportLibraryList.Count>0 then
  1943. importlib.generatelib;
  1944. { insert own objectfile }
  1945. insertobjectfile(curr);
  1946. { assemble and link }
  1947. create_objectfile(curr);
  1948. { We might need the symbols info if not using
  1949. the default do_extractsymbolinfo
  1950. which is a dummy function PM }
  1951. needsymbolinfo:=
  1952. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  1953. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  1954. { release all local symtables that are not needed anymore }
  1955. if (not needsymbolinfo) then
  1956. free_localsymtables(curr.localsymtable);
  1957. { leave when we got an error }
  1958. if (Errorcount>0) and not status.skip_error then
  1959. begin
  1960. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1961. status.skip_error:=true;
  1962. exit;
  1963. end;
  1964. { create the executable when we are at level 1 }
  1965. if (not curr.is_unit) and (compile_level=1) then
  1966. proc_create_executable(curr,sysinitmod,islibrary);
  1967. { Give Fatal with error count for linker errors }
  1968. if (Errorcount>0) and not status.skip_error then
  1969. begin
  1970. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1971. status.skip_error:=true;
  1972. end;
  1973. end;
  1974. procedure proc_program_declarations(curr : tmodule; islibrary : boolean);
  1975. var
  1976. initpd : tprocdef;
  1977. finalize_procinfo,
  1978. init_procinfo,
  1979. main_procinfo : tcgprocinfo;
  1980. force_init_final : boolean;
  1981. begin
  1982. main_procinfo:=nil;
  1983. init_procinfo:=nil;
  1984. finalize_procinfo:=nil;
  1985. {Insert the name of the main program into the symbol table.}
  1986. if curr.realmodulename^<>'' then
  1987. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1988. Message1(parser_u_parsing_implementation,curr.mainsource);
  1989. symtablestack.push(curr.localsymtable);
  1990. {$ifdef jvm}
  1991. { fake classdef to represent the class corresponding to the unit }
  1992. addmoduleclass;
  1993. {$endif}
  1994. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1995. maybe_load_got;
  1996. { create whole program optimisation information }
  1997. curr.wpoinfo:=tunitwpoinfo.create;
  1998. { The program intialization needs an alias, so it can be called
  1999. from the bootstrap code.}
  2000. if islibrary then
  2001. begin
  2002. initpd:=nil;
  2003. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2004. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2005. if target_info.system in systems_darwin then
  2006. begin
  2007. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2008. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2009. initpd:=main_procinfo.procdef;
  2010. main_procinfo.free;
  2011. end;
  2012. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2013. { Win32 startup code needs a single name }
  2014. if not(target_info.system in (systems_darwin+systems_aix)) then
  2015. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2016. else
  2017. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2018. if not(target_info.system in systems_darwin) then
  2019. initpd:=main_procinfo.procdef;
  2020. cnodeutils.RegisterModuleInitFunction(initpd);
  2021. end
  2022. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2023. begin
  2024. { create a stub with the name of the desired main routine, with
  2025. the same signature as the C "main" function, and call through to
  2026. FPC_SYSTEMMAIN, which will initialise everything based on its
  2027. parameters. This function cannot be in the system unit, because
  2028. its name can be configured on the command line (for use with e.g.
  2029. SDL, where the main function should be called SDL_main) }
  2030. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2031. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2032. main_procinfo.free;
  2033. { now create the PASCALMAIN routine (which will be called from
  2034. FPC_SYSTEMMAIN) }
  2035. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2036. end
  2037. else
  2038. begin
  2039. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2040. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2041. end;
  2042. main_procinfo.parse_body;
  2043. { save file pos for debuginfo }
  2044. curr.mainfilepos:=main_procinfo.entrypos;
  2045. { finalize? }
  2046. if token=_FINALIZATION then
  2047. begin
  2048. { Parse the finalize }
  2049. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2050. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2051. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2052. finalize_procinfo.parse_body;
  2053. end;
  2054. { Generate specializations of objectdefs methods }
  2055. if Errorcount=0 then
  2056. generate_specialization_procs;
  2057. { This needs to be done before we generate the VMTs }
  2058. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2059. add_synthetic_interface_classes_for_st(curr.localsymtable);
  2060. { Generate VMTs }
  2061. if Errorcount=0 then
  2062. write_vmts(curr.localsymtable,false);
  2063. { add implementations for synthetic method declarations added by
  2064. the compiler }
  2065. add_synthetic_method_implementations(curr.localsymtable);
  2066. { generate construction functions for all attributes in the program }
  2067. generate_attr_constrs(curr.used_rtti_attrs);
  2068. { should we force unit initialization? }
  2069. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2070. if force_init_final or cnodeutils.force_init then
  2071. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2072. { Add symbol to the exports section for win32 so smartlinking a
  2073. DLL will include the edata section }
  2074. if assigned(exportlib) and
  2075. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2076. (mf_has_exports in curr.moduleflags) then
  2077. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2078. if (force_init_final or cnodeutils.force_final) and
  2079. (
  2080. not assigned(finalize_procinfo)
  2081. or has_no_code(finalize_procinfo.code)
  2082. ) then
  2083. begin
  2084. { first release the not used finalize procinfo }
  2085. if assigned(finalize_procinfo) then
  2086. begin
  2087. release_proc_symbol(finalize_procinfo.procdef);
  2088. release_main_proc(curr,finalize_procinfo);
  2089. end;
  2090. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2091. end;
  2092. { the finalization routine of libraries is generic (and all libraries need to }
  2093. { be finalized, so they can finalize any units they use }
  2094. { Place in "pure assembler" list so that the llvm assembler writer
  2095. directly emits the generated directives }
  2096. if (islibrary) then
  2097. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2098. { all labels must be defined before generating code }
  2099. if Errorcount=0 then
  2100. tstoredsymtable(curr.localsymtable).checklabels;
  2101. { See remark in unit init/final }
  2102. main_procinfo.generate_code_tree;
  2103. main_procinfo.resetprocdef;
  2104. release_main_proc(curr,main_procinfo);
  2105. if assigned(init_procinfo) then
  2106. begin
  2107. { initialization can be implicit only }
  2108. include(curr.moduleflags,mf_init);
  2109. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2110. init_procinfo.generate_code;
  2111. init_procinfo.resetprocdef;
  2112. release_main_proc(curr,init_procinfo);
  2113. end;
  2114. if assigned(finalize_procinfo) then
  2115. begin
  2116. if force_init_final or
  2117. cnodeutils.force_init or
  2118. not(has_no_code(finalize_procinfo.code)) then
  2119. begin
  2120. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2121. finalize_procinfo.generate_code_tree;
  2122. include(curr.moduleflags,mf_finalize);
  2123. end;
  2124. finalize_procinfo.resetprocdef;
  2125. release_main_proc(curr,finalize_procinfo);
  2126. end;
  2127. symtablestack.pop(curr.localsymtable);
  2128. { consume the last point }
  2129. consume(_POINT);
  2130. proc_program_after_parsing(curr,islibrary);
  2131. end;
  2132. procedure proc_program(curr: tmodule; islibrary : boolean);
  2133. type
  2134. TProgramParam = record
  2135. name : ansistring;
  2136. nr : dword;
  2137. end;
  2138. var
  2139. main_file : tinputfile;
  2140. program_name : ansistring;
  2141. consume_semicolon_after_uses,
  2142. consume_semicolon_after_loaded : boolean;
  2143. ps : tprogramparasym;
  2144. paramnum : longint;
  2145. textsym : ttypesym;
  2146. sc : array of TProgramParam;
  2147. i : Longint;
  2148. feature : tfeature;
  2149. begin
  2150. Status.IsLibrary:=IsLibrary;
  2151. Status.IsPackage:=false;
  2152. Status.IsExe:=true;
  2153. parse_only:=false;
  2154. consume_semicolon_after_loaded:=false;
  2155. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2156. sc:=nil;
  2157. { DLL defaults to create reloc info }
  2158. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2159. begin
  2160. if not RelocSectionSetExplicitly then
  2161. RelocSection:=true;
  2162. end;
  2163. { Relocation works only without stabs under Windows when }
  2164. { external linker (LD) is used. LD generates relocs for }
  2165. { stab sections which is not loaded in memory. It causes }
  2166. { AV error when DLL is loaded and relocation is needed. }
  2167. { Internal linker does not have this problem. }
  2168. if RelocSection and
  2169. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2170. (cs_link_extern in current_settings.globalswitches) then
  2171. begin
  2172. include(current_settings.globalswitches,cs_link_strip);
  2173. { Warning stabs info does not work with reloc section !! }
  2174. if (cs_debuginfo in current_settings.moduleswitches) and
  2175. (target_dbg.id=dbg_stabs) then
  2176. begin
  2177. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2178. Message(parser_w_parser_win32_debug_needs_WN);
  2179. exclude(current_settings.moduleswitches,cs_debuginfo);
  2180. end;
  2181. end;
  2182. { get correct output names }
  2183. main_file := current_scanner.inputfile;
  2184. while assigned(main_file.next) do
  2185. main_file := main_file.next;
  2186. curr.SetFileName(main_file.path+main_file.name,true);
  2187. if islibrary then
  2188. begin
  2189. consume(_LIBRARY);
  2190. program_name:=orgpattern;
  2191. consume(_ID);
  2192. while token=_POINT do
  2193. begin
  2194. consume(_POINT);
  2195. program_name:=program_name+'.'+orgpattern;
  2196. consume(_ID);
  2197. end;
  2198. curr.setmodulename(program_name);
  2199. curr.islibrary:=true;
  2200. exportlib.preparelib(program_name);
  2201. if tf_library_needs_pic in target_info.flags then
  2202. begin
  2203. include(current_settings.moduleswitches,cs_create_pic);
  2204. { also set create_pic for all unit compilation }
  2205. include(init_settings.moduleswitches,cs_create_pic);
  2206. end;
  2207. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2208. read, all following directives are parsed as well }
  2209. setupglobalswitches;
  2210. consume_semicolon_after_loaded:=true;
  2211. {$ifdef DEBUG_NODE_XML}
  2212. XMLInitializeNodeFile('library', program_name);
  2213. {$endif DEBUG_NODE_XML}
  2214. end
  2215. else
  2216. { is there an program head ? }
  2217. if token=_PROGRAM then
  2218. begin
  2219. consume(_PROGRAM);
  2220. program_name:=orgpattern;
  2221. consume(_ID);
  2222. while token=_POINT do
  2223. begin
  2224. consume(_POINT);
  2225. program_name:=program_name+'.'+orgpattern;
  2226. consume(_ID);
  2227. end;
  2228. curr.setmodulename(program_name);
  2229. if (target_info.system in systems_unit_program_exports) then
  2230. exportlib.preparelib(program_name);
  2231. if token=_LKLAMMER then
  2232. begin
  2233. consume(_LKLAMMER);
  2234. paramnum:=1;
  2235. repeat
  2236. if m_isolike_program_para in current_settings.modeswitches then
  2237. begin
  2238. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2239. begin
  2240. { the symtablestack is not setup here, so text must be created later on }
  2241. Setlength(sc,length(sc)+1);
  2242. with sc[high(sc)] do
  2243. begin
  2244. name:=pattern;
  2245. nr:=paramnum;
  2246. end;
  2247. inc(paramnum);
  2248. end;
  2249. end;
  2250. consume(_ID);
  2251. until not try_to_consume(_COMMA);
  2252. consume(_RKLAMMER);
  2253. end;
  2254. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2255. read, all following directives are parsed as well }
  2256. setupglobalswitches;
  2257. consume_semicolon_after_loaded:=true;
  2258. {$ifdef DEBUG_NODE_XML}
  2259. XMLInitializeNodeFile('program', program_name);
  2260. {$endif DEBUG_NODE_XML}
  2261. end
  2262. else
  2263. begin
  2264. if (target_info.system in systems_unit_program_exports) then
  2265. exportlib.preparelib(curr.realmodulename^);
  2266. { setup things using the switches }
  2267. setupglobalswitches;
  2268. {$ifdef DEBUG_NODE_XML}
  2269. XMLInitializeNodeFile('program', curr.realmodulename^);
  2270. {$endif DEBUG_NODE_XML}
  2271. end;
  2272. { load all packages, so we know whether a unit is contained inside a
  2273. package or not }
  2274. load_packages;
  2275. { set implementation flag }
  2276. curr.in_interface:=false;
  2277. curr.interface_compiled:=true;
  2278. { insert after the unit symbol tables the static symbol table
  2279. of the program }
  2280. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2281. { load system unit }
  2282. loadsystemunit(curr);
  2283. { consume the semicolon now that the system unit is loaded }
  2284. if consume_semicolon_after_loaded then
  2285. consume(_SEMICOLON);
  2286. { global switches are read, so further changes aren't allowed }
  2287. curr.in_global:=false;
  2288. { system unit is loaded, now insert feature defines }
  2289. for feature:=low(tfeature) to high(tfeature) do
  2290. if feature in features then
  2291. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2292. { load standard units, e.g objpas,profile unit }
  2293. loaddefaultunits(curr);
  2294. { Load units provided on the command line }
  2295. loadautounits(curr);
  2296. { insert iso program parameters }
  2297. if length(sc)>0 then
  2298. begin
  2299. textsym:=search_system_type('TEXT');
  2300. if not(assigned(textsym)) then
  2301. internalerror(2013011201);
  2302. for i:=0 to high(sc) do
  2303. begin
  2304. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2305. curr.localsymtable.insertsym(ps,true);
  2306. end;
  2307. end;
  2308. { Load the units used by the program we compile. }
  2309. if token=_USES then
  2310. begin
  2311. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2312. if Assigned(curr) then
  2313. curr.Loadlocalnamespacelist
  2314. else
  2315. current_namespacelist:=Nil;
  2316. loadunits(curr,nil);
  2317. consume_semicolon_after_uses:=true;
  2318. end
  2319. else
  2320. consume_semicolon_after_uses:=false;
  2321. { All units are read, now give them a number }
  2322. curr.updatemaps;
  2323. { consume the semicolon after maps have been updated else conditional compiling expressions
  2324. might cause internal errors, see tw8611 }
  2325. if consume_semicolon_after_uses then
  2326. consume(_SEMICOLON);
  2327. proc_program_declarations(curr,islibrary);
  2328. end;
  2329. end.