pmodules.pas 83 KB

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