pmodules.pas 56 KB

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