pmodules.pas 80 KB

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