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