pmodules.pas 54 KB

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