pmodules.pas 56 KB

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