pmodules.pas 95 KB

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