pmodules.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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. procedure AddUnit(const s:string);
  378. var
  379. hp : tppumodule;
  380. unitsym : tunitsym;
  381. begin
  382. { load unit }
  383. hp:=registerunit(current_module,s,'');
  384. hp.loadppu;
  385. hp.adddependency(current_module);
  386. current_module.addusedunit(hp,false);
  387. { add to symtable stack }
  388. tsymtable(hp.globalsymtable).next:=symtablestack;
  389. symtablestack:=hp.globalsymtable;
  390. { insert unitsym }
  391. unitsym:=tunitsym.create(s,hp.globalsymtable);
  392. inc(unitsym.refs);
  393. refsymtable.insert(unitsym);
  394. end;
  395. begin
  396. { are we compiling the system unit? }
  397. if (cs_compilesystem in aktmoduleswitches) then
  398. begin
  399. { create system defines }
  400. createconstdefs;
  401. { we don't need to reset anything, it's already done in parser.pas }
  402. exit;
  403. end;
  404. { insert the system unit, it is allways the first }
  405. Symtablestack:=nil;
  406. AddUnit('System');
  407. SystemUnit:=TGlobalSymtable(Symtablestack);
  408. { read default constant definitions }
  409. make_ref:=false;
  410. readconstdefs;
  411. make_ref:=true;
  412. {$ifdef cpufpemu}
  413. { Floating point emulation unit? }
  414. if (cs_fp_emulation in aktmoduleswitches) then
  415. AddUnit('SoftFpu');
  416. {$endif cpufpemu}
  417. { Thread support unit? }
  418. if (cs_threading in aktmoduleswitches) then
  419. AddUnit('SysThrds');
  420. { Objpas unit? }
  421. if m_objpas in aktmodeswitches then
  422. AddUnit('ObjPas');
  423. { Profile unit? Needed for go32v2 only }
  424. if (cs_profile in aktmoduleswitches) and
  425. (target_info.system=system_i386_go32v2) then
  426. AddUnit('Profile');
  427. { Units only required for main module }
  428. if not(current_module.is_unit) then
  429. begin
  430. { Heaptrc unit }
  431. if (cs_gdb_heaptrc in aktglobalswitches) then
  432. AddUnit('HeapTrc');
  433. { Lineinfo unit }
  434. if (cs_gdb_lineinfo in aktglobalswitches) then
  435. AddUnit('LineInfo');
  436. end;
  437. { save default symtablestack }
  438. defaultsymtablestack:=symtablestack;
  439. end;
  440. procedure loadunits;
  441. var
  442. s,sorg : stringid;
  443. fn : string;
  444. pu : tused_unit;
  445. hp2 : tmodule;
  446. hp3 : tsymtable;
  447. oldprocsym:tprocsym;
  448. oldprocdef:tprocdef;
  449. unitsym : tunitsym;
  450. begin
  451. oldprocsym:=aktprocsym;
  452. oldprocdef:=aktprocdef;
  453. consume(_USES);
  454. {$ifdef DEBUG}
  455. test_symtablestack;
  456. {$endif DEBUG}
  457. repeat
  458. s:=pattern;
  459. sorg:=orgpattern;
  460. consume(_ID);
  461. { support "<unit> in '<file>'" construct, but not for tp7 }
  462. if not(m_tp7 in aktmodeswitches) then
  463. begin
  464. if try_to_consume(_OP_IN) then
  465. fn:=FixFileName(get_stringconst)
  466. else
  467. fn:='';
  468. end;
  469. { Give a warning if objpas is loaded }
  470. if s='OBJPAS' then
  471. Message(parser_w_no_objpas_use_mode);
  472. { check if the unit is already used }
  473. pu:=tused_unit(current_module.used_units.first);
  474. while assigned(pu) do
  475. begin
  476. if (pu.u.modulename^=s) then
  477. break;
  478. pu:=tused_unit(pu.next);
  479. end;
  480. { avoid uses of itself }
  481. if not assigned(pu) and (s<>current_module.modulename^) then
  482. begin
  483. { register the unit }
  484. hp2:=registerunit(current_module,sorg,fn);
  485. { the current module uses the unit hp2 }
  486. current_module.addusedunit(hp2,true);
  487. end
  488. else
  489. Message1(sym_e_duplicate_id,s);
  490. if token=_COMMA then
  491. begin
  492. pattern:='';
  493. consume(_COMMA);
  494. end
  495. else
  496. break;
  497. until false;
  498. consume(_SEMICOLON);
  499. { Load the units }
  500. pu:=tused_unit(current_module.used_units.first);
  501. while assigned(pu) do
  502. begin
  503. if pu.in_uses then
  504. begin
  505. tppumodule(pu.u).loadppu;
  506. { is our module compiled? then we can stop }
  507. if current_module.state=ms_compiled then
  508. exit;
  509. { add this unit to the dependencies }
  510. pu.u.adddependency(current_module);
  511. pu.checksum:=pu.u.crc;
  512. pu.interface_checksum:=pu.u.interface_crc;
  513. end;
  514. pu:=tused_unit(pu.next);
  515. end;
  516. { set the symtable to systemunit so it gets reorderd correctly,
  517. then insert the units in the symtablestack }
  518. pu:=tused_unit(current_module.used_units.first);
  519. symtablestack:=defaultsymtablestack;
  520. while assigned(pu) do
  521. begin
  522. {$IfDef GDB}
  523. if (cs_debuginfo in aktmoduleswitches) and
  524. (cs_gdb_dbx in aktglobalswitches) and
  525. not pu.is_stab_written then
  526. begin
  527. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  528. pu.is_stab_written:=true;
  529. pu.unitid:=tsymtable(pu.u.globalsymtable).unitid;
  530. end;
  531. {$EndIf GDB}
  532. if pu.in_uses then
  533. begin
  534. { Create unitsym }
  535. unitsym:=tunitsym.create(pu.u.realmodulename^,pu.u.globalsymtable);
  536. { never claim about unused unit if
  537. there is init or finalize code PM }
  538. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  539. inc(unitsym.refs);
  540. refsymtable.insert(unitsym);
  541. { Reinsert in symtablestack }
  542. hp3:=symtablestack;
  543. while assigned(hp3) do
  544. begin
  545. { insert units only once ! }
  546. if pu.u.globalsymtable=hp3 then
  547. break;
  548. hp3:=hp3.next;
  549. { unit isn't inserted }
  550. if hp3=nil then
  551. begin
  552. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  553. symtablestack:=tsymtable(pu.u.globalsymtable);
  554. {$ifdef DEBUG}
  555. test_symtablestack;
  556. {$endif DEBUG}
  557. end;
  558. end;
  559. end;
  560. pu:=tused_unit(pu.next);
  561. end;
  562. aktprocsym:=oldprocsym;
  563. aktprocdef:=oldprocdef;
  564. end;
  565. procedure write_gdb_info;
  566. {$IfDef GDB}
  567. var
  568. hp : tused_unit;
  569. begin
  570. if not (cs_debuginfo in aktmoduleswitches) then
  571. exit;
  572. { now insert the units in the symtablestack }
  573. hp:=tused_unit(current_module.used_units.first);
  574. while assigned(hp) do
  575. begin
  576. if (cs_debuginfo in aktmoduleswitches) and
  577. not hp.is_stab_written then
  578. begin
  579. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  580. hp.is_stab_written:=true;
  581. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  582. end;
  583. hp:=tused_unit(hp.next);
  584. end;
  585. if (not current_module.in_interface) and
  586. assigned(current_module.localsymtable) then
  587. begin
  588. { all types }
  589. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  590. { and all local symbols}
  591. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  592. end
  593. else if assigned(current_module.globalsymtable) then
  594. begin
  595. { all types }
  596. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  597. { and all local symbols}
  598. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  599. end;
  600. if (cs_gdb_dbx in aktglobalswitches) then
  601. begin
  602. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  603. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  604. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  605. debugList.concat(Tai_stabs.Create(strpnew('"'+
  606. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  607. tostr(N_EINCL)+',0,0,0')));
  608. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  609. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  610. do_count_dbx:=false;
  611. end;
  612. end;
  613. {$Else GDB}
  614. begin
  615. end;
  616. {$EndIf GDB}
  617. procedure parse_implementation_uses(symt:tsymtable);
  618. begin
  619. if token=_USES then
  620. begin
  621. loadunits;
  622. {$ifdef DEBUG}
  623. test_symtablestack;
  624. {$endif DEBUG}
  625. end;
  626. end;
  627. procedure setupglobalswitches;
  628. begin
  629. { can't have local browser when no global browser }
  630. if (cs_local_browser in aktmoduleswitches) and
  631. not(cs_browser in aktmoduleswitches) then
  632. exclude(aktmoduleswitches,cs_local_browser);
  633. { define a symbol in delphi,objfpc,tp,gpc mode }
  634. if (m_delphi in aktmodeswitches) then
  635. current_scanner.def_macro('FPC_DELPHI')
  636. else
  637. if (m_tp7 in aktmodeswitches) then
  638. current_scanner.def_macro('FPC_TP')
  639. else
  640. if (m_objfpc in aktmodeswitches) then
  641. current_scanner.def_macro('FPC_OBJFPC')
  642. else
  643. if (m_gpc in aktmodeswitches) then
  644. current_scanner.def_macro('FPC_GPC');
  645. end;
  646. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  647. var
  648. stt : tsymtable;
  649. begin
  650. {Generate a procsym for main}
  651. make_ref:=false;
  652. aktprocsym:=tprocsym.create('$'+name);
  653. { main are allways used }
  654. inc(aktprocsym.refs);
  655. {Try to insert in in static symtable ! }
  656. stt:=symtablestack;
  657. symtablestack:=st;
  658. aktprocdef:=tprocdef.create;
  659. aktprocsym.addprocdef(aktprocdef);
  660. aktprocdef.procsym:=aktprocsym;
  661. symtablestack:=stt;
  662. aktprocdef.proctypeoption:=options;
  663. aktprocdef.setmangledname(target_info.cprefix+name);
  664. aktprocdef.forwarddef:=false;
  665. make_ref:=true;
  666. { The localst is a local symtable. Change it into the static
  667. symtable }
  668. aktprocdef.localst.free;
  669. aktprocdef.localst:=st;
  670. { and insert the procsym in symtable }
  671. st.insert(aktprocsym);
  672. { set some informations about the main program }
  673. with procinfo do
  674. begin
  675. _class:=nil;
  676. para_offset:=target_info.first_parm_offset;
  677. framepointer:=FRAME_POINTER_REG;
  678. flags:=0;
  679. procdef:=aktprocdef;
  680. end;
  681. end;
  682. procedure proc_unit;
  683. function is_assembler_generated:boolean;
  684. begin
  685. is_assembler_generated:=(Errorcount=0) and
  686. not(
  687. codeSegment.empty and
  688. dataSegment.empty and
  689. bssSegment.empty and
  690. ((importssection=nil) or importsSection.empty) and
  691. ((resourcesection=nil) or resourceSection.empty) and
  692. ((resourcestringlist=nil) or resourcestringList.empty)
  693. );
  694. end;
  695. var
  696. main_file: tinputfile;
  697. st : tsymtable;
  698. unitst : tglobalsymtable;
  699. {$ifdef GDB}
  700. pu : tused_unit;
  701. {$endif GDB}
  702. store_crc,store_interface_crc : cardinal;
  703. s2 : ^string; {Saves stack space}
  704. force_init_final : boolean;
  705. begin
  706. consume(_UNIT);
  707. if compile_level=1 then
  708. Status.IsExe:=false;
  709. if token=_ID then
  710. begin
  711. { create filenames and unit name }
  712. main_file := current_scanner.inputfile;
  713. while assigned(main_file.next) do
  714. main_file := main_file.next;
  715. current_module.SetFileName(main_file.path^+main_file.name^,true);
  716. stringdispose(current_module.modulename);
  717. stringdispose(current_module.realmodulename);
  718. current_module.modulename:=stringdup(pattern);
  719. current_module.realmodulename:=stringdup(orgpattern);
  720. { check for system unit }
  721. new(s2);
  722. s2^:=upper(SplitName(main_file.name^));
  723. if (cs_check_unit_name in aktglobalswitches) and
  724. not((current_module.modulename^=s2^) or
  725. ((length(current_module.modulename^)>8) and
  726. (copy(current_module.modulename^,1,8)=s2^))) then
  727. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  728. if (current_module.modulename^='SYSTEM') then
  729. include(aktmoduleswitches,cs_compilesystem);
  730. dispose(s2);
  731. end;
  732. consume(_ID);
  733. consume(_SEMICOLON);
  734. consume(_INTERFACE);
  735. { global switches are read, so further changes aren't allowed }
  736. current_module.in_global:=false;
  737. { handle the global switches }
  738. setupglobalswitches;
  739. Comment(V_Used,'Loading interface units from '+current_module.modulename^);
  740. // Message1(unit_u_start_parse_interface,current_module.modulename^);
  741. { update status }
  742. status.currentmodule:=current_module.realmodulename^;
  743. { maybe turn off m_objpas if we are compiling objpas }
  744. if (current_module.modulename^='OBJPAS') then
  745. exclude(aktmodeswitches,m_objpas);
  746. parse_only:=true;
  747. { generate now the global symboltable }
  748. st:=tglobalsymtable.create(current_module.modulename^);
  749. refsymtable:=st;
  750. unitst:=tglobalsymtable(st);
  751. { define first as local to overcome dependency conflicts }
  752. current_module.localsymtable:=st;
  753. { the unit name must be usable as a unit specifier }
  754. { inside the unit itself (PM) }
  755. { this also forbids to have another symbol }
  756. { with the same name as the unit }
  757. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  758. { load default units, like the system unit }
  759. loaddefaultunits;
  760. { reset }
  761. make_ref:=true;
  762. lexlevel:=0;
  763. { insert qualifier for the system unit (allows system.writeln) }
  764. if not(cs_compilesystem in aktmoduleswitches) then
  765. begin
  766. if token=_USES then
  767. begin
  768. loadunits;
  769. { has it been compiled at a higher level ?}
  770. if current_module.state=ms_compiled then
  771. begin
  772. { this unit symtable is obsolete }
  773. { dispose(unitst,done);
  774. disposed as localsymtable !! }
  775. RestoreUnitSyms;
  776. exit;
  777. end;
  778. end;
  779. { ... but insert the symbol table later }
  780. st.next:=symtablestack;
  781. symtablestack:=st;
  782. end
  783. else
  784. { while compiling a system unit, some types are directly inserted }
  785. begin
  786. st.next:=symtablestack;
  787. symtablestack:=st;
  788. insert_intern_types(st);
  789. end;
  790. { now we know the place to insert the constants }
  791. constsymtable:=symtablestack;
  792. { move the global symtab from the temporary local to global }
  793. current_module.globalsymtable:=current_module.localsymtable;
  794. current_module.localsymtable:=nil;
  795. reset_global_defs;
  796. { number all units, so we know if a unit is used by this unit or
  797. needs to be added implicitly }
  798. current_module.numberunits;
  799. { ... parse the declarations }
  800. Comment(V_Used,'Parsing interface of '+current_module.modulename^);
  801. // Message1(parser_u_parsing_interface,current_module.realmodulename^);
  802. read_interface_declarations;
  803. { leave when we got an error }
  804. if (Errorcount>0) and not status.skip_error then
  805. begin
  806. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  807. status.skip_error:=true;
  808. exit;
  809. end;
  810. {else in inteface its somatimes necessary even if unused
  811. st^.allunitsused; }
  812. {$ifdef New_GDB}
  813. write_gdb_info;
  814. {$endIf Def New_GDB}
  815. if not(cs_compilesystem in aktmoduleswitches) then
  816. if (Errorcount=0) then
  817. tppumodule(current_module).getppucrc;
  818. { Parse the implementation section }
  819. consume(_IMPLEMENTATION);
  820. current_module.in_interface:=false;
  821. Comment(V_Used,'Loading implementation units from '+current_module.modulename^);
  822. // Message1(unit_u_start_parse_implementation,current_module.modulename^);
  823. parse_only:=false;
  824. { generates static symbol table }
  825. st:=tstaticsymtable.create(current_module.modulename^);
  826. current_module.localsymtable:=st;
  827. { remove the globalsymtable from the symtable stack }
  828. { to reinsert it after loading the implementation units }
  829. symtablestack:=unitst.next;
  830. { we don't want implementation units symbols in unitsymtable !! PM }
  831. refsymtable:=st;
  832. { Read the implementation units }
  833. parse_implementation_uses(unitst);
  834. if current_module.state=ms_compiled then
  835. begin
  836. RestoreUnitSyms;
  837. exit;
  838. end;
  839. { reset ranges/stabs in exported definitions }
  840. reset_global_defs;
  841. { All units are read, now give them a number }
  842. current_module.numberunits;
  843. { now we can change refsymtable }
  844. refsymtable:=st;
  845. { but reinsert the global symtable as lasts }
  846. unitst.next:=symtablestack;
  847. symtablestack:=unitst;
  848. tstoredsymtable(symtablestack).chainoperators;
  849. {$ifdef DEBUG}
  850. test_symtablestack;
  851. {$endif DEBUG}
  852. constsymtable:=symtablestack;
  853. {$ifdef Splitheap}
  854. if testsplit then
  855. begin
  856. Split_Heap;
  857. allow_special:=true;
  858. Switch_to_temp_heap;
  859. end;
  860. { it will report all crossings }
  861. allow_special:=false;
  862. {$endif Splitheap}
  863. Comment(V_Used,'Parsing implementation of '+current_module.modulename^);
  864. if current_module.in_interface then
  865. internalerror(200212285);
  866. // Message1(parser_u_parsing_implementation,current_module.modulename^);
  867. { Compile the unit }
  868. codegen_newprocedure;
  869. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  870. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  871. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  872. compile_proc_body(true,false);
  873. codegen_doneprocedure;
  874. { avoid self recursive destructor call !! PM }
  875. aktprocdef.localst:=nil;
  876. { if the unit contains ansi/widestrings, initialization and
  877. finalization code must be forced }
  878. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  879. tstaticsymtable(current_module.localsymtable).needs_init_final;
  880. { should we force unit initialization? }
  881. { this is a hack, but how can it be done better ? }
  882. if force_init_final and ((current_module.flags and uf_init)=0) then
  883. begin
  884. current_module.flags:=current_module.flags or uf_init;
  885. { now we can insert a cut }
  886. if (cs_create_smart in aktmoduleswitches) then
  887. codeSegment.concat(Tai_cut.Create);
  888. genimplicitunitinit(codesegment);
  889. end;
  890. { finalize? }
  891. if token=_FINALIZATION then
  892. begin
  893. { set module options }
  894. current_module.flags:=current_module.flags or uf_finalize;
  895. { Compile the finalize }
  896. codegen_newprocedure;
  897. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  898. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  899. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  900. compile_proc_body(true,false);
  901. codegen_doneprocedure;
  902. end
  903. else if force_init_final then
  904. begin
  905. current_module.flags:=current_module.flags or uf_finalize;
  906. { now we can insert a cut }
  907. if (cs_create_smart in aktmoduleswitches) then
  908. codeSegment.concat(Tai_cut.Create);
  909. genimplicitunitfinal(codesegment);
  910. end;
  911. { the last char should always be a point }
  912. consume(_POINT);
  913. { generate a list of threadvars }
  914. InsertThreadvars;
  915. { Generate resoucestrings }
  916. If ResourceStrings.ResStrCount>0 then
  917. begin
  918. ResourceStrings.CreateResourceStringList;
  919. current_module.flags:=current_module.flags or uf_has_resources;
  920. { only write if no errors found }
  921. if (Errorcount=0) then
  922. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  923. end;
  924. { avoid self recursive destructor call !! PM }
  925. aktprocdef.localst:=nil;
  926. { absence does not matter here !! }
  927. aktprocdef.forwarddef:=false;
  928. { test static symtable }
  929. if (Errorcount=0) then
  930. begin
  931. tstoredsymtable(st).allsymbolsused;
  932. tstoredsymtable(st).allunitsused;
  933. tstoredsymtable(st).allprivatesused;
  934. end;
  935. { size of the static data }
  936. datasize:=st.datasize;
  937. {$ifdef GDB}
  938. { add all used definitions even for implementation}
  939. if (cs_debuginfo in aktmoduleswitches) then
  940. begin
  941. {$IfnDef New_GDB}
  942. if assigned(current_module.globalsymtable) then
  943. begin
  944. { all types }
  945. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  946. { and all local symbols}
  947. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  948. end;
  949. { all local types }
  950. tglobalsymtable(st)^.concattypestabto(debuglist);
  951. { and all local symbols}
  952. st^.concatstabto(debuglist);
  953. {$else New_GDB}
  954. write_gdb_info;
  955. {$endIf Def New_GDB}
  956. end;
  957. {$endif GDB}
  958. reset_global_defs;
  959. if (Errorcount=0) then
  960. begin
  961. { tests, if all (interface) forwards are resolved }
  962. tstoredsymtable(symtablestack).check_forwards;
  963. { check if all private fields are used }
  964. tstoredsymtable(symtablestack).allprivatesused;
  965. { remove cross unit overloads }
  966. tstoredsymtable(symtablestack).unchain_overloaded;
  967. end;
  968. {$ifdef GDB}
  969. tglobalsymtable(symtablestack).is_stab_written:=false;
  970. {$endif GDB}
  971. { leave when we got an error }
  972. if (Errorcount>0) and not status.skip_error then
  973. begin
  974. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  975. status.skip_error:=true;
  976. exit;
  977. end;
  978. { generate imports }
  979. if current_module.uses_imports then
  980. importlib.generatelib;
  981. { insert own objectfile, or say that it's in a library
  982. (no check for an .o when loading) }
  983. if is_assembler_generated then
  984. insertobjectfile
  985. else
  986. current_module.flags:=current_module.flags or uf_no_link;
  987. if cs_local_browser in aktmoduleswitches then
  988. current_module.localsymtable:=refsymtable;
  989. {$ifdef GDB}
  990. pu:=tused_unit(usedunits.first);
  991. while assigned(pu) do
  992. begin
  993. if assigned(pu.u.globalsymtable) then
  994. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  995. pu:=tused_unit(pu.next);
  996. end;
  997. {$endif GDB}
  998. if is_assembler_generated then
  999. begin
  1000. { finish asmlist by adding segment starts }
  1001. insertsegment;
  1002. { assemble }
  1003. create_objectfile;
  1004. end;
  1005. { Write out the ppufile after the object file has been created }
  1006. store_interface_crc:=current_module.interface_crc;
  1007. store_crc:=current_module.crc;
  1008. if (Errorcount=0) then
  1009. tppumodule(current_module).writeppu;
  1010. if not(cs_compilesystem in aktmoduleswitches) then
  1011. if store_interface_crc<>current_module.interface_crc then
  1012. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1013. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  1014. {$ifdef EXTDEBUG}
  1015. if not(cs_compilesystem in aktmoduleswitches) then
  1016. if (store_crc<>current_module.crc) and simplify_ppu then
  1017. Comment(V_Note,current_module.ppufilename^+' implementation CRC changed '+
  1018. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1019. {$endif EXTDEBUG}
  1020. { remove static symtable (=refsymtable) here to save some mem }
  1021. if not (cs_local_browser in aktmoduleswitches) then
  1022. begin
  1023. st.free;
  1024. current_module.localsymtable:=nil;
  1025. end;
  1026. RestoreUnitSyms;
  1027. { leave when we got an error }
  1028. if (Errorcount>0) and not status.skip_error then
  1029. begin
  1030. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1031. status.skip_error:=true;
  1032. exit;
  1033. end;
  1034. Comment(V_Used,'Finished compiling module '+current_module.modulename^);
  1035. end;
  1036. procedure proc_program(islibrary : boolean);
  1037. var
  1038. main_file: tinputfile;
  1039. st : tsymtable;
  1040. hp : tmodule;
  1041. begin
  1042. DLLsource:=islibrary;
  1043. Status.IsLibrary:=IsLibrary;
  1044. Status.IsExe:=true;
  1045. parse_only:=false;
  1046. { relocation works only without stabs under win32 !! PM }
  1047. { internal assembler uses rva for stabs info
  1048. so it should work with relocated DLLs }
  1049. if RelocSection and
  1050. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1051. (target_info.assem<>as_i386_pecoff) then
  1052. begin
  1053. include(aktglobalswitches,cs_link_strip);
  1054. { Warning stabs info does not work with reloc section !! }
  1055. if cs_debuginfo in aktmoduleswitches then
  1056. begin
  1057. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1058. Message(parser_w_parser_win32_debug_needs_WN);
  1059. exclude(aktmoduleswitches,cs_debuginfo);
  1060. end;
  1061. end;
  1062. { get correct output names }
  1063. main_file := current_scanner.inputfile;
  1064. while assigned(main_file.next) do
  1065. main_file := main_file.next;
  1066. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1067. if islibrary then
  1068. begin
  1069. consume(_LIBRARY);
  1070. stringdispose(current_module.modulename);
  1071. current_module.modulename:=stringdup(pattern);
  1072. current_module.islibrary:=true;
  1073. exportlib.preparelib(pattern);
  1074. consume(_ID);
  1075. consume(_SEMICOLON);
  1076. end
  1077. else
  1078. { is there an program head ? }
  1079. if token=_PROGRAM then
  1080. begin
  1081. consume(_PROGRAM);
  1082. stringdispose(current_module.modulename);
  1083. stringdispose(current_module.realmodulename);
  1084. current_module.modulename:=stringdup(pattern);
  1085. current_module.realmodulename:=stringdup(orgpattern);
  1086. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1087. exportlib.preparelib(pattern);
  1088. consume(_ID);
  1089. if token=_LKLAMMER then
  1090. begin
  1091. consume(_LKLAMMER);
  1092. repeat
  1093. consume(_ID);
  1094. until not try_to_consume(_COMMA);
  1095. consume(_RKLAMMER);
  1096. end;
  1097. consume(_SEMICOLON);
  1098. end
  1099. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1100. exportlib.preparelib(current_module.modulename^);
  1101. { global switches are read, so further changes aren't allowed }
  1102. current_module.in_global:=false;
  1103. { setup things using the global switches }
  1104. setupglobalswitches;
  1105. { set implementation flag }
  1106. current_module.in_interface:=false;
  1107. { insert after the unit symbol tables the static symbol table }
  1108. { of the program }
  1109. st:=tstaticsymtable.create(current_module.modulename^);;
  1110. current_module.localsymtable:=st;
  1111. refsymtable:=st;
  1112. { load standard units (system,objpas,profile unit) }
  1113. loaddefaultunits;
  1114. { reset }
  1115. lexlevel:=0;
  1116. {Load the units used by the program we compile.}
  1117. if token=_USES then
  1118. loadunits;
  1119. tstoredsymtable(symtablestack).chainoperators;
  1120. { reset ranges/stabs in exported definitions }
  1121. reset_global_defs;
  1122. { All units are read, now give them a number }
  1123. current_module.numberunits;
  1124. {Insert the name of the main program into the symbol table.}
  1125. if current_module.realmodulename^<>'' then
  1126. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1127. { ...is also constsymtable, this is the symtable where }
  1128. { the elements of enumeration types are inserted }
  1129. constsymtable:=st;
  1130. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1131. {The program intialization needs an alias, so it can be called
  1132. from the bootstrap code.}
  1133. codegen_newprocedure;
  1134. if islibrary then
  1135. begin
  1136. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1137. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1138. { Win32 startup code needs a single name }
  1139. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1140. aktprocdef.aliasnames.insert('PASCALMAIN');
  1141. { this code is called from C so we need to save some
  1142. registers }
  1143. include(aktprocdef.procoptions,po_savestdregs);
  1144. end
  1145. else
  1146. begin
  1147. gen_main_procsym('main',potype_proginit,st);
  1148. aktprocdef.aliasnames.insert('program_init');
  1149. aktprocdef.aliasnames.insert('PASCALMAIN');
  1150. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1151. end;
  1152. compile_proc_body(true,false);
  1153. { should we force unit initialization? }
  1154. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1155. begin
  1156. current_module.flags:=current_module.flags or (uf_init or uf_finalize);
  1157. { Add initialize section }
  1158. if (cs_create_smart in aktmoduleswitches) then
  1159. codeSegment.concat(Tai_cut.Create);
  1160. genimplicitunitinit(codesegment);
  1161. { Add finalize section }
  1162. if (cs_create_smart in aktmoduleswitches) then
  1163. codeSegment.concat(Tai_cut.Create);
  1164. genimplicitunitfinal(codesegment);
  1165. end;
  1166. { Add symbol to the exports section for win32 so smartlinking a
  1167. DLL will include the edata section }
  1168. if assigned(exportlib) and
  1169. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1170. assigned(current_module._exports.first) then
  1171. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1172. { avoid self recursive destructor call !! PM }
  1173. aktprocdef.localst:=nil;
  1174. { consider these symbols as global ones for browser
  1175. but the typecasting of the globalsymtable with tglobalsymtable
  1176. can then lead to problems (PFV)
  1177. current_module.globalsymtable:=current_module.localsymtable;
  1178. current_module.localsymtable:=nil;}
  1179. If ResourceStrings.ResStrCount>0 then
  1180. begin
  1181. ResourceStrings.CreateResourceStringList;
  1182. { only write if no errors found }
  1183. if (Errorcount=0) then
  1184. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1185. end;
  1186. codegen_doneprocedure;
  1187. { finalize? }
  1188. if token=_FINALIZATION then
  1189. begin
  1190. { set module options }
  1191. current_module.flags:=current_module.flags or uf_finalize;
  1192. { Compile the finalize }
  1193. codegen_newprocedure;
  1194. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1195. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1196. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1197. compile_proc_body(true,false);
  1198. codegen_doneprocedure;
  1199. end;
  1200. { consume the last point }
  1201. consume(_POINT);
  1202. {$ifdef New_GDB}
  1203. write_gdb_info;
  1204. {$endIf Def New_GDB}
  1205. { leave when we got an error }
  1206. if (Errorcount>0) and not status.skip_error then
  1207. begin
  1208. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1209. status.skip_error:=true;
  1210. exit;
  1211. end;
  1212. { test static symtable }
  1213. if (Errorcount=0) then
  1214. begin
  1215. tstoredsymtable(st).allsymbolsused;
  1216. tstoredsymtable(st).allunitsused;
  1217. tstoredsymtable(st).allprivatesused;
  1218. end;
  1219. { generate a list of threadvars }
  1220. InsertThreadvars;
  1221. { generate imports }
  1222. if current_module.uses_imports then
  1223. importlib.generatelib;
  1224. if islibrary or
  1225. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1226. (target_info.system=system_i386_NETWARE) then
  1227. exportlib.generatelib;
  1228. { insert Tables and Heap }
  1229. insertThreadVarTablesTable;
  1230. insertResourceTablesTable;
  1231. insertinitfinaltable;
  1232. insertheap;
  1233. insertstacklength;
  1234. datasize:=symtablestack.datasize;
  1235. { finish asmlist by adding segment starts }
  1236. insertsegment;
  1237. { insert own objectfile }
  1238. insertobjectfile;
  1239. { assemble and link }
  1240. create_objectfile;
  1241. { leave when we got an error }
  1242. if (Errorcount>0) and not status.skip_error then
  1243. begin
  1244. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1245. status.skip_error:=true;
  1246. exit;
  1247. end;
  1248. { create the executable when we are at level 1 }
  1249. if (compile_level=1) then
  1250. begin
  1251. { insert all .o files from all loaded units }
  1252. hp:=tmodule(loaded_units.first);
  1253. while assigned(hp) do
  1254. begin
  1255. linker.AddModuleFiles(hp);
  1256. hp:=tmodule(hp.next);
  1257. end;
  1258. { write .def file }
  1259. if (cs_link_deffile in aktglobalswitches) then
  1260. deffile.writefile;
  1261. { finally we can create a executable }
  1262. if (not current_module.is_unit) then
  1263. begin
  1264. if DLLSource then
  1265. linker.MakeSharedLibrary
  1266. else
  1267. linker.MakeExecutable;
  1268. end;
  1269. end;
  1270. end;
  1271. end.
  1272. {
  1273. $Log$
  1274. Revision 1.89 2002-12-29 14:57:50 peter
  1275. * unit loading changed to first register units and load them
  1276. afterwards. This is needed to support uses xxx in yyy correctly
  1277. * unit dependency check fixed
  1278. Revision 1.87 2002/12/24 23:32:56 peter
  1279. * Use FixFilename for specified unit sourcefile in uses
  1280. Revision 1.86 2002/12/06 16:56:58 peter
  1281. * only compile cs_fp_emulation support when cpufpuemu is defined
  1282. * define cpufpuemu for m68k only
  1283. Revision 1.85 2002/11/30 21:32:24 carl
  1284. + Add loading of softfpu in emulation mode
  1285. + Correct routine call for softfpu
  1286. * Extended type must also be defined even with softfpu
  1287. Revision 1.84 2002/11/15 01:58:53 peter
  1288. * merged changes from 1.0.7 up to 04-11
  1289. - -V option for generating bug report tracing
  1290. - more tracing for option parsing
  1291. - errors for cdecl and high()
  1292. - win32 import stabs
  1293. - win32 records<=8 are returned in eax:edx (turned off by default)
  1294. - heaptrc update
  1295. - more info for temp management in .s file with EXTDEBUG
  1296. Revision 1.83 2002/11/09 15:33:26 carl
  1297. * major alignment updates
  1298. Revision 1.82 2002/10/16 06:32:52 michael
  1299. + Renamed thread unit to systhrds
  1300. Revision 1.81 2002/10/14 19:42:34 peter
  1301. * only use init tables for threadvars
  1302. Revision 1.80 2002/10/06 19:41:30 peter
  1303. * Add finalization of typed consts
  1304. * Finalization of globals in the main program
  1305. Revision 1.79 2002/09/09 17:34:15 peter
  1306. * tdicationary.replace added to replace and item in a dictionary. This
  1307. is only allowed for the same name
  1308. * varsyms are inserted in symtable before the types are parsed. This
  1309. fixes the long standing "var longint : longint" bug
  1310. - consume_idlist and idstringlist removed. The loops are inserted
  1311. at the callers place and uses the symtable for duplicate id checking
  1312. Revision 1.78 2002/09/07 15:25:07 peter
  1313. * old logs removed and tabs fixed
  1314. Revision 1.77 2002/09/03 16:26:27 daniel
  1315. * Make Tprocdef.defs protected
  1316. Revision 1.76 2002/09/02 18:46:26 peter
  1317. * insert PASCALMAIN in library for Win32 only
  1318. Revision 1.75 2002/08/31 15:59:30 florian
  1319. + HEAP* stuff must be generated for Linux/PPC as well
  1320. + direct assembler reader searches now global and static symtables as well
  1321. Revision 1.74 2002/08/25 19:25:20 peter
  1322. * sym.insert_in_data removed
  1323. * symtable.insertvardata/insertconstdata added
  1324. * removed insert_in_data call from symtable.insert, it needs to be
  1325. called separatly. This allows to deref the address calculation
  1326. * procedures now calculate the parast addresses after the procedure
  1327. directives are parsed. This fixes the cdecl parast problem
  1328. * push_addr_param has an extra argument that specifies if cdecl is used
  1329. or not
  1330. Revision 1.73 2002/08/18 20:06:25 peter
  1331. * inlining is now also allowed in interface
  1332. * renamed write/load to ppuwrite/ppuload
  1333. * tnode storing in ppu
  1334. * nld,ncon,nbas are already updated for storing in ppu
  1335. Revision 1.72 2002/08/17 09:23:39 florian
  1336. * first part of procinfo rewrite
  1337. Revision 1.71 2002/08/11 13:24:12 peter
  1338. * saving of asmsymbols in ppu supported
  1339. * asmsymbollist global is removed and moved into a new class
  1340. tasmlibrarydata that will hold the info of a .a file which
  1341. corresponds with a single module. Added librarydata to tmodule
  1342. to keep the library info stored for the module. In the future the
  1343. objectfiles will also be stored to the tasmlibrarydata class
  1344. * all getlabel/newasmsymbol and friends are moved to the new class
  1345. Revision 1.70 2002/08/10 14:46:29 carl
  1346. + moved target_cpu_string to cpuinfo
  1347. * renamed asmmode enum.
  1348. * assembler reader has now less ifdef's
  1349. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1350. Revision 1.69 2002/07/26 21:15:41 florian
  1351. * rewrote the system handling
  1352. Revision 1.68 2002/07/04 20:43:01 florian
  1353. * first x86-64 patches
  1354. Revision 1.67 2002/07/01 18:46:25 peter
  1355. * internal linker
  1356. * reorganized aasm layer
  1357. Revision 1.66 2002/05/16 19:46:43 carl
  1358. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1359. + try to fix temp allocation (still in ifdef)
  1360. + generic constructor calls
  1361. + start of tassembler / tmodulebase class cleanup
  1362. Revision 1.65 2002/05/14 19:34:49 peter
  1363. * removed old logs and updated copyright year
  1364. Revision 1.64 2002/05/12 16:53:09 peter
  1365. * moved entry and exitcode to ncgutil and cgobj
  1366. * foreach gets extra argument for passing local data to the
  1367. iterator function
  1368. * -CR checks also class typecasts at runtime by changing them
  1369. into as
  1370. * fixed compiler to cycle with the -CR option
  1371. * fixed stabs with elf writer, finally the global variables can
  1372. be watched
  1373. * removed a lot of routines from cga unit and replaced them by
  1374. calls to cgobj
  1375. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1376. u32bit then the other is typecasted also to u32bit without giving
  1377. a rangecheck warning/error.
  1378. * fixed pascal calling method with reversing also the high tree in
  1379. the parast, detected by tcalcst3 test
  1380. Revision 1.63 2002/05/06 19:54:50 carl
  1381. + added more patches from Mazen for SPARC port
  1382. Revision 1.62 2002/04/20 21:32:24 carl
  1383. + generic FPC_CHECKPOINTER
  1384. + first parameter offset in stack now portable
  1385. * rename some constants
  1386. + move some cpu stuff to other units
  1387. - remove unused constents
  1388. * fix stacksize for some targets
  1389. * fix generic size problems which depend now on EXTEND_SIZE constant
  1390. Revision 1.61 2002/04/19 15:46:02 peter
  1391. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1392. in most cases and not written to the ppu
  1393. * add mangeledname_prefix() routine to generate the prefix of
  1394. manglednames depending on the current procedure, object and module
  1395. * removed static procprefix since the mangledname is now build only
  1396. on demand from tprocdef.mangledname
  1397. Revision 1.60 2002/04/14 16:53:10 carl
  1398. + align code section and data section according to alignment rules
  1399. Revision 1.59 2002/04/07 17:58:38 carl
  1400. + generic stack checking
  1401. Revision 1.58 2002/04/04 19:06:03 peter
  1402. * removed unused units
  1403. * use tlocation.size in cg.a_*loc*() routines
  1404. }