pmodules.pas 90 KB

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