pmodules.pas 95 KB

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