pmodules.pas 86 KB

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