pmodules.pas 97 KB

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