pmodules.pas 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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. aasmtai,aasmcpu,
  31. cgbase,cpuinfo,cgobj,
  32. nbas,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,procinfo,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure fixseg(p:TAAsmoutput;sec:TSection);
  40. begin
  41. p.insert(Tai_section.Create(sec));
  42. if (cs_create_smart in aktmoduleswitches) then
  43. p.insert(Tai_cut.Create);
  44. p.concat(Tai_section.Create(sec_none));
  45. end;
  46. procedure create_objectfile;
  47. var
  48. DLLScanner : TDLLScanner;
  49. s : string;
  50. KeepShared : TStringList;
  51. begin
  52. { try to create import entries from system dlls }
  53. if target_info.DllScanSupported and
  54. (not current_module.linkOtherSharedLibs.Empty) then
  55. begin
  56. { Init DLLScanner }
  57. if assigned(CDLLScanner[target_info.system]) then
  58. DLLScanner:=CDLLScanner[target_info.system].Create
  59. else
  60. internalerror(200104121);
  61. KeepShared:=TStringList.Create;
  62. { Walk all shared libs }
  63. While not current_module.linkOtherSharedLibs.Empty do
  64. begin
  65. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  66. if not DLLScanner.scan(s) then
  67. KeepShared.Concat(s);
  68. end;
  69. DLLscanner.Free;
  70. { Recreate import section }
  71. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  72. begin
  73. if assigned(importssection)then
  74. importssection.clear
  75. else
  76. importssection:=taasmoutput.Create;
  77. importlib.generatelib;
  78. end;
  79. { Readd the not processed files }
  80. while not KeepShared.Empty do
  81. begin
  82. s:=KeepShared.GetFirst;
  83. current_module.linkOtherSharedLibs.add(s,link_allways);
  84. end;
  85. KeepShared.Free;
  86. end;
  87. { create the .s file and assemble it }
  88. GenerateAsm(false);
  89. { Also create a smartlinked version ? }
  90. if (cs_create_smart in aktmoduleswitches) then
  91. begin
  92. { regenerate the importssection for win32 }
  93. if assigned(importssection) and
  94. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  95. begin
  96. importsSection.clear;
  97. importlib.generatesmartlib;
  98. end;
  99. GenerateAsm(true);
  100. if target_asm.needar then
  101. Linker.MakeStaticLibrary;
  102. end;
  103. { resource files }
  104. CompileResourceFiles;
  105. end;
  106. procedure insertobjectfile;
  107. { Insert the used object file for this unit in the used list for this unit }
  108. begin
  109. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  110. current_module.flags:=current_module.flags or uf_static_linked;
  111. if (cs_create_smart in aktmoduleswitches) then
  112. begin
  113. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  114. current_module.flags:=current_module.flags or uf_smart_linked;
  115. end;
  116. end;
  117. procedure insertsegment;
  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.Createdataname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,'')));
  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.Createdataname(make_mangledname('THREADVARLIST',current_module.localsymtable,'')));
  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. end;
  187. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  188. var
  189. ltvTable : taasmoutput;
  190. begin
  191. ltvTable:=taasmoutput(arg);
  192. if (tsym(p).typ=varsym) and
  193. (vo_is_thread_var in tvarsym(p).varoptions) then
  194. begin
  195. { address of threadvar }
  196. ltvTable.concat(tai_const_symbol.createdataname(tvarsym(p).mangledname));
  197. { size of threadvar }
  198. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  199. end;
  200. end;
  201. procedure InsertThreadvars;
  202. var
  203. s : string;
  204. ltvTable : TAAsmoutput;
  205. begin
  206. ltvTable:=TAAsmoutput.create;
  207. if assigned(current_module.globalsymtable) then
  208. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  209. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. if ltvTable.first<>nil then
  211. begin
  212. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.createdataname_global(s,0));
  215. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(s));
  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.Createdataname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,'')));
  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.Createdataname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,'')));
  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(make_mangledname('INIT$',hp.u.globalsymtable,'')))
  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(make_mangledname('FINALIZE$',hp.u.globalsymtable,'')))
  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(make_mangledname('INIT$',current_module.localsymtable,'')))
  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(make_mangledname('FINALIZE$',current_module.localsymtable,'')))
  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. system_i386_OS2,system_i386_EMX:
  325. ;
  326. system_powerpc_macos:
  327. ;
  328. system_i386_watcom:
  329. ;
  330. system_alpha_linux:
  331. ;
  332. system_m68k_Mac:
  333. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  334. system_m68k_PalmOS:
  335. ;
  336. else
  337. begin
  338. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  339. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  340. end;
  341. end;
  342. {$ifdef m68k}
  343. if target_info.system<>system_m68k_PalmOS then
  344. begin
  345. dataSegment.concat(Tai_align.Create(const_align(4)));
  346. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  347. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  348. end;
  349. {$else m68k}
  350. dataSegment.concat(Tai_align.Create(const_align(4)));
  351. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  352. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  353. {$endif m68k}
  354. end;
  355. procedure insertstacklength;
  356. begin
  357. { stacksize can be specified and is now simulated }
  358. dataSegment.concat(Tai_align.Create(const_align(4)));
  359. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  360. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  361. end;
  362. procedure loaddefaultunits;
  363. procedure AddUnit(const s:string);
  364. var
  365. hp : tppumodule;
  366. unitsym : tunitsym;
  367. begin
  368. { load unit }
  369. hp:=registerunit(current_module,s,'');
  370. hp.loadppu;
  371. hp.adddependency(current_module);
  372. { add to symtable stack }
  373. tsymtable(hp.globalsymtable).next:=symtablestack;
  374. symtablestack:=hp.globalsymtable;
  375. { insert unitsym }
  376. unitsym:=tunitsym.create(s,hp.globalsymtable);
  377. inc(unitsym.refs);
  378. refsymtable.insert(unitsym);
  379. { add to used units }
  380. current_module.addusedunit(hp,false,unitsym);
  381. end;
  382. begin
  383. { are we compiling the system unit? }
  384. if (cs_compilesystem in aktmoduleswitches) then
  385. begin
  386. { create system defines }
  387. createconstdefs;
  388. { we don't need to reset anything, it's already done in parser.pas }
  389. exit;
  390. end;
  391. { insert the system unit, it is allways the first }
  392. Symtablestack:=nil;
  393. AddUnit('System');
  394. SystemUnit:=TGlobalSymtable(Symtablestack);
  395. { read default constant definitions }
  396. make_ref:=false;
  397. readconstdefs;
  398. make_ref:=true;
  399. {$ifdef cpufpemu}
  400. { Floating point emulation unit? }
  401. if (cs_fp_emulation in aktmoduleswitches) then
  402. AddUnit('SoftFpu');
  403. {$endif cpufpemu}
  404. { Thread support unit? }
  405. if (cs_threading in aktmoduleswitches) then
  406. AddUnit('SysThrds');
  407. { Objpas unit? }
  408. if m_objpas in aktmodeswitches then
  409. AddUnit('ObjPas');
  410. { Profile unit? Needed for go32v2 only }
  411. if (cs_profile in aktmoduleswitches) and
  412. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  413. AddUnit('Profile');
  414. { Units only required for main module }
  415. if not(current_module.is_unit) then
  416. begin
  417. { Heaptrc unit }
  418. if (cs_gdb_heaptrc in aktglobalswitches) then
  419. AddUnit('HeapTrc');
  420. { Lineinfo unit }
  421. if (cs_gdb_lineinfo in aktglobalswitches) then
  422. AddUnit('LineInfo');
  423. end;
  424. { save default symtablestack }
  425. defaultsymtablestack:=symtablestack;
  426. end;
  427. procedure loadunits;
  428. var
  429. s,sorg : stringid;
  430. fn : string;
  431. pu : tused_unit;
  432. hp2 : tmodule;
  433. hp3 : tsymtable;
  434. unitsym : tunitsym;
  435. begin
  436. consume(_USES);
  437. {$ifdef DEBUG}
  438. test_symtablestack;
  439. {$endif DEBUG}
  440. repeat
  441. s:=pattern;
  442. sorg:=orgpattern;
  443. consume(_ID);
  444. { support "<unit> in '<file>'" construct, but not for tp7 }
  445. if not(m_tp7 in aktmodeswitches) then
  446. begin
  447. if try_to_consume(_OP_IN) then
  448. fn:=FixFileName(get_stringconst)
  449. else
  450. fn:='';
  451. end;
  452. { Give a warning if objpas is loaded }
  453. if s='OBJPAS' then
  454. Message(parser_w_no_objpas_use_mode);
  455. { Using the unit itself is not possible }
  456. if (s<>current_module.modulename^) then
  457. begin
  458. { check if the unit is already used }
  459. hp2:=nil;
  460. pu:=tused_unit(current_module.used_units.first);
  461. while assigned(pu) do
  462. begin
  463. if (pu.u.modulename^=s) then
  464. begin
  465. hp2:=pu.u;
  466. break;
  467. end;
  468. pu:=tused_unit(pu.next);
  469. end;
  470. if not assigned(hp2) then
  471. hp2:=registerunit(current_module,sorg,fn)
  472. else
  473. Message1(sym_e_duplicate_id,s);
  474. { Create unitsym, we need to use the name as specified, we
  475. can not use the modulename because that can be different
  476. when -Un is used }
  477. unitsym:=tunitsym.create(sorg,nil);
  478. refsymtable.insert(unitsym);
  479. { the current module uses the unit hp2 }
  480. current_module.addusedunit(hp2,true,unitsym);
  481. end
  482. else
  483. Message1(sym_e_duplicate_id,s);
  484. if token=_COMMA then
  485. begin
  486. pattern:='';
  487. consume(_COMMA);
  488. end
  489. else
  490. break;
  491. until false;
  492. consume(_SEMICOLON);
  493. { Load the units }
  494. pu:=tused_unit(current_module.used_units.first);
  495. while assigned(pu) do
  496. begin
  497. { Only load the units that are in the current
  498. (interface/implementation) uses clause }
  499. if pu.in_uses and
  500. (pu.in_interface=current_module.in_interface) then
  501. begin
  502. tppumodule(pu.u).loadppu;
  503. { is our module compiled? then we can stop }
  504. if current_module.state=ms_compiled then
  505. exit;
  506. { add this unit to the dependencies }
  507. pu.u.adddependency(current_module);
  508. { save crc values }
  509. pu.checksum:=pu.u.crc;
  510. pu.interface_checksum:=pu.u.interface_crc;
  511. { connect unitsym to the globalsymtable of the unit }
  512. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  513. { increase refs of the unitsym when the unit contains
  514. initialization/finalization code so it doesn't trigger
  515. the unit not used hint }
  516. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  517. inc(pu.unitsym.refs);
  518. end;
  519. pu:=tused_unit(pu.next);
  520. end;
  521. { set the symtable to systemunit so it gets reorderd correctly,
  522. then insert the units in the symtablestack }
  523. pu:=tused_unit(current_module.used_units.first);
  524. symtablestack:=defaultsymtablestack;
  525. while assigned(pu) do
  526. begin
  527. {$IfDef GDB}
  528. if (cs_debuginfo in aktmoduleswitches) and
  529. (cs_gdb_dbx in aktglobalswitches) and
  530. not pu.is_stab_written then
  531. begin
  532. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  533. pu.is_stab_written:=true;
  534. pu.unitid:=tsymtable(pu.u.globalsymtable).unitid;
  535. end;
  536. {$EndIf GDB}
  537. if pu.in_uses then
  538. begin
  539. { Reinsert in symtablestack }
  540. hp3:=symtablestack;
  541. while assigned(hp3) do
  542. begin
  543. { insert units only once ! }
  544. if pu.u.globalsymtable=hp3 then
  545. break;
  546. hp3:=hp3.next;
  547. { unit isn't inserted }
  548. if hp3=nil then
  549. begin
  550. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  551. symtablestack:=tsymtable(pu.u.globalsymtable);
  552. {$ifdef DEBUG}
  553. test_symtablestack;
  554. {$endif DEBUG}
  555. end;
  556. end;
  557. end;
  558. pu:=tused_unit(pu.next);
  559. end;
  560. end;
  561. procedure write_gdb_info;
  562. {$IfDef GDB}
  563. var
  564. hp : tused_unit;
  565. begin
  566. if not (cs_debuginfo in aktmoduleswitches) then
  567. exit;
  568. { now insert the units in the symtablestack }
  569. hp:=tused_unit(current_module.used_units.first);
  570. while assigned(hp) do
  571. begin
  572. if (cs_debuginfo in aktmoduleswitches) and
  573. not hp.is_stab_written then
  574. begin
  575. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  576. hp.is_stab_written:=true;
  577. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  578. end;
  579. hp:=tused_unit(hp.next);
  580. end;
  581. if (not current_module.in_interface) and
  582. assigned(current_module.localsymtable) then
  583. begin
  584. { all types }
  585. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  586. { and all local symbols}
  587. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  588. end
  589. else if assigned(current_module.globalsymtable) then
  590. begin
  591. { all types }
  592. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  593. { and all local symbols}
  594. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  595. end;
  596. if (cs_gdb_dbx in aktglobalswitches) then
  597. begin
  598. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  599. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  600. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  601. debugList.concat(Tai_stabs.Create(strpnew('"'+
  602. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  603. tostr(N_EINCL)+',0,0,0')));
  604. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  605. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  606. do_count_dbx:=false;
  607. end;
  608. end;
  609. {$Else GDB}
  610. begin
  611. end;
  612. {$EndIf GDB}
  613. procedure parse_implementation_uses;
  614. begin
  615. if token=_USES then
  616. begin
  617. loadunits;
  618. {$ifdef DEBUG}
  619. test_symtablestack;
  620. {$endif DEBUG}
  621. end;
  622. end;
  623. procedure setupglobalswitches;
  624. begin
  625. { can't have local browser when no global browser }
  626. if (cs_local_browser in aktmoduleswitches) and
  627. not(cs_browser in aktmoduleswitches) then
  628. exclude(aktmoduleswitches,cs_local_browser);
  629. { define a symbol in delphi,objfpc,tp,gpc mode }
  630. if (m_delphi in aktmodeswitches) then
  631. current_scanner.def_macro('FPC_DELPHI')
  632. else
  633. if (m_tp7 in aktmodeswitches) then
  634. current_scanner.def_macro('FPC_TP')
  635. else
  636. if (m_objfpc in aktmodeswitches) then
  637. current_scanner.def_macro('FPC_OBJFPC')
  638. else
  639. if (m_gpc in aktmodeswitches) then
  640. current_scanner.def_macro('FPC_GPC');
  641. end;
  642. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  643. var
  644. stt : tsymtable;
  645. ps : tprocsym;
  646. pd : tprocdef;
  647. begin
  648. { there should be no current_procinfo available }
  649. if assigned(current_procinfo) then
  650. internalerror(200304275);
  651. {Generate a procsym for main}
  652. make_ref:=false;
  653. { try to insert in in static symtable ! }
  654. stt:=symtablestack;
  655. symtablestack:=st;
  656. { generate procsym }
  657. ps:=tprocsym.create('$'+name);
  658. { main are allways used }
  659. inc(ps.refs);
  660. symtablestack.insert(ps);
  661. pd:=tprocdef.create(main_program_level);
  662. include(pd.procoptions,po_public);
  663. pd.procsym:=ps;
  664. ps.addprocdef(pd);
  665. { restore symtable }
  666. make_ref:=true;
  667. symtablestack:=stt;
  668. { set procdef options }
  669. pd.proctypeoption:=potype;
  670. pd.proccalloption:=pocall_default;
  671. pd.forwarddef:=false;
  672. pd.setmangledname(target_info.cprefix+name);
  673. pd.aliasnames.insert(pd.mangledname);
  674. { We don't need is a local symtable. Change it into the static
  675. symtable }
  676. pd.localst.free;
  677. pd.localst:=st;
  678. { set procinfo and current_procinfo.procdef }
  679. current_procinfo:=cprocinfo.create(nil);
  680. current_module.procinfo:=current_procinfo;
  681. current_procinfo.procdef:=pd;
  682. { return procdef }
  683. create_main_proc:=pd;
  684. { main proc does always a call e.g. to init system unit }
  685. include(current_procinfo.flags,pi_do_call);
  686. end;
  687. procedure release_main_proc(pd:tprocdef);
  688. begin
  689. { this is a main proc, so there should be no parent }
  690. if not(assigned(current_procinfo)) or
  691. assigned(current_procinfo.parent) or
  692. not(current_procinfo.procdef=pd) then
  693. internalerror(200304276);
  694. { remove procinfo }
  695. current_module.procinfo:=nil;
  696. current_procinfo.free;
  697. current_procinfo:=nil;
  698. { remove localst as it was replaced by staticsymtable }
  699. pd.localst:=nil;
  700. end;
  701. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  702. var
  703. pd : tprocdef;
  704. begin
  705. { update module flags }
  706. current_module.flags:=current_module.flags or flag;
  707. { create procdef }
  708. case flag of
  709. uf_init :
  710. begin
  711. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  712. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  713. end;
  714. uf_finalize :
  715. begin
  716. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  717. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  718. end;
  719. else
  720. internalerror(200304253);
  721. end;
  722. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  723. tcgprocinfo(current_procinfo).add_entry_exit_code;
  724. tcgprocinfo(current_procinfo).generate_code;
  725. release_main_proc(pd);
  726. end;
  727. procedure proc_unit;
  728. function is_assembler_generated:boolean;
  729. begin
  730. is_assembler_generated:=(Errorcount=0) and
  731. not(
  732. codeSegment.empty and
  733. dataSegment.empty and
  734. bssSegment.empty and
  735. ((importssection=nil) or importsSection.empty) and
  736. ((resourcesection=nil) or resourceSection.empty) and
  737. ((resourcestringlist=nil) or resourcestringList.empty)
  738. );
  739. end;
  740. var
  741. main_file: tinputfile;
  742. st : tsymtable;
  743. unitst : tglobalsymtable;
  744. {$ifdef GDB}
  745. pu : tused_unit;
  746. {$endif GDB}
  747. store_crc,store_interface_crc : cardinal;
  748. s2 : ^string; {Saves stack space}
  749. force_init_final : boolean;
  750. pd : tprocdef;
  751. begin
  752. consume(_UNIT);
  753. if compile_level=1 then
  754. Status.IsExe:=false;
  755. if token=_ID then
  756. begin
  757. { create filenames and unit name }
  758. main_file := current_scanner.inputfile;
  759. while assigned(main_file.next) do
  760. main_file := main_file.next;
  761. current_module.SetFileName(main_file.path^+main_file.name^,true);
  762. current_module.SetModuleName(orgpattern);
  763. { check for system unit }
  764. new(s2);
  765. s2^:=upper(SplitName(main_file.name^));
  766. if (cs_check_unit_name in aktglobalswitches) and
  767. not((current_module.modulename^=s2^) or
  768. ((length(current_module.modulename^)>8) and
  769. (copy(current_module.modulename^,1,8)=s2^))) then
  770. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  771. if (current_module.modulename^='SYSTEM') then
  772. include(aktmoduleswitches,cs_compilesystem);
  773. dispose(s2);
  774. end;
  775. consume(_ID);
  776. consume(_SEMICOLON);
  777. consume(_INTERFACE);
  778. { global switches are read, so further changes aren't allowed }
  779. current_module.in_global:=false;
  780. { handle the global switches }
  781. setupglobalswitches;
  782. message1(unit_u_loading_interface_units,current_module.modulename^);
  783. { update status }
  784. status.currentmodule:=current_module.realmodulename^;
  785. { maybe turn off m_objpas if we are compiling objpas }
  786. if (current_module.modulename^='OBJPAS') then
  787. exclude(aktmodeswitches,m_objpas);
  788. parse_only:=true;
  789. { generate now the global symboltable }
  790. st:=tglobalsymtable.create(current_module.modulename^);
  791. refsymtable:=st;
  792. unitst:=tglobalsymtable(st);
  793. { define first as local to overcome dependency conflicts }
  794. current_module.localsymtable:=st;
  795. { the unit name must be usable as a unit specifier }
  796. { inside the unit itself (PM) }
  797. { this also forbids to have another symbol }
  798. { with the same name as the unit }
  799. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  800. { load default units, like the system unit }
  801. loaddefaultunits;
  802. { reset }
  803. make_ref:=true;
  804. { insert qualifier for the system unit (allows system.writeln) }
  805. if not(cs_compilesystem in aktmoduleswitches) then
  806. begin
  807. if token=_USES then
  808. begin
  809. loadunits;
  810. { has it been compiled at a higher level ?}
  811. if current_module.state=ms_compiled then
  812. exit;
  813. end;
  814. { ... but insert the symbol table later }
  815. st.next:=symtablestack;
  816. symtablestack:=st;
  817. end
  818. else
  819. { while compiling a system unit, some types are directly inserted }
  820. begin
  821. st.next:=symtablestack;
  822. symtablestack:=st;
  823. insert_intern_types(st);
  824. end;
  825. { now we know the place to insert the constants }
  826. constsymtable:=symtablestack;
  827. { move the global symtab from the temporary local to global }
  828. current_module.globalsymtable:=current_module.localsymtable;
  829. current_module.localsymtable:=nil;
  830. reset_global_defs;
  831. { number all units, so we know if a unit is used by this unit or
  832. needs to be added implicitly }
  833. current_module.numberunits;
  834. { ... parse the declarations }
  835. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  836. read_interface_declarations;
  837. { leave when we got an error }
  838. if (Errorcount>0) and not status.skip_error then
  839. begin
  840. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  841. status.skip_error:=true;
  842. exit;
  843. end;
  844. {$ifdef New_GDB}
  845. write_gdb_info;
  846. {$endIf Def New_GDB}
  847. { Our interface is compiled, generate CRC and switch to implementation }
  848. if not(cs_compilesystem in aktmoduleswitches) and
  849. (Errorcount=0) then
  850. tppumodule(current_module).getppucrc;
  851. current_module.in_interface:=false;
  852. current_module.interface_compiled:=true;
  853. { First reload all units depending on our interface, we need to do this
  854. in the implementation part to prevent errorneous circular references }
  855. reload_flagged_units;
  856. { Parse the implementation section }
  857. consume(_IMPLEMENTATION);
  858. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  859. parse_only:=false;
  860. { generates static symbol table }
  861. st:=tstaticsymtable.create(current_module.modulename^);
  862. current_module.localsymtable:=st;
  863. { remove the globalsymtable from the symtable stack }
  864. { to reinsert it after loading the implementation units }
  865. symtablestack:=unitst.next;
  866. { we don't want implementation units symbols in unitsymtable !! PM }
  867. refsymtable:=st;
  868. { Read the implementation units }
  869. parse_implementation_uses;
  870. if current_module.state=ms_compiled then
  871. exit;
  872. { reset ranges/stabs in exported definitions }
  873. reset_global_defs;
  874. { All units are read, now give them a number }
  875. current_module.numberunits;
  876. { now we can change refsymtable }
  877. refsymtable:=st;
  878. { but reinsert the global symtable as lasts }
  879. unitst.next:=symtablestack;
  880. symtablestack:=unitst;
  881. tstoredsymtable(symtablestack).chainoperators;
  882. {$ifdef DEBUG}
  883. test_symtablestack;
  884. {$endif DEBUG}
  885. constsymtable:=symtablestack;
  886. {$ifdef Splitheap}
  887. if testsplit then
  888. begin
  889. Split_Heap;
  890. allow_special:=true;
  891. Switch_to_temp_heap;
  892. end;
  893. { it will report all crossings }
  894. allow_special:=false;
  895. {$endif Splitheap}
  896. Message1(parser_u_parsing_implementation,current_module.modulename^);
  897. if current_module.in_interface then
  898. internalerror(200212285);
  899. { Compile the unit }
  900. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  901. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  902. tcgprocinfo(current_procinfo).parse_body;
  903. tcgprocinfo(current_procinfo).generate_code;
  904. tcgprocinfo(current_procinfo).resetprocdef;
  905. release_main_proc(pd);
  906. { if the unit contains ansi/widestrings, initialization and
  907. finalization code must be forced }
  908. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  909. tstaticsymtable(current_module.localsymtable).needs_init_final;
  910. { should we force unit initialization? }
  911. { this is a hack, but how can it be done better ? }
  912. if force_init_final and ((current_module.flags and uf_init)=0) then
  913. gen_implicit_initfinal(uf_init,st);
  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. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  921. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  922. tcgprocinfo(current_procinfo).parse_body;
  923. tcgprocinfo(current_procinfo).generate_code;
  924. tcgprocinfo(current_procinfo).resetprocdef;
  925. release_main_proc(pd);
  926. end
  927. else if force_init_final then
  928. gen_implicit_initfinal(uf_finalize,st);
  929. { the last char should always be a point }
  930. consume(_POINT);
  931. { generate a list of threadvars }
  932. InsertThreadvars;
  933. { Generate resoucestrings }
  934. If ResourceStrings.ResStrCount>0 then
  935. begin
  936. ResourceStrings.CreateResourceStringList;
  937. current_module.flags:=current_module.flags or uf_has_resources;
  938. { only write if no errors found }
  939. if (Errorcount=0) then
  940. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  941. end;
  942. if (Errorcount=0) then
  943. begin
  944. { test static symtable }
  945. tstoredsymtable(st).allsymbolsused;
  946. tstoredsymtable(st).allprivatesused;
  947. current_module.allunitsused;
  948. end;
  949. {$ifdef GDB}
  950. { add all used definitions even for implementation}
  951. if (cs_debuginfo in aktmoduleswitches) then
  952. begin
  953. {$IfnDef New_GDB}
  954. if assigned(current_module.globalsymtable) then
  955. begin
  956. { all types }
  957. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  958. { and all local symbols}
  959. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  960. end;
  961. { all local types }
  962. tglobalsymtable(st)^.concattypestabto(debuglist);
  963. { and all local symbols}
  964. st^.concatstabto(debuglist);
  965. {$else New_GDB}
  966. write_gdb_info;
  967. {$endIf Def New_GDB}
  968. end;
  969. {$endif GDB}
  970. reset_global_defs;
  971. if (Errorcount=0) then
  972. begin
  973. { tests, if all (interface) forwards are resolved }
  974. tstoredsymtable(symtablestack).check_forwards;
  975. { check if all private fields are used }
  976. tstoredsymtable(symtablestack).allprivatesused;
  977. { remove cross unit overloads }
  978. tstoredsymtable(symtablestack).unchain_overloaded;
  979. end;
  980. {$ifdef GDB}
  981. tglobalsymtable(symtablestack).is_stab_written:=false;
  982. {$endif GDB}
  983. { leave when we got an error }
  984. if (Errorcount>0) and not status.skip_error then
  985. begin
  986. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  987. status.skip_error:=true;
  988. exit;
  989. end;
  990. { generate imports }
  991. if current_module.uses_imports then
  992. importlib.generatelib;
  993. { insert own objectfile, or say that it's in a library
  994. (no check for an .o when loading) }
  995. if is_assembler_generated then
  996. insertobjectfile
  997. else
  998. current_module.flags:=current_module.flags or uf_no_link;
  999. if cs_local_browser in aktmoduleswitches then
  1000. current_module.localsymtable:=refsymtable;
  1001. {$ifdef GDB}
  1002. pu:=tused_unit(usedunits.first);
  1003. while assigned(pu) do
  1004. begin
  1005. if assigned(pu.u.globalsymtable) then
  1006. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  1007. pu:=tused_unit(pu.next);
  1008. end;
  1009. {$endif GDB}
  1010. if is_assembler_generated then
  1011. begin
  1012. { finish asmlist by adding segment starts }
  1013. insertsegment;
  1014. { assemble }
  1015. create_objectfile;
  1016. end;
  1017. { Write out the ppufile after the object file has been created }
  1018. store_interface_crc:=current_module.interface_crc;
  1019. store_crc:=current_module.crc;
  1020. if (Errorcount=0) then
  1021. tppumodule(current_module).writeppu;
  1022. if not(cs_compilesystem in aktmoduleswitches) then
  1023. if store_interface_crc<>current_module.interface_crc then
  1024. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1025. {$ifdef EXTDEBUG}
  1026. if not(cs_compilesystem in aktmoduleswitches) then
  1027. if (store_crc<>current_module.crc) and simplify_ppu then
  1028. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1029. {$endif EXTDEBUG}
  1030. { remove static symtable (=refsymtable) here to save some mem }
  1031. if not (cs_local_browser in aktmoduleswitches) then
  1032. begin
  1033. st.free;
  1034. current_module.localsymtable:=nil;
  1035. end;
  1036. { leave when we got an error }
  1037. if (Errorcount>0) and not status.skip_error then
  1038. begin
  1039. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1040. status.skip_error:=true;
  1041. exit;
  1042. end;
  1043. Message1(unit_u_finished_compiling,current_module.modulename^);
  1044. end;
  1045. procedure proc_program(islibrary : boolean);
  1046. var
  1047. main_file: tinputfile;
  1048. st : tsymtable;
  1049. hp : tmodule;
  1050. pd : tprocdef;
  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_interface:=false;
  1117. current_module.interface_compiled:=true;
  1118. { insert after the unit symbol tables the static symbol table }
  1119. { of the program }
  1120. st:=tstaticsymtable.create(current_module.modulename^);;
  1121. current_module.localsymtable:=st;
  1122. refsymtable:=st;
  1123. { load standard units (system,objpas,profile unit) }
  1124. loaddefaultunits;
  1125. {Load the units used by the program we compile.}
  1126. if token=_USES then
  1127. loadunits;
  1128. tstoredsymtable(symtablestack).chainoperators;
  1129. { reset ranges/stabs in exported definitions }
  1130. reset_global_defs;
  1131. { All units are read, now give them a number }
  1132. current_module.numberunits;
  1133. {Insert the name of the main program into the symbol table.}
  1134. if current_module.realmodulename^<>'' then
  1135. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1136. { ...is also constsymtable, this is the symtable where }
  1137. { the elements of enumeration types are inserted }
  1138. constsymtable:=st;
  1139. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1140. { The program intialization needs an alias, so it can be called
  1141. from the bootstrap code.}
  1142. if islibrary then
  1143. begin
  1144. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1145. { Win32 startup code needs a single name }
  1146. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1147. pd.aliasnames.insert('PASCALMAIN');
  1148. end
  1149. else
  1150. begin
  1151. pd:=create_main_proc('main',potype_proginit,st);
  1152. pd.aliasnames.insert('PASCALMAIN');
  1153. end;
  1154. tcgprocinfo(current_procinfo).parse_body;
  1155. tcgprocinfo(current_procinfo).generate_code;
  1156. tcgprocinfo(current_procinfo).resetprocdef;
  1157. release_main_proc(pd);
  1158. { should we force unit initialization? }
  1159. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1160. begin
  1161. { initialize section }
  1162. gen_implicit_initfinal(uf_init,st);
  1163. { finalize section }
  1164. gen_implicit_initfinal(uf_finalize,st);
  1165. end;
  1166. { Add symbol to the exports section for win32 so smartlinking a
  1167. DLL will include the edata section }
  1168. if assigned(exportlib) and
  1169. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1170. assigned(current_module._exports.first) then
  1171. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1172. If ResourceStrings.ResStrCount>0 then
  1173. begin
  1174. ResourceStrings.CreateResourceStringList;
  1175. { only write if no errors found }
  1176. if (Errorcount=0) then
  1177. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1178. end;
  1179. { finalize? }
  1180. if token=_FINALIZATION then
  1181. begin
  1182. { set module options }
  1183. current_module.flags:=current_module.flags or uf_finalize;
  1184. { Compile the finalize }
  1185. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1186. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1187. tcgprocinfo(current_procinfo).parse_body;
  1188. tcgprocinfo(current_procinfo).generate_code;
  1189. tcgprocinfo(current_procinfo).resetprocdef;
  1190. release_main_proc(pd);
  1191. end;
  1192. { consume the last point }
  1193. consume(_POINT);
  1194. {$ifdef New_GDB}
  1195. write_gdb_info;
  1196. {$endIf Def New_GDB}
  1197. { leave when we got an error }
  1198. if (Errorcount>0) and not status.skip_error then
  1199. begin
  1200. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1201. status.skip_error:=true;
  1202. exit;
  1203. end;
  1204. if (Errorcount=0) then
  1205. begin
  1206. { test static symtable }
  1207. tstoredsymtable(st).allsymbolsused;
  1208. tstoredsymtable(st).allprivatesused;
  1209. current_module.allunitsused;
  1210. end;
  1211. { generate a list of threadvars }
  1212. InsertThreadvars;
  1213. { generate imports }
  1214. if current_module.uses_imports then
  1215. importlib.generatelib;
  1216. if islibrary or
  1217. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1218. (target_info.system=system_i386_NETWARE) then
  1219. exportlib.generatelib;
  1220. { insert Tables and Heap }
  1221. insertThreadVarTablesTable;
  1222. insertResourceTablesTable;
  1223. insertinitfinaltable;
  1224. insertheap;
  1225. insertstacklength;
  1226. // datasize:=symtablestack.datasize;
  1227. { finish asmlist by adding segment starts }
  1228. insertsegment;
  1229. { insert own objectfile }
  1230. insertobjectfile;
  1231. { assemble and link }
  1232. create_objectfile;
  1233. { leave when we got an error }
  1234. if (Errorcount>0) and not status.skip_error then
  1235. begin
  1236. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1237. status.skip_error:=true;
  1238. exit;
  1239. end;
  1240. { create the executable when we are at level 1 }
  1241. if (compile_level=1) then
  1242. begin
  1243. { insert all .o files from all loaded units }
  1244. hp:=tmodule(loaded_units.first);
  1245. while assigned(hp) do
  1246. begin
  1247. linker.AddModuleFiles(hp);
  1248. hp:=tmodule(hp.next);
  1249. end;
  1250. { write .def file }
  1251. if (cs_link_deffile in aktglobalswitches) then
  1252. deffile.writefile;
  1253. { finally we can create a executable }
  1254. if (not current_module.is_unit) then
  1255. begin
  1256. if DLLSource then
  1257. linker.MakeSharedLibrary
  1258. else
  1259. linker.MakeExecutable;
  1260. end;
  1261. end;
  1262. end;
  1263. end.
  1264. {
  1265. $Log$
  1266. Revision 1.134 2003-12-08 22:33:43 peter
  1267. * don't allow duplicate uses
  1268. * fix wrong circular dependency
  1269. Revision 1.133 2003/11/29 20:13:25 florian
  1270. * fixed several pi_do_call problems
  1271. Revision 1.132 2003/10/29 19:48:51 peter
  1272. * renamed mangeldname_prefix to make_mangledname and made it more
  1273. generic
  1274. * make_mangledname is now also used for internal threadvar/resstring
  1275. lists
  1276. * Add P$ in front of program modulename to prevent duplicated symbols
  1277. at assembler level, because the main program can have the same name
  1278. as a unit, see webtbs/tw1251b
  1279. Revision 1.131 2003/10/24 17:40:23 peter
  1280. * cleanup of the entry and exit code insertion
  1281. Revision 1.130 2003/10/22 15:22:33 peter
  1282. * fixed unitsym-globalsymtable relation so the uses of a unit
  1283. is counted correctly
  1284. Revision 1.129 2003/10/21 15:14:33 peter
  1285. * fixed memleak for initfinalcode
  1286. * exit from generatecode when there are already errors
  1287. Revision 1.128 2003/10/01 20:34:49 peter
  1288. * procinfo unit contains tprocinfo
  1289. * cginfo renamed to cgbase
  1290. * moved cgmessage to verbose
  1291. * fixed ppc and sparc compiles
  1292. Revision 1.127 2003/09/30 08:39:50 michael
  1293. + Patch from Wiktor Sywula for watcom support
  1294. Revision 1.126 2003/09/23 18:03:08 peter
  1295. * add missing release of main_proc
  1296. Revision 1.125 2003/09/23 17:56:05 peter
  1297. * locals and paras are allocated in the code generation
  1298. * tvarsym.localloc contains the location of para/local when
  1299. generating code for the current procedure
  1300. Revision 1.124 2003/09/09 20:59:27 daniel
  1301. * Adding register allocation order
  1302. Revision 1.123 2003/09/09 15:55:44 peter
  1303. * use register with least interferences in spillregister
  1304. Revision 1.122 2003/09/07 22:09:35 peter
  1305. * preparations for different default calling conventions
  1306. * various RA fixes
  1307. Revision 1.121 2003/09/05 17:41:12 florian
  1308. * merged Wiktor's Watcom patches in 1.1
  1309. Revision 1.120 2003/08/23 22:29:24 peter
  1310. * reload flagged units when interface is loaded
  1311. Revision 1.119 2003/08/21 14:47:41 peter
  1312. * remove convert_registers
  1313. Revision 1.118 2003/08/20 17:48:49 peter
  1314. * fixed stackalloc to not allocate localst.datasize twice
  1315. * order of stackalloc code fixed for implicit init/final
  1316. Revision 1.117 2003/08/20 09:07:00 daniel
  1317. * New register coding now mandatory, some more convert_registers calls
  1318. removed.
  1319. Revision 1.116 2003/07/23 11:04:15 jonas
  1320. * split en_exit_code into a part that may allocate a register and a part
  1321. that doesn't, so the former can be done before the register colouring
  1322. has been performed
  1323. Revision 1.115 2003/07/06 17:58:22 peter
  1324. * framepointer fixes for sparc
  1325. * parent framepointer code more generic
  1326. Revision 1.114 2003/06/13 21:19:31 peter
  1327. * current_procdef removed, use current_procinfo.procdef instead
  1328. Revision 1.113 2003/06/09 12:23:30 peter
  1329. * init/final of procedure data splitted from genentrycode
  1330. * use asmnode getposition to insert final at the correct position
  1331. als for the implicit try...finally
  1332. Revision 1.112 2003/06/07 20:26:32 peter
  1333. * re-resolving added instead of reloading from ppu
  1334. * tderef object added to store deref info for resolving
  1335. Revision 1.111 2003/06/03 20:21:45 mazen
  1336. - removed unneeded ifdefs
  1337. - removed unneeded cases for sparc and x86_64
  1338. Revision 1.110 2003/06/03 13:01:59 daniel
  1339. * Register allocator finished
  1340. Revision 1.109 2003/05/26 21:17:17 peter
  1341. * procinlinenode removed
  1342. * aktexit2label removed, fast exit removed
  1343. + tcallnode.inlined_pass_2 added
  1344. Revision 1.108 2003/05/25 10:27:12 peter
  1345. * moved Comment calls to messge file
  1346. Revision 1.107 2003/05/22 21:31:35 peter
  1347. * defer codegeneration for nested procedures
  1348. Revision 1.106 2003/05/15 18:58:53 peter
  1349. * removed selfpointer_offset, vmtpointer_offset
  1350. * tvarsym.adjusted_address
  1351. * address in localsymtable is now in the real direction
  1352. * removed some obsolete globals
  1353. Revision 1.105 2003/05/11 19:31:28 florian
  1354. * fixed implicit init/final code for units, stack frame was wrong for ppc
  1355. Revision 1.104 2003/04/28 21:19:02 peter
  1356. * fix stabs generation for implicit initfinal
  1357. Revision 1.103 2003/04/27 11:21:34 peter
  1358. * aktprocdef renamed to current_procinfo.procdef
  1359. * procinfo renamed to current_procinfo
  1360. * procinfo will now be stored in current_module so it can be
  1361. cleaned up properly
  1362. * gen_main_procsym changed to create_main_proc and release_main_proc
  1363. to also generate a tprocinfo structure
  1364. * fixed unit implicit initfinal
  1365. Revision 1.102 2003/04/27 07:29:50 peter
  1366. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1367. a new procdef declaration
  1368. * aktprocsym removed
  1369. * lexlevel removed, use symtable.symtablelevel instead
  1370. * implicit init/final code uses the normal genentry/genexit
  1371. * funcret state checking updated for new funcret handling
  1372. Revision 1.101 2003/04/23 12:35:34 florian
  1373. * fixed several issues with powerpc
  1374. + applied a patch from Jonas for nested function calls (PowerPC only)
  1375. * ...
  1376. Revision 1.100 2003/04/12 15:13:03 peter
  1377. * Use the original unitname when defining a unitsym
  1378. Revision 1.99 2003/03/23 23:21:42 hajny
  1379. + emx target added
  1380. Revision 1.98 2003/03/17 22:20:08 peter
  1381. *** empty log message ***
  1382. Revision 1.97 2003/03/17 13:36:39 peter
  1383. * fix import linking under linux
  1384. Revision 1.96 2003/02/19 22:00:14 daniel
  1385. * Code generator converted to new register notation
  1386. - Horribily outdated todo.txt removed
  1387. Revision 1.95 2003/02/06 22:36:55 mazen
  1388. * fixing bug related to errornous program main entry stack frame
  1389. Revision 1.94 2003/01/30 21:46:20 peter
  1390. * tai_const_symbol.createdataname added
  1391. Revision 1.93 2003/01/11 11:19:54 hajny
  1392. * correction from rev. 1.88 put back
  1393. Revision 1.92 2003/01/08 18:43:56 daniel
  1394. * Tregister changed into a record
  1395. Revision 1.91 2003/01/05 13:36:53 florian
  1396. * x86-64 compiles
  1397. + very basic support for float128 type (x86-64 only)
  1398. Revision 1.90 2002/12/29 18:17:23 peter
  1399. * insert unitsym with the name as specified in the uses list
  1400. Revision 1.89 2002/12/29 14:57:50 peter
  1401. * unit loading changed to first register units and load them
  1402. afterwards. This is needed to support uses xxx in yyy correctly
  1403. * unit dependency check fixed
  1404. Revision 1.88 2002/12/27 19:09:33 hajny
  1405. * another (hopefully final ;-) ) fix for not linked import libraries for units with no code
  1406. Revision 1.87 2002/12/24 23:32:56 peter
  1407. * Use FixFilename for specified unit sourcefile in uses
  1408. Revision 1.86 2002/12/06 16:56:58 peter
  1409. * only compile cs_fp_emulation support when cpufpuemu is defined
  1410. * define cpufpuemu for m68k only
  1411. Revision 1.85 2002/11/30 21:32:24 carl
  1412. + Add loading of softfpu in emulation mode
  1413. + Correct routine call for softfpu
  1414. * Extended type must also be defined even with softfpu
  1415. Revision 1.84 2002/11/15 01:58:53 peter
  1416. * merged changes from 1.0.7 up to 04-11
  1417. - -V option for generating bug report tracing
  1418. - more tracing for option parsing
  1419. - errors for cdecl and high()
  1420. - win32 import stabs
  1421. - win32 records<=8 are returned in eax:edx (turned off by default)
  1422. - heaptrc update
  1423. - more info for temp management in .s file with EXTDEBUG
  1424. Revision 1.83 2002/11/09 15:33:26 carl
  1425. * major alignment updates
  1426. Revision 1.82 2002/10/16 06:32:52 michael
  1427. + Renamed thread unit to systhrds
  1428. Revision 1.81 2002/10/14 19:42:34 peter
  1429. * only use init tables for threadvars
  1430. Revision 1.80 2002/10/06 19:41:30 peter
  1431. * Add finalization of typed consts
  1432. * Finalization of globals in the main program
  1433. Revision 1.79 2002/09/09 17:34:15 peter
  1434. * tdicationary.replace added to replace and item in a dictionary. This
  1435. is only allowed for the same name
  1436. * varsyms are inserted in symtable before the types are parsed. This
  1437. fixes the long standing "var longint : longint" bug
  1438. - consume_idlist and idstringlist removed. The loops are inserted
  1439. at the callers place and uses the symtable for duplicate id checking
  1440. Revision 1.78 2002/09/07 15:25:07 peter
  1441. * old logs removed and tabs fixed
  1442. Revision 1.77 2002/09/03 16:26:27 daniel
  1443. * Make Tprocdef.defs protected
  1444. Revision 1.76 2002/09/02 18:46:26 peter
  1445. * insert PASCALMAIN in library for Win32 only
  1446. Revision 1.75 2002/08/31 15:59:30 florian
  1447. + HEAP* stuff must be generated for Linux/PPC as well
  1448. + direct assembler reader searches now global and static symtables as well
  1449. Revision 1.74 2002/08/25 19:25:20 peter
  1450. * sym.insert_in_data removed
  1451. * symtable.insertvardata/insertconstdata added
  1452. * removed insert_in_data call from symtable.insert, it needs to be
  1453. called separatly. This allows to deref the address calculation
  1454. * procedures now calculate the parast addresses after the procedure
  1455. directives are parsed. This fixes the cdecl parast problem
  1456. * push_addr_param has an extra argument that specifies if cdecl is used
  1457. or not
  1458. Revision 1.73 2002/08/18 20:06:25 peter
  1459. * inlining is now also allowed in interface
  1460. * renamed write/load to ppuwrite/ppuload
  1461. * tnode storing in ppu
  1462. * nld,ncon,nbas are already updated for storing in ppu
  1463. Revision 1.72 2002/08/17 09:23:39 florian
  1464. * first part of procinfo rewrite
  1465. Revision 1.71 2002/08/11 13:24:12 peter
  1466. * saving of asmsymbols in ppu supported
  1467. * asmsymbollist global is removed and moved into a new class
  1468. tasmlibrarydata that will hold the info of a .a file which
  1469. corresponds with a single module. Added librarydata to tmodule
  1470. to keep the library info stored for the module. In the future the
  1471. objectfiles will also be stored to the tasmlibrarydata class
  1472. * all getlabel/newasmsymbol and friends are moved to the new class
  1473. Revision 1.70 2002/08/10 14:46:29 carl
  1474. + moved target_cpu_string to cpuinfo
  1475. * renamed asmmode enum.
  1476. * assembler reader has now less ifdef's
  1477. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1478. Revision 1.69 2002/07/26 21:15:41 florian
  1479. * rewrote the system handling
  1480. Revision 1.68 2002/07/04 20:43:01 florian
  1481. * first x86-64 patches
  1482. Revision 1.67 2002/07/01 18:46:25 peter
  1483. * internal linker
  1484. * reorganized aasm layer
  1485. Revision 1.66 2002/05/16 19:46:43 carl
  1486. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1487. + try to fix temp allocation (still in ifdef)
  1488. + generic constructor calls
  1489. + start of tassembler / tmodulebase class cleanup
  1490. Revision 1.65 2002/05/14 19:34:49 peter
  1491. * removed old logs and updated copyright year
  1492. Revision 1.64 2002/05/12 16:53:09 peter
  1493. * moved entry and exitcode to ncgutil and cgobj
  1494. * foreach gets extra argument for passing local data to the
  1495. iterator function
  1496. * -CR checks also class typecasts at runtime by changing them
  1497. into as
  1498. * fixed compiler to cycle with the -CR option
  1499. * fixed stabs with elf writer, finally the global variables can
  1500. be watched
  1501. * removed a lot of routines from cga unit and replaced them by
  1502. calls to cgobj
  1503. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1504. u32bit then the other is typecasted also to u32bit without giving
  1505. a rangecheck warning/error.
  1506. * fixed pascal calling method with reversing also the high tree in
  1507. the parast, detected by tcalcst3 test
  1508. Revision 1.63 2002/05/06 19:54:50 carl
  1509. + added more patches from Mazen for SPARC port
  1510. Revision 1.62 2002/04/20 21:32:24 carl
  1511. + generic FPC_CHECKPOINTER
  1512. + first parameter offset in stack now portable
  1513. * rename some constants
  1514. + move some cpu stuff to other units
  1515. - remove unused constents
  1516. * fix stacksize for some targets
  1517. * fix generic size problems which depend now on EXTEND_SIZE constant
  1518. Revision 1.61 2002/04/19 15:46:02 peter
  1519. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1520. in most cases and not written to the ppu
  1521. * add mangeledname_prefix() routine to generate the prefix of
  1522. manglednames depending on the current procedure, object and module
  1523. * removed static procprefix since the mangledname is now build only
  1524. on demand from tprocdef.mangledname
  1525. Revision 1.60 2002/04/14 16:53:10 carl
  1526. + align code section and data section according to alignment rules
  1527. Revision 1.59 2002/04/07 17:58:38 carl
  1528. + generic stack checking
  1529. Revision 1.58 2002/04/04 19:06:03 peter
  1530. * removed unused units
  1531. * use tlocation.size in cg.a_*loc*() routines
  1532. }