pmodules.pas 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  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 AddToStructInits(p:TObject;arg:pointer);
  341. var
  342. StructList: TFPList absolute arg;
  343. begin
  344. if (tdef(p).typ in [objectdef,recorddef]) and
  345. ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  346. StructList.Add(p);
  347. end;
  348. procedure InsertInitFinalTable;
  349. var
  350. hp : tused_unit;
  351. unitinits : TAsmList;
  352. count : longint;
  353. procedure write_struct_inits(u: tmodule);
  354. var
  355. i: integer;
  356. structlist: TFPList;
  357. pd: tprocdef;
  358. begin
  359. structlist := TFPList.Create;
  360. if assigned(u.globalsymtable) then
  361. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  362. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  363. { write structures }
  364. for i := 0 to structlist.Count - 1 do
  365. begin
  366. pd := tabstractrecorddef(structlist[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 := tabstractrecorddef(structlist[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. structlist.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_struct_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_struct_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. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  858. procedure maybe_load_got;
  859. {$ifdef i386}
  860. var
  861. gotvarsym : tstaticvarsym;
  862. {$endif i386}
  863. begin
  864. {$ifdef i386}
  865. if (cs_create_pic in current_settings.moduleswitches) and
  866. (tf_pic_uses_got in target_info.flags) then
  867. begin
  868. { insert symbol for got access in assembler code}
  869. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  870. vs_value,voidpointertype,[vo_is_external]);
  871. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  872. current_module.localsymtable.insert(gotvarsym);
  873. { avoid unnecessary warnings }
  874. gotvarsym.varstate:=vs_read;
  875. gotvarsym.refs:=1;
  876. end;
  877. {$endif i386}
  878. end;
  879. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  880. begin
  881. { update module flags }
  882. current_module.flags:=current_module.flags or flag;
  883. { create procdef }
  884. case flag of
  885. uf_init :
  886. begin
  887. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  888. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  889. end;
  890. uf_finalize :
  891. begin
  892. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  893. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  894. if (not current_module.is_unit) then
  895. result.procdef.aliasnames.insert('PASCALFINALIZE');
  896. end;
  897. else
  898. internalerror(200304253);
  899. end;
  900. result.code:=cnothingnode.create;
  901. end;
  902. procedure copy_macro(p:TObject; arg:pointer);
  903. begin
  904. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  905. end;
  906. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  907. var
  908. last_is_deprecated:boolean;
  909. begin
  910. try_consume_hintdirective:=false;
  911. repeat
  912. last_is_deprecated:=false;
  913. case idtoken of
  914. _LIBRARY :
  915. begin
  916. include(moduleopt,mo_hint_library);
  917. try_consume_hintdirective:=true;
  918. end;
  919. _DEPRECATED :
  920. begin
  921. include(moduleopt,mo_hint_deprecated);
  922. try_consume_hintdirective:=true;
  923. last_is_deprecated:=true;
  924. end;
  925. _EXPERIMENTAL :
  926. begin
  927. include(moduleopt,mo_hint_experimental);
  928. try_consume_hintdirective:=true;
  929. end;
  930. _PLATFORM :
  931. begin
  932. include(moduleopt,mo_hint_platform);
  933. try_consume_hintdirective:=true;
  934. end;
  935. _UNIMPLEMENTED :
  936. begin
  937. include(moduleopt,mo_hint_unimplemented);
  938. try_consume_hintdirective:=true;
  939. end;
  940. else
  941. break;
  942. end;
  943. consume(Token);
  944. { handle deprecated message }
  945. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  946. begin
  947. if deprecatedmsg<>nil then
  948. internalerror(201001221);
  949. if token=_CSTRING then
  950. deprecatedmsg:=stringdup(cstringpattern)
  951. else
  952. deprecatedmsg:=stringdup(pattern);
  953. consume(token);
  954. include(moduleopt,mo_has_deprecated_msg);
  955. end;
  956. until false;
  957. end;
  958. procedure proc_unit;
  959. function is_assembler_generated:boolean;
  960. var
  961. hal : tasmlisttype;
  962. begin
  963. result:=false;
  964. if Errorcount=0 then
  965. begin
  966. for hal:=low(TasmlistType) to high(TasmlistType) do
  967. if not current_asmdata.asmlists[hal].empty then
  968. begin
  969. result:=true;
  970. exit;
  971. end;
  972. end;
  973. end;
  974. var
  975. main_file: tinputfile;
  976. {$ifdef EXTDEBUG}
  977. store_crc,
  978. {$endif EXTDEBUG}
  979. store_interface_crc,
  980. store_indirect_crc: cardinal;
  981. s1,s2 : ^string; {Saves stack space}
  982. force_init_final : boolean;
  983. init_procinfo,
  984. finalize_procinfo : tcgprocinfo;
  985. unitname8 : string[8];
  986. ag: boolean;
  987. {$ifdef debug_devirt}
  988. i: longint;
  989. {$endif debug_devirt}
  990. begin
  991. init_procinfo:=nil;
  992. finalize_procinfo:=nil;
  993. if m_mac in current_settings.modeswitches then
  994. current_module.mode_switch_allowed:= false;
  995. consume(_UNIT);
  996. if compile_level=1 then
  997. Status.IsExe:=false;
  998. if token=_ID then
  999. begin
  1000. { create filenames and unit name }
  1001. main_file := current_scanner.inputfile;
  1002. while assigned(main_file.next) do
  1003. main_file := main_file.next;
  1004. new(s1);
  1005. s1^:=current_module.modulename^;
  1006. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1007. current_module.SetModuleName(orgpattern);
  1008. { check for system unit }
  1009. new(s2);
  1010. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  1011. unitname8:=copy(current_module.modulename^,1,8);
  1012. if (cs_check_unit_name in current_settings.globalswitches) and
  1013. (
  1014. not(
  1015. (current_module.modulename^=s2^) or
  1016. (
  1017. (length(current_module.modulename^)>8) and
  1018. (unitname8=s2^)
  1019. )
  1020. )
  1021. or
  1022. (
  1023. (length(s1^)>8) and
  1024. (s1^<>current_module.modulename^)
  1025. )
  1026. ) then
  1027. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  1028. if (current_module.modulename^='SYSTEM') then
  1029. include(current_settings.moduleswitches,cs_compilesystem);
  1030. dispose(s2);
  1031. dispose(s1);
  1032. end;
  1033. if (target_info.system in systems_unit_program_exports) then
  1034. exportlib.preparelib(current_module.realmodulename^);
  1035. consume(_ID);
  1036. { parse hint directives }
  1037. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  1038. consume(_SEMICOLON);
  1039. consume(_INTERFACE);
  1040. { global switches are read, so further changes aren't allowed }
  1041. current_module.in_global:=false;
  1042. { handle the global switches }
  1043. setupglobalswitches;
  1044. message1(unit_u_loading_interface_units,current_module.modulename^);
  1045. { update status }
  1046. status.currentmodule:=current_module.realmodulename^;
  1047. { maybe turn off m_objpas if we are compiling objpas }
  1048. if (current_module.modulename^='OBJPAS') then
  1049. exclude(current_settings.modeswitches,m_objpas);
  1050. { maybe turn off m_mac if we are compiling macpas }
  1051. if (current_module.modulename^='MACPAS') then
  1052. exclude(current_settings.modeswitches,m_mac);
  1053. parse_only:=true;
  1054. { generate now the global symboltable,
  1055. define first as local to overcome dependency conflicts }
  1056. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  1057. { insert unitsym of this unit to prevent other units having
  1058. the same name }
  1059. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1060. { load default units, like the system unit }
  1061. loaddefaultunits;
  1062. { insert qualifier for the system unit (allows system.writeln) }
  1063. if not(cs_compilesystem in current_settings.moduleswitches) and
  1064. (token=_USES) then
  1065. begin
  1066. loadunits;
  1067. { has it been compiled at a higher level ?}
  1068. if current_module.state=ms_compiled then
  1069. exit;
  1070. end;
  1071. { move the global symtable from the temporary local to global }
  1072. current_module.globalsymtable:=current_module.localsymtable;
  1073. current_module.localsymtable:=nil;
  1074. { number all units, so we know if a unit is used by this unit or
  1075. needs to be added implicitly }
  1076. current_module.updatemaps;
  1077. { create whole program optimisation information (may already be
  1078. updated in the interface, e.g., in case of classrefdef typed
  1079. constants }
  1080. current_module.wpoinfo:=tunitwpoinfo.create;
  1081. { ... parse the declarations }
  1082. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1083. symtablestack.push(current_module.globalsymtable);
  1084. read_interface_declarations;
  1085. symtablestack.pop(current_module.globalsymtable);
  1086. { Export macros defined in the interface for macpas. The macros
  1087. are put in the globalmacrosymtable that will only be used by other
  1088. units. The current unit continues to use the localmacrosymtable }
  1089. if (m_mac in current_settings.modeswitches) then
  1090. begin
  1091. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  1092. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  1093. end;
  1094. { leave when we got an error }
  1095. if (Errorcount>0) and not status.skip_error then
  1096. begin
  1097. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1098. status.skip_error:=true;
  1099. exit;
  1100. end;
  1101. { Our interface is compiled, generate CRC and switch to implementation }
  1102. if not(cs_compilesystem in current_settings.moduleswitches) and
  1103. (Errorcount=0) then
  1104. tppumodule(current_module).getppucrc;
  1105. current_module.in_interface:=false;
  1106. current_module.interface_compiled:=true;
  1107. { First reload all units depending on our interface, we need to do this
  1108. in the implementation part to prevent erroneous circular references }
  1109. tppumodule(current_module).setdefgeneration;
  1110. tppumodule(current_module).reload_flagged_units;
  1111. { Parse the implementation section }
  1112. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1113. current_module.interface_only:=true
  1114. else
  1115. current_module.interface_only:=false;
  1116. parse_only:=false;
  1117. { create static symbol table }
  1118. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1119. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1120. maybe_load_got;
  1121. if not current_module.interface_only then
  1122. begin
  1123. consume(_IMPLEMENTATION);
  1124. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1125. { Read the implementation units }
  1126. parse_implementation_uses;
  1127. end;
  1128. if current_module.state=ms_compiled then
  1129. exit;
  1130. { All units are read, now give them a number }
  1131. current_module.updatemaps;
  1132. symtablestack.push(current_module.globalsymtable);
  1133. symtablestack.push(current_module.localsymtable);
  1134. if not current_module.interface_only then
  1135. begin
  1136. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1137. if current_module.in_interface then
  1138. internalerror(200212285);
  1139. { Compile the unit }
  1140. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  1141. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1142. init_procinfo.parse_body;
  1143. { save file pos for debuginfo }
  1144. current_module.mainfilepos:=init_procinfo.entrypos;
  1145. end;
  1146. { Generate specializations of objectdefs methods }
  1147. generate_specialization_procs;
  1148. { if the unit contains ansi/widestrings, initialization and
  1149. finalization code must be forced }
  1150. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1151. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1152. { should we force unit initialization? }
  1153. { this is a hack, but how can it be done better ? }
  1154. if force_init_final and ((current_module.flags and uf_init)=0) then
  1155. begin
  1156. { first release the not used init procinfo }
  1157. if assigned(init_procinfo) then
  1158. release_main_proc(init_procinfo);
  1159. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1160. end;
  1161. { finalize? }
  1162. if not current_module.interface_only and (token=_FINALIZATION) then
  1163. begin
  1164. { the uf_finalize flag is only set after we checked that it
  1165. wasn't empty }
  1166. { Compile the finalize }
  1167. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1168. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1169. finalize_procinfo.parse_body;
  1170. end
  1171. else if force_init_final then
  1172. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1173. { Now both init and finalize bodies are read and it is known
  1174. which variables are used in both init and finalize we can now
  1175. generate the code. This is required to prevent putting a variable in
  1176. a register that is also used in the finalize body (PFV) }
  1177. if assigned(init_procinfo) then
  1178. begin
  1179. init_procinfo.generate_code;
  1180. init_procinfo.resetprocdef;
  1181. release_main_proc(init_procinfo);
  1182. end;
  1183. if assigned(finalize_procinfo) then
  1184. begin
  1185. finalize_procinfo.generate_code;
  1186. finalize_procinfo.resetprocdef;
  1187. release_main_proc(finalize_procinfo);
  1188. end;
  1189. symtablestack.pop(current_module.localsymtable);
  1190. symtablestack.pop(current_module.globalsymtable);
  1191. { the last char should always be a point }
  1192. consume(_POINT);
  1193. { reset wpo flags for all defs }
  1194. reset_all_defs;
  1195. if (Errorcount=0) then
  1196. begin
  1197. { tests, if all (interface) forwards are resolved }
  1198. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1199. { check if all private fields are used }
  1200. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1201. { test static symtable }
  1202. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1203. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1204. tstoredsymtable(current_module.localsymtable).check_forwards;
  1205. tstoredsymtable(current_module.localsymtable).checklabels;
  1206. { used units }
  1207. current_module.allunitsused;
  1208. end;
  1209. { leave when we got an error }
  1210. if (Errorcount>0) and not status.skip_error then
  1211. begin
  1212. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1213. status.skip_error:=true;
  1214. exit;
  1215. end;
  1216. { if an Objective-C module, generate rtti and module info }
  1217. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1218. { do we need to add the variants unit? }
  1219. maybeloadvariantsunit;
  1220. { generate wrappers for interfaces }
  1221. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable,false);
  1222. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  1223. { generate pic helpers to load eip if necessary }
  1224. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1225. { generate rtti/init tables }
  1226. write_persistent_type_info(current_module.globalsymtable);
  1227. write_persistent_type_info(current_module.localsymtable);
  1228. { Tables }
  1229. InsertThreadvars;
  1230. { Resource strings }
  1231. GenerateResourceStrings;
  1232. { Widestring typed constants }
  1233. InsertWideInits;
  1234. { generate debuginfo }
  1235. if (cs_debuginfo in current_settings.moduleswitches) then
  1236. current_debuginfo.inserttypeinfo;
  1237. { generate imports }
  1238. if current_module.ImportLibraryList.Count>0 then
  1239. importlib.generatelib;
  1240. { insert own objectfile, or say that it's in a library
  1241. (no check for an .o when loading) }
  1242. ag:=is_assembler_generated;
  1243. if ag then
  1244. insertobjectfile
  1245. else
  1246. begin
  1247. current_module.flags:=current_module.flags or uf_no_link;
  1248. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1249. end;
  1250. if ag then
  1251. begin
  1252. { create callframe info }
  1253. create_dwarf_frame;
  1254. { assemble }
  1255. create_objectfile;
  1256. end;
  1257. { Write out the ppufile after the object file has been created }
  1258. store_interface_crc:=current_module.interface_crc;
  1259. store_indirect_crc:=current_module.indirect_crc;
  1260. {$ifdef EXTDEBUG}
  1261. store_crc:=current_module.crc;
  1262. {$endif EXTDEBUG}
  1263. if (Errorcount=0) then
  1264. tppumodule(current_module).writeppu;
  1265. if not(cs_compilesystem in current_settings.moduleswitches) then
  1266. begin
  1267. if store_interface_crc<>current_module.interface_crc then
  1268. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1269. if store_indirect_crc<>current_module.indirect_crc then
  1270. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1271. end;
  1272. {$ifdef EXTDEBUG}
  1273. if not(cs_compilesystem in current_settings.moduleswitches) then
  1274. if (store_crc<>current_module.crc) and simplify_ppu then
  1275. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1276. {$endif EXTDEBUG}
  1277. { release local symtables that are not needed anymore }
  1278. free_localsymtables(current_module.globalsymtable);
  1279. free_localsymtables(current_module.localsymtable);
  1280. { leave when we got an error }
  1281. if (Errorcount>0) and not status.skip_error then
  1282. begin
  1283. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1284. status.skip_error:=true;
  1285. exit;
  1286. end;
  1287. {$ifdef debug_devirt}
  1288. { print out all instantiated class/object types }
  1289. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1290. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1291. begin
  1292. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1293. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1294. objectdef:
  1295. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1296. odt_object:
  1297. writeln(' (object)');
  1298. odt_class:
  1299. writeln(' (class)');
  1300. else
  1301. internalerror(2008101103);
  1302. end;
  1303. else
  1304. internalerror(2008101104);
  1305. end;
  1306. end;
  1307. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1308. begin
  1309. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1310. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1311. objectdef:
  1312. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1313. odt_class:
  1314. writeln(' (classrefdef)');
  1315. else
  1316. internalerror(2008101105);
  1317. end
  1318. else
  1319. internalerror(2008101102);
  1320. end;
  1321. end;
  1322. {$endif debug_devirt}
  1323. Message1(unit_u_finished_compiling,current_module.modulename^);
  1324. end;
  1325. procedure procexport(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.exportprocedure(hp);
  1333. end;
  1334. procedure varexport(const s : string);
  1335. var
  1336. hp : texported_item;
  1337. begin
  1338. hp:=texported_item.create;
  1339. hp.name:=stringdup(s);
  1340. hp.options:=hp.options or eo_name;
  1341. exportlib.exportvar(hp);
  1342. end;
  1343. procedure insert_export(sym : TObject;arg:pointer);
  1344. var
  1345. i : longint;
  1346. item : TCmdStrListItem;
  1347. begin
  1348. case TSym(sym).typ of
  1349. { ignore: }
  1350. unitsym,
  1351. syssym,
  1352. constsym,
  1353. enumsym,
  1354. typesym:
  1355. ;
  1356. procsym:
  1357. begin
  1358. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1359. begin
  1360. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1361. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1362. ((tsymtable(arg).symtabletype=globalsymtable) or
  1363. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1364. ) then
  1365. begin
  1366. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1367. { walk through all aliases }
  1368. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1369. while assigned(item) do
  1370. begin
  1371. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1372. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1373. procexport(item.str);
  1374. item:=TCmdStrListItem(item.next);
  1375. end;
  1376. end;
  1377. end;
  1378. end;
  1379. staticvarsym:
  1380. begin
  1381. varexport(tsym(sym).mangledname);
  1382. end;
  1383. else
  1384. begin
  1385. writeln('unknown: ',ord(TSym(sym).typ));
  1386. end;
  1387. end;
  1388. end;
  1389. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1390. Var
  1391. MakeStatic : Boolean;
  1392. Var
  1393. buffer : array[0..$1fff] of byte;
  1394. inppu,
  1395. outppu : tppufile;
  1396. b,
  1397. untilb : byte;
  1398. l,m : longint;
  1399. f : file;
  1400. ext,
  1401. s : string;
  1402. ppuversion : dword;
  1403. begin
  1404. Result:=false;
  1405. MakeStatic:=False;
  1406. inppu:=tppufile.create(PPUFn);
  1407. if not inppu.openfile then
  1408. begin
  1409. inppu.free;
  1410. Comment(V_Error,'Could not open : '+PPUFn);
  1411. Exit;
  1412. end;
  1413. { Check the ppufile }
  1414. if not inppu.CheckPPUId then
  1415. begin
  1416. inppu.free;
  1417. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1418. Exit;
  1419. end;
  1420. ppuversion:=inppu.GetPPUVersion;
  1421. if ppuversion<CurrentPPUVersion then
  1422. begin
  1423. inppu.free;
  1424. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1425. Exit;
  1426. end;
  1427. { No .o file generated for this ppu, just skip }
  1428. if (inppu.header.flags and uf_no_link)<>0 then
  1429. begin
  1430. inppu.free;
  1431. Result:=true;
  1432. Exit;
  1433. end;
  1434. { Already a lib? }
  1435. if (inppu.header.flags and uf_in_library)<>0 then
  1436. begin
  1437. inppu.free;
  1438. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1439. Exit;
  1440. end;
  1441. { We need a static linked unit }
  1442. if (inppu.header.flags and uf_static_linked)=0 then
  1443. begin
  1444. inppu.free;
  1445. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1446. Exit;
  1447. end;
  1448. { Check if shared is allowed }
  1449. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1450. begin
  1451. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1452. MakeStatic:=true;
  1453. end;
  1454. { Create the new ppu }
  1455. if PPUFn=PPLFn then
  1456. outppu:=tppufile.create('ppumove.$$$')
  1457. else
  1458. outppu:=tppufile.create(PPLFn);
  1459. outppu.createfile;
  1460. { Create new header, with the new flags }
  1461. outppu.header:=inppu.header;
  1462. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1463. if MakeStatic then
  1464. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1465. else
  1466. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1467. { read until the object files are found }
  1468. untilb:=iblinkunitofiles;
  1469. repeat
  1470. b:=inppu.readentry;
  1471. if b in [ibendinterface,ibend] then
  1472. begin
  1473. inppu.free;
  1474. outppu.free;
  1475. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1476. Exit;
  1477. end;
  1478. if b<>untilb then
  1479. begin
  1480. repeat
  1481. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1482. outppu.putdata(buffer,l);
  1483. until l<sizeof(buffer);
  1484. outppu.writeentry(b);
  1485. end;
  1486. until (b=untilb);
  1487. { we have now reached the section for the files which need to be added,
  1488. now add them to the list }
  1489. case b of
  1490. iblinkunitofiles :
  1491. begin
  1492. { add all o files, and save the entry when not creating a static
  1493. library to keep staticlinking possible }
  1494. while not inppu.endofentry do
  1495. begin
  1496. s:=inppu.getstring;
  1497. m:=inppu.getlongint;
  1498. if not MakeStatic then
  1499. begin
  1500. outppu.putstring(s);
  1501. outppu.putlongint(m);
  1502. end;
  1503. current_module.linkotherofiles.add(s,link_always);;
  1504. end;
  1505. if not MakeStatic then
  1506. outppu.writeentry(b);
  1507. end;
  1508. { iblinkunitstaticlibs :
  1509. begin
  1510. AddToLinkFiles(ExtractLib(inppu.getstring));
  1511. if not inppu.endofentry then
  1512. begin
  1513. repeat
  1514. inppu.getdatabuf(buffer^,bufsize,l);
  1515. outppu.putdata(buffer^,l);
  1516. until l<bufsize;
  1517. outppu.writeentry(b);
  1518. end;
  1519. end; }
  1520. end;
  1521. { just add a new entry with the new lib }
  1522. if MakeStatic then
  1523. begin
  1524. outppu.putstring('imp'+current_module.realmodulename^);
  1525. outppu.putlongint(link_static);
  1526. outppu.writeentry(iblinkunitstaticlibs)
  1527. end
  1528. else
  1529. begin
  1530. outppu.putstring('imp'+current_module.realmodulename^);
  1531. outppu.putlongint(link_shared);
  1532. outppu.writeentry(iblinkunitsharedlibs);
  1533. end;
  1534. { read all entries until the end and write them also to the new ppu }
  1535. repeat
  1536. b:=inppu.readentry;
  1537. { don't write ibend, that's written automatically }
  1538. if b<>ibend then
  1539. begin
  1540. if b=iblinkothersharedlibs then
  1541. begin
  1542. while not inppu.endofentry do
  1543. begin
  1544. s:=inppu.getstring;
  1545. m:=inppu.getlongint;
  1546. outppu.putstring(s);
  1547. outppu.putlongint(m);
  1548. { strip lib prefix }
  1549. if copy(s,1,3)='lib' then
  1550. delete(s,1,3);
  1551. ext:=ExtractFileExt(s);
  1552. if ext<>'' then
  1553. delete(s,length(s)-length(ext)+1,length(ext));
  1554. current_module.linkOtherSharedLibs.add(s,link_always);
  1555. end;
  1556. end
  1557. else
  1558. repeat
  1559. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1560. outppu.putdata(buffer,l);
  1561. until l<sizeof(buffer);
  1562. outppu.writeentry(b);
  1563. end;
  1564. until b=ibend;
  1565. { write the last stuff and close }
  1566. outppu.flush;
  1567. outppu.writeheader;
  1568. outppu.free;
  1569. inppu.free;
  1570. { rename }
  1571. if PPUFn=PPLFn then
  1572. begin
  1573. {$I-}
  1574. assign(f,PPUFn);
  1575. erase(f);
  1576. assign(f,'ppumove.$$$');
  1577. rename(f,PPUFn);
  1578. {$I+}
  1579. if ioresult<>0 then;
  1580. end;
  1581. Result:=True;
  1582. end;
  1583. procedure createimportlibfromexports;
  1584. var
  1585. hp : texported_item;
  1586. begin
  1587. hp:=texported_item(current_module._exports.first);
  1588. while assigned(hp) do
  1589. begin
  1590. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.index,hp.is_var,false);
  1591. hp:=texported_item(hp.next);
  1592. end;
  1593. end;
  1594. procedure proc_package;
  1595. var
  1596. main_file : tinputfile;
  1597. hp,hp2 : tmodule;
  1598. {finalize_procinfo,
  1599. init_procinfo,
  1600. main_procinfo : tcgprocinfo;}
  1601. force_init_final : boolean;
  1602. uu : tused_unit;
  1603. begin
  1604. Status.IsPackage:=true;
  1605. Status.IsExe:=true;
  1606. parse_only:=false;
  1607. {main_procinfo:=nil;
  1608. init_procinfo:=nil;
  1609. finalize_procinfo:=nil;}
  1610. if not RelocSectionSetExplicitly then
  1611. RelocSection:=true;
  1612. { Relocation works only without stabs under Windows when }
  1613. { external linker (LD) is used. LD generates relocs for }
  1614. { stab sections which is not loaded in memory. It causes }
  1615. { AV error when DLL is loaded and relocation is needed. }
  1616. { Internal linker does not have this problem. }
  1617. if RelocSection and
  1618. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1619. (cs_link_extern in current_settings.globalswitches) then
  1620. begin
  1621. include(current_settings.globalswitches,cs_link_strip);
  1622. { Warning stabs info does not work with reloc section !! }
  1623. if (cs_debuginfo in current_settings.moduleswitches) and
  1624. (target_dbg.id=dbg_stabs) then
  1625. begin
  1626. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1627. Message(parser_w_parser_win32_debug_needs_WN);
  1628. exclude(current_settings.moduleswitches,cs_debuginfo);
  1629. end;
  1630. end;
  1631. { get correct output names }
  1632. main_file := current_scanner.inputfile;
  1633. while assigned(main_file.next) do
  1634. main_file := main_file.next;
  1635. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1636. consume(_ID);
  1637. current_module.setmodulename(orgpattern);
  1638. current_module.ispackage:=true;
  1639. exportlib.preparelib(orgpattern);
  1640. if tf_library_needs_pic in target_info.flags then
  1641. include(current_settings.moduleswitches,cs_create_pic);
  1642. consume(_ID);
  1643. consume(_SEMICOLON);
  1644. { global switches are read, so further changes aren't allowed }
  1645. current_module.in_global:=false;
  1646. { setup things using the switches }
  1647. setupglobalswitches;
  1648. { set implementation flag }
  1649. current_module.in_interface:=false;
  1650. current_module.interface_compiled:=true;
  1651. { insert after the unit symbol tables the static symbol table }
  1652. { of the program }
  1653. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1654. {Load the units used by the program we compile.}
  1655. if token=_REQUIRES then
  1656. begin
  1657. end;
  1658. {Load the units used by the program we compile.}
  1659. if (token=_ID) and (idtoken=_CONTAINS) then
  1660. begin
  1661. consume(_ID);
  1662. while true do
  1663. begin
  1664. if token=_ID then
  1665. AddUnit(pattern);
  1666. consume(_ID);
  1667. if token=_COMMA then
  1668. consume(_COMMA)
  1669. else break;
  1670. end;
  1671. consume(_SEMICOLON);
  1672. end;
  1673. { All units are read, now give them a number }
  1674. current_module.updatemaps;
  1675. {Insert the name of the main program into the symbol table.}
  1676. if current_module.realmodulename^<>'' then
  1677. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1678. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1679. symtablestack.push(current_module.localsymtable);
  1680. { create whole program optimisation information }
  1681. current_module.wpoinfo:=tunitwpoinfo.create;
  1682. { should we force unit initialization? }
  1683. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1684. if force_init_final then
  1685. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1686. { Add symbol to the exports section for win32 so smartlinking a
  1687. DLL will include the edata section }
  1688. if assigned(exportlib) and
  1689. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1690. ((current_module.flags and uf_has_exports)<>0) then
  1691. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1692. { all labels must be defined before generating code }
  1693. if Errorcount=0 then
  1694. tstoredsymtable(current_module.localsymtable).checklabels;
  1695. symtablestack.pop(current_module.localsymtable);
  1696. { consume the last point }
  1697. consume(_END);
  1698. consume(_POINT);
  1699. if (Errorcount=0) then
  1700. begin
  1701. { test static symtable }
  1702. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1703. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1704. tstoredsymtable(current_module.localsymtable).check_forwards;
  1705. current_module.allunitsused;
  1706. end;
  1707. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1708. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1709. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1710. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1711. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1712. {$ifdef i386}
  1713. { fix me! }
  1714. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1715. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const(A_RET,S_W,12));
  1716. {$endif i386}
  1717. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1718. { leave when we got an error }
  1719. if (Errorcount>0) and not status.skip_error then
  1720. begin
  1721. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1722. status.skip_error:=true;
  1723. exit;
  1724. end;
  1725. { remove all unused units, this happends when units are removed
  1726. from the uses clause in the source and the ppu was already being loaded }
  1727. hp:=tmodule(loaded_units.first);
  1728. while assigned(hp) do
  1729. begin
  1730. hp2:=hp;
  1731. hp:=tmodule(hp.next);
  1732. if hp2.is_unit and
  1733. not assigned(hp2.globalsymtable) then
  1734. loaded_units.remove(hp2);
  1735. end;
  1736. { force exports }
  1737. uu:=tused_unit(usedunits.first);
  1738. while assigned(uu) do
  1739. begin
  1740. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1741. { check localsymtable for exports too to get public symbols }
  1742. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1743. { create special exports }
  1744. if (uu.u.flags and uf_init)<>0 then
  1745. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1746. if (uu.u.flags and uf_finalize)<>0 then
  1747. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1748. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1749. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1750. uu:=tused_unit(uu.next);
  1751. end;
  1752. {$ifdef arm}
  1753. { Insert .pdata section for arm-wince.
  1754. It is needed for exception handling. }
  1755. if target_info.system in [system_arm_wince] then
  1756. InsertPData;
  1757. {$endif arm}
  1758. { generate debuginfo }
  1759. if (cs_debuginfo in current_settings.moduleswitches) then
  1760. current_debuginfo.inserttypeinfo;
  1761. exportlib.generatelib;
  1762. { write all our exports to the import library,
  1763. needs to be done after exportlib.generatelib; }
  1764. createimportlibfromexports;
  1765. { generate imports }
  1766. if current_module.ImportLibraryList.Count>0 then
  1767. importlib.generatelib;
  1768. { Reference all DEBUGINFO sections from the main .fpc section }
  1769. if (cs_debuginfo in current_settings.moduleswitches) then
  1770. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1771. { insert own objectfile }
  1772. insertobjectfile;
  1773. { assemble and link }
  1774. create_objectfile;
  1775. { We might need the symbols info if not using
  1776. the default do_extractsymbolinfo
  1777. which is a dummy function PM }
  1778. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1779. { release all local symtables that are not needed anymore }
  1780. if (not needsymbolinfo) then
  1781. free_localsymtables(current_module.localsymtable);
  1782. { leave when we got an error }
  1783. if (Errorcount>0) and not status.skip_error then
  1784. begin
  1785. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1786. status.skip_error:=true;
  1787. exit;
  1788. end;
  1789. if (not current_module.is_unit) then
  1790. begin
  1791. { finally rewrite all units included into the package }
  1792. uu:=tused_unit(usedunits.first);
  1793. while assigned(uu) do
  1794. begin
  1795. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1796. uu:=tused_unit(uu.next);
  1797. end;
  1798. { create the executable when we are at level 1 }
  1799. if (compile_level=1) then
  1800. begin
  1801. { create global resource file by collecting all resource files }
  1802. CollectResourceFiles;
  1803. { write .def file }
  1804. if (cs_link_deffile in current_settings.globalswitches) then
  1805. deffile.writefile;
  1806. { insert all .o files from all loaded units and
  1807. unload the units, we don't need them anymore.
  1808. Keep the current_module because that is still needed }
  1809. hp:=tmodule(loaded_units.first);
  1810. while assigned(hp) do
  1811. begin
  1812. { the package itself contains no code so far }
  1813. linker.AddModuleFiles(hp);
  1814. hp2:=tmodule(hp.next);
  1815. if (hp<>current_module) and
  1816. (not needsymbolinfo) then
  1817. begin
  1818. loaded_units.remove(hp);
  1819. hp.free;
  1820. end;
  1821. hp:=hp2;
  1822. end;
  1823. linker.MakeSharedLibrary
  1824. end;
  1825. { Give Fatal with error count for linker errors }
  1826. if (Errorcount>0) and not status.skip_error then
  1827. begin
  1828. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1829. status.skip_error:=true;
  1830. end;
  1831. end;
  1832. end;
  1833. procedure proc_program(islibrary : boolean);
  1834. var
  1835. main_file : tinputfile;
  1836. hp,hp2 : tmodule;
  1837. finalize_procinfo,
  1838. init_procinfo,
  1839. main_procinfo : tcgprocinfo;
  1840. force_init_final : boolean;
  1841. resources_used : boolean;
  1842. begin
  1843. DLLsource:=islibrary;
  1844. Status.IsLibrary:=IsLibrary;
  1845. Status.IsPackage:=false;
  1846. Status.IsExe:=true;
  1847. parse_only:=false;
  1848. main_procinfo:=nil;
  1849. init_procinfo:=nil;
  1850. finalize_procinfo:=nil;
  1851. resources_used:=false;
  1852. { DLL defaults to create reloc info }
  1853. if islibrary then
  1854. begin
  1855. if not RelocSectionSetExplicitly then
  1856. RelocSection:=true;
  1857. end;
  1858. { Relocation works only without stabs under Windows when }
  1859. { external linker (LD) is used. LD generates relocs for }
  1860. { stab sections which is not loaded in memory. It causes }
  1861. { AV error when DLL is loaded and relocation is needed. }
  1862. { Internal linker does not have this problem. }
  1863. if RelocSection and
  1864. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1865. (cs_link_extern in current_settings.globalswitches) then
  1866. begin
  1867. include(current_settings.globalswitches,cs_link_strip);
  1868. { Warning stabs info does not work with reloc section !! }
  1869. if (cs_debuginfo in current_settings.moduleswitches) and
  1870. (target_dbg.id=dbg_stabs) then
  1871. begin
  1872. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1873. Message(parser_w_parser_win32_debug_needs_WN);
  1874. exclude(current_settings.moduleswitches,cs_debuginfo);
  1875. end;
  1876. end;
  1877. { get correct output names }
  1878. main_file := current_scanner.inputfile;
  1879. while assigned(main_file.next) do
  1880. main_file := main_file.next;
  1881. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1882. if islibrary then
  1883. begin
  1884. consume(_LIBRARY);
  1885. current_module.setmodulename(orgpattern);
  1886. current_module.islibrary:=true;
  1887. exportlib.preparelib(orgpattern);
  1888. if tf_library_needs_pic in target_info.flags then
  1889. include(current_settings.moduleswitches,cs_create_pic);
  1890. consume(_ID);
  1891. consume(_SEMICOLON);
  1892. end
  1893. else
  1894. { is there an program head ? }
  1895. if token=_PROGRAM then
  1896. begin
  1897. consume(_PROGRAM);
  1898. current_module.setmodulename(orgpattern);
  1899. if (target_info.system in systems_unit_program_exports) then
  1900. exportlib.preparelib(orgpattern);
  1901. consume(_ID);
  1902. if token=_LKLAMMER then
  1903. begin
  1904. consume(_LKLAMMER);
  1905. repeat
  1906. consume(_ID);
  1907. until not try_to_consume(_COMMA);
  1908. consume(_RKLAMMER);
  1909. end;
  1910. consume(_SEMICOLON);
  1911. end
  1912. else if (target_info.system in systems_unit_program_exports) then
  1913. exportlib.preparelib(current_module.realmodulename^);
  1914. { global switches are read, so further changes aren't allowed }
  1915. current_module.in_global:=false;
  1916. { setup things using the switches }
  1917. setupglobalswitches;
  1918. { set implementation flag }
  1919. current_module.in_interface:=false;
  1920. current_module.interface_compiled:=true;
  1921. { insert after the unit symbol tables the static symbol table }
  1922. { of the program }
  1923. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1924. { load standard units (system,objpas,profile unit) }
  1925. loaddefaultunits;
  1926. { Load units provided on the command line }
  1927. loadautounits;
  1928. {Load the units used by the program we compile.}
  1929. if token=_USES then
  1930. loadunits;
  1931. { All units are read, now give them a number }
  1932. current_module.updatemaps;
  1933. {Insert the name of the main program into the symbol table.}
  1934. if current_module.realmodulename^<>'' then
  1935. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1936. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1937. symtablestack.push(current_module.localsymtable);
  1938. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1939. maybe_load_got;
  1940. { create whole program optimisation information }
  1941. current_module.wpoinfo:=tunitwpoinfo.create;
  1942. { The program intialization needs an alias, so it can be called
  1943. from the bootstrap code.}
  1944. if islibrary then
  1945. begin
  1946. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1947. { Win32 startup code needs a single name }
  1948. if not(target_info.system in systems_darwin) then
  1949. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1950. else
  1951. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1952. end
  1953. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1954. begin
  1955. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1956. end
  1957. else
  1958. begin
  1959. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1960. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1961. end;
  1962. main_procinfo.parse_body;
  1963. { save file pos for debuginfo }
  1964. current_module.mainfilepos:=main_procinfo.entrypos;
  1965. { Generate specializations of objectdefs methods }
  1966. generate_specialization_procs;
  1967. { should we force unit initialization? }
  1968. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1969. if force_init_final then
  1970. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1971. { Add symbol to the exports section for win32 so smartlinking a
  1972. DLL will include the edata section }
  1973. if assigned(exportlib) and
  1974. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1975. ((current_module.flags and uf_has_exports)<>0) then
  1976. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1977. { finalize? }
  1978. if token=_FINALIZATION then
  1979. begin
  1980. { the uf_finalize flag is only set after we checked that it
  1981. wasn't empty }
  1982. { Parse the finalize }
  1983. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1984. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1985. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1986. finalize_procinfo.parse_body;
  1987. end
  1988. else
  1989. if force_init_final then
  1990. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1991. { the finalization routine of libraries is generic (and all libraries need to }
  1992. { be finalized, so they can finalize any units they use }
  1993. if (islibrary) then
  1994. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1995. { all labels must be defined before generating code }
  1996. if Errorcount=0 then
  1997. tstoredsymtable(current_module.localsymtable).checklabels;
  1998. { See remark in unit init/final }
  1999. main_procinfo.generate_code;
  2000. main_procinfo.resetprocdef;
  2001. release_main_proc(main_procinfo);
  2002. if assigned(init_procinfo) then
  2003. begin
  2004. init_procinfo.generate_code;
  2005. init_procinfo.resetprocdef;
  2006. release_main_proc(init_procinfo);
  2007. end;
  2008. if assigned(finalize_procinfo) then
  2009. begin
  2010. finalize_procinfo.generate_code;
  2011. finalize_procinfo.resetprocdef;
  2012. release_main_proc(finalize_procinfo);
  2013. end;
  2014. symtablestack.pop(current_module.localsymtable);
  2015. { consume the last point }
  2016. consume(_POINT);
  2017. { reset wpo flags for all defs }
  2018. reset_all_defs;
  2019. if (Errorcount=0) then
  2020. begin
  2021. { test static symtable }
  2022. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2023. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2024. tstoredsymtable(current_module.localsymtable).check_forwards;
  2025. current_module.allunitsused;
  2026. end;
  2027. { leave when we got an error }
  2028. if (Errorcount>0) and not status.skip_error then
  2029. begin
  2030. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2031. status.skip_error:=true;
  2032. exit;
  2033. end;
  2034. { remove all unused units, this happens when units are removed
  2035. from the uses clause in the source and the ppu was already being loaded }
  2036. hp:=tmodule(loaded_units.first);
  2037. while assigned(hp) do
  2038. begin
  2039. hp2:=hp;
  2040. hp:=tmodule(hp.next);
  2041. if hp2.is_unit and
  2042. not assigned(hp2.globalsymtable) then
  2043. begin
  2044. loaded_units.remove(hp2);
  2045. unloaded_units.concat(hp2);
  2046. end;
  2047. end;
  2048. { do we need to add the variants unit? }
  2049. maybeloadvariantsunit;
  2050. { Now that everything has been compiled we know if we need resource
  2051. support. If not, remove the unit. }
  2052. resources_used:=MaybeRemoveResUnit;
  2053. linker.initsysinitunitname;
  2054. if target_info.system in systems_internal_sysinit then
  2055. begin
  2056. { add start/halt unit }
  2057. AddUnit(linker.sysinitunit);
  2058. end;
  2059. {$ifdef arm}
  2060. { Insert .pdata section for arm-wince.
  2061. It is needed for exception handling. }
  2062. if target_info.system in [system_arm_wince] then
  2063. InsertPData;
  2064. {$endif arm}
  2065. InsertThreadvars;
  2066. { generate pic helpers to load eip if necessary }
  2067. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  2068. { generate rtti/init tables }
  2069. write_persistent_type_info(current_module.localsymtable);
  2070. { if an Objective-C module, generate rtti and module info }
  2071. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2072. { generate wrappers for interfaces }
  2073. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  2074. { generate imports }
  2075. if current_module.ImportLibraryList.Count>0 then
  2076. importlib.generatelib;
  2077. { generate debuginfo }
  2078. if (cs_debuginfo in current_settings.moduleswitches) then
  2079. current_debuginfo.inserttypeinfo;
  2080. if islibrary or (target_info.system in systems_unit_program_exports) then
  2081. exportlib.generatelib;
  2082. { Reference all DEBUGINFO sections from the main .fpc section }
  2083. if (cs_debuginfo in current_settings.moduleswitches) then
  2084. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2085. { Resource strings }
  2086. GenerateResourceStrings;
  2087. { Windows widestring needing initialization }
  2088. InsertWideInits;
  2089. { insert Tables and StackLength }
  2090. InsertInitFinalTable;
  2091. InsertThreadvarTablesTable;
  2092. InsertResourceTablesTable;
  2093. InsertWideInitsTablesTable;
  2094. InsertMemorySizes;
  2095. { Insert symbol to resource info }
  2096. InsertResourceInfo(resources_used);
  2097. { create callframe info }
  2098. create_dwarf_frame;
  2099. { insert own objectfile }
  2100. insertobjectfile;
  2101. { assemble and link }
  2102. create_objectfile;
  2103. { We might need the symbols info if not using
  2104. the default do_extractsymbolinfo
  2105. which is a dummy function PM }
  2106. needsymbolinfo:=
  2107. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2108. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2109. { release all local symtables that are not needed anymore }
  2110. if (not needsymbolinfo) then
  2111. free_localsymtables(current_module.localsymtable);
  2112. { leave when we got an error }
  2113. if (Errorcount>0) and not status.skip_error then
  2114. begin
  2115. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2116. status.skip_error:=true;
  2117. exit;
  2118. end;
  2119. if (not current_module.is_unit) then
  2120. begin
  2121. { create the executable when we are at level 1 }
  2122. if (compile_level=1) then
  2123. begin
  2124. { create global resource file by collecting all resource files }
  2125. CollectResourceFiles;
  2126. { write .def file }
  2127. if (cs_link_deffile in current_settings.globalswitches) then
  2128. deffile.writefile;
  2129. { insert all .o files from all loaded units and
  2130. unload the units, we don't need them anymore.
  2131. Keep the current_module because that is still needed }
  2132. hp:=tmodule(loaded_units.first);
  2133. while assigned(hp) do
  2134. begin
  2135. linker.AddModuleFiles(hp);
  2136. hp2:=tmodule(hp.next);
  2137. if (hp<>current_module) and
  2138. (not needsymbolinfo) then
  2139. begin
  2140. loaded_units.remove(hp);
  2141. hp.free;
  2142. end;
  2143. hp:=hp2;
  2144. end;
  2145. { free also unneeded units we didn't free before }
  2146. if not needsymbolinfo then
  2147. unloaded_units.Clear;
  2148. { finally we can create a executable }
  2149. if DLLSource then
  2150. linker.MakeSharedLibrary
  2151. else
  2152. linker.MakeExecutable;
  2153. { collect all necessary information for whole-program optimization }
  2154. wpoinfomanager.extractwpoinfofromprogram;
  2155. end;
  2156. { Give Fatal with error count for linker errors }
  2157. if (Errorcount>0) and not status.skip_error then
  2158. begin
  2159. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2160. status.skip_error:=true;
  2161. end;
  2162. end;
  2163. end;
  2164. end.