pmodules.pas 39 KB

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