pmodules.pas 90 KB

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