pmodules.pas 61 KB

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