pmodules.pas 60 KB

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