pmodules.pas 58 KB

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