pmodules.pas 84 KB

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