pmodules.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. {$ifdef TEST_IMPL}
  297. if (loaded_unit^.crc<>0) and (loaded_unit^.crc<>checksum) 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^.modulename^)
  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. {$endif TEST_IMPL}
  318. { read until ibend }
  319. hp^.ppufile^.read_data(b,1,count);
  320. end;
  321. hp^.ppufile^.close;
  322. {$endif}
  323. dispose(hp^.map);
  324. hp^.map:=nil;
  325. end;
  326. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  327. var
  328. st : punitsymtable;
  329. old_current_module,hp,nextmodule : pmodule;
  330. pu : pused_unit;
  331. hs : pstring;
  332. begin
  333. old_current_module:=current_module;
  334. { be sure not to mix lines from different files }
  335. { update_line; }
  336. { unit not found }
  337. st:=nil;
  338. { search all loaded units }
  339. hp:=pmodule(loaded_units.first);
  340. while assigned(hp) do
  341. begin
  342. if hp^.modulename^=s then
  343. begin
  344. { the unit is already registered }
  345. { and this means that the unit }
  346. { is already compiled }
  347. { else there is a cyclic unit use }
  348. if assigned(hp^.symtable) then
  349. st:=punitsymtable(hp^.symtable)
  350. else
  351. begin
  352. { recompile the unit ? }
  353. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  354. Message(unit_f_circular_unit_reference);
  355. end;
  356. break;
  357. end;
  358. { the next unit }
  359. hp:=pmodule(hp^.next);
  360. end;
  361. { no error and the unit isn't loaded }
  362. if not(assigned(hp)) and (st=nil) then
  363. begin
  364. { generates a new unit info record }
  365. hp:=new(pmodule,init(s,true));
  366. { now we can register the unit }
  367. loaded_units.insert(hp);
  368. current_module:=hp;
  369. { force build ? }
  370. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  371. begin
  372. { we needn't the ppufile }
  373. if assigned(hp^.ppufile) then
  374. begin
  375. dispose(hp^.ppufile,done);
  376. hp^.ppufile:=nil;
  377. end;
  378. if not(hp^.sources_avail) then
  379. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  380. else
  381. begin
  382. if assigned(old_current_module^.current_inputfile) then
  383. old_current_module^.current_inputfile^.tempclose;
  384. compile(hp^.mainsource^,compile_system);
  385. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  386. old_current_module^.current_inputfile^.tempreopen;
  387. end;
  388. end
  389. else
  390. begin
  391. { only reassemble ? }
  392. if (hp^.do_assemble) then
  393. OnlyAsm(hp^.asmfilename^);
  394. { we should know there the PPU file else it's an error and
  395. we can't load the unit }
  396. {$ifdef NEWPPU}
  397. { if hp^.ppufile^.name^<>'' then}
  398. {$else}
  399. if hp^.ppufile^.name^<>'' then
  400. {$endif}
  401. load_ppu(old_current_module,hp,compile_system);
  402. { add the files for the linker }
  403. addlinkerfiles(hp);
  404. end;
  405. { register the unit _once_ }
  406. usedunits.concat(new(pused_unit,init(hp,0)));
  407. { the unit is written, so we can set the symtable type }
  408. { to unitsymtable, else we get some dupid errors }
  409. { this is not the right place because of the }
  410. { ready label }
  411. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  412. { placed at this end of proc_unit }
  413. psymtable(hp^.symtable)^.unitid:=0;
  414. { reset the unitnumbers for the other units }
  415. pu:=pused_unit(old_current_module^.used_units.first);
  416. while assigned(pu) do
  417. begin
  418. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  419. pu:=pused_unit(pu^.next);
  420. end;
  421. end
  422. else
  423. if assigned(hp) and (st=nil) then
  424. begin
  425. { we have to compile the unit again, but it is already inserted !!}
  426. { we may have problem with the lost symtable !! }
  427. current_module:=hp;
  428. { we must preserve the unit chain }
  429. nextmodule:=pmodule(hp^.next);
  430. { we have to cleanup a little }
  431. hp^.special_done;
  432. new(hs);
  433. hs^:=hp^.mainsource^;
  434. hp^.init(hs^,true);
  435. dispose(hs);
  436. { we must preserve the unit chain }
  437. hp^.next:=nextmodule;
  438. if assigned(hp^.ppufile) then
  439. load_ppu(old_current_module,hp,compile_system)
  440. else
  441. begin
  442. {$ifdef UseBrowser}
  443. { here we need to remove the names ! }
  444. hp^.sourcefiles.done;
  445. hp^.sourcefiles.init;
  446. {$endif not UseBrowser}
  447. if assigned(old_current_module^.current_inputfile) then
  448. old_current_module^.current_inputfile^.tempclose;
  449. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  450. compile(hp^.mainsource^,compile_system);
  451. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  452. old_current_module^.current_inputfile^.tempreopen;
  453. end;
  454. current_module^.compiled:=true;
  455. end;
  456. { set the old module }
  457. current_module:=old_current_module;
  458. { the current module uses the unit hp }
  459. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  460. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  461. if in_uses and not current_module^.in_implementation then
  462. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  463. loadunit:=hp;
  464. end;
  465. procedure loadunits;
  466. var
  467. s : stringid;
  468. hp : pused_unit;
  469. hp2 : pmodule;
  470. hp3 : psymtable;
  471. oldprocsym:Pprocsym;
  472. begin
  473. oldprocsym:=aktprocsym;
  474. consume(_USES);
  475. {$ifdef DEBUG}
  476. test_symtablestack;
  477. {$endif DEBUG}
  478. repeat
  479. s:=pattern;
  480. consume(ID);
  481. hp2:=loadunit(s,false,true);
  482. if current_module^.compiled then
  483. exit;
  484. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  485. if token=COMMA then
  486. begin
  487. pattern:='';
  488. consume(COMMA);
  489. end
  490. else
  491. break;
  492. until false;
  493. consume(SEMICOLON);
  494. { now insert the units in the symtablestack }
  495. hp:=pused_unit(current_module^.used_units.first);
  496. { set the symtable to systemunit so it gets reorderd correctly }
  497. symtablestack:=systemunit;
  498. while assigned(hp) do
  499. begin
  500. {$IfDef GDB}
  501. if (cs_debuginfo in aktswitches) and
  502. not hp^.is_stab_written then
  503. begin
  504. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  505. hp^.is_stab_written:=true;
  506. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  507. end;
  508. {$EndIf GDB}
  509. if hp^.in_uses then
  510. begin
  511. hp3:=symtablestack;
  512. while assigned(hp3) do
  513. begin
  514. { insert units only once ! }
  515. if hp^.u^.symtable=hp3 then
  516. break;
  517. hp3:=hp3^.next;
  518. { unit isn't inserted }
  519. if hp3=nil then
  520. begin
  521. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  522. symtablestack:=psymtable(hp^.u^.symtable);
  523. {$ifdef CHAINPROCSYMS}
  524. symtablestack^.chainprocsyms;
  525. {$endif CHAINPROCSYMS}
  526. {$ifdef DEBUG}
  527. test_symtablestack;
  528. {$endif DEBUG}
  529. end;
  530. end;
  531. end;
  532. hp:=pused_unit(hp^.next);
  533. end;
  534. aktprocsym:=oldprocsym;
  535. end;
  536. procedure parse_implementation_uses(symt:Psymtable);
  537. var
  538. old_module_in_implementation : boolean;
  539. begin
  540. if token=_USES then
  541. begin
  542. old_module_in_implementation:=module_in_implementation;
  543. module_in_implementation:=true;
  544. current_module^.in_implementation:=true;
  545. symt^.symtabletype:=unitsymtable;
  546. loadunits;
  547. symt^.symtabletype:=globalsymtable;
  548. {$ifdef DEBUG}
  549. test_symtablestack;
  550. {$endif DEBUG}
  551. module_in_implementation:=old_module_in_implementation;
  552. end;
  553. end;
  554. procedure proc_unit;
  555. var
  556. { unitname : stringid; }
  557. names:Tstringcontainer;
  558. p : psymtable;
  559. unitst : punitsymtable;
  560. pu : pused_unit;
  561. s1,s2 : ^string; {Saves stack space}
  562. begin
  563. consume(_UNIT);
  564. if token=ID then
  565. begin
  566. { create filenames and unit name }
  567. current_module^.SetFileName(current_module^.current_inputfile^.path^,current_module^.current_inputfile^.name^);
  568. stringdispose(current_module^.modulename);
  569. current_module^.modulename:=stringdup(upper(pattern));
  570. { check for system unit }
  571. new(s1);
  572. new(s2);
  573. s1^:=upper(target_info.system_unit);
  574. s2^:=upper(current_module^.current_inputfile^.name^);
  575. if (cs_compilesystem in aktswitches) then
  576. begin
  577. if (cs_check_unit_name in aktswitches) and
  578. ((length(current_module^.modulename^)>8) or
  579. (current_module^.modulename^<>s1^) or
  580. (current_module^.modulename^<>s2^)) then
  581. Message1(unit_e_illegal_unit_name,s1^);
  582. end
  583. else
  584. if (current_module^.modulename^=s1^) then
  585. Message(unit_w_switch_us_missed);
  586. dispose(s2);
  587. dispose(s1);
  588. { Add Object File }
  589. if (cs_smartlink in aktswitches) then
  590. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  591. else
  592. current_module^.linkofiles.insert(current_module^.objfilename^);
  593. end;
  594. consume(ID);
  595. consume(SEMICOLON);
  596. consume(_INTERFACE);
  597. { this should be placed after uses !!}
  598. {$ifndef UseNiceNames}
  599. procprefix:='_'+current_module^.modulename^+'$$';
  600. {$else UseNiceNames}
  601. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  602. {$endif UseNiceNames}
  603. parse_only:=true;
  604. { generate now the global symboltable }
  605. p:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  606. refsymtable:=p;
  607. unitst:=punitsymtable(p);
  608. { the unit name must be usable as a unit specifier }
  609. { inside the unit itself (PM) }
  610. { this also forbids to have another symbol }
  611. { with the same name as the unit }
  612. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  613. { set the symbol table for the current unit }
  614. { this must be set later for interdependency }
  615. { current_module^.symtable:=psymtable(p); }
  616. { a unit compiled at command line must be inside the loaded_unit list }
  617. if (compile_level=1) then
  618. begin
  619. loaded_units.insert(current_module);
  620. if cs_unit_to_lib in initswitches then
  621. begin
  622. current_module^.flags:=current_module^.flags or uf_in_library;
  623. if cs_shared_lib in initswitches then
  624. current_module^.flags:=current_module^.flags or uf_shared_library;
  625. end;
  626. end;
  627. { insert qualifier for the system unit (allows system.writeln) }
  628. if not(cs_compilesystem in aktswitches) then
  629. begin
  630. { insert the system unit }
  631. { it is allways the first }
  632. systemunit^.next:=nil;
  633. symtablestack:=systemunit;
  634. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  635. if token=_USES then
  636. begin
  637. unitst^.symtabletype:=unitsymtable;
  638. loadunits;
  639. { has it been compiled at a higher level ?}
  640. if current_module^.compiled then
  641. exit;
  642. unitst^.symtabletype:=globalsymtable;
  643. end;
  644. { ... but insert the symbol table later }
  645. p^.next:=symtablestack;
  646. symtablestack:=p;
  647. end
  648. else
  649. { while compiling a system unit, some types are directly inserted }
  650. begin
  651. p^.next:=symtablestack;
  652. symtablestack:=p;
  653. insert_intern_types(p);
  654. end;
  655. { displaced for inter-dependency considerations }
  656. current_module^.symtable:=psymtable(p);
  657. constsymtable:=symtablestack;
  658. { ... parse the declarations }
  659. read_interface_declarations;
  660. consume(_IMPLEMENTATION);
  661. parse_only:=false;
  662. refsymtable^.number_defs;
  663. {$ifdef GDB}
  664. { add all used definitions even for implementation}
  665. if (cs_debuginfo in aktswitches) then
  666. begin
  667. { all types }
  668. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  669. { and all local symbols}
  670. refsymtable^.concatstabto(debuglist);
  671. end;
  672. {$endif GDB}
  673. { for interdependent units
  674. the crc is included in the ppufile
  675. but it is not known when writing the first ppufile
  676. so I tried to add a fake writing of the ppu
  677. just to get the CRC
  678. but the result is different for the real CRC
  679. it calculates after, I don't know why
  680. Answer:
  681. -------
  682. When reading the interface part, the compiler assumes
  683. that all registers are modified by a procedure
  684. usedinproc:=$ff !
  685. If the definition is read, the compiler determines
  686. the used registers and write the correct value
  687. to usedinproc
  688. only_calculate_crc:=true;
  689. writeunitas(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^+
  690. +'.PPS',punitsymtable(symtablestack));
  691. only_calculate_crc:=false;
  692. }
  693. { generates static symbol table }
  694. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  695. { must be done only after _USES !! (PM)
  696. refsymtable:=p;}
  697. {Generate a procsym.}
  698. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  699. aktprocsym^.definition:=new(Pprocdef,init);
  700. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  701. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  702. {The generated procsym has a local symtable. Discard it and turn
  703. it into the static one.}
  704. dispose(aktprocsym^.definition^.localst,done);
  705. aktprocsym^.definition^.localst:=p;
  706. { testing !!!!!!!!! }
  707. { we set the interface part as a unitsymtable }
  708. { for the case we need to compile another unit }
  709. { remove the globalsymtable from the symtable stack }
  710. { to reinsert it after loading the implementation units }
  711. symtablestack:=unitst^.next;
  712. parse_implementation_uses(unitst);
  713. { now we can change refsymtable }
  714. refsymtable:=p;
  715. { but reinsert the global symtable as lasts }
  716. unitst^.next:=symtablestack;
  717. symtablestack:=unitst;
  718. {$ifdef DEBUG}
  719. test_symtablestack;
  720. {$endif DEBUG}
  721. constsymtable:=symtablestack;
  722. {$ifdef Splitheap}
  723. if testsplit then
  724. begin
  725. Split_Heap;
  726. allow_special:=true;
  727. Switch_to_temp_heap;
  728. end;
  729. { it will report all crossings }
  730. allow_special:=false;
  731. {$endif Splitheap}
  732. { set some informations }
  733. procinfo.retdef:=voiddef;
  734. procinfo._class:=nil;
  735. procinfo.call_offset:=8;
  736. { for temporary values }
  737. procinfo.framepointer:=frame_pointer;
  738. { clear flags }
  739. procinfo.flags:=0;
  740. {Reset the codegenerator.}
  741. codegen_newprocedure;
  742. names.init;
  743. names.insert(current_module^.modulename^+'_init');
  744. names.insert('INIT$$'+current_module^.modulename^);
  745. compile_proc_body(names,true,false);
  746. names.done;
  747. codegen_doneprocedure;
  748. consume(POINT);
  749. { size of the static data }
  750. datasize:=symtablestack^.datasize;
  751. { unsed static symbols ? }
  752. symtablestack^.allsymbolsused;
  753. {$ifdef GDB}
  754. { add all used definitions even for implementation}
  755. if (cs_debuginfo in aktswitches) then
  756. begin
  757. { all types }
  758. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  759. { and all local symbols}
  760. symtablestack^.concatstabto(debuglist);
  761. end;
  762. {$endif GDB}
  763. current_module^.in_implementation:=false;
  764. { deletes all symtables generated in the implementation part }
  765. while symtablestack^.symtabletype<>globalsymtable do
  766. dellexlevel;
  767. { tests, if all forwards are resolved }
  768. symtablestack^.check_forwards;
  769. symtablestack^.symtabletype:=unitsymtable;
  770. punitsymtable(symtablestack)^.is_stab_written:=false;
  771. {Write out the unit if the compile was succesfull.}
  772. if status.errorcount=0 then
  773. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  774. pu:=pused_unit(usedunits.first);
  775. while assigned(pu) do
  776. begin
  777. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  778. pu:=pused_unit(pu^.next);
  779. end;
  780. inc(datasize,symtablestack^.datasize);
  781. { finish asmlist by adding segment starts }
  782. insertsegment;
  783. end;
  784. procedure proc_program(islibrary : boolean);
  785. var
  786. st : psymtable;
  787. names : Tstringcontainer;
  788. begin
  789. { Trying to compile the system unit... }
  790. { if no unit defined... then issue a }
  791. { fatal error (avoids pointer problems)}
  792. { when referencing the non-existant }
  793. { system unit. }
  794. { System Unit should be compiled using proc_unit !! (PFV) }
  795. { if (cs_compilesystem in aktswitches) then
  796. Begin
  797. if token<>_UNIT then
  798. Message1(scan_f_syn_expected,'UNIT');
  799. consume(_UNIT);
  800. end;}
  801. parse_only:=false;
  802. if islibrary then
  803. begin
  804. consume(_LIBRARY);
  805. stringdispose(current_module^.modulename);
  806. current_module^.modulename:=stringdup(pattern);
  807. consume(ID);
  808. consume(SEMICOLON);
  809. end
  810. else
  811. { is there an program head ? }
  812. if token=_PROGRAM then
  813. begin
  814. consume(_PROGRAM);
  815. stringdispose(current_module^.modulename);
  816. current_module^.modulename:=stringdup(pattern);
  817. consume(ID);
  818. if token=LKLAMMER then
  819. begin
  820. consume(LKLAMMER);
  821. idlist;
  822. consume(RKLAMMER);
  823. end;
  824. consume(SEMICOLON);
  825. end;
  826. { insert after the unit symbol tables the static symbol table }
  827. { of the program }
  828. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  829. {Generate a procsym.}
  830. aktprocsym:=new(Pprocsym,init('main'));
  831. aktprocsym^.definition:=new(Pprocdef,init);
  832. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  833. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  834. {The localst is a local symtable. Change it into the static
  835. symtable.}
  836. dispose(aktprocsym^.definition^.localst,done);
  837. aktprocsym^.definition^.localst:=st;
  838. refsymtable:=st;
  839. { necessary for browser }
  840. loaded_units.insert(current_module);
  841. {Insert the symbols of the system unit into the stack of symbol
  842. tables.}
  843. symtablestack:=systemunit;
  844. systemunit^.next:=nil;
  845. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  846. {Load the units used by the program we compile.}
  847. if token=_USES then
  848. loadunits;
  849. {Insert the name of the main program into the symbol table.}
  850. if current_module^.modulename^<>'' then
  851. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  852. { ...is also constsymtable, this is the symtable where }
  853. { the elements of enumeration types are inserted }
  854. constsymtable:=st;
  855. { set some informations about the main program }
  856. with procinfo do
  857. begin
  858. retdef:=voiddef;
  859. _class:=nil;
  860. call_offset:=8;
  861. framepointer:=frame_pointer;
  862. flags:=0;
  863. end;
  864. procprefix:='';
  865. in_except_block:=false;
  866. codegen_newprocedure;
  867. {The program intialization needs an alias, so it can be called
  868. from the bootstrap code.}
  869. names.init;
  870. names.insert('program_init');
  871. names.insert('PASCALMAIN');
  872. names.insert(target_os.cprefix+'main');
  873. compile_proc_body(names,true,false);
  874. names.done;
  875. codegen_doneprocedure;
  876. consume(POINT);
  877. if (cs_smartlink in aktswitches) then
  878. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  879. else
  880. current_module^.linkofiles.insert(current_module^.objfilename^);
  881. insertheap;
  882. inserttargetspecific;
  883. datasize:=symtablestack^.datasize;
  884. { finish asmlist by adding segment starts }
  885. insertsegment;
  886. end;
  887. end.
  888. {
  889. $Log$
  890. Revision 1.20 1998-06-04 09:55:42 pierre
  891. * demangled name of procsym reworked to become independant of the mangling scheme
  892. Come test_funcret improvements (not yet working)S: ----------------------------------------------------------------------
  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. }