pmodules.pas 81 KB

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