pmodules.pas 84 KB

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