pmodules.pas 60 KB

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