pmodules.pas 90 KB

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