pmodules.pas 57 KB

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