pmodules.pas 95 KB

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