pmodules.pas 57 KB

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