pmodules.pas 78 KB

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