pmodules.pas 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  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]:=TAsmList.create;
  121. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  122. end;
  123. end;
  124. procedure InsertThreadvarTablesTable;
  125. var
  126. hp : tused_unit;
  127. ltvTables : TAsmList;
  128. count : longint;
  129. begin
  130. if (tf_section_threadvars in target_info.flags) then
  131. exit;
  132. ltvTables:=TAsmList.Create;
  133. count:=0;
  134. hp:=tused_unit(usedunits.first);
  135. while assigned(hp) do
  136. begin
  137. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  138. begin
  139. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  140. inc(count);
  141. end;
  142. hp:=tused_unit(hp.next);
  143. end;
  144. { Add program threadvars, if any }
  145. If (current_module.flags and uf_threadvars)=uf_threadvars then
  146. begin
  147. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  148. inc(count);
  149. end;
  150. { Insert TableCount at start }
  151. ltvTables.insert(Tai_const.Create_32bit(count));
  152. { insert in data segment }
  153. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  154. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(pint));
  155. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  156. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  157. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  158. ltvTables.free;
  159. end;
  160. procedure AddToThreadvarList(p:TObject;arg:pointer);
  161. var
  162. ltvTable : TAsmList;
  163. begin
  164. ltvTable:=TAsmList(arg);
  165. if (tsym(p).typ=staticvarsym) and
  166. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  167. begin
  168. { address of threadvar }
  169. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  170. { size of threadvar }
  171. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  172. end;
  173. end;
  174. procedure InsertThreadvars;
  175. var
  176. s : string;
  177. ltvTable : TAsmList;
  178. begin
  179. if (tf_section_threadvars in target_info.flags) then
  180. exit;
  181. ltvTable:=TAsmList.create;
  182. if assigned(current_module.globalsymtable) then
  183. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  184. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  185. if ltvTable.first<>nil then
  186. begin
  187. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  188. { end of the list marker }
  189. ltvTable.concat(tai_const.create_sym(nil));
  190. { add to datasegment }
  191. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  192. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  193. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  194. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  195. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  196. current_module.flags:=current_module.flags or uf_threadvars;
  197. end;
  198. ltvTable.Free;
  199. end;
  200. Function CheckResourcesUsed : boolean;
  201. var
  202. hp : tused_unit;
  203. found : Boolean;
  204. begin
  205. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  206. if not CheckResourcesUsed then exit;
  207. hp:=tused_unit(usedunits.first);
  208. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  209. If not found then
  210. While Assigned(hp) and not found do
  211. begin
  212. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  213. hp:=tused_unit(hp.next);
  214. end;
  215. CheckResourcesUsed:=found;
  216. end;
  217. Procedure InsertResourceInfo(ResourcesUsed : boolean);
  218. var
  219. I : Integer;
  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. has_impl,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. has_impl:= false
  931. else
  932. has_impl:= true;
  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 has_impl 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 has_impl 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 has_impl 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. { remove cross unit overloads }
  1029. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1030. { test static symtable }
  1031. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1032. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1033. tstoredsymtable(current_module.localsymtable).check_forwards;
  1034. tstoredsymtable(current_module.localsymtable).checklabels;
  1035. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1036. { used units }
  1037. current_module.allunitsused;
  1038. end;
  1039. { leave when we got an error }
  1040. if (Errorcount>0) and not status.skip_error then
  1041. begin
  1042. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1043. status.skip_error:=true;
  1044. exit;
  1045. end;
  1046. { do we need to add the variants unit? }
  1047. maybeloadvariantsunit;
  1048. { generate wrappers for interfaces }
  1049. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  1050. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1051. { generate pic helpers to load eip if necessary }
  1052. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1053. { generate rtti/init tables }
  1054. write_persistent_type_info(current_module.globalsymtable);
  1055. write_persistent_type_info(current_module.localsymtable);
  1056. { Tables }
  1057. insertThreadVars;
  1058. { Resource strings }
  1059. GenerateResourceStrings;
  1060. { generate debuginfo }
  1061. if (cs_debuginfo in current_settings.moduleswitches) then
  1062. current_debuginfo.inserttypeinfo;
  1063. { generate imports }
  1064. if current_module.ImportLibraryList.Count>0 then
  1065. importlib.generatelib;
  1066. { insert own objectfile, or say that it's in a library
  1067. (no check for an .o when loading) }
  1068. ag:=is_assembler_generated;
  1069. if ag then
  1070. insertobjectfile
  1071. else
  1072. begin
  1073. current_module.flags:=current_module.flags or uf_no_link;
  1074. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1075. end;
  1076. if ag then
  1077. begin
  1078. { create dwarf debuginfo }
  1079. create_dwarf;
  1080. { assemble }
  1081. create_objectfile;
  1082. end;
  1083. { Write out the ppufile after the object file has been created }
  1084. store_interface_crc:=current_module.interface_crc;
  1085. {$ifdef EXTDEBUG}
  1086. store_crc:=current_module.crc;
  1087. {$endif EXTDEBUG}
  1088. if (Errorcount=0) then
  1089. tppumodule(current_module).writeppu;
  1090. if not(cs_compilesystem in current_settings.moduleswitches) then
  1091. if store_interface_crc<>current_module.interface_crc then
  1092. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1093. {$ifdef EXTDEBUG}
  1094. if not(cs_compilesystem in current_settings.moduleswitches) then
  1095. if (store_crc<>current_module.crc) and simplify_ppu then
  1096. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1097. {$endif EXTDEBUG}
  1098. { release all overload references and local symtables that
  1099. are not needed anymore }
  1100. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1101. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1102. free_localsymtables(current_module.globalsymtable);
  1103. free_localsymtables(current_module.localsymtable);
  1104. { leave when we got an error }
  1105. if (Errorcount>0) and not status.skip_error then
  1106. begin
  1107. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1108. status.skip_error:=true;
  1109. exit;
  1110. end;
  1111. Message1(unit_u_finished_compiling,current_module.modulename^);
  1112. end;
  1113. procedure procexport(const s : string);
  1114. var
  1115. hp : texported_item;
  1116. begin
  1117. hp:=texported_item.create;
  1118. hp.name:=stringdup(s);
  1119. hp.options:=hp.options or eo_name;
  1120. exportlib.exportprocedure(hp);
  1121. end;
  1122. procedure varexport(const s : string);
  1123. var
  1124. hp : texported_item;
  1125. begin
  1126. hp:=texported_item.create;
  1127. hp.name:=stringdup(s);
  1128. hp.options:=hp.options or eo_name;
  1129. exportlib.exportvar(hp);
  1130. end;
  1131. procedure insert_export(sym : TObject;arg:pointer);
  1132. var
  1133. hp : texported_item;
  1134. i : longint;
  1135. item : TCmdStrListItem;
  1136. begin
  1137. case TSym(sym).typ of
  1138. { ignore: }
  1139. unitsym,
  1140. syssym,
  1141. constsym,
  1142. enumsym,
  1143. typesym:
  1144. ;
  1145. procsym:
  1146. begin
  1147. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1148. begin
  1149. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1150. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1151. ((tsymtable(arg).symtabletype=globalsymtable) or
  1152. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1153. ) then
  1154. begin
  1155. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1156. { walk through all aliases }
  1157. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1158. while assigned(item) do
  1159. begin
  1160. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1161. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1162. procexport(item.str);
  1163. item:=TCmdStrListItem(item.next);
  1164. end;
  1165. end;
  1166. end;
  1167. end;
  1168. staticvarsym:
  1169. begin
  1170. varexport(tsym(sym).mangledname);
  1171. end;
  1172. else
  1173. begin
  1174. writeln('unknown: ',ord(TSym(sym).typ));
  1175. end;
  1176. end;
  1177. end;
  1178. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1179. Var
  1180. MakeStatic : Boolean;
  1181. Var
  1182. buffer : array[0..$1fff] of byte;
  1183. inppu,
  1184. outppu : tppufile;
  1185. b,
  1186. untilb : byte;
  1187. l,m : longint;
  1188. f : file;
  1189. ext,
  1190. s : string;
  1191. ppuversion : dword;
  1192. begin
  1193. Result:=false;
  1194. MakeStatic:=False;
  1195. inppu:=tppufile.create(PPUFn);
  1196. if not inppu.openfile then
  1197. begin
  1198. inppu.free;
  1199. Comment(V_Error,'Could not open : '+PPUFn);
  1200. Exit;
  1201. end;
  1202. { Check the ppufile }
  1203. if not inppu.CheckPPUId then
  1204. begin
  1205. inppu.free;
  1206. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1207. Exit;
  1208. end;
  1209. ppuversion:=inppu.GetPPUVersion;
  1210. if ppuversion<CurrentPPUVersion then
  1211. begin
  1212. inppu.free;
  1213. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1214. Exit;
  1215. end;
  1216. { No .o file generated for this ppu, just skip }
  1217. if (inppu.header.flags and uf_no_link)<>0 then
  1218. begin
  1219. inppu.free;
  1220. Result:=true;
  1221. Exit;
  1222. end;
  1223. { Already a lib? }
  1224. if (inppu.header.flags and uf_in_library)<>0 then
  1225. begin
  1226. inppu.free;
  1227. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1228. Exit;
  1229. end;
  1230. { We need a static linked unit }
  1231. if (inppu.header.flags and uf_static_linked)=0 then
  1232. begin
  1233. inppu.free;
  1234. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1235. Exit;
  1236. end;
  1237. { Check if shared is allowed }
  1238. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1239. begin
  1240. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1241. MakeStatic:=true;
  1242. end;
  1243. { Create the new ppu }
  1244. if PPUFn=PPLFn then
  1245. outppu:=tppufile.create('ppumove.$$$')
  1246. else
  1247. outppu:=tppufile.create(PPLFn);
  1248. outppu.createfile;
  1249. { Create new header, with the new flags }
  1250. outppu.header:=inppu.header;
  1251. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1252. if MakeStatic then
  1253. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1254. else
  1255. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1256. { read until the object files are found }
  1257. untilb:=iblinkunitofiles;
  1258. repeat
  1259. b:=inppu.readentry;
  1260. if b in [ibendinterface,ibend] then
  1261. begin
  1262. inppu.free;
  1263. outppu.free;
  1264. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1265. Exit;
  1266. end;
  1267. if b<>untilb then
  1268. begin
  1269. repeat
  1270. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1271. outppu.putdata(buffer,l);
  1272. until l<sizeof(buffer);
  1273. outppu.writeentry(b);
  1274. end;
  1275. until (b=untilb);
  1276. { we have now reached the section for the files which need to be added,
  1277. now add them to the list }
  1278. case b of
  1279. iblinkunitofiles :
  1280. begin
  1281. { add all o files, and save the entry when not creating a static
  1282. library to keep staticlinking possible }
  1283. while not inppu.endofentry do
  1284. begin
  1285. s:=inppu.getstring;
  1286. m:=inppu.getlongint;
  1287. if not MakeStatic then
  1288. begin
  1289. outppu.putstring(s);
  1290. outppu.putlongint(m);
  1291. end;
  1292. current_module.linkotherofiles.add(s,link_always);;
  1293. end;
  1294. if not MakeStatic then
  1295. outppu.writeentry(b);
  1296. end;
  1297. { iblinkunitstaticlibs :
  1298. begin
  1299. AddToLinkFiles(ExtractLib(inppu.getstring));
  1300. if not inppu.endofentry then
  1301. begin
  1302. repeat
  1303. inppu.getdatabuf(buffer^,bufsize,l);
  1304. outppu.putdata(buffer^,l);
  1305. until l<bufsize;
  1306. outppu.writeentry(b);
  1307. end;
  1308. end; }
  1309. end;
  1310. { just add a new entry with the new lib }
  1311. if MakeStatic then
  1312. begin
  1313. outppu.putstring('imp'+current_module.realmodulename^);
  1314. outppu.putlongint(link_static);
  1315. outppu.writeentry(iblinkunitstaticlibs)
  1316. end
  1317. else
  1318. begin
  1319. outppu.putstring('imp'+current_module.realmodulename^);
  1320. outppu.putlongint(link_shared);
  1321. outppu.writeentry(iblinkunitsharedlibs);
  1322. end;
  1323. { read all entries until the end and write them also to the new ppu }
  1324. repeat
  1325. b:=inppu.readentry;
  1326. { don't write ibend, that's written automaticly }
  1327. if b<>ibend then
  1328. begin
  1329. if b=iblinkothersharedlibs then
  1330. begin
  1331. while not inppu.endofentry do
  1332. begin
  1333. s:=inppu.getstring;
  1334. m:=inppu.getlongint;
  1335. outppu.putstring(s);
  1336. outppu.putlongint(m);
  1337. { strip lib prefix }
  1338. if copy(s,1,3)='lib' then
  1339. delete(s,1,3);
  1340. ext:=ExtractFileExt(s);
  1341. if ext<>'' then
  1342. delete(s,length(s)-length(ext)+1,length(ext));
  1343. current_module.linkOtherSharedLibs.add(s,link_always);
  1344. end;
  1345. end
  1346. else
  1347. repeat
  1348. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1349. outppu.putdata(buffer,l);
  1350. until l<sizeof(buffer);
  1351. outppu.writeentry(b);
  1352. end;
  1353. until b=ibend;
  1354. { write the last stuff and close }
  1355. outppu.flush;
  1356. outppu.writeheader;
  1357. outppu.free;
  1358. inppu.free;
  1359. { rename }
  1360. if PPUFn=PPLFn then
  1361. begin
  1362. {$I-}
  1363. assign(f,PPUFn);
  1364. erase(f);
  1365. assign(f,'ppumove.$$$');
  1366. rename(f,PPUFn);
  1367. {$I+}
  1368. if ioresult<>0 then;
  1369. end;
  1370. Result:=True;
  1371. end;
  1372. procedure createimportlibfromexports;
  1373. var
  1374. hp : texported_item;
  1375. begin
  1376. hp:=texported_item(current_module._exports.first);
  1377. while assigned(hp) do
  1378. begin
  1379. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.index,hp.is_var,false);
  1380. hp:=texported_item(hp.next);
  1381. end;
  1382. end;
  1383. procedure proc_package;
  1384. var
  1385. main_file : tinputfile;
  1386. hp,hp2 : tmodule;
  1387. finalize_procinfo,
  1388. init_procinfo,
  1389. main_procinfo : tcgprocinfo;
  1390. force_init_final : boolean;
  1391. uu : tused_unit;
  1392. begin
  1393. Status.IsPackage:=true;
  1394. Status.IsExe:=true;
  1395. parse_only:=false;
  1396. main_procinfo:=nil;
  1397. init_procinfo:=nil;
  1398. finalize_procinfo:=nil;
  1399. if not RelocSectionSetExplicitly then
  1400. RelocSection:=true;
  1401. { Relocation works only without stabs under Windows when }
  1402. { external linker (LD) is used. LD generates relocs for }
  1403. { stab sections which is not loaded in memory. It causes }
  1404. { AV error when DLL is loaded and relocation is needed. }
  1405. { Internal linker does not have this problem. }
  1406. if RelocSection and
  1407. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1408. (cs_link_extern in current_settings.globalswitches) then
  1409. begin
  1410. include(current_settings.globalswitches,cs_link_strip);
  1411. { Warning stabs info does not work with reloc section !! }
  1412. if cs_debuginfo in current_settings.moduleswitches then
  1413. begin
  1414. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1415. Message(parser_w_parser_win32_debug_needs_WN);
  1416. exclude(current_settings.moduleswitches,cs_debuginfo);
  1417. end;
  1418. end;
  1419. { get correct output names }
  1420. main_file := current_scanner.inputfile;
  1421. while assigned(main_file.next) do
  1422. main_file := main_file.next;
  1423. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1424. consume(_ID);
  1425. current_module.setmodulename(orgpattern);
  1426. current_module.ispackage:=true;
  1427. exportlib.preparelib(orgpattern);
  1428. if tf_library_needs_pic in target_info.flags then
  1429. include(current_settings.moduleswitches,cs_create_pic);
  1430. consume(_ID);
  1431. consume(_SEMICOLON);
  1432. { global switches are read, so further changes aren't allowed }
  1433. current_module.in_global:=false;
  1434. { setup things using the switches }
  1435. setupglobalswitches;
  1436. { set implementation flag }
  1437. current_module.in_interface:=false;
  1438. current_module.interface_compiled:=true;
  1439. { insert after the unit symbol tables the static symbol table }
  1440. { of the program }
  1441. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1442. {Load the units used by the program we compile.}
  1443. if token=_REQUIRES then
  1444. begin
  1445. end;
  1446. {Load the units used by the program we compile.}
  1447. if (token=_ID) and (idtoken=_CONTAINS) then
  1448. begin
  1449. consume(_ID);
  1450. while true do
  1451. begin
  1452. if token=_ID then
  1453. AddUnit(pattern);
  1454. consume(_ID);
  1455. if token=_COMMA then
  1456. consume(_COMMA)
  1457. else break;
  1458. end;
  1459. consume(_SEMICOLON);
  1460. end;
  1461. { reset ranges/stabs in exported definitions }
  1462. reset_all_defs;
  1463. { All units are read, now give them a number }
  1464. current_module.updatemaps;
  1465. {Insert the name of the main program into the symbol table.}
  1466. if current_module.realmodulename^<>'' then
  1467. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1468. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1469. symtablestack.push(current_module.localsymtable);
  1470. { should we force unit initialization? }
  1471. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1472. if force_init_final then
  1473. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1474. { Add symbol to the exports section for win32 so smartlinking a
  1475. DLL will include the edata section }
  1476. if assigned(exportlib) and
  1477. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1478. ((current_module.flags and uf_has_exports)<>0) then
  1479. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1480. { all labels must be defined before generating code }
  1481. if Errorcount=0 then
  1482. tstoredsymtable(current_module.localsymtable).checklabels;
  1483. symtablestack.pop(current_module.localsymtable);
  1484. { consume the last point }
  1485. consume(_END);
  1486. consume(_POINT);
  1487. if (Errorcount=0) then
  1488. begin
  1489. { test static symtable }
  1490. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1491. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1492. tstoredsymtable(current_module.localsymtable).check_forwards;
  1493. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1494. current_module.allunitsused;
  1495. end;
  1496. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1497. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1498. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1499. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1500. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1501. {$ifdef i386}
  1502. { fix me! }
  1503. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1504. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const(A_RET,S_W,12));
  1505. {$endif i386}
  1506. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1507. { leave when we got an error }
  1508. if (Errorcount>0) and not status.skip_error then
  1509. begin
  1510. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1511. status.skip_error:=true;
  1512. exit;
  1513. end;
  1514. { remove all unused units, this happends when units are removed
  1515. from the uses clause in the source and the ppu was already being loaded }
  1516. hp:=tmodule(loaded_units.first);
  1517. while assigned(hp) do
  1518. begin
  1519. hp2:=hp;
  1520. hp:=tmodule(hp.next);
  1521. if hp2.is_unit and
  1522. not assigned(hp2.globalsymtable) then
  1523. loaded_units.remove(hp2);
  1524. end;
  1525. { force exports }
  1526. uu:=tused_unit(usedunits.first);
  1527. while assigned(uu) do
  1528. begin
  1529. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1530. { check localsymtable for exports too to get public symbols }
  1531. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1532. { create special exports }
  1533. if (uu.u.flags and uf_init)<>0 then
  1534. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1535. if (uu.u.flags and uf_finalize)<>0 then
  1536. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1537. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1538. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1539. uu:=tused_unit(uu.next);
  1540. end;
  1541. {$ifdef arm}
  1542. { Insert .pdata section for arm-wince.
  1543. It is needed for exception handling. }
  1544. if target_info.system in [system_arm_wince] then
  1545. InsertPData;
  1546. {$endif arm}
  1547. { generate debuginfo }
  1548. if (cs_debuginfo in current_settings.moduleswitches) then
  1549. current_debuginfo.inserttypeinfo;
  1550. exportlib.generatelib;
  1551. { write all our exports to the import library,
  1552. needs to be done after exportlib.generatelib; }
  1553. createimportlibfromexports;
  1554. { generate imports }
  1555. if current_module.ImportLibraryList.Count>0 then
  1556. importlib.generatelib;
  1557. { Reference all DEBUGINFO sections from the main .fpc section }
  1558. if (cs_debuginfo in current_settings.moduleswitches) then
  1559. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1560. { insert own objectfile }
  1561. insertobjectfile;
  1562. { assemble and link }
  1563. create_objectfile;
  1564. { We might need the symbols info if not using
  1565. the default do_extractsymbolinfo
  1566. which is a dummy function PM }
  1567. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1568. { release all local symtables that are not needed anymore }
  1569. if (not needsymbolinfo) then
  1570. free_localsymtables(current_module.localsymtable);
  1571. { leave when we got an error }
  1572. if (Errorcount>0) and not status.skip_error then
  1573. begin
  1574. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1575. status.skip_error:=true;
  1576. exit;
  1577. end;
  1578. if (not current_module.is_unit) then
  1579. begin
  1580. { finally rewrite all units included into the package }
  1581. uu:=tused_unit(usedunits.first);
  1582. while assigned(uu) do
  1583. begin
  1584. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1585. uu:=tused_unit(uu.next);
  1586. end;
  1587. { create the executable when we are at level 1 }
  1588. if (compile_level=1) then
  1589. begin
  1590. { create global resource file by collecting all resource files }
  1591. CollectResourceFiles;
  1592. { write .def file }
  1593. if (cs_link_deffile in current_settings.globalswitches) then
  1594. deffile.writefile;
  1595. { insert all .o files from all loaded units and
  1596. unload the units, we don't need them anymore.
  1597. Keep the current_module because that is still needed }
  1598. hp:=tmodule(loaded_units.first);
  1599. while assigned(hp) do
  1600. begin
  1601. { the package itself contains no code so far }
  1602. linker.AddModuleFiles(hp);
  1603. hp2:=tmodule(hp.next);
  1604. if (hp<>current_module) and
  1605. (not needsymbolinfo) then
  1606. begin
  1607. loaded_units.remove(hp);
  1608. hp.free;
  1609. end;
  1610. hp:=hp2;
  1611. end;
  1612. linker.MakeSharedLibrary
  1613. end;
  1614. { Give Fatal with error count for linker errors }
  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. end;
  1620. end;
  1621. end;
  1622. procedure proc_program(islibrary : boolean);
  1623. var
  1624. main_file : tinputfile;
  1625. hp,hp2 : tmodule;
  1626. finalize_procinfo,
  1627. init_procinfo,
  1628. main_procinfo : tcgprocinfo;
  1629. force_init_final : boolean;
  1630. resources_used : boolean;
  1631. begin
  1632. DLLsource:=islibrary;
  1633. Status.IsLibrary:=IsLibrary;
  1634. Status.IsPackage:=false;
  1635. Status.IsExe:=true;
  1636. parse_only:=false;
  1637. main_procinfo:=nil;
  1638. init_procinfo:=nil;
  1639. finalize_procinfo:=nil;
  1640. resources_used:=false;
  1641. { DLL defaults to create reloc info }
  1642. if islibrary then
  1643. begin
  1644. if not RelocSectionSetExplicitly then
  1645. RelocSection:=true;
  1646. end;
  1647. { Relocation works only without stabs under Windows when }
  1648. { external linker (LD) is used. LD generates relocs for }
  1649. { stab sections which is not loaded in memory. It causes }
  1650. { AV error when DLL is loaded and relocation is needed. }
  1651. { Internal linker does not have this problem. }
  1652. if RelocSection and
  1653. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1654. (cs_link_extern in current_settings.globalswitches) then
  1655. begin
  1656. include(current_settings.globalswitches,cs_link_strip);
  1657. { Warning stabs info does not work with reloc section !! }
  1658. if cs_debuginfo in current_settings.moduleswitches then
  1659. begin
  1660. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1661. Message(parser_w_parser_win32_debug_needs_WN);
  1662. exclude(current_settings.moduleswitches,cs_debuginfo);
  1663. end;
  1664. end;
  1665. { get correct output names }
  1666. main_file := current_scanner.inputfile;
  1667. while assigned(main_file.next) do
  1668. main_file := main_file.next;
  1669. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1670. if islibrary then
  1671. begin
  1672. consume(_LIBRARY);
  1673. current_module.setmodulename(orgpattern);
  1674. current_module.islibrary:=true;
  1675. exportlib.preparelib(orgpattern);
  1676. if tf_library_needs_pic in target_info.flags then
  1677. include(current_settings.moduleswitches,cs_create_pic);
  1678. consume(_ID);
  1679. consume(_SEMICOLON);
  1680. end
  1681. else
  1682. { is there an program head ? }
  1683. if token=_PROGRAM then
  1684. begin
  1685. consume(_PROGRAM);
  1686. current_module.setmodulename(orgpattern);
  1687. if (target_info.system in system_unit_program_exports) then
  1688. exportlib.preparelib(orgpattern);
  1689. consume(_ID);
  1690. if token=_LKLAMMER then
  1691. begin
  1692. consume(_LKLAMMER);
  1693. repeat
  1694. consume(_ID);
  1695. until not try_to_consume(_COMMA);
  1696. consume(_RKLAMMER);
  1697. end;
  1698. consume(_SEMICOLON);
  1699. end
  1700. else if (target_info.system in system_unit_program_exports) then
  1701. exportlib.preparelib(current_module.realmodulename^);
  1702. { global switches are read, so further changes aren't allowed }
  1703. current_module.in_global:=false;
  1704. { setup things using the switches }
  1705. setupglobalswitches;
  1706. { set implementation flag }
  1707. current_module.in_interface:=false;
  1708. current_module.interface_compiled:=true;
  1709. { insert after the unit symbol tables the static symbol table }
  1710. { of the program }
  1711. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1712. { load standard units (system,objpas,profile unit) }
  1713. loaddefaultunits;
  1714. { Load units provided on the command line }
  1715. loadautounits;
  1716. {Load the units used by the program we compile.}
  1717. if token=_USES then
  1718. loadunits;
  1719. { reset ranges/stabs in exported definitions }
  1720. reset_all_defs;
  1721. { All units are read, now give them a number }
  1722. current_module.updatemaps;
  1723. {Insert the name of the main program into the symbol table.}
  1724. if current_module.realmodulename^<>'' then
  1725. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1726. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1727. symtablestack.push(current_module.localsymtable);
  1728. { The program intialization needs an alias, so it can be called
  1729. from the bootstrap code.}
  1730. if islibrary then
  1731. begin
  1732. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1733. { Win32 startup code needs a single name }
  1734. if not(target_info.system in systems_darwin) then
  1735. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1736. else
  1737. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1738. end
  1739. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1740. begin
  1741. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1742. end
  1743. else
  1744. begin
  1745. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1746. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1747. end;
  1748. main_procinfo.parse_body;
  1749. { save file pos for debuginfo }
  1750. current_module.mainfilepos:=main_procinfo.entrypos;
  1751. { Generate specializations of objectdefs methods }
  1752. generate_specialization_procs;
  1753. { should we force unit initialization? }
  1754. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1755. if force_init_final then
  1756. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1757. { Add symbol to the exports section for win32 so smartlinking a
  1758. DLL will include the edata section }
  1759. if assigned(exportlib) and
  1760. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1761. ((current_module.flags and uf_has_exports)<>0) then
  1762. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1763. { finalize? }
  1764. if token=_FINALIZATION then
  1765. begin
  1766. { set module options }
  1767. current_module.flags:=current_module.flags or uf_finalize;
  1768. { Parse the finalize }
  1769. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1770. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1771. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1772. finalize_procinfo.parse_body;
  1773. end
  1774. else
  1775. if force_init_final then
  1776. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1777. { the finalization routine of libraries is generic (and all libraries need to }
  1778. { be finalized, so they can finalize any units they use }
  1779. if (islibrary) then
  1780. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1781. { all labels must be defined before generating code }
  1782. if Errorcount=0 then
  1783. tstoredsymtable(current_module.localsymtable).checklabels;
  1784. { See remark in unit init/final }
  1785. main_procinfo.generate_code;
  1786. main_procinfo.resetprocdef;
  1787. release_main_proc(main_procinfo);
  1788. if assigned(init_procinfo) then
  1789. begin
  1790. init_procinfo.generate_code;
  1791. init_procinfo.resetprocdef;
  1792. release_main_proc(init_procinfo);
  1793. end;
  1794. if assigned(finalize_procinfo) then
  1795. begin
  1796. finalize_procinfo.generate_code;
  1797. finalize_procinfo.resetprocdef;
  1798. release_main_proc(finalize_procinfo);
  1799. end;
  1800. symtablestack.pop(current_module.localsymtable);
  1801. { consume the last point }
  1802. consume(_POINT);
  1803. if (Errorcount=0) then
  1804. begin
  1805. { test static symtable }
  1806. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1807. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1808. tstoredsymtable(current_module.localsymtable).check_forwards;
  1809. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1810. current_module.allunitsused;
  1811. end;
  1812. { leave when we got an error }
  1813. if (Errorcount>0) and not status.skip_error then
  1814. begin
  1815. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1816. status.skip_error:=true;
  1817. exit;
  1818. end;
  1819. { remove all unused units, this happens when units are removed
  1820. from the uses clause in the source and the ppu was already being loaded }
  1821. hp:=tmodule(loaded_units.first);
  1822. while assigned(hp) do
  1823. begin
  1824. hp2:=hp;
  1825. hp:=tmodule(hp.next);
  1826. if hp2.is_unit and
  1827. not assigned(hp2.globalsymtable) then
  1828. begin
  1829. loaded_units.remove(hp2);
  1830. unloaded_units.concat(hp2);
  1831. end;
  1832. end;
  1833. { do we need to add the variants unit? }
  1834. maybeloadvariantsunit;
  1835. { Now that everything has been compiled we know if we need resource
  1836. support. If not, remove the unit. }
  1837. resources_used:=MaybeRemoveResUnit;
  1838. linker.initsysinitunitname;
  1839. if target_info.system in system_internal_sysinit then
  1840. begin
  1841. { add start/halt unit }
  1842. AddUnit(linker.sysinitunit);
  1843. end;
  1844. {$ifdef arm}
  1845. { Insert .pdata section for arm-wince.
  1846. It is needed for exception handling. }
  1847. if target_info.system in [system_arm_wince] then
  1848. InsertPData;
  1849. {$endif arm}
  1850. InsertThreadvars;
  1851. { generate pic helpers to load eip if necessary }
  1852. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1853. { generate rtti/init tables }
  1854. write_persistent_type_info(current_module.localsymtable);
  1855. { generate wrappers for interfaces }
  1856. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1857. { generate imports }
  1858. if current_module.ImportLibraryList.Count>0 then
  1859. importlib.generatelib;
  1860. { generate debuginfo }
  1861. if (cs_debuginfo in current_settings.moduleswitches) then
  1862. current_debuginfo.inserttypeinfo;
  1863. if islibrary or (target_info.system in system_unit_program_exports) then
  1864. exportlib.generatelib;
  1865. { Reference all DEBUGINFO sections from the main .fpc section }
  1866. if (cs_debuginfo in current_settings.moduleswitches) then
  1867. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1868. { Resource strings }
  1869. GenerateResourceStrings;
  1870. { insert Tables and StackLength }
  1871. insertinitfinaltable;
  1872. InsertThreadvarTablesTable;
  1873. InsertResourceTablesTable;
  1874. insertmemorysizes;
  1875. { Insert symbol to resource info }
  1876. InsertResourceInfo(resources_used);
  1877. { create dwarf debuginfo }
  1878. create_dwarf;
  1879. { insert own objectfile }
  1880. insertobjectfile;
  1881. { assemble and link }
  1882. create_objectfile;
  1883. { We might need the symbols info if not using
  1884. the default do_extractsymbolinfo
  1885. which is a dummy function PM }
  1886. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1887. { release all local symtables that are not needed anymore }
  1888. if (not needsymbolinfo) then
  1889. free_localsymtables(current_module.localsymtable);
  1890. { leave when we got an error }
  1891. if (Errorcount>0) and not status.skip_error then
  1892. begin
  1893. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1894. status.skip_error:=true;
  1895. exit;
  1896. end;
  1897. if (not current_module.is_unit) then
  1898. begin
  1899. { create the executable when we are at level 1 }
  1900. if (compile_level=1) then
  1901. begin
  1902. { create global resource file by collecting all resource files }
  1903. CollectResourceFiles;
  1904. { write .def file }
  1905. if (cs_link_deffile in current_settings.globalswitches) then
  1906. deffile.writefile;
  1907. { insert all .o files from all loaded units and
  1908. unload the units, we don't need them anymore.
  1909. Keep the current_module because that is still needed }
  1910. hp:=tmodule(loaded_units.first);
  1911. while assigned(hp) do
  1912. begin
  1913. linker.AddModuleFiles(hp);
  1914. hp2:=tmodule(hp.next);
  1915. if (hp<>current_module) and
  1916. (not needsymbolinfo) then
  1917. begin
  1918. loaded_units.remove(hp);
  1919. hp.free;
  1920. end;
  1921. hp:=hp2;
  1922. end;
  1923. { free also unneeded units we didn't free before }
  1924. if not needsymbolinfo then
  1925. unloaded_units.Clear;
  1926. { finally we can create a executable }
  1927. if DLLSource then
  1928. linker.MakeSharedLibrary
  1929. else
  1930. linker.MakeExecutable;
  1931. end;
  1932. { Give Fatal with error count for linker errors }
  1933. if (Errorcount>0) and not status.skip_error then
  1934. begin
  1935. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1936. status.skip_error:=true;
  1937. end;
  1938. end;
  1939. end;
  1940. end.