pmodules.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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(mangledname);
  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. if (cs_create_smart in aktmoduleswitches) then
  919. dataSegment.concat(Tai_cut.Create);
  920. dataSegment.concatlist(ltvTable);
  921. ltvTable.Free;
  922. current_module.flags:=current_module.flags or uf_local_threadvars;
  923. end;
  924. { the last char should always be a point }
  925. consume(_POINT);
  926. If ResourceStrings.ResStrCount>0 then
  927. begin
  928. ResourceStrings.CreateResourceStringList;
  929. current_module.flags:=current_module.flags or uf_has_resources;
  930. { only write if no errors found }
  931. if (Errorcount=0) then
  932. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  933. end;
  934. { avoid self recursive destructor call !! PM }
  935. aktprocdef.localst:=nil;
  936. { absence does not matter here !! }
  937. aktprocdef.forwarddef:=false;
  938. { test static symtable }
  939. if (Errorcount=0) then
  940. begin
  941. tstoredsymtable(st).allsymbolsused;
  942. tstoredsymtable(st).allunitsused;
  943. tstoredsymtable(st).allprivatesused;
  944. end;
  945. { size of the static data }
  946. datasize:=st.datasize;
  947. {$ifdef GDB}
  948. { add all used definitions even for implementation}
  949. if (cs_debuginfo in aktmoduleswitches) then
  950. begin
  951. {$IfnDef New_GDB}
  952. if assigned(current_module.globalsymtable) then
  953. begin
  954. { all types }
  955. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  956. { and all local symbols}
  957. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  958. end;
  959. { all local types }
  960. tglobalsymtable(st)^.concattypestabto(debuglist);
  961. { and all local symbols}
  962. st^.concatstabto(debuglist);
  963. {$else New_GDB}
  964. write_gdb_info;
  965. {$endIf Def New_GDB}
  966. end;
  967. {$endif GDB}
  968. reset_global_defs;
  969. if (Errorcount=0) then
  970. begin
  971. { tests, if all (interface) forwards are resolved }
  972. tstoredsymtable(symtablestack).check_forwards;
  973. { check if all private fields are used }
  974. tstoredsymtable(symtablestack).allprivatesused;
  975. { remove cross unit overloads }
  976. tstoredsymtable(symtablestack).unchain_overloaded;
  977. end;
  978. current_module.in_implementation:=false;
  979. {$ifdef GDB}
  980. tglobalsymtable(symtablestack).is_stab_written:=false;
  981. {$endif GDB}
  982. { leave when we got an error }
  983. if (Errorcount>0) and not status.skip_error then
  984. begin
  985. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  986. status.skip_error:=true;
  987. exit;
  988. end;
  989. { generate imports }
  990. if current_module.uses_imports then
  991. importlib.generatelib;
  992. { insert own objectfile, or say that it's in a library
  993. (no check for an .o when loading) }
  994. if is_assembler_generated then
  995. insertobjectfile
  996. else
  997. current_module.flags:=current_module.flags or uf_no_link;
  998. if cs_local_browser in aktmoduleswitches then
  999. current_module.localsymtable:=refsymtable;
  1000. {$ifdef GDB}
  1001. pu:=tused_unit(usedunits.first);
  1002. while assigned(pu) do
  1003. begin
  1004. if assigned(pu.u.globalsymtable) then
  1005. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  1006. pu:=tused_unit(pu.next);
  1007. end;
  1008. {$endif GDB}
  1009. if is_assembler_generated then
  1010. begin
  1011. { finish asmlist by adding segment starts }
  1012. insertsegment;
  1013. { assemble }
  1014. create_objectfile;
  1015. end;
  1016. { Write out the ppufile after the object file has been created }
  1017. store_interface_crc:=current_module.interface_crc;
  1018. store_crc:=current_module.crc;
  1019. if (Errorcount=0) then
  1020. tppumodule(current_module).writeppu;
  1021. if not(cs_compilesystem in aktmoduleswitches) then
  1022. if store_interface_crc<>current_module.interface_crc then
  1023. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1024. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  1025. {$ifdef EXTDEBUG}
  1026. if not(cs_compilesystem in aktmoduleswitches) then
  1027. if (store_crc<>current_module.crc) and simplify_ppu then
  1028. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  1029. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1030. {$endif EXTDEBUG}
  1031. { remove static symtable (=refsymtable) here to save some mem }
  1032. if not (cs_local_browser in aktmoduleswitches) then
  1033. begin
  1034. st.free;
  1035. current_module.localsymtable:=nil;
  1036. end;
  1037. RestoreUnitSyms;
  1038. { leave when we got an error }
  1039. if (Errorcount>0) and not status.skip_error then
  1040. begin
  1041. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1042. status.skip_error:=true;
  1043. exit;
  1044. end;
  1045. end;
  1046. procedure proc_program(islibrary : boolean);
  1047. var
  1048. main_file: tinputfile;
  1049. st : tsymtable;
  1050. hp : tmodule;
  1051. begin
  1052. DLLsource:=islibrary;
  1053. Status.IsLibrary:=IsLibrary;
  1054. Status.IsExe:=true;
  1055. parse_only:=false;
  1056. { relocation works only without stabs under win32 !! PM }
  1057. { internal assembler uses rva for stabs info
  1058. so it should work with relocated DLLs }
  1059. if RelocSection and
  1060. (target_info.target=target_i386_win32) and
  1061. (target_info.assem<>as_i386_pecoff) then
  1062. begin
  1063. include(aktglobalswitches,cs_link_strip);
  1064. { Warning stabs info does not work with reloc section !! }
  1065. if cs_debuginfo in aktmoduleswitches then
  1066. begin
  1067. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1068. Message(parser_w_parser_win32_debug_needs_WN);
  1069. exclude(aktmoduleswitches,cs_debuginfo);
  1070. end;
  1071. end;
  1072. { get correct output names }
  1073. main_file := current_scanner.inputfile;
  1074. while assigned(main_file.next) do
  1075. main_file := main_file.next;
  1076. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1077. if islibrary then
  1078. begin
  1079. consume(_LIBRARY);
  1080. stringdispose(current_module.modulename);
  1081. current_module.modulename:=stringdup(pattern);
  1082. current_module.islibrary:=true;
  1083. exportlib.preparelib(pattern);
  1084. consume(_ID);
  1085. consume(_SEMICOLON);
  1086. end
  1087. else
  1088. { is there an program head ? }
  1089. if token=_PROGRAM then
  1090. begin
  1091. consume(_PROGRAM);
  1092. stringdispose(current_module.modulename);
  1093. stringdispose(current_module.realmodulename);
  1094. current_module.modulename:=stringdup(pattern);
  1095. current_module.realmodulename:=stringdup(orgpattern);
  1096. if (target_info.target=target_i386_WIN32) then
  1097. exportlib.preparelib(pattern);
  1098. consume(_ID);
  1099. if token=_LKLAMMER then
  1100. begin
  1101. consume(_LKLAMMER);
  1102. consume_idlist;
  1103. consume(_RKLAMMER);
  1104. end;
  1105. consume(_SEMICOLON);
  1106. end
  1107. else if (target_info.target=target_i386_WIN32) then
  1108. exportlib.preparelib(current_module.modulename^);
  1109. { global switches are read, so further changes aren't allowed }
  1110. current_module.in_global:=false;
  1111. { setup things using the global switches }
  1112. setupglobalswitches;
  1113. { set implementation flag }
  1114. current_module.in_implementation:=true;
  1115. { insert after the unit symbol tables the static symbol table }
  1116. { of the program }
  1117. st:=tstaticsymtable.create(current_module.modulename^);;
  1118. current_module.localsymtable:=st;
  1119. refsymtable:=st;
  1120. { load standard units (system,objpas,profile unit) }
  1121. loaddefaultunits;
  1122. { reset }
  1123. lexlevel:=0;
  1124. {Load the units used by the program we compile.}
  1125. if token=_USES then
  1126. loadunits;
  1127. tstoredsymtable(symtablestack).chainoperators;
  1128. { reset ranges/stabs in exported definitions }
  1129. reset_global_defs;
  1130. { All units are read, now give them a number }
  1131. current_module.numberunits;
  1132. {Insert the name of the main program into the symbol table.}
  1133. if current_module.realmodulename^<>'' then
  1134. st.insert(tunitsym.create(current_module.realmodulename^,tglobalsymtable(st)));
  1135. { ...is also constsymtable, this is the symtable where }
  1136. { the elements of enumeration types are inserted }
  1137. constsymtable:=st;
  1138. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1139. { reset }
  1140. procprefix:='';
  1141. {The program intialization needs an alias, so it can be called
  1142. from the bootstrap code.}
  1143. codegen_newprocedure;
  1144. if islibrary then
  1145. begin
  1146. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1147. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1148. aktprocdef.aliasnames.insert('PASCALMAIN');
  1149. { this code is called from C so we need to save some
  1150. registers }
  1151. include(aktprocdef.procoptions,po_savestdregs);
  1152. end
  1153. else
  1154. begin
  1155. gen_main_procsym('main',potype_proginit,st);
  1156. aktprocdef.aliasnames.insert('program_init');
  1157. aktprocdef.aliasnames.insert('PASCALMAIN');
  1158. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1159. end;
  1160. insertLocalThreadvarsTablesTable;
  1161. compile_proc_body(true,false);
  1162. { Add symbol to the exports section for win32 so smartlinking a
  1163. DLL will include the edata section }
  1164. if assigned(exportlib) and
  1165. (target_info.target=target_i386_win32) and
  1166. assigned(current_module._exports.first) then
  1167. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1168. { avoid self recursive destructor call !! PM }
  1169. aktprocdef.localst:=nil;
  1170. { consider these symbols as global ones for browser
  1171. but the typecasting of the globalsymtable with tglobalsymtable
  1172. can then lead to problems (PFV)
  1173. current_module.globalsymtable:=current_module.localsymtable;
  1174. current_module.localsymtable:=nil;}
  1175. If ResourceStrings.ResStrCount>0 then
  1176. begin
  1177. ResourceStrings.CreateResourceStringList;
  1178. { only write if no errors found }
  1179. if (Errorcount=0) then
  1180. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1181. end;
  1182. codegen_doneprocedure;
  1183. { finalize? }
  1184. if token=_FINALIZATION then
  1185. begin
  1186. { set module options }
  1187. current_module.flags:=current_module.flags or uf_finalize;
  1188. { Compile the finalize }
  1189. codegen_newprocedure;
  1190. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1191. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1192. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1193. compile_proc_body(true,false);
  1194. codegen_doneprocedure;
  1195. end;
  1196. { consume the last point }
  1197. consume(_POINT);
  1198. {$ifdef New_GDB}
  1199. write_gdb_info;
  1200. {$endIf Def New_GDB}
  1201. { leave when we got an error }
  1202. if (Errorcount>0) and not status.skip_error then
  1203. begin
  1204. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1205. status.skip_error:=true;
  1206. exit;
  1207. end;
  1208. { test static symtable }
  1209. if (Errorcount=0) then
  1210. begin
  1211. tstoredsymtable(st).allsymbolsused;
  1212. tstoredsymtable(st).allunitsused;
  1213. tstoredsymtable(st).allprivatesused;
  1214. end;
  1215. { generate imports }
  1216. if current_module.uses_imports then
  1217. importlib.generatelib;
  1218. if islibrary or
  1219. (target_info.target=target_i386_WIN32) or
  1220. (target_info.target=target_i386_NETWARE) then
  1221. exportlib.generatelib;
  1222. { insert heap }
  1223. insertResourceTablesTable;
  1224. insertinitfinaltable;
  1225. insertheap;
  1226. inserttargetspecific;
  1227. datasize:=symtablestack.datasize;
  1228. { finish asmlist by adding segment starts }
  1229. insertsegment;
  1230. { insert own objectfile }
  1231. insertobjectfile;
  1232. { assemble and link }
  1233. create_objectfile;
  1234. { leave when we got an error }
  1235. if (Errorcount>0) and not status.skip_error then
  1236. begin
  1237. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1238. status.skip_error:=true;
  1239. exit;
  1240. end;
  1241. { create the executable when we are at level 1 }
  1242. if (compile_level=1) then
  1243. begin
  1244. { insert all .o files from all loaded units }
  1245. hp:=tmodule(loaded_units.first);
  1246. while assigned(hp) do
  1247. begin
  1248. linker.AddModuleFiles(hp);
  1249. hp:=tmodule(hp.next);
  1250. end;
  1251. { write .def file }
  1252. if (cs_link_deffile in aktglobalswitches) then
  1253. deffile.writefile;
  1254. { finally we can create a executable }
  1255. if (not current_module.is_unit) then
  1256. begin
  1257. if DLLSource then
  1258. linker.MakeSharedLibrary
  1259. else
  1260. linker.MakeExecutable;
  1261. end;
  1262. end;
  1263. end;
  1264. end.
  1265. {
  1266. $Log$
  1267. Revision 1.55 2002-04-01 13:43:32 armin
  1268. addToLocalThreadvarList used '_'+name instead of mangledname to find asm symbol
  1269. Revision 1.54 2002/03/29 17:19:50 armin
  1270. + allow exports for netware
  1271. Revision 1.53 2002/03/29 09:00:56 armin
  1272. + forgot to delete a debug writeln
  1273. Revision 1.52 2002/03/28 16:07:52 armin
  1274. + initialize threadvars defined local in units
  1275. Revision 1.51 2002/01/24 18:25:49 peter
  1276. * implicit result variable generation for assembler routines
  1277. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1278. Revision 1.50 2001/12/09 03:34:58 carl
  1279. + Stack checking for solaris
  1280. Revision 1.49 2001/11/02 23:16:51 peter
  1281. * removed obsolete chainprocsym and test_procsym code
  1282. Revision 1.48 2001/11/02 22:58:05 peter
  1283. * procsym definition rewrite
  1284. Revision 1.47 2001/09/18 11:30:48 michael
  1285. * Fixes win32 linking problems with import libraries
  1286. * LINKLIB Libraries are now looked for using C file extensions
  1287. * get_exepath fix
  1288. Revision 1.46 2001/09/13 14:47:47 michael
  1289. + Committed patch from peter
  1290. Revision 1.45 2001/08/26 13:36:46 florian
  1291. * some cg reorganisation
  1292. * some PPC updates
  1293. Revision 1.44 2001/08/19 11:22:23 peter
  1294. * palmos support from v10 merged
  1295. Revision 1.43 2001/08/12 19:59:49 peter
  1296. * typo in extdebug msg
  1297. Revision 1.42 2001/08/06 21:40:47 peter
  1298. * funcret moved from tprocinfo to tprocdef
  1299. Revision 1.41 2001/08/05 12:26:52 peter
  1300. * beos fix (merged)
  1301. Revision 1.40 2001/08/04 10:23:54 peter
  1302. * updates so it works with the ide
  1303. Revision 1.38 2001/07/30 20:59:27 peter
  1304. * m68k updates from v10 merged
  1305. Revision 1.37 2001/06/18 20:36:25 peter
  1306. * -Ur switch (merged)
  1307. * masm fixes (merged)
  1308. * quoted filenames for go32v2 and win32
  1309. Revision 1.36 2001/06/06 21:58:16 peter
  1310. * Win32 fixes for Makefile so it doesn't require sh.exe
  1311. Revision 1.35 2001/06/03 21:57:36 peter
  1312. + hint directive parsing support
  1313. Revision 1.34 2001/06/03 15:15:31 peter
  1314. * dllprt0 stub for linux shared libs
  1315. * pass -init and -fini for linux shared libs
  1316. * libprefix splitted into staticlibprefix and sharedlibprefix
  1317. Revision 1.33 2001/05/19 23:05:19 peter
  1318. * support uses <unit> in <file> construction
  1319. Revision 1.32 2001/05/18 22:26:36 peter
  1320. * merged alignment for non-i386
  1321. Revision 1.31 2001/05/09 14:11:10 jonas
  1322. * range check error fixes from Peter
  1323. Revision 1.30 2001/05/06 14:49:17 peter
  1324. * ppu object to class rewrite
  1325. * move ppu read and write stuff to fppu
  1326. Revision 1.29 2001/04/18 22:01:57 peter
  1327. * registration of targets and assemblers
  1328. Revision 1.28 2001/04/13 18:08:37 peter
  1329. * scanner object to class
  1330. Revision 1.27 2001/04/13 01:22:12 peter
  1331. * symtable change to classes
  1332. * range check generation and errors fixed, make cycle DEBUG=1 works
  1333. * memory leaks fixed
  1334. Revision 1.26 2001/04/02 21:20:33 peter
  1335. * resulttype rewrite
  1336. Revision 1.25 2001/03/13 18:45:07 peter
  1337. * fixed some memory leaks
  1338. Revision 1.24 2001/03/06 18:28:02 peter
  1339. * patch from Pavel with a new and much faster DLL Scanner for
  1340. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1341. Revision 1.23 2001/02/24 10:44:56 peter
  1342. * generate .rst from ppufilename instead of modulename
  1343. Revision 1.22 2001/02/21 19:37:19 peter
  1344. * moved deref to be done after loading of implementation units. prederef
  1345. is still done directly after loading of symbols and definitions.
  1346. Revision 1.21 2001/01/14 22:13:52 peter
  1347. * fixed crash with program name as a important unit name
  1348. Revision 1.20 2000/12/25 00:07:27 peter
  1349. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1350. tlinkedlist objects)
  1351. Revision 1.19 2000/11/29 00:30:36 florian
  1352. * unused units removed from uses clause
  1353. * some changes for widestrings
  1354. Revision 1.18 2000/11/01 23:04:37 peter
  1355. * tprocdef.fullprocname added for better casesensitve writing of
  1356. procedures
  1357. Revision 1.17 2000/10/31 22:02:50 peter
  1358. * symtable splitted, no real code changes
  1359. Revision 1.16 2000/10/21 14:36:26 peter
  1360. * merged pierres fixes
  1361. Revision 1.15 2000/10/15 09:08:58 peter
  1362. * use System for the systemunit instead of target dependent
  1363. Revision 1.14 2000/10/15 07:47:51 peter
  1364. * unit names and procedure names are stored mixed case
  1365. Revision 1.13 2000/10/04 14:51:08 pierre
  1366. * IsExe restored
  1367. Revision 1.12 2000/09/30 16:07:40 peter
  1368. * filepos when unit not found (merged)
  1369. Revision 1.11 2000/09/24 21:33:47 peter
  1370. * message updates merges
  1371. Revision 1.10 2000/09/24 15:06:22 peter
  1372. * use defines.inc
  1373. Revision 1.9 2000/08/31 07:53:02 michael
  1374. + Applied patch from Peter
  1375. Revision 1.8 2000/08/29 19:00:01 peter
  1376. * _init and _finalize procsyms also need a $ prefix
  1377. Revision 1.7 2000/08/27 20:19:39 peter
  1378. * store strings with case in ppu, when an internal symbol is created
  1379. a '$' is prefixed so it's not automatic uppercased
  1380. Revision 1.6 2000/08/27 16:11:52 peter
  1381. * moved some util functions from globals,cobjects to cutils
  1382. * splitted files into finput,fmodule
  1383. Revision 1.5 2000/08/25 08:48:22 jonas
  1384. * fixed bug with include files at the very beginning of .pp/.pas files
  1385. (wrong name used for generating exe/checking unit name) (merged from
  1386. fixes branch)
  1387. Revision 1.4 2000/08/21 11:27:44 pierre
  1388. * fix the stabs problems
  1389. Revision 1.3 2000/07/13 12:08:26 michael
  1390. + patched to 1.1.0 with former 1.09patch from peter
  1391. Revision 1.2 2000/07/13 11:32:45 michael
  1392. + removed logs
  1393. }