pmodules.pas 92 KB

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