pmodules.pas 95 KB

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