pmodules.pas 57 KB

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