pmodules.pas 39 KB

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