pmodules.pas 90 KB

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