pmodules.pas 60 KB

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