pmodules.pas 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i fpcdefs.inc}
  20. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,ncgutil,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,procinfo,
  34. dwarf,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub,pdecsub;
  39. procedure fixseg(p:TAAsmoutput; sec:TAsmSectionType; secname: string);
  40. begin
  41. maybe_new_object_file(p);
  42. if target_info.system <> system_powerpc_macos then
  43. p.insert(Tai_section.Create(sec,'',0))
  44. else
  45. p.insert(Tai_section.Create(sec,secname,0));
  46. end;
  47. procedure create_objectfile;
  48. var
  49. DLLScanner : TDLLScanner;
  50. s : string;
  51. KeepShared : TStringList;
  52. begin
  53. { try to create import entries from system dlls }
  54. if target_info.DllScanSupported and
  55. (not current_module.linkOtherSharedLibs.Empty) then
  56. begin
  57. { Init DLLScanner }
  58. if assigned(CDLLScanner[target_info.system]) then
  59. DLLScanner:=CDLLScanner[target_info.system].Create
  60. else
  61. internalerror(200104121);
  62. KeepShared:=TStringList.Create;
  63. { Walk all shared libs }
  64. While not current_module.linkOtherSharedLibs.Empty do
  65. begin
  66. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  67. if not DLLScanner.scan(s) then
  68. KeepShared.Concat(s);
  69. end;
  70. DLLscanner.Free;
  71. { Recreate import section }
  72. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  73. begin
  74. if assigned(importssection)then
  75. importssection.clear
  76. else
  77. importssection:=taasmoutput.Create;
  78. importlib.generatelib;
  79. end;
  80. { Readd the not processed files }
  81. while not KeepShared.Empty do
  82. begin
  83. s:=KeepShared.GetFirst;
  84. current_module.linkOtherSharedLibs.add(s,link_allways);
  85. end;
  86. KeepShared.Free;
  87. end;
  88. { create the .s file and assemble it }
  89. GenerateAsm(false);
  90. { Also create a smartlinked version ? }
  91. if (cs_create_smart in aktmoduleswitches) then
  92. begin
  93. { regenerate the importssection for win32 }
  94. if assigned(importssection) and
  95. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  96. begin
  97. importsSection.clear;
  98. importlib.generatesmartlib;
  99. end;
  100. GenerateAsm(true);
  101. if (af_needar in target_asm.flags) then
  102. Linker.MakeStaticLibrary;
  103. end;
  104. { resource files }
  105. CompileResourceFiles;
  106. end;
  107. procedure insertobjectfile;
  108. { Insert the used object file for this unit in the used list for this unit }
  109. begin
  110. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  111. current_module.flags:=current_module.flags or uf_static_linked;
  112. if (cs_create_smart in aktmoduleswitches) then
  113. begin
  114. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  115. current_module.flags:=current_module.flags or uf_smart_linked;
  116. end;
  117. end;
  118. procedure create_dwarf;
  119. begin
  120. dwarflist:=taasmoutput.create;
  121. { Call frame information }
  122. if (tf_needs_dwarf_cfi in target_info.flags) and
  123. (af_supports_dwarf in target_asm.flags) then
  124. dwarfcfi.generate_code(dwarflist);
  125. end;
  126. procedure insertsegment;
  127. var
  128. oldaktfilepos : tfileposinfo;
  129. {Note: Sections get names in macos only.}
  130. begin
  131. { Insert Ident of the compiler }
  132. if (not (cs_create_smart in aktmoduleswitches))
  133. {$ifndef EXTDEBUG}
  134. and (not current_module.is_unit)
  135. {$endif}
  136. then
  137. begin
  138. { align the first data }
  139. dataSegment.insert(Tai_align.Create(const_align(32)));
  140. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  141. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  142. end;
  143. { align code segment }
  144. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  145. { Insert start and end of sections }
  146. fixseg(codesegment,sec_code,'____seg_code');
  147. fixseg(datasegment,sec_data,'____seg_data');
  148. fixseg(bsssegment,sec_bss,'____seg_bss');
  149. { we should use .rdata section for these two no ?
  150. .rdata is a read only data section (PM) }
  151. fixseg(rttilist,sec_data,'____seg_rtti');
  152. fixseg(consts,sec_data,'____seg_consts');
  153. fixseg(picdata,sec_data,'____seg_picdata');
  154. if assigned(resourcestringlist) then
  155. fixseg(resourcestringlist,sec_data,'____seg_resstrings');
  156. {$ifdef GDB}
  157. if assigned(debuglist) then
  158. begin
  159. oldaktfilepos:=aktfilepos;
  160. aktfilepos.line:=0;
  161. debugList.insert(Tai_symbol.Createname('gcc2_compiled',AT_DATA,0));
  162. debugList.insert(Tai_symbol.Createname('fpc_compiled',AT_DATA,0));
  163. fixseg(debuglist,sec_code,'____seg_debug');
  164. aktfilepos:=oldaktfilepos;
  165. end;
  166. {$endif GDB}
  167. end;
  168. procedure InsertThreadvarTablesTable;
  169. var
  170. hp : tused_unit;
  171. ltvTables : taasmoutput;
  172. count : longint;
  173. begin
  174. ltvTables:=TAAsmOutput.Create;
  175. count:=0;
  176. hp:=tused_unit(usedunits.first);
  177. while assigned(hp) do
  178. begin
  179. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  180. begin
  181. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,0));
  182. inc(count);
  183. end;
  184. hp:=tused_unit(hp.next);
  185. end;
  186. { Add program threadvars, if any }
  187. If (current_module.flags and uf_threadvars)=uf_threadvars then
  188. begin
  189. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,0));
  190. inc(count);
  191. end;
  192. { TableCount }
  193. ltvTables.insert(Tai_const.Create_32bit(count));
  194. ltvTables.insert(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  195. ltvTables.insert(Tai_align.Create(const_align(sizeof(aint))));
  196. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  197. { insert in data segment }
  198. maybe_new_object_file(dataSegment);
  199. dataSegment.concatlist(ltvTables);
  200. ltvTables.free;
  201. end;
  202. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  203. var
  204. ltvTable : taasmoutput;
  205. begin
  206. ltvTable:=taasmoutput(arg);
  207. if (tsym(p).typ=globalvarsym) and
  208. (vo_is_thread_var in tglobalvarsym(p).varoptions) then
  209. begin
  210. { address of threadvar }
  211. ltvTable.concat(tai_const.Createname(tglobalvarsym(p).mangledname,AT_DATA,0));
  212. { size of threadvar }
  213. ltvTable.concat(tai_const.create_32bit(tglobalvarsym(p).getsize));
  214. end;
  215. end;
  216. procedure InsertThreadvars;
  217. var
  218. s : string;
  219. ltvTable : TAAsmoutput;
  220. begin
  221. ltvTable:=TAAsmoutput.create;
  222. if assigned(current_module.globalsymtable) then
  223. current_module.globalsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  224. current_module.localsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  225. if ltvTable.first<>nil then
  226. begin
  227. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  228. { add begin and end of the list }
  229. ltvTable.insert(tai_symbol.Createname_global(s,AT_DATA,0));
  230. ltvTable.insert(Tai_align.Create(const_align(32)));
  231. ltvTable.concat(tai_const.create_sym(nil)); { end of list marker }
  232. ltvTable.concat(tai_symbol_end.createname(s));
  233. maybe_new_object_file(dataSegment);
  234. dataSegment.concatlist(ltvTable);
  235. current_module.flags:=current_module.flags or uf_threadvars;
  236. end;
  237. ltvTable.Free;
  238. end;
  239. Procedure InsertResourceTablesTable;
  240. var
  241. hp : tused_unit;
  242. ResourceStringTables : taasmoutput;
  243. count : longint;
  244. begin
  245. ResourceStringTables:=TAAsmOutput.Create;
  246. count:=0;
  247. hp:=tused_unit(usedunits.first);
  248. while assigned(hp) do
  249. begin
  250. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  251. begin
  252. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  253. inc(count);
  254. end;
  255. hp:=tused_unit(hp.next);
  256. end;
  257. { Add program resources, if any }
  258. If ResourceStringList<>Nil then
  259. begin
  260. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  261. Inc(Count);
  262. end;
  263. { TableCount }
  264. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  265. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  266. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  267. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  268. { insert in data segment }
  269. maybe_new_object_file(dataSegment);
  270. dataSegment.concatlist(ResourceStringTables);
  271. ResourceStringTables.free;
  272. end;
  273. procedure InsertInitFinalTable;
  274. var
  275. hp : tused_unit;
  276. unitinits : taasmoutput;
  277. count : longint;
  278. begin
  279. unitinits:=TAAsmOutput.Create;
  280. count:=0;
  281. hp:=tused_unit(usedunits.first);
  282. while assigned(hp) do
  283. begin
  284. { call the unit init code and make it external }
  285. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  286. begin
  287. if (hp.u.flags and uf_init)<>0 then
  288. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  289. else
  290. unitinits.concat(Tai_const.Create_sym(nil));
  291. if (hp.u.flags and uf_finalize)<>0 then
  292. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  293. else
  294. unitinits.concat(Tai_const.Create_sym(nil));
  295. inc(count);
  296. end;
  297. hp:=tused_unit(hp.next);
  298. end;
  299. { Insert initialization/finalization of the program }
  300. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  301. begin
  302. if (current_module.flags and uf_init)<>0 then
  303. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  304. else
  305. unitinits.concat(Tai_const.Create_sym(nil));
  306. if (current_module.flags and uf_finalize)<>0 then
  307. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  308. else
  309. unitinits.concat(Tai_const.Create_sym(nil));
  310. inc(count);
  311. end;
  312. { TableCount,InitCount }
  313. unitinits.insert(Tai_const.Create_32bit(0));
  314. unitinits.insert(Tai_const.Create_32bit(count));
  315. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  316. unitinits.insert(Tai_align.Create(const_align(4)));
  317. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  318. { insert in data segment }
  319. maybe_new_object_file(dataSegment);
  320. dataSegment.concatlist(unitinits);
  321. unitinits.free;
  322. end;
  323. procedure insertmemorysizes;
  324. begin
  325. { stacksize can be specified and is now simulated }
  326. dataSegment.concat(Tai_align.Create(const_align(4)));
  327. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  328. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  329. dataSegment.concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,4));
  330. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  331. end;
  332. procedure AddUnit(const s:string);
  333. var
  334. hp : tppumodule;
  335. unitsym : tunitsym;
  336. begin
  337. { load unit }
  338. hp:=registerunit(current_module,s,'');
  339. hp.loadppu;
  340. hp.adddependency(current_module);
  341. { add to symtable stack }
  342. tsymtable(hp.globalsymtable).next:=symtablestack;
  343. symtablestack:=hp.globalsymtable;
  344. if (m_mac in aktmodeswitches) and assigned(hp.globalmacrosymtable) then
  345. begin
  346. tsymtable(hp.globalmacrosymtable).next:=macrosymtablestack;
  347. macrosymtablestack:=hp.globalmacrosymtable;
  348. end;
  349. { insert unitsym }
  350. unitsym:=tunitsym.create(s,hp.globalsymtable);
  351. inc(unitsym.refs);
  352. refsymtable.insert(unitsym);
  353. { add to used units }
  354. current_module.addusedunit(hp,false,unitsym);
  355. end;
  356. procedure maybeloadvariantsunit;
  357. var
  358. hp : tmodule;
  359. begin
  360. { Do we need the variants unit? Skip this
  361. for VarUtils unit for bootstrapping }
  362. if (current_module.flags and uf_uses_variants=0) or
  363. (current_module.modulename^='VARUTILS') then
  364. exit;
  365. { Variants unit already loaded? }
  366. hp:=tmodule(loaded_units.first);
  367. while assigned(hp) do
  368. begin
  369. if hp.modulename^='VARIANTS' then
  370. exit;
  371. hp:=tmodule(hp.next);
  372. end;
  373. { Variants unit is not loaded yet, load it now }
  374. Message(parser_w_implicit_uses_of_variants_unit);
  375. AddUnit('Variants');
  376. end;
  377. procedure loaddefaultunits;
  378. begin
  379. { are we compiling the system unit? }
  380. if (cs_compilesystem in aktmoduleswitches) then
  381. begin
  382. { create system defines }
  383. createconstdefs;
  384. { we don't need to reset anything, it's already done in parser.pas }
  385. exit;
  386. end;
  387. { insert the system unit, it is allways the first }
  388. symtablestack:=nil;
  389. macrosymtablestack:=initialmacrosymtable;
  390. AddUnit('System');
  391. SystemUnit:=TGlobalSymtable(Symtablestack);
  392. { read default constant definitions }
  393. make_ref:=false;
  394. readconstdefs;
  395. make_ref:=true;
  396. { Set the owner of errorsym and errortype to symtable to
  397. prevent crashes when accessing .owner }
  398. generrorsym.owner:=systemunit;
  399. generrortype.def.owner:=systemunit;
  400. {$ifdef cpufpemu}
  401. { Floating point emulation unit? }
  402. if (cs_fp_emulation in aktmoduleswitches) then
  403. AddUnit('SoftFpu');
  404. {$endif cpufpemu}
  405. { Objpas unit? }
  406. if m_objpas in aktmodeswitches then
  407. AddUnit('ObjPas');
  408. { Macpas unit? }
  409. if m_mac in aktmodeswitches then
  410. AddUnit('MacPas');
  411. { Profile unit? Needed for go32v2 only }
  412. if (cs_profile in aktmoduleswitches) and
  413. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  414. AddUnit('Profile');
  415. { Units only required for main module }
  416. if not(current_module.is_unit) then
  417. begin
  418. { Heaptrc unit }
  419. if (cs_gdb_heaptrc in aktglobalswitches) then
  420. AddUnit('HeapTrc');
  421. { Lineinfo unit }
  422. if (cs_gdb_lineinfo in aktglobalswitches) then
  423. AddUnit('LineInfo');
  424. { Lineinfo unit }
  425. if (cs_gdb_valgrind in aktglobalswitches) then
  426. AddUnit('CMem');
  427. end;
  428. { save default symtablestack }
  429. defaultsymtablestack:=symtablestack;
  430. defaultmacrosymtablestack:=macrosymtablestack;
  431. end;
  432. procedure loadautounits;
  433. var
  434. hs,s : string;
  435. begin
  436. hs:=autoloadunits;
  437. repeat
  438. s:=GetToken(hs,',');
  439. if s='' then
  440. break;
  441. AddUnit(s);
  442. until false;
  443. end;
  444. procedure loadunits;
  445. var
  446. s,sorg : stringid;
  447. fn : string;
  448. pu : tused_unit;
  449. hp2 : tmodule;
  450. hp3 : tsymtable;
  451. unitsym : tunitsym;
  452. top_of_macrosymtable : tsymtable;
  453. begin
  454. consume(_USES);
  455. {$ifdef DEBUG}
  456. test_symtablestack;
  457. {$endif DEBUG}
  458. repeat
  459. s:=pattern;
  460. sorg:=orgpattern;
  461. consume(_ID);
  462. { support "<unit> in '<file>'" construct, but not for tp7 }
  463. if not(m_tp7 in aktmodeswitches) then
  464. begin
  465. if try_to_consume(_OP_IN) then
  466. fn:=FixFileName(get_stringconst)
  467. else
  468. fn:='';
  469. end;
  470. { Give a warning if objpas is loaded }
  471. if s='OBJPAS' then
  472. Message(parser_w_no_objpas_use_mode);
  473. { Using the unit itself is not possible }
  474. if (s<>current_module.modulename^) then
  475. begin
  476. { check if the unit is already used }
  477. hp2:=nil;
  478. pu:=tused_unit(current_module.used_units.first);
  479. while assigned(pu) do
  480. begin
  481. if (pu.u.modulename^=s) then
  482. begin
  483. hp2:=pu.u;
  484. break;
  485. end;
  486. pu:=tused_unit(pu.next);
  487. end;
  488. if not assigned(hp2) then
  489. hp2:=registerunit(current_module,sorg,fn)
  490. else
  491. Message1(sym_e_duplicate_id,s);
  492. { Create unitsym, we need to use the name as specified, we
  493. can not use the modulename because that can be different
  494. when -Un is used }
  495. unitsym:=tunitsym.create(sorg,nil);
  496. refsymtable.insert(unitsym);
  497. { the current module uses the unit hp2 }
  498. current_module.addusedunit(hp2,true,unitsym);
  499. end
  500. else
  501. Message1(sym_e_duplicate_id,s);
  502. if token=_COMMA then
  503. begin
  504. pattern:='';
  505. consume(_COMMA);
  506. end
  507. else
  508. break;
  509. until false;
  510. { Load the units }
  511. top_of_macrosymtable:= macrosymtablestack;
  512. pu:=tused_unit(current_module.used_units.first);
  513. while assigned(pu) do
  514. begin
  515. { Only load the units that are in the current
  516. (interface/implementation) uses clause }
  517. if pu.in_uses and
  518. (pu.in_interface=current_module.in_interface) then
  519. begin
  520. tppumodule(pu.u).loadppu;
  521. { is our module compiled? then we can stop }
  522. if current_module.state=ms_compiled then
  523. exit;
  524. { add this unit to the dependencies }
  525. pu.u.adddependency(current_module);
  526. { save crc values }
  527. pu.checksum:=pu.u.crc;
  528. pu.interface_checksum:=pu.u.interface_crc;
  529. { connect unitsym to the globalsymtable of the unit }
  530. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  531. end;
  532. pu:=tused_unit(pu.next);
  533. end;
  534. { set the symtable to systemunit so it gets reorderd correctly,
  535. then insert the units in the symtablestack }
  536. pu:=tused_unit(current_module.used_units.first);
  537. symtablestack:=defaultsymtablestack;
  538. macrosymtablestack:=defaultmacrosymtablestack;
  539. while assigned(pu) do
  540. begin
  541. if pu.in_uses then
  542. begin
  543. { Reinsert in symtablestack }
  544. hp3:=symtablestack;
  545. while assigned(hp3) do
  546. begin
  547. { insert units only once ! }
  548. if pu.u.globalsymtable=hp3 then
  549. break;
  550. hp3:=hp3.next;
  551. { unit isn't inserted }
  552. if hp3=nil then
  553. begin
  554. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  555. symtablestack:=tsymtable(pu.u.globalsymtable);
  556. if (m_mac in aktmodeswitches) and assigned(pu.u.globalmacrosymtable) then
  557. begin
  558. tsymtable(pu.u.globalmacrosymtable).next:=macrosymtablestack;
  559. macrosymtablestack:=tsymtable(pu.u.globalmacrosymtable);
  560. end;
  561. {$ifdef DEBUG}
  562. test_symtablestack;
  563. {$endif DEBUG}
  564. end;
  565. end;
  566. end;
  567. pu:=tused_unit(pu.next);
  568. end;
  569. if assigned (current_module.globalmacrosymtable) then
  570. top_of_macrosymtable.next.next:= macrosymtablestack
  571. else
  572. top_of_macrosymtable.next:= macrosymtablestack;
  573. macrosymtablestack:= top_of_macrosymtable;
  574. consume(_SEMICOLON);
  575. end;
  576. {$IfDef GDB}
  577. procedure write_gdb_info;
  578. procedure reset_unit_type_info;
  579. var
  580. hp : tmodule;
  581. begin
  582. hp:=tmodule(loaded_units.first);
  583. while assigned(hp) do
  584. begin
  585. hp.is_stab_written:=false;
  586. hp:=tmodule(hp.next);
  587. end;
  588. end;
  589. procedure write_used_unit_type_info(hp:tmodule);
  590. var
  591. pu : tused_unit;
  592. begin
  593. pu:=tused_unit(hp.used_units.first);
  594. while assigned(pu) do
  595. begin
  596. if not pu.u.is_stab_written then
  597. begin
  598. { prevent infinte loop for circular dependencies }
  599. pu.u.is_stab_written:=true;
  600. { write type info from used units, use a depth first
  601. strategy to reduce the recursion in writing all
  602. dependent stabs }
  603. write_used_unit_type_info(pu.u);
  604. if assigned(pu.u.globalsymtable) then
  605. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  606. end;
  607. pu:=tused_unit(pu.next);
  608. end;
  609. end;
  610. var
  611. vardebuglist : taasmoutput;
  612. storefilepos : tfileposinfo;
  613. begin
  614. if not (cs_debuginfo in aktmoduleswitches) then
  615. exit;
  616. storefilepos:=aktfilepos;
  617. aktfilepos:=current_module.mainfilepos;
  618. { include symbol that will be referenced from the program to be sure to
  619. include this debuginfo .o file }
  620. if current_module.is_unit then
  621. begin
  622. current_module.flags:=current_module.flags or uf_has_debuginfo;
  623. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.globalsymtable,''),AT_DATA,0));
  624. end
  625. else
  626. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  627. { first write all global/local symbols again to a temp list. This will flag
  628. all required tdefs. After that the temp list can be removed since the debuginfo is already
  629. written to the stabs when the variables/consts were written }
  630. {$warning Hack to get all needed types}
  631. vardebuglist:=taasmoutput.create;
  632. new_section(vardebuglist,sec_data,'',0);
  633. if assigned(current_module.globalsymtable) then
  634. tglobalsymtable(current_module.globalsymtable).concatstabto(vardebuglist);
  635. if assigned(current_module.localsymtable) then
  636. tstaticsymtable(current_module.localsymtable).concatstabto(vardebuglist);
  637. vardebuglist.free;
  638. { reset unit type info flag }
  639. reset_unit_type_info;
  640. { write used types from the used units }
  641. write_used_unit_type_info(current_module);
  642. { last write the types from this unit }
  643. if assigned(current_module.globalsymtable) then
  644. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  645. if assigned(current_module.localsymtable) then
  646. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  647. { include files }
  648. if (cs_gdb_dbx in aktglobalswitches) then
  649. begin
  650. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  651. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  652. tostr(tglobalsymtable(current_module.globalsymtable).moduleid))));
  653. debugList.concat(Tai_stabs.Create(strpnew('"'+
  654. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  655. tostr(N_EINCL)+',0,0,0')));
  656. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  657. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  658. do_count_dbx:=false;
  659. end;
  660. aktfilepos:=storefilepos;
  661. end;
  662. {$EndIf GDB}
  663. procedure reset_all_defs;
  664. procedure reset_used_unit_defs(hp:tmodule);
  665. var
  666. pu : tused_unit;
  667. begin
  668. pu:=tused_unit(hp.used_units.first);
  669. while assigned(pu) do
  670. begin
  671. if not pu.u.is_reset then
  672. begin
  673. { prevent infinte loop for circular dependencies }
  674. pu.u.is_reset:=true;
  675. if assigned(pu.u.globalsymtable) then
  676. begin
  677. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  678. reset_used_unit_defs(pu.u);
  679. end;
  680. end;
  681. pu:=tused_unit(pu.next);
  682. end;
  683. end;
  684. var
  685. hp2 : tmodule;
  686. begin
  687. hp2:=tmodule(loaded_units.first);
  688. while assigned(hp2) do
  689. begin
  690. hp2.is_reset:=false;
  691. hp2:=tmodule(hp2.next);
  692. end;
  693. reset_used_unit_defs(current_module);
  694. end;
  695. procedure free_localsymtables(st:tsymtable);
  696. var
  697. def : tstoreddef;
  698. pd : tprocdef;
  699. begin
  700. def:=tstoreddef(st.defindex.first);
  701. while assigned(def) do
  702. begin
  703. if def.deftype=procdef then
  704. begin
  705. pd:=tprocdef(def);
  706. if assigned(pd.localst) and
  707. (pd.localst.symtabletype<>staticsymtable) and
  708. not((pd.proccalloption=pocall_inline) or
  709. ((current_module.flags and uf_local_browser)<>0)) then
  710. begin
  711. free_localsymtables(pd.localst);
  712. pd.localst.free;
  713. pd.localst:=nil;
  714. end;
  715. end;
  716. def:=tstoreddef(def.indexnext);
  717. end;
  718. end;
  719. procedure parse_implementation_uses;
  720. begin
  721. if token=_USES then
  722. begin
  723. loadunits;
  724. {$ifdef DEBUG}
  725. test_symtablestack;
  726. {$endif DEBUG}
  727. end;
  728. end;
  729. procedure setupglobalswitches;
  730. begin
  731. { can't have local browser when no global browser }
  732. if (cs_local_browser in aktmoduleswitches) and
  733. not(cs_browser in aktmoduleswitches) then
  734. exclude(aktmoduleswitches,cs_local_browser);
  735. if (cs_create_pic in aktmoduleswitches) then
  736. def_system_macro('FPC_PIC');
  737. end;
  738. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  739. var
  740. stt : tsymtable;
  741. ps : tprocsym;
  742. pd : tprocdef;
  743. begin
  744. { there should be no current_procinfo available }
  745. if assigned(current_procinfo) then
  746. internalerror(200304275);
  747. {Generate a procsym for main}
  748. make_ref:=false;
  749. { try to insert in in static symtable ! }
  750. stt:=symtablestack;
  751. symtablestack:=st;
  752. { generate procsym }
  753. ps:=tprocsym.create('$'+name);
  754. { main are allways used }
  755. inc(ps.refs);
  756. symtablestack.insert(ps);
  757. pd:=tprocdef.create(main_program_level);
  758. include(pd.procoptions,po_global);
  759. pd.procsym:=ps;
  760. ps.addprocdef(pd);
  761. { restore symtable }
  762. make_ref:=true;
  763. symtablestack:=stt;
  764. { set procdef options }
  765. pd.proctypeoption:=potype;
  766. pd.proccalloption:=pocall_default;
  767. pd.forwarddef:=false;
  768. pd.setmangledname(target_info.cprefix+name);
  769. pd.aliasnames.insert(pd.mangledname);
  770. handle_calling_convention(pd);
  771. { We don't need is a local symtable. Change it into the static
  772. symtable }
  773. pd.localst.free;
  774. pd.localst:=st;
  775. { set procinfo and current_procinfo.procdef }
  776. current_procinfo:=cprocinfo.create(nil);
  777. current_module.procinfo:=current_procinfo;
  778. current_procinfo.procdef:=pd;
  779. { return procdef }
  780. create_main_proc:=pd;
  781. { main proc does always a call e.g. to init system unit }
  782. include(current_procinfo.flags,pi_do_call);
  783. end;
  784. procedure release_main_proc(pd:tprocdef);
  785. begin
  786. { this is a main proc, so there should be no parent }
  787. if not(assigned(current_procinfo)) or
  788. assigned(current_procinfo.parent) or
  789. not(current_procinfo.procdef=pd) then
  790. internalerror(200304276);
  791. { remove procinfo }
  792. current_module.procinfo:=nil;
  793. current_procinfo.free;
  794. current_procinfo:=nil;
  795. { remove localst as it was replaced by staticsymtable }
  796. pd.localst:=nil;
  797. end;
  798. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  799. var
  800. pd : tprocdef;
  801. begin
  802. { update module flags }
  803. current_module.flags:=current_module.flags or flag;
  804. { create procdef }
  805. case flag of
  806. uf_init :
  807. begin
  808. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  809. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  810. end;
  811. uf_finalize :
  812. begin
  813. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  814. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  815. end;
  816. else
  817. internalerror(200304253);
  818. end;
  819. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  820. tcgprocinfo(current_procinfo).generate_code;
  821. release_main_proc(pd);
  822. end;
  823. procedure delete_duplicate_macros(p:TNamedIndexItem; arg:pointer);
  824. var
  825. hp: tsymentry;
  826. begin
  827. hp:= current_module.localmacrosymtable.search(p.name);
  828. if assigned(hp) then
  829. current_module.localmacrosymtable.delete(hp);
  830. end;
  831. procedure proc_unit;
  832. function is_assembler_generated:boolean;
  833. begin
  834. is_assembler_generated:=(Errorcount=0) and
  835. not(
  836. codeSegment.empty and
  837. dataSegment.empty and
  838. bssSegment.empty and
  839. ((importssection=nil) or importsSection.empty) and
  840. ((resourcesection=nil) or resourceSection.empty) and
  841. ((resourcestringlist=nil) or resourcestringList.empty)
  842. );
  843. end;
  844. var
  845. main_file: tinputfile;
  846. st : tsymtable;
  847. unitst : tglobalsymtable;
  848. {$ifdef EXTDEBUG}
  849. store_crc,
  850. {$endif EXTDEBUG}
  851. store_interface_crc : cardinal;
  852. s1,s2 : ^string; {Saves stack space}
  853. force_init_final : boolean;
  854. pd : tprocdef;
  855. unitname8 : string[8];
  856. has_impl: boolean;
  857. begin
  858. if m_mac in aktmodeswitches then
  859. begin
  860. ConsolidateMode;
  861. current_module.mode_switch_allowed:= false;
  862. end;
  863. consume(_UNIT);
  864. if compile_level=1 then
  865. Status.IsExe:=false;
  866. if token=_ID then
  867. begin
  868. { create filenames and unit name }
  869. main_file := current_scanner.inputfile;
  870. while assigned(main_file.next) do
  871. main_file := main_file.next;
  872. new(s1);
  873. s1^:=current_module.modulename^;
  874. current_module.SetFileName(main_file.path^+main_file.name^,true);
  875. current_module.SetModuleName(orgpattern);
  876. { check for system unit }
  877. new(s2);
  878. s2^:=upper(SplitName(main_file.name^));
  879. unitname8:=copy(current_module.modulename^,1,8);
  880. if (cs_check_unit_name in aktglobalswitches) and
  881. (
  882. not(
  883. (current_module.modulename^=s2^) or
  884. (
  885. (length(current_module.modulename^)>8) and
  886. (unitname8=s2^)
  887. )
  888. )
  889. or
  890. (
  891. (length(s1^)>8) and
  892. (s1^<>current_module.modulename^)
  893. )
  894. ) then
  895. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  896. if (current_module.modulename^='SYSTEM') then
  897. include(aktmoduleswitches,cs_compilesystem);
  898. dispose(s2);
  899. dispose(s1);
  900. end;
  901. consume(_ID);
  902. consume(_SEMICOLON);
  903. consume(_INTERFACE);
  904. { global switches are read, so further changes aren't allowed }
  905. current_module.in_global:=false;
  906. { handle the global switches }
  907. ConsolidateMode;
  908. setupglobalswitches;
  909. message1(unit_u_loading_interface_units,current_module.modulename^);
  910. { update status }
  911. status.currentmodule:=current_module.realmodulename^;
  912. { maybe turn off m_objpas if we are compiling objpas }
  913. if (current_module.modulename^='OBJPAS') then
  914. exclude(aktmodeswitches,m_objpas);
  915. { maybe turn off m_mac if we are compiling macpas }
  916. if (current_module.modulename^='MACPAS') then
  917. exclude(aktmodeswitches,m_mac);
  918. parse_only:=true;
  919. { generate now the global symboltable }
  920. st:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  921. refsymtable:=st;
  922. unitst:=tglobalsymtable(st);
  923. { define first as local to overcome dependency conflicts }
  924. current_module.localsymtable:=st;
  925. { the unit name must be usable as a unit specifier }
  926. { inside the unit itself (PM) }
  927. { this also forbids to have another symbol }
  928. { with the same name as the unit }
  929. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  930. macrosymtablestack:= initialmacrosymtable;
  931. { load default units, like the system unit }
  932. loaddefaultunits;
  933. current_module.localmacrosymtable.next:=macrosymtablestack;
  934. if assigned(current_module.globalmacrosymtable) then
  935. begin
  936. current_module.globalmacrosymtable.next:= current_module.localmacrosymtable;
  937. macrosymtablestack:=current_module.globalmacrosymtable;
  938. end
  939. else
  940. macrosymtablestack:=current_module.localmacrosymtable;
  941. { reset }
  942. make_ref:=true;
  943. { insert qualifier for the system unit (allows system.writeln) }
  944. if not(cs_compilesystem in aktmoduleswitches) then
  945. begin
  946. if token=_USES then
  947. begin
  948. loadunits;
  949. { has it been compiled at a higher level ?}
  950. if current_module.state=ms_compiled then
  951. exit;
  952. end;
  953. { ... but insert the symbol table later }
  954. st.next:=symtablestack;
  955. symtablestack:=st;
  956. end
  957. else
  958. { while compiling a system unit, some types are directly inserted }
  959. begin
  960. st.next:=symtablestack;
  961. symtablestack:=st;
  962. insert_intern_types(st);
  963. end;
  964. { now we know the place to insert the constants }
  965. constsymtable:=symtablestack;
  966. { move the global symtab from the temporary local to global }
  967. current_module.globalsymtable:=current_module.localsymtable;
  968. current_module.localsymtable:=nil;
  969. reset_all_defs;
  970. { number all units, so we know if a unit is used by this unit or
  971. needs to be added implicitly }
  972. current_module.updatemaps;
  973. { ... parse the declarations }
  974. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  975. read_interface_declarations;
  976. { leave when we got an error }
  977. if (Errorcount>0) and not status.skip_error then
  978. begin
  979. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  980. status.skip_error:=true;
  981. exit;
  982. end;
  983. { Our interface is compiled, generate CRC and switch to implementation }
  984. if not(cs_compilesystem in aktmoduleswitches) and
  985. (Errorcount=0) then
  986. tppumodule(current_module).getppucrc;
  987. current_module.in_interface:=false;
  988. current_module.interface_compiled:=true;
  989. { First reload all units depending on our interface, we need to do this
  990. in the implementation part to prevent errorneous circular references }
  991. reload_flagged_units;
  992. { Parse the implementation section }
  993. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  994. has_impl:= false
  995. else
  996. has_impl:= true;
  997. parse_only:=false;
  998. { generates static symbol table }
  999. st:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1000. current_module.localsymtable:=st;
  1001. { Swap the positions of the local and global macro sym table}
  1002. if assigned(current_module.globalmacrosymtable) then
  1003. begin
  1004. macrosymtablestack:=current_module.localmacrosymtable;
  1005. current_module.globalmacrosymtable.next:= current_module.localmacrosymtable.next;
  1006. current_module.localmacrosymtable.next:=current_module.globalmacrosymtable;
  1007. current_module.globalmacrosymtable.foreach_static(@delete_duplicate_macros, nil);
  1008. end;
  1009. { remove the globalsymtable from the symtable stack }
  1010. { to reinsert it after loading the implementation units }
  1011. symtablestack:=unitst.next;
  1012. { we don't want implementation units symbols in unitsymtable !! PM }
  1013. refsymtable:=st;
  1014. if has_impl then
  1015. begin
  1016. consume(_IMPLEMENTATION);
  1017. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1018. { Read the implementation units }
  1019. parse_implementation_uses;
  1020. end;
  1021. if current_module.state=ms_compiled then
  1022. exit;
  1023. { reset ranges/stabs in exported definitions }
  1024. reset_all_defs;
  1025. { All units are read, now give them a number }
  1026. current_module.updatemaps;
  1027. { now we can change refsymtable }
  1028. refsymtable:=st;
  1029. { but reinsert the global symtable as lasts }
  1030. unitst.next:=symtablestack;
  1031. symtablestack:=unitst;
  1032. {$ifdef DEBUG}
  1033. test_symtablestack;
  1034. {$endif DEBUG}
  1035. constsymtable:=symtablestack;
  1036. if has_impl then
  1037. begin
  1038. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1039. if current_module.in_interface then
  1040. internalerror(200212285);
  1041. { Compile the unit }
  1042. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  1043. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1044. tcgprocinfo(current_procinfo).parse_body;
  1045. tcgprocinfo(current_procinfo).generate_code;
  1046. tcgprocinfo(current_procinfo).resetprocdef;
  1047. { save file pos for debuginfo }
  1048. current_module.mainfilepos:=current_procinfo.entrypos;
  1049. release_main_proc(pd);
  1050. end;
  1051. { if the unit contains ansi/widestrings, initialization and
  1052. finalization code must be forced }
  1053. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1054. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1055. { should we force unit initialization? }
  1056. { this is a hack, but how can it be done better ? }
  1057. if force_init_final and ((current_module.flags and uf_init)=0) then
  1058. gen_implicit_initfinal(uf_init,st);
  1059. { finalize? }
  1060. if has_impl and (token=_FINALIZATION) then
  1061. begin
  1062. { set module options }
  1063. current_module.flags:=current_module.flags or uf_finalize;
  1064. { Compile the finalize }
  1065. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1066. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1067. tcgprocinfo(current_procinfo).parse_body;
  1068. tcgprocinfo(current_procinfo).generate_code;
  1069. tcgprocinfo(current_procinfo).resetprocdef;
  1070. release_main_proc(pd);
  1071. end
  1072. else if force_init_final then
  1073. gen_implicit_initfinal(uf_finalize,st);
  1074. { the last char should always be a point }
  1075. consume(_POINT);
  1076. { Generate resoucestrings }
  1077. If ResourceStrings.ResStrCount>0 then
  1078. begin
  1079. ResourceStrings.CreateResourceStringList;
  1080. current_module.flags:=current_module.flags or uf_has_resources;
  1081. { only write if no errors found }
  1082. if (Errorcount=0) then
  1083. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1084. end;
  1085. if (Errorcount=0) then
  1086. begin
  1087. { tests, if all (interface) forwards are resolved }
  1088. tstoredsymtable(symtablestack).check_forwards;
  1089. { check if all private fields are used }
  1090. tstoredsymtable(symtablestack).allprivatesused;
  1091. { remove cross unit overloads }
  1092. tstoredsymtable(symtablestack).unchain_overloaded;
  1093. { test static symtable }
  1094. tstoredsymtable(st).allsymbolsused;
  1095. tstoredsymtable(st).allprivatesused;
  1096. tstoredsymtable(st).check_forwards;
  1097. tstoredsymtable(st).checklabels;
  1098. tstoredsymtable(st).unchain_overloaded;
  1099. { used units }
  1100. current_module.allunitsused;
  1101. end;
  1102. { leave when we got an error }
  1103. if (Errorcount>0) and not status.skip_error then
  1104. begin
  1105. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1106. status.skip_error:=true;
  1107. exit;
  1108. end;
  1109. { do we need to add the variants unit? }
  1110. maybeloadvariantsunit;
  1111. { generate debuginfo }
  1112. {$ifdef GDB}
  1113. write_gdb_info;
  1114. {$endif GDB}
  1115. { generate wrappers for interfaces }
  1116. gen_intf_wrappers(codesegment,current_module.globalsymtable);
  1117. gen_intf_wrappers(codesegment,current_module.localsymtable);
  1118. { generate a list of threadvars }
  1119. InsertThreadvars;
  1120. { generate imports }
  1121. if current_module.uses_imports then
  1122. importlib.generatelib;
  1123. { insert own objectfile, or say that it's in a library
  1124. (no check for an .o when loading) }
  1125. if is_assembler_generated then
  1126. insertobjectfile
  1127. else
  1128. current_module.flags:=current_module.flags or uf_no_link;
  1129. if cs_local_browser in aktmoduleswitches then
  1130. current_module.localsymtable:=refsymtable;
  1131. if is_assembler_generated then
  1132. begin
  1133. { create dwarf debuginfo }
  1134. create_dwarf;
  1135. { finish asmlist by adding segment starts }
  1136. insertsegment;
  1137. { assemble }
  1138. create_objectfile;
  1139. end;
  1140. { Write out the ppufile after the object file has been created }
  1141. store_interface_crc:=current_module.interface_crc;
  1142. {$ifdef EXTDEBUG}
  1143. store_crc:=current_module.crc;
  1144. {$endif EXTDEBUG}
  1145. if (Errorcount=0) then
  1146. tppumodule(current_module).writeppu;
  1147. if not(cs_compilesystem in aktmoduleswitches) then
  1148. if store_interface_crc<>current_module.interface_crc then
  1149. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1150. {$ifdef EXTDEBUG}
  1151. if not(cs_compilesystem in aktmoduleswitches) then
  1152. if (store_crc<>current_module.crc) and simplify_ppu then
  1153. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1154. {$endif EXTDEBUG}
  1155. { release all local symtables that are not needed anymore }
  1156. free_localsymtables(current_module.globalsymtable);
  1157. free_localsymtables(current_module.localsymtable);
  1158. { remove static symtable (=refsymtable) here to save some mem }
  1159. if not (cs_local_browser in aktmoduleswitches) then
  1160. begin
  1161. st.free;
  1162. current_module.localsymtable:=nil;
  1163. end;
  1164. { leave when we got an error }
  1165. if (Errorcount>0) and not status.skip_error then
  1166. begin
  1167. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1168. status.skip_error:=true;
  1169. exit;
  1170. end;
  1171. Message1(unit_u_finished_compiling,current_module.modulename^);
  1172. end;
  1173. procedure proc_program(islibrary : boolean);
  1174. var
  1175. main_file : tinputfile;
  1176. st : tsymtable;
  1177. hp,hp2 : tmodule;
  1178. pd : tprocdef;
  1179. begin
  1180. DLLsource:=islibrary;
  1181. Status.IsLibrary:=IsLibrary;
  1182. Status.IsExe:=true;
  1183. parse_only:=false;
  1184. { DLL defaults to create reloc info }
  1185. if islibrary then
  1186. begin
  1187. if not RelocSectionSetExplicitly then
  1188. RelocSection:=true;
  1189. end;
  1190. { relocation works only without stabs under win32 !! PM }
  1191. { internal assembler uses rva for stabs info
  1192. so it should work with relocated DLLs }
  1193. if RelocSection and
  1194. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1195. (target_info.assem<>as_i386_pecoff) then
  1196. begin
  1197. include(aktglobalswitches,cs_link_strip);
  1198. { Warning stabs info does not work with reloc section !! }
  1199. if cs_debuginfo in aktmoduleswitches then
  1200. begin
  1201. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1202. Message(parser_w_parser_win32_debug_needs_WN);
  1203. exclude(aktmoduleswitches,cs_debuginfo);
  1204. end;
  1205. end;
  1206. { get correct output names }
  1207. main_file := current_scanner.inputfile;
  1208. while assigned(main_file.next) do
  1209. main_file := main_file.next;
  1210. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1211. if islibrary then
  1212. begin
  1213. consume(_LIBRARY);
  1214. stringdispose(current_module.modulename);
  1215. stringdispose(current_module.realmodulename);
  1216. current_module.modulename:=stringdup(pattern);
  1217. current_module.realmodulename:=stringdup(orgpattern);
  1218. current_module.islibrary:=true;
  1219. exportlib.preparelib(orgpattern);
  1220. if tf_library_needs_pic in target_info.flags then
  1221. include(aktmoduleswitches,cs_create_pic);
  1222. consume(_ID);
  1223. consume(_SEMICOLON);
  1224. end
  1225. else
  1226. { is there an program head ? }
  1227. if token=_PROGRAM then
  1228. begin
  1229. consume(_PROGRAM);
  1230. stringdispose(current_module.modulename);
  1231. stringdispose(current_module.realmodulename);
  1232. current_module.modulename:=stringdup(pattern);
  1233. current_module.realmodulename:=stringdup(orgpattern);
  1234. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1235. exportlib.preparelib(orgpattern);
  1236. consume(_ID);
  1237. if token=_LKLAMMER then
  1238. begin
  1239. consume(_LKLAMMER);
  1240. repeat
  1241. consume(_ID);
  1242. until not try_to_consume(_COMMA);
  1243. consume(_RKLAMMER);
  1244. end;
  1245. consume(_SEMICOLON);
  1246. end
  1247. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1248. exportlib.preparelib(current_module.realmodulename^);
  1249. { global switches are read, so further changes aren't allowed }
  1250. current_module.in_global:=false;
  1251. { setup things using the switches }
  1252. ConsolidateMode;
  1253. setupglobalswitches;
  1254. { set implementation flag }
  1255. current_module.in_interface:=false;
  1256. current_module.interface_compiled:=true;
  1257. { insert after the unit symbol tables the static symbol table }
  1258. { of the program }
  1259. st:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1260. current_module.localsymtable:=st;
  1261. refsymtable:=st;
  1262. macrosymtablestack:= nil;
  1263. { load standard units (system,objpas,profile unit) }
  1264. loaddefaultunits;
  1265. current_module.localmacrosymtable.next:=macrosymtablestack;
  1266. macrosymtablestack:=current_module.localmacrosymtable;
  1267. { Load units provided on the command line }
  1268. loadautounits;
  1269. {Load the units used by the program we compile.}
  1270. if token=_USES then
  1271. loadunits;
  1272. { reset ranges/stabs in exported definitions }
  1273. reset_all_defs;
  1274. { All units are read, now give them a number }
  1275. current_module.updatemaps;
  1276. {Insert the name of the main program into the symbol table.}
  1277. if current_module.realmodulename^<>'' then
  1278. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1279. { ...is also constsymtable, this is the symtable where }
  1280. { the elements of enumeration types are inserted }
  1281. constsymtable:=st;
  1282. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1283. { The program intialization needs an alias, so it can be called
  1284. from the bootstrap code.}
  1285. if islibrary or
  1286. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1287. begin
  1288. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1289. { Win32 startup code needs a single name }
  1290. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1291. pd.aliasnames.insert('PASCALMAIN');
  1292. end
  1293. else
  1294. begin
  1295. if (target_info.system = system_i386_netware) or
  1296. (target_info.system = system_i386_netwlibc) then
  1297. begin
  1298. pd:=create_main_proc('PASCALMAIN',potype_proginit,st); { main is need by the netware rtl }
  1299. end else
  1300. begin
  1301. pd:=create_main_proc('main',potype_proginit,st);
  1302. pd.aliasnames.insert('PASCALMAIN');
  1303. end;
  1304. end;
  1305. tcgprocinfo(current_procinfo).parse_body;
  1306. tcgprocinfo(current_procinfo).generate_code;
  1307. tcgprocinfo(current_procinfo).resetprocdef;
  1308. { save file pos for debuginfo }
  1309. current_module.mainfilepos:=current_procinfo.entrypos;
  1310. release_main_proc(pd);
  1311. { should we force unit initialization? }
  1312. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1313. begin
  1314. { initialize section }
  1315. gen_implicit_initfinal(uf_init,st);
  1316. { finalize section }
  1317. gen_implicit_initfinal(uf_finalize,st);
  1318. end;
  1319. { Add symbol to the exports section for win32 so smartlinking a
  1320. DLL will include the edata section }
  1321. if assigned(exportlib) and
  1322. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1323. assigned(current_module._exports.first) then
  1324. codesegment.concat(tai_const.create_sym(exportlib.edatalabel));
  1325. If ResourceStrings.ResStrCount>0 then
  1326. begin
  1327. ResourceStrings.CreateResourceStringList;
  1328. { only write if no errors found }
  1329. if (Errorcount=0) then
  1330. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1331. end;
  1332. { finalize? }
  1333. if token=_FINALIZATION then
  1334. begin
  1335. { set module options }
  1336. current_module.flags:=current_module.flags or uf_finalize;
  1337. { Compile the finalize }
  1338. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1339. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1340. tcgprocinfo(current_procinfo).parse_body;
  1341. tcgprocinfo(current_procinfo).generate_code;
  1342. tcgprocinfo(current_procinfo).resetprocdef;
  1343. release_main_proc(pd);
  1344. end;
  1345. { consume the last point }
  1346. consume(_POINT);
  1347. if (Errorcount=0) then
  1348. begin
  1349. { test static symtable }
  1350. tstoredsymtable(st).allsymbolsused;
  1351. tstoredsymtable(st).allprivatesused;
  1352. tstoredsymtable(st).check_forwards;
  1353. tstoredsymtable(st).checklabels;
  1354. tstoredsymtable(st).unchain_overloaded;
  1355. current_module.allunitsused;
  1356. end;
  1357. { leave when we got an error }
  1358. if (Errorcount>0) and not status.skip_error then
  1359. begin
  1360. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1361. status.skip_error:=true;
  1362. exit;
  1363. end;
  1364. { remove all unused units, this happends when units are removed
  1365. from the uses clause in the source and the ppu was already being loaded }
  1366. hp:=tmodule(loaded_units.first);
  1367. while assigned(hp) do
  1368. begin
  1369. hp2:=hp;
  1370. hp:=tmodule(hp.next);
  1371. if hp2.is_unit and
  1372. not assigned(hp2.globalsymtable) then
  1373. loaded_units.remove(hp2);
  1374. end;
  1375. { do we need to add the variants unit? }
  1376. maybeloadvariantsunit;
  1377. { generate debuginfo }
  1378. {$ifdef GDB}
  1379. write_gdb_info;
  1380. {$endif GDB}
  1381. { generate wrappers for interfaces }
  1382. gen_intf_wrappers(codesegment,current_module.localsymtable);
  1383. { generate a list of threadvars }
  1384. InsertThreadvars;
  1385. { generate imports }
  1386. if current_module.uses_imports then
  1387. importlib.generatelib;
  1388. if islibrary or
  1389. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1390. (target_info.system=system_i386_NETWARE) then
  1391. exportlib.generatelib;
  1392. { insert Tables and StackLength }
  1393. insertThreadVarTablesTable;
  1394. insertResourceTablesTable;
  1395. insertinitfinaltable;
  1396. insertmemorysizes;
  1397. { create dwarf debuginfo }
  1398. create_dwarf;
  1399. { finish asmlist by adding segment starts }
  1400. insertsegment;
  1401. { insert own objectfile }
  1402. insertobjectfile;
  1403. { assemble and link }
  1404. create_objectfile;
  1405. { release all local symtables that are not needed anymore }
  1406. free_localsymtables(current_module.localsymtable);
  1407. { leave when we got an error }
  1408. if (Errorcount>0) and not status.skip_error then
  1409. begin
  1410. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1411. status.skip_error:=true;
  1412. exit;
  1413. end;
  1414. { create the executable when we are at level 1 }
  1415. if (compile_level=1) then
  1416. begin
  1417. { insert all .o files from all loaded units }
  1418. hp:=tmodule(loaded_units.first);
  1419. while assigned(hp) do
  1420. begin
  1421. linker.AddModuleFiles(hp);
  1422. hp:=tmodule(hp.next);
  1423. end;
  1424. { write .def file }
  1425. if (cs_link_deffile in aktglobalswitches) then
  1426. deffile.writefile;
  1427. { finally we can create a executable }
  1428. if (not current_module.is_unit) then
  1429. begin
  1430. if DLLSource then
  1431. linker.MakeSharedLibrary
  1432. else
  1433. linker.MakeExecutable;
  1434. end;
  1435. end;
  1436. end;
  1437. end.
  1438. {
  1439. $Log$
  1440. Revision 1.186 2005-03-25 21:55:43 jonas
  1441. * removed some unused variables
  1442. Revision 1.185 2005/02/14 17:13:07 peter
  1443. * truncate log
  1444. Revision 1.184 2005/02/06 21:33:28 peter
  1445. * -Fa option added, it'll load the units before the uses
  1446. line is parsed. Can be used to load cthreads from the commandline.
  1447. Example '-g -Faheaptrc,lineinfo' is the same as '-ghl'
  1448. Revision 1.183 2005/02/06 11:15:31 peter
  1449. * removed $threading
  1450. Revision 1.182 2005/02/06 00:05:56 florian
  1451. + x86_64 pic draft
  1452. Revision 1.181 2005/01/24 22:08:32 peter
  1453. * interface wrapper generation moved to cgobj
  1454. * generate interface wrappers after the module is parsed
  1455. Revision 1.180 2005/01/19 22:19:41 peter
  1456. * unit mapping rewrite
  1457. * new derefmap added
  1458. Revision 1.179 2005/01/09 20:24:43 olle
  1459. * rework of macro subsystem
  1460. + exportable macros for mode macpas
  1461. }