pmodules.pas 95 KB

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