pmodules.pas 57 KB

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