pmodules.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {define TEST_IMPL does not work well }
  20. interface
  21. uses
  22. files;
  23. procedure addlinkerfiles(hp:pmodule);
  24. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  25. procedure proc_unit;
  26. procedure proc_program(islibrary : boolean);
  27. implementation
  28. uses
  29. cobjects,verbose,systems,globals,
  30. symtable,aasm,hcodegen,
  31. link,assemble,import
  32. {$ifdef NEWPPU}
  33. ,ppu
  34. {$endif NEWPPU}
  35. {$ifdef i386}
  36. ,i386
  37. {$endif}
  38. {$ifdef m68k}
  39. ,m68k
  40. {$endif}
  41. ,scanner,pbase,psystem,pdecl,psub,parser;
  42. procedure addlinkerfiles(hp:pmodule);
  43. begin
  44. with hp^ do
  45. begin
  46. while not linkofiles.empty do
  47. Linker.AddObject(linkofiles.Get);
  48. while not linksharedlibs.empty do
  49. Linker.AddSharedLibrary(linksharedlibs.Get);
  50. while not linkstaticlibs.empty do
  51. Linker.AddStaticLibrary(linkstaticlibs.Get);
  52. end;
  53. end;
  54. procedure insertsegment;
  55. procedure fixseg(p:paasmoutput;sec:tsection);
  56. begin
  57. p^.insert(new(pai_section,init(sec)));
  58. if (cs_smartlink in aktswitches) then
  59. p^.insert(new(pai_cut,init));
  60. p^.concat(new(pai_section,init(sec_none)));
  61. end;
  62. begin
  63. {Insert Ident of the compiler}
  64. if (not (cs_smartlink in aktswitches))
  65. {$ifndef EXTDEBUG}
  66. and (not current_module^.is_unit)
  67. {$endif}
  68. then
  69. begin
  70. datasegment^.insert(new(pai_align,init(4)));
  71. datasegment^.insert(new(pai_string,init('FPC '+version_string+' for '+target_string+' - '+target_info.short_name)));
  72. end;
  73. { Insert start and end of sections }
  74. fixseg(codesegment,sec_code);
  75. fixseg(datasegment,sec_data);
  76. fixseg(bsssegment,sec_bss);
  77. fixseg(consts,sec_data);
  78. end;
  79. procedure insertheap;
  80. begin
  81. if (cs_smartlink in aktswitches) then
  82. begin
  83. bsssegment^.concat(new(pai_cut,init));
  84. datasegment^.concat(new(pai_cut,init));
  85. end;
  86. { On the Macintosh Classic M68k Architecture
  87. The Heap variable is simply a POINTER to the
  88. real HEAP. The HEAP must be set up by the RTL
  89. and must store the pointer in this value.
  90. On OS/2 the heap is also intialized by the RTL. We do
  91. not output a pointer }
  92. case target_info.target of
  93. {$ifdef i386}
  94. target_OS2 : ;
  95. {$endif i386}
  96. {$ifdef m68k}
  97. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  98. {$endif m68k}
  99. else
  100. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  101. end;
  102. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  103. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  104. end;
  105. procedure inserttargetspecific;
  106. begin
  107. {$ifdef i386}
  108. case target_info.target of
  109. target_GO32V2 : begin
  110. { stacksize can be specified }
  111. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  112. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  113. end;
  114. target_WIN32 : begin
  115. { Generate an external entry to be sure that _mainCRTStarup will be
  116. linked, can't use concat_external because those aren't written for
  117. asw (PFV) }
  118. datasegment^.concat(new(pai_const,init_symbol('_mainCRTStartup')));
  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. { generates static symbol table }
  664. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  665. {Generate a procsym.}
  666. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  667. aktprocsym^.definition:=new(Pprocdef,init);
  668. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  669. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  670. {The generated procsym has a local symtable. Discard it and turn
  671. it into the static one.}
  672. dispose(aktprocsym^.definition^.localst,done);
  673. aktprocsym^.definition^.localst:=p;
  674. { remove the globalsymtable from the symtable stack }
  675. { to reinsert it after loading the implementation units }
  676. symtablestack:=unitst^.next;
  677. { Read the implementation units }
  678. parse_implementation_uses(unitst);
  679. { now we can change refsymtable }
  680. refsymtable:=p;
  681. { but reinsert the global symtable as lasts }
  682. unitst^.next:=symtablestack;
  683. symtablestack:=unitst;
  684. {$ifdef DEBUG}
  685. test_symtablestack;
  686. {$endif DEBUG}
  687. constsymtable:=symtablestack;
  688. {$ifdef Splitheap}
  689. if testsplit then
  690. begin
  691. Split_Heap;
  692. allow_special:=true;
  693. Switch_to_temp_heap;
  694. end;
  695. { it will report all crossings }
  696. allow_special:=false;
  697. {$endif Splitheap}
  698. { set some informations }
  699. procinfo.retdef:=voiddef;
  700. procinfo._class:=nil;
  701. procinfo.call_offset:=8;
  702. { for temporary values }
  703. procinfo.framepointer:=frame_pointer;
  704. { clear flags }
  705. procinfo.flags:=0;
  706. { Create a new procedure }
  707. codegen_newprocedure;
  708. { Compile the unit }
  709. names.init;
  710. names.insert(current_module^.modulename^+'_init');
  711. names.insert('INIT$$'+current_module^.modulename^);
  712. compile_proc_body(names,true,false);
  713. names.done;
  714. { Shutdown the codegen for this procedure }
  715. codegen_doneprocedure;
  716. consume(POINT);
  717. { size of the static data }
  718. datasize:=symtablestack^.datasize;
  719. { unsed static symbols ? }
  720. symtablestack^.allsymbolsused;
  721. {$ifdef GDB}
  722. { add all used definitions even for implementation}
  723. if (cs_debuginfo in aktswitches) then
  724. begin
  725. { all types }
  726. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  727. { and all local symbols}
  728. symtablestack^.concatstabto(debuglist);
  729. end;
  730. {$endif GDB}
  731. current_module^.in_implementation:=false;
  732. { deletes all symtables generated in the implementation part }
  733. while symtablestack^.symtabletype<>globalsymtable do
  734. dellexlevel;
  735. { tests, if all forwards are resolved }
  736. symtablestack^.check_forwards;
  737. symtablestack^.symtabletype:=unitsymtable;
  738. punitsymtable(symtablestack)^.is_stab_written:=false;
  739. {Write out the unit if the compile was succesfull.}
  740. if status.errorcount=0 then
  741. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  742. pu:=pused_unit(usedunits.first);
  743. while assigned(pu) do
  744. begin
  745. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  746. pu:=pused_unit(pu^.next);
  747. end;
  748. inc(datasize,symtablestack^.datasize);
  749. { generate imports }
  750. if current_module^.uses_imports then
  751. importlib^.generatelib;
  752. { finish asmlist by adding segment starts }
  753. insertsegment;
  754. end;
  755. procedure proc_program(islibrary : boolean);
  756. var
  757. st : psymtable;
  758. names : Tstringcontainer;
  759. begin
  760. { Trying to compile the system unit... }
  761. { if no unit defined... then issue a }
  762. { fatal error (avoids pointer problems)}
  763. { when referencing the non-existant }
  764. { system unit. }
  765. { System Unit should be compiled using proc_unit !! (PFV) }
  766. { if (cs_compilesystem in aktswitches) then
  767. Begin
  768. if token<>_UNIT then
  769. Message1(scan_f_syn_expected,'UNIT');
  770. consume(_UNIT);
  771. end;}
  772. parse_only:=false;
  773. if islibrary then
  774. begin
  775. consume(_LIBRARY);
  776. stringdispose(current_module^.modulename);
  777. current_module^.modulename:=stringdup(pattern);
  778. consume(ID);
  779. consume(SEMICOLON);
  780. end
  781. else
  782. { is there an program head ? }
  783. if token=_PROGRAM then
  784. begin
  785. consume(_PROGRAM);
  786. stringdispose(current_module^.modulename);
  787. current_module^.modulename:=stringdup(pattern);
  788. consume(ID);
  789. if token=LKLAMMER then
  790. begin
  791. consume(LKLAMMER);
  792. idlist;
  793. consume(RKLAMMER);
  794. end;
  795. consume(SEMICOLON);
  796. end;
  797. { insert after the unit symbol tables the static symbol table }
  798. { of the program }
  799. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  800. {Generate a procsym.}
  801. aktprocsym:=new(Pprocsym,init('main'));
  802. aktprocsym^.definition:=new(Pprocdef,init);
  803. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  804. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  805. {The localst is a local symtable. Change it into the static
  806. symtable.}
  807. dispose(aktprocsym^.definition^.localst,done);
  808. aktprocsym^.definition^.localst:=st;
  809. refsymtable:=st;
  810. { necessary for browser }
  811. loaded_units.insert(current_module);
  812. {Insert the symbols of the system unit into the stack of symbol
  813. tables.}
  814. symtablestack:=systemunit;
  815. systemunit^.next:=nil;
  816. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  817. {Load the units used by the program we compile.}
  818. if token=_USES then
  819. loadunits;
  820. {Insert the name of the main program into the symbol table.}
  821. if current_module^.modulename^<>'' then
  822. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  823. { ...is also constsymtable, this is the symtable where }
  824. { the elements of enumeration types are inserted }
  825. constsymtable:=st;
  826. { set some informations about the main program }
  827. with procinfo do
  828. begin
  829. retdef:=voiddef;
  830. _class:=nil;
  831. call_offset:=8;
  832. framepointer:=frame_pointer;
  833. flags:=0;
  834. end;
  835. procprefix:='';
  836. in_except_block:=false;
  837. codegen_newprocedure;
  838. {The program intialization needs an alias, so it can be called
  839. from the bootstrap code.}
  840. names.init;
  841. names.insert('program_init');
  842. names.insert('PASCALMAIN');
  843. names.insert(target_os.cprefix+'main');
  844. compile_proc_body(names,true,false);
  845. names.done;
  846. codegen_doneprocedure;
  847. consume(POINT);
  848. if (cs_smartlink in aktswitches) then
  849. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  850. else
  851. current_module^.linkofiles.insert(current_module^.objfilename^);
  852. { insert heap }
  853. insertheap;
  854. { generate imports }
  855. if current_module^.uses_imports then
  856. importlib^.generatelib;
  857. inserttargetspecific;
  858. datasize:=symtablestack^.datasize;
  859. { finish asmlist by adding segment starts }
  860. insertsegment;
  861. end;
  862. end.
  863. {
  864. $Log$
  865. Revision 1.27 1998-06-11 13:58:08 peter
  866. * small fix to let newppu compile
  867. Revision 1.26 1998/06/09 16:01:47 pierre
  868. + added procedure directive parsing for procvars
  869. (accepted are popstack cdecl and pascal)
  870. + added C vars with the following syntax
  871. var C calias 'true_c_name';(can be followed by external)
  872. reason is that you must add the Cprefix
  873. which is target dependent
  874. Revision 1.25 1998/06/08 22:59:49 peter
  875. * smartlinking works for win32
  876. * some defines to exclude some compiler parts
  877. Revision 1.24 1998/06/08 13:13:44 pierre
  878. + temporary variables now in temp_gen.pas unit
  879. because it is processor independent
  880. * mppc68k.bat modified to undefine i386 and support_mmx
  881. (which are defaults for i386)
  882. Revision 1.23 1998/06/05 17:47:29 peter
  883. * some better uses clauses
  884. Revision 1.22 1998/06/05 14:37:34 pierre
  885. * fixes for inline for operators
  886. * inline procedure more correctly restricted
  887. Revision 1.21 1998/06/04 23:51:53 peter
  888. * m68k compiles
  889. + .def file creation moved to gendef.pas so it could also be used
  890. for win32
  891. Revision 1.20 1998/06/04 09:55:42 pierre
  892. * demangled name of procsym reworked to become independant of the mangling scheme
  893. Revision 1.19 1998/06/03 23:40:38 peter
  894. + unlimited file support, release tempclose
  895. Revision 1.18 1998/06/03 22:49:00 peter
  896. + wordbool,longbool
  897. * rename bis,von -> high,low
  898. * moved some systemunit loading/creating to psystem.pas
  899. Revision 1.17 1998/05/28 14:40:25 peter
  900. * fixes for newppu, remake3 works now with it
  901. Revision 1.16 1998/05/27 19:45:06 peter
  902. * symtable.pas splitted into includefiles
  903. * symtable adapted for $ifdef NEWPPU
  904. Revision 1.15 1998/05/23 01:21:22 peter
  905. + aktasmmode, aktoptprocessor, aktoutputformat
  906. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  907. + $LIBNAME to set the library name where the unit will be put in
  908. * splitted cgi386 a bit (codeseg to large for bp7)
  909. * nasm, tasm works again. nasm moved to ag386nsm.pas
  910. Revision 1.14 1998/05/20 09:42:35 pierre
  911. + UseTokenInfo now default
  912. * unit in interface uses and implementation uses gives error now
  913. * only one error for unknown symbol (uses lastsymknown boolean)
  914. the problem came from the label code !
  915. + first inlined procedures and function work
  916. (warning there might be allowed cases were the result is still wrong !!)
  917. * UseBrower updated gives a global list of all position of all used symbols
  918. with switch -gb
  919. Revision 1.13 1998/05/12 10:47:00 peter
  920. * moved printstatus to verb_def
  921. + V_Normal which is between V_Error and V_Warning and doesn't have a
  922. prefix like error: warning: and is included in V_Default
  923. * fixed some messages
  924. * first time parameter scan is only for -v and -T
  925. - removed old style messages
  926. Revision 1.12 1998/05/11 13:07:56 peter
  927. + $ifdef NEWPPU for the new ppuformat
  928. + $define GDB not longer required
  929. * removed all warnings and stripped some log comments
  930. * no findfirst/findnext anymore to remove smartlink *.o files
  931. Revision 1.11 1998/05/06 18:36:53 peter
  932. * tai_section extended with code,data,bss sections and enumerated type
  933. * ident 'compiled by FPC' moved to pmodules
  934. * small fix for smartlink
  935. Revision 1.10 1998/05/04 17:54:28 peter
  936. + smartlinking works (only case jumptable left todo)
  937. * redesign of systems.pas to support assemblers and linkers
  938. + Unitname is now also in the PPU-file, increased version to 14
  939. Revision 1.9 1998/05/01 16:38:45 florian
  940. * handling of private and protected fixed
  941. + change_keywords_to_tp implemented to remove
  942. keywords which aren't supported by tp
  943. * break and continue are now symbols of the system unit
  944. + widestring, longstring and ansistring type released
  945. Revision 1.8 1998/04/30 15:59:41 pierre
  946. * GDB works again better :
  947. correct type info in one pass
  948. + UseTokenInfo for better source position
  949. * fixed one remaining bug in scanner for line counts
  950. * several little fixes
  951. Revision 1.7 1998/04/29 10:33:59 pierre
  952. + added some code for ansistring (not complete nor working yet)
  953. * corrected operator overloading
  954. * corrected nasm output
  955. + started inline procedures
  956. + added starstarn : use ** for exponentiation (^ gave problems)
  957. + started UseTokenInfo cond to get accurate positions
  958. Revision 1.6 1998/04/27 23:10:28 peter
  959. + new scanner
  960. * $makelib -> if smartlink
  961. * small filename fixes pmodule.setfilename
  962. * moved import from files.pas -> import.pas
  963. Revision 1.5 1998/04/14 23:27:03 florian
  964. + exclude/include with constant second parameter added
  965. Revision 1.4 1998/04/10 14:41:43 peter
  966. * removed some Hints
  967. * small speed optimization for AsmLn
  968. Revision 1.3 1998/04/03 09:51:00 daniel
  969. * Fixed heap allocation for OS/2.
  970. }