pmodules.pas 86 KB

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