pmodules.pas 84 KB

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