pmodules.pas 97 KB

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