pmodules.pas 55 KB

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