pmodules.pas 90 KB

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