pmodules.pas 94 KB

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