pmodules.pas 55 KB

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