pmodules.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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,symdef,symsym,symtable,aasm,
  30. cgbase,
  31. cga,
  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. else
  264. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  265. end;
  266. {$ifdef m68k}
  267. if target_info.target<>target_m68k_PalmOS then
  268. begin
  269. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  270. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  271. end;
  272. {$else m68k}
  273. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  274. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  275. {$endif m68k}
  276. end;
  277. procedure insertstacklength;
  278. begin
  279. { stacksize can be specified and is now simulated }
  280. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  281. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  282. end;
  283. procedure loaddefaultunits;
  284. var
  285. hp : tmodule;
  286. unitsym : tunitsym;
  287. begin
  288. { are we compiling the system unit? }
  289. if (cs_compilesystem in aktmoduleswitches) then
  290. begin
  291. { create system defines }
  292. createconstdefs;
  293. { we don't need to reset anything, it's already done in parser.pas }
  294. exit;
  295. end;
  296. { insert the system unit, it is allways the first }
  297. hp:=loadunit('System','');
  298. systemunit:=tglobalsymtable(hp.globalsymtable);
  299. { it's always the first unit }
  300. systemunit.next:=nil;
  301. symtablestack:=systemunit;
  302. { add to the used units }
  303. current_module.used_units.concat(tused_unit.create(hp,true));
  304. unitsym:=tunitsym.create('System',systemunit);
  305. inc(unitsym.refs);
  306. refsymtable.insert(unitsym);
  307. { read default constant definitions }
  308. make_ref:=false;
  309. readconstdefs;
  310. make_ref:=true;
  311. { Objpas unit? }
  312. if m_objpas in aktmodeswitches then
  313. begin
  314. hp:=loadunit('ObjPas','');
  315. tsymtable(hp.globalsymtable).next:=symtablestack;
  316. symtablestack:=hp.globalsymtable;
  317. { add to the used units }
  318. current_module.used_units.concat(tused_unit.create(hp,true));
  319. unitsym:=tunitsym.create('ObjPas',hp.globalsymtable);
  320. inc(unitsym.refs);
  321. refsymtable.insert(unitsym);
  322. end;
  323. { Profile unit? Needed for go32v2 only }
  324. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  325. begin
  326. hp:=loadunit('Profile','');
  327. tsymtable(hp.globalsymtable).next:=symtablestack;
  328. symtablestack:=hp.globalsymtable;
  329. { add to the used units }
  330. current_module.used_units.concat(tused_unit.create(hp,true));
  331. unitsym:=tunitsym.create('Profile',hp.globalsymtable);
  332. inc(unitsym.refs);
  333. refsymtable.insert(unitsym);
  334. end;
  335. { Units only required for main module }
  336. if not(current_module.is_unit) then
  337. begin
  338. { Heaptrc unit }
  339. if (cs_gdb_heaptrc in aktglobalswitches) then
  340. begin
  341. hp:=loadunit('HeapTrc','');
  342. tsymtable(hp.globalsymtable).next:=symtablestack;
  343. symtablestack:=hp.globalsymtable;
  344. { add to the used units }
  345. current_module.used_units.concat(tused_unit.create(hp,true));
  346. unitsym:=tunitsym.create('HeapTrc',hp.globalsymtable);
  347. inc(unitsym.refs);
  348. refsymtable.insert(unitsym);
  349. end;
  350. { Lineinfo unit }
  351. if (cs_gdb_lineinfo in aktglobalswitches) then
  352. begin
  353. hp:=loadunit('LineInfo','');
  354. tsymtable(hp.globalsymtable).next:=symtablestack;
  355. symtablestack:=hp.globalsymtable;
  356. { add to the used units }
  357. current_module.used_units.concat(tused_unit.create(hp,true));
  358. unitsym:=tunitsym.create('LineInfo',hp.globalsymtable);
  359. inc(unitsym.refs);
  360. refsymtable.insert(unitsym);
  361. end;
  362. end;
  363. { save default symtablestack }
  364. defaultsymtablestack:=symtablestack;
  365. end;
  366. procedure loadunits;
  367. var
  368. s,sorg : stringid;
  369. fn : string;
  370. pu,
  371. hp : tused_unit;
  372. hp2 : tmodule;
  373. hp3 : tsymtable;
  374. oldprocsym:tprocsym;
  375. oldprocdef:tprocdef;
  376. unitsym : tunitsym;
  377. begin
  378. oldprocsym:=aktprocsym;
  379. oldprocdef:=aktprocdef;
  380. consume(_USES);
  381. {$ifdef DEBUG}
  382. test_symtablestack;
  383. {$endif DEBUG}
  384. repeat
  385. s:=pattern;
  386. sorg:=orgpattern;
  387. consume(_ID);
  388. { support "<unit> in '<file>'" construct, but not for tp7 }
  389. if not(m_tp7 in aktmodeswitches) then
  390. begin
  391. if try_to_consume(_OP_IN) then
  392. fn:=get_stringconst
  393. else
  394. fn:='';
  395. end;
  396. { Give a warning if objpas is loaded }
  397. if s='OBJPAS' then
  398. Message(parser_w_no_objpas_use_mode);
  399. { check if the unit is already used }
  400. pu:=tused_unit(current_module.used_units.first);
  401. while assigned(pu) do
  402. begin
  403. if (pu.name^=s) then
  404. break;
  405. pu:=tused_unit(pu.next);
  406. end;
  407. { avoid uses of itself }
  408. if not assigned(pu) and (s<>current_module.modulename^) then
  409. begin
  410. { load the unit }
  411. hp2:=loadunit(sorg,fn);
  412. { the current module uses the unit hp2 }
  413. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  414. tused_unit(current_module.used_units.last).in_uses:=true;
  415. if current_module.compiled then
  416. exit;
  417. unitsym:=tunitsym.create(sorg,hp2.globalsymtable);
  418. { never claim about unused unit if
  419. there is init or finalize code PM }
  420. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  421. inc(unitsym.refs);
  422. refsymtable.insert(unitsym);
  423. end
  424. else
  425. Message1(sym_e_duplicate_id,s);
  426. if token=_COMMA then
  427. begin
  428. pattern:='';
  429. consume(_COMMA);
  430. end
  431. else
  432. break;
  433. until false;
  434. consume(_SEMICOLON);
  435. { set the symtable to systemunit so it gets reorderd correctly }
  436. symtablestack:=defaultsymtablestack;
  437. { now insert the units in the symtablestack }
  438. hp:=tused_unit(current_module.used_units.first);
  439. while assigned(hp) do
  440. begin
  441. {$IfDef GDB}
  442. if (cs_debuginfo in aktmoduleswitches) and
  443. (cs_gdb_dbx in aktglobalswitches) and
  444. not hp.is_stab_written then
  445. begin
  446. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  447. hp.is_stab_written:=true;
  448. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  449. end;
  450. {$EndIf GDB}
  451. if hp.in_uses then
  452. begin
  453. hp3:=symtablestack;
  454. while assigned(hp3) do
  455. begin
  456. { insert units only once ! }
  457. if hp.u.globalsymtable=hp3 then
  458. break;
  459. hp3:=hp3.next;
  460. { unit isn't inserted }
  461. if hp3=nil then
  462. begin
  463. tsymtable(hp.u.globalsymtable).next:=symtablestack;
  464. symtablestack:=tsymtable(hp.u.globalsymtable);
  465. {$ifdef DEBUG}
  466. test_symtablestack;
  467. {$endif DEBUG}
  468. end;
  469. end;
  470. end;
  471. hp:=tused_unit(hp.next);
  472. end;
  473. aktprocsym:=oldprocsym;
  474. aktprocdef:=oldprocdef;
  475. end;
  476. procedure write_gdb_info;
  477. {$IfDef GDB}
  478. var
  479. hp : tused_unit;
  480. begin
  481. if not (cs_debuginfo in aktmoduleswitches) then
  482. exit;
  483. if (cs_gdb_dbx in aktglobalswitches) then
  484. begin
  485. debugList.concat(Tai_asm_comment.Create(strpnew('EINCL of global '+
  486. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  487. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  488. debugList.concat(Tai_stabs.Create(strpnew('"'+
  489. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  490. tostr(N_EINCL)+',0,0,0')));
  491. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  492. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  493. do_count_dbx:=false;
  494. end;
  495. { now insert the units in the symtablestack }
  496. hp:=tused_unit(current_module.used_units.first);
  497. while assigned(hp) do
  498. begin
  499. if (cs_debuginfo in aktmoduleswitches) and
  500. not hp.is_stab_written then
  501. begin
  502. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  503. hp.is_stab_written:=true;
  504. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  505. end;
  506. hp:=tused_unit(hp.next);
  507. end;
  508. if current_module.in_implementation and
  509. assigned(current_module.localsymtable) then
  510. begin
  511. { all types }
  512. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  513. { and all local symbols}
  514. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  515. end
  516. else if assigned(current_module.globalsymtable) then
  517. begin
  518. { all types }
  519. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  520. { and all local symbols}
  521. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  522. end;
  523. end;
  524. {$Else GDB}
  525. begin
  526. end;
  527. {$EndIf GDB}
  528. procedure parse_implementation_uses(symt:tsymtable);
  529. begin
  530. if token=_USES then
  531. begin
  532. loadunits;
  533. {$ifdef DEBUG}
  534. test_symtablestack;
  535. {$endif DEBUG}
  536. end;
  537. end;
  538. procedure setupglobalswitches;
  539. begin
  540. { can't have local browser when no global browser }
  541. if (cs_local_browser in aktmoduleswitches) and
  542. not(cs_browser in aktmoduleswitches) then
  543. exclude(aktmoduleswitches,cs_local_browser);
  544. { define a symbol in delphi,objfpc,tp,gpc mode }
  545. if (m_delphi in aktmodeswitches) then
  546. current_scanner.def_macro('FPC_DELPHI')
  547. else
  548. if (m_tp7 in aktmodeswitches) then
  549. current_scanner.def_macro('FPC_TP')
  550. else
  551. if (m_objfpc in aktmodeswitches) then
  552. current_scanner.def_macro('FPC_OBJFPC')
  553. else
  554. if (m_gpc in aktmodeswitches) then
  555. current_scanner.def_macro('FPC_GPC');
  556. end;
  557. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  558. var
  559. stt : tsymtable;
  560. procdefs : pprocdeflist;
  561. begin
  562. {Generate a procsym for main}
  563. make_ref:=false;
  564. aktprocsym:=tprocsym.create('$'+name);
  565. { main are allways used }
  566. inc(aktprocsym.refs);
  567. {Try to insert in in static symtable ! }
  568. stt:=symtablestack;
  569. symtablestack:=st;
  570. aktprocdef:=tprocdef.create;
  571. new(procdefs);
  572. procdefs^.def:=aktprocdef;
  573. procdefs^.next:=aktprocsym.defs;
  574. aktprocsym.defs:=procdefs;
  575. aktprocdef.procsym:=aktprocsym;
  576. symtablestack:=stt;
  577. aktprocdef.proctypeoption:=options;
  578. aktprocdef.setmangledname(target_info.cprefix+name);
  579. aktprocdef.forwarddef:=false;
  580. make_ref:=true;
  581. { The localst is a local symtable. Change it into the static
  582. symtable }
  583. aktprocdef.localst.free;
  584. aktprocdef.localst:=st;
  585. { and insert the procsym in symtable }
  586. st.insert(aktprocsym);
  587. { set some informations about the main program }
  588. with procinfo^ do
  589. begin
  590. _class:=nil;
  591. para_offset:=target_info.first_parm_offset;
  592. framepointer:=FRAME_POINTER_REG;
  593. flags:=0;
  594. procdef:=aktprocdef;
  595. end;
  596. end;
  597. procedure insertLocalThreadvarsTablesTable;
  598. var
  599. hp : tused_unit;
  600. ltvTables : taasmoutput;
  601. count : longint;
  602. begin
  603. ltvTables:=TAAsmOutput.Create;
  604. count:=0;
  605. hp:=tused_unit(usedunits.first);
  606. while assigned(hp) do
  607. begin
  608. If (hp.u.flags and uf_local_threadvars)=uf_local_threadvars then
  609. begin
  610. ltvTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_$LOCALTHREADVARLIST'));
  611. inc(count);
  612. end;
  613. hp:=tused_unit(hp.next);
  614. end;
  615. { TableCount }
  616. ltvTables.insert(Tai_const.Create_32bit(count));
  617. ltvTables.insert(Tai_symbol.Createdataname_global('FPC_LOCALTHREADVARTABLES',0));
  618. ltvTables.concat(Tai_symbol_end.Createname('FPC_LOCALTHREADVARTABLES'));
  619. { insert in data segment }
  620. if (cs_create_smart in aktmoduleswitches) then
  621. dataSegment.concat(Tai_cut.Create);
  622. dataSegment.concatlist(ltvTables);
  623. ltvTables.free;
  624. if count > 0 then
  625. have_local_threadvars := true;
  626. end;
  627. var ltvTable : taasmoutput;
  628. procedure addToLocalThreadvarTab(p:tnamedindexitem);
  629. var
  630. asym : tasmsymbol;
  631. begin
  632. with tvarsym(p) do
  633. begin
  634. if (typ=varsym) and (vo_is_thread_var IN varoptions) then
  635. begin
  636. if ltvTable = nil then
  637. begin { first threadvar }
  638. ltvTable := TAAsmOutput.Create;
  639. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$LOCALTHREADVARLIST',0));
  640. end;
  641. asym := getasmsymbol(mangledname);
  642. if asym <> nil then
  643. begin
  644. ltvTable.concat(tai_const_symbol.create(asym)); { address of threadvar }
  645. ltvTable.concat(tai_const.create_32bit(getsize)); { size of threadvar }
  646. end;
  647. end;
  648. end;
  649. end;
  650. procedure proc_unit;
  651. function is_assembler_generated:boolean;
  652. begin
  653. is_assembler_generated:=(Errorcount=0) and
  654. not(
  655. codeSegment.empty and
  656. dataSegment.empty and
  657. bssSegment.empty and
  658. ((importssection=nil) or importsSection.empty) and
  659. ((resourcesection=nil) or resourceSection.empty) and
  660. ((resourcestringlist=nil) or resourcestringList.empty)
  661. );
  662. end;
  663. var
  664. main_file: tinputfile;
  665. st : tsymtable;
  666. unitst : tglobalsymtable;
  667. {$ifdef GDB}
  668. pu : tused_unit;
  669. {$endif GDB}
  670. store_crc,store_interface_crc : cardinal;
  671. s2 : ^string; {Saves stack space}
  672. force_init_final : boolean;
  673. begin
  674. consume(_UNIT);
  675. if compile_level=1 then
  676. Status.IsExe:=false;
  677. if token=_ID then
  678. begin
  679. { create filenames and unit name }
  680. main_file := current_scanner.inputfile;
  681. while assigned(main_file.next) do
  682. main_file := main_file.next;
  683. current_module.SetFileName(main_file.path^+main_file.name^,true);
  684. stringdispose(current_module.modulename);
  685. stringdispose(current_module.realmodulename);
  686. current_module.modulename:=stringdup(pattern);
  687. current_module.realmodulename:=stringdup(orgpattern);
  688. { check for system unit }
  689. new(s2);
  690. s2^:=upper(SplitName(main_file.name^));
  691. if (cs_check_unit_name in aktglobalswitches) and
  692. not((current_module.modulename^=s2^) or
  693. ((length(current_module.modulename^)>8) and
  694. (copy(current_module.modulename^,1,8)=s2^))) then
  695. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  696. if (current_module.modulename^='SYSTEM') then
  697. include(aktmoduleswitches,cs_compilesystem);
  698. dispose(s2);
  699. end;
  700. consume(_ID);
  701. consume(_SEMICOLON);
  702. consume(_INTERFACE);
  703. { global switches are read, so further changes aren't allowed }
  704. current_module.in_global:=false;
  705. { handle the global switches }
  706. setupglobalswitches;
  707. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  708. { update status }
  709. status.currentmodule:=current_module.realmodulename^;
  710. { maybe turn off m_objpas if we are compiling objpas }
  711. if (current_module.modulename^='OBJPAS') then
  712. exclude(aktmodeswitches,m_objpas);
  713. parse_only:=true;
  714. { generate now the global symboltable }
  715. st:=tglobalsymtable.create(current_module.modulename^);
  716. refsymtable:=st;
  717. unitst:=tglobalsymtable(st);
  718. { define first as local to overcome dependency conflicts }
  719. current_module.localsymtable:=st;
  720. { the unit name must be usable as a unit specifier }
  721. { inside the unit itself (PM) }
  722. { this also forbids to have another symbol }
  723. { with the same name as the unit }
  724. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  725. { load default units, like the system unit }
  726. loaddefaultunits;
  727. { reset }
  728. make_ref:=true;
  729. lexlevel:=0;
  730. { insert qualifier for the system unit (allows system.writeln) }
  731. if not(cs_compilesystem in aktmoduleswitches) then
  732. begin
  733. if token=_USES then
  734. begin
  735. loadunits;
  736. { has it been compiled at a higher level ?}
  737. if current_module.compiled then
  738. begin
  739. { this unit symtable is obsolete }
  740. { dispose(unitst,done);
  741. disposed as localsymtable !! }
  742. RestoreUnitSyms;
  743. exit;
  744. end;
  745. end;
  746. { ... but insert the symbol table later }
  747. st.next:=symtablestack;
  748. symtablestack:=st;
  749. end
  750. else
  751. { while compiling a system unit, some types are directly inserted }
  752. begin
  753. st.next:=symtablestack;
  754. symtablestack:=st;
  755. insert_intern_types(st);
  756. end;
  757. { now we know the place to insert the constants }
  758. constsymtable:=symtablestack;
  759. { move the global symtab from the temporary local to global }
  760. current_module.globalsymtable:=current_module.localsymtable;
  761. current_module.localsymtable:=nil;
  762. reset_global_defs;
  763. { number all units, so we know if a unit is used by this unit or
  764. needs to be added implicitly }
  765. current_module.numberunits;
  766. { ... parse the declarations }
  767. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  768. read_interface_declarations;
  769. { leave when we got an error }
  770. if (Errorcount>0) and not status.skip_error then
  771. begin
  772. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  773. status.skip_error:=true;
  774. exit;
  775. end;
  776. {else in inteface its somatimes necessary even if unused
  777. st^.allunitsused; }
  778. {$ifdef New_GDB}
  779. write_gdb_info;
  780. {$endIf Def New_GDB}
  781. if not(cs_compilesystem in aktmoduleswitches) then
  782. if (Errorcount=0) then
  783. tppumodule(current_module).getppucrc;
  784. { Parse the implementation section }
  785. consume(_IMPLEMENTATION);
  786. current_module.in_implementation:=true;
  787. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  788. parse_only:=false;
  789. { generates static symbol table }
  790. st:=tstaticsymtable.create(current_module.modulename^);
  791. current_module.localsymtable:=st;
  792. { remove the globalsymtable from the symtable stack }
  793. { to reinsert it after loading the implementation units }
  794. symtablestack:=unitst.next;
  795. { we don't want implementation units symbols in unitsymtable !! PM }
  796. refsymtable:=st;
  797. { Read the implementation units }
  798. parse_implementation_uses(unitst);
  799. if current_module.compiled then
  800. begin
  801. RestoreUnitSyms;
  802. exit;
  803. end;
  804. { reset ranges/stabs in exported definitions }
  805. reset_global_defs;
  806. { All units are read, now give them a number }
  807. current_module.numberunits;
  808. { now we can change refsymtable }
  809. refsymtable:=st;
  810. { but reinsert the global symtable as lasts }
  811. unitst.next:=symtablestack;
  812. symtablestack:=unitst;
  813. tstoredsymtable(symtablestack).chainoperators;
  814. {$ifdef DEBUG}
  815. test_symtablestack;
  816. {$endif DEBUG}
  817. constsymtable:=symtablestack;
  818. {$ifdef Splitheap}
  819. if testsplit then
  820. begin
  821. Split_Heap;
  822. allow_special:=true;
  823. Switch_to_temp_heap;
  824. end;
  825. { it will report all crossings }
  826. allow_special:=false;
  827. {$endif Splitheap}
  828. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  829. { Compile the unit }
  830. codegen_newprocedure;
  831. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  832. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  833. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  834. compile_proc_body(true,false);
  835. codegen_doneprocedure;
  836. { avoid self recursive destructor call !! PM }
  837. aktprocdef.localst:=nil;
  838. { if the unit contains ansi/widestrings, initialization and
  839. finalization code must be forced }
  840. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  841. tstaticsymtable(current_module.localsymtable).needs_init_final;
  842. { should we force unit initialization? }
  843. { this is a hack, but how can it be done better ? }
  844. if force_init_final and ((current_module.flags and uf_init)=0) then
  845. begin
  846. current_module.flags:=current_module.flags or uf_init;
  847. { now we can insert a cut }
  848. if (cs_create_smart in aktmoduleswitches) then
  849. codeSegment.concat(Tai_cut.Create);
  850. genimplicitunitinit(codesegment);
  851. end;
  852. { finalize? }
  853. if token=_FINALIZATION then
  854. begin
  855. { set module options }
  856. current_module.flags:=current_module.flags or uf_finalize;
  857. { Compile the finalize }
  858. codegen_newprocedure;
  859. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  860. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  861. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  862. compile_proc_body(true,false);
  863. codegen_doneprocedure;
  864. end
  865. else if force_init_final then
  866. begin
  867. current_module.flags:=current_module.flags or uf_finalize;
  868. { now we can insert a cut }
  869. if (cs_create_smart in aktmoduleswitches) then
  870. codeSegment.concat(Tai_cut.Create);
  871. genimplicitunitfinal(codesegment);
  872. end;
  873. { generate a list of local threadvars }
  874. ltvTable := nil;
  875. st.foreach_static (@addToLocalThreadvarTab);
  876. if ltvTable <> nil then
  877. begin
  878. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  879. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$LOCALTHREADVARLIST'));
  880. if (cs_create_smart in aktmoduleswitches) then
  881. dataSegment.concat(Tai_cut.Create);
  882. dataSegment.concatlist(ltvTable);
  883. ltvTable.Free;
  884. current_module.flags:=current_module.flags or uf_local_threadvars;
  885. end;
  886. { the last char should always be a point }
  887. consume(_POINT);
  888. If ResourceStrings.ResStrCount>0 then
  889. begin
  890. ResourceStrings.CreateResourceStringList;
  891. current_module.flags:=current_module.flags or uf_has_resources;
  892. { only write if no errors found }
  893. if (Errorcount=0) then
  894. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  895. end;
  896. { avoid self recursive destructor call !! PM }
  897. aktprocdef.localst:=nil;
  898. { absence does not matter here !! }
  899. aktprocdef.forwarddef:=false;
  900. { test static symtable }
  901. if (Errorcount=0) then
  902. begin
  903. tstoredsymtable(st).allsymbolsused;
  904. tstoredsymtable(st).allunitsused;
  905. tstoredsymtable(st).allprivatesused;
  906. end;
  907. { size of the static data }
  908. datasize:=st.datasize;
  909. {$ifdef GDB}
  910. { add all used definitions even for implementation}
  911. if (cs_debuginfo in aktmoduleswitches) then
  912. begin
  913. {$IfnDef New_GDB}
  914. if assigned(current_module.globalsymtable) then
  915. begin
  916. { all types }
  917. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  918. { and all local symbols}
  919. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  920. end;
  921. { all local types }
  922. tglobalsymtable(st)^.concattypestabto(debuglist);
  923. { and all local symbols}
  924. st^.concatstabto(debuglist);
  925. {$else New_GDB}
  926. write_gdb_info;
  927. {$endIf Def New_GDB}
  928. end;
  929. {$endif GDB}
  930. reset_global_defs;
  931. if (Errorcount=0) then
  932. begin
  933. { tests, if all (interface) forwards are resolved }
  934. tstoredsymtable(symtablestack).check_forwards;
  935. { check if all private fields are used }
  936. tstoredsymtable(symtablestack).allprivatesused;
  937. { remove cross unit overloads }
  938. tstoredsymtable(symtablestack).unchain_overloaded;
  939. end;
  940. current_module.in_implementation:=false;
  941. {$ifdef GDB}
  942. tglobalsymtable(symtablestack).is_stab_written:=false;
  943. {$endif GDB}
  944. { leave when we got an error }
  945. if (Errorcount>0) and not status.skip_error then
  946. begin
  947. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  948. status.skip_error:=true;
  949. exit;
  950. end;
  951. { generate imports }
  952. if current_module.uses_imports then
  953. importlib.generatelib;
  954. { insert own objectfile, or say that it's in a library
  955. (no check for an .o when loading) }
  956. if is_assembler_generated then
  957. insertobjectfile
  958. else
  959. current_module.flags:=current_module.flags or uf_no_link;
  960. if cs_local_browser in aktmoduleswitches then
  961. current_module.localsymtable:=refsymtable;
  962. {$ifdef GDB}
  963. pu:=tused_unit(usedunits.first);
  964. while assigned(pu) do
  965. begin
  966. if assigned(pu.u.globalsymtable) then
  967. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  968. pu:=tused_unit(pu.next);
  969. end;
  970. {$endif GDB}
  971. if is_assembler_generated then
  972. begin
  973. { finish asmlist by adding segment starts }
  974. insertsegment;
  975. { assemble }
  976. create_objectfile;
  977. end;
  978. { Write out the ppufile after the object file has been created }
  979. store_interface_crc:=current_module.interface_crc;
  980. store_crc:=current_module.crc;
  981. if (Errorcount=0) then
  982. tppumodule(current_module).writeppu;
  983. if not(cs_compilesystem in aktmoduleswitches) then
  984. if store_interface_crc<>current_module.interface_crc then
  985. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  986. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  987. {$ifdef EXTDEBUG}
  988. if not(cs_compilesystem in aktmoduleswitches) then
  989. if (store_crc<>current_module.crc) and simplify_ppu then
  990. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  991. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  992. {$endif EXTDEBUG}
  993. { remove static symtable (=refsymtable) here to save some mem }
  994. if not (cs_local_browser in aktmoduleswitches) then
  995. begin
  996. st.free;
  997. current_module.localsymtable:=nil;
  998. end;
  999. RestoreUnitSyms;
  1000. { leave when we got an error }
  1001. if (Errorcount>0) and not status.skip_error then
  1002. begin
  1003. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1004. status.skip_error:=true;
  1005. exit;
  1006. end;
  1007. end;
  1008. procedure proc_program(islibrary : boolean);
  1009. var
  1010. main_file: tinputfile;
  1011. st : tsymtable;
  1012. hp : tmodule;
  1013. begin
  1014. DLLsource:=islibrary;
  1015. Status.IsLibrary:=IsLibrary;
  1016. Status.IsExe:=true;
  1017. parse_only:=false;
  1018. { relocation works only without stabs under win32 !! PM }
  1019. { internal assembler uses rva for stabs info
  1020. so it should work with relocated DLLs }
  1021. if RelocSection and
  1022. (target_info.target in [target_i386_win32,target_i386_wdosx]) and
  1023. (target_info.assem<>as_i386_pecoff) then
  1024. begin
  1025. include(aktglobalswitches,cs_link_strip);
  1026. { Warning stabs info does not work with reloc section !! }
  1027. if cs_debuginfo in aktmoduleswitches then
  1028. begin
  1029. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1030. Message(parser_w_parser_win32_debug_needs_WN);
  1031. exclude(aktmoduleswitches,cs_debuginfo);
  1032. end;
  1033. end;
  1034. { get correct output names }
  1035. main_file := current_scanner.inputfile;
  1036. while assigned(main_file.next) do
  1037. main_file := main_file.next;
  1038. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1039. if islibrary then
  1040. begin
  1041. consume(_LIBRARY);
  1042. stringdispose(current_module.modulename);
  1043. current_module.modulename:=stringdup(pattern);
  1044. current_module.islibrary:=true;
  1045. exportlib.preparelib(pattern);
  1046. consume(_ID);
  1047. consume(_SEMICOLON);
  1048. end
  1049. else
  1050. { is there an program head ? }
  1051. if token=_PROGRAM then
  1052. begin
  1053. consume(_PROGRAM);
  1054. stringdispose(current_module.modulename);
  1055. stringdispose(current_module.realmodulename);
  1056. current_module.modulename:=stringdup(pattern);
  1057. current_module.realmodulename:=stringdup(orgpattern);
  1058. if (target_info.target in [target_i386_WIN32,target_i386_wdosx]) then
  1059. exportlib.preparelib(pattern);
  1060. consume(_ID);
  1061. if token=_LKLAMMER then
  1062. begin
  1063. consume(_LKLAMMER);
  1064. consume_idlist;
  1065. consume(_RKLAMMER);
  1066. end;
  1067. consume(_SEMICOLON);
  1068. end
  1069. else if (target_info.target in [target_i386_WIN32,target_i386_wdosx]) then
  1070. exportlib.preparelib(current_module.modulename^);
  1071. { global switches are read, so further changes aren't allowed }
  1072. current_module.in_global:=false;
  1073. { setup things using the global switches }
  1074. setupglobalswitches;
  1075. { set implementation flag }
  1076. current_module.in_implementation:=true;
  1077. { insert after the unit symbol tables the static symbol table }
  1078. { of the program }
  1079. st:=tstaticsymtable.create(current_module.modulename^);;
  1080. current_module.localsymtable:=st;
  1081. refsymtable:=st;
  1082. { load standard units (system,objpas,profile unit) }
  1083. loaddefaultunits;
  1084. { reset }
  1085. lexlevel:=0;
  1086. {Load the units used by the program we compile.}
  1087. if token=_USES then
  1088. loadunits;
  1089. tstoredsymtable(symtablestack).chainoperators;
  1090. { reset ranges/stabs in exported definitions }
  1091. reset_global_defs;
  1092. { All units are read, now give them a number }
  1093. current_module.numberunits;
  1094. {Insert the name of the main program into the symbol table.}
  1095. if current_module.realmodulename^<>'' then
  1096. st.insert(tunitsym.create(current_module.realmodulename^,tglobalsymtable(st)));
  1097. { ...is also constsymtable, this is the symtable where }
  1098. { the elements of enumeration types are inserted }
  1099. constsymtable:=st;
  1100. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1101. {The program intialization needs an alias, so it can be called
  1102. from the bootstrap code.}
  1103. codegen_newprocedure;
  1104. if islibrary then
  1105. begin
  1106. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1107. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1108. aktprocdef.aliasnames.insert('PASCALMAIN');
  1109. { this code is called from C so we need to save some
  1110. registers }
  1111. include(aktprocdef.procoptions,po_savestdregs);
  1112. end
  1113. else
  1114. begin
  1115. gen_main_procsym('main',potype_proginit,st);
  1116. aktprocdef.aliasnames.insert('program_init');
  1117. aktprocdef.aliasnames.insert('PASCALMAIN');
  1118. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1119. end;
  1120. insertLocalThreadvarsTablesTable;
  1121. compile_proc_body(true,false);
  1122. { Add symbol to the exports section for win32 so smartlinking a
  1123. DLL will include the edata section }
  1124. if assigned(exportlib) and
  1125. (target_info.target in [target_i386_win32,target_i386_wdosx]) and
  1126. assigned(current_module._exports.first) then
  1127. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1128. { avoid self recursive destructor call !! PM }
  1129. aktprocdef.localst:=nil;
  1130. { consider these symbols as global ones for browser
  1131. but the typecasting of the globalsymtable with tglobalsymtable
  1132. can then lead to problems (PFV)
  1133. current_module.globalsymtable:=current_module.localsymtable;
  1134. current_module.localsymtable:=nil;}
  1135. If ResourceStrings.ResStrCount>0 then
  1136. begin
  1137. ResourceStrings.CreateResourceStringList;
  1138. { only write if no errors found }
  1139. if (Errorcount=0) then
  1140. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1141. end;
  1142. codegen_doneprocedure;
  1143. { finalize? }
  1144. if token=_FINALIZATION then
  1145. begin
  1146. { set module options }
  1147. current_module.flags:=current_module.flags or uf_finalize;
  1148. { Compile the finalize }
  1149. codegen_newprocedure;
  1150. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1151. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1152. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1153. compile_proc_body(true,false);
  1154. codegen_doneprocedure;
  1155. end;
  1156. { consume the last point }
  1157. consume(_POINT);
  1158. {$ifdef New_GDB}
  1159. write_gdb_info;
  1160. {$endIf Def New_GDB}
  1161. { leave when we got an error }
  1162. if (Errorcount>0) and not status.skip_error then
  1163. begin
  1164. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1165. status.skip_error:=true;
  1166. exit;
  1167. end;
  1168. { test static symtable }
  1169. if (Errorcount=0) then
  1170. begin
  1171. tstoredsymtable(st).allsymbolsused;
  1172. tstoredsymtable(st).allunitsused;
  1173. tstoredsymtable(st).allprivatesused;
  1174. end;
  1175. { generate imports }
  1176. if current_module.uses_imports then
  1177. importlib.generatelib;
  1178. if islibrary or
  1179. (target_info.target in [target_i386_WIN32,target_i386_wdosx]) or
  1180. (target_info.target=target_i386_NETWARE) then
  1181. exportlib.generatelib;
  1182. { insert heap }
  1183. insertResourceTablesTable;
  1184. insertinitfinaltable;
  1185. insertheap;
  1186. insertstacklength;
  1187. datasize:=symtablestack.datasize;
  1188. { finish asmlist by adding segment starts }
  1189. insertsegment;
  1190. { insert own objectfile }
  1191. insertobjectfile;
  1192. { assemble and link }
  1193. create_objectfile;
  1194. { leave when we got an error }
  1195. if (Errorcount>0) and not status.skip_error then
  1196. begin
  1197. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1198. status.skip_error:=true;
  1199. exit;
  1200. end;
  1201. { create the executable when we are at level 1 }
  1202. if (compile_level=1) then
  1203. begin
  1204. { insert all .o files from all loaded units }
  1205. hp:=tmodule(loaded_units.first);
  1206. while assigned(hp) do
  1207. begin
  1208. linker.AddModuleFiles(hp);
  1209. hp:=tmodule(hp.next);
  1210. end;
  1211. { write .def file }
  1212. if (cs_link_deffile in aktglobalswitches) then
  1213. deffile.writefile;
  1214. { finally we can create a executable }
  1215. if (not current_module.is_unit) then
  1216. begin
  1217. if DLLSource then
  1218. linker.MakeSharedLibrary
  1219. else
  1220. linker.MakeExecutable;
  1221. end;
  1222. end;
  1223. end;
  1224. end.
  1225. {
  1226. $Log$
  1227. Revision 1.62 2002-04-20 21:32:24 carl
  1228. + generic FPC_CHECKPOINTER
  1229. + first parameter offset in stack now portable
  1230. * rename some constants
  1231. + move some cpu stuff to other units
  1232. - remove unused constents
  1233. * fix stacksize for some targets
  1234. * fix generic size problems which depend now on EXTEND_SIZE constant
  1235. Revision 1.61 2002/04/19 15:46:02 peter
  1236. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1237. in most cases and not written to the ppu
  1238. * add mangeledname_prefix() routine to generate the prefix of
  1239. manglednames depending on the current procedure, object and module
  1240. * removed static procprefix since the mangledname is now build only
  1241. on demand from tprocdef.mangledname
  1242. Revision 1.60 2002/04/14 16:53:10 carl
  1243. + align code section and data section according to alignment rules
  1244. Revision 1.59 2002/04/07 17:58:38 carl
  1245. + generic stack checking
  1246. Revision 1.58 2002/04/04 19:06:03 peter
  1247. * removed unused units
  1248. * use tlocation.size in cg.a_*loc*() routines
  1249. Revision 1.57 2002/04/04 18:42:49 carl
  1250. + added wdosx support (patch from Pavel)
  1251. Revision 1.56 2002/04/02 17:11:29 peter
  1252. * tlocation,treference update
  1253. * LOC_CONSTANT added for better constant handling
  1254. * secondadd splitted in multiple routines
  1255. * location_force_reg added for loading a location to a register
  1256. of a specified size
  1257. * secondassignment parses now first the right and then the left node
  1258. (this is compatible with Kylix). This saves a lot of push/pop especially
  1259. with string operations
  1260. * adapted some routines to use the new cg methods
  1261. Revision 1.55 2002/04/01 13:43:32 armin
  1262. addToLocalThreadvarList used '_'+name instead of mangledname to find asm symbol
  1263. Revision 1.54 2002/03/29 17:19:50 armin
  1264. + allow exports for netware
  1265. Revision 1.53 2002/03/29 09:00:56 armin
  1266. + forgot to delete a debug writeln
  1267. Revision 1.52 2002/03/28 16:07:52 armin
  1268. + initialize threadvars defined local in units
  1269. Revision 1.51 2002/01/24 18:25:49 peter
  1270. * implicit result variable generation for assembler routines
  1271. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1272. Revision 1.50 2001/12/09 03:34:58 carl
  1273. + Stack checking for solaris
  1274. Revision 1.49 2001/11/02 23:16:51 peter
  1275. * removed obsolete chainprocsym and test_procsym code
  1276. Revision 1.48 2001/11/02 22:58:05 peter
  1277. * procsym definition rewrite
  1278. Revision 1.47 2001/09/18 11:30:48 michael
  1279. * Fixes win32 linking problems with import libraries
  1280. * LINKLIB Libraries are now looked for using C file extensions
  1281. * get_exepath fix
  1282. Revision 1.46 2001/09/13 14:47:47 michael
  1283. + Committed patch from peter
  1284. Revision 1.45 2001/08/26 13:36:46 florian
  1285. * some cg reorganisation
  1286. * some PPC updates
  1287. Revision 1.44 2001/08/19 11:22:23 peter
  1288. * palmos support from v10 merged
  1289. Revision 1.43 2001/08/12 19:59:49 peter
  1290. * typo in extdebug msg
  1291. Revision 1.42 2001/08/06 21:40:47 peter
  1292. * funcret moved from tprocinfo to tprocdef
  1293. Revision 1.41 2001/08/05 12:26:52 peter
  1294. * beos fix (merged)
  1295. Revision 1.40 2001/08/04 10:23:54 peter
  1296. * updates so it works with the ide
  1297. Revision 1.38 2001/07/30 20:59:27 peter
  1298. * m68k updates from v10 merged
  1299. Revision 1.37 2001/06/18 20:36:25 peter
  1300. * -Ur switch (merged)
  1301. * masm fixes (merged)
  1302. * quoted filenames for go32v2 and win32
  1303. Revision 1.36 2001/06/06 21:58:16 peter
  1304. * Win32 fixes for Makefile so it doesn't require sh.exe
  1305. Revision 1.35 2001/06/03 21:57:36 peter
  1306. + hint directive parsing support
  1307. Revision 1.34 2001/06/03 15:15:31 peter
  1308. * dllprt0 stub for linux shared libs
  1309. * pass -init and -fini for linux shared libs
  1310. * libprefix splitted into staticlibprefix and sharedlibprefix
  1311. Revision 1.33 2001/05/19 23:05:19 peter
  1312. * support uses <unit> in <file> construction
  1313. Revision 1.32 2001/05/18 22:26:36 peter
  1314. * merged alignment for non-i386
  1315. Revision 1.31 2001/05/09 14:11:10 jonas
  1316. * range check error fixes from Peter
  1317. Revision 1.30 2001/05/06 14:49:17 peter
  1318. * ppu object to class rewrite
  1319. * move ppu read and write stuff to fppu
  1320. Revision 1.29 2001/04/18 22:01:57 peter
  1321. * registration of targets and assemblers
  1322. Revision 1.28 2001/04/13 18:08:37 peter
  1323. * scanner object to class
  1324. Revision 1.27 2001/04/13 01:22:12 peter
  1325. * symtable change to classes
  1326. * range check generation and errors fixed, make cycle DEBUG=1 works
  1327. * memory leaks fixed
  1328. Revision 1.26 2001/04/02 21:20:33 peter
  1329. * resulttype rewrite
  1330. Revision 1.25 2001/03/13 18:45:07 peter
  1331. * fixed some memory leaks
  1332. Revision 1.24 2001/03/06 18:28:02 peter
  1333. * patch from Pavel with a new and much faster DLL Scanner for
  1334. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1335. Revision 1.23 2001/02/24 10:44:56 peter
  1336. * generate .rst from ppufilename instead of modulename
  1337. Revision 1.22 2001/02/21 19:37:19 peter
  1338. * moved deref to be done after loading of implementation units. prederef
  1339. is still done directly after loading of symbols and definitions.
  1340. Revision 1.21 2001/01/14 22:13:52 peter
  1341. * fixed crash with program name as a important unit name
  1342. Revision 1.20 2000/12/25 00:07:27 peter
  1343. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1344. tlinkedlist objects)
  1345. Revision 1.19 2000/11/29 00:30:36 florian
  1346. * unused units removed from uses clause
  1347. * some changes for widestrings
  1348. Revision 1.18 2000/11/01 23:04:37 peter
  1349. * tprocdef.fullprocname added for better casesensitve writing of
  1350. procedures
  1351. Revision 1.17 2000/10/31 22:02:50 peter
  1352. * symtable splitted, no real code changes
  1353. Revision 1.16 2000/10/21 14:36:26 peter
  1354. * merged pierres fixes
  1355. Revision 1.15 2000/10/15 09:08:58 peter
  1356. * use System for the systemunit instead of target dependent
  1357. Revision 1.14 2000/10/15 07:47:51 peter
  1358. * unit names and procedure names are stored mixed case
  1359. Revision 1.13 2000/10/04 14:51:08 pierre
  1360. * IsExe restored
  1361. Revision 1.12 2000/09/30 16:07:40 peter
  1362. * filepos when unit not found (merged)
  1363. Revision 1.11 2000/09/24 21:33:47 peter
  1364. * message updates merges
  1365. Revision 1.10 2000/09/24 15:06:22 peter
  1366. * use defines.inc
  1367. Revision 1.9 2000/08/31 07:53:02 michael
  1368. + Applied patch from Peter
  1369. Revision 1.8 2000/08/29 19:00:01 peter
  1370. * _init and _finalize procsyms also need a $ prefix
  1371. Revision 1.7 2000/08/27 20:19:39 peter
  1372. * store strings with case in ppu, when an internal symbol is created
  1373. a '$' is prefixed so it's not automatic uppercased
  1374. Revision 1.6 2000/08/27 16:11:52 peter
  1375. * moved some util functions from globals,cobjects to cutils
  1376. * splitted files into finput,fmodule
  1377. Revision 1.5 2000/08/25 08:48:22 jonas
  1378. * fixed bug with include files at the very beginning of .pp/.pas files
  1379. (wrong name used for generating exe/checking unit name) (merged from
  1380. fixes branch)
  1381. Revision 1.4 2000/08/21 11:27:44 pierre
  1382. * fix the stabs problems
  1383. Revision 1.3 2000/07/13 12:08:26 michael
  1384. + patched to 1.1.0 with former 1.09patch from peter
  1385. Revision 1.2 2000/07/13 11:32:45 michael
  1386. + removed logs
  1387. }