pmodules.pas 39 KB

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