pmodules.pas 55 KB

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