pmodules.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. {$define TEST_IMPL}
  20. interface
  21. uses
  22. files;
  23. procedure addlinkerfiles(hp:pmodule);
  24. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  25. procedure proc_unit;
  26. procedure proc_program(islibrary : boolean);
  27. implementation
  28. uses
  29. cobjects,verbose,systems,globals,
  30. symtable,aasm,hcodegen,
  31. link,assemble,import
  32. {$ifdef i386}
  33. ,i386
  34. {$endif}
  35. {$ifdef m68k}
  36. ,m68k
  37. {$endif}
  38. ,scanner,pbase,psystem,pdecl,psub,parser;
  39. procedure addlinkerfiles(hp:pmodule);
  40. begin
  41. with hp^ do
  42. begin
  43. while not linkofiles.empty do
  44. Linker.AddObject(linkofiles.Get);
  45. while not linksharedlibs.empty do
  46. Linker.AddSharedLibrary(linksharedlibs.Get);
  47. while not linkstaticlibs.empty do
  48. Linker.AddStaticLibrary(linkstaticlibs.Get);
  49. end;
  50. end;
  51. procedure insertsegment;
  52. procedure fixseg(p:paasmoutput;sec:tsection);
  53. begin
  54. p^.insert(new(pai_section,init(sec)));
  55. if (cs_smartlink in aktswitches) then
  56. p^.insert(new(pai_cut,init));
  57. p^.concat(new(pai_section,init(sec_none)));
  58. end;
  59. begin
  60. {Insert Ident of the compiler}
  61. if (not (cs_smartlink in aktswitches))
  62. {$ifndef EXTDEBUG}
  63. and (not current_module^.is_unit)
  64. {$endif}
  65. then
  66. begin
  67. datasegment^.insert(new(pai_align,init(4)));
  68. datasegment^.insert(new(pai_string,init('FPC '+version_string+' for '+target_string+' - '+target_info.short_name)));
  69. end;
  70. { Insert start and end of sections }
  71. fixseg(codesegment,sec_code);
  72. fixseg(datasegment,sec_data);
  73. fixseg(bsssegment,sec_bss);
  74. fixseg(consts,sec_data);
  75. end;
  76. procedure insertheap;
  77. begin
  78. if (cs_smartlink in aktswitches) then
  79. begin
  80. bsssegment^.concat(new(pai_cut,init));
  81. datasegment^.concat(new(pai_cut,init));
  82. end;
  83. { On the Macintosh Classic M68k Architecture
  84. The Heap variable is simply a POINTER to the
  85. real HEAP. The HEAP must be set up by the RTL
  86. and must store the pointer in this value.
  87. On OS/2 the heap is also intialized by the RTL. We do
  88. not output a pointer }
  89. case target_info.target of
  90. {$ifdef i386}
  91. target_OS2 : ;
  92. {$endif i386}
  93. {$ifdef m68k}
  94. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  95. {$endif m68k}
  96. else
  97. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  98. end;
  99. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  100. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  101. end;
  102. procedure inserttargetspecific;
  103. var
  104. i : longint;
  105. begin
  106. {$ifdef i386}
  107. case target_info.target of
  108. target_GO32V2 : begin
  109. { stacksize can be specified }
  110. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  111. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  112. end;
  113. target_WIN32 : begin
  114. { Generate an external entry to be sure that _mainCRTStarup will be
  115. linked, can't use concat_external because those aren't written for
  116. asw (PFV) }
  117. datasegment^.concat(new(pai_const,init_symbol('_mainCRTStartup')));
  118. { generate the last entry for the imports directory, is done
  119. in the ld script }
  120. { if not(assigned(importssection)) then
  121. importssection:=new(paasmoutput,init);
  122. importssection^.concat(new(pai_section,init_idata(3)));
  123. for i:=1 to 5 do
  124. importssection^.concat(new(pai_const,init_32bit(0))); }
  125. end;
  126. end;
  127. {$endif i386}
  128. end;
  129. procedure load_ppu(oldhp,hp : pmodule;compile_system : boolean);
  130. var
  131. loaded_unit : pmodule;
  132. b : byte;
  133. checksum,
  134. {$ifndef NEWPPU}
  135. count,
  136. {$endif NEWPPU}
  137. nextmapentry : longint;
  138. hs : string;
  139. begin
  140. { init the map }
  141. new(hp^.map);
  142. nextmapentry:=1;
  143. {$ifdef NEWPPU}
  144. { load the used units from interface }
  145. b:=hp^.ppufile^.readentry;
  146. if b=ibloadunit_int then
  147. begin
  148. while not hp^.ppufile^.endofentry do
  149. begin
  150. hs:=hp^.ppufile^.getstring;
  151. checksum:=hp^.ppufile^.getlongint;
  152. loaded_unit:=loadunit(hs,false,false);
  153. if hp^.compiled then
  154. exit;
  155. { if the crc of a used unit is the same as written to the
  156. PPU file, we needn't to recompile the current unit }
  157. if (loaded_unit^.crc<>checksum) then
  158. begin
  159. { we have to compile the current unit remove stuff which isn't
  160. needed }
  161. { forget the map }
  162. dispose(hp^.map);
  163. hp^.map:=nil;
  164. { remove the ppufile }
  165. dispose(hp^.ppufile,done);
  166. hp^.ppufile:=nil;
  167. { recompile or give an fatal error }
  168. if not(hp^.sources_avail) then
  169. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  170. else
  171. begin
  172. if assigned(oldhp^.current_inputfile) then
  173. oldhp^.current_inputfile^.tempclose;
  174. compile(hp^.mainsource^,compile_system);
  175. if (not oldhp^.compiled) and assigned(oldhp^.current_inputfile) then
  176. oldhp^.current_inputfile^.tempreopen;
  177. end;
  178. exit;
  179. end;
  180. { setup the map entry for deref }
  181. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  182. inc(nextmapentry);
  183. if nextmapentry>maxunits then
  184. Message(unit_f_too_much_units);
  185. end;
  186. { ok, now load the unit }
  187. hp^.symtable:=new(punitsymtable,load(hp));
  188. { if this is the system unit insert the intern symbols }
  189. make_ref:=false;
  190. if compile_system then
  191. insertinternsyms(psymtable(hp^.symtable));
  192. make_ref:=true;
  193. end;
  194. { now only read the implementation part }
  195. hp^.in_implementation:=true;
  196. { load the used units from implementation }
  197. b:=hp^.ppufile^.readentry;
  198. if b=ibloadunit_imp then
  199. begin
  200. while not hp^.ppufile^.endofentry do
  201. begin
  202. hs:=hp^.ppufile^.getstring;
  203. checksum:=hp^.ppufile^.getlongint;
  204. loaded_unit:=loadunit(hs,false,false);
  205. if hp^.compiled then
  206. exit;
  207. end;
  208. end;
  209. {$ifdef NEWPPU}
  210. { The next entry should be an ibendimplementation }
  211. b:=hp^.ppufile^.readentry;
  212. if b <> ibendimplementation then
  213. Message1(unit_f_ppu_invalid_entry,tostr(b));
  214. { The next entry should be an ibend }
  215. b:=hp^.ppufile^.readentry;
  216. if b <> ibend then
  217. Message1(unit_f_ppu_invalid_entry,tostr(b));
  218. {$endif}
  219. hp^.ppufile^.close;
  220. {! dispose(hp^.ppufile,done);}
  221. {$else}
  222. { load the used units from interface }
  223. hp^.ppufile^.read_data(b,1,count);
  224. while (b=ibloadunit) do
  225. begin
  226. { read unit name }
  227. hp^.ppufile^.read_data(hs[0],1,count);
  228. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  229. hp^.ppufile^.read_data(checksum,4,count);
  230. loaded_unit:=loadunit(hs,false,false);
  231. if hp^.compiled then
  232. exit;
  233. { if the crc of a used unit is the same as }
  234. { written to the PPU file, we needn't to }
  235. { recompile the current unit }
  236. if (loaded_unit^.crc<>checksum) then
  237. begin
  238. { we have to compile the current unit }
  239. { remove stuff which isn't needed }
  240. { forget the map }
  241. dispose(hp^.map);
  242. hp^.map:=nil;
  243. hp^.ppufile^.close;
  244. dispose(hp^.ppufile,done);
  245. hp^.ppufile:=nil;
  246. if not(hp^.sources_avail) then
  247. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  248. else
  249. begin
  250. if assigned(oldhp^.current_inputfile) then
  251. oldhp^.current_inputfile^.tempclose;
  252. compile(hp^.mainsource^,compile_system);
  253. if (not oldhp^.compiled) and assigned(oldhp^.current_inputfile) then
  254. oldhp^.current_inputfile^.tempreopen;
  255. end;
  256. exit;
  257. end;
  258. { setup the map entry for deref }
  259. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  260. inc(nextmapentry);
  261. if nextmapentry>maxunits then
  262. Message(unit_f_too_much_units);
  263. { read until ibend }
  264. hp^.ppufile^.read_data(b,1,count);
  265. end;
  266. { ok, now load the unit }
  267. hp^.symtable:=new(punitsymtable,load(hp));
  268. { if this is the system unit insert the intern }
  269. { symbols }
  270. make_ref:=false;
  271. if compile_system then
  272. insertinternsyms(psymtable(hp^.symtable));
  273. make_ref:=true;
  274. { now only read the implementation part }
  275. hp^.in_implementation:=true;
  276. { load the used units from implementation }
  277. hp^.ppufile^.read_data(b,1,count);
  278. while (b<>ibend) and (b=ibloadunit) do
  279. begin
  280. { read unit name }
  281. hp^.ppufile^.read_data(hs[0],1,count);
  282. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  283. hp^.ppufile^.read_data(checksum,4,count);
  284. loaded_unit:=loadunit(hs,false,false);
  285. if hp^.compiled then exit;
  286. { if the crc of a used unit is the same as }
  287. { written to the PPU file, we needn't to }
  288. { recompile the current unit }
  289. { but for the implementation part }
  290. { the written crc is false, because }
  291. { not defined when writing the ppufile !! }
  292. {$ifdef TEST_IMPL}
  293. if (checksum<>0) and (loaded_unit^.crc<>checksum) then
  294. begin
  295. { we have to compile the current unit }
  296. { remove stuff which isn't needed }
  297. { forget the map }
  298. dispose(hp^.map);
  299. hp^.map:=nil;
  300. hp^.ppufile^.close;
  301. dispose(hp^.ppufile,done);
  302. hp^.ppufile:=nil;
  303. if not(hp^.sources_avail) then
  304. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  305. else
  306. begin
  307. oldhp^.current_inputfile^.tempclose;
  308. compile(hp^.mainsource^,compile_system);
  309. oldhp^.current_inputfile^.tempclose;
  310. end;
  311. exit;
  312. end;
  313. {$endif TEST_IMPL}
  314. { read until ibend }
  315. hp^.ppufile^.read_data(b,1,count);
  316. end;
  317. hp^.ppufile^.close;
  318. {$endif}
  319. dispose(hp^.map);
  320. hp^.map:=nil;
  321. end;
  322. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  323. var
  324. st : punitsymtable;
  325. old_current_module,hp,nextmodule : pmodule;
  326. pu : pused_unit;
  327. hs : pstring;
  328. begin
  329. old_current_module:=current_module;
  330. { be sure not to mix lines from different files }
  331. { update_line; }
  332. { unit not found }
  333. st:=nil;
  334. { search all loaded units }
  335. hp:=pmodule(loaded_units.first);
  336. while assigned(hp) do
  337. begin
  338. if hp^.modulename^=s then
  339. begin
  340. { the unit is already registered }
  341. { and this means that the unit }
  342. { is already compiled }
  343. { else there is a cyclic unit use }
  344. if assigned(hp^.symtable) then
  345. st:=punitsymtable(hp^.symtable)
  346. else
  347. begin
  348. { recompile the unit ? }
  349. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  350. Message(unit_f_circular_unit_reference);
  351. end;
  352. break;
  353. end;
  354. { the next unit }
  355. hp:=pmodule(hp^.next);
  356. end;
  357. { no error and the unit isn't loaded }
  358. if not(assigned(hp)) and (st=nil) then
  359. begin
  360. { generates a new unit info record }
  361. hp:=new(pmodule,init(s,true));
  362. { now we can register the unit }
  363. loaded_units.insert(hp);
  364. current_module:=hp;
  365. { force build ? }
  366. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  367. begin
  368. { we needn't the ppufile }
  369. if assigned(hp^.ppufile) then
  370. begin
  371. dispose(hp^.ppufile,done);
  372. hp^.ppufile:=nil;
  373. end;
  374. if not(hp^.sources_avail) then
  375. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  376. else
  377. begin
  378. if assigned(old_current_module^.current_inputfile) then
  379. old_current_module^.current_inputfile^.tempclose;
  380. compile(hp^.mainsource^,compile_system);
  381. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  382. old_current_module^.current_inputfile^.tempreopen;
  383. end;
  384. end
  385. else
  386. begin
  387. { only reassemble ? }
  388. if (hp^.do_assemble) then
  389. OnlyAsm(hp^.asmfilename^);
  390. { we should know there the PPU file else it's an error and
  391. we can't load the unit }
  392. {$ifdef NEWPPU}
  393. { if hp^.ppufile^.name^<>'' then}
  394. {$else}
  395. if hp^.ppufile^.name^<>'' then
  396. {$endif}
  397. load_ppu(old_current_module,hp,compile_system);
  398. { add the files for the linker }
  399. addlinkerfiles(hp);
  400. end;
  401. { register the unit _once_ }
  402. usedunits.concat(new(pused_unit,init(hp,0)));
  403. { the unit is written, so we can set the symtable type }
  404. { to unitsymtable, else we get some dupid errors }
  405. { this is not the right place because of the }
  406. { ready label }
  407. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  408. { placed at this end of proc_unit }
  409. psymtable(hp^.symtable)^.unitid:=0;
  410. { reset the unitnumbers for the other units }
  411. pu:=pused_unit(old_current_module^.used_units.first);
  412. while assigned(pu) do
  413. begin
  414. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  415. pu:=pused_unit(pu^.next);
  416. end;
  417. end
  418. else
  419. if assigned(hp) and (st=nil) then
  420. begin
  421. { we have to compile the unit again, but it is already inserted !!}
  422. { we may have problem with the lost symtable !! }
  423. current_module:=hp;
  424. { we must preserve the unit chain }
  425. nextmodule:=pmodule(hp^.next);
  426. { we have to cleanup a little }
  427. hp^.special_done;
  428. new(hs);
  429. hs^:=hp^.mainsource^;
  430. hp^.init(hs^,true);
  431. dispose(hs);
  432. { we must preserve the unit chain }
  433. hp^.next:=nextmodule;
  434. if assigned(hp^.ppufile) then
  435. load_ppu(old_current_module,hp,compile_system)
  436. else
  437. begin
  438. {$ifdef UseBrowser}
  439. { here we need to remove the names ! }
  440. hp^.sourcefiles.done;
  441. hp^.sourcefiles.init;
  442. {$endif not UseBrowser}
  443. if assigned(old_current_module^.current_inputfile) then
  444. old_current_module^.current_inputfile^.tempclose;
  445. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  446. compile(hp^.mainsource^,compile_system);
  447. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  448. old_current_module^.current_inputfile^.tempreopen;
  449. end;
  450. current_module^.compiled:=true;
  451. end;
  452. { set the old module }
  453. current_module:=old_current_module;
  454. { the current module uses the unit hp }
  455. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  456. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  457. if in_uses and not current_module^.in_implementation then
  458. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  459. loadunit:=hp;
  460. end;
  461. procedure loadunits;
  462. var
  463. s : stringid;
  464. hp : pused_unit;
  465. hp2 : pmodule;
  466. hp3 : psymtable;
  467. oldprocsym:Pprocsym;
  468. begin
  469. oldprocsym:=aktprocsym;
  470. consume(_USES);
  471. {$ifdef DEBUG}
  472. test_symtablestack;
  473. {$endif DEBUG}
  474. repeat
  475. s:=pattern;
  476. consume(ID);
  477. hp2:=loadunit(s,false,true);
  478. if current_module^.compiled then
  479. exit;
  480. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  481. if token=COMMA then
  482. begin
  483. pattern:='';
  484. consume(COMMA);
  485. end
  486. else
  487. break;
  488. until false;
  489. consume(SEMICOLON);
  490. { now insert the units in the symtablestack }
  491. hp:=pused_unit(current_module^.used_units.first);
  492. { set the symtable to systemunit so it gets reorderd correctly }
  493. symtablestack:=systemunit;
  494. while assigned(hp) do
  495. begin
  496. {$IfDef GDB}
  497. if (cs_debuginfo in aktswitches) and
  498. not hp^.is_stab_written then
  499. begin
  500. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  501. hp^.is_stab_written:=true;
  502. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  503. end;
  504. {$EndIf GDB}
  505. if hp^.in_uses then
  506. begin
  507. hp3:=symtablestack;
  508. while assigned(hp3) do
  509. begin
  510. { insert units only once ! }
  511. if hp^.u^.symtable=hp3 then
  512. break;
  513. hp3:=hp3^.next;
  514. { unit isn't inserted }
  515. if hp3=nil then
  516. begin
  517. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  518. symtablestack:=psymtable(hp^.u^.symtable);
  519. {$ifdef CHAINPROCSYMS}
  520. symtablestack^.chainprocsyms;
  521. {$endif CHAINPROCSYMS}
  522. {$ifdef DEBUG}
  523. test_symtablestack;
  524. {$endif DEBUG}
  525. end;
  526. end;
  527. end;
  528. hp:=pused_unit(hp^.next);
  529. end;
  530. aktprocsym:=oldprocsym;
  531. end;
  532. procedure parse_implementation_uses(symt:Psymtable);
  533. var
  534. old_module_in_implementation : boolean;
  535. begin
  536. if token=_USES then
  537. begin
  538. old_module_in_implementation:=module_in_implementation;
  539. module_in_implementation:=true;
  540. current_module^.in_implementation:=true;
  541. symt^.symtabletype:=unitsymtable;
  542. loadunits;
  543. symt^.symtabletype:=globalsymtable;
  544. {$ifdef DEBUG}
  545. test_symtablestack;
  546. {$endif DEBUG}
  547. module_in_implementation:=old_module_in_implementation;
  548. end;
  549. end;
  550. procedure proc_unit;
  551. var
  552. { unitname : stringid; }
  553. names:Tstringcontainer;
  554. p : psymtable;
  555. unitst : punitsymtable;
  556. pu : pused_unit;
  557. s1,s2 : ^string; {Saves stack space}
  558. begin
  559. consume(_UNIT);
  560. if token=ID then
  561. begin
  562. { create filenames and unit name }
  563. current_module^.SetFileName(current_module^.current_inputfile^.path^,current_module^.current_inputfile^.name^);
  564. stringdispose(current_module^.modulename);
  565. current_module^.modulename:=stringdup(upper(pattern));
  566. { check for system unit }
  567. new(s1);
  568. new(s2);
  569. s1^:=upper(target_info.system_unit);
  570. s2^:=upper(current_module^.current_inputfile^.name^);
  571. if (cs_compilesystem in aktswitches) then
  572. begin
  573. if (cs_check_unit_name in aktswitches) and
  574. ((length(current_module^.modulename^)>8) or
  575. (current_module^.modulename^<>s1^) or
  576. (current_module^.modulename^<>s2^)) then
  577. Message1(unit_e_illegal_unit_name,s1^);
  578. end
  579. else
  580. if (current_module^.modulename^=s1^) then
  581. Message(unit_w_switch_us_missed);
  582. dispose(s2);
  583. dispose(s1);
  584. { Add Object File }
  585. if (cs_smartlink in aktswitches) then
  586. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  587. else
  588. current_module^.linkofiles.insert(current_module^.objfilename^);
  589. end;
  590. consume(ID);
  591. consume(SEMICOLON);
  592. consume(_INTERFACE);
  593. { this should be placed after uses !!}
  594. {$ifndef UseNiceNames}
  595. procprefix:='_'+current_module^.modulename^+'$$';
  596. {$else UseNiceNames}
  597. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  598. {$endif UseNiceNames}
  599. parse_only:=true;
  600. { generate now the global symboltable }
  601. p:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  602. refsymtable:=p;
  603. unitst:=punitsymtable(p);
  604. { the unit name must be usable as a unit specifier }
  605. { inside the unit itself (PM) }
  606. { this also forbids to have another symbol }
  607. { with the same name as the unit }
  608. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  609. { set the symbol table for the current unit }
  610. { this must be set later for interdependency }
  611. { current_module^.symtable:=psymtable(p); }
  612. { a unit compiled at command line must be inside the loaded_unit list }
  613. if (compile_level=1) then
  614. begin
  615. loaded_units.insert(current_module);
  616. if cs_unit_to_lib in initswitches then
  617. begin
  618. current_module^.flags:=current_module^.flags or uf_in_library;
  619. if cs_shared_lib in initswitches then
  620. current_module^.flags:=current_module^.flags or uf_shared_library;
  621. end;
  622. end;
  623. { insert qualifier for the system unit (allows system.writeln) }
  624. if not(cs_compilesystem in aktswitches) then
  625. begin
  626. { insert the system unit }
  627. { it is allways the first }
  628. systemunit^.next:=nil;
  629. symtablestack:=systemunit;
  630. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  631. if token=_USES then
  632. begin
  633. unitst^.symtabletype:=unitsymtable;
  634. loadunits;
  635. { has it been compiled at a higher level ?}
  636. if current_module^.compiled then
  637. exit;
  638. unitst^.symtabletype:=globalsymtable;
  639. end;
  640. { ... but insert the symbol table later }
  641. p^.next:=symtablestack;
  642. symtablestack:=p;
  643. end
  644. else
  645. { while compiling a system unit, some types are directly inserted }
  646. begin
  647. p^.next:=symtablestack;
  648. symtablestack:=p;
  649. insert_intern_types(p);
  650. end;
  651. { displaced for inter-dependency considerations }
  652. current_module^.symtable:=psymtable(p);
  653. constsymtable:=symtablestack;
  654. { ... parse the declarations }
  655. read_interface_declarations;
  656. consume(_IMPLEMENTATION);
  657. parse_only:=false;
  658. refsymtable^.number_defs;
  659. {$ifdef GDB}
  660. { add all used definitions even for implementation}
  661. if (cs_debuginfo in aktswitches) then
  662. begin
  663. { all types }
  664. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  665. { and all local symbols}
  666. refsymtable^.concatstabto(debuglist);
  667. end;
  668. {$endif GDB}
  669. { for interdependent units
  670. the crc is included in the ppufile
  671. but it is not known when writing the first ppufile
  672. so I tried to add a fake writing of the ppu
  673. just to get the CRC
  674. but the result is different for the real CRC
  675. it calculates after, I don't know why
  676. Answer:
  677. -------
  678. When reading the interface part, the compiler assumes
  679. that all registers are modified by a procedure
  680. usedinproc:=$ff !
  681. If the definition is read, the compiler determines
  682. the used registers and write the correct value
  683. to usedinproc
  684. only_calculate_crc:=true;
  685. writeunitas(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^+
  686. +'.PPS',punitsymtable(symtablestack));
  687. only_calculate_crc:=false;
  688. }
  689. { generates static symbol table }
  690. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  691. { must be done only after _USES !! (PM)
  692. refsymtable:=p;}
  693. {Generate a procsym.}
  694. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  695. aktprocsym^.definition:=new(Pprocdef,init);
  696. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  697. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  698. {The generated procsym has a local symtable. Discard it and turn
  699. it into the static one.}
  700. dispose(aktprocsym^.definition^.localst,done);
  701. aktprocsym^.definition^.localst:=p;
  702. { testing !!!!!!!!! }
  703. { we set the interface part as a unitsymtable }
  704. { for the case we need to compile another unit }
  705. { remove the globalsymtable from the symtable stack }
  706. { to reinsert it after loading the implementation units }
  707. symtablestack:=unitst^.next;
  708. parse_implementation_uses(unitst);
  709. { now we can change refsymtable }
  710. refsymtable:=p;
  711. { but reinsert the global symtable as lasts }
  712. unitst^.next:=symtablestack;
  713. symtablestack:=unitst;
  714. {$ifdef DEBUG}
  715. test_symtablestack;
  716. {$endif DEBUG}
  717. constsymtable:=symtablestack;
  718. {$ifdef Splitheap}
  719. if testsplit then
  720. begin
  721. Split_Heap;
  722. allow_special:=true;
  723. Switch_to_temp_heap;
  724. end;
  725. { it will report all crossings }
  726. allow_special:=false;
  727. {$endif Splitheap}
  728. { set some informations }
  729. procinfo.retdef:=voiddef;
  730. procinfo._class:=nil;
  731. procinfo.call_offset:=8;
  732. { for temporary values }
  733. procinfo.framepointer:=frame_pointer;
  734. { clear flags }
  735. procinfo.flags:=0;
  736. {Reset the codegenerator.}
  737. codegen_newprocedure;
  738. names.init;
  739. names.insert(current_module^.modulename^+'_init');
  740. names.insert('INIT$$'+current_module^.modulename^);
  741. compile_proc_body(names,true,false);
  742. names.done;
  743. codegen_doneprocedure;
  744. consume(POINT);
  745. { size of the static data }
  746. datasize:=symtablestack^.datasize;
  747. { unsed static symbols ? }
  748. symtablestack^.allsymbolsused;
  749. {$ifdef GDB}
  750. { add all used definitions even for implementation}
  751. if (cs_debuginfo in aktswitches) then
  752. begin
  753. { all types }
  754. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  755. { and all local symbols}
  756. symtablestack^.concatstabto(debuglist);
  757. end;
  758. {$endif GDB}
  759. current_module^.in_implementation:=false;
  760. { deletes all symtables generated in the implementation part }
  761. while symtablestack^.symtabletype<>globalsymtable do
  762. dellexlevel;
  763. { tests, if all forwards are resolved }
  764. symtablestack^.check_forwards;
  765. symtablestack^.symtabletype:=unitsymtable;
  766. punitsymtable(symtablestack)^.is_stab_written:=false;
  767. {Write out the unit if the compile was succesfull.}
  768. if status.errorcount=0 then
  769. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  770. pu:=pused_unit(usedunits.first);
  771. while assigned(pu) do
  772. begin
  773. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  774. pu:=pused_unit(pu^.next);
  775. end;
  776. inc(datasize,symtablestack^.datasize);
  777. { generate imports }
  778. if current_module^.uses_imports then
  779. importlib^.generatelib;
  780. { finish asmlist by adding segment starts }
  781. insertsegment;
  782. end;
  783. procedure proc_program(islibrary : boolean);
  784. var
  785. st : psymtable;
  786. names : Tstringcontainer;
  787. begin
  788. { Trying to compile the system unit... }
  789. { if no unit defined... then issue a }
  790. { fatal error (avoids pointer problems)}
  791. { when referencing the non-existant }
  792. { system unit. }
  793. { System Unit should be compiled using proc_unit !! (PFV) }
  794. { if (cs_compilesystem in aktswitches) then
  795. Begin
  796. if token<>_UNIT then
  797. Message1(scan_f_syn_expected,'UNIT');
  798. consume(_UNIT);
  799. end;}
  800. parse_only:=false;
  801. if islibrary then
  802. begin
  803. consume(_LIBRARY);
  804. stringdispose(current_module^.modulename);
  805. current_module^.modulename:=stringdup(pattern);
  806. consume(ID);
  807. consume(SEMICOLON);
  808. end
  809. else
  810. { is there an program head ? }
  811. if token=_PROGRAM then
  812. begin
  813. consume(_PROGRAM);
  814. stringdispose(current_module^.modulename);
  815. current_module^.modulename:=stringdup(pattern);
  816. consume(ID);
  817. if token=LKLAMMER then
  818. begin
  819. consume(LKLAMMER);
  820. idlist;
  821. consume(RKLAMMER);
  822. end;
  823. consume(SEMICOLON);
  824. end;
  825. { insert after the unit symbol tables the static symbol table }
  826. { of the program }
  827. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  828. {Generate a procsym.}
  829. aktprocsym:=new(Pprocsym,init('main'));
  830. aktprocsym^.definition:=new(Pprocdef,init);
  831. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  832. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  833. {The localst is a local symtable. Change it into the static
  834. symtable.}
  835. dispose(aktprocsym^.definition^.localst,done);
  836. aktprocsym^.definition^.localst:=st;
  837. refsymtable:=st;
  838. { necessary for browser }
  839. loaded_units.insert(current_module);
  840. {Insert the symbols of the system unit into the stack of symbol
  841. tables.}
  842. symtablestack:=systemunit;
  843. systemunit^.next:=nil;
  844. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  845. {Load the units used by the program we compile.}
  846. if token=_USES then
  847. loadunits;
  848. {Insert the name of the main program into the symbol table.}
  849. if current_module^.modulename^<>'' then
  850. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  851. { ...is also constsymtable, this is the symtable where }
  852. { the elements of enumeration types are inserted }
  853. constsymtable:=st;
  854. { set some informations about the main program }
  855. with procinfo do
  856. begin
  857. retdef:=voiddef;
  858. _class:=nil;
  859. call_offset:=8;
  860. framepointer:=frame_pointer;
  861. flags:=0;
  862. end;
  863. procprefix:='';
  864. in_except_block:=false;
  865. codegen_newprocedure;
  866. {The program intialization needs an alias, so it can be called
  867. from the bootstrap code.}
  868. names.init;
  869. names.insert('program_init');
  870. names.insert('PASCALMAIN');
  871. names.insert(target_os.cprefix+'main');
  872. compile_proc_body(names,true,false);
  873. names.done;
  874. codegen_doneprocedure;
  875. consume(POINT);
  876. if (cs_smartlink in aktswitches) then
  877. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  878. else
  879. current_module^.linkofiles.insert(current_module^.objfilename^);
  880. { insert heap }
  881. insertheap;
  882. { generate imports }
  883. if current_module^.uses_imports then
  884. importlib^.generatelib;
  885. inserttargetspecific;
  886. datasize:=symtablestack^.datasize;
  887. { finish asmlist by adding segment starts }
  888. insertsegment;
  889. end;
  890. end.
  891. {
  892. $Log$
  893. Revision 1.25 1998-06-08 22:59:49 peter
  894. * smartlinking works for win32
  895. * some defines to exclude some compiler parts
  896. Revision 1.24 1998/06/08 13:13:44 pierre
  897. + temporary variables now in temp_gen.pas unit
  898. because it is processor independent
  899. * mppc68k.bat modified to undefine i386 and support_mmx
  900. (which are defaults for i386)
  901. Revision 1.23 1998/06/05 17:47:29 peter
  902. * some better uses clauses
  903. Revision 1.22 1998/06/05 14:37:34 pierre
  904. * fixes for inline for operators
  905. * inline procedure more correctly restricted
  906. Revision 1.21 1998/06/04 23:51:53 peter
  907. * m68k compiles
  908. + .def file creation moved to gendef.pas so it could also be used
  909. for win32
  910. Revision 1.20 1998/06/04 09:55:42 pierre
  911. * demangled name of procsym reworked to become independant of the mangling scheme
  912. Revision 1.19 1998/06/03 23:40:38 peter
  913. + unlimited file support, release tempclose
  914. Revision 1.18 1998/06/03 22:49:00 peter
  915. + wordbool,longbool
  916. * rename bis,von -> high,low
  917. * moved some systemunit loading/creating to psystem.pas
  918. Revision 1.17 1998/05/28 14:40:25 peter
  919. * fixes for newppu, remake3 works now with it
  920. Revision 1.16 1998/05/27 19:45:06 peter
  921. * symtable.pas splitted into includefiles
  922. * symtable adapted for $ifdef NEWPPU
  923. Revision 1.15 1998/05/23 01:21:22 peter
  924. + aktasmmode, aktoptprocessor, aktoutputformat
  925. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  926. + $LIBNAME to set the library name where the unit will be put in
  927. * splitted cgi386 a bit (codeseg to large for bp7)
  928. * nasm, tasm works again. nasm moved to ag386nsm.pas
  929. Revision 1.14 1998/05/20 09:42:35 pierre
  930. + UseTokenInfo now default
  931. * unit in interface uses and implementation uses gives error now
  932. * only one error for unknown symbol (uses lastsymknown boolean)
  933. the problem came from the label code !
  934. + first inlined procedures and function work
  935. (warning there might be allowed cases were the result is still wrong !!)
  936. * UseBrower updated gives a global list of all position of all used symbols
  937. with switch -gb
  938. Revision 1.13 1998/05/12 10:47:00 peter
  939. * moved printstatus to verb_def
  940. + V_Normal which is between V_Error and V_Warning and doesn't have a
  941. prefix like error: warning: and is included in V_Default
  942. * fixed some messages
  943. * first time parameter scan is only for -v and -T
  944. - removed old style messages
  945. Revision 1.12 1998/05/11 13:07:56 peter
  946. + $ifdef NEWPPU for the new ppuformat
  947. + $define GDB not longer required
  948. * removed all warnings and stripped some log comments
  949. * no findfirst/findnext anymore to remove smartlink *.o files
  950. Revision 1.11 1998/05/06 18:36:53 peter
  951. * tai_section extended with code,data,bss sections and enumerated type
  952. * ident 'compiled by FPC' moved to pmodules
  953. * small fix for smartlink
  954. Revision 1.10 1998/05/04 17:54:28 peter
  955. + smartlinking works (only case jumptable left todo)
  956. * redesign of systems.pas to support assemblers and linkers
  957. + Unitname is now also in the PPU-file, increased version to 14
  958. Revision 1.9 1998/05/01 16:38:45 florian
  959. * handling of private and protected fixed
  960. + change_keywords_to_tp implemented to remove
  961. keywords which aren't supported by tp
  962. * break and continue are now symbols of the system unit
  963. + widestring, longstring and ansistring type released
  964. Revision 1.8 1998/04/30 15:59:41 pierre
  965. * GDB works again better :
  966. correct type info in one pass
  967. + UseTokenInfo for better source position
  968. * fixed one remaining bug in scanner for line counts
  969. * several little fixes
  970. Revision 1.7 1998/04/29 10:33:59 pierre
  971. + added some code for ansistring (not complete nor working yet)
  972. * corrected operator overloading
  973. * corrected nasm output
  974. + started inline procedures
  975. + added starstarn : use ** for exponentiation (^ gave problems)
  976. + started UseTokenInfo cond to get accurate positions
  977. Revision 1.6 1998/04/27 23:10:28 peter
  978. + new scanner
  979. * $makelib -> if smartlink
  980. * small filename fixes pmodule.setfilename
  981. * moved import from files.pas -> import.pas
  982. Revision 1.5 1998/04/14 23:27:03 florian
  983. + exclude/include with constant second parameter added
  984. Revision 1.4 1998/04/10 14:41:43 peter
  985. * removed some Hints
  986. * small speed optimization for AsmLn
  987. Revision 1.3 1998/04/03 09:51:00 daniel
  988. * Fixed heap allocation for OS/2.
  989. }