pmodules.pas 51 KB

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