pmodules.pas 56 KB

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