pmodules.pas 90 KB

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