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