pmodules.pas 93 KB

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