pmodules.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. begin
  626. def_system_macro('FPC_PIC');
  627. def_system_macro('PIC');
  628. end;
  629. end;
  630. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  631. var
  632. ps : tprocsym;
  633. pd : tprocdef;
  634. begin
  635. { there should be no current_procinfo available }
  636. if assigned(current_procinfo) then
  637. internalerror(200304275);
  638. {Generate a procsym for main}
  639. ps:=tprocsym.create('$'+name);
  640. { main are allways used }
  641. inc(ps.refs);
  642. st.insert(ps);
  643. pd:=tprocdef.create(main_program_level);
  644. include(pd.procoptions,po_global);
  645. pd.procsym:=ps;
  646. ps.addprocdef(pd);
  647. { set procdef options }
  648. pd.proctypeoption:=potype;
  649. pd.proccalloption:=pocall_default;
  650. pd.forwarddef:=false;
  651. pd.setmangledname(target_info.cprefix+name);
  652. pd.aliasnames.insert(pd.mangledname);
  653. handle_calling_convention(pd);
  654. { We don't need is a local symtable. Change it into the static
  655. symtable }
  656. pd.localst.free;
  657. pd.localst:=st;
  658. { set procinfo and current_procinfo.procdef }
  659. current_procinfo:=cprocinfo.create(nil);
  660. current_module.procinfo:=current_procinfo;
  661. current_procinfo.procdef:=pd;
  662. { return procdef }
  663. create_main_proc:=pd;
  664. { main proc does always a call e.g. to init system unit }
  665. include(current_procinfo.flags,pi_do_call);
  666. end;
  667. procedure release_main_proc(pd:tprocdef);
  668. begin
  669. { this is a main proc, so there should be no parent }
  670. if not(assigned(current_procinfo)) or
  671. assigned(current_procinfo.parent) or
  672. not(current_procinfo.procdef=pd) then
  673. internalerror(200304276);
  674. { remove procinfo }
  675. current_module.procinfo:=nil;
  676. current_procinfo.free;
  677. current_procinfo:=nil;
  678. { remove localst as it was replaced by staticsymtable }
  679. pd.localst:=nil;
  680. end;
  681. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  682. var
  683. pd : tprocdef;
  684. begin
  685. { update module flags }
  686. current_module.flags:=current_module.flags or flag;
  687. { create procdef }
  688. case flag of
  689. uf_init :
  690. begin
  691. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  692. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  693. end;
  694. uf_finalize :
  695. begin
  696. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  697. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  698. end;
  699. else
  700. internalerror(200304253);
  701. end;
  702. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  703. tcgprocinfo(current_procinfo).generate_code;
  704. release_main_proc(pd);
  705. end;
  706. procedure copy_macro(p:TNamedIndexItem; arg:pointer);
  707. begin
  708. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  709. end;
  710. procedure proc_unit;
  711. function is_assembler_generated:boolean;
  712. var
  713. hal : tasmlisttype;
  714. begin
  715. result:=false;
  716. if Errorcount=0 then
  717. begin
  718. for hal:=low(TasmlistType) to high(TasmlistType) do
  719. if not current_asmdata.asmlists[hal].empty then
  720. begin
  721. result:=true;
  722. exit;
  723. end;
  724. end;
  725. end;
  726. var
  727. main_file: tinputfile;
  728. {$ifdef EXTDEBUG}
  729. store_crc,
  730. {$endif EXTDEBUG}
  731. store_interface_crc : cardinal;
  732. s1,s2 : ^string; {Saves stack space}
  733. force_init_final : boolean;
  734. pd : tprocdef;
  735. unitname8 : string[8];
  736. has_impl,ag: boolean;
  737. globalvarsym : tglobalvarsym;
  738. begin
  739. if m_mac in aktmodeswitches then
  740. current_module.mode_switch_allowed:= false;
  741. consume(_UNIT);
  742. if compile_level=1 then
  743. Status.IsExe:=false;
  744. if token=_ID then
  745. begin
  746. { create filenames and unit name }
  747. main_file := current_scanner.inputfile;
  748. while assigned(main_file.next) do
  749. main_file := main_file.next;
  750. new(s1);
  751. s1^:=current_module.modulename^;
  752. current_module.SetFileName(main_file.path^+main_file.name^,true);
  753. current_module.SetModuleName(orgpattern);
  754. { check for system unit }
  755. new(s2);
  756. s2^:=upper(SplitName(main_file.name^));
  757. unitname8:=copy(current_module.modulename^,1,8);
  758. if (cs_check_unit_name in aktglobalswitches) and
  759. (
  760. not(
  761. (current_module.modulename^=s2^) or
  762. (
  763. (length(current_module.modulename^)>8) and
  764. (unitname8=s2^)
  765. )
  766. )
  767. or
  768. (
  769. (length(s1^)>8) and
  770. (s1^<>current_module.modulename^)
  771. )
  772. ) then
  773. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  774. if (current_module.modulename^='SYSTEM') then
  775. include(aktmoduleswitches,cs_compilesystem);
  776. dispose(s2);
  777. dispose(s1);
  778. end;
  779. if (target_info.system in system_unit_program_exports) then
  780. exportlib.preparelib(current_module.realmodulename^);
  781. consume(_ID);
  782. consume(_SEMICOLON);
  783. consume(_INTERFACE);
  784. { global switches are read, so further changes aren't allowed }
  785. current_module.in_global:=false;
  786. { handle the global switches }
  787. setupglobalswitches;
  788. message1(unit_u_loading_interface_units,current_module.modulename^);
  789. { update status }
  790. status.currentmodule:=current_module.realmodulename^;
  791. { maybe turn off m_objpas if we are compiling objpas }
  792. if (current_module.modulename^='OBJPAS') then
  793. exclude(aktmodeswitches,m_objpas);
  794. { maybe turn off m_mac if we are compiling macpas }
  795. if (current_module.modulename^='MACPAS') then
  796. exclude(aktmodeswitches,m_mac);
  797. parse_only:=true;
  798. { generate now the global symboltable,
  799. define first as local to overcome dependency conflicts }
  800. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  801. { insert unitsym of this unit to prevent other units having
  802. the same name }
  803. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  804. { load default units, like the system unit }
  805. loaddefaultunits;
  806. { reset }
  807. make_ref:=true;
  808. { insert qualifier for the system unit (allows system.writeln) }
  809. if not(cs_compilesystem in aktmoduleswitches) and
  810. (token=_USES) then
  811. begin
  812. loadunits;
  813. { has it been compiled at a higher level ?}
  814. if current_module.state=ms_compiled then
  815. exit;
  816. end;
  817. { move the global symtable from the temporary local to global }
  818. current_module.globalsymtable:=current_module.localsymtable;
  819. current_module.localsymtable:=nil;
  820. reset_all_defs;
  821. { number all units, so we know if a unit is used by this unit or
  822. needs to be added implicitly }
  823. current_module.updatemaps;
  824. { ... parse the declarations }
  825. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  826. symtablestack.push(current_module.globalsymtable);
  827. read_interface_declarations;
  828. symtablestack.pop(current_module.globalsymtable);
  829. { Export macros defined in the interface for macpas. The macros
  830. are put in the globalmacrosymtable that will only be used by other
  831. units. The current unit continues to use the localmacrosymtable }
  832. if (m_mac in aktmodeswitches) then
  833. begin
  834. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  835. current_module.localmacrosymtable.foreach_static(@copy_macro,nil);
  836. end;
  837. { leave when we got an error }
  838. if (Errorcount>0) and not status.skip_error then
  839. begin
  840. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  841. status.skip_error:=true;
  842. exit;
  843. end;
  844. { Our interface is compiled, generate CRC and switch to implementation }
  845. if not(cs_compilesystem in aktmoduleswitches) and
  846. (Errorcount=0) then
  847. tppumodule(current_module).getppucrc;
  848. current_module.in_interface:=false;
  849. current_module.interface_compiled:=true;
  850. { First reload all units depending on our interface, we need to do this
  851. in the implementation part to prevent errorneous circular references }
  852. reload_flagged_units;
  853. { Parse the implementation section }
  854. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  855. has_impl:= false
  856. else
  857. has_impl:= true;
  858. parse_only:=false;
  859. { generates static symbol table }
  860. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  861. {$ifdef i386}
  862. if cs_create_pic in aktmoduleswitches then
  863. begin
  864. { insert symbol for got access in assembler code}
  865. globalvarsym:=tglobalvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external,vo_is_C_var]);
  866. globalvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  867. current_module.localsymtable.insert(globalvarsym);
  868. { avoid unnecessary warnings }
  869. globalvarsym.varstate:=vs_read;
  870. globalvarsym.refs:=1;
  871. end;
  872. {$endif i386}
  873. if has_impl then
  874. begin
  875. consume(_IMPLEMENTATION);
  876. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  877. { Read the implementation units }
  878. parse_implementation_uses;
  879. end;
  880. if current_module.state=ms_compiled then
  881. exit;
  882. { reset ranges/stabs in exported definitions }
  883. reset_all_defs;
  884. { All units are read, now give them a number }
  885. current_module.updatemaps;
  886. symtablestack.push(current_module.globalsymtable);
  887. symtablestack.push(current_module.localsymtable);
  888. if has_impl then
  889. begin
  890. Message1(parser_u_parsing_implementation,current_module.modulename^);
  891. if current_module.in_interface then
  892. internalerror(200212285);
  893. { Compile the unit }
  894. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  895. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  896. tcgprocinfo(current_procinfo).parse_body;
  897. tcgprocinfo(current_procinfo).generate_code;
  898. tcgprocinfo(current_procinfo).resetprocdef;
  899. { save file pos for debuginfo }
  900. current_module.mainfilepos:=current_procinfo.entrypos;
  901. release_main_proc(pd);
  902. end;
  903. { Generate specializations of objectdefs methods }
  904. generate_specialization_procs;
  905. { if the unit contains ansi/widestrings, initialization and
  906. finalization code must be forced }
  907. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  908. tstaticsymtable(current_module.localsymtable).needs_init_final;
  909. { should we force unit initialization? }
  910. { this is a hack, but how can it be done better ? }
  911. if force_init_final and ((current_module.flags and uf_init)=0) then
  912. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  913. { finalize? }
  914. if has_impl and (token=_FINALIZATION) then
  915. begin
  916. { set module options }
  917. current_module.flags:=current_module.flags or uf_finalize;
  918. { Compile the finalize }
  919. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  920. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  921. tcgprocinfo(current_procinfo).parse_body;
  922. tcgprocinfo(current_procinfo).generate_code;
  923. tcgprocinfo(current_procinfo).resetprocdef;
  924. release_main_proc(pd);
  925. end
  926. else if force_init_final then
  927. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  928. symtablestack.pop(current_module.localsymtable);
  929. symtablestack.pop(current_module.globalsymtable);
  930. { the last char should always be a point }
  931. consume(_POINT);
  932. if (Errorcount=0) then
  933. begin
  934. { tests, if all (interface) forwards are resolved }
  935. tstoredsymtable(current_module.globalsymtable).check_forwards;
  936. { check if all private fields are used }
  937. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  938. { remove cross unit overloads }
  939. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  940. { test static symtable }
  941. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  942. tstoredsymtable(current_module.localsymtable).allprivatesused;
  943. tstoredsymtable(current_module.localsymtable).check_forwards;
  944. tstoredsymtable(current_module.localsymtable).checklabels;
  945. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  946. { used units }
  947. current_module.allunitsused;
  948. end;
  949. { leave when we got an error }
  950. if (Errorcount>0) and not status.skip_error then
  951. begin
  952. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  953. status.skip_error:=true;
  954. exit;
  955. end;
  956. { do we need to add the variants unit? }
  957. maybeloadvariantsunit;
  958. { generate debuginfo }
  959. if (cs_debuginfo in aktmoduleswitches) then
  960. debuginfo.inserttypeinfo;
  961. { generate wrappers for interfaces }
  962. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  963. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  964. { generate pic helpers to load eip if necessary }
  965. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  966. { Tables }
  967. insertThreadVars;
  968. { Resource strings }
  969. GenerateResourceStrings;
  970. { generate imports }
  971. if current_module.uses_imports then
  972. importlib.generatelib;
  973. { insert own objectfile, or say that it's in a library
  974. (no check for an .o when loading) }
  975. ag:=is_assembler_generated;
  976. if ag then
  977. insertobjectfile
  978. else
  979. begin
  980. current_module.flags:=current_module.flags or uf_no_link;
  981. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  982. end;
  983. if ag then
  984. begin
  985. { create dwarf debuginfo }
  986. create_dwarf;
  987. { assemble }
  988. create_objectfile;
  989. end;
  990. { Write out the ppufile after the object file has been created }
  991. store_interface_crc:=current_module.interface_crc;
  992. {$ifdef EXTDEBUG}
  993. store_crc:=current_module.crc;
  994. {$endif EXTDEBUG}
  995. if (Errorcount=0) then
  996. tppumodule(current_module).writeppu;
  997. if not(cs_compilesystem in aktmoduleswitches) then
  998. if store_interface_crc<>current_module.interface_crc then
  999. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1000. {$ifdef EXTDEBUG}
  1001. if not(cs_compilesystem in aktmoduleswitches) then
  1002. if (store_crc<>current_module.crc) and simplify_ppu then
  1003. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1004. {$endif EXTDEBUG}
  1005. { release all overload references and local symtables that
  1006. are not needed anymore }
  1007. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1008. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1009. free_localsymtables(current_module.globalsymtable);
  1010. free_localsymtables(current_module.localsymtable);
  1011. { leave when we got an error }
  1012. if (Errorcount>0) and not status.skip_error then
  1013. begin
  1014. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1015. status.skip_error:=true;
  1016. exit;
  1017. end;
  1018. Message1(unit_u_finished_compiling,current_module.modulename^);
  1019. end;
  1020. procedure proc_program(islibrary : boolean);
  1021. var
  1022. main_file : tinputfile;
  1023. hp,hp2 : tmodule;
  1024. pd : tprocdef;
  1025. begin
  1026. DLLsource:=islibrary;
  1027. Status.IsLibrary:=IsLibrary;
  1028. Status.IsExe:=true;
  1029. parse_only:=false;
  1030. { DLL defaults to create reloc info }
  1031. if islibrary then
  1032. begin
  1033. if not RelocSectionSetExplicitly then
  1034. RelocSection:=true;
  1035. end;
  1036. { relocation works only without stabs under win32 !! PM }
  1037. { internal assembler uses rva for stabs info
  1038. so it should work with relocated DLLs }
  1039. if RelocSection and
  1040. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1041. (target_info.assem<>as_i386_pecoff) then
  1042. begin
  1043. include(aktglobalswitches,cs_link_strip);
  1044. { Warning stabs info does not work with reloc section !! }
  1045. if cs_debuginfo in aktmoduleswitches then
  1046. begin
  1047. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1048. Message(parser_w_parser_win32_debug_needs_WN);
  1049. exclude(aktmoduleswitches,cs_debuginfo);
  1050. end;
  1051. end;
  1052. { get correct output names }
  1053. main_file := current_scanner.inputfile;
  1054. while assigned(main_file.next) do
  1055. main_file := main_file.next;
  1056. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1057. if islibrary then
  1058. begin
  1059. consume(_LIBRARY);
  1060. stringdispose(current_module.modulename);
  1061. stringdispose(current_module.realmodulename);
  1062. current_module.modulename:=stringdup(pattern);
  1063. current_module.realmodulename:=stringdup(orgpattern);
  1064. current_module.islibrary:=true;
  1065. exportlib.preparelib(orgpattern);
  1066. if tf_library_needs_pic in target_info.flags then
  1067. include(aktmoduleswitches,cs_create_pic);
  1068. consume(_ID);
  1069. consume(_SEMICOLON);
  1070. end
  1071. else
  1072. { is there an program head ? }
  1073. if token=_PROGRAM then
  1074. begin
  1075. consume(_PROGRAM);
  1076. stringdispose(current_module.modulename);
  1077. stringdispose(current_module.realmodulename);
  1078. current_module.modulename:=stringdup(pattern);
  1079. current_module.realmodulename:=stringdup(orgpattern);
  1080. if (target_info.system in system_unit_program_exports) then
  1081. exportlib.preparelib(orgpattern);
  1082. consume(_ID);
  1083. if token=_LKLAMMER then
  1084. begin
  1085. consume(_LKLAMMER);
  1086. repeat
  1087. consume(_ID);
  1088. until not try_to_consume(_COMMA);
  1089. consume(_RKLAMMER);
  1090. end;
  1091. consume(_SEMICOLON);
  1092. end
  1093. else if (target_info.system in system_unit_program_exports) then
  1094. exportlib.preparelib(current_module.realmodulename^);
  1095. { global switches are read, so further changes aren't allowed }
  1096. current_module.in_global:=false;
  1097. { setup things using the switches }
  1098. setupglobalswitches;
  1099. { set implementation flag }
  1100. current_module.in_interface:=false;
  1101. current_module.interface_compiled:=true;
  1102. { insert after the unit symbol tables the static symbol table }
  1103. { of the program }
  1104. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1105. { load standard units (system,objpas,profile unit) }
  1106. loaddefaultunits;
  1107. { Load units provided on the command line }
  1108. loadautounits;
  1109. {Load the units used by the program we compile.}
  1110. if token=_USES then
  1111. loadunits;
  1112. { reset ranges/stabs in exported definitions }
  1113. reset_all_defs;
  1114. { All units are read, now give them a number }
  1115. current_module.updatemaps;
  1116. {Insert the name of the main program into the symbol table.}
  1117. if current_module.realmodulename^<>'' then
  1118. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1119. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1120. symtablestack.push(current_module.localsymtable);
  1121. { The program intialization needs an alias, so it can be called
  1122. from the bootstrap code.}
  1123. if islibrary then
  1124. begin
  1125. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1126. { Win32 startup code needs a single name }
  1127. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1128. pd.aliasnames.insert('PASCALMAIN')
  1129. else
  1130. pd.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1131. end
  1132. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1133. begin
  1134. pd:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1135. end
  1136. else
  1137. begin
  1138. pd:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1139. pd.aliasnames.insert('PASCALMAIN');
  1140. end;
  1141. tcgprocinfo(current_procinfo).parse_body;
  1142. tcgprocinfo(current_procinfo).generate_code;
  1143. tcgprocinfo(current_procinfo).resetprocdef;
  1144. { save file pos for debuginfo }
  1145. current_module.mainfilepos:=current_procinfo.entrypos;
  1146. release_main_proc(pd);
  1147. { Generate specializations of objectdefs methods }
  1148. generate_specialization_procs;
  1149. { should we force unit initialization? }
  1150. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1151. begin
  1152. { initialize section }
  1153. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1154. { finalize section }
  1155. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1156. end;
  1157. { Add symbol to the exports section for win32 so smartlinking a
  1158. DLL will include the edata section }
  1159. if assigned(exportlib) and
  1160. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1161. ((current_module.flags and uf_has_exports)<>0) then
  1162. current_asmdata.asmlists[al_procedures].concat(tai_const.create_sym(exportlib.edatalabel));
  1163. { finalize? }
  1164. if token=_FINALIZATION then
  1165. begin
  1166. { set module options }
  1167. current_module.flags:=current_module.flags or uf_finalize;
  1168. { Compile the finalize }
  1169. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1170. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1171. tcgprocinfo(current_procinfo).parse_body;
  1172. tcgprocinfo(current_procinfo).generate_code;
  1173. tcgprocinfo(current_procinfo).resetprocdef;
  1174. release_main_proc(pd);
  1175. end;
  1176. symtablestack.pop(current_module.localsymtable);
  1177. { consume the last point }
  1178. consume(_POINT);
  1179. if (Errorcount=0) then
  1180. begin
  1181. { test static symtable }
  1182. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1183. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1184. tstoredsymtable(current_module.localsymtable).check_forwards;
  1185. tstoredsymtable(current_module.localsymtable).checklabels;
  1186. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1187. current_module.allunitsused;
  1188. end;
  1189. { leave when we got an error }
  1190. if (Errorcount>0) and not status.skip_error then
  1191. begin
  1192. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1193. status.skip_error:=true;
  1194. exit;
  1195. end;
  1196. { remove all unused units, this happends when units are removed
  1197. from the uses clause in the source and the ppu was already being loaded }
  1198. hp:=tmodule(loaded_units.first);
  1199. while assigned(hp) do
  1200. begin
  1201. hp2:=hp;
  1202. hp:=tmodule(hp.next);
  1203. if hp2.is_unit and
  1204. not assigned(hp2.globalsymtable) then
  1205. loaded_units.remove(hp2);
  1206. end;
  1207. { do we need to add the variants unit? }
  1208. maybeloadvariantsunit;
  1209. { generate debuginfo }
  1210. if (cs_debuginfo in aktmoduleswitches) then
  1211. debuginfo.inserttypeinfo;
  1212. InsertThreadvars;
  1213. { generate wrappers for interfaces }
  1214. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1215. { generate pic helpers to load eip if necessary }
  1216. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1217. { generate imports }
  1218. if current_module.uses_imports then
  1219. importlib.generatelib;
  1220. if islibrary or (target_info.system in system_unit_program_exports) then
  1221. exportlib.generatelib;
  1222. { Reference all DEBUGINFO sections from the main .text section }
  1223. if (cs_debuginfo in aktmoduleswitches) then
  1224. debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1225. { Resource strings }
  1226. GenerateResourceStrings;
  1227. { insert Tables and StackLength }
  1228. insertinitfinaltable;
  1229. InsertThreadvarTablesTable;
  1230. InsertResourceTablesTable;
  1231. insertmemorysizes;
  1232. { Insert symbol to resource info }
  1233. InsertResourceInfo;
  1234. { create dwarf debuginfo }
  1235. create_dwarf;
  1236. { insert own objectfile }
  1237. insertobjectfile;
  1238. { assemble and link }
  1239. create_objectfile;
  1240. { release all local symtables that are not needed anymore }
  1241. free_localsymtables(current_module.localsymtable);
  1242. { leave when we got an error }
  1243. if (Errorcount>0) and not status.skip_error then
  1244. begin
  1245. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1246. status.skip_error:=true;
  1247. exit;
  1248. end;
  1249. if (not current_module.is_unit) then
  1250. begin
  1251. { create the executable when we are at level 1 }
  1252. if (compile_level=1) then
  1253. begin
  1254. { write .def file }
  1255. if (cs_link_deffile in aktglobalswitches) then
  1256. deffile.writefile;
  1257. { insert all .o files from all loaded units and
  1258. unload the units, we don't need them anymore.
  1259. Keep the current_module because that is still needed }
  1260. hp:=tmodule(loaded_units.first);
  1261. while assigned(hp) do
  1262. begin
  1263. linker.AddModuleFiles(hp);
  1264. hp2:=tmodule(hp.next);
  1265. if hp<>current_module then
  1266. begin
  1267. loaded_units.remove(hp);
  1268. hp.free;
  1269. end;
  1270. hp:=hp2;
  1271. end;
  1272. { finally we can create a executable }
  1273. if DLLSource then
  1274. linker.MakeSharedLibrary
  1275. else
  1276. linker.MakeExecutable;
  1277. end;
  1278. end;
  1279. end;
  1280. end.