pmodules.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  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 systems_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 systems_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. pu.indirect_checksum:=pu.u.indirect_crc;
  716. { connect unitsym to the module }
  717. pu.unitsym.module:=pu.u;
  718. { add to symtable stack }
  719. symtablestack.push(pu.u.globalsymtable);
  720. if (m_mac in current_settings.modeswitches) and
  721. assigned(pu.u.globalmacrosymtable) then
  722. macrosymtablestack.push(pu.u.globalmacrosymtable);
  723. { check hints }
  724. pu.u.check_hints;
  725. end;
  726. pu:=tused_unit(pu.next);
  727. end;
  728. consume(_SEMICOLON);
  729. end;
  730. procedure reset_all_defs;
  731. procedure reset_used_unit_defs(hp:tmodule);
  732. var
  733. pu : tused_unit;
  734. begin
  735. pu:=tused_unit(hp.used_units.first);
  736. while assigned(pu) do
  737. begin
  738. if not pu.u.is_reset then
  739. begin
  740. { prevent infinte loop for circular dependencies }
  741. pu.u.is_reset:=true;
  742. if assigned(pu.u.globalsymtable) then
  743. begin
  744. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  745. reset_used_unit_defs(pu.u);
  746. end;
  747. end;
  748. pu:=tused_unit(pu.next);
  749. end;
  750. end;
  751. var
  752. hp2 : tmodule;
  753. begin
  754. hp2:=tmodule(loaded_units.first);
  755. while assigned(hp2) do
  756. begin
  757. hp2.is_reset:=false;
  758. hp2:=tmodule(hp2.next);
  759. end;
  760. reset_used_unit_defs(current_module);
  761. end;
  762. procedure free_localsymtables(st:TSymtable);
  763. var
  764. i : longint;
  765. def : tstoreddef;
  766. pd : tprocdef;
  767. begin
  768. for i:=0 to st.DefList.Count-1 do
  769. begin
  770. def:=tstoreddef(st.DefList[i]);
  771. if def.typ=procdef then
  772. begin
  773. pd:=tprocdef(def);
  774. if assigned(pd.localst) and
  775. (pd.localst.symtabletype<>staticsymtable) and
  776. not(po_inline in pd.procoptions) then
  777. begin
  778. free_localsymtables(pd.localst);
  779. pd.localst.free;
  780. pd.localst:=nil;
  781. end;
  782. end;
  783. end;
  784. end;
  785. procedure parse_implementation_uses;
  786. begin
  787. if token=_USES then
  788. loadunits;
  789. end;
  790. procedure setupglobalswitches;
  791. begin
  792. if (cs_create_pic in current_settings.moduleswitches) then
  793. begin
  794. def_system_macro('FPC_PIC');
  795. def_system_macro('PIC');
  796. end;
  797. end;
  798. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  799. var
  800. ps : tprocsym;
  801. pd : tprocdef;
  802. begin
  803. { there should be no current_procinfo available }
  804. if assigned(current_procinfo) then
  805. internalerror(200304275);
  806. {Generate a procsym for main}
  807. ps:=tprocsym.create('$'+name);
  808. { main are allways used }
  809. inc(ps.refs);
  810. st.insert(ps);
  811. pd:=tprocdef.create(main_program_level);
  812. include(pd.procoptions,po_global);
  813. pd.procsym:=ps;
  814. ps.ProcdefList.Add(pd);
  815. { set procdef options }
  816. pd.proctypeoption:=potype;
  817. pd.proccalloption:=pocall_default;
  818. include(pd.procoptions,po_hascallingconvention);
  819. pd.forwarddef:=false;
  820. pd.setmangledname(target_info.cprefix+name);
  821. pd.aliasnames.insert(pd.mangledname);
  822. handle_calling_convention(pd);
  823. { We don't need is a local symtable. Change it into the static
  824. symtable }
  825. pd.localst.free;
  826. pd.localst:=st;
  827. { set procinfo and current_procinfo.procdef }
  828. result:=tcgprocinfo(cprocinfo.create(nil));
  829. result.procdef:=pd;
  830. { main proc does always a call e.g. to init system unit }
  831. include(result.flags,pi_do_call);
  832. end;
  833. procedure release_main_proc(pi:tcgprocinfo);
  834. begin
  835. { remove localst as it was replaced by staticsymtable }
  836. pi.procdef.localst:=nil;
  837. { remove procinfo }
  838. current_module.procinfo:=nil;
  839. pi.free;
  840. pi:=nil;
  841. end;
  842. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  843. begin
  844. { update module flags }
  845. current_module.flags:=current_module.flags or flag;
  846. { create procdef }
  847. case flag of
  848. uf_init :
  849. begin
  850. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  851. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  852. end;
  853. uf_finalize :
  854. begin
  855. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  856. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  857. if (not current_module.is_unit) then
  858. result.procdef.aliasnames.insert('PASCALFINALIZE');
  859. end;
  860. else
  861. internalerror(200304253);
  862. end;
  863. result.code:=cnothingnode.create;
  864. end;
  865. procedure copy_macro(p:TObject; arg:pointer);
  866. begin
  867. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  868. end;
  869. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  870. var
  871. last_is_deprecated:boolean;
  872. begin
  873. try_consume_hintdirective:=false;
  874. repeat
  875. last_is_deprecated:=false;
  876. case idtoken of
  877. _LIBRARY :
  878. begin
  879. include(moduleopt,mo_hint_library);
  880. try_consume_hintdirective:=true;
  881. end;
  882. _DEPRECATED :
  883. begin
  884. include(moduleopt,mo_hint_deprecated);
  885. try_consume_hintdirective:=true;
  886. last_is_deprecated:=true;
  887. end;
  888. _EXPERIMENTAL :
  889. begin
  890. include(moduleopt,mo_hint_experimental);
  891. try_consume_hintdirective:=true;
  892. end;
  893. _PLATFORM :
  894. begin
  895. include(moduleopt,mo_hint_platform);
  896. try_consume_hintdirective:=true;
  897. end;
  898. _UNIMPLEMENTED :
  899. begin
  900. include(moduleopt,mo_hint_unimplemented);
  901. try_consume_hintdirective:=true;
  902. end;
  903. else
  904. break;
  905. end;
  906. consume(Token);
  907. { handle deprecated message }
  908. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  909. begin
  910. if deprecatedmsg<>nil then
  911. internalerror(201001221);
  912. deprecatedmsg:=stringdup(pattern);
  913. consume(token);
  914. include(moduleopt,mo_has_deprecated_msg);
  915. end;
  916. until false;
  917. end;
  918. procedure proc_unit;
  919. function is_assembler_generated:boolean;
  920. var
  921. hal : tasmlisttype;
  922. begin
  923. result:=false;
  924. if Errorcount=0 then
  925. begin
  926. for hal:=low(TasmlistType) to high(TasmlistType) do
  927. if not current_asmdata.asmlists[hal].empty then
  928. begin
  929. result:=true;
  930. exit;
  931. end;
  932. end;
  933. end;
  934. var
  935. main_file: tinputfile;
  936. {$ifdef EXTDEBUG}
  937. store_crc,
  938. {$endif EXTDEBUG}
  939. store_interface_crc,
  940. store_indirect_crc: cardinal;
  941. s1,s2 : ^string; {Saves stack space}
  942. force_init_final : boolean;
  943. init_procinfo,
  944. finalize_procinfo : tcgprocinfo;
  945. unitname8 : string[8];
  946. ag: boolean;
  947. {$ifdef i386}
  948. gotvarsym : tstaticvarsym;
  949. {$endif i386}
  950. {$ifdef debug_devirt}
  951. i: longint;
  952. {$endif debug_devirt}
  953. begin
  954. init_procinfo:=nil;
  955. finalize_procinfo:=nil;
  956. if m_mac in current_settings.modeswitches then
  957. current_module.mode_switch_allowed:= false;
  958. consume(_UNIT);
  959. if compile_level=1 then
  960. Status.IsExe:=false;
  961. if token=_ID then
  962. begin
  963. { create filenames and unit name }
  964. main_file := current_scanner.inputfile;
  965. while assigned(main_file.next) do
  966. main_file := main_file.next;
  967. new(s1);
  968. s1^:=current_module.modulename^;
  969. current_module.SetFileName(main_file.path^+main_file.name^,true);
  970. current_module.SetModuleName(orgpattern);
  971. { check for system unit }
  972. new(s2);
  973. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  974. unitname8:=copy(current_module.modulename^,1,8);
  975. if (cs_check_unit_name in current_settings.globalswitches) and
  976. (
  977. not(
  978. (current_module.modulename^=s2^) or
  979. (
  980. (length(current_module.modulename^)>8) and
  981. (unitname8=s2^)
  982. )
  983. )
  984. or
  985. (
  986. (length(s1^)>8) and
  987. (s1^<>current_module.modulename^)
  988. )
  989. ) then
  990. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  991. if (current_module.modulename^='SYSTEM') then
  992. include(current_settings.moduleswitches,cs_compilesystem);
  993. dispose(s2);
  994. dispose(s1);
  995. end;
  996. if (target_info.system in systems_unit_program_exports) then
  997. exportlib.preparelib(current_module.realmodulename^);
  998. consume(_ID);
  999. { parse hint directives }
  1000. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  1001. consume(_SEMICOLON);
  1002. consume(_INTERFACE);
  1003. { global switches are read, so further changes aren't allowed }
  1004. current_module.in_global:=false;
  1005. { handle the global switches }
  1006. setupglobalswitches;
  1007. message1(unit_u_loading_interface_units,current_module.modulename^);
  1008. { update status }
  1009. status.currentmodule:=current_module.realmodulename^;
  1010. { maybe turn off m_objpas if we are compiling objpas }
  1011. if (current_module.modulename^='OBJPAS') then
  1012. exclude(current_settings.modeswitches,m_objpas);
  1013. { maybe turn off m_mac if we are compiling macpas }
  1014. if (current_module.modulename^='MACPAS') then
  1015. exclude(current_settings.modeswitches,m_mac);
  1016. parse_only:=true;
  1017. { generate now the global symboltable,
  1018. define first as local to overcome dependency conflicts }
  1019. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  1020. { insert unitsym of this unit to prevent other units having
  1021. the same name }
  1022. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1023. { load default units, like the system unit }
  1024. loaddefaultunits;
  1025. { insert qualifier for the system unit (allows system.writeln) }
  1026. if not(cs_compilesystem in current_settings.moduleswitches) and
  1027. (token=_USES) then
  1028. begin
  1029. loadunits;
  1030. { has it been compiled at a higher level ?}
  1031. if current_module.state=ms_compiled then
  1032. exit;
  1033. end;
  1034. { move the global symtable from the temporary local to global }
  1035. current_module.globalsymtable:=current_module.localsymtable;
  1036. current_module.localsymtable:=nil;
  1037. reset_all_defs;
  1038. { number all units, so we know if a unit is used by this unit or
  1039. needs to be added implicitly }
  1040. current_module.updatemaps;
  1041. { create whole program optimisation information (may already be
  1042. updated in the interface, e.g., in case of classrefdef typed
  1043. constants }
  1044. current_module.wpoinfo:=tunitwpoinfo.create;
  1045. { ... parse the declarations }
  1046. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1047. symtablestack.push(current_module.globalsymtable);
  1048. read_interface_declarations;
  1049. symtablestack.pop(current_module.globalsymtable);
  1050. { Export macros defined in the interface for macpas. The macros
  1051. are put in the globalmacrosymtable that will only be used by other
  1052. units. The current unit continues to use the localmacrosymtable }
  1053. if (m_mac in current_settings.modeswitches) then
  1054. begin
  1055. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  1056. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  1057. end;
  1058. { leave when we got an error }
  1059. if (Errorcount>0) and not status.skip_error then
  1060. begin
  1061. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1062. status.skip_error:=true;
  1063. exit;
  1064. end;
  1065. { Our interface is compiled, generate CRC and switch to implementation }
  1066. if not(cs_compilesystem in current_settings.moduleswitches) and
  1067. (Errorcount=0) then
  1068. tppumodule(current_module).getppucrc;
  1069. current_module.in_interface:=false;
  1070. current_module.interface_compiled:=true;
  1071. { First reload all units depending on our interface, we need to do this
  1072. in the implementation part to prevent erroneous circular references }
  1073. tppumodule(current_module).setdefgeneration;
  1074. tppumodule(current_module).reload_flagged_units;
  1075. { Parse the implementation section }
  1076. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1077. current_module.interface_only:=true
  1078. else
  1079. current_module.interface_only:=false;
  1080. parse_only:=false;
  1081. { create static symbol table }
  1082. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1083. {$ifdef i386}
  1084. if cs_create_pic in current_settings.moduleswitches then
  1085. begin
  1086. { insert symbol for got access in assembler code}
  1087. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  1088. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  1089. current_module.localsymtable.insert(gotvarsym);
  1090. { avoid unnecessary warnings }
  1091. gotvarsym.varstate:=vs_read;
  1092. gotvarsym.refs:=1;
  1093. end;
  1094. {$endif i386}
  1095. if not current_module.interface_only then
  1096. begin
  1097. consume(_IMPLEMENTATION);
  1098. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1099. { Read the implementation units }
  1100. parse_implementation_uses;
  1101. end;
  1102. if current_module.state=ms_compiled then
  1103. exit;
  1104. { reset ranges/stabs in exported definitions }
  1105. reset_all_defs;
  1106. { All units are read, now give them a number }
  1107. current_module.updatemaps;
  1108. symtablestack.push(current_module.globalsymtable);
  1109. symtablestack.push(current_module.localsymtable);
  1110. if not current_module.interface_only then
  1111. begin
  1112. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1113. if current_module.in_interface then
  1114. internalerror(200212285);
  1115. { Compile the unit }
  1116. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  1117. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1118. init_procinfo.parse_body;
  1119. { save file pos for debuginfo }
  1120. current_module.mainfilepos:=init_procinfo.entrypos;
  1121. end;
  1122. { Generate specializations of objectdefs methods }
  1123. generate_specialization_procs;
  1124. { if the unit contains ansi/widestrings, initialization and
  1125. finalization code must be forced }
  1126. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1127. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1128. { should we force unit initialization? }
  1129. { this is a hack, but how can it be done better ? }
  1130. if force_init_final and ((current_module.flags and uf_init)=0) then
  1131. begin
  1132. { first release the not used init procinfo }
  1133. if assigned(init_procinfo) then
  1134. release_main_proc(init_procinfo);
  1135. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1136. end;
  1137. { finalize? }
  1138. if not current_module.interface_only and (token=_FINALIZATION) then
  1139. begin
  1140. { the uf_finalize flag is only set after we checked that it
  1141. wasn't empty }
  1142. { Compile the finalize }
  1143. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1144. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1145. finalize_procinfo.parse_body;
  1146. end
  1147. else if force_init_final then
  1148. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1149. { Now both init and finalize bodies are read and it is known
  1150. which variables are used in both init and finalize we can now
  1151. generate the code. This is required to prevent putting a variable in
  1152. a register that is also used in the finalize body (PFV) }
  1153. if assigned(init_procinfo) then
  1154. begin
  1155. init_procinfo.generate_code;
  1156. init_procinfo.resetprocdef;
  1157. release_main_proc(init_procinfo);
  1158. end;
  1159. if assigned(finalize_procinfo) then
  1160. begin
  1161. finalize_procinfo.generate_code;
  1162. finalize_procinfo.resetprocdef;
  1163. release_main_proc(finalize_procinfo);
  1164. end;
  1165. symtablestack.pop(current_module.localsymtable);
  1166. symtablestack.pop(current_module.globalsymtable);
  1167. { the last char should always be a point }
  1168. consume(_POINT);
  1169. if (Errorcount=0) then
  1170. begin
  1171. { tests, if all (interface) forwards are resolved }
  1172. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1173. { check if all private fields are used }
  1174. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1175. { test static symtable }
  1176. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1177. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1178. tstoredsymtable(current_module.localsymtable).check_forwards;
  1179. tstoredsymtable(current_module.localsymtable).checklabels;
  1180. { used units }
  1181. current_module.allunitsused;
  1182. end;
  1183. { leave when we got an error }
  1184. if (Errorcount>0) and not status.skip_error then
  1185. begin
  1186. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1187. status.skip_error:=true;
  1188. exit;
  1189. end;
  1190. { if an Objective-C module, generate rtti and module info }
  1191. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1192. { do we need to add the variants unit? }
  1193. maybeloadvariantsunit;
  1194. { generate wrappers for interfaces }
  1195. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  1196. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1197. { generate pic helpers to load eip if necessary }
  1198. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1199. { generate rtti/init tables }
  1200. write_persistent_type_info(current_module.globalsymtable);
  1201. write_persistent_type_info(current_module.localsymtable);
  1202. { Tables }
  1203. insertThreadVars;
  1204. { Resource strings }
  1205. GenerateResourceStrings;
  1206. { Widestring typed constants }
  1207. InsertWideInits;
  1208. { generate debuginfo }
  1209. if (cs_debuginfo in current_settings.moduleswitches) then
  1210. current_debuginfo.inserttypeinfo;
  1211. { generate imports }
  1212. if current_module.ImportLibraryList.Count>0 then
  1213. importlib.generatelib;
  1214. { insert own objectfile, or say that it's in a library
  1215. (no check for an .o when loading) }
  1216. ag:=is_assembler_generated;
  1217. if ag then
  1218. insertobjectfile
  1219. else
  1220. begin
  1221. current_module.flags:=current_module.flags or uf_no_link;
  1222. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1223. end;
  1224. if ag then
  1225. begin
  1226. { create callframe info }
  1227. create_dwarf_frame;
  1228. { assemble }
  1229. create_objectfile;
  1230. end;
  1231. { Write out the ppufile after the object file has been created }
  1232. store_interface_crc:=current_module.interface_crc;
  1233. store_indirect_crc:=current_module.indirect_crc;
  1234. {$ifdef EXTDEBUG}
  1235. store_crc:=current_module.crc;
  1236. {$endif EXTDEBUG}
  1237. if (Errorcount=0) then
  1238. tppumodule(current_module).writeppu;
  1239. if not(cs_compilesystem in current_settings.moduleswitches) then
  1240. begin
  1241. if store_interface_crc<>current_module.interface_crc then
  1242. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1243. if store_indirect_crc<>current_module.indirect_crc then
  1244. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1245. end;
  1246. {$ifdef EXTDEBUG}
  1247. if not(cs_compilesystem in current_settings.moduleswitches) then
  1248. if (store_crc<>current_module.crc) and simplify_ppu then
  1249. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1250. {$endif EXTDEBUG}
  1251. { release local symtables that are not needed anymore }
  1252. free_localsymtables(current_module.globalsymtable);
  1253. free_localsymtables(current_module.localsymtable);
  1254. { leave when we got an error }
  1255. if (Errorcount>0) and not status.skip_error then
  1256. begin
  1257. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1258. status.skip_error:=true;
  1259. exit;
  1260. end;
  1261. {$ifdef debug_devirt}
  1262. { print out all instantiated class/object types }
  1263. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1264. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1265. begin
  1266. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1267. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1268. objectdef:
  1269. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1270. odt_object:
  1271. writeln(' (object)');
  1272. odt_class:
  1273. writeln(' (class)');
  1274. else
  1275. internalerror(2008101103);
  1276. end;
  1277. else
  1278. internalerror(2008101104);
  1279. end;
  1280. end;
  1281. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1282. begin
  1283. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1284. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1285. objectdef:
  1286. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1287. odt_class:
  1288. writeln(' (classrefdef)');
  1289. else
  1290. internalerror(2008101105);
  1291. end
  1292. else
  1293. internalerror(2008101102);
  1294. end;
  1295. end;
  1296. {$endif debug_devirt}
  1297. Message1(unit_u_finished_compiling,current_module.modulename^);
  1298. end;
  1299. procedure procexport(const s : string);
  1300. var
  1301. hp : texported_item;
  1302. begin
  1303. hp:=texported_item.create;
  1304. hp.name:=stringdup(s);
  1305. hp.options:=hp.options or eo_name;
  1306. exportlib.exportprocedure(hp);
  1307. end;
  1308. procedure varexport(const s : string);
  1309. var
  1310. hp : texported_item;
  1311. begin
  1312. hp:=texported_item.create;
  1313. hp.name:=stringdup(s);
  1314. hp.options:=hp.options or eo_name;
  1315. exportlib.exportvar(hp);
  1316. end;
  1317. procedure insert_export(sym : TObject;arg:pointer);
  1318. var
  1319. i : longint;
  1320. item : TCmdStrListItem;
  1321. begin
  1322. case TSym(sym).typ of
  1323. { ignore: }
  1324. unitsym,
  1325. syssym,
  1326. constsym,
  1327. enumsym,
  1328. typesym:
  1329. ;
  1330. procsym:
  1331. begin
  1332. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1333. begin
  1334. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1335. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1336. ((tsymtable(arg).symtabletype=globalsymtable) or
  1337. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1338. ) then
  1339. begin
  1340. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1341. { walk through all aliases }
  1342. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1343. while assigned(item) do
  1344. begin
  1345. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1346. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1347. procexport(item.str);
  1348. item:=TCmdStrListItem(item.next);
  1349. end;
  1350. end;
  1351. end;
  1352. end;
  1353. staticvarsym:
  1354. begin
  1355. varexport(tsym(sym).mangledname);
  1356. end;
  1357. else
  1358. begin
  1359. writeln('unknown: ',ord(TSym(sym).typ));
  1360. end;
  1361. end;
  1362. end;
  1363. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1364. Var
  1365. MakeStatic : Boolean;
  1366. Var
  1367. buffer : array[0..$1fff] of byte;
  1368. inppu,
  1369. outppu : tppufile;
  1370. b,
  1371. untilb : byte;
  1372. l,m : longint;
  1373. f : file;
  1374. ext,
  1375. s : string;
  1376. ppuversion : dword;
  1377. begin
  1378. Result:=false;
  1379. MakeStatic:=False;
  1380. inppu:=tppufile.create(PPUFn);
  1381. if not inppu.openfile then
  1382. begin
  1383. inppu.free;
  1384. Comment(V_Error,'Could not open : '+PPUFn);
  1385. Exit;
  1386. end;
  1387. { Check the ppufile }
  1388. if not inppu.CheckPPUId then
  1389. begin
  1390. inppu.free;
  1391. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1392. Exit;
  1393. end;
  1394. ppuversion:=inppu.GetPPUVersion;
  1395. if ppuversion<CurrentPPUVersion then
  1396. begin
  1397. inppu.free;
  1398. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1399. Exit;
  1400. end;
  1401. { No .o file generated for this ppu, just skip }
  1402. if (inppu.header.flags and uf_no_link)<>0 then
  1403. begin
  1404. inppu.free;
  1405. Result:=true;
  1406. Exit;
  1407. end;
  1408. { Already a lib? }
  1409. if (inppu.header.flags and uf_in_library)<>0 then
  1410. begin
  1411. inppu.free;
  1412. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1413. Exit;
  1414. end;
  1415. { We need a static linked unit }
  1416. if (inppu.header.flags and uf_static_linked)=0 then
  1417. begin
  1418. inppu.free;
  1419. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1420. Exit;
  1421. end;
  1422. { Check if shared is allowed }
  1423. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1424. begin
  1425. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1426. MakeStatic:=true;
  1427. end;
  1428. { Create the new ppu }
  1429. if PPUFn=PPLFn then
  1430. outppu:=tppufile.create('ppumove.$$$')
  1431. else
  1432. outppu:=tppufile.create(PPLFn);
  1433. outppu.createfile;
  1434. { Create new header, with the new flags }
  1435. outppu.header:=inppu.header;
  1436. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1437. if MakeStatic then
  1438. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1439. else
  1440. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1441. { read until the object files are found }
  1442. untilb:=iblinkunitofiles;
  1443. repeat
  1444. b:=inppu.readentry;
  1445. if b in [ibendinterface,ibend] then
  1446. begin
  1447. inppu.free;
  1448. outppu.free;
  1449. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1450. Exit;
  1451. end;
  1452. if b<>untilb then
  1453. begin
  1454. repeat
  1455. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1456. outppu.putdata(buffer,l);
  1457. until l<sizeof(buffer);
  1458. outppu.writeentry(b);
  1459. end;
  1460. until (b=untilb);
  1461. { we have now reached the section for the files which need to be added,
  1462. now add them to the list }
  1463. case b of
  1464. iblinkunitofiles :
  1465. begin
  1466. { add all o files, and save the entry when not creating a static
  1467. library to keep staticlinking possible }
  1468. while not inppu.endofentry do
  1469. begin
  1470. s:=inppu.getstring;
  1471. m:=inppu.getlongint;
  1472. if not MakeStatic then
  1473. begin
  1474. outppu.putstring(s);
  1475. outppu.putlongint(m);
  1476. end;
  1477. current_module.linkotherofiles.add(s,link_always);;
  1478. end;
  1479. if not MakeStatic then
  1480. outppu.writeentry(b);
  1481. end;
  1482. { iblinkunitstaticlibs :
  1483. begin
  1484. AddToLinkFiles(ExtractLib(inppu.getstring));
  1485. if not inppu.endofentry then
  1486. begin
  1487. repeat
  1488. inppu.getdatabuf(buffer^,bufsize,l);
  1489. outppu.putdata(buffer^,l);
  1490. until l<bufsize;
  1491. outppu.writeentry(b);
  1492. end;
  1493. end; }
  1494. end;
  1495. { just add a new entry with the new lib }
  1496. if MakeStatic then
  1497. begin
  1498. outppu.putstring('imp'+current_module.realmodulename^);
  1499. outppu.putlongint(link_static);
  1500. outppu.writeentry(iblinkunitstaticlibs)
  1501. end
  1502. else
  1503. begin
  1504. outppu.putstring('imp'+current_module.realmodulename^);
  1505. outppu.putlongint(link_shared);
  1506. outppu.writeentry(iblinkunitsharedlibs);
  1507. end;
  1508. { read all entries until the end and write them also to the new ppu }
  1509. repeat
  1510. b:=inppu.readentry;
  1511. { don't write ibend, that's written automaticly }
  1512. if b<>ibend then
  1513. begin
  1514. if b=iblinkothersharedlibs then
  1515. begin
  1516. while not inppu.endofentry do
  1517. begin
  1518. s:=inppu.getstring;
  1519. m:=inppu.getlongint;
  1520. outppu.putstring(s);
  1521. outppu.putlongint(m);
  1522. { strip lib prefix }
  1523. if copy(s,1,3)='lib' then
  1524. delete(s,1,3);
  1525. ext:=ExtractFileExt(s);
  1526. if ext<>'' then
  1527. delete(s,length(s)-length(ext)+1,length(ext));
  1528. current_module.linkOtherSharedLibs.add(s,link_always);
  1529. end;
  1530. end
  1531. else
  1532. repeat
  1533. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1534. outppu.putdata(buffer,l);
  1535. until l<sizeof(buffer);
  1536. outppu.writeentry(b);
  1537. end;
  1538. until b=ibend;
  1539. { write the last stuff and close }
  1540. outppu.flush;
  1541. outppu.writeheader;
  1542. outppu.free;
  1543. inppu.free;
  1544. { rename }
  1545. if PPUFn=PPLFn then
  1546. begin
  1547. {$I-}
  1548. assign(f,PPUFn);
  1549. erase(f);
  1550. assign(f,'ppumove.$$$');
  1551. rename(f,PPUFn);
  1552. {$I+}
  1553. if ioresult<>0 then;
  1554. end;
  1555. Result:=True;
  1556. end;
  1557. procedure createimportlibfromexports;
  1558. var
  1559. hp : texported_item;
  1560. begin
  1561. hp:=texported_item(current_module._exports.first);
  1562. while assigned(hp) do
  1563. begin
  1564. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.index,hp.is_var,false);
  1565. hp:=texported_item(hp.next);
  1566. end;
  1567. end;
  1568. procedure proc_package;
  1569. var
  1570. main_file : tinputfile;
  1571. hp,hp2 : tmodule;
  1572. {finalize_procinfo,
  1573. init_procinfo,
  1574. main_procinfo : tcgprocinfo;}
  1575. force_init_final : boolean;
  1576. uu : tused_unit;
  1577. begin
  1578. Status.IsPackage:=true;
  1579. Status.IsExe:=true;
  1580. parse_only:=false;
  1581. {main_procinfo:=nil;
  1582. init_procinfo:=nil;
  1583. finalize_procinfo:=nil;}
  1584. if not RelocSectionSetExplicitly then
  1585. RelocSection:=true;
  1586. { Relocation works only without stabs under Windows when }
  1587. { external linker (LD) is used. LD generates relocs for }
  1588. { stab sections which is not loaded in memory. It causes }
  1589. { AV error when DLL is loaded and relocation is needed. }
  1590. { Internal linker does not have this problem. }
  1591. if RelocSection and
  1592. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1593. (cs_link_extern in current_settings.globalswitches) then
  1594. begin
  1595. include(current_settings.globalswitches,cs_link_strip);
  1596. { Warning stabs info does not work with reloc section !! }
  1597. if (cs_debuginfo in current_settings.moduleswitches) and
  1598. (target_dbg.id=dbg_stabs) then
  1599. begin
  1600. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1601. Message(parser_w_parser_win32_debug_needs_WN);
  1602. exclude(current_settings.moduleswitches,cs_debuginfo);
  1603. end;
  1604. end;
  1605. { get correct output names }
  1606. main_file := current_scanner.inputfile;
  1607. while assigned(main_file.next) do
  1608. main_file := main_file.next;
  1609. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1610. consume(_ID);
  1611. current_module.setmodulename(orgpattern);
  1612. current_module.ispackage:=true;
  1613. exportlib.preparelib(orgpattern);
  1614. if tf_library_needs_pic in target_info.flags then
  1615. include(current_settings.moduleswitches,cs_create_pic);
  1616. consume(_ID);
  1617. consume(_SEMICOLON);
  1618. { global switches are read, so further changes aren't allowed }
  1619. current_module.in_global:=false;
  1620. { setup things using the switches }
  1621. setupglobalswitches;
  1622. { set implementation flag }
  1623. current_module.in_interface:=false;
  1624. current_module.interface_compiled:=true;
  1625. { insert after the unit symbol tables the static symbol table }
  1626. { of the program }
  1627. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1628. {Load the units used by the program we compile.}
  1629. if token=_REQUIRES then
  1630. begin
  1631. end;
  1632. {Load the units used by the program we compile.}
  1633. if (token=_ID) and (idtoken=_CONTAINS) then
  1634. begin
  1635. consume(_ID);
  1636. while true do
  1637. begin
  1638. if token=_ID then
  1639. AddUnit(pattern);
  1640. consume(_ID);
  1641. if token=_COMMA then
  1642. consume(_COMMA)
  1643. else break;
  1644. end;
  1645. consume(_SEMICOLON);
  1646. end;
  1647. { reset ranges/stabs in exported definitions }
  1648. reset_all_defs;
  1649. { All units are read, now give them a number }
  1650. current_module.updatemaps;
  1651. {Insert the name of the main program into the symbol table.}
  1652. if current_module.realmodulename^<>'' then
  1653. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1654. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1655. symtablestack.push(current_module.localsymtable);
  1656. { create whole program optimisation information }
  1657. current_module.wpoinfo:=tunitwpoinfo.create;
  1658. { should we force unit initialization? }
  1659. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1660. if force_init_final then
  1661. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1662. { Add symbol to the exports section for win32 so smartlinking a
  1663. DLL will include the edata section }
  1664. if assigned(exportlib) and
  1665. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1666. ((current_module.flags and uf_has_exports)<>0) then
  1667. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1668. { all labels must be defined before generating code }
  1669. if Errorcount=0 then
  1670. tstoredsymtable(current_module.localsymtable).checklabels;
  1671. symtablestack.pop(current_module.localsymtable);
  1672. { consume the last point }
  1673. consume(_END);
  1674. consume(_POINT);
  1675. if (Errorcount=0) then
  1676. begin
  1677. { test static symtable }
  1678. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1679. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1680. tstoredsymtable(current_module.localsymtable).check_forwards;
  1681. current_module.allunitsused;
  1682. end;
  1683. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1684. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1685. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1686. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1687. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1688. {$ifdef i386}
  1689. { fix me! }
  1690. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1691. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const(A_RET,S_W,12));
  1692. {$endif i386}
  1693. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1694. { leave when we got an error }
  1695. if (Errorcount>0) and not status.skip_error then
  1696. begin
  1697. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1698. status.skip_error:=true;
  1699. exit;
  1700. end;
  1701. { remove all unused units, this happends when units are removed
  1702. from the uses clause in the source and the ppu was already being loaded }
  1703. hp:=tmodule(loaded_units.first);
  1704. while assigned(hp) do
  1705. begin
  1706. hp2:=hp;
  1707. hp:=tmodule(hp.next);
  1708. if hp2.is_unit and
  1709. not assigned(hp2.globalsymtable) then
  1710. loaded_units.remove(hp2);
  1711. end;
  1712. { force exports }
  1713. uu:=tused_unit(usedunits.first);
  1714. while assigned(uu) do
  1715. begin
  1716. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1717. { check localsymtable for exports too to get public symbols }
  1718. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1719. { create special exports }
  1720. if (uu.u.flags and uf_init)<>0 then
  1721. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1722. if (uu.u.flags and uf_finalize)<>0 then
  1723. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1724. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1725. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1726. uu:=tused_unit(uu.next);
  1727. end;
  1728. {$ifdef arm}
  1729. { Insert .pdata section for arm-wince.
  1730. It is needed for exception handling. }
  1731. if target_info.system in [system_arm_wince] then
  1732. InsertPData;
  1733. {$endif arm}
  1734. { generate debuginfo }
  1735. if (cs_debuginfo in current_settings.moduleswitches) then
  1736. current_debuginfo.inserttypeinfo;
  1737. exportlib.generatelib;
  1738. { write all our exports to the import library,
  1739. needs to be done after exportlib.generatelib; }
  1740. createimportlibfromexports;
  1741. { generate imports }
  1742. if current_module.ImportLibraryList.Count>0 then
  1743. importlib.generatelib;
  1744. { Reference all DEBUGINFO sections from the main .fpc section }
  1745. if (cs_debuginfo in current_settings.moduleswitches) then
  1746. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1747. { insert own objectfile }
  1748. insertobjectfile;
  1749. { assemble and link }
  1750. create_objectfile;
  1751. { We might need the symbols info if not using
  1752. the default do_extractsymbolinfo
  1753. which is a dummy function PM }
  1754. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1755. { release all local symtables that are not needed anymore }
  1756. if (not needsymbolinfo) then
  1757. free_localsymtables(current_module.localsymtable);
  1758. { leave when we got an error }
  1759. if (Errorcount>0) and not status.skip_error then
  1760. begin
  1761. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1762. status.skip_error:=true;
  1763. exit;
  1764. end;
  1765. if (not current_module.is_unit) then
  1766. begin
  1767. { finally rewrite all units included into the package }
  1768. uu:=tused_unit(usedunits.first);
  1769. while assigned(uu) do
  1770. begin
  1771. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1772. uu:=tused_unit(uu.next);
  1773. end;
  1774. { create the executable when we are at level 1 }
  1775. if (compile_level=1) then
  1776. begin
  1777. { create global resource file by collecting all resource files }
  1778. CollectResourceFiles;
  1779. { write .def file }
  1780. if (cs_link_deffile in current_settings.globalswitches) then
  1781. deffile.writefile;
  1782. { insert all .o files from all loaded units and
  1783. unload the units, we don't need them anymore.
  1784. Keep the current_module because that is still needed }
  1785. hp:=tmodule(loaded_units.first);
  1786. while assigned(hp) do
  1787. begin
  1788. { the package itself contains no code so far }
  1789. linker.AddModuleFiles(hp);
  1790. hp2:=tmodule(hp.next);
  1791. if (hp<>current_module) and
  1792. (not needsymbolinfo) then
  1793. begin
  1794. loaded_units.remove(hp);
  1795. hp.free;
  1796. end;
  1797. hp:=hp2;
  1798. end;
  1799. linker.MakeSharedLibrary
  1800. end;
  1801. { Give Fatal with error count for linker errors }
  1802. if (Errorcount>0) and not status.skip_error then
  1803. begin
  1804. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1805. status.skip_error:=true;
  1806. end;
  1807. end;
  1808. end;
  1809. procedure proc_program(islibrary : boolean);
  1810. var
  1811. main_file : tinputfile;
  1812. hp,hp2 : tmodule;
  1813. finalize_procinfo,
  1814. init_procinfo,
  1815. main_procinfo : tcgprocinfo;
  1816. force_init_final : boolean;
  1817. resources_used : boolean;
  1818. begin
  1819. DLLsource:=islibrary;
  1820. Status.IsLibrary:=IsLibrary;
  1821. Status.IsPackage:=false;
  1822. Status.IsExe:=true;
  1823. parse_only:=false;
  1824. main_procinfo:=nil;
  1825. init_procinfo:=nil;
  1826. finalize_procinfo:=nil;
  1827. resources_used:=false;
  1828. { DLL defaults to create reloc info }
  1829. if islibrary then
  1830. begin
  1831. if not RelocSectionSetExplicitly then
  1832. RelocSection:=true;
  1833. end;
  1834. { Relocation works only without stabs under Windows when }
  1835. { external linker (LD) is used. LD generates relocs for }
  1836. { stab sections which is not loaded in memory. It causes }
  1837. { AV error when DLL is loaded and relocation is needed. }
  1838. { Internal linker does not have this problem. }
  1839. if RelocSection and
  1840. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1841. (cs_link_extern in current_settings.globalswitches) then
  1842. begin
  1843. include(current_settings.globalswitches,cs_link_strip);
  1844. { Warning stabs info does not work with reloc section !! }
  1845. if (cs_debuginfo in current_settings.moduleswitches) and
  1846. (target_dbg.id=dbg_stabs) then
  1847. begin
  1848. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1849. Message(parser_w_parser_win32_debug_needs_WN);
  1850. exclude(current_settings.moduleswitches,cs_debuginfo);
  1851. end;
  1852. end;
  1853. { get correct output names }
  1854. main_file := current_scanner.inputfile;
  1855. while assigned(main_file.next) do
  1856. main_file := main_file.next;
  1857. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1858. if islibrary then
  1859. begin
  1860. consume(_LIBRARY);
  1861. current_module.setmodulename(orgpattern);
  1862. current_module.islibrary:=true;
  1863. exportlib.preparelib(orgpattern);
  1864. if tf_library_needs_pic in target_info.flags then
  1865. include(current_settings.moduleswitches,cs_create_pic);
  1866. consume(_ID);
  1867. consume(_SEMICOLON);
  1868. end
  1869. else
  1870. { is there an program head ? }
  1871. if token=_PROGRAM then
  1872. begin
  1873. consume(_PROGRAM);
  1874. current_module.setmodulename(orgpattern);
  1875. if (target_info.system in systems_unit_program_exports) then
  1876. exportlib.preparelib(orgpattern);
  1877. consume(_ID);
  1878. if token=_LKLAMMER then
  1879. begin
  1880. consume(_LKLAMMER);
  1881. repeat
  1882. consume(_ID);
  1883. until not try_to_consume(_COMMA);
  1884. consume(_RKLAMMER);
  1885. end;
  1886. consume(_SEMICOLON);
  1887. end
  1888. else if (target_info.system in systems_unit_program_exports) then
  1889. exportlib.preparelib(current_module.realmodulename^);
  1890. { global switches are read, so further changes aren't allowed }
  1891. current_module.in_global:=false;
  1892. { setup things using the switches }
  1893. setupglobalswitches;
  1894. { set implementation flag }
  1895. current_module.in_interface:=false;
  1896. current_module.interface_compiled:=true;
  1897. { insert after the unit symbol tables the static symbol table }
  1898. { of the program }
  1899. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1900. { load standard units (system,objpas,profile unit) }
  1901. loaddefaultunits;
  1902. { Load units provided on the command line }
  1903. loadautounits;
  1904. {Load the units used by the program we compile.}
  1905. if token=_USES then
  1906. loadunits;
  1907. { reset ranges/stabs in exported definitions }
  1908. reset_all_defs;
  1909. { All units are read, now give them a number }
  1910. current_module.updatemaps;
  1911. {Insert the name of the main program into the symbol table.}
  1912. if current_module.realmodulename^<>'' then
  1913. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1914. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1915. symtablestack.push(current_module.localsymtable);
  1916. { create whole program optimisation information }
  1917. current_module.wpoinfo:=tunitwpoinfo.create;
  1918. { The program intialization needs an alias, so it can be called
  1919. from the bootstrap code.}
  1920. if islibrary then
  1921. begin
  1922. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1923. { Win32 startup code needs a single name }
  1924. if not(target_info.system in systems_darwin) then
  1925. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1926. else
  1927. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1928. end
  1929. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1930. begin
  1931. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1932. end
  1933. else
  1934. begin
  1935. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1936. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1937. end;
  1938. main_procinfo.parse_body;
  1939. { save file pos for debuginfo }
  1940. current_module.mainfilepos:=main_procinfo.entrypos;
  1941. { Generate specializations of objectdefs methods }
  1942. generate_specialization_procs;
  1943. { should we force unit initialization? }
  1944. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1945. if force_init_final then
  1946. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1947. { Add symbol to the exports section for win32 so smartlinking a
  1948. DLL will include the edata section }
  1949. if assigned(exportlib) and
  1950. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1951. ((current_module.flags and uf_has_exports)<>0) then
  1952. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1953. { finalize? }
  1954. if token=_FINALIZATION then
  1955. begin
  1956. { the uf_finalize flag is only set after we checked that it
  1957. wasn't empty }
  1958. { Parse the finalize }
  1959. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1960. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1961. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1962. finalize_procinfo.parse_body;
  1963. end
  1964. else
  1965. if force_init_final then
  1966. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1967. { the finalization routine of libraries is generic (and all libraries need to }
  1968. { be finalized, so they can finalize any units they use }
  1969. if (islibrary) then
  1970. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1971. { all labels must be defined before generating code }
  1972. if Errorcount=0 then
  1973. tstoredsymtable(current_module.localsymtable).checklabels;
  1974. { See remark in unit init/final }
  1975. main_procinfo.generate_code;
  1976. main_procinfo.resetprocdef;
  1977. release_main_proc(main_procinfo);
  1978. if assigned(init_procinfo) then
  1979. begin
  1980. init_procinfo.generate_code;
  1981. init_procinfo.resetprocdef;
  1982. release_main_proc(init_procinfo);
  1983. end;
  1984. if assigned(finalize_procinfo) then
  1985. begin
  1986. finalize_procinfo.generate_code;
  1987. finalize_procinfo.resetprocdef;
  1988. release_main_proc(finalize_procinfo);
  1989. end;
  1990. symtablestack.pop(current_module.localsymtable);
  1991. { consume the last point }
  1992. consume(_POINT);
  1993. if (Errorcount=0) then
  1994. begin
  1995. { test static symtable }
  1996. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1997. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1998. tstoredsymtable(current_module.localsymtable).check_forwards;
  1999. current_module.allunitsused;
  2000. end;
  2001. { leave when we got an error }
  2002. if (Errorcount>0) and not status.skip_error then
  2003. begin
  2004. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2005. status.skip_error:=true;
  2006. exit;
  2007. end;
  2008. { remove all unused units, this happens when units are removed
  2009. from the uses clause in the source and the ppu was already being loaded }
  2010. hp:=tmodule(loaded_units.first);
  2011. while assigned(hp) do
  2012. begin
  2013. hp2:=hp;
  2014. hp:=tmodule(hp.next);
  2015. if hp2.is_unit and
  2016. not assigned(hp2.globalsymtable) then
  2017. begin
  2018. loaded_units.remove(hp2);
  2019. unloaded_units.concat(hp2);
  2020. end;
  2021. end;
  2022. { do we need to add the variants unit? }
  2023. maybeloadvariantsunit;
  2024. { Now that everything has been compiled we know if we need resource
  2025. support. If not, remove the unit. }
  2026. resources_used:=MaybeRemoveResUnit;
  2027. linker.initsysinitunitname;
  2028. if target_info.system in systems_internal_sysinit then
  2029. begin
  2030. { add start/halt unit }
  2031. AddUnit(linker.sysinitunit);
  2032. end;
  2033. {$ifdef arm}
  2034. { Insert .pdata section for arm-wince.
  2035. It is needed for exception handling. }
  2036. if target_info.system in [system_arm_wince] then
  2037. InsertPData;
  2038. {$endif arm}
  2039. InsertThreadvars;
  2040. { generate pic helpers to load eip if necessary }
  2041. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  2042. { generate rtti/init tables }
  2043. write_persistent_type_info(current_module.localsymtable);
  2044. { if an Objective-C module, generate rtti and module info }
  2045. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2046. { generate wrappers for interfaces }
  2047. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  2048. { generate imports }
  2049. if current_module.ImportLibraryList.Count>0 then
  2050. importlib.generatelib;
  2051. { generate debuginfo }
  2052. if (cs_debuginfo in current_settings.moduleswitches) then
  2053. current_debuginfo.inserttypeinfo;
  2054. if islibrary or (target_info.system in systems_unit_program_exports) then
  2055. exportlib.generatelib;
  2056. { Reference all DEBUGINFO sections from the main .fpc section }
  2057. if (cs_debuginfo in current_settings.moduleswitches) then
  2058. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2059. { Resource strings }
  2060. GenerateResourceStrings;
  2061. { Windows widestring needing initialization }
  2062. InsertWideInits;
  2063. { insert Tables and StackLength }
  2064. insertinitfinaltable;
  2065. InsertThreadvarTablesTable;
  2066. InsertResourceTablesTable;
  2067. InsertWideInitsTablesTable;
  2068. insertmemorysizes;
  2069. { Insert symbol to resource info }
  2070. InsertResourceInfo(resources_used);
  2071. { create callframe info }
  2072. create_dwarf_frame;
  2073. { insert own objectfile }
  2074. insertobjectfile;
  2075. { assemble and link }
  2076. create_objectfile;
  2077. { We might need the symbols info if not using
  2078. the default do_extractsymbolinfo
  2079. which is a dummy function PM }
  2080. needsymbolinfo:=
  2081. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2082. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2083. { release all local symtables that are not needed anymore }
  2084. if (not needsymbolinfo) then
  2085. free_localsymtables(current_module.localsymtable);
  2086. { leave when we got an error }
  2087. if (Errorcount>0) and not status.skip_error then
  2088. begin
  2089. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2090. status.skip_error:=true;
  2091. exit;
  2092. end;
  2093. if (not current_module.is_unit) then
  2094. begin
  2095. { create the executable when we are at level 1 }
  2096. if (compile_level=1) then
  2097. begin
  2098. { create global resource file by collecting all resource files }
  2099. CollectResourceFiles;
  2100. { write .def file }
  2101. if (cs_link_deffile in current_settings.globalswitches) then
  2102. deffile.writefile;
  2103. { insert all .o files from all loaded units and
  2104. unload the units, we don't need them anymore.
  2105. Keep the current_module because that is still needed }
  2106. hp:=tmodule(loaded_units.first);
  2107. while assigned(hp) do
  2108. begin
  2109. linker.AddModuleFiles(hp);
  2110. hp2:=tmodule(hp.next);
  2111. if (hp<>current_module) and
  2112. (not needsymbolinfo) then
  2113. begin
  2114. loaded_units.remove(hp);
  2115. hp.free;
  2116. end;
  2117. hp:=hp2;
  2118. end;
  2119. { free also unneeded units we didn't free before }
  2120. if not needsymbolinfo then
  2121. unloaded_units.Clear;
  2122. { finally we can create a executable }
  2123. if DLLSource then
  2124. linker.MakeSharedLibrary
  2125. else
  2126. linker.MakeExecutable;
  2127. { collect all necessary information for whole-program optimization }
  2128. wpoinfomanager.extractwpoinfofromprogram;
  2129. end;
  2130. { Give Fatal with error count for linker errors }
  2131. if (Errorcount>0) and not status.skip_error then
  2132. begin
  2133. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2134. status.skip_error:=true;
  2135. end;
  2136. end;
  2137. end;
  2138. end.