pmodules.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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,aasmdata,aasmcpu,aasmbase,
  29. cgbase,cgobj,
  30. nbas,ncgutil,
  31. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  32. cresstr,procinfo,
  33. pexports,
  34. scanner,pbase,pexpr,psystem,psub,pdecsub;
  35. procedure create_objectfile;
  36. var
  37. DLLScanner : TDLLScanner;
  38. s : string;
  39. KeepShared : TStringList;
  40. begin
  41. { try to create import entries from system dlls }
  42. if (tf_has_dllscanner in target_info.flags) and
  43. (not current_module.linkOtherSharedLibs.Empty) then
  44. begin
  45. { Init DLLScanner }
  46. if assigned(CDLLScanner[target_info.system]) then
  47. DLLScanner:=CDLLScanner[target_info.system].Create
  48. else
  49. internalerror(200104121);
  50. KeepShared:=TStringList.Create;
  51. { Walk all shared libs }
  52. While not current_module.linkOtherSharedLibs.Empty do
  53. begin
  54. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  55. if not DLLScanner.scan(s) then
  56. KeepShared.Concat(s);
  57. end;
  58. DLLscanner.Free;
  59. { Recreate import section }
  60. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  61. begin
  62. if assigned(current_asmdata.asmlists[al_imports]) then
  63. current_asmdata.asmlists[al_imports].clear
  64. else
  65. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  66. importlib.generatelib;
  67. end;
  68. { Readd the not processed files }
  69. while not KeepShared.Empty do
  70. begin
  71. s:=KeepShared.GetFirst;
  72. current_module.linkOtherSharedLibs.add(s,link_always);
  73. end;
  74. KeepShared.Free;
  75. end;
  76. { Start and end module debuginfo, at least required for stabs
  77. to insert n_sourcefile lines }
  78. if (cs_debuginfo in aktmoduleswitches) or
  79. (cs_use_lineinfo in aktglobalswitches) then
  80. debuginfo.insertmoduleinfo;
  81. { create the .s file and assemble it }
  82. GenerateAsm(false);
  83. { Also create a smartlinked version ? }
  84. if (cs_create_smart in aktmoduleswitches) and
  85. (not use_smartlink_section) then
  86. begin
  87. GenerateAsm(true);
  88. if (af_needar in target_asm.flags) then
  89. Linker.MakeStaticLibrary;
  90. end;
  91. { resource files }
  92. CompileResourceFiles;
  93. end;
  94. procedure insertobjectfile;
  95. { Insert the used object file for this unit in the used list for this unit }
  96. begin
  97. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  98. current_module.flags:=current_module.flags or uf_static_linked;
  99. if (cs_create_smart in aktmoduleswitches) and
  100. not use_smartlink_section then
  101. begin
  102. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  103. current_module.flags:=current_module.flags or uf_smart_linked;
  104. end;
  105. end;
  106. procedure create_dwarf;
  107. begin
  108. { Dwarf conflicts with smartlinking in separate .a files }
  109. if (cs_create_smart in aktmoduleswitches) and
  110. not use_smartlink_section then
  111. exit;
  112. { Call frame information }
  113. if (tf_needs_dwarf_cfi in target_info.flags) and
  114. (af_supports_dwarf in target_asm.flags) then
  115. begin
  116. current_asmdata.asmlists[al_dwarf]:=TAsmList.create;
  117. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  118. end;
  119. end;
  120. procedure InsertThreadvarTablesTable;
  121. var
  122. hp : tused_unit;
  123. ltvTables : TAsmList;
  124. count : longint;
  125. begin
  126. if (tf_section_threadvars in target_info.flags) then
  127. exit;
  128. ltvTables:=TAsmList.Create;
  129. count:=0;
  130. hp:=tused_unit(usedunits.first);
  131. while assigned(hp) do
  132. begin
  133. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  134. begin
  135. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  136. inc(count);
  137. end;
  138. hp:=tused_unit(hp.next);
  139. end;
  140. { Add program threadvars, if any }
  141. If (current_module.flags and uf_threadvars)=uf_threadvars then
  142. begin
  143. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  144. inc(count);
  145. end;
  146. { Insert TableCount at start }
  147. ltvTables.insert(Tai_const.Create_32bit(count));
  148. { insert in data segment }
  149. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  150. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(aint));
  151. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  152. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  153. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  154. ltvTables.free;
  155. end;
  156. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  157. var
  158. ltvTable : TAsmList;
  159. begin
  160. ltvTable:=TAsmList(arg);
  161. if (tsym(p).typ=globalvarsym) and
  162. (vo_is_thread_var in tglobalvarsym(p).varoptions) then
  163. begin
  164. { address of threadvar }
  165. ltvTable.concat(tai_const.Createname(tglobalvarsym(p).mangledname,0));
  166. { size of threadvar }
  167. ltvTable.concat(tai_const.create_32bit(tglobalvarsym(p).getsize));
  168. end;
  169. end;
  170. procedure InsertThreadvars;
  171. var
  172. s : string;
  173. ltvTable : TAsmList;
  174. begin
  175. if (tf_section_threadvars in target_info.flags) then
  176. exit;
  177. ltvTable:=TAsmList.create;
  178. if assigned(current_module.globalsymtable) then
  179. current_module.globalsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  180. current_module.localsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  181. if ltvTable.first<>nil then
  182. begin
  183. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  184. { end of the list marker }
  185. ltvTable.concat(tai_const.create_sym(nil));
  186. { add to datasegment }
  187. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  188. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(aint));
  189. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  190. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  191. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  192. current_module.flags:=current_module.flags or uf_threadvars;
  193. end;
  194. ltvTable.Free;
  195. end;
  196. Procedure InsertResourceInfo;
  197. var
  198. hp : tused_unit;
  199. found : Boolean;
  200. I : Integer;
  201. ResourceInfo : TAsmList;
  202. begin
  203. if target_res.id=res_elf then
  204. begin
  205. hp:=tused_unit(usedunits.first);
  206. found:=false;
  207. Found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  208. If not found then
  209. While Assigned(hp) and not Found do
  210. begin
  211. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  212. hp:=tused_unit(hp.next);
  213. end;
  214. ResourceInfo:=TAsmList.Create;
  215. if found then
  216. begin
  217. { Valid pointer to resource information }
  218. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  219. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0));
  220. {$ifdef EXTERNALRESPTRS}
  221. current_module.linkotherofiles.add('resptrs.o',link_always);
  222. {$else EXTERNALRESPTRS}
  223. new_section(ResourceInfo,sec_fpc,'resptrs',4);
  224. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESSYMBOL',AT_DATA,0));
  225. For I:=1 to 32 do
  226. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  227. {$endif EXTERNALRESPTRS}
  228. end
  229. else
  230. begin
  231. { Nil pointer to resource information }
  232. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  233. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  234. end;
  235. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  236. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  237. ResourceInfo.free;
  238. end;
  239. end;
  240. Procedure InsertResourceTablesTable;
  241. var
  242. hp : tmodule;
  243. ResourceStringTables : tasmlist;
  244. count : longint;
  245. begin
  246. ResourceStringTables:=tasmlist.Create;
  247. count:=0;
  248. hp:=tmodule(loaded_units.first);
  249. while assigned(hp) do
  250. begin
  251. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  252. begin
  253. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  254. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  255. inc(count);
  256. end;
  257. hp:=tmodule(hp.next);
  258. end;
  259. { Insert TableCount at start }
  260. ResourceStringTables.insert(Tai_const.Create_aint(count));
  261. { Add to data segment }
  262. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  263. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(aint));
  264. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  265. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  266. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  267. ResourceStringTables.free;
  268. end;
  269. procedure InsertInitFinalTable;
  270. var
  271. hp : tused_unit;
  272. unitinits : TAsmList;
  273. count : longint;
  274. begin
  275. unitinits:=TAsmList.Create;
  276. count:=0;
  277. hp:=tused_unit(usedunits.first);
  278. while assigned(hp) do
  279. begin
  280. { call the unit init code and make it external }
  281. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  282. begin
  283. if (hp.u.flags and uf_init)<>0 then
  284. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  285. else
  286. unitinits.concat(Tai_const.Create_sym(nil));
  287. if (hp.u.flags and uf_finalize)<>0 then
  288. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  289. else
  290. unitinits.concat(Tai_const.Create_sym(nil));
  291. inc(count);
  292. end;
  293. hp:=tused_unit(hp.next);
  294. end;
  295. { Insert initialization/finalization of the program }
  296. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  297. begin
  298. if (current_module.flags and uf_init)<>0 then
  299. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  300. else
  301. unitinits.concat(Tai_const.Create_sym(nil));
  302. if (current_module.flags and uf_finalize)<>0 then
  303. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  304. else
  305. unitinits.concat(Tai_const.Create_sym(nil));
  306. inc(count);
  307. end;
  308. { Insert TableCount,InitCount at start }
  309. unitinits.insert(Tai_const.Create_32bit(0));
  310. unitinits.insert(Tai_const.Create_32bit(count));
  311. { Add to data segment }
  312. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  313. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(aint));
  314. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  315. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  316. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  317. unitinits.free;
  318. end;
  319. procedure insertmemorysizes;
  320. {$IFDEF POWERPC}
  321. var
  322. stkcookie: string;
  323. {$ENDIF POWERPC}
  324. begin
  325. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  326. { Insert Ident of the compiler in the main .data section }
  327. current_asmdata.asmlists[al_globals].concat(Tai_section.create(sec_data,'',0));
  328. current_asmdata.asmlists[al_globals].concat(Tai_align.Create(const_align(32)));
  329. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  330. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  331. { stacksize can be specified and is now simulated }
  332. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(aint));
  333. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(aint)));
  334. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(stacksize));
  335. {$IFDEF POWERPC}
  336. { AmigaOS4 "stack cookie" support }
  337. if ( target_info.system = system_powerpc_amiga ) then
  338. begin
  339. { this symbol is needed to ignite powerpc amigaos' }
  340. { stack allocation magic for us with the given stack size. }
  341. { note: won't work for m68k amigaos or morphos. (KB) }
  342. str(stacksize,stkcookie);
  343. stkcookie:='$STACK: '+stkcookie+#0;
  344. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  345. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  346. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  347. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  348. end;
  349. {$ENDIF POWERPC}
  350. { Initial heapsize }
  351. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  352. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(aint));
  353. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(aint)));
  354. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(heapsize));
  355. end;
  356. procedure AddUnit(const s:string);
  357. var
  358. hp : tppumodule;
  359. unitsym : tunitsym;
  360. begin
  361. { load unit }
  362. hp:=registerunit(current_module,s,'');
  363. hp.loadppu;
  364. hp.adddependency(current_module);
  365. { add to symtable stack }
  366. symtablestack.push(hp.globalsymtable);
  367. if (m_mac in aktmodeswitches) and
  368. assigned(hp.globalmacrosymtable) then
  369. macrosymtablestack.push(hp.globalmacrosymtable);
  370. { insert unitsym }
  371. unitsym:=tunitsym.create(s,hp);
  372. inc(unitsym.refs);
  373. current_module.localsymtable.insert(unitsym);
  374. { add to used units }
  375. current_module.addusedunit(hp,false,unitsym);
  376. end;
  377. procedure maybeloadvariantsunit;
  378. var
  379. hp : tmodule;
  380. begin
  381. { Do we need the variants unit? Skip this
  382. for VarUtils unit for bootstrapping }
  383. if (current_module.flags and uf_uses_variants=0) or
  384. (current_module.modulename^='VARUTILS') then
  385. exit;
  386. { Variants unit already loaded? }
  387. hp:=tmodule(loaded_units.first);
  388. while assigned(hp) do
  389. begin
  390. if hp.modulename^='VARIANTS' then
  391. exit;
  392. hp:=tmodule(hp.next);
  393. end;
  394. { Variants unit is not loaded yet, load it now }
  395. Message(parser_w_implicit_uses_of_variants_unit);
  396. AddUnit('Variants');
  397. end;
  398. procedure loaddefaultunits;
  399. begin
  400. { we are going to rebuild the symtablestack, clear it first }
  401. symtablestack.clear;
  402. macrosymtablestack.clear;
  403. { macro symtable }
  404. macrosymtablestack.push(initialmacrosymtable);
  405. { are we compiling the system unit? }
  406. if (cs_compilesystem in aktmoduleswitches) then
  407. begin
  408. systemunit:=tglobalsymtable(current_module.localsymtable);
  409. { create system defines }
  410. create_intern_symbols;
  411. create_intern_types;
  412. exit;
  413. end;
  414. { insert the system unit, it is allways the first. Load also the
  415. internal types from the system unit }
  416. AddUnit('System');
  417. systemunit:=tglobalsymtable(symtablestack.top);
  418. load_intern_types;
  419. { Set the owner of errorsym and errortype to symtable to
  420. prevent crashes when accessing .owner }
  421. generrorsym.owner:=systemunit;
  422. generrortype.def.owner:=systemunit;
  423. { Units only required for main module }
  424. if not(current_module.is_unit) then
  425. begin
  426. { Heaptrc unit, load heaptrace before any other units especially objpas }
  427. if (cs_use_heaptrc in aktglobalswitches) then
  428. AddUnit('HeapTrc');
  429. { Lineinfo unit }
  430. if (cs_use_lineinfo in aktglobalswitches) then
  431. AddUnit('LineInfo');
  432. { Lineinfo unit }
  433. if (cs_gdb_valgrind in aktglobalswitches) then
  434. AddUnit('CMem');
  435. {$ifdef cpufpemu}
  436. { Floating point emulation unit? }
  437. if (cs_fp_emulation in aktmoduleswitches) and not(target_info.system in system_wince) then
  438. AddUnit('SoftFpu');
  439. {$endif cpufpemu}
  440. end;
  441. { Objpas unit? }
  442. if m_objpas in aktmodeswitches then
  443. AddUnit('ObjPas');
  444. { Macpas unit? }
  445. if m_mac in aktmodeswitches then
  446. AddUnit('MacPas');
  447. { Profile unit? Needed for go32v2 only }
  448. if (cs_profile in aktmoduleswitches) and
  449. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  450. AddUnit('Profile');
  451. if (cs_load_fpcylix_unit in aktglobalswitches) then
  452. begin
  453. AddUnit('FPCylix');
  454. AddUnit('DynLibs');
  455. end;
  456. end;
  457. procedure loadautounits;
  458. var
  459. hs,s : string;
  460. begin
  461. hs:=autoloadunits;
  462. repeat
  463. s:=GetToken(hs,',');
  464. if s='' then
  465. break;
  466. AddUnit(s);
  467. until false;
  468. end;
  469. procedure loadunits;
  470. var
  471. s,sorg : stringid;
  472. fn : string;
  473. pu : tused_unit;
  474. hp2 : tmodule;
  475. unitsym : tunitsym;
  476. begin
  477. consume(_USES);
  478. repeat
  479. s:=pattern;
  480. sorg:=orgpattern;
  481. consume(_ID);
  482. { support "<unit> in '<file>'" construct, but not for tp7 }
  483. fn:='';
  484. if not(m_tp7 in aktmodeswitches) and
  485. try_to_consume(_OP_IN) then
  486. fn:=FixFileName(get_stringconst);
  487. { Give a warning if objpas is loaded }
  488. if s='OBJPAS' then
  489. Message(parser_w_no_objpas_use_mode);
  490. { Using the unit itself is not possible }
  491. if (s<>current_module.modulename^) then
  492. begin
  493. { check if the unit is already used }
  494. hp2:=nil;
  495. pu:=tused_unit(current_module.used_units.first);
  496. while assigned(pu) do
  497. begin
  498. if (pu.u.modulename^=s) then
  499. begin
  500. hp2:=pu.u;
  501. break;
  502. end;
  503. pu:=tused_unit(pu.next);
  504. end;
  505. if not assigned(hp2) then
  506. hp2:=registerunit(current_module,sorg,fn)
  507. else
  508. Message1(sym_e_duplicate_id,s);
  509. { Create unitsym, we need to use the name as specified, we
  510. can not use the modulename because that can be different
  511. when -Un is used }
  512. unitsym:=tunitsym.create(sorg,nil);
  513. current_module.localsymtable.insert(unitsym);
  514. { the current module uses the unit hp2 }
  515. current_module.addusedunit(hp2,true,unitsym);
  516. end
  517. else
  518. Message1(sym_e_duplicate_id,s);
  519. if token=_COMMA then
  520. begin
  521. pattern:='';
  522. consume(_COMMA);
  523. end
  524. else
  525. break;
  526. until false;
  527. { Load the units }
  528. pu:=tused_unit(current_module.used_units.first);
  529. while assigned(pu) do
  530. begin
  531. { Only load the units that are in the current
  532. (interface/implementation) uses clause }
  533. if pu.in_uses and
  534. (pu.in_interface=current_module.in_interface) then
  535. begin
  536. tppumodule(pu.u).loadppu;
  537. { is our module compiled? then we can stop }
  538. if current_module.state=ms_compiled then
  539. exit;
  540. { add this unit to the dependencies }
  541. pu.u.adddependency(current_module);
  542. { save crc values }
  543. pu.checksum:=pu.u.crc;
  544. pu.interface_checksum:=pu.u.interface_crc;
  545. { connect unitsym to the module }
  546. pu.unitsym.module:=pu.u;
  547. { add to symtable stack }
  548. symtablestack.push(pu.u.globalsymtable);
  549. if (m_mac in aktmodeswitches) and
  550. assigned(pu.u.globalmacrosymtable) then
  551. macrosymtablestack.push(pu.u.globalmacrosymtable);
  552. end;
  553. pu:=tused_unit(pu.next);
  554. end;
  555. consume(_SEMICOLON);
  556. end;
  557. procedure reset_all_defs;
  558. procedure reset_used_unit_defs(hp:tmodule);
  559. var
  560. pu : tused_unit;
  561. begin
  562. pu:=tused_unit(hp.used_units.first);
  563. while assigned(pu) do
  564. begin
  565. if not pu.u.is_reset then
  566. begin
  567. { prevent infinte loop for circular dependencies }
  568. pu.u.is_reset:=true;
  569. if assigned(pu.u.globalsymtable) then
  570. begin
  571. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  572. reset_used_unit_defs(pu.u);
  573. end;
  574. end;
  575. pu:=tused_unit(pu.next);
  576. end;
  577. end;
  578. var
  579. hp2 : tmodule;
  580. begin
  581. hp2:=tmodule(loaded_units.first);
  582. while assigned(hp2) do
  583. begin
  584. hp2.is_reset:=false;
  585. hp2:=tmodule(hp2.next);
  586. end;
  587. reset_used_unit_defs(current_module);
  588. end;
  589. procedure free_localsymtables(st:tsymtable);
  590. var
  591. def : tstoreddef;
  592. pd : tprocdef;
  593. begin
  594. def:=tstoreddef(st.defindex.first);
  595. while assigned(def) do
  596. begin
  597. if def.deftype=procdef then
  598. begin
  599. pd:=tprocdef(def);
  600. if assigned(pd.localst) and
  601. (pd.localst.symtabletype<>staticsymtable) and
  602. not((po_inline in pd.procoptions) or
  603. ((current_module.flags and uf_local_browser)<>0)) then
  604. begin
  605. free_localsymtables(pd.localst);
  606. pd.localst.free;
  607. pd.localst:=nil;
  608. end;
  609. end;
  610. def:=tstoreddef(def.indexnext);
  611. end;
  612. end;
  613. procedure parse_implementation_uses;
  614. begin
  615. if token=_USES then
  616. loadunits;
  617. end;
  618. procedure setupglobalswitches;
  619. begin
  620. { can't have local browser when no global browser }
  621. if (cs_local_browser in aktmoduleswitches) and
  622. not(cs_browser in aktmoduleswitches) then
  623. exclude(aktmoduleswitches,cs_local_browser);
  624. if (cs_create_pic in aktmoduleswitches) then
  625. def_system_macro('FPC_PIC');
  626. end;
  627. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  628. var
  629. ps : tprocsym;
  630. pd : tprocdef;
  631. begin
  632. { there should be no current_procinfo available }
  633. if assigned(current_procinfo) then
  634. internalerror(200304275);
  635. {Generate a procsym for main}
  636. ps:=tprocsym.create('$'+name);
  637. { main are allways used }
  638. inc(ps.refs);
  639. st.insert(ps);
  640. pd:=tprocdef.create(main_program_level);
  641. include(pd.procoptions,po_global);
  642. pd.procsym:=ps;
  643. ps.addprocdef(pd);
  644. { set procdef options }
  645. pd.proctypeoption:=potype;
  646. pd.proccalloption:=pocall_default;
  647. pd.forwarddef:=false;
  648. pd.setmangledname(target_info.cprefix+name);
  649. pd.aliasnames.insert(pd.mangledname);
  650. handle_calling_convention(pd);
  651. { We don't need is a local symtable. Change it into the static
  652. symtable }
  653. pd.localst.free;
  654. pd.localst:=st;
  655. { set procinfo and current_procinfo.procdef }
  656. current_procinfo:=cprocinfo.create(nil);
  657. current_module.procinfo:=current_procinfo;
  658. current_procinfo.procdef:=pd;
  659. { return procdef }
  660. create_main_proc:=pd;
  661. { main proc does always a call e.g. to init system unit }
  662. include(current_procinfo.flags,pi_do_call);
  663. end;
  664. procedure release_main_proc(pd:tprocdef);
  665. begin
  666. { this is a main proc, so there should be no parent }
  667. if not(assigned(current_procinfo)) or
  668. assigned(current_procinfo.parent) or
  669. not(current_procinfo.procdef=pd) then
  670. internalerror(200304276);
  671. { remove procinfo }
  672. current_module.procinfo:=nil;
  673. current_procinfo.free;
  674. current_procinfo:=nil;
  675. { remove localst as it was replaced by staticsymtable }
  676. pd.localst:=nil;
  677. end;
  678. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  679. var
  680. pd : tprocdef;
  681. begin
  682. { update module flags }
  683. current_module.flags:=current_module.flags or flag;
  684. { create procdef }
  685. case flag of
  686. uf_init :
  687. begin
  688. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  689. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  690. end;
  691. uf_finalize :
  692. begin
  693. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  694. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  695. end;
  696. else
  697. internalerror(200304253);
  698. end;
  699. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  700. tcgprocinfo(current_procinfo).generate_code;
  701. release_main_proc(pd);
  702. end;
  703. procedure copy_macro(p:TNamedIndexItem; arg:pointer);
  704. begin
  705. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  706. end;
  707. procedure proc_unit;
  708. function is_assembler_generated:boolean;
  709. var
  710. hal : tasmlisttype;
  711. begin
  712. result:=false;
  713. if Errorcount=0 then
  714. begin
  715. for hal:=low(TasmlistType) to high(TasmlistType) do
  716. if not current_asmdata.asmlists[hal].empty then
  717. begin
  718. result:=true;
  719. exit;
  720. end;
  721. end;
  722. end;
  723. var
  724. main_file: tinputfile;
  725. {$ifdef EXTDEBUG}
  726. store_crc,
  727. {$endif EXTDEBUG}
  728. store_interface_crc : cardinal;
  729. s1,s2 : ^string; {Saves stack space}
  730. force_init_final : boolean;
  731. pd : tprocdef;
  732. unitname8 : string[8];
  733. has_impl,ag: boolean;
  734. globalvarsym : tglobalvarsym;
  735. begin
  736. if m_mac in aktmodeswitches then
  737. current_module.mode_switch_allowed:= false;
  738. consume(_UNIT);
  739. if compile_level=1 then
  740. Status.IsExe:=false;
  741. if token=_ID then
  742. begin
  743. { create filenames and unit name }
  744. main_file := current_scanner.inputfile;
  745. while assigned(main_file.next) do
  746. main_file := main_file.next;
  747. new(s1);
  748. s1^:=current_module.modulename^;
  749. current_module.SetFileName(main_file.path^+main_file.name^,true);
  750. current_module.SetModuleName(orgpattern);
  751. { check for system unit }
  752. new(s2);
  753. s2^:=upper(SplitName(main_file.name^));
  754. unitname8:=copy(current_module.modulename^,1,8);
  755. if (cs_check_unit_name in aktglobalswitches) and
  756. (
  757. not(
  758. (current_module.modulename^=s2^) or
  759. (
  760. (length(current_module.modulename^)>8) and
  761. (unitname8=s2^)
  762. )
  763. )
  764. or
  765. (
  766. (length(s1^)>8) and
  767. (s1^<>current_module.modulename^)
  768. )
  769. ) then
  770. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  771. if (current_module.modulename^='SYSTEM') then
  772. include(aktmoduleswitches,cs_compilesystem);
  773. dispose(s2);
  774. dispose(s1);
  775. end;
  776. if (target_info.system in system_unit_program_exports) then
  777. exportlib.preparelib(current_module.realmodulename^);
  778. consume(_ID);
  779. consume(_SEMICOLON);
  780. consume(_INTERFACE);
  781. { global switches are read, so further changes aren't allowed }
  782. current_module.in_global:=false;
  783. { handle the global switches }
  784. setupglobalswitches;
  785. message1(unit_u_loading_interface_units,current_module.modulename^);
  786. { update status }
  787. status.currentmodule:=current_module.realmodulename^;
  788. { maybe turn off m_objpas if we are compiling objpas }
  789. if (current_module.modulename^='OBJPAS') then
  790. exclude(aktmodeswitches,m_objpas);
  791. { maybe turn off m_mac if we are compiling macpas }
  792. if (current_module.modulename^='MACPAS') then
  793. exclude(aktmodeswitches,m_mac);
  794. parse_only:=true;
  795. { generate now the global symboltable,
  796. define first as local to overcome dependency conflicts }
  797. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  798. { insert unitsym of this unit to prevent other units having
  799. the same name }
  800. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  801. { load default units, like the system unit }
  802. loaddefaultunits;
  803. { reset }
  804. make_ref:=true;
  805. { insert qualifier for the system unit (allows system.writeln) }
  806. if not(cs_compilesystem in aktmoduleswitches) and
  807. (token=_USES) then
  808. begin
  809. loadunits;
  810. { has it been compiled at a higher level ?}
  811. if current_module.state=ms_compiled then
  812. exit;
  813. end;
  814. { move the global symtable from the temporary local to global }
  815. current_module.globalsymtable:=current_module.localsymtable;
  816. current_module.localsymtable:=nil;
  817. reset_all_defs;
  818. { number all units, so we know if a unit is used by this unit or
  819. needs to be added implicitly }
  820. current_module.updatemaps;
  821. { ... parse the declarations }
  822. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  823. symtablestack.push(current_module.globalsymtable);
  824. read_interface_declarations;
  825. symtablestack.pop(current_module.globalsymtable);
  826. { Export macros defined in the interface for macpas. The macros
  827. are put in the globalmacrosymtable that will only be used by other
  828. units. The current unit continues to use the localmacrosymtable }
  829. if (m_mac in aktmodeswitches) then
  830. begin
  831. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  832. current_module.localmacrosymtable.foreach_static(@copy_macro,nil);
  833. end;
  834. { leave when we got an error }
  835. if (Errorcount>0) and not status.skip_error then
  836. begin
  837. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  838. status.skip_error:=true;
  839. exit;
  840. end;
  841. { Our interface is compiled, generate CRC and switch to implementation }
  842. if not(cs_compilesystem in aktmoduleswitches) and
  843. (Errorcount=0) then
  844. tppumodule(current_module).getppucrc;
  845. current_module.in_interface:=false;
  846. current_module.interface_compiled:=true;
  847. { First reload all units depending on our interface, we need to do this
  848. in the implementation part to prevent errorneous circular references }
  849. reload_flagged_units;
  850. { Parse the implementation section }
  851. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  852. has_impl:= false
  853. else
  854. has_impl:= true;
  855. parse_only:=false;
  856. { generates static symbol table }
  857. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  858. {$ifdef i386}
  859. if cs_create_pic in aktmoduleswitches then
  860. begin
  861. { insert symbol for got access in assembler code}
  862. globalvarsym:=tglobalvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external,vo_is_C_var]);
  863. globalvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  864. current_module.localsymtable.insert(globalvarsym);
  865. { avoid unnecessary warnings }
  866. globalvarsym.varstate:=vs_read;
  867. globalvarsym.refs:=1;
  868. end;
  869. {$endif i386}
  870. if has_impl then
  871. begin
  872. consume(_IMPLEMENTATION);
  873. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  874. { Read the implementation units }
  875. parse_implementation_uses;
  876. end;
  877. if current_module.state=ms_compiled then
  878. exit;
  879. { reset ranges/stabs in exported definitions }
  880. reset_all_defs;
  881. { All units are read, now give them a number }
  882. current_module.updatemaps;
  883. symtablestack.push(current_module.globalsymtable);
  884. symtablestack.push(current_module.localsymtable);
  885. if has_impl then
  886. begin
  887. Message1(parser_u_parsing_implementation,current_module.modulename^);
  888. if current_module.in_interface then
  889. internalerror(200212285);
  890. { Compile the unit }
  891. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  892. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  893. tcgprocinfo(current_procinfo).parse_body;
  894. tcgprocinfo(current_procinfo).generate_code;
  895. tcgprocinfo(current_procinfo).resetprocdef;
  896. { save file pos for debuginfo }
  897. current_module.mainfilepos:=current_procinfo.entrypos;
  898. release_main_proc(pd);
  899. end;
  900. { Generate specializations of objectdefs methods }
  901. generate_specialization_procs;
  902. { if the unit contains ansi/widestrings, initialization and
  903. finalization code must be forced }
  904. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  905. tstaticsymtable(current_module.localsymtable).needs_init_final;
  906. { should we force unit initialization? }
  907. { this is a hack, but how can it be done better ? }
  908. if force_init_final and ((current_module.flags and uf_init)=0) then
  909. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  910. { finalize? }
  911. if has_impl and (token=_FINALIZATION) then
  912. begin
  913. { set module options }
  914. current_module.flags:=current_module.flags or uf_finalize;
  915. { Compile the finalize }
  916. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  917. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  918. tcgprocinfo(current_procinfo).parse_body;
  919. tcgprocinfo(current_procinfo).generate_code;
  920. tcgprocinfo(current_procinfo).resetprocdef;
  921. release_main_proc(pd);
  922. end
  923. else if force_init_final then
  924. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  925. symtablestack.pop(current_module.localsymtable);
  926. symtablestack.pop(current_module.globalsymtable);
  927. { the last char should always be a point }
  928. consume(_POINT);
  929. if (Errorcount=0) then
  930. begin
  931. { tests, if all (interface) forwards are resolved }
  932. tstoredsymtable(current_module.globalsymtable).check_forwards;
  933. { check if all private fields are used }
  934. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  935. { remove cross unit overloads }
  936. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  937. { test static symtable }
  938. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  939. tstoredsymtable(current_module.localsymtable).allprivatesused;
  940. tstoredsymtable(current_module.localsymtable).check_forwards;
  941. tstoredsymtable(current_module.localsymtable).checklabels;
  942. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  943. { used units }
  944. current_module.allunitsused;
  945. end;
  946. { leave when we got an error }
  947. if (Errorcount>0) and not status.skip_error then
  948. begin
  949. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  950. status.skip_error:=true;
  951. exit;
  952. end;
  953. { do we need to add the variants unit? }
  954. maybeloadvariantsunit;
  955. { generate debuginfo }
  956. if (cs_debuginfo in aktmoduleswitches) then
  957. debuginfo.inserttypeinfo;
  958. { generate wrappers for interfaces }
  959. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  960. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  961. { generate pic helpers to load eip if necessary }
  962. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  963. { Tables }
  964. insertThreadVars;
  965. { Resource strings }
  966. GenerateResourceStrings;
  967. { generate imports }
  968. if current_module.uses_imports then
  969. importlib.generatelib;
  970. { insert own objectfile, or say that it's in a library
  971. (no check for an .o when loading) }
  972. ag:=is_assembler_generated;
  973. if ag then
  974. insertobjectfile
  975. else
  976. begin
  977. current_module.flags:=current_module.flags or uf_no_link;
  978. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  979. end;
  980. if ag then
  981. begin
  982. { create dwarf debuginfo }
  983. create_dwarf;
  984. { assemble }
  985. create_objectfile;
  986. end;
  987. { Write out the ppufile after the object file has been created }
  988. store_interface_crc:=current_module.interface_crc;
  989. {$ifdef EXTDEBUG}
  990. store_crc:=current_module.crc;
  991. {$endif EXTDEBUG}
  992. if (Errorcount=0) then
  993. tppumodule(current_module).writeppu;
  994. if not(cs_compilesystem in aktmoduleswitches) then
  995. if store_interface_crc<>current_module.interface_crc then
  996. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  997. {$ifdef EXTDEBUG}
  998. if not(cs_compilesystem in aktmoduleswitches) then
  999. if (store_crc<>current_module.crc) and simplify_ppu then
  1000. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1001. {$endif EXTDEBUG}
  1002. { release all overload references and local symtables that
  1003. are not needed anymore }
  1004. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1005. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1006. free_localsymtables(current_module.globalsymtable);
  1007. free_localsymtables(current_module.localsymtable);
  1008. { leave when we got an error }
  1009. if (Errorcount>0) and not status.skip_error then
  1010. begin
  1011. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1012. status.skip_error:=true;
  1013. exit;
  1014. end;
  1015. Message1(unit_u_finished_compiling,current_module.modulename^);
  1016. end;
  1017. procedure proc_program(islibrary : boolean);
  1018. var
  1019. main_file : tinputfile;
  1020. hp,hp2 : tmodule;
  1021. pd : tprocdef;
  1022. begin
  1023. DLLsource:=islibrary;
  1024. Status.IsLibrary:=IsLibrary;
  1025. Status.IsExe:=true;
  1026. parse_only:=false;
  1027. { DLL defaults to create reloc info }
  1028. if islibrary then
  1029. begin
  1030. if not RelocSectionSetExplicitly then
  1031. RelocSection:=true;
  1032. end;
  1033. { relocation works only without stabs under win32 !! PM }
  1034. { internal assembler uses rva for stabs info
  1035. so it should work with relocated DLLs }
  1036. if RelocSection and
  1037. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1038. (target_info.assem<>as_i386_pecoff) then
  1039. begin
  1040. include(aktglobalswitches,cs_link_strip);
  1041. { Warning stabs info does not work with reloc section !! }
  1042. if cs_debuginfo in aktmoduleswitches then
  1043. begin
  1044. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1045. Message(parser_w_parser_win32_debug_needs_WN);
  1046. exclude(aktmoduleswitches,cs_debuginfo);
  1047. end;
  1048. end;
  1049. { get correct output names }
  1050. main_file := current_scanner.inputfile;
  1051. while assigned(main_file.next) do
  1052. main_file := main_file.next;
  1053. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1054. if islibrary then
  1055. begin
  1056. consume(_LIBRARY);
  1057. stringdispose(current_module.modulename);
  1058. stringdispose(current_module.realmodulename);
  1059. current_module.modulename:=stringdup(pattern);
  1060. current_module.realmodulename:=stringdup(orgpattern);
  1061. current_module.islibrary:=true;
  1062. exportlib.preparelib(orgpattern);
  1063. if tf_library_needs_pic in target_info.flags then
  1064. include(aktmoduleswitches,cs_create_pic);
  1065. consume(_ID);
  1066. consume(_SEMICOLON);
  1067. end
  1068. else
  1069. { is there an program head ? }
  1070. if token=_PROGRAM then
  1071. begin
  1072. consume(_PROGRAM);
  1073. stringdispose(current_module.modulename);
  1074. stringdispose(current_module.realmodulename);
  1075. current_module.modulename:=stringdup(pattern);
  1076. current_module.realmodulename:=stringdup(orgpattern);
  1077. if (target_info.system in system_unit_program_exports) then
  1078. exportlib.preparelib(orgpattern);
  1079. consume(_ID);
  1080. if token=_LKLAMMER then
  1081. begin
  1082. consume(_LKLAMMER);
  1083. repeat
  1084. consume(_ID);
  1085. until not try_to_consume(_COMMA);
  1086. consume(_RKLAMMER);
  1087. end;
  1088. consume(_SEMICOLON);
  1089. end
  1090. else if (target_info.system in system_unit_program_exports) then
  1091. exportlib.preparelib(current_module.realmodulename^);
  1092. { global switches are read, so further changes aren't allowed }
  1093. current_module.in_global:=false;
  1094. { setup things using the switches }
  1095. setupglobalswitches;
  1096. { set implementation flag }
  1097. current_module.in_interface:=false;
  1098. current_module.interface_compiled:=true;
  1099. { insert after the unit symbol tables the static symbol table }
  1100. { of the program }
  1101. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1102. { load standard units (system,objpas,profile unit) }
  1103. loaddefaultunits;
  1104. { Load units provided on the command line }
  1105. loadautounits;
  1106. {Load the units used by the program we compile.}
  1107. if token=_USES then
  1108. loadunits;
  1109. { reset ranges/stabs in exported definitions }
  1110. reset_all_defs;
  1111. { All units are read, now give them a number }
  1112. current_module.updatemaps;
  1113. {Insert the name of the main program into the symbol table.}
  1114. if current_module.realmodulename^<>'' then
  1115. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1116. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1117. symtablestack.push(current_module.localsymtable);
  1118. { The program intialization needs an alias, so it can be called
  1119. from the bootstrap code.}
  1120. if islibrary then
  1121. begin
  1122. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1123. { Win32 startup code needs a single name }
  1124. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1125. pd.aliasnames.insert('PASCALMAIN')
  1126. else
  1127. pd.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1128. end
  1129. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1130. begin
  1131. pd:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1132. end
  1133. else
  1134. begin
  1135. pd:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1136. pd.aliasnames.insert('PASCALMAIN');
  1137. end;
  1138. tcgprocinfo(current_procinfo).parse_body;
  1139. tcgprocinfo(current_procinfo).generate_code;
  1140. tcgprocinfo(current_procinfo).resetprocdef;
  1141. { save file pos for debuginfo }
  1142. current_module.mainfilepos:=current_procinfo.entrypos;
  1143. release_main_proc(pd);
  1144. { Generate specializations of objectdefs methods }
  1145. generate_specialization_procs;
  1146. { should we force unit initialization? }
  1147. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1148. begin
  1149. { initialize section }
  1150. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1151. { finalize section }
  1152. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1153. end;
  1154. { Add symbol to the exports section for win32 so smartlinking a
  1155. DLL will include the edata section }
  1156. if assigned(exportlib) and
  1157. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1158. ((current_module.flags and uf_has_exports)<>0) then
  1159. current_asmdata.asmlists[al_procedures].concat(tai_const.create_sym(exportlib.edatalabel));
  1160. { finalize? }
  1161. if token=_FINALIZATION then
  1162. begin
  1163. { set module options }
  1164. current_module.flags:=current_module.flags or uf_finalize;
  1165. { Compile the finalize }
  1166. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1167. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1168. tcgprocinfo(current_procinfo).parse_body;
  1169. tcgprocinfo(current_procinfo).generate_code;
  1170. tcgprocinfo(current_procinfo).resetprocdef;
  1171. release_main_proc(pd);
  1172. end;
  1173. symtablestack.pop(current_module.localsymtable);
  1174. { consume the last point }
  1175. consume(_POINT);
  1176. if (Errorcount=0) then
  1177. begin
  1178. { test static symtable }
  1179. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1180. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1181. tstoredsymtable(current_module.localsymtable).check_forwards;
  1182. tstoredsymtable(current_module.localsymtable).checklabels;
  1183. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1184. current_module.allunitsused;
  1185. end;
  1186. { leave when we got an error }
  1187. if (Errorcount>0) and not status.skip_error then
  1188. begin
  1189. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1190. status.skip_error:=true;
  1191. exit;
  1192. end;
  1193. { remove all unused units, this happends when units are removed
  1194. from the uses clause in the source and the ppu was already being loaded }
  1195. hp:=tmodule(loaded_units.first);
  1196. while assigned(hp) do
  1197. begin
  1198. hp2:=hp;
  1199. hp:=tmodule(hp.next);
  1200. if hp2.is_unit and
  1201. not assigned(hp2.globalsymtable) then
  1202. loaded_units.remove(hp2);
  1203. end;
  1204. { do we need to add the variants unit? }
  1205. maybeloadvariantsunit;
  1206. { generate debuginfo }
  1207. if (cs_debuginfo in aktmoduleswitches) then
  1208. debuginfo.inserttypeinfo;
  1209. InsertThreadvars;
  1210. { generate wrappers for interfaces }
  1211. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1212. { generate pic helpers to load eip if necessary }
  1213. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1214. { generate imports }
  1215. if current_module.uses_imports then
  1216. importlib.generatelib;
  1217. if islibrary or (target_info.system in system_unit_program_exports) then
  1218. exportlib.generatelib;
  1219. { Reference all DEBUGINFO sections from the main .text section }
  1220. if (cs_debuginfo in aktmoduleswitches) then
  1221. debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1222. { Resource strings }
  1223. GenerateResourceStrings;
  1224. { insert Tables and StackLength }
  1225. insertinitfinaltable;
  1226. InsertThreadvarTablesTable;
  1227. InsertResourceTablesTable;
  1228. insertmemorysizes;
  1229. { Insert symbol to resource info }
  1230. InsertResourceInfo;
  1231. { create dwarf debuginfo }
  1232. create_dwarf;
  1233. { insert own objectfile }
  1234. insertobjectfile;
  1235. { assemble and link }
  1236. create_objectfile;
  1237. { release all local symtables that are not needed anymore }
  1238. free_localsymtables(current_module.localsymtable);
  1239. { leave when we got an error }
  1240. if (Errorcount>0) and not status.skip_error then
  1241. begin
  1242. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1243. status.skip_error:=true;
  1244. exit;
  1245. end;
  1246. if (not current_module.is_unit) then
  1247. begin
  1248. { create the executable when we are at level 1 }
  1249. if (compile_level=1) then
  1250. begin
  1251. { write .def file }
  1252. if (cs_link_deffile in aktglobalswitches) then
  1253. deffile.writefile;
  1254. { insert all .o files from all loaded units and
  1255. unload the units, we don't need them anymore.
  1256. Keep the current_module because that is still needed }
  1257. hp:=tmodule(loaded_units.first);
  1258. while assigned(hp) do
  1259. begin
  1260. linker.AddModuleFiles(hp);
  1261. hp2:=tmodule(hp.next);
  1262. if hp<>current_module then
  1263. begin
  1264. loaded_units.remove(hp);
  1265. hp.free;
  1266. end;
  1267. hp:=hp2;
  1268. end;
  1269. { finally we can create a executable }
  1270. if DLLSource then
  1271. linker.MakeSharedLibrary
  1272. else
  1273. linker.MakeExecutable;
  1274. end;
  1275. end;
  1276. end;
  1277. end.