pmodules.pas 86 KB

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