pmodules.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i fpcdefs.inc}
  20. {$define New_GDB}
  21. interface
  22. procedure proc_unit;
  23. procedure proc_program(islibrary : boolean);
  24. implementation
  25. uses
  26. globtype,version,systems,tokens,
  27. cutils,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. aasmbase,aasmtai,aasmcpu,
  31. cgbase,cpuinfo,
  32. ncgutil,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,cpubase,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure create_objectfile;
  40. var
  41. DLLScanner : TDLLScanner;
  42. s : string;
  43. KeepShared : TStringList;
  44. begin
  45. { try to create import entries from system dlls }
  46. if target_info.DllScanSupported and
  47. (not current_module.linkOtherSharedLibs.Empty) then
  48. begin
  49. { Init DLLScanner }
  50. if assigned(CDLLScanner[target_info.system]) then
  51. DLLScanner:=CDLLScanner[target_info.system].Create
  52. else
  53. internalerror(200104121);
  54. KeepShared:=TStringList.Create;
  55. { Walk all shared libs }
  56. While not current_module.linkOtherSharedLibs.Empty do
  57. begin
  58. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  59. if not DLLScanner.scan(s) then
  60. KeepShared.Concat(s);
  61. end;
  62. DLLscanner.Free;
  63. { Recreate import section }
  64. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  65. begin
  66. if assigned(importssection)then
  67. importssection.clear
  68. else
  69. importssection:=taasmoutput.Create;
  70. importlib.generatelib;
  71. end;
  72. { Readd the not processed files }
  73. while not KeepShared.Empty do
  74. begin
  75. s:=KeepShared.GetFirst;
  76. current_module.linkOtherSharedLibs.add(s,link_allways);
  77. end;
  78. KeepShared.Free;
  79. end;
  80. { create the .s file and assemble it }
  81. GenerateAsm(false);
  82. { Also create a smartlinked version ? }
  83. if (cs_create_smart in aktmoduleswitches) then
  84. begin
  85. { regenerate the importssection for win32 }
  86. if assigned(importssection) and
  87. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  88. begin
  89. importsSection.clear;
  90. importlib.generatesmartlib;
  91. end;
  92. GenerateAsm(true);
  93. if target_asm.needar then
  94. Linker.MakeStaticLibrary;
  95. end;
  96. { resource files }
  97. CompileResourceFiles;
  98. end;
  99. procedure insertobjectfile;
  100. { Insert the used object file for this unit in the used list for this unit }
  101. begin
  102. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  103. current_module.flags:=current_module.flags or uf_static_linked;
  104. if (cs_create_smart in aktmoduleswitches) then
  105. begin
  106. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  107. current_module.flags:=current_module.flags or uf_smart_linked;
  108. end;
  109. end;
  110. procedure insertsegment;
  111. procedure fixseg(p:TAAsmoutput;sec:TSection);
  112. begin
  113. p.insert(Tai_section.Create(sec));
  114. if (cs_create_smart in aktmoduleswitches) then
  115. p.insert(Tai_cut.Create);
  116. p.concat(Tai_section.Create(sec_none));
  117. end;
  118. begin
  119. { Insert Ident of the compiler }
  120. if (not (cs_create_smart in aktmoduleswitches))
  121. {$ifndef EXTDEBUG}
  122. and (not current_module.is_unit)
  123. {$endif}
  124. then
  125. begin
  126. { align the first data }
  127. dataSegment.insert(Tai_align.Create(const_align(32)));
  128. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  129. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  130. end;
  131. { align code segment }
  132. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  133. { Insert start and end of sections }
  134. fixseg(codesegment,sec_code);
  135. fixseg(datasegment,sec_data);
  136. fixseg(bsssegment,sec_bss);
  137. { we should use .rdata section for these two no ? }
  138. { .rdata is a read only data section (PM) }
  139. fixseg(rttilist,sec_data);
  140. fixseg(consts,sec_data);
  141. if assigned(resourcestringlist) then
  142. fixseg(resourcestringlist,sec_data);
  143. {$ifdef GDB}
  144. if assigned(debuglist) then
  145. begin
  146. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  147. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  148. fixseg(debuglist,sec_code);
  149. end;
  150. {$endif GDB}
  151. end;
  152. procedure InsertThreadvarTablesTable;
  153. var
  154. hp : tused_unit;
  155. ltvTables : taasmoutput;
  156. count : longint;
  157. begin
  158. ltvTables:=TAAsmOutput.Create;
  159. count:=0;
  160. hp:=tused_unit(usedunits.first);
  161. while assigned(hp) do
  162. begin
  163. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  164. begin
  165. ltvTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_$THREADVARLIST'));
  166. inc(count);
  167. end;
  168. hp:=tused_unit(hp.next);
  169. end;
  170. { Add program threadvars, if any }
  171. If (current_module.flags and uf_threadvars)=uf_threadvars then
  172. begin
  173. ltvTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_$THREADVARLIST'));
  174. inc(count);
  175. end;
  176. { TableCount }
  177. ltvTables.insert(Tai_const.Create_32bit(count));
  178. ltvTables.insert(Tai_symbol.Createdataname_global('FPC_THREADVARTABLES',0));
  179. ltvTables.insert(Tai_align.Create(const_align(pointer_size)));
  180. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  181. { insert in data segment }
  182. if (cs_create_smart in aktmoduleswitches) then
  183. dataSegment.concat(Tai_cut.Create);
  184. dataSegment.concatlist(ltvTables);
  185. ltvTables.free;
  186. if count > 0 then
  187. have_local_threadvars := true;
  188. end;
  189. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  190. var
  191. ltvTable : taasmoutput;
  192. begin
  193. ltvTable:=taasmoutput(arg);
  194. if (tsym(p).typ=varsym) and
  195. (vo_is_thread_var in tvarsym(p).varoptions) then
  196. begin
  197. { address of threadvar }
  198. ltvTable.concat(tai_const_symbol.createname(tvarsym(p).mangledname));
  199. { size of threadvar }
  200. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  201. end;
  202. end;
  203. procedure InsertThreadvars;
  204. var
  205. ltvTable : TAAsmoutput;
  206. begin
  207. ltvTable:=TAAsmoutput.create;
  208. if assigned(current_module.globalsymtable) then
  209. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  211. if ltvTable.first<>nil then
  212. begin
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$THREADVARLIST',0));
  215. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$THREADVARLIST'));
  217. if (cs_create_smart in aktmoduleswitches) then
  218. dataSegment.concat(Tai_cut.Create);
  219. dataSegment.concatlist(ltvTable);
  220. current_module.flags:=current_module.flags or uf_threadvars;
  221. end;
  222. ltvTable.Free;
  223. end;
  224. Procedure InsertResourceTablesTable;
  225. var
  226. hp : tused_unit;
  227. ResourceStringTables : taasmoutput;
  228. count : longint;
  229. begin
  230. ResourceStringTables:=TAAsmOutput.Create;
  231. count:=0;
  232. hp:=tused_unit(usedunits.first);
  233. while assigned(hp) do
  234. begin
  235. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  236. begin
  237. ResourceStringTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  238. inc(count);
  239. end;
  240. hp:=tused_unit(hp.next);
  241. end;
  242. { Add program resources, if any }
  243. If ResourceStringList<>Nil then
  244. begin
  245. ResourceStringTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_RESOURCESTRINGLIST'));
  246. Inc(Count);
  247. end;
  248. { TableCount }
  249. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  250. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  251. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  252. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  253. { insert in data segment }
  254. if (cs_create_smart in aktmoduleswitches) then
  255. dataSegment.concat(Tai_cut.Create);
  256. dataSegment.concatlist(ResourceStringTables);
  257. ResourceStringTables.free;
  258. end;
  259. procedure InsertInitFinalTable;
  260. var
  261. hp : tused_unit;
  262. unitinits : taasmoutput;
  263. count : longint;
  264. begin
  265. unitinits:=TAAsmOutput.Create;
  266. count:=0;
  267. hp:=tused_unit(usedunits.first);
  268. while assigned(hp) do
  269. begin
  270. { call the unit init code and make it external }
  271. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  272. begin
  273. if (hp.u.flags and uf_init)<>0 then
  274. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+hp.u.modulename^))
  275. else
  276. unitinits.concat(Tai_const.Create_32bit(0));
  277. if (hp.u.flags and uf_finalize)<>0 then
  278. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+hp.u.modulename^))
  279. else
  280. unitinits.concat(Tai_const.Create_32bit(0));
  281. inc(count);
  282. end;
  283. hp:=tused_unit(hp.next);
  284. end;
  285. { Insert initialization/finalization of the program }
  286. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  287. begin
  288. if (current_module.flags and uf_init)<>0 then
  289. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+current_module.modulename^))
  290. else
  291. unitinits.concat(Tai_const.Create_32bit(0));
  292. if (current_module.flags and uf_finalize)<>0 then
  293. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+current_module.modulename^))
  294. else
  295. unitinits.concat(Tai_const.Create_32bit(0));
  296. inc(count);
  297. end;
  298. { TableCount,InitCount }
  299. unitinits.insert(Tai_const.Create_32bit(0));
  300. unitinits.insert(Tai_const.Create_32bit(count));
  301. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  302. unitinits.insert(Tai_align.Create(const_align(4)));
  303. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  304. { insert in data segment }
  305. if (cs_create_smart in aktmoduleswitches) then
  306. dataSegment.concat(Tai_cut.Create);
  307. dataSegment.concatlist(unitinits);
  308. unitinits.free;
  309. end;
  310. procedure insertheap;
  311. begin
  312. if (cs_create_smart in aktmoduleswitches) then
  313. begin
  314. bssSegment.concat(Tai_cut.Create);
  315. dataSegment.concat(Tai_cut.Create);
  316. end;
  317. { On the Macintosh Classic M68k Architecture
  318. The Heap variable is simply a POINTER to the
  319. real HEAP. The HEAP must be set up by the RTL
  320. and must store the pointer in this value.
  321. On OS/2 the heap is also intialized by the RTL. We do
  322. not output a pointer }
  323. case target_info.system of
  324. {$ifdef x86_64}
  325. target_x86_64_linux:
  326. ;
  327. {$endif x86_64}
  328. {$ifdef i386}
  329. system_i386_OS2:
  330. ;
  331. {$endif i386}
  332. {$ifdef powerpc}
  333. system_powerpc_macos:
  334. ;
  335. {$endif powerpc}
  336. {$ifdef alpha}
  337. system_alpha_linux:
  338. ;
  339. {$endif alpha}
  340. {$ifdef m68k}
  341. system_m68k_Mac:
  342. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  343. system_m68k_PalmOS:
  344. ;
  345. {$endif m68k}
  346. {$IFDEF SPARC}
  347. system_SPARC_Linux:
  348. ;
  349. {$ENDIF SPARC}
  350. else
  351. begin
  352. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  353. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  354. end;
  355. end;
  356. {$ifdef m68k}
  357. if target_info.system<>system_m68k_PalmOS then
  358. begin
  359. dataSegment.concat(Tai_align.Create(const_align(4)));
  360. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  361. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  362. end;
  363. {$else m68k}
  364. dataSegment.concat(Tai_align.Create(const_align(4)));
  365. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  366. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  367. {$endif m68k}
  368. end;
  369. procedure insertstacklength;
  370. begin
  371. { stacksize can be specified and is now simulated }
  372. dataSegment.concat(Tai_align.Create(const_align(4)));
  373. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  374. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  375. end;
  376. procedure loaddefaultunits;
  377. var
  378. hp : tmodule;
  379. unitsym : tunitsym;
  380. begin
  381. { are we compiling the system unit? }
  382. if (cs_compilesystem in aktmoduleswitches) then
  383. begin
  384. { create system defines }
  385. createconstdefs;
  386. { we don't need to reset anything, it's already done in parser.pas }
  387. exit;
  388. end;
  389. { insert the system unit, it is allways the first }
  390. hp:=loadunit('System','');
  391. systemunit:=tglobalsymtable(hp.globalsymtable);
  392. { it's always the first unit }
  393. systemunit.next:=nil;
  394. symtablestack:=systemunit;
  395. { add to the used units }
  396. current_module.used_units.concat(tused_unit.create(hp,true));
  397. unitsym:=tunitsym.create('System',systemunit);
  398. inc(unitsym.refs);
  399. refsymtable.insert(unitsym);
  400. { read default constant definitions }
  401. make_ref:=false;
  402. readconstdefs;
  403. make_ref:=true;
  404. {$ifdef cpufpemu}
  405. { Floating point emulation unit? }
  406. if (cs_fp_emulation in aktmoduleswitches) then
  407. begin
  408. hp:=loadunit('softfpu','');
  409. tsymtable(hp.globalsymtable).next:=symtablestack;
  410. symtablestack:=hp.globalsymtable;
  411. { add to the used units }
  412. current_module.used_units.concat(tused_unit.create(hp,true));
  413. unitsym:=tunitsym.create('Softfpu',hp.globalsymtable);
  414. inc(unitsym.refs);
  415. refsymtable.insert(unitsym);
  416. end;
  417. {$endif cpufpemu}
  418. { Thread support unit? }
  419. if (cs_threading in aktmoduleswitches) then
  420. begin
  421. hp:=loadunit('systhrds','');
  422. tsymtable(hp.globalsymtable).next:=symtablestack;
  423. symtablestack:=hp.globalsymtable;
  424. { add to the used units }
  425. current_module.used_units.concat(tused_unit.create(hp,true));
  426. unitsym:=tunitsym.create('Threads',hp.globalsymtable);
  427. inc(unitsym.refs);
  428. refsymtable.insert(unitsym);
  429. end;
  430. { Objpas unit? }
  431. if m_objpas in aktmodeswitches then
  432. begin
  433. hp:=loadunit('ObjPas','');
  434. tsymtable(hp.globalsymtable).next:=symtablestack;
  435. symtablestack:=hp.globalsymtable;
  436. { add to the used units }
  437. current_module.used_units.concat(tused_unit.create(hp,true));
  438. unitsym:=tunitsym.create('ObjPas',hp.globalsymtable);
  439. inc(unitsym.refs);
  440. refsymtable.insert(unitsym);
  441. end;
  442. { Profile unit? Needed for go32v2 only }
  443. if (cs_profile in aktmoduleswitches) and (target_info.system=system_i386_go32v2) then
  444. begin
  445. hp:=loadunit('Profile','');
  446. tsymtable(hp.globalsymtable).next:=symtablestack;
  447. symtablestack:=hp.globalsymtable;
  448. { add to the used units }
  449. current_module.used_units.concat(tused_unit.create(hp,true));
  450. unitsym:=tunitsym.create('Profile',hp.globalsymtable);
  451. inc(unitsym.refs);
  452. refsymtable.insert(unitsym);
  453. end;
  454. { Units only required for main module }
  455. if not(current_module.is_unit) then
  456. begin
  457. { Heaptrc unit }
  458. if (cs_gdb_heaptrc in aktglobalswitches) then
  459. begin
  460. hp:=loadunit('HeapTrc','');
  461. tsymtable(hp.globalsymtable).next:=symtablestack;
  462. symtablestack:=hp.globalsymtable;
  463. { add to the used units }
  464. current_module.used_units.concat(tused_unit.create(hp,true));
  465. unitsym:=tunitsym.create('HeapTrc',hp.globalsymtable);
  466. inc(unitsym.refs);
  467. refsymtable.insert(unitsym);
  468. end;
  469. { Lineinfo unit }
  470. if (cs_gdb_lineinfo in aktglobalswitches) then
  471. begin
  472. hp:=loadunit('LineInfo','');
  473. tsymtable(hp.globalsymtable).next:=symtablestack;
  474. symtablestack:=hp.globalsymtable;
  475. { add to the used units }
  476. current_module.used_units.concat(tused_unit.create(hp,true));
  477. unitsym:=tunitsym.create('LineInfo',hp.globalsymtable);
  478. inc(unitsym.refs);
  479. refsymtable.insert(unitsym);
  480. end;
  481. end;
  482. { save default symtablestack }
  483. defaultsymtablestack:=symtablestack;
  484. end;
  485. procedure loadunits;
  486. var
  487. s,sorg : stringid;
  488. fn : string;
  489. pu,
  490. hp : tused_unit;
  491. hp2 : tmodule;
  492. hp3 : tsymtable;
  493. oldprocsym:tprocsym;
  494. oldprocdef:tprocdef;
  495. unitsym : tunitsym;
  496. begin
  497. oldprocsym:=aktprocsym;
  498. oldprocdef:=aktprocdef;
  499. consume(_USES);
  500. {$ifdef DEBUG}
  501. test_symtablestack;
  502. {$endif DEBUG}
  503. repeat
  504. s:=pattern;
  505. sorg:=orgpattern;
  506. consume(_ID);
  507. { support "<unit> in '<file>'" construct, but not for tp7 }
  508. if not(m_tp7 in aktmodeswitches) then
  509. begin
  510. if try_to_consume(_OP_IN) then
  511. fn:=FixFileName(get_stringconst)
  512. else
  513. fn:='';
  514. end;
  515. { Give a warning if objpas is loaded }
  516. if s='OBJPAS' then
  517. Message(parser_w_no_objpas_use_mode);
  518. { check if the unit is already used }
  519. pu:=tused_unit(current_module.used_units.first);
  520. while assigned(pu) do
  521. begin
  522. if (pu.name^=s) then
  523. break;
  524. pu:=tused_unit(pu.next);
  525. end;
  526. { avoid uses of itself }
  527. if not assigned(pu) and (s<>current_module.modulename^) then
  528. begin
  529. { load the unit }
  530. hp2:=loadunit(sorg,fn);
  531. { the current module uses the unit hp2 }
  532. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  533. tused_unit(current_module.used_units.last).in_uses:=true;
  534. if current_module.compiled then
  535. exit;
  536. unitsym:=tunitsym.create(sorg,hp2.globalsymtable);
  537. { never claim about unused unit if
  538. there is init or finalize code PM }
  539. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  540. inc(unitsym.refs);
  541. refsymtable.insert(unitsym);
  542. end
  543. else
  544. Message1(sym_e_duplicate_id,s);
  545. if token=_COMMA then
  546. begin
  547. pattern:='';
  548. consume(_COMMA);
  549. end
  550. else
  551. break;
  552. until false;
  553. consume(_SEMICOLON);
  554. { set the symtable to systemunit so it gets reorderd correctly }
  555. symtablestack:=defaultsymtablestack;
  556. { now insert the units in the symtablestack }
  557. hp:=tused_unit(current_module.used_units.first);
  558. while assigned(hp) do
  559. begin
  560. {$IfDef GDB}
  561. if (cs_debuginfo in aktmoduleswitches) and
  562. (cs_gdb_dbx in aktglobalswitches) and
  563. not hp.is_stab_written then
  564. begin
  565. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  566. hp.is_stab_written:=true;
  567. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  568. end;
  569. {$EndIf GDB}
  570. if hp.in_uses then
  571. begin
  572. hp3:=symtablestack;
  573. while assigned(hp3) do
  574. begin
  575. { insert units only once ! }
  576. if hp.u.globalsymtable=hp3 then
  577. break;
  578. hp3:=hp3.next;
  579. { unit isn't inserted }
  580. if hp3=nil then
  581. begin
  582. tsymtable(hp.u.globalsymtable).next:=symtablestack;
  583. symtablestack:=tsymtable(hp.u.globalsymtable);
  584. {$ifdef DEBUG}
  585. test_symtablestack;
  586. {$endif DEBUG}
  587. end;
  588. end;
  589. end;
  590. hp:=tused_unit(hp.next);
  591. end;
  592. aktprocsym:=oldprocsym;
  593. aktprocdef:=oldprocdef;
  594. end;
  595. procedure write_gdb_info;
  596. {$IfDef GDB}
  597. var
  598. hp : tused_unit;
  599. begin
  600. if not (cs_debuginfo in aktmoduleswitches) then
  601. exit;
  602. { now insert the units in the symtablestack }
  603. hp:=tused_unit(current_module.used_units.first);
  604. while assigned(hp) do
  605. begin
  606. if (cs_debuginfo in aktmoduleswitches) and
  607. not hp.is_stab_written then
  608. begin
  609. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  610. hp.is_stab_written:=true;
  611. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  612. end;
  613. hp:=tused_unit(hp.next);
  614. end;
  615. if current_module.in_implementation and
  616. assigned(current_module.localsymtable) then
  617. begin
  618. { all types }
  619. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  620. { and all local symbols}
  621. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  622. end
  623. else if assigned(current_module.globalsymtable) then
  624. begin
  625. { all types }
  626. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  627. { and all local symbols}
  628. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  629. end;
  630. if (cs_gdb_dbx in aktglobalswitches) then
  631. begin
  632. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  633. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  634. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  635. debugList.concat(Tai_stabs.Create(strpnew('"'+
  636. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  637. tostr(N_EINCL)+',0,0,0')));
  638. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  639. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  640. do_count_dbx:=false;
  641. end;
  642. end;
  643. {$Else GDB}
  644. begin
  645. end;
  646. {$EndIf GDB}
  647. procedure parse_implementation_uses(symt:tsymtable);
  648. begin
  649. if token=_USES then
  650. begin
  651. loadunits;
  652. {$ifdef DEBUG}
  653. test_symtablestack;
  654. {$endif DEBUG}
  655. end;
  656. end;
  657. procedure setupglobalswitches;
  658. begin
  659. { can't have local browser when no global browser }
  660. if (cs_local_browser in aktmoduleswitches) and
  661. not(cs_browser in aktmoduleswitches) then
  662. exclude(aktmoduleswitches,cs_local_browser);
  663. { define a symbol in delphi,objfpc,tp,gpc mode }
  664. if (m_delphi in aktmodeswitches) then
  665. current_scanner.def_macro('FPC_DELPHI')
  666. else
  667. if (m_tp7 in aktmodeswitches) then
  668. current_scanner.def_macro('FPC_TP')
  669. else
  670. if (m_objfpc in aktmodeswitches) then
  671. current_scanner.def_macro('FPC_OBJFPC')
  672. else
  673. if (m_gpc in aktmodeswitches) then
  674. current_scanner.def_macro('FPC_GPC');
  675. end;
  676. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  677. var
  678. stt : tsymtable;
  679. begin
  680. {Generate a procsym for main}
  681. make_ref:=false;
  682. aktprocsym:=tprocsym.create('$'+name);
  683. { main are allways used }
  684. inc(aktprocsym.refs);
  685. {Try to insert in in static symtable ! }
  686. stt:=symtablestack;
  687. symtablestack:=st;
  688. aktprocdef:=tprocdef.create;
  689. aktprocsym.addprocdef(aktprocdef);
  690. aktprocdef.procsym:=aktprocsym;
  691. symtablestack:=stt;
  692. aktprocdef.proctypeoption:=options;
  693. aktprocdef.setmangledname(target_info.cprefix+name);
  694. aktprocdef.forwarddef:=false;
  695. make_ref:=true;
  696. { The localst is a local symtable. Change it into the static
  697. symtable }
  698. aktprocdef.localst.free;
  699. aktprocdef.localst:=st;
  700. { and insert the procsym in symtable }
  701. st.insert(aktprocsym);
  702. { set some informations about the main program }
  703. with procinfo do
  704. begin
  705. _class:=nil;
  706. para_offset:=target_info.first_parm_offset;
  707. framepointer:=FRAME_POINTER_REG;
  708. flags:=0;
  709. procdef:=aktprocdef;
  710. end;
  711. end;
  712. procedure proc_unit;
  713. function is_assembler_generated:boolean;
  714. begin
  715. is_assembler_generated:=(Errorcount=0) and
  716. not(
  717. codeSegment.empty and
  718. dataSegment.empty and
  719. bssSegment.empty and
  720. ((importssection=nil) or importsSection.empty) and
  721. ((resourcesection=nil) or resourceSection.empty) and
  722. ((resourcestringlist=nil) or resourcestringList.empty)
  723. );
  724. end;
  725. var
  726. main_file: tinputfile;
  727. st : tsymtable;
  728. unitst : tglobalsymtable;
  729. {$ifdef GDB}
  730. pu : tused_unit;
  731. {$endif GDB}
  732. store_crc,store_interface_crc : cardinal;
  733. s2 : ^string; {Saves stack space}
  734. force_init_final : boolean;
  735. begin
  736. consume(_UNIT);
  737. if compile_level=1 then
  738. Status.IsExe:=false;
  739. if token=_ID then
  740. begin
  741. { create filenames and unit name }
  742. main_file := current_scanner.inputfile;
  743. while assigned(main_file.next) do
  744. main_file := main_file.next;
  745. current_module.SetFileName(main_file.path^+main_file.name^,true);
  746. stringdispose(current_module.modulename);
  747. stringdispose(current_module.realmodulename);
  748. current_module.modulename:=stringdup(pattern);
  749. current_module.realmodulename:=stringdup(orgpattern);
  750. { check for system unit }
  751. new(s2);
  752. s2^:=upper(SplitName(main_file.name^));
  753. if (cs_check_unit_name in aktglobalswitches) and
  754. not((current_module.modulename^=s2^) or
  755. ((length(current_module.modulename^)>8) and
  756. (copy(current_module.modulename^,1,8)=s2^))) then
  757. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  758. if (current_module.modulename^='SYSTEM') then
  759. include(aktmoduleswitches,cs_compilesystem);
  760. dispose(s2);
  761. end;
  762. consume(_ID);
  763. consume(_SEMICOLON);
  764. consume(_INTERFACE);
  765. { global switches are read, so further changes aren't allowed }
  766. current_module.in_global:=false;
  767. { handle the global switches }
  768. setupglobalswitches;
  769. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  770. { update status }
  771. status.currentmodule:=current_module.realmodulename^;
  772. { maybe turn off m_objpas if we are compiling objpas }
  773. if (current_module.modulename^='OBJPAS') then
  774. exclude(aktmodeswitches,m_objpas);
  775. parse_only:=true;
  776. { generate now the global symboltable }
  777. st:=tglobalsymtable.create(current_module.modulename^);
  778. refsymtable:=st;
  779. unitst:=tglobalsymtable(st);
  780. { define first as local to overcome dependency conflicts }
  781. current_module.localsymtable:=st;
  782. { the unit name must be usable as a unit specifier }
  783. { inside the unit itself (PM) }
  784. { this also forbids to have another symbol }
  785. { with the same name as the unit }
  786. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  787. { load default units, like the system unit }
  788. loaddefaultunits;
  789. { reset }
  790. make_ref:=true;
  791. lexlevel:=0;
  792. { insert qualifier for the system unit (allows system.writeln) }
  793. if not(cs_compilesystem in aktmoduleswitches) then
  794. begin
  795. if token=_USES then
  796. begin
  797. loadunits;
  798. { has it been compiled at a higher level ?}
  799. if current_module.compiled then
  800. begin
  801. { this unit symtable is obsolete }
  802. { dispose(unitst,done);
  803. disposed as localsymtable !! }
  804. RestoreUnitSyms;
  805. exit;
  806. end;
  807. end;
  808. { ... but insert the symbol table later }
  809. st.next:=symtablestack;
  810. symtablestack:=st;
  811. end
  812. else
  813. { while compiling a system unit, some types are directly inserted }
  814. begin
  815. st.next:=symtablestack;
  816. symtablestack:=st;
  817. insert_intern_types(st);
  818. end;
  819. { now we know the place to insert the constants }
  820. constsymtable:=symtablestack;
  821. { move the global symtab from the temporary local to global }
  822. current_module.globalsymtable:=current_module.localsymtable;
  823. current_module.localsymtable:=nil;
  824. reset_global_defs;
  825. { number all units, so we know if a unit is used by this unit or
  826. needs to be added implicitly }
  827. current_module.numberunits;
  828. { ... parse the declarations }
  829. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  830. read_interface_declarations;
  831. { leave when we got an error }
  832. if (Errorcount>0) and not status.skip_error then
  833. begin
  834. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  835. status.skip_error:=true;
  836. exit;
  837. end;
  838. {else in inteface its somatimes necessary even if unused
  839. st^.allunitsused; }
  840. {$ifdef New_GDB}
  841. write_gdb_info;
  842. {$endIf Def New_GDB}
  843. if not(cs_compilesystem in aktmoduleswitches) then
  844. if (Errorcount=0) then
  845. tppumodule(current_module).getppucrc;
  846. { Parse the implementation section }
  847. consume(_IMPLEMENTATION);
  848. current_module.in_implementation:=true;
  849. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  850. parse_only:=false;
  851. { generates static symbol table }
  852. st:=tstaticsymtable.create(current_module.modulename^);
  853. current_module.localsymtable:=st;
  854. { remove the globalsymtable from the symtable stack }
  855. { to reinsert it after loading the implementation units }
  856. symtablestack:=unitst.next;
  857. { we don't want implementation units symbols in unitsymtable !! PM }
  858. refsymtable:=st;
  859. { Read the implementation units }
  860. parse_implementation_uses(unitst);
  861. if current_module.compiled then
  862. begin
  863. RestoreUnitSyms;
  864. exit;
  865. end;
  866. { reset ranges/stabs in exported definitions }
  867. reset_global_defs;
  868. { All units are read, now give them a number }
  869. current_module.numberunits;
  870. { now we can change refsymtable }
  871. refsymtable:=st;
  872. { but reinsert the global symtable as lasts }
  873. unitst.next:=symtablestack;
  874. symtablestack:=unitst;
  875. tstoredsymtable(symtablestack).chainoperators;
  876. {$ifdef DEBUG}
  877. test_symtablestack;
  878. {$endif DEBUG}
  879. constsymtable:=symtablestack;
  880. {$ifdef Splitheap}
  881. if testsplit then
  882. begin
  883. Split_Heap;
  884. allow_special:=true;
  885. Switch_to_temp_heap;
  886. end;
  887. { it will report all crossings }
  888. allow_special:=false;
  889. {$endif Splitheap}
  890. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  891. { Compile the unit }
  892. codegen_newprocedure;
  893. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  894. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  895. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  896. compile_proc_body(true,false);
  897. codegen_doneprocedure;
  898. { avoid self recursive destructor call !! PM }
  899. aktprocdef.localst:=nil;
  900. { if the unit contains ansi/widestrings, initialization and
  901. finalization code must be forced }
  902. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  903. tstaticsymtable(current_module.localsymtable).needs_init_final;
  904. { should we force unit initialization? }
  905. { this is a hack, but how can it be done better ? }
  906. if force_init_final and ((current_module.flags and uf_init)=0) then
  907. begin
  908. current_module.flags:=current_module.flags or uf_init;
  909. { now we can insert a cut }
  910. if (cs_create_smart in aktmoduleswitches) then
  911. codeSegment.concat(Tai_cut.Create);
  912. genimplicitunitinit(codesegment);
  913. end;
  914. { finalize? }
  915. if token=_FINALIZATION then
  916. begin
  917. { set module options }
  918. current_module.flags:=current_module.flags or uf_finalize;
  919. { Compile the finalize }
  920. codegen_newprocedure;
  921. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  922. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  923. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  924. compile_proc_body(true,false);
  925. codegen_doneprocedure;
  926. end
  927. else if force_init_final then
  928. begin
  929. current_module.flags:=current_module.flags or uf_finalize;
  930. { now we can insert a cut }
  931. if (cs_create_smart in aktmoduleswitches) then
  932. codeSegment.concat(Tai_cut.Create);
  933. genimplicitunitfinal(codesegment);
  934. end;
  935. { the last char should always be a point }
  936. consume(_POINT);
  937. { generate a list of threadvars }
  938. InsertThreadvars;
  939. { Generate resoucestrings }
  940. If ResourceStrings.ResStrCount>0 then
  941. begin
  942. ResourceStrings.CreateResourceStringList;
  943. current_module.flags:=current_module.flags or uf_has_resources;
  944. { only write if no errors found }
  945. if (Errorcount=0) then
  946. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  947. end;
  948. { avoid self recursive destructor call !! PM }
  949. aktprocdef.localst:=nil;
  950. { absence does not matter here !! }
  951. aktprocdef.forwarddef:=false;
  952. { test static symtable }
  953. if (Errorcount=0) then
  954. begin
  955. tstoredsymtable(st).allsymbolsused;
  956. tstoredsymtable(st).allunitsused;
  957. tstoredsymtable(st).allprivatesused;
  958. end;
  959. { size of the static data }
  960. datasize:=st.datasize;
  961. {$ifdef GDB}
  962. { add all used definitions even for implementation}
  963. if (cs_debuginfo in aktmoduleswitches) then
  964. begin
  965. {$IfnDef New_GDB}
  966. if assigned(current_module.globalsymtable) then
  967. begin
  968. { all types }
  969. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  970. { and all local symbols}
  971. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  972. end;
  973. { all local types }
  974. tglobalsymtable(st)^.concattypestabto(debuglist);
  975. { and all local symbols}
  976. st^.concatstabto(debuglist);
  977. {$else New_GDB}
  978. write_gdb_info;
  979. {$endIf Def New_GDB}
  980. end;
  981. {$endif GDB}
  982. reset_global_defs;
  983. if (Errorcount=0) then
  984. begin
  985. { tests, if all (interface) forwards are resolved }
  986. tstoredsymtable(symtablestack).check_forwards;
  987. { check if all private fields are used }
  988. tstoredsymtable(symtablestack).allprivatesused;
  989. { remove cross unit overloads }
  990. tstoredsymtable(symtablestack).unchain_overloaded;
  991. end;
  992. current_module.in_implementation:=false;
  993. {$ifdef GDB}
  994. tglobalsymtable(symtablestack).is_stab_written:=false;
  995. {$endif GDB}
  996. { leave when we got an error }
  997. if (Errorcount>0) and not status.skip_error then
  998. begin
  999. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1000. status.skip_error:=true;
  1001. exit;
  1002. end;
  1003. { generate imports }
  1004. if current_module.uses_imports then
  1005. importlib.generatelib;
  1006. { insert own objectfile, or say that it's in a library
  1007. (no check for an .o when loading) }
  1008. if is_assembler_generated then
  1009. insertobjectfile
  1010. else
  1011. current_module.flags:=current_module.flags or uf_no_link;
  1012. if cs_local_browser in aktmoduleswitches then
  1013. current_module.localsymtable:=refsymtable;
  1014. {$ifdef GDB}
  1015. pu:=tused_unit(usedunits.first);
  1016. while assigned(pu) do
  1017. begin
  1018. if assigned(pu.u.globalsymtable) then
  1019. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  1020. pu:=tused_unit(pu.next);
  1021. end;
  1022. {$endif GDB}
  1023. if is_assembler_generated then
  1024. begin
  1025. { finish asmlist by adding segment starts }
  1026. insertsegment;
  1027. { assemble }
  1028. create_objectfile;
  1029. end;
  1030. { Write out the ppufile after the object file has been created }
  1031. store_interface_crc:=current_module.interface_crc;
  1032. store_crc:=current_module.crc;
  1033. if (Errorcount=0) then
  1034. tppumodule(current_module).writeppu;
  1035. if not(cs_compilesystem in aktmoduleswitches) then
  1036. if store_interface_crc<>current_module.interface_crc then
  1037. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1038. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  1039. {$ifdef EXTDEBUG}
  1040. if not(cs_compilesystem in aktmoduleswitches) then
  1041. if (store_crc<>current_module.crc) and simplify_ppu then
  1042. Comment(V_Note,current_module.ppufilename^+' implementation CRC changed '+
  1043. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1044. {$endif EXTDEBUG}
  1045. { remove static symtable (=refsymtable) here to save some mem }
  1046. if not (cs_local_browser in aktmoduleswitches) then
  1047. begin
  1048. st.free;
  1049. current_module.localsymtable:=nil;
  1050. end;
  1051. RestoreUnitSyms;
  1052. { leave when we got an error }
  1053. if (Errorcount>0) and not status.skip_error then
  1054. begin
  1055. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1056. status.skip_error:=true;
  1057. exit;
  1058. end;
  1059. end;
  1060. procedure proc_program(islibrary : boolean);
  1061. var
  1062. main_file: tinputfile;
  1063. st : tsymtable;
  1064. hp : tmodule;
  1065. begin
  1066. DLLsource:=islibrary;
  1067. Status.IsLibrary:=IsLibrary;
  1068. Status.IsExe:=true;
  1069. parse_only:=false;
  1070. { relocation works only without stabs under win32 !! PM }
  1071. { internal assembler uses rva for stabs info
  1072. so it should work with relocated DLLs }
  1073. if RelocSection and
  1074. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1075. (target_info.assem<>as_i386_pecoff) then
  1076. begin
  1077. include(aktglobalswitches,cs_link_strip);
  1078. { Warning stabs info does not work with reloc section !! }
  1079. if cs_debuginfo in aktmoduleswitches then
  1080. begin
  1081. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1082. Message(parser_w_parser_win32_debug_needs_WN);
  1083. exclude(aktmoduleswitches,cs_debuginfo);
  1084. end;
  1085. end;
  1086. { get correct output names }
  1087. main_file := current_scanner.inputfile;
  1088. while assigned(main_file.next) do
  1089. main_file := main_file.next;
  1090. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1091. if islibrary then
  1092. begin
  1093. consume(_LIBRARY);
  1094. stringdispose(current_module.modulename);
  1095. current_module.modulename:=stringdup(pattern);
  1096. current_module.islibrary:=true;
  1097. exportlib.preparelib(pattern);
  1098. consume(_ID);
  1099. consume(_SEMICOLON);
  1100. end
  1101. else
  1102. { is there an program head ? }
  1103. if token=_PROGRAM then
  1104. begin
  1105. consume(_PROGRAM);
  1106. stringdispose(current_module.modulename);
  1107. stringdispose(current_module.realmodulename);
  1108. current_module.modulename:=stringdup(pattern);
  1109. current_module.realmodulename:=stringdup(orgpattern);
  1110. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1111. exportlib.preparelib(pattern);
  1112. consume(_ID);
  1113. if token=_LKLAMMER then
  1114. begin
  1115. consume(_LKLAMMER);
  1116. repeat
  1117. consume(_ID);
  1118. until not try_to_consume(_COMMA);
  1119. consume(_RKLAMMER);
  1120. end;
  1121. consume(_SEMICOLON);
  1122. end
  1123. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1124. exportlib.preparelib(current_module.modulename^);
  1125. { global switches are read, so further changes aren't allowed }
  1126. current_module.in_global:=false;
  1127. { setup things using the global switches }
  1128. setupglobalswitches;
  1129. { set implementation flag }
  1130. current_module.in_implementation:=true;
  1131. { insert after the unit symbol tables the static symbol table }
  1132. { of the program }
  1133. st:=tstaticsymtable.create(current_module.modulename^);;
  1134. current_module.localsymtable:=st;
  1135. refsymtable:=st;
  1136. { load standard units (system,objpas,profile unit) }
  1137. loaddefaultunits;
  1138. { reset }
  1139. lexlevel:=0;
  1140. {Load the units used by the program we compile.}
  1141. if token=_USES then
  1142. loadunits;
  1143. tstoredsymtable(symtablestack).chainoperators;
  1144. { reset ranges/stabs in exported definitions }
  1145. reset_global_defs;
  1146. { All units are read, now give them a number }
  1147. current_module.numberunits;
  1148. {Insert the name of the main program into the symbol table.}
  1149. if current_module.realmodulename^<>'' then
  1150. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1151. { ...is also constsymtable, this is the symtable where }
  1152. { the elements of enumeration types are inserted }
  1153. constsymtable:=st;
  1154. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1155. {The program intialization needs an alias, so it can be called
  1156. from the bootstrap code.}
  1157. codegen_newprocedure;
  1158. if islibrary then
  1159. begin
  1160. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1161. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1162. { Win32 startup code needs a single name }
  1163. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1164. aktprocdef.aliasnames.insert('PASCALMAIN');
  1165. { this code is called from C so we need to save some
  1166. registers }
  1167. include(aktprocdef.procoptions,po_savestdregs);
  1168. end
  1169. else
  1170. begin
  1171. gen_main_procsym('main',potype_proginit,st);
  1172. aktprocdef.aliasnames.insert('program_init');
  1173. aktprocdef.aliasnames.insert('PASCALMAIN');
  1174. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1175. end;
  1176. compile_proc_body(true,false);
  1177. { should we force unit initialization? }
  1178. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1179. begin
  1180. current_module.flags:=current_module.flags or (uf_init or uf_finalize);
  1181. { Add initialize section }
  1182. if (cs_create_smart in aktmoduleswitches) then
  1183. codeSegment.concat(Tai_cut.Create);
  1184. genimplicitunitinit(codesegment);
  1185. { Add finalize section }
  1186. if (cs_create_smart in aktmoduleswitches) then
  1187. codeSegment.concat(Tai_cut.Create);
  1188. genimplicitunitfinal(codesegment);
  1189. end;
  1190. { Add symbol to the exports section for win32 so smartlinking a
  1191. DLL will include the edata section }
  1192. if assigned(exportlib) and
  1193. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1194. assigned(current_module._exports.first) then
  1195. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1196. { avoid self recursive destructor call !! PM }
  1197. aktprocdef.localst:=nil;
  1198. { consider these symbols as global ones for browser
  1199. but the typecasting of the globalsymtable with tglobalsymtable
  1200. can then lead to problems (PFV)
  1201. current_module.globalsymtable:=current_module.localsymtable;
  1202. current_module.localsymtable:=nil;}
  1203. If ResourceStrings.ResStrCount>0 then
  1204. begin
  1205. ResourceStrings.CreateResourceStringList;
  1206. { only write if no errors found }
  1207. if (Errorcount=0) then
  1208. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1209. end;
  1210. codegen_doneprocedure;
  1211. { finalize? }
  1212. if token=_FINALIZATION then
  1213. begin
  1214. { set module options }
  1215. current_module.flags:=current_module.flags or uf_finalize;
  1216. { Compile the finalize }
  1217. codegen_newprocedure;
  1218. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1219. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1220. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1221. compile_proc_body(true,false);
  1222. codegen_doneprocedure;
  1223. end;
  1224. { consume the last point }
  1225. consume(_POINT);
  1226. {$ifdef New_GDB}
  1227. write_gdb_info;
  1228. {$endIf Def New_GDB}
  1229. { leave when we got an error }
  1230. if (Errorcount>0) and not status.skip_error then
  1231. begin
  1232. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1233. status.skip_error:=true;
  1234. exit;
  1235. end;
  1236. { test static symtable }
  1237. if (Errorcount=0) then
  1238. begin
  1239. tstoredsymtable(st).allsymbolsused;
  1240. tstoredsymtable(st).allunitsused;
  1241. tstoredsymtable(st).allprivatesused;
  1242. end;
  1243. { generate a list of threadvars }
  1244. InsertThreadvars;
  1245. { generate imports }
  1246. if current_module.uses_imports then
  1247. importlib.generatelib;
  1248. if islibrary or
  1249. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1250. (target_info.system=system_i386_NETWARE) then
  1251. exportlib.generatelib;
  1252. { insert Tables and Heap }
  1253. insertThreadVarTablesTable;
  1254. insertResourceTablesTable;
  1255. insertinitfinaltable;
  1256. insertheap;
  1257. insertstacklength;
  1258. datasize:=symtablestack.datasize;
  1259. { finish asmlist by adding segment starts }
  1260. insertsegment;
  1261. { insert own objectfile }
  1262. insertobjectfile;
  1263. { assemble and link }
  1264. create_objectfile;
  1265. { leave when we got an error }
  1266. if (Errorcount>0) and not status.skip_error then
  1267. begin
  1268. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1269. status.skip_error:=true;
  1270. exit;
  1271. end;
  1272. { create the executable when we are at level 1 }
  1273. if (compile_level=1) then
  1274. begin
  1275. { insert all .o files from all loaded units }
  1276. hp:=tmodule(loaded_units.first);
  1277. while assigned(hp) do
  1278. begin
  1279. linker.AddModuleFiles(hp);
  1280. hp:=tmodule(hp.next);
  1281. end;
  1282. { write .def file }
  1283. if (cs_link_deffile in aktglobalswitches) then
  1284. deffile.writefile;
  1285. { finally we can create a executable }
  1286. if (not current_module.is_unit) then
  1287. begin
  1288. if DLLSource then
  1289. linker.MakeSharedLibrary
  1290. else
  1291. linker.MakeExecutable;
  1292. end;
  1293. end;
  1294. end;
  1295. end.
  1296. {
  1297. $Log$
  1298. Revision 1.87 2002-12-24 23:32:56 peter
  1299. * Use FixFilename for specified unit sourcefile in uses
  1300. Revision 1.86 2002/12/06 16:56:58 peter
  1301. * only compile cs_fp_emulation support when cpufpuemu is defined
  1302. * define cpufpuemu for m68k only
  1303. Revision 1.85 2002/11/30 21:32:24 carl
  1304. + Add loading of softfpu in emulation mode
  1305. + Correct routine call for softfpu
  1306. * Extended type must also be defined even with softfpu
  1307. Revision 1.84 2002/11/15 01:58:53 peter
  1308. * merged changes from 1.0.7 up to 04-11
  1309. - -V option for generating bug report tracing
  1310. - more tracing for option parsing
  1311. - errors for cdecl and high()
  1312. - win32 import stabs
  1313. - win32 records<=8 are returned in eax:edx (turned off by default)
  1314. - heaptrc update
  1315. - more info for temp management in .s file with EXTDEBUG
  1316. Revision 1.83 2002/11/09 15:33:26 carl
  1317. * major alignment updates
  1318. Revision 1.82 2002/10/16 06:32:52 michael
  1319. + Renamed thread unit to systhrds
  1320. Revision 1.81 2002/10/14 19:42:34 peter
  1321. * only use init tables for threadvars
  1322. Revision 1.80 2002/10/06 19:41:30 peter
  1323. * Add finalization of typed consts
  1324. * Finalization of globals in the main program
  1325. Revision 1.79 2002/09/09 17:34:15 peter
  1326. * tdicationary.replace added to replace and item in a dictionary. This
  1327. is only allowed for the same name
  1328. * varsyms are inserted in symtable before the types are parsed. This
  1329. fixes the long standing "var longint : longint" bug
  1330. - consume_idlist and idstringlist removed. The loops are inserted
  1331. at the callers place and uses the symtable for duplicate id checking
  1332. Revision 1.78 2002/09/07 15:25:07 peter
  1333. * old logs removed and tabs fixed
  1334. Revision 1.77 2002/09/03 16:26:27 daniel
  1335. * Make Tprocdef.defs protected
  1336. Revision 1.76 2002/09/02 18:46:26 peter
  1337. * insert PASCALMAIN in library for Win32 only
  1338. Revision 1.75 2002/08/31 15:59:30 florian
  1339. + HEAP* stuff must be generated for Linux/PPC as well
  1340. + direct assembler reader searches now global and static symtables as well
  1341. Revision 1.74 2002/08/25 19:25:20 peter
  1342. * sym.insert_in_data removed
  1343. * symtable.insertvardata/insertconstdata added
  1344. * removed insert_in_data call from symtable.insert, it needs to be
  1345. called separatly. This allows to deref the address calculation
  1346. * procedures now calculate the parast addresses after the procedure
  1347. directives are parsed. This fixes the cdecl parast problem
  1348. * push_addr_param has an extra argument that specifies if cdecl is used
  1349. or not
  1350. Revision 1.73 2002/08/18 20:06:25 peter
  1351. * inlining is now also allowed in interface
  1352. * renamed write/load to ppuwrite/ppuload
  1353. * tnode storing in ppu
  1354. * nld,ncon,nbas are already updated for storing in ppu
  1355. Revision 1.72 2002/08/17 09:23:39 florian
  1356. * first part of procinfo rewrite
  1357. Revision 1.71 2002/08/11 13:24:12 peter
  1358. * saving of asmsymbols in ppu supported
  1359. * asmsymbollist global is removed and moved into a new class
  1360. tasmlibrarydata that will hold the info of a .a file which
  1361. corresponds with a single module. Added librarydata to tmodule
  1362. to keep the library info stored for the module. In the future the
  1363. objectfiles will also be stored to the tasmlibrarydata class
  1364. * all getlabel/newasmsymbol and friends are moved to the new class
  1365. Revision 1.70 2002/08/10 14:46:29 carl
  1366. + moved target_cpu_string to cpuinfo
  1367. * renamed asmmode enum.
  1368. * assembler reader has now less ifdef's
  1369. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1370. Revision 1.69 2002/07/26 21:15:41 florian
  1371. * rewrote the system handling
  1372. Revision 1.68 2002/07/04 20:43:01 florian
  1373. * first x86-64 patches
  1374. Revision 1.67 2002/07/01 18:46:25 peter
  1375. * internal linker
  1376. * reorganized aasm layer
  1377. Revision 1.66 2002/05/16 19:46:43 carl
  1378. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1379. + try to fix temp allocation (still in ifdef)
  1380. + generic constructor calls
  1381. + start of tassembler / tmodulebase class cleanup
  1382. Revision 1.65 2002/05/14 19:34:49 peter
  1383. * removed old logs and updated copyright year
  1384. Revision 1.64 2002/05/12 16:53:09 peter
  1385. * moved entry and exitcode to ncgutil and cgobj
  1386. * foreach gets extra argument for passing local data to the
  1387. iterator function
  1388. * -CR checks also class typecasts at runtime by changing them
  1389. into as
  1390. * fixed compiler to cycle with the -CR option
  1391. * fixed stabs with elf writer, finally the global variables can
  1392. be watched
  1393. * removed a lot of routines from cga unit and replaced them by
  1394. calls to cgobj
  1395. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1396. u32bit then the other is typecasted also to u32bit without giving
  1397. a rangecheck warning/error.
  1398. * fixed pascal calling method with reversing also the high tree in
  1399. the parast, detected by tcalcst3 test
  1400. Revision 1.63 2002/05/06 19:54:50 carl
  1401. + added more patches from Mazen for SPARC port
  1402. Revision 1.62 2002/04/20 21:32:24 carl
  1403. + generic FPC_CHECKPOINTER
  1404. + first parameter offset in stack now portable
  1405. * rename some constants
  1406. + move some cpu stuff to other units
  1407. - remove unused constents
  1408. * fix stacksize for some targets
  1409. * fix generic size problems which depend now on EXTEND_SIZE constant
  1410. Revision 1.61 2002/04/19 15:46:02 peter
  1411. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1412. in most cases and not written to the ppu
  1413. * add mangeledname_prefix() routine to generate the prefix of
  1414. manglednames depending on the current procedure, object and module
  1415. * removed static procprefix since the mangledname is now build only
  1416. on demand from tprocdef.mangledname
  1417. Revision 1.60 2002/04/14 16:53:10 carl
  1418. + align code section and data section according to alignment rules
  1419. Revision 1.59 2002/04/07 17:58:38 carl
  1420. + generic stack checking
  1421. Revision 1.58 2002/04/04 19:06:03 peter
  1422. * removed unused units
  1423. * use tlocation.size in cg.a_*loc*() routines
  1424. }