pmodules.pas 56 KB

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