pmodules.pas 94 KB

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