pmodules.pas 96 KB

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