pmodules.pas 75 KB

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