pmodules.pas 89 KB

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