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