pmodules.pas 83 KB

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