pmodules.pas 53 KB

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