pmodules.pas 39 KB

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