pmodules.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmcpu,aasmbase,
  30. cgbase,cpuinfo,cgobj,
  31. nbas,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,procinfo,
  34. dwarf,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure fixseg(p:TAAsmoutput;sec:TAsmSectionType);
  40. begin
  41. maybe_new_object_file(p);
  42. p.insert(Tai_section.Create(sec,'',0));
  43. end;
  44. procedure create_objectfile;
  45. var
  46. DLLScanner : TDLLScanner;
  47. s : string;
  48. KeepShared : TStringList;
  49. begin
  50. { try to create import entries from system dlls }
  51. if target_info.DllScanSupported and
  52. (not current_module.linkOtherSharedLibs.Empty) then
  53. begin
  54. { Init DLLScanner }
  55. if assigned(CDLLScanner[target_info.system]) then
  56. DLLScanner:=CDLLScanner[target_info.system].Create
  57. else
  58. internalerror(200104121);
  59. KeepShared:=TStringList.Create;
  60. { Walk all shared libs }
  61. While not current_module.linkOtherSharedLibs.Empty do
  62. begin
  63. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  64. if not DLLScanner.scan(s) then
  65. KeepShared.Concat(s);
  66. end;
  67. DLLscanner.Free;
  68. { Recreate import section }
  69. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  70. begin
  71. if assigned(importssection)then
  72. importssection.clear
  73. else
  74. importssection:=taasmoutput.Create;
  75. importlib.generatelib;
  76. end;
  77. { Readd the not processed files }
  78. while not KeepShared.Empty do
  79. begin
  80. s:=KeepShared.GetFirst;
  81. current_module.linkOtherSharedLibs.add(s,link_allways);
  82. end;
  83. KeepShared.Free;
  84. end;
  85. { create the .s file and assemble it }
  86. GenerateAsm(false);
  87. { Also create a smartlinked version ? }
  88. if (cs_create_smart in aktmoduleswitches) then
  89. begin
  90. { regenerate the importssection for win32 }
  91. if assigned(importssection) and
  92. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  93. begin
  94. importsSection.clear;
  95. importlib.generatesmartlib;
  96. end;
  97. GenerateAsm(true);
  98. if (af_needar in target_asm.flags) then
  99. Linker.MakeStaticLibrary;
  100. end;
  101. { resource files }
  102. CompileResourceFiles;
  103. end;
  104. procedure insertobjectfile;
  105. { Insert the used object file for this unit in the used list for this unit }
  106. begin
  107. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  108. current_module.flags:=current_module.flags or uf_static_linked;
  109. if (cs_create_smart in aktmoduleswitches) then
  110. begin
  111. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  112. current_module.flags:=current_module.flags or uf_smart_linked;
  113. end;
  114. end;
  115. procedure create_dwarf;
  116. begin
  117. dwarflist:=taasmoutput.create;
  118. { Call frame information }
  119. if (tf_needs_dwarf_cfi in target_info.flags) and
  120. (af_supports_dwarf in target_asm.flags) then
  121. dwarfcfi.generate_code(dwarflist);
  122. end;
  123. procedure insertsegment;
  124. var
  125. oldaktfilepos : tfileposinfo;
  126. begin
  127. { Insert Ident of the compiler }
  128. if (not (cs_create_smart in aktmoduleswitches))
  129. {$ifndef EXTDEBUG}
  130. and (not current_module.is_unit)
  131. {$endif}
  132. then
  133. begin
  134. { align the first data }
  135. dataSegment.insert(Tai_align.Create(const_align(32)));
  136. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  137. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  138. end;
  139. { align code segment }
  140. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  141. { Insert start and end of sections }
  142. fixseg(codesegment,sec_code);
  143. fixseg(datasegment,sec_data);
  144. fixseg(bsssegment,sec_bss);
  145. { we should use .rdata section for these two no ?
  146. .rdata is a read only data section (PM) }
  147. fixseg(rttilist,sec_data);
  148. fixseg(consts,sec_data);
  149. fixseg(picdata,sec_data);
  150. if assigned(resourcestringlist) then
  151. fixseg(resourcestringlist,sec_data);
  152. {$ifdef GDB}
  153. if assigned(debuglist) then
  154. begin
  155. oldaktfilepos:=aktfilepos;
  156. aktfilepos.line:=0;
  157. debugList.insert(Tai_symbol.Createname('gcc2_compiled',AT_DATA,0));
  158. debugList.insert(Tai_symbol.Createname('fpc_compiled',AT_DATA,0));
  159. fixseg(debuglist,sec_code);
  160. aktfilepos:=oldaktfilepos;
  161. end;
  162. {$endif GDB}
  163. end;
  164. procedure InsertThreadvarTablesTable;
  165. var
  166. hp : tused_unit;
  167. ltvTables : taasmoutput;
  168. count : longint;
  169. begin
  170. ltvTables:=TAAsmOutput.Create;
  171. count:=0;
  172. hp:=tused_unit(usedunits.first);
  173. while assigned(hp) do
  174. begin
  175. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  176. begin
  177. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,0));
  178. inc(count);
  179. end;
  180. hp:=tused_unit(hp.next);
  181. end;
  182. { Add program threadvars, if any }
  183. If (current_module.flags and uf_threadvars)=uf_threadvars then
  184. begin
  185. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,0));
  186. inc(count);
  187. end;
  188. { TableCount }
  189. ltvTables.insert(Tai_const.Create_32bit(count));
  190. ltvTables.insert(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  191. ltvTables.insert(Tai_align.Create(const_align(sizeof(aint))));
  192. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  193. { insert in data segment }
  194. maybe_new_object_file(dataSegment);
  195. dataSegment.concatlist(ltvTables);
  196. ltvTables.free;
  197. end;
  198. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  199. var
  200. ltvTable : taasmoutput;
  201. begin
  202. ltvTable:=taasmoutput(arg);
  203. if (tsym(p).typ=varsym) and
  204. (vo_is_thread_var in tvarsym(p).varoptions) then
  205. begin
  206. { address of threadvar }
  207. ltvTable.concat(tai_const.Createname(tvarsym(p).mangledname,AT_DATA,0));
  208. { size of threadvar }
  209. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  210. end;
  211. end;
  212. procedure InsertThreadvars;
  213. var
  214. s : string;
  215. ltvTable : TAAsmoutput;
  216. begin
  217. ltvTable:=TAAsmoutput.create;
  218. if assigned(current_module.globalsymtable) then
  219. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  220. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  221. if ltvTable.first<>nil then
  222. begin
  223. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  224. { add begin and end of the list }
  225. ltvTable.insert(tai_symbol.Createname_global(s,AT_DATA,0));
  226. ltvTable.concat(tai_const.create_sym(nil)); { end of list marker }
  227. ltvTable.concat(tai_symbol_end.createname(s));
  228. maybe_new_object_file(dataSegment);
  229. dataSegment.concatlist(ltvTable);
  230. current_module.flags:=current_module.flags or uf_threadvars;
  231. end;
  232. ltvTable.Free;
  233. end;
  234. Procedure InsertResourceTablesTable;
  235. var
  236. hp : tused_unit;
  237. ResourceStringTables : taasmoutput;
  238. count : longint;
  239. begin
  240. ResourceStringTables:=TAAsmOutput.Create;
  241. count:=0;
  242. hp:=tused_unit(usedunits.first);
  243. while assigned(hp) do
  244. begin
  245. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  246. begin
  247. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  248. inc(count);
  249. end;
  250. hp:=tused_unit(hp.next);
  251. end;
  252. { Add program resources, if any }
  253. If ResourceStringList<>Nil then
  254. begin
  255. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  256. Inc(Count);
  257. end;
  258. { TableCount }
  259. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  260. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  261. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  262. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  263. { insert in data segment }
  264. maybe_new_object_file(dataSegment);
  265. dataSegment.concatlist(ResourceStringTables);
  266. ResourceStringTables.free;
  267. end;
  268. procedure InsertInitFinalTable;
  269. var
  270. hp : tused_unit;
  271. unitinits : taasmoutput;
  272. count : longint;
  273. begin
  274. unitinits:=TAAsmOutput.Create;
  275. count:=0;
  276. hp:=tused_unit(usedunits.first);
  277. while assigned(hp) do
  278. begin
  279. { call the unit init code and make it external }
  280. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  281. begin
  282. if (hp.u.flags and uf_init)<>0 then
  283. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  284. else
  285. unitinits.concat(Tai_const.Create_sym(nil));
  286. if (hp.u.flags and uf_finalize)<>0 then
  287. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  288. else
  289. unitinits.concat(Tai_const.Create_sym(nil));
  290. inc(count);
  291. end;
  292. hp:=tused_unit(hp.next);
  293. end;
  294. { Insert initialization/finalization of the program }
  295. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  296. begin
  297. if (current_module.flags and uf_init)<>0 then
  298. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  299. else
  300. unitinits.concat(Tai_const.Create_sym(nil));
  301. if (current_module.flags and uf_finalize)<>0 then
  302. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  303. else
  304. unitinits.concat(Tai_const.Create_sym(nil));
  305. inc(count);
  306. end;
  307. { TableCount,InitCount }
  308. unitinits.insert(Tai_const.Create_32bit(0));
  309. unitinits.insert(Tai_const.Create_32bit(count));
  310. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  311. unitinits.insert(Tai_align.Create(const_align(4)));
  312. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  313. { insert in data segment }
  314. maybe_new_object_file(dataSegment);
  315. dataSegment.concatlist(unitinits);
  316. unitinits.free;
  317. end;
  318. procedure insertheap;
  319. begin
  320. maybe_new_object_file(bssSegment);
  321. maybe_new_object_file(dataSegment);
  322. { On the Macintosh Classic M68k Architecture
  323. The Heap variable is simply a POINTER to the
  324. real HEAP. The HEAP must be set up by the RTL
  325. and must store the pointer in this value.
  326. On OS/2 the heap is also intialized by the RTL. We do
  327. not output a pointer }
  328. case target_info.system of
  329. system_i386_OS2:
  330. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  331. system_i386_EMX:
  332. ;
  333. system_powerpc_macos:
  334. ;
  335. system_i386_watcom:
  336. ;
  337. system_alpha_linux:
  338. ;
  339. system_m68k_Mac:
  340. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  341. system_m68k_PalmOS:
  342. ;
  343. else
  344. begin
  345. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  346. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  347. end;
  348. end;
  349. {$ifdef m68k}
  350. if target_info.system<>system_m68k_PalmOS then
  351. begin
  352. dataSegment.concat(Tai_align.Create(const_align(4)));
  353. dataSegment.concat(Tai_symbol.Createname_global('HEAPSIZE',AT_DATA,4));
  354. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  355. end;
  356. {$else m68k}
  357. dataSegment.concat(Tai_align.Create(const_align(4)));
  358. dataSegment.concat(Tai_symbol.Createname_global('HEAPSIZE',AT_DATA,4));
  359. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  360. {$endif m68k}
  361. end;
  362. procedure insertstacklength;
  363. begin
  364. { stacksize can be specified and is now simulated }
  365. dataSegment.concat(Tai_align.Create(const_align(4)));
  366. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  367. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  368. end;
  369. procedure loaddefaultunits;
  370. procedure AddUnit(const s:string);
  371. var
  372. hp : tppumodule;
  373. unitsym : tunitsym;
  374. begin
  375. { load unit }
  376. hp:=registerunit(current_module,s,'');
  377. hp.loadppu;
  378. hp.adddependency(current_module);
  379. { add to symtable stack }
  380. tsymtable(hp.globalsymtable).next:=symtablestack;
  381. symtablestack:=hp.globalsymtable;
  382. { insert unitsym }
  383. unitsym:=tunitsym.create(s,hp.globalsymtable);
  384. inc(unitsym.refs);
  385. refsymtable.insert(unitsym);
  386. { add to used units }
  387. current_module.addusedunit(hp,false,unitsym);
  388. end;
  389. begin
  390. { are we compiling the system unit? }
  391. if (cs_compilesystem in aktmoduleswitches) then
  392. begin
  393. { create system defines }
  394. createconstdefs;
  395. { we don't need to reset anything, it's already done in parser.pas }
  396. exit;
  397. end;
  398. { insert the system unit, it is allways the first }
  399. Symtablestack:=nil;
  400. AddUnit('System');
  401. SystemUnit:=TGlobalSymtable(Symtablestack);
  402. { read default constant definitions }
  403. make_ref:=false;
  404. readconstdefs;
  405. make_ref:=true;
  406. { Set the owner of errorsym and errortype to symtable to
  407. prevent crashes when accessing .owner }
  408. generrorsym.owner:=systemunit;
  409. generrortype.def.owner:=systemunit;
  410. {$ifdef cpufpemu}
  411. { Floating point emulation unit? }
  412. if (cs_fp_emulation in aktmoduleswitches) then
  413. AddUnit('SoftFpu');
  414. {$endif cpufpemu}
  415. { Thread support unit? }
  416. if (cs_threading in aktmoduleswitches) then
  417. AddUnit('SysThrds');
  418. { Objpas unit? }
  419. if m_objpas in aktmodeswitches then
  420. AddUnit('ObjPas');
  421. { Macpas unit? }
  422. if m_mac in aktmodeswitches then
  423. AddUnit('MacPas');
  424. { Profile unit? Needed for go32v2 only }
  425. if (cs_profile in aktmoduleswitches) and
  426. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  427. AddUnit('Profile');
  428. { Units only required for main module }
  429. if not(current_module.is_unit) then
  430. begin
  431. { Heaptrc unit }
  432. if (cs_gdb_heaptrc in aktglobalswitches) then
  433. AddUnit('HeapTrc');
  434. { Lineinfo unit }
  435. if (cs_gdb_lineinfo in aktglobalswitches) then
  436. AddUnit('LineInfo');
  437. { Lineinfo unit }
  438. if (cs_gdb_valgrind in aktglobalswitches) then
  439. AddUnit('CMem');
  440. end;
  441. { save default symtablestack }
  442. defaultsymtablestack:=symtablestack;
  443. end;
  444. procedure loadunits;
  445. var
  446. s,sorg : stringid;
  447. fn : string;
  448. pu : tused_unit;
  449. hp2 : tmodule;
  450. hp3 : tsymtable;
  451. unitsym : tunitsym;
  452. begin
  453. consume(_USES);
  454. {$ifdef DEBUG}
  455. test_symtablestack;
  456. {$endif DEBUG}
  457. repeat
  458. s:=pattern;
  459. sorg:=orgpattern;
  460. consume(_ID);
  461. { support "<unit> in '<file>'" construct, but not for tp7 }
  462. if not(m_tp7 in aktmodeswitches) then
  463. begin
  464. if try_to_consume(_OP_IN) then
  465. fn:=FixFileName(get_stringconst)
  466. else
  467. fn:='';
  468. end;
  469. { Give a warning if objpas is loaded }
  470. if s='OBJPAS' then
  471. Message(parser_w_no_objpas_use_mode);
  472. { Using the unit itself is not possible }
  473. if (s<>current_module.modulename^) then
  474. begin
  475. { check if the unit is already used }
  476. hp2:=nil;
  477. pu:=tused_unit(current_module.used_units.first);
  478. while assigned(pu) do
  479. begin
  480. if (pu.u.modulename^=s) then
  481. begin
  482. hp2:=pu.u;
  483. break;
  484. end;
  485. pu:=tused_unit(pu.next);
  486. end;
  487. if not assigned(hp2) then
  488. hp2:=registerunit(current_module,sorg,fn)
  489. else
  490. Message1(sym_e_duplicate_id,s);
  491. { Create unitsym, we need to use the name as specified, we
  492. can not use the modulename because that can be different
  493. when -Un is used }
  494. unitsym:=tunitsym.create(sorg,nil);
  495. refsymtable.insert(unitsym);
  496. { the current module uses the unit hp2 }
  497. current_module.addusedunit(hp2,true,unitsym);
  498. end
  499. else
  500. Message1(sym_e_duplicate_id,s);
  501. if token=_COMMA then
  502. begin
  503. pattern:='';
  504. consume(_COMMA);
  505. end
  506. else
  507. break;
  508. until false;
  509. consume(_SEMICOLON);
  510. { Load the units }
  511. pu:=tused_unit(current_module.used_units.first);
  512. while assigned(pu) do
  513. begin
  514. { Only load the units that are in the current
  515. (interface/implementation) uses clause }
  516. if pu.in_uses and
  517. (pu.in_interface=current_module.in_interface) then
  518. begin
  519. tppumodule(pu.u).loadppu;
  520. { is our module compiled? then we can stop }
  521. if current_module.state=ms_compiled then
  522. exit;
  523. { add this unit to the dependencies }
  524. pu.u.adddependency(current_module);
  525. { save crc values }
  526. pu.checksum:=pu.u.crc;
  527. pu.interface_checksum:=pu.u.interface_crc;
  528. { connect unitsym to the globalsymtable of the unit }
  529. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  530. { increase refs of the unitsym when the unit contains
  531. initialization/finalization code so it doesn't trigger
  532. the unit not used hint }
  533. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  534. inc(pu.unitsym.refs);
  535. end;
  536. pu:=tused_unit(pu.next);
  537. end;
  538. { set the symtable to systemunit so it gets reorderd correctly,
  539. then insert the units in the symtablestack }
  540. pu:=tused_unit(current_module.used_units.first);
  541. symtablestack:=defaultsymtablestack;
  542. while assigned(pu) do
  543. begin
  544. if pu.in_uses then
  545. begin
  546. { Reinsert in symtablestack }
  547. hp3:=symtablestack;
  548. while assigned(hp3) do
  549. begin
  550. { insert units only once ! }
  551. if pu.u.globalsymtable=hp3 then
  552. break;
  553. hp3:=hp3.next;
  554. { unit isn't inserted }
  555. if hp3=nil then
  556. begin
  557. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  558. symtablestack:=tsymtable(pu.u.globalsymtable);
  559. {$ifdef DEBUG}
  560. test_symtablestack;
  561. {$endif DEBUG}
  562. end;
  563. end;
  564. end;
  565. pu:=tused_unit(pu.next);
  566. end;
  567. end;
  568. {$IfDef GDB}
  569. procedure write_gdb_info;
  570. procedure reset_unit_type_info;
  571. var
  572. hp : tmodule;
  573. begin
  574. hp:=tmodule(loaded_units.first);
  575. while assigned(hp) do
  576. begin
  577. hp.is_stab_written:=false;
  578. hp:=tmodule(hp.next);
  579. end;
  580. end;
  581. procedure write_used_unit_type_info(hp:tmodule);
  582. var
  583. pu : tused_unit;
  584. begin
  585. pu:=tused_unit(hp.used_units.first);
  586. while assigned(pu) do
  587. begin
  588. if not pu.u.is_stab_written then
  589. begin
  590. { prevent infinte loop for circular dependencies }
  591. pu.u.is_stab_written:=true;
  592. { write type info from used units, use a depth first
  593. strategy to reduce the recursion in writing all
  594. dependent stabs }
  595. write_used_unit_type_info(pu.u);
  596. if assigned(pu.u.globalsymtable) then
  597. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  598. end;
  599. pu:=tused_unit(pu.next);
  600. end;
  601. end;
  602. var
  603. vardebuglist : taasmoutput;
  604. begin
  605. if not (cs_debuginfo in aktmoduleswitches) then
  606. exit;
  607. { include symbol that will be referenced from the program to be sure to
  608. include this debuginfo .o file }
  609. if current_module.is_unit then
  610. begin
  611. current_module.flags:=current_module.flags or uf_has_debuginfo;
  612. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.globalsymtable,''),AT_DATA,0));
  613. end
  614. else
  615. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  616. { first write all global/local symbols to a temp list. This will flag
  617. all required tdefs. Afterwards this list will be added }
  618. vardebuglist:=taasmoutput.create;
  619. new_section(vardebuglist,sec_data,'',0);
  620. if assigned(current_module.globalsymtable) then
  621. tglobalsymtable(current_module.globalsymtable).concatstabto(vardebuglist);
  622. if assigned(current_module.localsymtable) then
  623. tstaticsymtable(current_module.localsymtable).concatstabto(vardebuglist);
  624. { reset unit type info flag }
  625. reset_unit_type_info;
  626. { write used types from the used units }
  627. write_used_unit_type_info(current_module);
  628. { last write the types from this unit }
  629. if assigned(current_module.globalsymtable) then
  630. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  631. if assigned(current_module.localsymtable) then
  632. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  633. { now all defs have a type in the debuglist, we now can add the vardebuglist since
  634. all references to defs can be solved }
  635. debuglist.concatlist(vardebuglist);
  636. vardebuglist.free;
  637. { include files }
  638. if (cs_gdb_dbx in aktglobalswitches) then
  639. begin
  640. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  641. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  642. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  643. debugList.concat(Tai_stabs.Create(strpnew('"'+
  644. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  645. tostr(N_EINCL)+',0,0,0')));
  646. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  647. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  648. do_count_dbx:=false;
  649. end;
  650. end;
  651. {$EndIf GDB}
  652. procedure reset_all_defs;
  653. procedure reset_used_unit_defs(hp:tmodule);
  654. var
  655. pu : tused_unit;
  656. begin
  657. pu:=tused_unit(hp.used_units.first);
  658. while assigned(pu) do
  659. begin
  660. if not pu.u.is_reset then
  661. begin
  662. { prevent infinte loop for circular dependencies }
  663. pu.u.is_reset:=true;
  664. if assigned(pu.u.globalsymtable) then
  665. begin
  666. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  667. reset_used_unit_defs(pu.u);
  668. end;
  669. end;
  670. pu:=tused_unit(pu.next);
  671. end;
  672. end;
  673. var
  674. hp2 : tmodule;
  675. begin
  676. hp2:=tmodule(loaded_units.first);
  677. while assigned(hp2) do
  678. begin
  679. hp2.is_reset:=false;
  680. hp2:=tmodule(hp2.next);
  681. end;
  682. reset_used_unit_defs(current_module);
  683. end;
  684. procedure free_localsymtables(st:tsymtable);
  685. var
  686. def : tstoreddef;
  687. pd : tprocdef;
  688. begin
  689. def:=tstoreddef(st.defindex.first);
  690. while assigned(def) do
  691. begin
  692. if def.deftype=procdef then
  693. begin
  694. pd:=tprocdef(def);
  695. if assigned(pd.localst) and
  696. (pd.localst.symtabletype<>staticsymtable) and
  697. not((pd.proccalloption=pocall_inline) or
  698. ((current_module.flags and uf_local_browser)<>0)) then
  699. begin
  700. free_localsymtables(pd.localst);
  701. pd.localst.free;
  702. pd.localst:=nil;
  703. end;
  704. end;
  705. def:=tstoreddef(def.indexnext);
  706. end;
  707. end;
  708. procedure parse_implementation_uses;
  709. begin
  710. if token=_USES then
  711. begin
  712. loadunits;
  713. {$ifdef DEBUG}
  714. test_symtablestack;
  715. {$endif DEBUG}
  716. end;
  717. end;
  718. procedure setupglobalswitches;
  719. begin
  720. { can't have local browser when no global browser }
  721. if (cs_local_browser in aktmoduleswitches) and
  722. not(cs_browser in aktmoduleswitches) then
  723. exclude(aktmoduleswitches,cs_local_browser);
  724. { define a symbol in delphi,objfpc,tp,gpc mode }
  725. if (m_delphi in aktmodeswitches) then
  726. current_scanner.def_macro('FPC_DELPHI')
  727. else
  728. if (m_tp7 in aktmodeswitches) then
  729. current_scanner.def_macro('FPC_TP')
  730. else
  731. if (m_objfpc in aktmodeswitches) then
  732. current_scanner.def_macro('FPC_OBJFPC')
  733. else
  734. if (m_gpc in aktmodeswitches) then
  735. current_scanner.def_macro('FPC_GPC')
  736. else
  737. if (m_mac in aktmodeswitches) then
  738. current_scanner.def_macro('FPC_MACPAS');
  739. end;
  740. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  741. var
  742. stt : tsymtable;
  743. ps : tprocsym;
  744. pd : tprocdef;
  745. begin
  746. { there should be no current_procinfo available }
  747. if assigned(current_procinfo) then
  748. internalerror(200304275);
  749. {Generate a procsym for main}
  750. make_ref:=false;
  751. { try to insert in in static symtable ! }
  752. stt:=symtablestack;
  753. symtablestack:=st;
  754. { generate procsym }
  755. ps:=tprocsym.create('$'+name);
  756. { main are allways used }
  757. inc(ps.refs);
  758. symtablestack.insert(ps);
  759. pd:=tprocdef.create(main_program_level);
  760. include(pd.procoptions,po_public);
  761. pd.procsym:=ps;
  762. ps.addprocdef(pd);
  763. { restore symtable }
  764. make_ref:=true;
  765. symtablestack:=stt;
  766. { set procdef options }
  767. pd.proctypeoption:=potype;
  768. pd.proccalloption:=pocall_default;
  769. pd.forwarddef:=false;
  770. pd.setmangledname(target_info.cprefix+name);
  771. pd.aliasnames.insert(pd.mangledname);
  772. { We don't need is a local symtable. Change it into the static
  773. symtable }
  774. pd.localst.free;
  775. pd.localst:=st;
  776. { set procinfo and current_procinfo.procdef }
  777. current_procinfo:=cprocinfo.create(nil);
  778. current_module.procinfo:=current_procinfo;
  779. current_procinfo.procdef:=pd;
  780. { return procdef }
  781. create_main_proc:=pd;
  782. { main proc does always a call e.g. to init system unit }
  783. include(current_procinfo.flags,pi_do_call);
  784. end;
  785. procedure release_main_proc(pd:tprocdef);
  786. begin
  787. { this is a main proc, so there should be no parent }
  788. if not(assigned(current_procinfo)) or
  789. assigned(current_procinfo.parent) or
  790. not(current_procinfo.procdef=pd) then
  791. internalerror(200304276);
  792. { remove procinfo }
  793. current_module.procinfo:=nil;
  794. current_procinfo.free;
  795. current_procinfo:=nil;
  796. { remove localst as it was replaced by staticsymtable }
  797. pd.localst:=nil;
  798. end;
  799. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  800. var
  801. pd : tprocdef;
  802. begin
  803. { update module flags }
  804. current_module.flags:=current_module.flags or flag;
  805. { create procdef }
  806. case flag of
  807. uf_init :
  808. begin
  809. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  810. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  811. end;
  812. uf_finalize :
  813. begin
  814. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  815. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  816. end;
  817. else
  818. internalerror(200304253);
  819. end;
  820. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  821. tcgprocinfo(current_procinfo).generate_code;
  822. release_main_proc(pd);
  823. end;
  824. procedure proc_unit;
  825. function is_assembler_generated:boolean;
  826. begin
  827. is_assembler_generated:=(Errorcount=0) and
  828. not(
  829. codeSegment.empty and
  830. dataSegment.empty and
  831. bssSegment.empty and
  832. ((importssection=nil) or importsSection.empty) and
  833. ((resourcesection=nil) or resourceSection.empty) and
  834. ((resourcestringlist=nil) or resourcestringList.empty)
  835. );
  836. end;
  837. var
  838. main_file: tinputfile;
  839. st : tsymtable;
  840. unitst : tglobalsymtable;
  841. store_crc,store_interface_crc : cardinal;
  842. s1,s2 : ^string; {Saves stack space}
  843. force_init_final : boolean;
  844. pd : tprocdef;
  845. unitname8 : string[8];
  846. has_impl: boolean;
  847. begin
  848. consume(_UNIT);
  849. if compile_level=1 then
  850. Status.IsExe:=false;
  851. if token=_ID then
  852. begin
  853. { create filenames and unit name }
  854. main_file := current_scanner.inputfile;
  855. while assigned(main_file.next) do
  856. main_file := main_file.next;
  857. new(s1);
  858. s1^:=current_module.modulename^;
  859. current_module.SetFileName(main_file.path^+main_file.name^,true);
  860. current_module.SetModuleName(orgpattern);
  861. { check for system unit }
  862. new(s2);
  863. s2^:=upper(SplitName(main_file.name^));
  864. unitname8:=copy(current_module.modulename^,1,8);
  865. if (cs_check_unit_name in aktglobalswitches) and
  866. (
  867. not(
  868. (current_module.modulename^=s2^) or
  869. (
  870. (length(current_module.modulename^)>8) and
  871. (unitname8=s2^)
  872. )
  873. )
  874. or
  875. (
  876. (length(s1^)>8) and
  877. (s1^<>current_module.modulename^)
  878. )
  879. ) then
  880. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  881. if (current_module.modulename^='SYSTEM') then
  882. include(aktmoduleswitches,cs_compilesystem);
  883. dispose(s2);
  884. dispose(s1);
  885. end;
  886. consume(_ID);
  887. consume(_SEMICOLON);
  888. consume(_INTERFACE);
  889. { global switches are read, so further changes aren't allowed }
  890. current_module.in_global:=false;
  891. { handle the global switches }
  892. setupglobalswitches;
  893. message1(unit_u_loading_interface_units,current_module.modulename^);
  894. { update status }
  895. status.currentmodule:=current_module.realmodulename^;
  896. { maybe turn off m_objpas if we are compiling objpas }
  897. if (current_module.modulename^='OBJPAS') then
  898. exclude(aktmodeswitches,m_objpas);
  899. { maybe turn off m_mac if we are compiling macpas }
  900. if (current_module.modulename^='MACPAS') then
  901. exclude(aktmodeswitches,m_mac);
  902. parse_only:=true;
  903. { generate now the global symboltable }
  904. st:=tglobalsymtable.create(current_module.modulename^);
  905. refsymtable:=st;
  906. unitst:=tglobalsymtable(st);
  907. { define first as local to overcome dependency conflicts }
  908. current_module.localsymtable:=st;
  909. { the unit name must be usable as a unit specifier }
  910. { inside the unit itself (PM) }
  911. { this also forbids to have another symbol }
  912. { with the same name as the unit }
  913. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  914. { load default units, like the system unit }
  915. loaddefaultunits;
  916. { reset }
  917. make_ref:=true;
  918. { insert qualifier for the system unit (allows system.writeln) }
  919. if not(cs_compilesystem in aktmoduleswitches) then
  920. begin
  921. if token=_USES then
  922. begin
  923. loadunits;
  924. { has it been compiled at a higher level ?}
  925. if current_module.state=ms_compiled then
  926. exit;
  927. end;
  928. { ... but insert the symbol table later }
  929. st.next:=symtablestack;
  930. symtablestack:=st;
  931. end
  932. else
  933. { while compiling a system unit, some types are directly inserted }
  934. begin
  935. st.next:=symtablestack;
  936. symtablestack:=st;
  937. insert_intern_types(st);
  938. end;
  939. { now we know the place to insert the constants }
  940. constsymtable:=symtablestack;
  941. { move the global symtab from the temporary local to global }
  942. current_module.globalsymtable:=current_module.localsymtable;
  943. current_module.localsymtable:=nil;
  944. reset_all_defs;
  945. { number all units, so we know if a unit is used by this unit or
  946. needs to be added implicitly }
  947. current_module.numberunits;
  948. { ... parse the declarations }
  949. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  950. read_interface_declarations;
  951. { leave when we got an error }
  952. if (Errorcount>0) and not status.skip_error then
  953. begin
  954. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  955. status.skip_error:=true;
  956. exit;
  957. end;
  958. { Our interface is compiled, generate CRC and switch to implementation }
  959. if not(cs_compilesystem in aktmoduleswitches) and
  960. (Errorcount=0) then
  961. tppumodule(current_module).getppucrc;
  962. current_module.in_interface:=false;
  963. current_module.interface_compiled:=true;
  964. { First reload all units depending on our interface, we need to do this
  965. in the implementation part to prevent errorneous circular references }
  966. reload_flagged_units;
  967. { Parse the implementation section }
  968. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  969. has_impl:= false
  970. else
  971. begin
  972. consume(_IMPLEMENTATION);
  973. has_impl:= true;
  974. end;
  975. if has_impl then
  976. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  977. parse_only:=false;
  978. { generates static symbol table }
  979. st:=tstaticsymtable.create(current_module.modulename^);
  980. current_module.localsymtable:=st;
  981. { remove the globalsymtable from the symtable stack }
  982. { to reinsert it after loading the implementation units }
  983. symtablestack:=unitst.next;
  984. { we don't want implementation units symbols in unitsymtable !! PM }
  985. refsymtable:=st;
  986. { Read the implementation units }
  987. if has_impl then
  988. parse_implementation_uses;
  989. if current_module.state=ms_compiled then
  990. exit;
  991. { reset ranges/stabs in exported definitions }
  992. reset_all_defs;
  993. { All units are read, now give them a number }
  994. current_module.numberunits;
  995. { now we can change refsymtable }
  996. refsymtable:=st;
  997. { but reinsert the global symtable as lasts }
  998. unitst.next:=symtablestack;
  999. symtablestack:=unitst;
  1000. {$ifdef DEBUG}
  1001. test_symtablestack;
  1002. {$endif DEBUG}
  1003. constsymtable:=symtablestack;
  1004. {$ifdef Splitheap}
  1005. if testsplit then
  1006. begin
  1007. Split_Heap;
  1008. allow_special:=true;
  1009. Switch_to_temp_heap;
  1010. end;
  1011. { it will report all crossings }
  1012. allow_special:=false;
  1013. {$endif Splitheap}
  1014. if has_impl then
  1015. begin
  1016. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1017. if current_module.in_interface then
  1018. internalerror(200212285);
  1019. { Compile the unit }
  1020. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  1021. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1022. tcgprocinfo(current_procinfo).parse_body;
  1023. tcgprocinfo(current_procinfo).generate_code;
  1024. tcgprocinfo(current_procinfo).resetprocdef;
  1025. release_main_proc(pd);
  1026. end;
  1027. { if the unit contains ansi/widestrings, initialization and
  1028. finalization code must be forced }
  1029. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1030. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1031. { should we force unit initialization? }
  1032. { this is a hack, but how can it be done better ? }
  1033. if force_init_final and ((current_module.flags and uf_init)=0) then
  1034. gen_implicit_initfinal(uf_init,st);
  1035. { finalize? }
  1036. if has_impl and (token=_FINALIZATION) then
  1037. begin
  1038. { set module options }
  1039. current_module.flags:=current_module.flags or uf_finalize;
  1040. { Compile the finalize }
  1041. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1042. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1043. tcgprocinfo(current_procinfo).parse_body;
  1044. tcgprocinfo(current_procinfo).generate_code;
  1045. tcgprocinfo(current_procinfo).resetprocdef;
  1046. release_main_proc(pd);
  1047. end
  1048. else if force_init_final then
  1049. gen_implicit_initfinal(uf_finalize,st);
  1050. { the last char should always be a point }
  1051. consume(_POINT);
  1052. { Generate resoucestrings }
  1053. If ResourceStrings.ResStrCount>0 then
  1054. begin
  1055. ResourceStrings.CreateResourceStringList;
  1056. current_module.flags:=current_module.flags or uf_has_resources;
  1057. { only write if no errors found }
  1058. if (Errorcount=0) then
  1059. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1060. end;
  1061. if (Errorcount=0) then
  1062. begin
  1063. { tests, if all (interface) forwards are resolved }
  1064. tstoredsymtable(symtablestack).check_forwards;
  1065. { check if all private fields are used }
  1066. tstoredsymtable(symtablestack).allprivatesused;
  1067. { remove cross unit overloads }
  1068. tstoredsymtable(symtablestack).unchain_overloaded;
  1069. { test static symtable }
  1070. tstoredsymtable(st).allsymbolsused;
  1071. tstoredsymtable(st).allprivatesused;
  1072. tstoredsymtable(st).check_forwards;
  1073. tstoredsymtable(st).checklabels;
  1074. tstoredsymtable(st).unchain_overloaded;
  1075. { used units }
  1076. current_module.allunitsused;
  1077. end;
  1078. { leave when we got an error }
  1079. if (Errorcount>0) and not status.skip_error then
  1080. begin
  1081. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1082. status.skip_error:=true;
  1083. exit;
  1084. end;
  1085. { generate debuginfo }
  1086. {$ifdef GDB}
  1087. write_gdb_info;
  1088. {$endif GDB}
  1089. { generate a list of threadvars }
  1090. InsertThreadvars;
  1091. { generate imports }
  1092. if current_module.uses_imports then
  1093. importlib.generatelib;
  1094. { insert own objectfile, or say that it's in a library
  1095. (no check for an .o when loading) }
  1096. if is_assembler_generated then
  1097. insertobjectfile
  1098. else
  1099. current_module.flags:=current_module.flags or uf_no_link;
  1100. if cs_local_browser in aktmoduleswitches then
  1101. current_module.localsymtable:=refsymtable;
  1102. if is_assembler_generated then
  1103. begin
  1104. { create dwarf debuginfo }
  1105. create_dwarf;
  1106. { finish asmlist by adding segment starts }
  1107. insertsegment;
  1108. { assemble }
  1109. create_objectfile;
  1110. end;
  1111. { Write out the ppufile after the object file has been created }
  1112. store_interface_crc:=current_module.interface_crc;
  1113. store_crc:=current_module.crc;
  1114. if (Errorcount=0) then
  1115. tppumodule(current_module).writeppu;
  1116. if not(cs_compilesystem in aktmoduleswitches) then
  1117. if store_interface_crc<>current_module.interface_crc then
  1118. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1119. {$ifdef EXTDEBUG}
  1120. if not(cs_compilesystem in aktmoduleswitches) then
  1121. if (store_crc<>current_module.crc) and simplify_ppu then
  1122. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1123. {$endif EXTDEBUG}
  1124. { release all local symtables that are not needed anymore }
  1125. free_localsymtables(current_module.globalsymtable);
  1126. free_localsymtables(current_module.localsymtable);
  1127. { remove static symtable (=refsymtable) here to save some mem }
  1128. if not (cs_local_browser in aktmoduleswitches) then
  1129. begin
  1130. st.free;
  1131. current_module.localsymtable:=nil;
  1132. end;
  1133. { leave when we got an error }
  1134. if (Errorcount>0) and not status.skip_error then
  1135. begin
  1136. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1137. status.skip_error:=true;
  1138. exit;
  1139. end;
  1140. Message1(unit_u_finished_compiling,current_module.modulename^);
  1141. end;
  1142. procedure proc_program(islibrary : boolean);
  1143. var
  1144. main_file : tinputfile;
  1145. st : tsymtable;
  1146. hp,hp2 : tmodule;
  1147. pd : tprocdef;
  1148. begin
  1149. DLLsource:=islibrary;
  1150. Status.IsLibrary:=IsLibrary;
  1151. Status.IsExe:=true;
  1152. parse_only:=false;
  1153. { DLL defaults to create reloc info }
  1154. if islibrary then
  1155. begin
  1156. if not RelocSectionSetExplicitly then
  1157. RelocSection:=true;
  1158. end;
  1159. { relocation works only without stabs under win32 !! PM }
  1160. { internal assembler uses rva for stabs info
  1161. so it should work with relocated DLLs }
  1162. if RelocSection and
  1163. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1164. (target_info.assem<>as_i386_pecoff) then
  1165. begin
  1166. include(aktglobalswitches,cs_link_strip);
  1167. { Warning stabs info does not work with reloc section !! }
  1168. if cs_debuginfo in aktmoduleswitches then
  1169. begin
  1170. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1171. Message(parser_w_parser_win32_debug_needs_WN);
  1172. exclude(aktmoduleswitches,cs_debuginfo);
  1173. end;
  1174. end;
  1175. { get correct output names }
  1176. main_file := current_scanner.inputfile;
  1177. while assigned(main_file.next) do
  1178. main_file := main_file.next;
  1179. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1180. if islibrary then
  1181. begin
  1182. consume(_LIBRARY);
  1183. stringdispose(current_module.modulename);
  1184. current_module.modulename:=stringdup(pattern);
  1185. current_module.islibrary:=true;
  1186. exportlib.preparelib(pattern);
  1187. consume(_ID);
  1188. consume(_SEMICOLON);
  1189. end
  1190. else
  1191. { is there an program head ? }
  1192. if token=_PROGRAM then
  1193. begin
  1194. consume(_PROGRAM);
  1195. stringdispose(current_module.modulename);
  1196. stringdispose(current_module.realmodulename);
  1197. current_module.modulename:=stringdup(pattern);
  1198. current_module.realmodulename:=stringdup(orgpattern);
  1199. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1200. exportlib.preparelib(pattern);
  1201. consume(_ID);
  1202. if token=_LKLAMMER then
  1203. begin
  1204. consume(_LKLAMMER);
  1205. repeat
  1206. consume(_ID);
  1207. until not try_to_consume(_COMMA);
  1208. consume(_RKLAMMER);
  1209. end;
  1210. consume(_SEMICOLON);
  1211. end
  1212. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1213. exportlib.preparelib(current_module.modulename^);
  1214. { global switches are read, so further changes aren't allowed }
  1215. current_module.in_global:=false;
  1216. { setup things using the global switches }
  1217. setupglobalswitches;
  1218. { set implementation flag }
  1219. current_module.in_interface:=false;
  1220. current_module.interface_compiled:=true;
  1221. { insert after the unit symbol tables the static symbol table }
  1222. { of the program }
  1223. st:=tstaticsymtable.create(current_module.modulename^);;
  1224. current_module.localsymtable:=st;
  1225. refsymtable:=st;
  1226. { load standard units (system,objpas,profile unit) }
  1227. loaddefaultunits;
  1228. {Load the units used by the program we compile.}
  1229. if token=_USES then
  1230. loadunits;
  1231. { reset ranges/stabs in exported definitions }
  1232. reset_all_defs;
  1233. { All units are read, now give them a number }
  1234. current_module.numberunits;
  1235. {Insert the name of the main program into the symbol table.}
  1236. if current_module.realmodulename^<>'' then
  1237. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1238. { ...is also constsymtable, this is the symtable where }
  1239. { the elements of enumeration types are inserted }
  1240. constsymtable:=st;
  1241. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1242. { The program intialization needs an alias, so it can be called
  1243. from the bootstrap code.}
  1244. if islibrary or
  1245. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1246. begin
  1247. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1248. { Win32 startup code needs a single name }
  1249. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1250. pd.aliasnames.insert('PASCALMAIN');
  1251. end
  1252. else
  1253. begin
  1254. if (target_info.system = system_i386_netware) or
  1255. (target_info.system = system_i386_netwlibc) then
  1256. begin
  1257. pd:=create_main_proc('PASCALMAIN',potype_proginit,st); { main is need by the netware rtl }
  1258. end else
  1259. begin
  1260. pd:=create_main_proc('main',potype_proginit,st);
  1261. pd.aliasnames.insert('PASCALMAIN');
  1262. end;
  1263. end;
  1264. tcgprocinfo(current_procinfo).parse_body;
  1265. tcgprocinfo(current_procinfo).generate_code;
  1266. tcgprocinfo(current_procinfo).resetprocdef;
  1267. release_main_proc(pd);
  1268. { should we force unit initialization? }
  1269. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1270. begin
  1271. { initialize section }
  1272. gen_implicit_initfinal(uf_init,st);
  1273. { finalize section }
  1274. gen_implicit_initfinal(uf_finalize,st);
  1275. end;
  1276. { Add symbol to the exports section for win32 so smartlinking a
  1277. DLL will include the edata section }
  1278. if assigned(exportlib) and
  1279. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1280. assigned(current_module._exports.first) then
  1281. codesegment.concat(tai_const.create_sym(exportlib.edatalabel));
  1282. If ResourceStrings.ResStrCount>0 then
  1283. begin
  1284. ResourceStrings.CreateResourceStringList;
  1285. { only write if no errors found }
  1286. if (Errorcount=0) then
  1287. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1288. end;
  1289. { finalize? }
  1290. if token=_FINALIZATION then
  1291. begin
  1292. { set module options }
  1293. current_module.flags:=current_module.flags or uf_finalize;
  1294. { Compile the finalize }
  1295. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1296. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1297. tcgprocinfo(current_procinfo).parse_body;
  1298. tcgprocinfo(current_procinfo).generate_code;
  1299. tcgprocinfo(current_procinfo).resetprocdef;
  1300. release_main_proc(pd);
  1301. end;
  1302. { consume the last point }
  1303. consume(_POINT);
  1304. if (Errorcount=0) then
  1305. begin
  1306. { test static symtable }
  1307. tstoredsymtable(st).allsymbolsused;
  1308. tstoredsymtable(st).allprivatesused;
  1309. tstoredsymtable(st).check_forwards;
  1310. tstoredsymtable(st).checklabels;
  1311. tstoredsymtable(st).unchain_overloaded;
  1312. current_module.allunitsused;
  1313. end;
  1314. { leave when we got an error }
  1315. if (Errorcount>0) and not status.skip_error then
  1316. begin
  1317. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1318. status.skip_error:=true;
  1319. exit;
  1320. end;
  1321. { generate debuginfo }
  1322. {$ifdef GDB}
  1323. write_gdb_info;
  1324. {$endif GDB}
  1325. { generate a list of threadvars }
  1326. InsertThreadvars;
  1327. { generate imports }
  1328. if current_module.uses_imports then
  1329. importlib.generatelib;
  1330. if islibrary or
  1331. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1332. (target_info.system=system_i386_NETWARE) then
  1333. exportlib.generatelib;
  1334. { insert Tables and Heap }
  1335. insertThreadVarTablesTable;
  1336. insertResourceTablesTable;
  1337. insertinitfinaltable;
  1338. insertheap;
  1339. insertstacklength;
  1340. { create dwarf debuginfo }
  1341. create_dwarf;
  1342. { finish asmlist by adding segment starts }
  1343. insertsegment;
  1344. { insert own objectfile }
  1345. insertobjectfile;
  1346. { assemble and link }
  1347. create_objectfile;
  1348. { release all local symtables that are not needed anymore }
  1349. free_localsymtables(current_module.localsymtable);
  1350. { leave when we got an error }
  1351. if (Errorcount>0) and not status.skip_error then
  1352. begin
  1353. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1354. status.skip_error:=true;
  1355. exit;
  1356. end;
  1357. { create the executable when we are at level 1 }
  1358. if (compile_level=1) then
  1359. begin
  1360. { remove all unused units, this happends when units are removed
  1361. from the uses clause in the source and the ppu was already being loaded }
  1362. hp:=tmodule(loaded_units.first);
  1363. while assigned(hp) do
  1364. begin
  1365. hp2:=hp;
  1366. hp:=tmodule(hp.next);
  1367. if hp2.is_unit and
  1368. not assigned(hp2.globalsymtable) then
  1369. loaded_units.remove(hp2);
  1370. end;
  1371. { insert all .o files from all loaded units }
  1372. hp:=tmodule(loaded_units.first);
  1373. while assigned(hp) do
  1374. begin
  1375. linker.AddModuleFiles(hp);
  1376. hp:=tmodule(hp.next);
  1377. end;
  1378. { write .def file }
  1379. if (cs_link_deffile in aktglobalswitches) then
  1380. deffile.writefile;
  1381. { finally we can create a executable }
  1382. if (not current_module.is_unit) then
  1383. begin
  1384. if DLLSource then
  1385. linker.MakeSharedLibrary
  1386. else
  1387. linker.MakeExecutable;
  1388. end;
  1389. end;
  1390. end;
  1391. end.
  1392. {
  1393. $Log$
  1394. Revision 1.165 2004-10-04 18:26:51 peter
  1395. * debuginfo fixes
  1396. Revision 1.164 2004/09/14 16:33:46 peter
  1397. * release localsymtables when module is compiled
  1398. Revision 1.163 2004/09/04 21:18:47 armin
  1399. * target netwlibc added (libc is preferred for newer netware versions)
  1400. Revision 1.162 2004/09/03 16:12:32 armin
  1401. * dont create main for netware (only PASCALMAIN)
  1402. Revision 1.161 2004/08/16 22:52:35 olle
  1403. + Added automatic use of unit macpas under mode macpas
  1404. Revision 1.160 2004/07/06 20:23:25 peter
  1405. * remove unused and not loaded units before linking
  1406. Revision 1.159 2004/06/29 21:00:08 peter
  1407. * only enable dwarf for supported platforms
  1408. Revision 1.158 2004/06/20 08:55:30 florian
  1409. * logs truncated
  1410. Revision 1.157 2004/06/18 15:16:27 peter
  1411. * fixed debuginfo symbol
  1412. Revision 1.156 2004/06/16 20:07:09 florian
  1413. * dwarf branch merged
  1414. Revision 1.155 2004/05/23 20:56:42 peter
  1415. * initialize errorsym/errortype.def.owner to prevent crashes
  1416. Revision 1.154 2004/05/23 15:06:21 peter
  1417. * implicit_finally flag must be set in pass1
  1418. * add check whether the implicit frame is generated when expected
  1419. Revision 1.153 2004/05/19 21:16:13 peter
  1420. * add DEBUGINFO symbol to reference the .o file that includes the
  1421. stabs info for types and global/static variables
  1422. * debuginfo flag added to ppu to indicate whether debuginfo is
  1423. generated or not
  1424. }