pmodules.pas 82 KB

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