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,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 : 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. 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 begin
  441. if (paratargetdbg = dbg_stabs) then
  442. AddUnit('lineinfo')
  443. else
  444. AddUnit('lnfodwrf');
  445. end;
  446. { Valgrind requires c memory manager }
  447. if (cs_gdb_valgrind in current_settings.globalswitches) then
  448. AddUnit('CMem');
  449. {$ifdef cpufpemu}
  450. { Floating point emulation unit?
  451. softfpu must be in the system unit anyways (FK)
  452. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  453. AddUnit('SoftFpu');
  454. }
  455. {$endif cpufpemu}
  456. end;
  457. { Objpas unit? }
  458. if m_objpas in current_settings.modeswitches then
  459. AddUnit('ObjPas');
  460. { Macpas unit? }
  461. if m_mac in current_settings.modeswitches then
  462. AddUnit('MacPas');
  463. { Profile unit? Needed for go32v2 only }
  464. if (cs_profile in current_settings.moduleswitches) and
  465. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  466. AddUnit('Profile');
  467. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  468. begin
  469. AddUnit('FPCylix');
  470. AddUnit('DynLibs');
  471. end;
  472. end;
  473. procedure loadautounits;
  474. var
  475. hs,s : string;
  476. begin
  477. hs:=autoloadunits;
  478. repeat
  479. s:=GetToken(hs,',');
  480. if s='' then
  481. break;
  482. AddUnit(s);
  483. until false;
  484. end;
  485. procedure loadunits;
  486. var
  487. s,sorg : TIDString;
  488. fn : string;
  489. pu : tused_unit;
  490. hp2 : tmodule;
  491. unitsym : tunitsym;
  492. begin
  493. consume(_USES);
  494. repeat
  495. s:=pattern;
  496. sorg:=orgpattern;
  497. consume(_ID);
  498. { support "<unit> in '<file>'" construct, but not for tp7 }
  499. fn:='';
  500. if not(m_tp7 in current_settings.modeswitches) and
  501. try_to_consume(_OP_IN) then
  502. fn:=FixFileName(get_stringconst);
  503. { Give a warning if lineinfo is loaded }
  504. if s='LINEINFO' then begin
  505. Message(parser_w_no_lineinfo_use_switch);
  506. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  507. s := 'LNFODWRF';
  508. sorg := s;
  509. end;
  510. { Give a warning if objpas is loaded }
  511. if s='OBJPAS' then
  512. Message(parser_w_no_objpas_use_mode);
  513. { Using the unit itself is not possible }
  514. if (s<>current_module.modulename^) then
  515. begin
  516. { check if the unit is already used }
  517. hp2:=nil;
  518. pu:=tused_unit(current_module.used_units.first);
  519. while assigned(pu) do
  520. begin
  521. if (pu.u.modulename^=s) then
  522. begin
  523. hp2:=pu.u;
  524. break;
  525. end;
  526. pu:=tused_unit(pu.next);
  527. end;
  528. if not assigned(hp2) then
  529. hp2:=registerunit(current_module,sorg,fn)
  530. else
  531. Message1(sym_e_duplicate_id,s);
  532. { Create unitsym, we need to use the name as specified, we
  533. can not use the modulename because that can be different
  534. when -Un is used }
  535. unitsym:=tunitsym.create(sorg,nil);
  536. current_module.localsymtable.insert(unitsym);
  537. { the current module uses the unit hp2 }
  538. current_module.addusedunit(hp2,true,unitsym);
  539. end
  540. else
  541. Message1(sym_e_duplicate_id,s);
  542. if token=_COMMA then
  543. begin
  544. pattern:='';
  545. consume(_COMMA);
  546. end
  547. else
  548. break;
  549. until false;
  550. { Load the units }
  551. pu:=tused_unit(current_module.used_units.first);
  552. while assigned(pu) do
  553. begin
  554. { Only load the units that are in the current
  555. (interface/implementation) uses clause }
  556. if pu.in_uses and
  557. (pu.in_interface=current_module.in_interface) then
  558. begin
  559. tppumodule(pu.u).loadppu;
  560. { is our module compiled? then we can stop }
  561. if current_module.state=ms_compiled then
  562. exit;
  563. { add this unit to the dependencies }
  564. pu.u.adddependency(current_module);
  565. { save crc values }
  566. pu.checksum:=pu.u.crc;
  567. pu.interface_checksum:=pu.u.interface_crc;
  568. { connect unitsym to the module }
  569. pu.unitsym.module:=pu.u;
  570. { add to symtable stack }
  571. symtablestack.push(pu.u.globalsymtable);
  572. if (m_mac in current_settings.modeswitches) and
  573. assigned(pu.u.globalmacrosymtable) then
  574. macrosymtablestack.push(pu.u.globalmacrosymtable);
  575. end;
  576. pu:=tused_unit(pu.next);
  577. end;
  578. consume(_SEMICOLON);
  579. end;
  580. procedure reset_all_defs;
  581. procedure reset_used_unit_defs(hp:tmodule);
  582. var
  583. pu : tused_unit;
  584. begin
  585. pu:=tused_unit(hp.used_units.first);
  586. while assigned(pu) do
  587. begin
  588. if not pu.u.is_reset then
  589. begin
  590. { prevent infinte loop for circular dependencies }
  591. pu.u.is_reset:=true;
  592. if assigned(pu.u.globalsymtable) then
  593. begin
  594. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  595. reset_used_unit_defs(pu.u);
  596. end;
  597. end;
  598. pu:=tused_unit(pu.next);
  599. end;
  600. end;
  601. var
  602. hp2 : tmodule;
  603. begin
  604. hp2:=tmodule(loaded_units.first);
  605. while assigned(hp2) do
  606. begin
  607. hp2.is_reset:=false;
  608. hp2:=tmodule(hp2.next);
  609. end;
  610. reset_used_unit_defs(current_module);
  611. end;
  612. procedure free_localsymtables(st:TSymtable);
  613. var
  614. i : longint;
  615. def : tstoreddef;
  616. pd : tprocdef;
  617. begin
  618. for i:=0 to st.DefList.Count-1 do
  619. begin
  620. def:=tstoreddef(st.DefList[i]);
  621. if def.typ=procdef then
  622. begin
  623. pd:=tprocdef(def);
  624. if assigned(pd.localst) and
  625. (pd.localst.symtabletype<>staticsymtable) and
  626. not(po_inline in pd.procoptions) then
  627. begin
  628. free_localsymtables(pd.localst);
  629. pd.localst.free;
  630. pd.localst:=nil;
  631. end;
  632. end;
  633. end;
  634. end;
  635. procedure parse_implementation_uses;
  636. begin
  637. if token=_USES then
  638. loadunits;
  639. end;
  640. procedure setupglobalswitches;
  641. begin
  642. if (cs_create_pic in current_settings.moduleswitches) then
  643. begin
  644. def_system_macro('FPC_PIC');
  645. def_system_macro('PIC');
  646. end;
  647. end;
  648. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  649. var
  650. ps : tprocsym;
  651. pd : tprocdef;
  652. begin
  653. { there should be no current_procinfo available }
  654. if assigned(current_procinfo) then
  655. internalerror(200304275);
  656. {Generate a procsym for main}
  657. ps:=tprocsym.create('$'+name);
  658. { main are allways used }
  659. inc(ps.refs);
  660. st.insert(ps);
  661. pd:=tprocdef.create(main_program_level);
  662. include(pd.procoptions,po_global);
  663. pd.procsym:=ps;
  664. ps.ProcdefList.Add(pd);
  665. { set procdef options }
  666. pd.proctypeoption:=potype;
  667. pd.proccalloption:=pocall_default;
  668. include(pd.procoptions,po_hascallingconvention);
  669. pd.forwarddef:=false;
  670. pd.setmangledname(target_info.cprefix+name);
  671. pd.aliasnames.insert(pd.mangledname);
  672. handle_calling_convention(pd);
  673. { We don't need is a local symtable. Change it into the static
  674. symtable }
  675. pd.localst.free;
  676. pd.localst:=st;
  677. { set procinfo and current_procinfo.procdef }
  678. result:=tcgprocinfo(cprocinfo.create(nil));
  679. result.procdef:=pd;
  680. { main proc does always a call e.g. to init system unit }
  681. include(result.flags,pi_do_call);
  682. end;
  683. procedure release_main_proc(pi:tcgprocinfo);
  684. begin
  685. { remove localst as it was replaced by staticsymtable }
  686. pi.procdef.localst:=nil;
  687. { remove procinfo }
  688. current_module.procinfo:=nil;
  689. pi.free;
  690. pi:=nil;
  691. end;
  692. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  693. begin
  694. { update module flags }
  695. current_module.flags:=current_module.flags or flag;
  696. { create procdef }
  697. case flag of
  698. uf_init :
  699. begin
  700. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  701. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  702. end;
  703. uf_finalize :
  704. begin
  705. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  706. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  707. end;
  708. else
  709. internalerror(200304253);
  710. end;
  711. result.code:=cnothingnode.create;
  712. end;
  713. procedure copy_macro(p:TObject; arg:pointer);
  714. begin
  715. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  716. end;
  717. procedure proc_unit;
  718. function is_assembler_generated:boolean;
  719. var
  720. hal : tasmlisttype;
  721. begin
  722. result:=false;
  723. if Errorcount=0 then
  724. begin
  725. for hal:=low(TasmlistType) to high(TasmlistType) do
  726. if not current_asmdata.asmlists[hal].empty then
  727. begin
  728. result:=true;
  729. exit;
  730. end;
  731. end;
  732. end;
  733. var
  734. main_file: tinputfile;
  735. {$ifdef EXTDEBUG}
  736. store_crc,
  737. {$endif EXTDEBUG}
  738. store_interface_crc : cardinal;
  739. s1,s2 : ^string; {Saves stack space}
  740. force_init_final : boolean;
  741. init_procinfo,
  742. finalize_procinfo : tcgprocinfo;
  743. unitname8 : string[8];
  744. has_impl,ag: boolean;
  745. {$ifdef i386}
  746. gotvarsym : tstaticvarsym;
  747. {$endif i386}
  748. begin
  749. init_procinfo:=nil;
  750. finalize_procinfo:=nil;
  751. if m_mac in current_settings.modeswitches then
  752. current_module.mode_switch_allowed:= false;
  753. consume(_UNIT);
  754. if compile_level=1 then
  755. Status.IsExe:=false;
  756. if token=_ID then
  757. begin
  758. { create filenames and unit name }
  759. main_file := current_scanner.inputfile;
  760. while assigned(main_file.next) do
  761. main_file := main_file.next;
  762. new(s1);
  763. s1^:=current_module.modulename^;
  764. current_module.SetFileName(main_file.path^+main_file.name^,true);
  765. current_module.SetModuleName(orgpattern);
  766. { check for system unit }
  767. new(s2);
  768. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  769. unitname8:=copy(current_module.modulename^,1,8);
  770. if (cs_check_unit_name in current_settings.globalswitches) and
  771. (
  772. not(
  773. (current_module.modulename^=s2^) or
  774. (
  775. (length(current_module.modulename^)>8) and
  776. (unitname8=s2^)
  777. )
  778. )
  779. or
  780. (
  781. (length(s1^)>8) and
  782. (s1^<>current_module.modulename^)
  783. )
  784. ) then
  785. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  786. if (current_module.modulename^='SYSTEM') then
  787. include(current_settings.moduleswitches,cs_compilesystem);
  788. dispose(s2);
  789. dispose(s1);
  790. end;
  791. if (target_info.system in system_unit_program_exports) then
  792. exportlib.preparelib(current_module.realmodulename^);
  793. consume(_ID);
  794. consume(_SEMICOLON);
  795. consume(_INTERFACE);
  796. { global switches are read, so further changes aren't allowed }
  797. current_module.in_global:=false;
  798. { handle the global switches }
  799. setupglobalswitches;
  800. message1(unit_u_loading_interface_units,current_module.modulename^);
  801. { update status }
  802. status.currentmodule:=current_module.realmodulename^;
  803. { maybe turn off m_objpas if we are compiling objpas }
  804. if (current_module.modulename^='OBJPAS') then
  805. exclude(current_settings.modeswitches,m_objpas);
  806. { maybe turn off m_mac if we are compiling macpas }
  807. if (current_module.modulename^='MACPAS') then
  808. exclude(current_settings.modeswitches,m_mac);
  809. parse_only:=true;
  810. { generate now the global symboltable,
  811. define first as local to overcome dependency conflicts }
  812. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  813. { insert unitsym of this unit to prevent other units having
  814. the same name }
  815. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  816. { load default units, like the system unit }
  817. loaddefaultunits;
  818. { insert qualifier for the system unit (allows system.writeln) }
  819. if not(cs_compilesystem in current_settings.moduleswitches) and
  820. (token=_USES) then
  821. begin
  822. loadunits;
  823. { has it been compiled at a higher level ?}
  824. if current_module.state=ms_compiled then
  825. exit;
  826. end;
  827. { move the global symtable from the temporary local to global }
  828. current_module.globalsymtable:=current_module.localsymtable;
  829. current_module.localsymtable:=nil;
  830. reset_all_defs;
  831. { number all units, so we know if a unit is used by this unit or
  832. needs to be added implicitly }
  833. current_module.updatemaps;
  834. { ... parse the declarations }
  835. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  836. symtablestack.push(current_module.globalsymtable);
  837. read_interface_declarations;
  838. symtablestack.pop(current_module.globalsymtable);
  839. { Export macros defined in the interface for macpas. The macros
  840. are put in the globalmacrosymtable that will only be used by other
  841. units. The current unit continues to use the localmacrosymtable }
  842. if (m_mac in current_settings.modeswitches) then
  843. begin
  844. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  845. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  846. end;
  847. { leave when we got an error }
  848. if (Errorcount>0) and not status.skip_error then
  849. begin
  850. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  851. status.skip_error:=true;
  852. exit;
  853. end;
  854. { Our interface is compiled, generate CRC and switch to implementation }
  855. if not(cs_compilesystem in current_settings.moduleswitches) and
  856. (Errorcount=0) then
  857. tppumodule(current_module).getppucrc;
  858. current_module.in_interface:=false;
  859. current_module.interface_compiled:=true;
  860. { First reload all units depending on our interface, we need to do this
  861. in the implementation part to prevent errorneous circular references }
  862. reload_flagged_units;
  863. { Parse the implementation section }
  864. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  865. has_impl:= false
  866. else
  867. has_impl:= true;
  868. parse_only:=false;
  869. { generates static symbol table }
  870. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  871. {$ifdef i386}
  872. if cs_create_pic in current_settings.moduleswitches then
  873. begin
  874. { insert symbol for got access in assembler code}
  875. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  876. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  877. current_module.localsymtable.insert(gotvarsym);
  878. { avoid unnecessary warnings }
  879. gotvarsym.varstate:=vs_read;
  880. gotvarsym.refs:=1;
  881. end;
  882. {$endif i386}
  883. if has_impl then
  884. begin
  885. consume(_IMPLEMENTATION);
  886. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  887. { Read the implementation units }
  888. parse_implementation_uses;
  889. end;
  890. if current_module.state=ms_compiled then
  891. exit;
  892. { reset ranges/stabs in exported definitions }
  893. reset_all_defs;
  894. { All units are read, now give them a number }
  895. current_module.updatemaps;
  896. symtablestack.push(current_module.globalsymtable);
  897. symtablestack.push(current_module.localsymtable);
  898. if has_impl then
  899. begin
  900. Message1(parser_u_parsing_implementation,current_module.modulename^);
  901. if current_module.in_interface then
  902. internalerror(200212285);
  903. { Compile the unit }
  904. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  905. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  906. init_procinfo.parse_body;
  907. { save file pos for debuginfo }
  908. current_module.mainfilepos:=init_procinfo.entrypos;
  909. end;
  910. { Generate specializations of objectdefs methods }
  911. generate_specialization_procs;
  912. { if the unit contains ansi/widestrings, initialization and
  913. finalization code must be forced }
  914. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  915. tstaticsymtable(current_module.localsymtable).needs_init_final;
  916. { should we force unit initialization? }
  917. { this is a hack, but how can it be done better ? }
  918. if force_init_final and ((current_module.flags and uf_init)=0) then
  919. begin
  920. { first release the not used init procinfo }
  921. if assigned(init_procinfo) then
  922. release_main_proc(init_procinfo);
  923. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  924. end;
  925. { finalize? }
  926. if has_impl and (token=_FINALIZATION) then
  927. begin
  928. { set module options }
  929. current_module.flags:=current_module.flags or uf_finalize;
  930. { Compile the finalize }
  931. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  932. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  933. finalize_procinfo.parse_body;
  934. end
  935. else if force_init_final then
  936. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  937. { Now both init and finalize bodies are read and it is known
  938. which variables are used in both init and finalize we can now
  939. generate the code. This is required to prevent putting a variable in
  940. a register that is also used in the finalize body (PFV) }
  941. if assigned(init_procinfo) then
  942. begin
  943. init_procinfo.generate_code;
  944. init_procinfo.resetprocdef;
  945. release_main_proc(init_procinfo);
  946. end;
  947. if assigned(finalize_procinfo) then
  948. begin
  949. finalize_procinfo.generate_code;
  950. finalize_procinfo.resetprocdef;
  951. release_main_proc(finalize_procinfo);
  952. end;
  953. symtablestack.pop(current_module.localsymtable);
  954. symtablestack.pop(current_module.globalsymtable);
  955. { the last char should always be a point }
  956. consume(_POINT);
  957. if (Errorcount=0) then
  958. begin
  959. { tests, if all (interface) forwards are resolved }
  960. tstoredsymtable(current_module.globalsymtable).check_forwards;
  961. { check if all private fields are used }
  962. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  963. { remove cross unit overloads }
  964. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  965. { test static symtable }
  966. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  967. tstoredsymtable(current_module.localsymtable).allprivatesused;
  968. tstoredsymtable(current_module.localsymtable).check_forwards;
  969. tstoredsymtable(current_module.localsymtable).checklabels;
  970. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  971. { used units }
  972. current_module.allunitsused;
  973. end;
  974. { leave when we got an error }
  975. if (Errorcount>0) and not status.skip_error then
  976. begin
  977. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  978. status.skip_error:=true;
  979. exit;
  980. end;
  981. { do we need to add the variants unit? }
  982. maybeloadvariantsunit;
  983. { generate wrappers for interfaces }
  984. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  985. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  986. { generate pic helpers to load eip if necessary }
  987. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  988. { generate rtti/init tables }
  989. write_persistent_type_info(current_module.globalsymtable);
  990. write_persistent_type_info(current_module.localsymtable);
  991. { Tables }
  992. insertThreadVars;
  993. { Resource strings }
  994. GenerateResourceStrings;
  995. { generate debuginfo }
  996. if (cs_debuginfo in current_settings.moduleswitches) then
  997. debuginfo.inserttypeinfo;
  998. { generate imports }
  999. if current_module.ImportLibraryList.Count>0 then
  1000. importlib.generatelib;
  1001. { insert own objectfile, or say that it's in a library
  1002. (no check for an .o when loading) }
  1003. ag:=is_assembler_generated;
  1004. if ag then
  1005. insertobjectfile
  1006. else
  1007. begin
  1008. current_module.flags:=current_module.flags or uf_no_link;
  1009. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1010. end;
  1011. if ag then
  1012. begin
  1013. { create dwarf debuginfo }
  1014. create_dwarf;
  1015. { assemble }
  1016. create_objectfile;
  1017. end;
  1018. { Write out the ppufile after the object file has been created }
  1019. store_interface_crc:=current_module.interface_crc;
  1020. {$ifdef EXTDEBUG}
  1021. store_crc:=current_module.crc;
  1022. {$endif EXTDEBUG}
  1023. if (Errorcount=0) then
  1024. tppumodule(current_module).writeppu;
  1025. if not(cs_compilesystem in current_settings.moduleswitches) then
  1026. if store_interface_crc<>current_module.interface_crc then
  1027. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1028. {$ifdef EXTDEBUG}
  1029. if not(cs_compilesystem in current_settings.moduleswitches) then
  1030. if (store_crc<>current_module.crc) and simplify_ppu then
  1031. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1032. {$endif EXTDEBUG}
  1033. { release all overload references and local symtables that
  1034. are not needed anymore }
  1035. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1036. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1037. free_localsymtables(current_module.globalsymtable);
  1038. free_localsymtables(current_module.localsymtable);
  1039. { leave when we got an error }
  1040. if (Errorcount>0) and not status.skip_error then
  1041. begin
  1042. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1043. status.skip_error:=true;
  1044. exit;
  1045. end;
  1046. Message1(unit_u_finished_compiling,current_module.modulename^);
  1047. end;
  1048. procedure proc_program(islibrary : boolean);
  1049. var
  1050. main_file : tinputfile;
  1051. hp,hp2 : tmodule;
  1052. finalize_procinfo,
  1053. init_procinfo,
  1054. main_procinfo : tcgprocinfo;
  1055. force_init_final : boolean;
  1056. begin
  1057. DLLsource:=islibrary;
  1058. Status.IsLibrary:=IsLibrary;
  1059. Status.IsExe:=true;
  1060. parse_only:=false;
  1061. main_procinfo:=nil;
  1062. init_procinfo:=nil;
  1063. finalize_procinfo:=nil;
  1064. { DLL defaults to create reloc info }
  1065. if islibrary then
  1066. begin
  1067. if not RelocSectionSetExplicitly then
  1068. RelocSection:=true;
  1069. end;
  1070. { Relocation works only without stabs under Windows when }
  1071. { external linker (LD) is used. LD generates relocs for }
  1072. { stab sections which is not loaded in memory. It causes }
  1073. { AV error when DLL is loaded and relocation is needed. }
  1074. { Internal linker does not have this problem. }
  1075. if RelocSection and
  1076. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1077. (cs_link_extern in current_settings.globalswitches) then
  1078. begin
  1079. include(current_settings.globalswitches,cs_link_strip);
  1080. { Warning stabs info does not work with reloc section !! }
  1081. if cs_debuginfo in current_settings.moduleswitches then
  1082. begin
  1083. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1084. Message(parser_w_parser_win32_debug_needs_WN);
  1085. exclude(current_settings.moduleswitches,cs_debuginfo);
  1086. end;
  1087. end;
  1088. { get correct output names }
  1089. main_file := current_scanner.inputfile;
  1090. while assigned(main_file.next) do
  1091. main_file := main_file.next;
  1092. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1093. if islibrary then
  1094. begin
  1095. consume(_LIBRARY);
  1096. current_module.setmodulename(orgpattern);
  1097. current_module.islibrary:=true;
  1098. exportlib.preparelib(orgpattern);
  1099. if tf_library_needs_pic in target_info.flags then
  1100. include(current_settings.moduleswitches,cs_create_pic);
  1101. consume(_ID);
  1102. consume(_SEMICOLON);
  1103. end
  1104. else
  1105. { is there an program head ? }
  1106. if token=_PROGRAM then
  1107. begin
  1108. consume(_PROGRAM);
  1109. current_module.setmodulename(orgpattern);
  1110. if (target_info.system in system_unit_program_exports) then
  1111. exportlib.preparelib(orgpattern);
  1112. consume(_ID);
  1113. if token=_LKLAMMER then
  1114. begin
  1115. consume(_LKLAMMER);
  1116. repeat
  1117. consume(_ID);
  1118. until not try_to_consume(_COMMA);
  1119. consume(_RKLAMMER);
  1120. end;
  1121. consume(_SEMICOLON);
  1122. end
  1123. else if (target_info.system in system_unit_program_exports) then
  1124. exportlib.preparelib(current_module.realmodulename^);
  1125. { global switches are read, so further changes aren't allowed }
  1126. current_module.in_global:=false;
  1127. { setup things using the switches }
  1128. setupglobalswitches;
  1129. { set implementation flag }
  1130. current_module.in_interface:=false;
  1131. current_module.interface_compiled:=true;
  1132. { insert after the unit symbol tables the static symbol table }
  1133. { of the program }
  1134. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1135. { load standard units (system,objpas,profile unit) }
  1136. loaddefaultunits;
  1137. { Load units provided on the command line }
  1138. loadautounits;
  1139. {Load the units used by the program we compile.}
  1140. if token=_USES then
  1141. loadunits;
  1142. { reset ranges/stabs in exported definitions }
  1143. reset_all_defs;
  1144. { All units are read, now give them a number }
  1145. current_module.updatemaps;
  1146. {Insert the name of the main program into the symbol table.}
  1147. if current_module.realmodulename^<>'' then
  1148. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1149. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1150. symtablestack.push(current_module.localsymtable);
  1151. { The program intialization needs an alias, so it can be called
  1152. from the bootstrap code.}
  1153. if islibrary then
  1154. begin
  1155. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1156. { Win32 startup code needs a single name }
  1157. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1158. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1159. else
  1160. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1161. end
  1162. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1163. begin
  1164. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1165. end
  1166. else
  1167. begin
  1168. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1169. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1170. end;
  1171. main_procinfo.parse_body;
  1172. { save file pos for debuginfo }
  1173. current_module.mainfilepos:=main_procinfo.entrypos;
  1174. { Generate specializations of objectdefs methods }
  1175. generate_specialization_procs;
  1176. { should we force unit initialization? }
  1177. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1178. if force_init_final then
  1179. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1180. { Add symbol to the exports section for win32 so smartlinking a
  1181. DLL will include the edata section }
  1182. if assigned(exportlib) and
  1183. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1184. ((current_module.flags and uf_has_exports)<>0) then
  1185. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1186. { finalize? }
  1187. if token=_FINALIZATION then
  1188. begin
  1189. { set module options }
  1190. current_module.flags:=current_module.flags or uf_finalize;
  1191. { Parse the finalize }
  1192. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1193. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  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. 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. 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.