pmodules.pas 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  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. procedure create_objectfile;
  42. var
  43. DLLScanner : TDLLScanner;
  44. s : string;
  45. KeepShared : TCmdStrList;
  46. begin
  47. { try to create import entries from system dlls }
  48. if (tf_has_dllscanner in target_info.flags) and
  49. (not current_module.linkOtherSharedLibs.Empty) then
  50. begin
  51. { Init DLLScanner }
  52. if assigned(CDLLScanner[target_info.system]) then
  53. DLLScanner:=CDLLScanner[target_info.system].Create
  54. else
  55. internalerror(200104121);
  56. KeepShared:=TCmdStrList.Create;
  57. { Walk all shared libs }
  58. While not current_module.linkOtherSharedLibs.Empty do
  59. begin
  60. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  61. if not DLLScanner.scan(s) then
  62. KeepShared.Concat(s);
  63. end;
  64. DLLscanner.Free;
  65. { Recreate import section }
  66. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  67. begin
  68. if assigned(current_asmdata.asmlists[al_imports]) then
  69. current_asmdata.asmlists[al_imports].clear
  70. else
  71. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  72. importlib.generatelib;
  73. end;
  74. { Readd the not processed files }
  75. while not KeepShared.Empty do
  76. begin
  77. s:=KeepShared.GetFirst;
  78. current_module.linkOtherSharedLibs.add(s,link_always);
  79. end;
  80. KeepShared.Free;
  81. end;
  82. { Start and end module debuginfo, at least required for stabs
  83. to insert n_sourcefile lines }
  84. if (cs_debuginfo in current_settings.moduleswitches) or
  85. (cs_use_lineinfo in current_settings.globalswitches) then
  86. current_debuginfo.insertmoduleinfo;
  87. { create the .s file and assemble it }
  88. GenerateAsm(false);
  89. { Also create a smartlinked version ? }
  90. if create_smartlink_library then
  91. begin
  92. GenerateAsm(true);
  93. if (af_needar in target_asm.flags) then
  94. Linker.MakeStaticLibrary;
  95. end;
  96. { resource files }
  97. CompileResourceFiles;
  98. end;
  99. procedure insertobjectfile;
  100. { Insert the used object file for this unit in the used list for this unit }
  101. begin
  102. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  103. current_module.flags:=current_module.flags or uf_static_linked;
  104. if create_smartlink_library then
  105. begin
  106. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  107. current_module.flags:=current_module.flags or uf_smart_linked;
  108. end;
  109. end;
  110. procedure create_dwarf_frame;
  111. begin
  112. { Dwarf conflicts with smartlinking in separate .a files }
  113. if create_smartlink_library then
  114. exit;
  115. { Call frame information }
  116. { MWE: we write our own info, so dwarf asm support is not really needed }
  117. { if (af_supports_dwarf in target_asm.flags) and }
  118. { CFI is currently broken for Darwin }
  119. if not(target_info.system in systems_darwin) and
  120. (
  121. (tf_needs_dwarf_cfi in target_info.flags) or
  122. (paratargetdbg in [dbg_dwarf2, dbg_dwarf3])
  123. ) then
  124. begin
  125. current_asmdata.asmlists[al_dwarf_frame].Free;
  126. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  127. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  128. end;
  129. end;
  130. procedure InsertThreadvarTablesTable;
  131. var
  132. hp : tused_unit;
  133. ltvTables : TAsmList;
  134. count : longint;
  135. begin
  136. if (tf_section_threadvars in target_info.flags) then
  137. exit;
  138. ltvTables:=TAsmList.Create;
  139. count:=0;
  140. hp:=tused_unit(usedunits.first);
  141. while assigned(hp) do
  142. begin
  143. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  144. begin
  145. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  146. inc(count);
  147. end;
  148. hp:=tused_unit(hp.next);
  149. end;
  150. { Add program threadvars, if any }
  151. If (current_module.flags and uf_threadvars)=uf_threadvars then
  152. begin
  153. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  154. inc(count);
  155. end;
  156. { Insert TableCount at start }
  157. ltvTables.insert(Tai_const.Create_32bit(count));
  158. { insert in data segment }
  159. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  160. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(pint));
  161. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  162. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  163. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  164. ltvTables.free;
  165. end;
  166. procedure AddToThreadvarList(p:TObject;arg:pointer);
  167. var
  168. ltvTable : TAsmList;
  169. begin
  170. ltvTable:=TAsmList(arg);
  171. if (tsym(p).typ=staticvarsym) and
  172. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  173. begin
  174. { address of threadvar }
  175. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  176. { size of threadvar }
  177. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  178. end;
  179. end;
  180. procedure InsertThreadvars;
  181. var
  182. s : string;
  183. ltvTable : TAsmList;
  184. begin
  185. if (tf_section_threadvars in target_info.flags) then
  186. exit;
  187. ltvTable:=TAsmList.create;
  188. if assigned(current_module.globalsymtable) then
  189. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  190. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  191. if ltvTable.first<>nil then
  192. begin
  193. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  194. { end of the list marker }
  195. ltvTable.concat(tai_const.create_sym(nil));
  196. { add to datasegment }
  197. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  198. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  199. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  200. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  201. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  202. current_module.flags:=current_module.flags or uf_threadvars;
  203. end;
  204. ltvTable.Free;
  205. end;
  206. procedure InsertWideInits;
  207. var
  208. s: string;
  209. item: TTCInitItem;
  210. begin
  211. item:=TTCInitItem(current_asmdata.WideInits.First);
  212. if item=nil then
  213. exit;
  214. s:=make_mangledname('WIDEINITS',current_module.localsymtable,'');
  215. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  216. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  217. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  218. repeat
  219. { optimize away unused local/static symbols }
  220. if (item.sym.refs>0) or (item.sym.owner.symtabletype=globalsymtable) then
  221. begin
  222. { address to initialize }
  223. current_asmdata.asmlists[al_globals].concat(Tai_const.createname(item.sym.mangledname, item.offset));
  224. { value with which to initialize }
  225. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(item.datalabel));
  226. end;
  227. item:=TTCInitItem(item.Next);
  228. until item=nil;
  229. { end-of-list marker }
  230. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  231. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  232. current_module.flags:=current_module.flags or uf_wideinits;
  233. end;
  234. procedure InsertWideInitsTablesTable;
  235. var
  236. hp: tused_unit;
  237. lwiTables: TAsmList;
  238. count: longint;
  239. begin
  240. lwiTables:=TAsmList.Create;
  241. count:=0;
  242. hp:=tused_unit(usedunits.first);
  243. while assigned(hp) do
  244. begin
  245. if (hp.u.flags and uf_wideinits)=uf_wideinits then
  246. begin
  247. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',hp.u.globalsymtable,''),0));
  248. inc(count);
  249. end;
  250. hp:=tused_unit(hp.next);
  251. end;
  252. { Add program widestring consts, if any }
  253. if (current_module.flags and uf_wideinits)=uf_wideinits then
  254. begin
  255. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',current_module.localsymtable,''),0));
  256. inc(count);
  257. end;
  258. { Insert TableCount at start }
  259. lwiTables.insert(Tai_const.Create_32bit(count));
  260. { insert in data segment }
  261. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  262. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_WIDEINITTABLES',sizeof(pint));
  263. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_WIDEINITTABLES',AT_DATA,0));
  264. current_asmdata.asmlists[al_globals].concatlist(lwiTables);
  265. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_WIDEINITTABLES'));
  266. lwiTables.free;
  267. end;
  268. Function CheckResourcesUsed : boolean;
  269. var
  270. hp : tused_unit;
  271. found : Boolean;
  272. begin
  273. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  274. if not CheckResourcesUsed then exit;
  275. hp:=tused_unit(usedunits.first);
  276. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  277. If not found then
  278. While Assigned(hp) and not found do
  279. begin
  280. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  281. hp:=tused_unit(hp.next);
  282. end;
  283. CheckResourcesUsed:=found;
  284. end;
  285. Procedure InsertResourceInfo(ResourcesUsed : boolean);
  286. var
  287. ResourceInfo : TAsmList;
  288. begin
  289. if (target_res.id in [res_elf,res_macho]) then
  290. begin
  291. ResourceInfo:=TAsmList.Create;
  292. maybe_new_object_file(ResourceInfo);
  293. new_section(ResourceInfo,sec_data,'FPC_RESLOCATION',sizeof(aint));
  294. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  295. if ResourcesUsed then
  296. { Valid pointer to resource information }
  297. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0))
  298. else
  299. { Nil pointer to resource information }
  300. {$IFDEF CPU32}
  301. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  302. {$ELSE}
  303. ResourceInfo.Concat(Tai_const.Create_64bit(0));
  304. {$ENDIF}
  305. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  306. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  307. ResourceInfo.free;
  308. end;
  309. end;
  310. Procedure InsertResourceTablesTable;
  311. var
  312. hp : tmodule;
  313. ResourceStringTables : tasmlist;
  314. count : longint;
  315. begin
  316. ResourceStringTables:=tasmlist.Create;
  317. count:=0;
  318. hp:=tmodule(loaded_units.first);
  319. while assigned(hp) do
  320. begin
  321. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  322. begin
  323. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  324. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  325. inc(count);
  326. end;
  327. hp:=tmodule(hp.next);
  328. end;
  329. { Insert TableCount at start }
  330. ResourceStringTables.insert(Tai_const.Create_pint(count));
  331. { Add to data segment }
  332. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  333. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(pint));
  334. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  335. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  336. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  337. ResourceStringTables.free;
  338. end;
  339. procedure AddToStructInits(p:TObject;arg:pointer);
  340. var
  341. StructList: TFPList absolute arg;
  342. begin
  343. if (tdef(p).typ in [objectdef,recorddef]) and
  344. ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  345. StructList.Add(p);
  346. end;
  347. procedure InsertInitFinalTable;
  348. var
  349. hp : tused_unit;
  350. unitinits : TAsmList;
  351. count : longint;
  352. procedure write_struct_inits(u: tmodule);
  353. var
  354. i: integer;
  355. structlist: TFPList;
  356. pd: tprocdef;
  357. begin
  358. structlist := TFPList.Create;
  359. if assigned(u.globalsymtable) then
  360. u.globalsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  361. u.localsymtable.DefList.ForEachCall(@AddToStructInits,structlist);
  362. { write structures }
  363. for i := 0 to structlist.Count - 1 do
  364. begin
  365. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_constructor);
  366. if assigned(pd) then
  367. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  368. else
  369. unitinits.concat(Tai_const.Create_pint(0));
  370. pd := tabstractrecorddef(structlist[i]).find_procdef_bytype(potype_class_destructor);
  371. if assigned(pd) then
  372. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  373. else
  374. unitinits.concat(Tai_const.Create_pint(0));
  375. inc(count);
  376. end;
  377. structlist.free;
  378. end;
  379. begin
  380. unitinits:=TAsmList.Create;
  381. count:=0;
  382. hp:=tused_unit(usedunits.first);
  383. while assigned(hp) do
  384. begin
  385. { insert class constructors/destructors of the unit }
  386. if (hp.u.flags and uf_classinits) <> 0 then
  387. write_struct_inits(hp.u);
  388. { call the unit init code and make it external }
  389. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  390. begin
  391. if (hp.u.flags and uf_init)<>0 then
  392. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  393. else
  394. unitinits.concat(Tai_const.Create_sym(nil));
  395. if (hp.u.flags and uf_finalize)<>0 then
  396. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  397. else
  398. unitinits.concat(Tai_const.Create_sym(nil));
  399. inc(count);
  400. end;
  401. hp:=tused_unit(hp.next);
  402. end;
  403. { insert class constructors/destructor of the program }
  404. if (current_module.flags and uf_classinits) <> 0 then
  405. write_struct_inits(current_module);
  406. { Insert initialization/finalization of the program }
  407. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  408. begin
  409. if (current_module.flags and uf_init)<>0 then
  410. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  411. else
  412. unitinits.concat(Tai_const.Create_sym(nil));
  413. if (current_module.flags and uf_finalize)<>0 then
  414. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  415. else
  416. unitinits.concat(Tai_const.Create_sym(nil));
  417. inc(count);
  418. end;
  419. { Insert TableCount,InitCount at start }
  420. unitinits.insert(Tai_const.Create_32bit(0));
  421. unitinits.insert(Tai_const.Create_32bit(count));
  422. { Add to data segment }
  423. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  424. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(pint));
  425. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  426. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  427. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  428. unitinits.free;
  429. end;
  430. procedure InsertMemorySizes;
  431. {$IFDEF POWERPC}
  432. var
  433. stkcookie: string;
  434. {$ENDIF POWERPC}
  435. begin
  436. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  437. { Insert Ident of the compiler in the .fpc.version section }
  438. new_section(current_asmdata.asmlists[al_globals],sec_fpc,'version',const_align(32));
  439. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  440. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  441. if not(tf_no_generic_stackcheck in target_info.flags) then
  442. begin
  443. { stacksize can be specified and is now simulated }
  444. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(pint));
  445. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(pint)));
  446. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(stacksize));
  447. end;
  448. {$IFDEF POWERPC}
  449. { AmigaOS4 "stack cookie" support }
  450. if ( target_info.system = system_powerpc_amiga ) then
  451. begin
  452. { this symbol is needed to ignite powerpc amigaos' }
  453. { stack allocation magic for us with the given stack size. }
  454. { note: won't work for m68k amigaos or morphos. (KB) }
  455. str(stacksize,stkcookie);
  456. stkcookie:='$STACK: '+stkcookie+#0;
  457. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  458. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  459. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  460. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  461. end;
  462. {$ENDIF POWERPC}
  463. { Initial heapsize }
  464. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  465. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(pint));
  466. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(pint)));
  467. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(heapsize));
  468. { Initial heapsize }
  469. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  470. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  471. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  472. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  473. end;
  474. procedure AddUnit(const s:string);
  475. var
  476. hp : tppumodule;
  477. unitsym : tunitsym;
  478. begin
  479. { load unit }
  480. hp:=registerunit(current_module,s,'');
  481. hp.loadppu;
  482. hp.adddependency(current_module);
  483. { add to symtable stack }
  484. symtablestack.push(hp.globalsymtable);
  485. if (m_mac in current_settings.modeswitches) and
  486. assigned(hp.globalmacrosymtable) then
  487. macrosymtablestack.push(hp.globalmacrosymtable);
  488. { insert unitsym }
  489. unitsym:=tunitsym.create(s,hp);
  490. inc(unitsym.refs);
  491. current_module.localsymtable.insert(unitsym);
  492. { add to used units }
  493. current_module.addusedunit(hp,false,unitsym);
  494. end;
  495. procedure maybeloadvariantsunit;
  496. var
  497. hp : tmodule;
  498. begin
  499. { Do we need the variants unit? Skip this
  500. for VarUtils unit for bootstrapping }
  501. if (current_module.flags and uf_uses_variants=0) or
  502. (current_module.modulename^='VARUTILS') then
  503. exit;
  504. { Variants unit already loaded? }
  505. hp:=tmodule(loaded_units.first);
  506. while assigned(hp) do
  507. begin
  508. if hp.modulename^='VARIANTS' then
  509. exit;
  510. hp:=tmodule(hp.next);
  511. end;
  512. { Variants unit is not loaded yet, load it now }
  513. Message(parser_w_implicit_uses_of_variants_unit);
  514. AddUnit('variants');
  515. end;
  516. function MaybeRemoveResUnit : boolean;
  517. var
  518. resources_used : boolean;
  519. hp : tmodule;
  520. uu : tused_unit;
  521. unitname : shortstring;
  522. begin
  523. { We simply remove the unit from:
  524. - usedunit list, so that things like init/finalization table won't
  525. contain references to this unit
  526. - loaded_units list, so that the unit object file doesn't get linked
  527. with the executable. }
  528. { Note: on windows we always need resources! }
  529. resources_used:=(target_info.system in systems_all_windows)
  530. or CheckResourcesUsed;
  531. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  532. begin
  533. { resources aren't used, so we don't need this unit }
  534. if target_res.id=res_ext then
  535. unitname:='FPEXTRES'
  536. else
  537. unitname:='FPINTRES';
  538. Message1(unit_u_unload_resunit,unitname);
  539. { find the module }
  540. hp:=tmodule(loaded_units.first);
  541. while assigned(hp) do
  542. begin
  543. if hp.is_unit and (hp.modulename^=unitname) then break;
  544. hp:=tmodule(hp.next);
  545. end;
  546. if not assigned(hp) then
  547. internalerror(200801071);
  548. { find its tused_unit in the global list }
  549. uu:=tused_unit(usedunits.first);
  550. while assigned(uu) do
  551. begin
  552. if uu.u=hp then break;
  553. uu:=tused_unit(uu.next);
  554. end;
  555. if not assigned(uu) then
  556. internalerror(200801072);
  557. { remove the tused_unit }
  558. usedunits.Remove(uu);
  559. uu.Free;
  560. { remove the module }
  561. loaded_units.Remove(hp);
  562. unloaded_units.Concat(hp);
  563. end;
  564. MaybeRemoveResUnit:=resources_used;
  565. end;
  566. procedure loaddefaultunits;
  567. begin
  568. { we are going to rebuild the symtablestack, clear it first }
  569. symtablestack.clear;
  570. macrosymtablestack.clear;
  571. { macro symtable }
  572. macrosymtablestack.push(initialmacrosymtable);
  573. { are we compiling the system unit? }
  574. if (cs_compilesystem in current_settings.moduleswitches) then
  575. begin
  576. systemunit:=tglobalsymtable(current_module.localsymtable);
  577. { create system defines }
  578. create_intern_symbols;
  579. create_intern_types;
  580. { Set the owner of errorsym and errortype to symtable to
  581. prevent crashes when accessing .owner }
  582. generrorsym.owner:=systemunit;
  583. generrordef.owner:=systemunit;
  584. exit;
  585. end;
  586. { insert the system unit, it is allways the first. Load also the
  587. internal types from the system unit }
  588. AddUnit('system');
  589. systemunit:=tglobalsymtable(symtablestack.top);
  590. load_intern_types;
  591. { Set the owner of errorsym and errortype to symtable to
  592. prevent crashes when accessing .owner }
  593. generrorsym.owner:=systemunit;
  594. generrordef.owner:=systemunit;
  595. { Units only required for main module }
  596. if not(current_module.is_unit) then
  597. begin
  598. { Heaptrc unit, load heaptrace before any other units especially objpas }
  599. if (cs_use_heaptrc in current_settings.globalswitches) then
  600. AddUnit('heaptrc');
  601. { Lineinfo unit }
  602. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  603. if (paratargetdbg = dbg_stabs) then
  604. AddUnit('lineinfo')
  605. else
  606. AddUnit('lnfodwrf');
  607. end;
  608. { Valgrind requires c memory manager }
  609. if (cs_gdb_valgrind in current_settings.globalswitches) then
  610. AddUnit('cmem');
  611. {$ifdef cpufpemu}
  612. { Floating point emulation unit?
  613. softfpu must be in the system unit anyways (FK)
  614. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  615. AddUnit('softfpu');
  616. }
  617. {$endif cpufpemu}
  618. { Which kind of resource support?
  619. Note: if resources aren't used this unit will be removed later,
  620. otherwise we need it here since it must be loaded quite early }
  621. if (tf_has_winlike_resources in target_info.flags) then
  622. if target_res.id=res_ext then
  623. AddUnit('fpextres')
  624. else
  625. AddUnit('fpintres');
  626. end;
  627. { Objpas unit? }
  628. if m_objpas in current_settings.modeswitches then
  629. AddUnit('objpas');
  630. { Macpas unit? }
  631. if m_mac in current_settings.modeswitches then
  632. AddUnit('macpas');
  633. if m_iso in current_settings.modeswitches then
  634. AddUnit('iso7185');
  635. { Objective-C support unit? }
  636. if (m_objectivec1 in current_settings.modeswitches) then
  637. begin
  638. { interface to Objective-C run time }
  639. AddUnit('objc');
  640. loadobjctypes;
  641. { NSObject }
  642. if not(current_module.is_unit) or
  643. (current_module.modulename^<>'OBJCBASE') then
  644. AddUnit('objcbase');
  645. end;
  646. { Profile unit? Needed for go32v2 only }
  647. if (cs_profile in current_settings.moduleswitches) and
  648. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  649. AddUnit('profile');
  650. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  651. begin
  652. AddUnit('fpcylix');
  653. AddUnit('dynlibs');
  654. end;
  655. { CPU targets with microcontroller support can add a controller specific unit }
  656. {$if defined(ARM) or defined(AVR)}
  657. if (target_info.system in systems_embedded) and (current_settings.controllertype<>ct_none) and
  658. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') then
  659. AddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  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 rtti/init tables }
  1224. write_persistent_type_info(current_module.globalsymtable);
  1225. write_persistent_type_info(current_module.localsymtable);
  1226. { Tables }
  1227. InsertThreadvars;
  1228. { Resource strings }
  1229. GenerateResourceStrings;
  1230. { Widestring typed constants }
  1231. InsertWideInits;
  1232. { generate debuginfo }
  1233. if (cs_debuginfo in current_settings.moduleswitches) then
  1234. current_debuginfo.inserttypeinfo;
  1235. { generate imports }
  1236. if current_module.ImportLibraryList.Count>0 then
  1237. importlib.generatelib;
  1238. { insert own objectfile, or say that it's in a library
  1239. (no check for an .o when loading) }
  1240. ag:=is_assembler_generated;
  1241. if ag then
  1242. insertobjectfile
  1243. else
  1244. begin
  1245. current_module.flags:=current_module.flags or uf_no_link;
  1246. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1247. end;
  1248. if ag then
  1249. begin
  1250. { create callframe info }
  1251. create_dwarf_frame;
  1252. { assemble }
  1253. create_objectfile;
  1254. end;
  1255. { Write out the ppufile after the object file has been created }
  1256. store_interface_crc:=current_module.interface_crc;
  1257. store_indirect_crc:=current_module.indirect_crc;
  1258. {$ifdef EXTDEBUG}
  1259. store_crc:=current_module.crc;
  1260. {$endif EXTDEBUG}
  1261. if (Errorcount=0) then
  1262. tppumodule(current_module).writeppu;
  1263. if not(cs_compilesystem in current_settings.moduleswitches) then
  1264. begin
  1265. if store_interface_crc<>current_module.interface_crc then
  1266. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1267. if store_indirect_crc<>current_module.indirect_crc then
  1268. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1269. end;
  1270. {$ifdef EXTDEBUG}
  1271. if not(cs_compilesystem in current_settings.moduleswitches) then
  1272. if (store_crc<>current_module.crc) and simplify_ppu then
  1273. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1274. {$endif EXTDEBUG}
  1275. { release local symtables that are not needed anymore }
  1276. free_localsymtables(current_module.globalsymtable);
  1277. free_localsymtables(current_module.localsymtable);
  1278. { leave when we got an error }
  1279. if (Errorcount>0) and not status.skip_error then
  1280. begin
  1281. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1282. status.skip_error:=true;
  1283. exit;
  1284. end;
  1285. {$ifdef debug_devirt}
  1286. { print out all instantiated class/object types }
  1287. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1288. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1289. begin
  1290. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1291. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1292. objectdef:
  1293. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1294. odt_object:
  1295. writeln(' (object)');
  1296. odt_class:
  1297. writeln(' (class)');
  1298. else
  1299. internalerror(2008101103);
  1300. end;
  1301. else
  1302. internalerror(2008101104);
  1303. end;
  1304. end;
  1305. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1306. begin
  1307. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1308. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1309. objectdef:
  1310. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1311. odt_class:
  1312. writeln(' (classrefdef)');
  1313. else
  1314. internalerror(2008101105);
  1315. end
  1316. else
  1317. internalerror(2008101102);
  1318. end;
  1319. end;
  1320. {$endif debug_devirt}
  1321. Message1(unit_u_finished_compiling,current_module.modulename^);
  1322. end;
  1323. procedure procexport(const s : string);
  1324. var
  1325. hp : texported_item;
  1326. begin
  1327. hp:=texported_item.create;
  1328. hp.name:=stringdup(s);
  1329. hp.options:=hp.options or eo_name;
  1330. exportlib.exportprocedure(hp);
  1331. end;
  1332. procedure varexport(const s : string);
  1333. var
  1334. hp : texported_item;
  1335. begin
  1336. hp:=texported_item.create;
  1337. hp.name:=stringdup(s);
  1338. hp.options:=hp.options or eo_name;
  1339. exportlib.exportvar(hp);
  1340. end;
  1341. procedure insert_export(sym : TObject;arg:pointer);
  1342. var
  1343. i : longint;
  1344. item : TCmdStrListItem;
  1345. begin
  1346. case TSym(sym).typ of
  1347. { ignore: }
  1348. unitsym,
  1349. syssym,
  1350. constsym,
  1351. enumsym,
  1352. typesym:
  1353. ;
  1354. procsym:
  1355. begin
  1356. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1357. begin
  1358. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1359. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1360. ((tsymtable(arg).symtabletype=globalsymtable) or
  1361. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1362. ) then
  1363. begin
  1364. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1365. { walk through all aliases }
  1366. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1367. while assigned(item) do
  1368. begin
  1369. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1370. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1371. procexport(item.str);
  1372. item:=TCmdStrListItem(item.next);
  1373. end;
  1374. end;
  1375. end;
  1376. end;
  1377. staticvarsym:
  1378. begin
  1379. varexport(tsym(sym).mangledname);
  1380. end;
  1381. else
  1382. begin
  1383. writeln('unknown: ',ord(TSym(sym).typ));
  1384. end;
  1385. end;
  1386. end;
  1387. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1388. Var
  1389. MakeStatic : Boolean;
  1390. Var
  1391. buffer : array[0..$1fff] of byte;
  1392. inppu,
  1393. outppu : tppufile;
  1394. b,
  1395. untilb : byte;
  1396. l,m : longint;
  1397. f : file;
  1398. ext,
  1399. s : string;
  1400. ppuversion : dword;
  1401. begin
  1402. Result:=false;
  1403. MakeStatic:=False;
  1404. inppu:=tppufile.create(PPUFn);
  1405. if not inppu.openfile then
  1406. begin
  1407. inppu.free;
  1408. Comment(V_Error,'Could not open : '+PPUFn);
  1409. Exit;
  1410. end;
  1411. { Check the ppufile }
  1412. if not inppu.CheckPPUId then
  1413. begin
  1414. inppu.free;
  1415. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1416. Exit;
  1417. end;
  1418. ppuversion:=inppu.GetPPUVersion;
  1419. if ppuversion<CurrentPPUVersion then
  1420. begin
  1421. inppu.free;
  1422. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1423. Exit;
  1424. end;
  1425. { No .o file generated for this ppu, just skip }
  1426. if (inppu.header.flags and uf_no_link)<>0 then
  1427. begin
  1428. inppu.free;
  1429. Result:=true;
  1430. Exit;
  1431. end;
  1432. { Already a lib? }
  1433. if (inppu.header.flags and uf_in_library)<>0 then
  1434. begin
  1435. inppu.free;
  1436. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1437. Exit;
  1438. end;
  1439. { We need a static linked unit }
  1440. if (inppu.header.flags and uf_static_linked)=0 then
  1441. begin
  1442. inppu.free;
  1443. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1444. Exit;
  1445. end;
  1446. { Check if shared is allowed }
  1447. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1448. begin
  1449. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1450. MakeStatic:=true;
  1451. end;
  1452. { Create the new ppu }
  1453. if PPUFn=PPLFn then
  1454. outppu:=tppufile.create('ppumove.$$$')
  1455. else
  1456. outppu:=tppufile.create(PPLFn);
  1457. outppu.createfile;
  1458. { Create new header, with the new flags }
  1459. outppu.header:=inppu.header;
  1460. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1461. if MakeStatic then
  1462. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1463. else
  1464. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1465. { read until the object files are found }
  1466. untilb:=iblinkunitofiles;
  1467. repeat
  1468. b:=inppu.readentry;
  1469. if b in [ibendinterface,ibend] then
  1470. begin
  1471. inppu.free;
  1472. outppu.free;
  1473. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1474. Exit;
  1475. end;
  1476. if b<>untilb then
  1477. begin
  1478. repeat
  1479. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1480. outppu.putdata(buffer,l);
  1481. until l<sizeof(buffer);
  1482. outppu.writeentry(b);
  1483. end;
  1484. until (b=untilb);
  1485. { we have now reached the section for the files which need to be added,
  1486. now add them to the list }
  1487. case b of
  1488. iblinkunitofiles :
  1489. begin
  1490. { add all o files, and save the entry when not creating a static
  1491. library to keep staticlinking possible }
  1492. while not inppu.endofentry do
  1493. begin
  1494. s:=inppu.getstring;
  1495. m:=inppu.getlongint;
  1496. if not MakeStatic then
  1497. begin
  1498. outppu.putstring(s);
  1499. outppu.putlongint(m);
  1500. end;
  1501. current_module.linkotherofiles.add(s,link_always);;
  1502. end;
  1503. if not MakeStatic then
  1504. outppu.writeentry(b);
  1505. end;
  1506. { iblinkunitstaticlibs :
  1507. begin
  1508. AddToLinkFiles(ExtractLib(inppu.getstring));
  1509. if not inppu.endofentry then
  1510. begin
  1511. repeat
  1512. inppu.getdatabuf(buffer^,bufsize,l);
  1513. outppu.putdata(buffer^,l);
  1514. until l<bufsize;
  1515. outppu.writeentry(b);
  1516. end;
  1517. end; }
  1518. end;
  1519. { just add a new entry with the new lib }
  1520. if MakeStatic then
  1521. begin
  1522. outppu.putstring('imp'+current_module.realmodulename^);
  1523. outppu.putlongint(link_static);
  1524. outppu.writeentry(iblinkunitstaticlibs)
  1525. end
  1526. else
  1527. begin
  1528. outppu.putstring('imp'+current_module.realmodulename^);
  1529. outppu.putlongint(link_shared);
  1530. outppu.writeentry(iblinkunitsharedlibs);
  1531. end;
  1532. { read all entries until the end and write them also to the new ppu }
  1533. repeat
  1534. b:=inppu.readentry;
  1535. { don't write ibend, that's written automatically }
  1536. if b<>ibend then
  1537. begin
  1538. if b=iblinkothersharedlibs then
  1539. begin
  1540. while not inppu.endofentry do
  1541. begin
  1542. s:=inppu.getstring;
  1543. m:=inppu.getlongint;
  1544. outppu.putstring(s);
  1545. outppu.putlongint(m);
  1546. { strip lib prefix }
  1547. if copy(s,1,3)='lib' then
  1548. delete(s,1,3);
  1549. ext:=ExtractFileExt(s);
  1550. if ext<>'' then
  1551. delete(s,length(s)-length(ext)+1,length(ext));
  1552. current_module.linkOtherSharedLibs.add(s,link_always);
  1553. end;
  1554. end
  1555. else
  1556. repeat
  1557. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1558. outppu.putdata(buffer,l);
  1559. until l<sizeof(buffer);
  1560. outppu.writeentry(b);
  1561. end;
  1562. until b=ibend;
  1563. { write the last stuff and close }
  1564. outppu.flush;
  1565. outppu.writeheader;
  1566. outppu.free;
  1567. inppu.free;
  1568. { rename }
  1569. if PPUFn=PPLFn then
  1570. begin
  1571. {$I-}
  1572. assign(f,PPUFn);
  1573. erase(f);
  1574. assign(f,'ppumove.$$$');
  1575. rename(f,PPUFn);
  1576. {$I+}
  1577. if ioresult<>0 then;
  1578. end;
  1579. Result:=True;
  1580. end;
  1581. procedure createimportlibfromexports;
  1582. var
  1583. hp : texported_item;
  1584. begin
  1585. hp:=texported_item(current_module._exports.first);
  1586. while assigned(hp) do
  1587. begin
  1588. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.name^,hp.index,hp.is_var,false);
  1589. hp:=texported_item(hp.next);
  1590. end;
  1591. end;
  1592. procedure proc_package;
  1593. var
  1594. main_file : tinputfile;
  1595. hp,hp2 : tmodule;
  1596. {finalize_procinfo,
  1597. init_procinfo,
  1598. main_procinfo : tcgprocinfo;}
  1599. force_init_final : boolean;
  1600. uu : tused_unit;
  1601. begin
  1602. Status.IsPackage:=true;
  1603. Status.IsExe:=true;
  1604. parse_only:=false;
  1605. {main_procinfo:=nil;
  1606. init_procinfo:=nil;
  1607. finalize_procinfo:=nil;}
  1608. if not RelocSectionSetExplicitly then
  1609. RelocSection:=true;
  1610. { Relocation works only without stabs under Windows when }
  1611. { external linker (LD) is used. LD generates relocs for }
  1612. { stab sections which is not loaded in memory. It causes }
  1613. { AV error when DLL is loaded and relocation is needed. }
  1614. { Internal linker does not have this problem. }
  1615. if RelocSection and
  1616. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1617. (cs_link_extern in current_settings.globalswitches) then
  1618. begin
  1619. include(current_settings.globalswitches,cs_link_strip);
  1620. { Warning stabs info does not work with reloc section !! }
  1621. if (cs_debuginfo in current_settings.moduleswitches) and
  1622. (target_dbg.id=dbg_stabs) then
  1623. begin
  1624. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1625. Message(parser_w_parser_win32_debug_needs_WN);
  1626. exclude(current_settings.moduleswitches,cs_debuginfo);
  1627. end;
  1628. end;
  1629. { get correct output names }
  1630. main_file := current_scanner.inputfile;
  1631. while assigned(main_file.next) do
  1632. main_file := main_file.next;
  1633. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1634. consume(_ID);
  1635. current_module.setmodulename(orgpattern);
  1636. current_module.ispackage:=true;
  1637. exportlib.preparelib(orgpattern);
  1638. if tf_library_needs_pic in target_info.flags then
  1639. include(current_settings.moduleswitches,cs_create_pic);
  1640. consume(_ID);
  1641. consume(_SEMICOLON);
  1642. { global switches are read, so further changes aren't allowed }
  1643. current_module.in_global:=false;
  1644. { setup things using the switches }
  1645. setupglobalswitches;
  1646. { set implementation flag }
  1647. current_module.in_interface:=false;
  1648. current_module.interface_compiled:=true;
  1649. { insert after the unit symbol tables the static symbol table }
  1650. { of the program }
  1651. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1652. {Load the units used by the program we compile.}
  1653. if token=_REQUIRES then
  1654. begin
  1655. end;
  1656. {Load the units used by the program we compile.}
  1657. if (token=_ID) and (idtoken=_CONTAINS) then
  1658. begin
  1659. consume(_ID);
  1660. while true do
  1661. begin
  1662. if token=_ID then
  1663. AddUnit(pattern);
  1664. consume(_ID);
  1665. if token=_COMMA then
  1666. consume(_COMMA)
  1667. else break;
  1668. end;
  1669. consume(_SEMICOLON);
  1670. end;
  1671. { All units are read, now give them a number }
  1672. current_module.updatemaps;
  1673. {Insert the name of the main program into the symbol table.}
  1674. if current_module.realmodulename^<>'' then
  1675. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1676. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1677. symtablestack.push(current_module.localsymtable);
  1678. { create whole program optimisation information }
  1679. current_module.wpoinfo:=tunitwpoinfo.create;
  1680. { should we force unit initialization? }
  1681. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1682. if force_init_final then
  1683. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1684. { Add symbol to the exports section for win32 so smartlinking a
  1685. DLL will include the edata section }
  1686. if assigned(exportlib) and
  1687. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1688. ((current_module.flags and uf_has_exports)<>0) then
  1689. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1690. { all labels must be defined before generating code }
  1691. if Errorcount=0 then
  1692. tstoredsymtable(current_module.localsymtable).checklabels;
  1693. symtablestack.pop(current_module.localsymtable);
  1694. { consume the last point }
  1695. consume(_END);
  1696. consume(_POINT);
  1697. if (Errorcount=0) then
  1698. begin
  1699. { test static symtable }
  1700. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1701. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1702. tstoredsymtable(current_module.localsymtable).check_forwards;
  1703. current_module.allunitsused;
  1704. end;
  1705. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1706. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1707. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1708. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1709. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1710. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1711. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1712. { leave when we got an error }
  1713. if (Errorcount>0) and not status.skip_error then
  1714. begin
  1715. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1716. status.skip_error:=true;
  1717. exit;
  1718. end;
  1719. { remove all unused units, this happends when units are removed
  1720. from the uses clause in the source and the ppu was already being loaded }
  1721. hp:=tmodule(loaded_units.first);
  1722. while assigned(hp) do
  1723. begin
  1724. hp2:=hp;
  1725. hp:=tmodule(hp.next);
  1726. if hp2.is_unit and
  1727. not assigned(hp2.globalsymtable) then
  1728. loaded_units.remove(hp2);
  1729. end;
  1730. { force exports }
  1731. uu:=tused_unit(usedunits.first);
  1732. while assigned(uu) do
  1733. begin
  1734. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1735. { check localsymtable for exports too to get public symbols }
  1736. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1737. { create special exports }
  1738. if (uu.u.flags and uf_init)<>0 then
  1739. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1740. if (uu.u.flags and uf_finalize)<>0 then
  1741. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1742. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1743. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1744. uu:=tused_unit(uu.next);
  1745. end;
  1746. {$ifdef arm}
  1747. { Insert .pdata section for arm-wince.
  1748. It is needed for exception handling. }
  1749. if target_info.system in [system_arm_wince] then
  1750. InsertPData;
  1751. {$endif arm}
  1752. { generate debuginfo }
  1753. if (cs_debuginfo in current_settings.moduleswitches) then
  1754. current_debuginfo.inserttypeinfo;
  1755. exportlib.generatelib;
  1756. { write all our exports to the import library,
  1757. needs to be done after exportlib.generatelib; }
  1758. createimportlibfromexports;
  1759. { generate imports }
  1760. if current_module.ImportLibraryList.Count>0 then
  1761. importlib.generatelib;
  1762. { Reference all DEBUGINFO sections from the main .fpc section }
  1763. if (cs_debuginfo in current_settings.moduleswitches) then
  1764. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1765. { insert own objectfile }
  1766. insertobjectfile;
  1767. { assemble and link }
  1768. create_objectfile;
  1769. { We might need the symbols info if not using
  1770. the default do_extractsymbolinfo
  1771. which is a dummy function PM }
  1772. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1773. { release all local symtables that are not needed anymore }
  1774. if (not needsymbolinfo) then
  1775. free_localsymtables(current_module.localsymtable);
  1776. { leave when we got an error }
  1777. if (Errorcount>0) and not status.skip_error then
  1778. begin
  1779. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1780. status.skip_error:=true;
  1781. exit;
  1782. end;
  1783. if (not current_module.is_unit) then
  1784. begin
  1785. { finally rewrite all units included into the package }
  1786. uu:=tused_unit(usedunits.first);
  1787. while assigned(uu) do
  1788. begin
  1789. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1790. uu:=tused_unit(uu.next);
  1791. end;
  1792. { create the executable when we are at level 1 }
  1793. if (compile_level=1) then
  1794. begin
  1795. { create global resource file by collecting all resource files }
  1796. CollectResourceFiles;
  1797. { write .def file }
  1798. if (cs_link_deffile in current_settings.globalswitches) then
  1799. deffile.writefile;
  1800. { insert all .o files from all loaded units and
  1801. unload the units, we don't need them anymore.
  1802. Keep the current_module because that is still needed }
  1803. hp:=tmodule(loaded_units.first);
  1804. while assigned(hp) do
  1805. begin
  1806. { the package itself contains no code so far }
  1807. linker.AddModuleFiles(hp);
  1808. hp2:=tmodule(hp.next);
  1809. if (hp<>current_module) and
  1810. (not needsymbolinfo) then
  1811. begin
  1812. loaded_units.remove(hp);
  1813. hp.free;
  1814. end;
  1815. hp:=hp2;
  1816. end;
  1817. linker.MakeSharedLibrary
  1818. end;
  1819. { Give Fatal with error count for linker errors }
  1820. if (Errorcount>0) and not status.skip_error then
  1821. begin
  1822. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1823. status.skip_error:=true;
  1824. end;
  1825. end;
  1826. end;
  1827. procedure proc_program(islibrary : boolean);
  1828. var
  1829. main_file : tinputfile;
  1830. hp,hp2 : tmodule;
  1831. finalize_procinfo,
  1832. init_procinfo,
  1833. main_procinfo : tcgprocinfo;
  1834. force_init_final : boolean;
  1835. resources_used : boolean;
  1836. begin
  1837. DLLsource:=islibrary;
  1838. Status.IsLibrary:=IsLibrary;
  1839. Status.IsPackage:=false;
  1840. Status.IsExe:=true;
  1841. parse_only:=false;
  1842. main_procinfo:=nil;
  1843. init_procinfo:=nil;
  1844. finalize_procinfo:=nil;
  1845. resources_used:=false;
  1846. { DLL defaults to create reloc info }
  1847. if islibrary then
  1848. begin
  1849. if not RelocSectionSetExplicitly then
  1850. RelocSection:=true;
  1851. end;
  1852. { Relocation works only without stabs under Windows when }
  1853. { external linker (LD) is used. LD generates relocs for }
  1854. { stab sections which is not loaded in memory. It causes }
  1855. { AV error when DLL is loaded and relocation is needed. }
  1856. { Internal linker does not have this problem. }
  1857. if RelocSection and
  1858. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1859. (cs_link_extern in current_settings.globalswitches) then
  1860. begin
  1861. include(current_settings.globalswitches,cs_link_strip);
  1862. { Warning stabs info does not work with reloc section !! }
  1863. if (cs_debuginfo in current_settings.moduleswitches) and
  1864. (target_dbg.id=dbg_stabs) then
  1865. begin
  1866. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1867. Message(parser_w_parser_win32_debug_needs_WN);
  1868. exclude(current_settings.moduleswitches,cs_debuginfo);
  1869. end;
  1870. end;
  1871. { get correct output names }
  1872. main_file := current_scanner.inputfile;
  1873. while assigned(main_file.next) do
  1874. main_file := main_file.next;
  1875. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1876. if islibrary then
  1877. begin
  1878. consume(_LIBRARY);
  1879. current_module.setmodulename(orgpattern);
  1880. current_module.islibrary:=true;
  1881. exportlib.preparelib(orgpattern);
  1882. if tf_library_needs_pic in target_info.flags then
  1883. include(current_settings.moduleswitches,cs_create_pic);
  1884. consume(_ID);
  1885. consume(_SEMICOLON);
  1886. end
  1887. else
  1888. { is there an program head ? }
  1889. if token=_PROGRAM then
  1890. begin
  1891. consume(_PROGRAM);
  1892. current_module.setmodulename(orgpattern);
  1893. if (target_info.system in systems_unit_program_exports) then
  1894. exportlib.preparelib(orgpattern);
  1895. consume(_ID);
  1896. if token=_LKLAMMER then
  1897. begin
  1898. consume(_LKLAMMER);
  1899. repeat
  1900. consume(_ID);
  1901. until not try_to_consume(_COMMA);
  1902. consume(_RKLAMMER);
  1903. end;
  1904. consume(_SEMICOLON);
  1905. end
  1906. else if (target_info.system in systems_unit_program_exports) then
  1907. exportlib.preparelib(current_module.realmodulename^);
  1908. { global switches are read, so further changes aren't allowed }
  1909. current_module.in_global:=false;
  1910. { setup things using the switches }
  1911. setupglobalswitches;
  1912. { set implementation flag }
  1913. current_module.in_interface:=false;
  1914. current_module.interface_compiled:=true;
  1915. { insert after the unit symbol tables the static symbol table }
  1916. { of the program }
  1917. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1918. { load standard units (system,objpas,profile unit) }
  1919. loaddefaultunits;
  1920. { Load units provided on the command line }
  1921. loadautounits;
  1922. {Load the units used by the program we compile.}
  1923. if token=_USES then
  1924. loadunits;
  1925. { All units are read, now give them a number }
  1926. current_module.updatemaps;
  1927. {Insert the name of the main program into the symbol table.}
  1928. if current_module.realmodulename^<>'' then
  1929. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1930. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1931. symtablestack.push(current_module.localsymtable);
  1932. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1933. maybe_load_got;
  1934. { create whole program optimisation information }
  1935. current_module.wpoinfo:=tunitwpoinfo.create;
  1936. { The program intialization needs an alias, so it can be called
  1937. from the bootstrap code.}
  1938. if islibrary then
  1939. begin
  1940. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1941. { Win32 startup code needs a single name }
  1942. if not(target_info.system in systems_darwin) then
  1943. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1944. else
  1945. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1946. end
  1947. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1948. begin
  1949. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1950. end
  1951. else
  1952. begin
  1953. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1954. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1955. end;
  1956. main_procinfo.parse_body;
  1957. { save file pos for debuginfo }
  1958. current_module.mainfilepos:=main_procinfo.entrypos;
  1959. { Generate specializations of objectdefs methods }
  1960. generate_specialization_procs;
  1961. { should we force unit initialization? }
  1962. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1963. if force_init_final then
  1964. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1965. { Add symbol to the exports section for win32 so smartlinking a
  1966. DLL will include the edata section }
  1967. if assigned(exportlib) and
  1968. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1969. ((current_module.flags and uf_has_exports)<>0) then
  1970. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1971. { finalize? }
  1972. if token=_FINALIZATION then
  1973. begin
  1974. { the uf_finalize flag is only set after we checked that it
  1975. wasn't empty }
  1976. { Parse the finalize }
  1977. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1978. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1979. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1980. finalize_procinfo.parse_body;
  1981. end
  1982. else
  1983. if force_init_final then
  1984. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1985. { the finalization routine of libraries is generic (and all libraries need to }
  1986. { be finalized, so they can finalize any units they use }
  1987. if (islibrary) then
  1988. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1989. { all labels must be defined before generating code }
  1990. if Errorcount=0 then
  1991. tstoredsymtable(current_module.localsymtable).checklabels;
  1992. { See remark in unit init/final }
  1993. main_procinfo.generate_code;
  1994. main_procinfo.resetprocdef;
  1995. release_main_proc(main_procinfo);
  1996. if assigned(init_procinfo) then
  1997. begin
  1998. init_procinfo.generate_code;
  1999. init_procinfo.resetprocdef;
  2000. release_main_proc(init_procinfo);
  2001. end;
  2002. if assigned(finalize_procinfo) then
  2003. begin
  2004. finalize_procinfo.generate_code;
  2005. finalize_procinfo.resetprocdef;
  2006. release_main_proc(finalize_procinfo);
  2007. end;
  2008. symtablestack.pop(current_module.localsymtable);
  2009. { consume the last point }
  2010. consume(_POINT);
  2011. { reset wpo flags for all defs }
  2012. reset_all_defs;
  2013. if (Errorcount=0) then
  2014. begin
  2015. { test static symtable }
  2016. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2017. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2018. tstoredsymtable(current_module.localsymtable).check_forwards;
  2019. current_module.allunitsused;
  2020. end;
  2021. { leave when we got an error }
  2022. if (Errorcount>0) and not status.skip_error then
  2023. begin
  2024. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2025. status.skip_error:=true;
  2026. exit;
  2027. end;
  2028. { remove all unused units, this happens when units are removed
  2029. from the uses clause in the source and the ppu was already being loaded }
  2030. hp:=tmodule(loaded_units.first);
  2031. while assigned(hp) do
  2032. begin
  2033. hp2:=hp;
  2034. hp:=tmodule(hp.next);
  2035. if hp2.is_unit and
  2036. not assigned(hp2.globalsymtable) then
  2037. begin
  2038. loaded_units.remove(hp2);
  2039. unloaded_units.concat(hp2);
  2040. end;
  2041. end;
  2042. { do we need to add the variants unit? }
  2043. maybeloadvariantsunit;
  2044. { Now that everything has been compiled we know if we need resource
  2045. support. If not, remove the unit. }
  2046. resources_used:=MaybeRemoveResUnit;
  2047. linker.initsysinitunitname;
  2048. if target_info.system in systems_internal_sysinit then
  2049. begin
  2050. { add start/halt unit }
  2051. AddUnit(linker.sysinitunit);
  2052. end;
  2053. {$ifdef arm}
  2054. { Insert .pdata section for arm-wince.
  2055. It is needed for exception handling. }
  2056. if target_info.system in [system_arm_wince] then
  2057. InsertPData;
  2058. {$endif arm}
  2059. InsertThreadvars;
  2060. { generate rtti/init tables }
  2061. write_persistent_type_info(current_module.localsymtable);
  2062. { if an Objective-C module, generate rtti and module info }
  2063. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2064. { generate wrappers for interfaces }
  2065. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  2066. { generate imports }
  2067. if current_module.ImportLibraryList.Count>0 then
  2068. importlib.generatelib;
  2069. { generate debuginfo }
  2070. if (cs_debuginfo in current_settings.moduleswitches) then
  2071. current_debuginfo.inserttypeinfo;
  2072. if islibrary or (target_info.system in systems_unit_program_exports) then
  2073. exportlib.generatelib;
  2074. { Reference all DEBUGINFO sections from the main .fpc section }
  2075. if (cs_debuginfo in current_settings.moduleswitches) then
  2076. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2077. { Resource strings }
  2078. GenerateResourceStrings;
  2079. { Windows widestring needing initialization }
  2080. InsertWideInits;
  2081. { insert Tables and StackLength }
  2082. InsertInitFinalTable;
  2083. InsertThreadvarTablesTable;
  2084. InsertResourceTablesTable;
  2085. InsertWideInitsTablesTable;
  2086. InsertMemorySizes;
  2087. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2088. if target_info.system in systems_interrupt_table then
  2089. InsertInterruptTable;
  2090. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2091. { Insert symbol to resource info }
  2092. InsertResourceInfo(resources_used);
  2093. { create callframe info }
  2094. create_dwarf_frame;
  2095. { insert own objectfile }
  2096. insertobjectfile;
  2097. { assemble and link }
  2098. create_objectfile;
  2099. { We might need the symbols info if not using
  2100. the default do_extractsymbolinfo
  2101. which is a dummy function PM }
  2102. needsymbolinfo:=
  2103. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2104. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2105. { release all local symtables that are not needed anymore }
  2106. if (not needsymbolinfo) then
  2107. free_localsymtables(current_module.localsymtable);
  2108. { leave when we got an error }
  2109. if (Errorcount>0) and not status.skip_error then
  2110. begin
  2111. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2112. status.skip_error:=true;
  2113. exit;
  2114. end;
  2115. if (not current_module.is_unit) then
  2116. begin
  2117. { create the executable when we are at level 1 }
  2118. if (compile_level=1) then
  2119. begin
  2120. { create global resource file by collecting all resource files }
  2121. CollectResourceFiles;
  2122. { write .def file }
  2123. if (cs_link_deffile in current_settings.globalswitches) then
  2124. deffile.writefile;
  2125. { insert all .o files from all loaded units and
  2126. unload the units, we don't need them anymore.
  2127. Keep the current_module because that is still needed }
  2128. hp:=tmodule(loaded_units.first);
  2129. while assigned(hp) do
  2130. begin
  2131. linker.AddModuleFiles(hp);
  2132. hp2:=tmodule(hp.next);
  2133. if (hp<>current_module) and
  2134. (not needsymbolinfo) then
  2135. begin
  2136. loaded_units.remove(hp);
  2137. hp.free;
  2138. end;
  2139. hp:=hp2;
  2140. end;
  2141. { free also unneeded units we didn't free before }
  2142. if not needsymbolinfo then
  2143. unloaded_units.Clear;
  2144. { finally we can create a executable }
  2145. if DLLSource then
  2146. linker.MakeSharedLibrary
  2147. else
  2148. linker.MakeExecutable;
  2149. { collect all necessary information for whole-program optimization }
  2150. wpoinfomanager.extractwpoinfofromprogram;
  2151. end;
  2152. { Give Fatal with error count for linker errors }
  2153. if (Errorcount>0) and not status.skip_error then
  2154. begin
  2155. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2156. status.skip_error:=true;
  2157. end;
  2158. end;
  2159. end;
  2160. end.