pmodules.pas 96 KB

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