pmodules.pas 56 KB

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