pmodules.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. {
  2. Copyright (c) 1998-2002 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_program(islibrary : boolean);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,version,systems,tokens,
  26. cutils,cfileutl,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmdata,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,ncgutil,
  32. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  33. cresstr,procinfo,
  34. pexports,
  35. scanner,pbase,pexpr,psystem,psub,pdecsub,ptype;
  36. procedure create_objectfile;
  37. var
  38. DLLScanner : TDLLScanner;
  39. s : string;
  40. KeepShared : TCmdStrList;
  41. begin
  42. { try to create import entries from system dlls }
  43. if (tf_has_dllscanner in target_info.flags) and
  44. (not current_module.linkOtherSharedLibs.Empty) then
  45. begin
  46. { Init DLLScanner }
  47. if assigned(CDLLScanner[target_info.system]) then
  48. DLLScanner:=CDLLScanner[target_info.system].Create
  49. else
  50. internalerror(200104121);
  51. KeepShared:=TCmdStrList.Create;
  52. { Walk all shared libs }
  53. While not current_module.linkOtherSharedLibs.Empty do
  54. begin
  55. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  56. if not DLLScanner.scan(s) then
  57. KeepShared.Concat(s);
  58. end;
  59. DLLscanner.Free;
  60. { Recreate import section }
  61. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  62. begin
  63. if assigned(current_asmdata.asmlists[al_imports]) then
  64. current_asmdata.asmlists[al_imports].clear
  65. else
  66. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  67. importlib.generatelib;
  68. end;
  69. { Readd the not processed files }
  70. while not KeepShared.Empty do
  71. begin
  72. s:=KeepShared.GetFirst;
  73. current_module.linkOtherSharedLibs.add(s,link_always);
  74. end;
  75. KeepShared.Free;
  76. end;
  77. { Start and end module debuginfo, at least required for stabs
  78. to insert n_sourcefile lines }
  79. if (cs_debuginfo in current_settings.moduleswitches) or
  80. (cs_use_lineinfo in current_settings.globalswitches) then
  81. current_debuginfo.insertmoduleinfo;
  82. { create the .s file and assemble it }
  83. GenerateAsm(false);
  84. { Also create a smartlinked version ? }
  85. if create_smartlink_library then
  86. begin
  87. GenerateAsm(true);
  88. if (af_needar in target_asm.flags) then
  89. Linker.MakeStaticLibrary;
  90. end;
  91. { resource files }
  92. CompileResourceFiles;
  93. end;
  94. procedure insertobjectfile;
  95. { Insert the used object file for this unit in the used list for this unit }
  96. begin
  97. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  98. current_module.flags:=current_module.flags or uf_static_linked;
  99. if create_smartlink_library then
  100. begin
  101. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  102. current_module.flags:=current_module.flags or uf_smart_linked;
  103. end;
  104. end;
  105. procedure create_dwarf;
  106. begin
  107. { Dwarf conflicts with smartlinking in separate .a files }
  108. if create_smartlink_library then
  109. exit;
  110. { Call frame information }
  111. if (tf_needs_dwarf_cfi in target_info.flags) and
  112. (af_supports_dwarf in target_asm.flags) then
  113. begin
  114. current_asmdata.asmlists[al_dwarf]:=TAsmList.create;
  115. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  116. end;
  117. end;
  118. procedure InsertThreadvarTablesTable;
  119. var
  120. hp : tused_unit;
  121. ltvTables : TAsmList;
  122. count : longint;
  123. begin
  124. if (tf_section_threadvars in target_info.flags) then
  125. exit;
  126. ltvTables:=TAsmList.Create;
  127. count:=0;
  128. hp:=tused_unit(usedunits.first);
  129. while assigned(hp) do
  130. begin
  131. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  132. begin
  133. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  134. inc(count);
  135. end;
  136. hp:=tused_unit(hp.next);
  137. end;
  138. { Add program threadvars, if any }
  139. If (current_module.flags and uf_threadvars)=uf_threadvars then
  140. begin
  141. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  142. inc(count);
  143. end;
  144. { Insert TableCount at start }
  145. ltvTables.insert(Tai_const.Create_32bit(count));
  146. { insert in data segment }
  147. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  148. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(aint));
  149. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  150. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  151. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  152. ltvTables.free;
  153. end;
  154. procedure AddToThreadvarList(p:TObject;arg:pointer);
  155. var
  156. ltvTable : TAsmList;
  157. begin
  158. ltvTable:=TAsmList(arg);
  159. if (tsym(p).typ=staticvarsym) and
  160. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  161. begin
  162. { address of threadvar }
  163. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  164. { size of threadvar }
  165. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  166. end;
  167. end;
  168. procedure InsertThreadvars;
  169. var
  170. s : string;
  171. ltvTable : TAsmList;
  172. begin
  173. if (tf_section_threadvars in target_info.flags) then
  174. exit;
  175. ltvTable:=TAsmList.create;
  176. if assigned(current_module.globalsymtable) then
  177. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  178. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  179. if ltvTable.first<>nil then
  180. begin
  181. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  182. { end of the list marker }
  183. ltvTable.concat(tai_const.create_sym(nil));
  184. { add to datasegment }
  185. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  186. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(aint));
  187. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  188. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  189. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  190. current_module.flags:=current_module.flags or uf_threadvars;
  191. end;
  192. ltvTable.Free;
  193. end;
  194. Procedure InsertResourceInfo;
  195. var
  196. hp : tused_unit;
  197. found : Boolean;
  198. I : Integer;
  199. ResourceInfo : TAsmList;
  200. begin
  201. if target_res.id=res_elf then
  202. begin
  203. hp:=tused_unit(usedunits.first);
  204. found:=false;
  205. Found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  206. If not found then
  207. While Assigned(hp) and not Found do
  208. begin
  209. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  210. hp:=tused_unit(hp.next);
  211. end;
  212. ResourceInfo:=TAsmList.Create;
  213. if found then
  214. begin
  215. { Valid pointer to resource information }
  216. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  217. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0));
  218. {$ifdef EXTERNALRESPTRS}
  219. current_module.linkotherofiles.add('resptrs.o',link_always);
  220. {$else EXTERNALRESPTRS}
  221. new_section(ResourceInfo,sec_fpc,'resptrs',4);
  222. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESSYMBOL',AT_DATA,0));
  223. For I:=1 to 32 do
  224. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  225. {$endif EXTERNALRESPTRS}
  226. end
  227. else
  228. begin
  229. { Nil pointer to resource information }
  230. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  231. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  232. end;
  233. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  234. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  235. ResourceInfo.free;
  236. end;
  237. end;
  238. Procedure InsertResourceTablesTable;
  239. var
  240. hp : tmodule;
  241. ResourceStringTables : tasmlist;
  242. count : longint;
  243. begin
  244. ResourceStringTables:=tasmlist.Create;
  245. count:=0;
  246. hp:=tmodule(loaded_units.first);
  247. while assigned(hp) do
  248. begin
  249. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  250. begin
  251. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  252. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  253. inc(count);
  254. end;
  255. hp:=tmodule(hp.next);
  256. end;
  257. { Insert TableCount at start }
  258. ResourceStringTables.insert(Tai_const.Create_aint(count));
  259. { Add to data segment }
  260. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  261. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(aint));
  262. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  263. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  264. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  265. ResourceStringTables.free;
  266. end;
  267. procedure InsertInitFinalTable;
  268. var
  269. hp : tused_unit;
  270. unitinits : TAsmList;
  271. count : longint;
  272. begin
  273. unitinits:=TAsmList.Create;
  274. count:=0;
  275. hp:=tused_unit(usedunits.first);
  276. while assigned(hp) do
  277. begin
  278. { call the unit init code and make it external }
  279. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  280. begin
  281. if (hp.u.flags and uf_init)<>0 then
  282. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  283. else
  284. unitinits.concat(Tai_const.Create_sym(nil));
  285. if (hp.u.flags and uf_finalize)<>0 then
  286. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  287. else
  288. unitinits.concat(Tai_const.Create_sym(nil));
  289. inc(count);
  290. end;
  291. hp:=tused_unit(hp.next);
  292. end;
  293. { Insert initialization/finalization of the program }
  294. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  295. begin
  296. if (current_module.flags and uf_init)<>0 then
  297. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  298. else
  299. unitinits.concat(Tai_const.Create_sym(nil));
  300. if (current_module.flags and uf_finalize)<>0 then
  301. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  302. else
  303. unitinits.concat(Tai_const.Create_sym(nil));
  304. inc(count);
  305. end;
  306. { Insert TableCount,InitCount at start }
  307. unitinits.insert(Tai_const.Create_32bit(0));
  308. unitinits.insert(Tai_const.Create_32bit(count));
  309. { Add to data segment }
  310. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  311. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(aint));
  312. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  313. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  314. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  315. unitinits.free;
  316. end;
  317. procedure insertmemorysizes;
  318. {$IFDEF POWERPC}
  319. var
  320. stkcookie: string;
  321. {$ENDIF POWERPC}
  322. begin
  323. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  324. { Insert Ident of the compiler in the .fpc.version section }
  325. current_asmdata.asmlists[al_globals].concat(Tai_section.create(sec_fpc,'version',0));
  326. current_asmdata.asmlists[al_globals].concat(Tai_align.Create(const_align(32)));
  327. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  328. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  329. { stacksize can be specified and is now simulated }
  330. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(aint));
  331. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(aint)));
  332. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(stacksize));
  333. {$IFDEF POWERPC}
  334. { AmigaOS4 "stack cookie" support }
  335. if ( target_info.system = system_powerpc_amiga ) then
  336. begin
  337. { this symbol is needed to ignite powerpc amigaos' }
  338. { stack allocation magic for us with the given stack size. }
  339. { note: won't work for m68k amigaos or morphos. (KB) }
  340. str(stacksize,stkcookie);
  341. stkcookie:='$STACK: '+stkcookie+#0;
  342. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  343. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  344. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  345. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  346. end;
  347. {$ENDIF POWERPC}
  348. { Initial heapsize }
  349. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  350. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(aint));
  351. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(aint)));
  352. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(heapsize));
  353. { Initial heapsize }
  354. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  355. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  356. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  357. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  358. end;
  359. procedure AddUnit(const s:string);
  360. var
  361. hp : tppumodule;
  362. unitsym : tunitsym;
  363. begin
  364. { load unit }
  365. hp:=registerunit(current_module,s,'');
  366. hp.loadppu;
  367. hp.adddependency(current_module);
  368. { add to symtable stack }
  369. symtablestack.push(hp.globalsymtable);
  370. if (m_mac in current_settings.modeswitches) and
  371. assigned(hp.globalmacrosymtable) then
  372. macrosymtablestack.push(hp.globalmacrosymtable);
  373. { insert unitsym }
  374. unitsym:=tunitsym.create(s,hp);
  375. inc(unitsym.refs);
  376. current_module.localsymtable.insert(unitsym);
  377. { add to used units }
  378. current_module.addusedunit(hp,false,unitsym);
  379. end;
  380. procedure maybeloadvariantsunit;
  381. var
  382. hp : tmodule;
  383. begin
  384. { Do we need the variants unit? Skip this
  385. for VarUtils unit for bootstrapping }
  386. if (current_module.flags and uf_uses_variants=0) or
  387. (current_module.modulename^='VARUTILS') then
  388. exit;
  389. { Variants unit already loaded? }
  390. hp:=tmodule(loaded_units.first);
  391. while assigned(hp) do
  392. begin
  393. if hp.modulename^='VARIANTS' then
  394. exit;
  395. hp:=tmodule(hp.next);
  396. end;
  397. { Variants unit is not loaded yet, load it now }
  398. Message(parser_w_implicit_uses_of_variants_unit);
  399. AddUnit('Variants');
  400. end;
  401. procedure loaddefaultunits;
  402. begin
  403. { we are going to rebuild the symtablestack, clear it first }
  404. symtablestack.clear;
  405. macrosymtablestack.clear;
  406. { macro symtable }
  407. macrosymtablestack.push(initialmacrosymtable);
  408. { are we compiling the system unit? }
  409. if (cs_compilesystem in current_settings.moduleswitches) then
  410. begin
  411. systemunit:=tglobalsymtable(current_module.localsymtable);
  412. { create system defines }
  413. create_intern_symbols;
  414. create_intern_types;
  415. { Set the owner of errorsym and errortype to symtable to
  416. prevent crashes when accessing .owner }
  417. generrorsym.owner:=systemunit;
  418. generrordef.owner:=systemunit;
  419. exit;
  420. end;
  421. { insert the system unit, it is allways the first. Load also the
  422. internal types from the system unit }
  423. AddUnit('System');
  424. systemunit:=tglobalsymtable(symtablestack.top);
  425. load_intern_types;
  426. { Set the owner of errorsym and errortype to symtable to
  427. prevent crashes when accessing .owner }
  428. generrorsym.owner:=systemunit;
  429. generrordef.owner:=systemunit;
  430. { Units only required for main module }
  431. if not(current_module.is_unit) then
  432. begin
  433. { Heaptrc unit, load heaptrace before any other units especially objpas }
  434. if (cs_use_heaptrc in current_settings.globalswitches) then
  435. AddUnit('HeapTrc');
  436. { Lineinfo unit }
  437. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  438. if (paratargetdbg = dbg_stabs) then
  439. AddUnit('lineinfo')
  440. else
  441. AddUnit('lnfodwrf');
  442. end;
  443. { Valgrind requires c memory manager }
  444. if (cs_gdb_valgrind in current_settings.globalswitches) then
  445. AddUnit('CMem');
  446. {$ifdef cpufpemu}
  447. { Floating point emulation unit?
  448. softfpu must be in the system unit anyways (FK)
  449. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  450. AddUnit('SoftFpu');
  451. }
  452. {$endif cpufpemu}
  453. end;
  454. { Objpas unit? }
  455. if m_objpas in current_settings.modeswitches then
  456. AddUnit('ObjPas');
  457. { Macpas unit? }
  458. if m_mac in current_settings.modeswitches then
  459. AddUnit('MacPas');
  460. { Profile unit? Needed for go32v2 only }
  461. if (cs_profile in current_settings.moduleswitches) and
  462. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  463. AddUnit('Profile');
  464. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  465. begin
  466. AddUnit('FPCylix');
  467. AddUnit('DynLibs');
  468. end;
  469. end;
  470. procedure loadautounits;
  471. var
  472. hs,s : string;
  473. begin
  474. hs:=autoloadunits;
  475. repeat
  476. s:=GetToken(hs,',');
  477. if s='' then
  478. break;
  479. AddUnit(s);
  480. until false;
  481. end;
  482. procedure loadunits;
  483. var
  484. s,sorg : TIDString;
  485. fn : string;
  486. pu : tused_unit;
  487. hp2 : tmodule;
  488. unitsym : tunitsym;
  489. begin
  490. consume(_USES);
  491. repeat
  492. s:=pattern;
  493. sorg:=orgpattern;
  494. consume(_ID);
  495. { support "<unit> in '<file>'" construct, but not for tp7 }
  496. fn:='';
  497. if not(m_tp7 in current_settings.modeswitches) and
  498. try_to_consume(_OP_IN) then
  499. fn:=FixFileName(get_stringconst);
  500. { Give a warning if lineinfo is loaded }
  501. if s='LINEINFO' then begin
  502. Message(parser_w_no_lineinfo_use_switch);
  503. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  504. s := 'LNFODWRF';
  505. sorg := s;
  506. end;
  507. { Give a warning if objpas is loaded }
  508. if s='OBJPAS' then
  509. Message(parser_w_no_objpas_use_mode);
  510. { Using the unit itself is not possible }
  511. if (s<>current_module.modulename^) then
  512. begin
  513. { check if the unit is already used }
  514. hp2:=nil;
  515. pu:=tused_unit(current_module.used_units.first);
  516. while assigned(pu) do
  517. begin
  518. if (pu.u.modulename^=s) then
  519. begin
  520. hp2:=pu.u;
  521. break;
  522. end;
  523. pu:=tused_unit(pu.next);
  524. end;
  525. if not assigned(hp2) then
  526. hp2:=registerunit(current_module,sorg,fn)
  527. else
  528. Message1(sym_e_duplicate_id,s);
  529. { Create unitsym, we need to use the name as specified, we
  530. can not use the modulename because that can be different
  531. when -Un is used }
  532. unitsym:=tunitsym.create(sorg,nil);
  533. current_module.localsymtable.insert(unitsym);
  534. { the current module uses the unit hp2 }
  535. current_module.addusedunit(hp2,true,unitsym);
  536. end
  537. else
  538. Message1(sym_e_duplicate_id,s);
  539. if token=_COMMA then
  540. begin
  541. pattern:='';
  542. consume(_COMMA);
  543. end
  544. else
  545. break;
  546. until false;
  547. { Load the units }
  548. pu:=tused_unit(current_module.used_units.first);
  549. while assigned(pu) do
  550. begin
  551. { Only load the units that are in the current
  552. (interface/implementation) uses clause }
  553. if pu.in_uses and
  554. (pu.in_interface=current_module.in_interface) then
  555. begin
  556. tppumodule(pu.u).loadppu;
  557. { is our module compiled? then we can stop }
  558. if current_module.state=ms_compiled then
  559. exit;
  560. { add this unit to the dependencies }
  561. pu.u.adddependency(current_module);
  562. { save crc values }
  563. pu.checksum:=pu.u.crc;
  564. pu.interface_checksum:=pu.u.interface_crc;
  565. { connect unitsym to the module }
  566. pu.unitsym.module:=pu.u;
  567. { add to symtable stack }
  568. symtablestack.push(pu.u.globalsymtable);
  569. if (m_mac in current_settings.modeswitches) and
  570. assigned(pu.u.globalmacrosymtable) then
  571. macrosymtablestack.push(pu.u.globalmacrosymtable);
  572. end;
  573. pu:=tused_unit(pu.next);
  574. end;
  575. consume(_SEMICOLON);
  576. end;
  577. procedure reset_all_defs;
  578. procedure reset_used_unit_defs(hp:tmodule);
  579. var
  580. pu : tused_unit;
  581. begin
  582. pu:=tused_unit(hp.used_units.first);
  583. while assigned(pu) do
  584. begin
  585. if not pu.u.is_reset then
  586. begin
  587. { prevent infinte loop for circular dependencies }
  588. pu.u.is_reset:=true;
  589. if assigned(pu.u.globalsymtable) then
  590. begin
  591. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  592. reset_used_unit_defs(pu.u);
  593. end;
  594. end;
  595. pu:=tused_unit(pu.next);
  596. end;
  597. end;
  598. var
  599. hp2 : tmodule;
  600. begin
  601. hp2:=tmodule(loaded_units.first);
  602. while assigned(hp2) do
  603. begin
  604. hp2.is_reset:=false;
  605. hp2:=tmodule(hp2.next);
  606. end;
  607. reset_used_unit_defs(current_module);
  608. end;
  609. procedure free_localsymtables(st:TSymtable);
  610. var
  611. i : longint;
  612. def : tstoreddef;
  613. pd : tprocdef;
  614. begin
  615. for i:=0 to st.DefList.Count-1 do
  616. begin
  617. def:=tstoreddef(st.DefList[i]);
  618. if def.typ=procdef then
  619. begin
  620. pd:=tprocdef(def);
  621. if assigned(pd.localst) and
  622. (pd.localst.symtabletype<>staticsymtable) and
  623. not(po_inline in pd.procoptions) then
  624. begin
  625. free_localsymtables(pd.localst);
  626. pd.localst.free;
  627. pd.localst:=nil;
  628. end;
  629. end;
  630. end;
  631. end;
  632. procedure parse_implementation_uses;
  633. begin
  634. if token=_USES then
  635. loadunits;
  636. end;
  637. procedure setupglobalswitches;
  638. begin
  639. if (cs_create_pic in current_settings.moduleswitches) then
  640. begin
  641. def_system_macro('FPC_PIC');
  642. def_system_macro('PIC');
  643. end;
  644. end;
  645. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  646. var
  647. ps : tprocsym;
  648. pd : tprocdef;
  649. begin
  650. { there should be no current_procinfo available }
  651. if assigned(current_procinfo) then
  652. internalerror(200304275);
  653. {Generate a procsym for main}
  654. ps:=tprocsym.create('$'+name);
  655. { main are allways used }
  656. inc(ps.refs);
  657. st.insert(ps);
  658. pd:=tprocdef.create(main_program_level);
  659. include(pd.procoptions,po_global);
  660. pd.procsym:=ps;
  661. ps.ProcdefList.Add(pd);
  662. { set procdef options }
  663. pd.proctypeoption:=potype;
  664. pd.proccalloption:=pocall_default;
  665. include(pd.procoptions,po_hascallingconvention);
  666. pd.forwarddef:=false;
  667. pd.setmangledname(target_info.cprefix+name);
  668. pd.aliasnames.insert(pd.mangledname);
  669. handle_calling_convention(pd);
  670. { We don't need is a local symtable. Change it into the static
  671. symtable }
  672. pd.localst.free;
  673. pd.localst:=st;
  674. { set procinfo and current_procinfo.procdef }
  675. result:=tcgprocinfo(cprocinfo.create(nil));
  676. result.procdef:=pd;
  677. { main proc does always a call e.g. to init system unit }
  678. include(result.flags,pi_do_call);
  679. end;
  680. procedure release_main_proc(pi:tcgprocinfo);
  681. begin
  682. { remove localst as it was replaced by staticsymtable }
  683. pi.procdef.localst:=nil;
  684. { remove procinfo }
  685. current_module.procinfo:=nil;
  686. pi.free;
  687. pi:=nil;
  688. end;
  689. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  690. begin
  691. { update module flags }
  692. current_module.flags:=current_module.flags or flag;
  693. { create procdef }
  694. case flag of
  695. uf_init :
  696. begin
  697. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  698. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  699. end;
  700. uf_finalize :
  701. begin
  702. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  703. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  704. if (not current_module.is_unit) then
  705. result.procdef.aliasnames.insert('PASCALFINALIZE');
  706. end;
  707. else
  708. internalerror(200304253);
  709. end;
  710. result.code:=cnothingnode.create;
  711. end;
  712. procedure copy_macro(p:TObject; arg:pointer);
  713. begin
  714. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  715. end;
  716. procedure proc_unit;
  717. function is_assembler_generated:boolean;
  718. var
  719. hal : tasmlisttype;
  720. begin
  721. result:=false;
  722. if Errorcount=0 then
  723. begin
  724. for hal:=low(TasmlistType) to high(TasmlistType) do
  725. if not current_asmdata.asmlists[hal].empty then
  726. begin
  727. result:=true;
  728. exit;
  729. end;
  730. end;
  731. end;
  732. var
  733. main_file: tinputfile;
  734. {$ifdef EXTDEBUG}
  735. store_crc,
  736. {$endif EXTDEBUG}
  737. store_interface_crc : cardinal;
  738. s1,s2 : ^string; {Saves stack space}
  739. force_init_final : boolean;
  740. init_procinfo,
  741. finalize_procinfo : tcgprocinfo;
  742. unitname8 : string[8];
  743. has_impl,ag: boolean;
  744. {$ifdef i386}
  745. gotvarsym : tstaticvarsym;
  746. {$endif i386}
  747. begin
  748. init_procinfo:=nil;
  749. finalize_procinfo:=nil;
  750. if m_mac in current_settings.modeswitches then
  751. current_module.mode_switch_allowed:= false;
  752. consume(_UNIT);
  753. if compile_level=1 then
  754. Status.IsExe:=false;
  755. if token=_ID then
  756. begin
  757. { create filenames and unit name }
  758. main_file := current_scanner.inputfile;
  759. while assigned(main_file.next) do
  760. main_file := main_file.next;
  761. new(s1);
  762. s1^:=current_module.modulename^;
  763. current_module.SetFileName(main_file.path^+main_file.name^,true);
  764. current_module.SetModuleName(orgpattern);
  765. { check for system unit }
  766. new(s2);
  767. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  768. unitname8:=copy(current_module.modulename^,1,8);
  769. if (cs_check_unit_name in current_settings.globalswitches) and
  770. (
  771. not(
  772. (current_module.modulename^=s2^) or
  773. (
  774. (length(current_module.modulename^)>8) and
  775. (unitname8=s2^)
  776. )
  777. )
  778. or
  779. (
  780. (length(s1^)>8) and
  781. (s1^<>current_module.modulename^)
  782. )
  783. ) then
  784. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  785. if (current_module.modulename^='SYSTEM') then
  786. include(current_settings.moduleswitches,cs_compilesystem);
  787. dispose(s2);
  788. dispose(s1);
  789. end;
  790. if (target_info.system in system_unit_program_exports) then
  791. exportlib.preparelib(current_module.realmodulename^);
  792. consume(_ID);
  793. consume(_SEMICOLON);
  794. consume(_INTERFACE);
  795. { global switches are read, so further changes aren't allowed }
  796. current_module.in_global:=false;
  797. { handle the global switches }
  798. setupglobalswitches;
  799. message1(unit_u_loading_interface_units,current_module.modulename^);
  800. { update status }
  801. status.currentmodule:=current_module.realmodulename^;
  802. { maybe turn off m_objpas if we are compiling objpas }
  803. if (current_module.modulename^='OBJPAS') then
  804. exclude(current_settings.modeswitches,m_objpas);
  805. { maybe turn off m_mac if we are compiling macpas }
  806. if (current_module.modulename^='MACPAS') then
  807. exclude(current_settings.modeswitches,m_mac);
  808. parse_only:=true;
  809. { generate now the global symboltable,
  810. define first as local to overcome dependency conflicts }
  811. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  812. { insert unitsym of this unit to prevent other units having
  813. the same name }
  814. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  815. { load default units, like the system unit }
  816. loaddefaultunits;
  817. { insert qualifier for the system unit (allows system.writeln) }
  818. if not(cs_compilesystem in current_settings.moduleswitches) and
  819. (token=_USES) then
  820. begin
  821. loadunits;
  822. { has it been compiled at a higher level ?}
  823. if current_module.state=ms_compiled then
  824. exit;
  825. end;
  826. { move the global symtable from the temporary local to global }
  827. current_module.globalsymtable:=current_module.localsymtable;
  828. current_module.localsymtable:=nil;
  829. reset_all_defs;
  830. { number all units, so we know if a unit is used by this unit or
  831. needs to be added implicitly }
  832. current_module.updatemaps;
  833. { ... parse the declarations }
  834. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  835. symtablestack.push(current_module.globalsymtable);
  836. read_interface_declarations;
  837. symtablestack.pop(current_module.globalsymtable);
  838. { Export macros defined in the interface for macpas. The macros
  839. are put in the globalmacrosymtable that will only be used by other
  840. units. The current unit continues to use the localmacrosymtable }
  841. if (m_mac in current_settings.modeswitches) then
  842. begin
  843. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  844. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  845. end;
  846. { leave when we got an error }
  847. if (Errorcount>0) and not status.skip_error then
  848. begin
  849. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  850. status.skip_error:=true;
  851. exit;
  852. end;
  853. { Our interface is compiled, generate CRC and switch to implementation }
  854. if not(cs_compilesystem in current_settings.moduleswitches) and
  855. (Errorcount=0) then
  856. tppumodule(current_module).getppucrc;
  857. current_module.in_interface:=false;
  858. current_module.interface_compiled:=true;
  859. { First reload all units depending on our interface, we need to do this
  860. in the implementation part to prevent errorneous circular references }
  861. reload_flagged_units;
  862. { Parse the implementation section }
  863. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  864. has_impl:= false
  865. else
  866. has_impl:= true;
  867. parse_only:=false;
  868. { generates static symbol table }
  869. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  870. {$ifdef i386}
  871. if cs_create_pic in current_settings.moduleswitches then
  872. begin
  873. { insert symbol for got access in assembler code}
  874. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  875. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  876. current_module.localsymtable.insert(gotvarsym);
  877. { avoid unnecessary warnings }
  878. gotvarsym.varstate:=vs_read;
  879. gotvarsym.refs:=1;
  880. end;
  881. {$endif i386}
  882. if has_impl then
  883. begin
  884. consume(_IMPLEMENTATION);
  885. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  886. { Read the implementation units }
  887. parse_implementation_uses;
  888. end;
  889. if current_module.state=ms_compiled then
  890. exit;
  891. { reset ranges/stabs in exported definitions }
  892. reset_all_defs;
  893. { All units are read, now give them a number }
  894. current_module.updatemaps;
  895. symtablestack.push(current_module.globalsymtable);
  896. symtablestack.push(current_module.localsymtable);
  897. if has_impl then
  898. begin
  899. Message1(parser_u_parsing_implementation,current_module.modulename^);
  900. if current_module.in_interface then
  901. internalerror(200212285);
  902. { Compile the unit }
  903. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  904. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  905. init_procinfo.parse_body;
  906. { save file pos for debuginfo }
  907. current_module.mainfilepos:=init_procinfo.entrypos;
  908. end;
  909. { Generate specializations of objectdefs methods }
  910. generate_specialization_procs;
  911. { if the unit contains ansi/widestrings, initialization and
  912. finalization code must be forced }
  913. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  914. tstaticsymtable(current_module.localsymtable).needs_init_final;
  915. { should we force unit initialization? }
  916. { this is a hack, but how can it be done better ? }
  917. if force_init_final and ((current_module.flags and uf_init)=0) then
  918. begin
  919. { first release the not used init procinfo }
  920. if assigned(init_procinfo) then
  921. release_main_proc(init_procinfo);
  922. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  923. end;
  924. { finalize? }
  925. if has_impl and (token=_FINALIZATION) then
  926. begin
  927. { set module options }
  928. current_module.flags:=current_module.flags or uf_finalize;
  929. { Compile the finalize }
  930. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  931. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  932. finalize_procinfo.parse_body;
  933. end
  934. else if force_init_final then
  935. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  936. { Now both init and finalize bodies are read and it is known
  937. which variables are used in both init and finalize we can now
  938. generate the code. This is required to prevent putting a variable in
  939. a register that is also used in the finalize body (PFV) }
  940. if assigned(init_procinfo) then
  941. begin
  942. init_procinfo.generate_code;
  943. init_procinfo.resetprocdef;
  944. release_main_proc(init_procinfo);
  945. end;
  946. if assigned(finalize_procinfo) then
  947. begin
  948. finalize_procinfo.generate_code;
  949. finalize_procinfo.resetprocdef;
  950. release_main_proc(finalize_procinfo);
  951. end;
  952. symtablestack.pop(current_module.localsymtable);
  953. symtablestack.pop(current_module.globalsymtable);
  954. { the last char should always be a point }
  955. consume(_POINT);
  956. if (Errorcount=0) then
  957. begin
  958. { tests, if all (interface) forwards are resolved }
  959. tstoredsymtable(current_module.globalsymtable).check_forwards;
  960. { check if all private fields are used }
  961. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  962. { remove cross unit overloads }
  963. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  964. { test static symtable }
  965. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  966. tstoredsymtable(current_module.localsymtable).allprivatesused;
  967. tstoredsymtable(current_module.localsymtable).check_forwards;
  968. tstoredsymtable(current_module.localsymtable).checklabels;
  969. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  970. { used units }
  971. current_module.allunitsused;
  972. end;
  973. { leave when we got an error }
  974. if (Errorcount>0) and not status.skip_error then
  975. begin
  976. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  977. status.skip_error:=true;
  978. exit;
  979. end;
  980. { do we need to add the variants unit? }
  981. maybeloadvariantsunit;
  982. { generate wrappers for interfaces }
  983. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  984. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  985. { generate pic helpers to load eip if necessary }
  986. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  987. { generate rtti/init tables }
  988. write_persistent_type_info(current_module.globalsymtable);
  989. write_persistent_type_info(current_module.localsymtable);
  990. { Tables }
  991. insertThreadVars;
  992. { Resource strings }
  993. GenerateResourceStrings;
  994. { generate debuginfo }
  995. if (cs_debuginfo in current_settings.moduleswitches) then
  996. current_debuginfo.inserttypeinfo;
  997. { generate imports }
  998. if current_module.ImportLibraryList.Count>0 then
  999. importlib.generatelib;
  1000. { insert own objectfile, or say that it's in a library
  1001. (no check for an .o when loading) }
  1002. ag:=is_assembler_generated;
  1003. if ag then
  1004. insertobjectfile
  1005. else
  1006. begin
  1007. current_module.flags:=current_module.flags or uf_no_link;
  1008. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1009. end;
  1010. if ag then
  1011. begin
  1012. { create dwarf debuginfo }
  1013. create_dwarf;
  1014. { assemble }
  1015. create_objectfile;
  1016. end;
  1017. { Write out the ppufile after the object file has been created }
  1018. store_interface_crc:=current_module.interface_crc;
  1019. {$ifdef EXTDEBUG}
  1020. store_crc:=current_module.crc;
  1021. {$endif EXTDEBUG}
  1022. if (Errorcount=0) then
  1023. tppumodule(current_module).writeppu;
  1024. if not(cs_compilesystem in current_settings.moduleswitches) then
  1025. if store_interface_crc<>current_module.interface_crc then
  1026. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1027. {$ifdef EXTDEBUG}
  1028. if not(cs_compilesystem in current_settings.moduleswitches) then
  1029. if (store_crc<>current_module.crc) and simplify_ppu then
  1030. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1031. {$endif EXTDEBUG}
  1032. { release all overload references and local symtables that
  1033. are not needed anymore }
  1034. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1035. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1036. free_localsymtables(current_module.globalsymtable);
  1037. free_localsymtables(current_module.localsymtable);
  1038. { leave when we got an error }
  1039. if (Errorcount>0) and not status.skip_error then
  1040. begin
  1041. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1042. status.skip_error:=true;
  1043. exit;
  1044. end;
  1045. Message1(unit_u_finished_compiling,current_module.modulename^);
  1046. end;
  1047. procedure proc_program(islibrary : boolean);
  1048. var
  1049. main_file : tinputfile;
  1050. hp,hp2 : tmodule;
  1051. finalize_procinfo,
  1052. init_procinfo,
  1053. main_procinfo : tcgprocinfo;
  1054. force_init_final : boolean;
  1055. begin
  1056. DLLsource:=islibrary;
  1057. Status.IsLibrary:=IsLibrary;
  1058. Status.IsExe:=true;
  1059. parse_only:=false;
  1060. main_procinfo:=nil;
  1061. init_procinfo:=nil;
  1062. finalize_procinfo:=nil;
  1063. { DLL defaults to create reloc info }
  1064. if islibrary then
  1065. begin
  1066. if not RelocSectionSetExplicitly then
  1067. RelocSection:=true;
  1068. end;
  1069. { Relocation works only without stabs under Windows when }
  1070. { external linker (LD) is used. LD generates relocs for }
  1071. { stab sections which is not loaded in memory. It causes }
  1072. { AV error when DLL is loaded and relocation is needed. }
  1073. { Internal linker does not have this problem. }
  1074. if RelocSection and
  1075. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1076. (cs_link_extern in current_settings.globalswitches) then
  1077. begin
  1078. include(current_settings.globalswitches,cs_link_strip);
  1079. { Warning stabs info does not work with reloc section !! }
  1080. if cs_debuginfo in current_settings.moduleswitches then
  1081. begin
  1082. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1083. Message(parser_w_parser_win32_debug_needs_WN);
  1084. exclude(current_settings.moduleswitches,cs_debuginfo);
  1085. end;
  1086. end;
  1087. { get correct output names }
  1088. main_file := current_scanner.inputfile;
  1089. while assigned(main_file.next) do
  1090. main_file := main_file.next;
  1091. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1092. if islibrary then
  1093. begin
  1094. consume(_LIBRARY);
  1095. current_module.setmodulename(orgpattern);
  1096. current_module.islibrary:=true;
  1097. exportlib.preparelib(orgpattern);
  1098. if tf_library_needs_pic in target_info.flags then
  1099. include(current_settings.moduleswitches,cs_create_pic);
  1100. consume(_ID);
  1101. consume(_SEMICOLON);
  1102. end
  1103. else
  1104. { is there an program head ? }
  1105. if token=_PROGRAM then
  1106. begin
  1107. consume(_PROGRAM);
  1108. current_module.setmodulename(orgpattern);
  1109. if (target_info.system in system_unit_program_exports) then
  1110. exportlib.preparelib(orgpattern);
  1111. consume(_ID);
  1112. if token=_LKLAMMER then
  1113. begin
  1114. consume(_LKLAMMER);
  1115. repeat
  1116. consume(_ID);
  1117. until not try_to_consume(_COMMA);
  1118. consume(_RKLAMMER);
  1119. end;
  1120. consume(_SEMICOLON);
  1121. end
  1122. else if (target_info.system in system_unit_program_exports) then
  1123. exportlib.preparelib(current_module.realmodulename^);
  1124. { global switches are read, so further changes aren't allowed }
  1125. current_module.in_global:=false;
  1126. { setup things using the switches }
  1127. setupglobalswitches;
  1128. { set implementation flag }
  1129. current_module.in_interface:=false;
  1130. current_module.interface_compiled:=true;
  1131. { insert after the unit symbol tables the static symbol table }
  1132. { of the program }
  1133. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1134. { load standard units (system,objpas,profile unit) }
  1135. loaddefaultunits;
  1136. { Load units provided on the command line }
  1137. loadautounits;
  1138. {Load the units used by the program we compile.}
  1139. if token=_USES then
  1140. loadunits;
  1141. { reset ranges/stabs in exported definitions }
  1142. reset_all_defs;
  1143. { All units are read, now give them a number }
  1144. current_module.updatemaps;
  1145. {Insert the name of the main program into the symbol table.}
  1146. if current_module.realmodulename^<>'' then
  1147. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1148. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1149. symtablestack.push(current_module.localsymtable);
  1150. { The program intialization needs an alias, so it can be called
  1151. from the bootstrap code.}
  1152. if islibrary then
  1153. begin
  1154. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1155. { Win32 startup code needs a single name }
  1156. if not(target_info.system in systems_darwin) then
  1157. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1158. else
  1159. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1160. end
  1161. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1162. begin
  1163. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1164. end
  1165. else
  1166. begin
  1167. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1168. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1169. end;
  1170. main_procinfo.parse_body;
  1171. { save file pos for debuginfo }
  1172. current_module.mainfilepos:=main_procinfo.entrypos;
  1173. { Generate specializations of objectdefs methods }
  1174. generate_specialization_procs;
  1175. { should we force unit initialization? }
  1176. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1177. if force_init_final then
  1178. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1179. { Add symbol to the exports section for win32 so smartlinking a
  1180. DLL will include the edata section }
  1181. if assigned(exportlib) and
  1182. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1183. ((current_module.flags and uf_has_exports)<>0) then
  1184. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1185. { finalize? }
  1186. if token=_FINALIZATION then
  1187. begin
  1188. { set module options }
  1189. current_module.flags:=current_module.flags or uf_finalize;
  1190. { Parse the finalize }
  1191. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1192. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1193. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1194. finalize_procinfo.parse_body;
  1195. end
  1196. else
  1197. if force_init_final then
  1198. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1199. { all labels must be defined before generating code }
  1200. if Errorcount=0 then
  1201. tstoredsymtable(current_module.localsymtable).checklabels;
  1202. { See remark in unit init/final }
  1203. main_procinfo.generate_code;
  1204. main_procinfo.resetprocdef;
  1205. release_main_proc(main_procinfo);
  1206. if assigned(init_procinfo) then
  1207. begin
  1208. init_procinfo.generate_code;
  1209. init_procinfo.resetprocdef;
  1210. release_main_proc(init_procinfo);
  1211. end;
  1212. if assigned(finalize_procinfo) then
  1213. begin
  1214. finalize_procinfo.generate_code;
  1215. finalize_procinfo.resetprocdef;
  1216. release_main_proc(finalize_procinfo);
  1217. end;
  1218. symtablestack.pop(current_module.localsymtable);
  1219. { consume the last point }
  1220. consume(_POINT);
  1221. if (Errorcount=0) then
  1222. begin
  1223. { test static symtable }
  1224. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1225. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1226. tstoredsymtable(current_module.localsymtable).check_forwards;
  1227. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1228. current_module.allunitsused;
  1229. end;
  1230. { leave when we got an error }
  1231. if (Errorcount>0) and not status.skip_error then
  1232. begin
  1233. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1234. status.skip_error:=true;
  1235. exit;
  1236. end;
  1237. { remove all unused units, this happends when units are removed
  1238. from the uses clause in the source and the ppu was already being loaded }
  1239. hp:=tmodule(loaded_units.first);
  1240. while assigned(hp) do
  1241. begin
  1242. hp2:=hp;
  1243. hp:=tmodule(hp.next);
  1244. if hp2.is_unit and
  1245. not assigned(hp2.globalsymtable) then
  1246. loaded_units.remove(hp2);
  1247. end;
  1248. { do we need to add the variants unit? }
  1249. maybeloadvariantsunit;
  1250. linker.initsysinitunitname;
  1251. if target_info.system in system_internal_sysinit then
  1252. begin
  1253. { add start/halt unit }
  1254. AddUnit(linker.sysinitunit);
  1255. end;
  1256. {$ifdef arm}
  1257. { Insert .pdata section for arm-wince.
  1258. It is needed for exception handling. }
  1259. if target_info.system in [system_arm_wince] then
  1260. InsertPData;
  1261. {$endif arm}
  1262. InsertThreadvars;
  1263. { generate pic helpers to load eip if necessary }
  1264. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1265. { generate rtti/init tables }
  1266. write_persistent_type_info(current_module.localsymtable);
  1267. { generate wrappers for interfaces }
  1268. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1269. { generate imports }
  1270. if current_module.ImportLibraryList.Count>0 then
  1271. importlib.generatelib;
  1272. { generate debuginfo }
  1273. if (cs_debuginfo in current_settings.moduleswitches) then
  1274. current_debuginfo.inserttypeinfo;
  1275. if islibrary or (target_info.system in system_unit_program_exports) then
  1276. exportlib.generatelib;
  1277. { Reference all DEBUGINFO sections from the main .fpc section }
  1278. if (cs_debuginfo in current_settings.moduleswitches) then
  1279. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1280. { Resource strings }
  1281. GenerateResourceStrings;
  1282. { insert Tables and StackLength }
  1283. insertinitfinaltable;
  1284. InsertThreadvarTablesTable;
  1285. InsertResourceTablesTable;
  1286. insertmemorysizes;
  1287. { Insert symbol to resource info }
  1288. InsertResourceInfo;
  1289. { create dwarf debuginfo }
  1290. create_dwarf;
  1291. { insert own objectfile }
  1292. insertobjectfile;
  1293. { assemble and link }
  1294. create_objectfile;
  1295. { We might need the symbols info if not using
  1296. the default do_extractsymbolinfo
  1297. which is a dummy function PM }
  1298. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1299. { release all local symtables that are not needed anymore }
  1300. if (not needsymbolinfo) then
  1301. free_localsymtables(current_module.localsymtable);
  1302. { leave when we got an error }
  1303. if (Errorcount>0) and not status.skip_error then
  1304. begin
  1305. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1306. status.skip_error:=true;
  1307. exit;
  1308. end;
  1309. if (not current_module.is_unit) then
  1310. begin
  1311. { create the executable when we are at level 1 }
  1312. if (compile_level=1) then
  1313. begin
  1314. { create global resource file by collecting all resource files }
  1315. CollectResourceFiles;
  1316. { write .def file }
  1317. if (cs_link_deffile in current_settings.globalswitches) then
  1318. deffile.writefile;
  1319. { insert all .o files from all loaded units and
  1320. unload the units, we don't need them anymore.
  1321. Keep the current_module because that is still needed }
  1322. hp:=tmodule(loaded_units.first);
  1323. while assigned(hp) do
  1324. begin
  1325. linker.AddModuleFiles(hp);
  1326. hp2:=tmodule(hp.next);
  1327. if (hp<>current_module) and
  1328. (not needsymbolinfo) then
  1329. begin
  1330. loaded_units.remove(hp);
  1331. hp.free;
  1332. end;
  1333. hp:=hp2;
  1334. end;
  1335. { finally we can create a executable }
  1336. if DLLSource then
  1337. linker.MakeSharedLibrary
  1338. else
  1339. linker.MakeExecutable;
  1340. end;
  1341. { Give Fatal with error count for linker errors }
  1342. if (Errorcount>0) and not status.skip_error then
  1343. begin
  1344. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1345. status.skip_error:=true;
  1346. end;
  1347. end;
  1348. end;
  1349. end.