pmodules.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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. procedure loadsystemunit;
  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. {$ifndef OLDPPU}
  33. ,ppu
  34. {$endif OLDPPU}
  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. {$ifndef OLDPPU}
  124. function loadunit(const s : string;compile_system:boolean) : pmodule;forward;
  125. procedure load_usedunits(compile_system:boolean);
  126. var
  127. pu : pused_unit;
  128. loaded_unit : pmodule;
  129. nextmapentry : longint;
  130. begin
  131. { init the map }
  132. new(current_module^.map);
  133. nextmapentry:=1;
  134. { load the used units from interface }
  135. pu:=pused_unit(current_module^.used_units.first);
  136. while assigned(pu) do
  137. begin
  138. if (not pu^.loaded) and (pu^.in_interface) then
  139. begin
  140. loaded_unit:=loadunit(pu^.name^,false);
  141. if current_module^.compiled then
  142. exit;
  143. { register unit in used units }
  144. pu^.u:=loaded_unit;
  145. pu^.loaded:=true;
  146. { need to recompile the current unit ? }
  147. if loaded_unit^.crc<>pu^.checksum then
  148. begin
  149. current_module^.do_compile:=true;
  150. exit;
  151. end;
  152. { setup the map entry for deref }
  153. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  154. inc(nextmapentry);
  155. if nextmapentry>maxunits then
  156. Message(unit_f_too_much_units);
  157. end;
  158. pu:=pused_unit(pu^.next);
  159. end;
  160. { ok, now load the unit }
  161. current_module^.symtable:=new(punitsymtable,loadasunit);
  162. { if this is the system unit insert the intern symbols }
  163. if compile_system then
  164. begin
  165. make_ref:=false;
  166. insertinternsyms(psymtable(current_module^.symtable));
  167. make_ref:=true;
  168. end;
  169. { now only read the implementation part }
  170. current_module^.in_implementation:=true;
  171. { load the used units from implementation }
  172. pu:=pused_unit(current_module^.used_units.first);
  173. while assigned(pu) do
  174. begin
  175. if (not pu^.loaded) and (not pu^.in_interface) then
  176. begin
  177. loaded_unit:=loadunit(pu^.name^,false);
  178. if current_module^.compiled then
  179. exit;
  180. { register unit in used units }
  181. pu^.u:=loaded_unit;
  182. pu^.loaded:=true;
  183. {$ifdef TEST_IMPL}
  184. { need to recompile the current unit ? }
  185. if loaded_unit^.crc<>pu^.checksum then
  186. begin
  187. current_module^.do_compile:=true;
  188. exit;
  189. end;
  190. { setup the map entry for deref }
  191. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  192. inc(nextmapentry);
  193. if nextmapentry>maxunits then
  194. Message(unit_f_too_much_units);
  195. {$endif TEST_IMPL}
  196. end;
  197. pu:=pused_unit(pu^.next);
  198. end;
  199. { remove the map, it's not needed anymore }
  200. dispose(current_module^.map);
  201. current_module^.map:=nil;
  202. end;
  203. function loadunit(const s : string;compile_system:boolean) : pmodule;
  204. var
  205. st : punitsymtable;
  206. old_current_ppu : pppufile;
  207. old_current_module,hp : pmodule;
  208. procedure loadppufile;
  209. begin
  210. { load interface section }
  211. if not current_module^.do_compile then
  212. load_interface;
  213. { only load units when we don't recompile }
  214. if not current_module^.do_compile then
  215. load_usedunits(compile_system);
  216. { recompile if set }
  217. if current_module^.do_compile then
  218. begin
  219. { we needn't the ppufile }
  220. if assigned(current_module^.ppufile) then
  221. begin
  222. dispose(current_module^.ppufile,done);
  223. current_module^.ppufile:=nil;
  224. end;
  225. { recompile the unit or give a fatal error if sources not available }
  226. if not(current_module^.sources_avail) then
  227. Message1(unit_f_cant_compile_unit,current_module^.modulename^)
  228. else
  229. begin
  230. if assigned(old_current_module^.current_inputfile) then
  231. old_current_module^.current_inputfile^.tempclose;
  232. compile(current_module^.mainsource^,compile_system);
  233. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  234. old_current_module^.current_inputfile^.tempreopen;
  235. end;
  236. end
  237. else
  238. begin
  239. { only reassemble ? }
  240. if (current_module^.do_assemble) then
  241. OnlyAsm(current_module^.asmfilename^);
  242. { add the files for the linker }
  243. addlinkerfiles(current_module);
  244. end;
  245. if assigned(current_module^.ppufile) then
  246. begin
  247. dispose(current_module^.ppufile,done);
  248. current_module^.ppufile:=nil;
  249. end;
  250. end;
  251. begin
  252. old_current_module:=current_module;
  253. old_current_ppu:=current_ppu;
  254. { be sure not to mix lines from different files }
  255. { update_line; }
  256. { unit not found }
  257. st:=nil;
  258. { search all loaded units }
  259. hp:=pmodule(loaded_units.first);
  260. while assigned(hp) do
  261. begin
  262. if hp^.modulename^=s then
  263. begin
  264. { the unit is already registered }
  265. { and this means that the unit }
  266. { is already compiled }
  267. { else there is a cyclic unit use }
  268. if assigned(hp^.symtable) then
  269. st:=punitsymtable(hp^.symtable)
  270. else
  271. begin
  272. { recompile the unit ? }
  273. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  274. Message(unit_f_circular_unit_reference);
  275. end;
  276. break;
  277. end;
  278. { the next unit }
  279. hp:=pmodule(hp^.next);
  280. end;
  281. { the unit is not in the symtable stack }
  282. if (not assigned(st)) then
  283. begin
  284. { if the unit is loaded remove it first }
  285. if assigned(hp) then
  286. begin
  287. { remove the old unit }
  288. loaded_units.remove(hp);
  289. dispose(hp,done);
  290. end;
  291. { generates a new unit info record }
  292. current_module:=new(pmodule,init(s,true));
  293. current_ppu:=current_module^.ppufile;
  294. { now we can register the unit }
  295. loaded_units.insert(current_module);
  296. { now realy load the ppu }
  297. loadppufile;
  298. { set compiled flag }
  299. current_module^.compiled:=true;
  300. { register the unit _once_ }
  301. usedunits.concat(new(pused_unit,init(current_module,true)));
  302. { load return pointer }
  303. hp:=current_module;
  304. end;
  305. { set the old module }
  306. current_ppu:=old_current_ppu;
  307. current_module:=old_current_module;
  308. loadunit:=hp;
  309. end;
  310. {$else OLDPPU}
  311. {*****************************************************************************
  312. Old PPU
  313. *****************************************************************************}
  314. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;forward;
  315. procedure load_ppu(oldhp,hp : pmodule;compile_system : boolean);
  316. var
  317. loaded_unit : pmodule;
  318. b : byte;
  319. checksum,
  320. count,
  321. nextmapentry : longint;
  322. hs : string;
  323. begin
  324. { init the map }
  325. new(hp^.map);
  326. nextmapentry:=1;
  327. { load the used units from interface }
  328. hp^.ppufile^.read_data(b,1,count);
  329. while (b=ibloadunit) do
  330. begin
  331. { read unit name }
  332. hp^.ppufile^.read_data(hs[0],1,count);
  333. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  334. hp^.ppufile^.read_data(checksum,4,count);
  335. loaded_unit:=loadunit(hs,false,false);
  336. if hp^.compiled then
  337. exit;
  338. { if the crc of a used unit is the same as }
  339. { written to the PPU file, we needn't to }
  340. { recompile the current unit }
  341. if (loaded_unit^.crc<>checksum) then
  342. begin
  343. { we have to compile the current unit }
  344. { remove stuff which isn't needed }
  345. { forget the map }
  346. dispose(hp^.map);
  347. hp^.map:=nil;
  348. hp^.ppufile^.close;
  349. dispose(hp^.ppufile,done);
  350. hp^.ppufile:=nil;
  351. if not(hp^.sources_avail) then
  352. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  353. else
  354. begin
  355. if assigned(oldhp^.current_inputfile) then
  356. oldhp^.current_inputfile^.tempclose;
  357. compile(hp^.mainsource^,compile_system);
  358. if (not oldhp^.compiled) and assigned(oldhp^.current_inputfile) then
  359. oldhp^.current_inputfile^.tempreopen;
  360. end;
  361. exit;
  362. end;
  363. { setup the map entry for deref }
  364. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  365. inc(nextmapentry);
  366. if nextmapentry>maxunits then
  367. Message(unit_f_too_much_units);
  368. { read until ibend }
  369. hp^.ppufile^.read_data(b,1,count);
  370. end;
  371. { ok, now load the unit }
  372. hp^.symtable:=new(punitsymtable,load(hp^.modulename^));
  373. { if this is the system unit insert the intern }
  374. { symbols }
  375. make_ref:=false;
  376. if compile_system then
  377. insertinternsyms(psymtable(hp^.symtable));
  378. make_ref:=true;
  379. { now only read the implementation part }
  380. hp^.in_implementation:=true;
  381. { load the used units from implementation }
  382. hp^.ppufile^.read_data(b,1,count);
  383. while (b<>ibend) and (b=ibloadunit) do
  384. begin
  385. { read unit name }
  386. hp^.ppufile^.read_data(hs[0],1,count);
  387. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  388. hp^.ppufile^.read_data(checksum,4,count);
  389. loaded_unit:=loadunit(hs,false,false);
  390. if hp^.compiled then exit;
  391. { if the crc of a used unit is the same as }
  392. { written to the PPU file, we needn't to }
  393. { recompile the current unit }
  394. { but for the implementation part }
  395. { the written crc is false, because }
  396. { not defined when writing the ppufile !! }
  397. {$ifdef TEST_IMPL}
  398. if (checksum<>0) and (loaded_unit^.crc<>checksum) then
  399. begin
  400. { we have to compile the current unit }
  401. { remove stuff which isn't needed }
  402. { forget the map }
  403. dispose(hp^.map);
  404. hp^.map:=nil;
  405. hp^.ppufile^.close;
  406. dispose(hp^.ppufile,done);
  407. hp^.ppufile:=nil;
  408. if not(hp^.sources_avail) then
  409. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  410. else
  411. begin
  412. oldhp^.current_inputfile^.tempclose;
  413. compile(hp^.mainsource^,compile_system);
  414. oldhp^.current_inputfile^.tempclose;
  415. end;
  416. exit;
  417. end;
  418. {$endif TEST_IMPL}
  419. { read until ibend }
  420. hp^.ppufile^.read_data(b,1,count);
  421. end;
  422. hp^.ppufile^.close;
  423. dispose(hp^.map);
  424. hp^.map:=nil;
  425. end;
  426. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  427. var
  428. st : punitsymtable;
  429. old_current_module,hp,nextmodule : pmodule;
  430. pu : pused_unit;
  431. hs : pstring;
  432. begin
  433. old_current_module:=current_module;
  434. { be sure not to mix lines from different files }
  435. { update_line; }
  436. { unit not found }
  437. st:=nil;
  438. { search all loaded units }
  439. hp:=pmodule(loaded_units.first);
  440. while assigned(hp) do
  441. begin
  442. if hp^.modulename^=s then
  443. begin
  444. { the unit is already registered }
  445. { and this means that the unit }
  446. { is already compiled }
  447. { else there is a cyclic unit use }
  448. if assigned(hp^.symtable) then
  449. st:=punitsymtable(hp^.symtable)
  450. else
  451. begin
  452. { recompile the unit ? }
  453. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  454. Message(unit_f_circular_unit_reference);
  455. end;
  456. break;
  457. end;
  458. { the next unit }
  459. hp:=pmodule(hp^.next);
  460. end;
  461. { no error and the unit isn't loaded }
  462. if not(assigned(hp)) and (st=nil) then
  463. begin
  464. { generates a new unit info record }
  465. hp:=new(pmodule,init(s,true));
  466. { now we can register the unit }
  467. loaded_units.insert(hp);
  468. current_module:=hp;
  469. { force build ? }
  470. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  471. begin
  472. { we needn't the ppufile }
  473. if assigned(hp^.ppufile) then
  474. begin
  475. dispose(hp^.ppufile,done);
  476. hp^.ppufile:=nil;
  477. end;
  478. if not(hp^.sources_avail) then
  479. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  480. else
  481. begin
  482. if assigned(old_current_module^.current_inputfile) then
  483. old_current_module^.current_inputfile^.tempclose;
  484. compile(hp^.mainsource^,compile_system);
  485. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  486. old_current_module^.current_inputfile^.tempreopen;
  487. end;
  488. end
  489. else
  490. begin
  491. { only reassemble ? }
  492. if (hp^.do_assemble) then
  493. OnlyAsm(hp^.asmfilename^);
  494. { we should know there the PPU file else it's an error and
  495. we can't load the unit }
  496. if hp^.ppufile^.name^<>'' then
  497. load_ppu(old_current_module,hp,compile_system);
  498. { add the files for the linker }
  499. addlinkerfiles(hp);
  500. end;
  501. { register the unit _once_ }
  502. usedunits.concat(new(pused_unit,init(hp,0)));
  503. { the unit is written, so we can set the symtable type }
  504. { to unitsymtable, else we get some dupid errors }
  505. { this is not the right place because of the }
  506. { ready label }
  507. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  508. { placed at this end of proc_unit }
  509. psymtable(hp^.symtable)^.unitid:=0;
  510. { reset the unitnumbers for the other units }
  511. pu:=pused_unit(old_current_module^.used_units.first);
  512. while assigned(pu) do
  513. begin
  514. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  515. pu:=pused_unit(pu^.next);
  516. end;
  517. end
  518. else
  519. if assigned(hp) and (st=nil) then
  520. begin
  521. { we have to compile the unit again, but it is already inserted !!}
  522. { we may have problem with the lost symtable !! }
  523. current_module:=hp;
  524. { we must preserve the unit chain }
  525. nextmodule:=pmodule(hp^.next);
  526. { we have to cleanup a little }
  527. hp^.special_done;
  528. new(hs);
  529. hs^:=hp^.mainsource^;
  530. hp^.init(hs^,true);
  531. dispose(hs);
  532. { we must preserve the unit chain }
  533. hp^.next:=nextmodule;
  534. if assigned(hp^.ppufile) then
  535. load_ppu(old_current_module,hp,compile_system)
  536. else
  537. begin
  538. {$ifdef UseBrowser}
  539. { here we need to remove the names ! }
  540. hp^.sourcefiles.done;
  541. hp^.sourcefiles.init;
  542. {$endif UseBrowser}
  543. if assigned(old_current_module^.current_inputfile) then
  544. old_current_module^.current_inputfile^.tempclose;
  545. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  546. compile(hp^.mainsource^,compile_system);
  547. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  548. old_current_module^.current_inputfile^.tempreopen;
  549. end;
  550. current_module^.compiled:=true;
  551. end;
  552. { set the old module }
  553. current_module:=old_current_module;
  554. { the current module uses the unit hp }
  555. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  556. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  557. if in_uses and not current_module^.in_implementation then
  558. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  559. loadunit:=hp;
  560. end;
  561. {$endif OLDPPU}
  562. procedure loadsystemunit;
  563. var
  564. hp : pmodule;
  565. begin
  566. { if the current file isn't a system unit the the system unit
  567. will be loaded }
  568. if not(cs_compilesystem in aktswitches) then
  569. begin
  570. {$ifndef OLDPPU}
  571. hp:=loadunit(upper(target_info.system_unit),true);
  572. systemunit:=hp^.symtable;
  573. { add to the used units }
  574. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  575. {$else OLDPPU}
  576. hp:=loadunit(upper(target_info.system_unit),true,true);
  577. systemunit:=hp^.symtable;
  578. { add to the used units }
  579. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  580. {$endif OLDPPU}
  581. { read default constant definitions }
  582. make_ref:=false;
  583. readconstdefs;
  584. { we could try to overload caret by default }
  585. symtablestack:=systemunit;
  586. { if POWER is defined in the RTL then use it for starstar overloading }
  587. getsym('POWER',false);
  588. if assigned(srsym) and (srsym^.typ=procsym) and
  589. (overloaded_operators[STARSTAR]=nil) then
  590. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  591. make_ref:=true;
  592. end
  593. else
  594. begin
  595. createconstdefs;
  596. systemunit:=nil;
  597. end;
  598. end;
  599. procedure loadunits;
  600. var
  601. s : stringid;
  602. hp : pused_unit;
  603. hp2 : pmodule;
  604. hp3 : psymtable;
  605. oldprocsym:Pprocsym;
  606. begin
  607. oldprocsym:=aktprocsym;
  608. consume(_USES);
  609. {$ifdef DEBUG}
  610. test_symtablestack;
  611. {$endif DEBUG}
  612. repeat
  613. s:=pattern;
  614. consume(ID);
  615. {$ifndef OLDPPU}
  616. hp2:=loadunit(s,false);
  617. { the current module uses the unit hp2 }
  618. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  619. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  620. {$else OLDPPU}
  621. hp2:=loadunit(s,false,true);
  622. {$endif OLDPPU}
  623. if current_module^.compiled then
  624. exit;
  625. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  626. if token=COMMA then
  627. begin
  628. pattern:='';
  629. consume(COMMA);
  630. end
  631. else
  632. break;
  633. until false;
  634. consume(SEMICOLON);
  635. { set the symtable to systemunit so it gets reorderd correctly }
  636. symtablestack:=systemunit;
  637. { now insert the units in the symtablestack }
  638. hp:=pused_unit(current_module^.used_units.first);
  639. while assigned(hp) do
  640. begin
  641. {$IfDef GDB}
  642. if (cs_debuginfo in aktswitches) and
  643. not hp^.is_stab_written then
  644. begin
  645. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  646. hp^.is_stab_written:=true;
  647. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  648. end;
  649. {$EndIf GDB}
  650. if hp^.in_uses then
  651. begin
  652. hp3:=symtablestack;
  653. while assigned(hp3) do
  654. begin
  655. { insert units only once ! }
  656. if hp^.u^.symtable=hp3 then
  657. break;
  658. hp3:=hp3^.next;
  659. { unit isn't inserted }
  660. if hp3=nil then
  661. begin
  662. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  663. symtablestack:=psymtable(hp^.u^.symtable);
  664. {$ifdef CHAINPROCSYMS}
  665. symtablestack^.chainprocsyms;
  666. {$endif CHAINPROCSYMS}
  667. {$ifdef DEBUG}
  668. test_symtablestack;
  669. {$endif DEBUG}
  670. end;
  671. end;
  672. end;
  673. hp:=pused_unit(hp^.next);
  674. end;
  675. aktprocsym:=oldprocsym;
  676. end;
  677. procedure parse_implementation_uses(symt:Psymtable);
  678. var
  679. old_module_in_implementation : boolean;
  680. begin
  681. if token=_USES then
  682. begin
  683. old_module_in_implementation:=module_in_implementation;
  684. module_in_implementation:=true;
  685. current_module^.in_implementation:=true;
  686. symt^.symtabletype:=unitsymtable;
  687. loadunits;
  688. symt^.symtabletype:=globalsymtable;
  689. {$ifdef DEBUG}
  690. test_symtablestack;
  691. {$endif DEBUG}
  692. module_in_implementation:=old_module_in_implementation;
  693. end;
  694. end;
  695. procedure proc_unit;
  696. var
  697. { unitname : stringid; }
  698. names:Tstringcontainer;
  699. p : psymtable;
  700. unitst : punitsymtable;
  701. pu : pused_unit;
  702. s1,s2 : ^string; {Saves stack space}
  703. begin
  704. consume(_UNIT);
  705. if token=ID then
  706. begin
  707. { create filenames and unit name }
  708. current_module^.SetFileName(current_module^.current_inputfile^.path^,current_module^.current_inputfile^.name^);
  709. stringdispose(current_module^.modulename);
  710. current_module^.modulename:=stringdup(upper(pattern));
  711. { check for system unit }
  712. new(s1);
  713. new(s2);
  714. s1^:=upper(target_info.system_unit);
  715. s2^:=upper(current_module^.current_inputfile^.name^);
  716. if (cs_compilesystem in aktswitches) then
  717. begin
  718. if (cs_check_unit_name in aktswitches) and
  719. ((length(current_module^.modulename^)>8) or
  720. (current_module^.modulename^<>s1^) or
  721. (current_module^.modulename^<>s2^)) then
  722. Message1(unit_e_illegal_unit_name,s1^);
  723. end
  724. else
  725. if (current_module^.modulename^=s1^) then
  726. Message(unit_w_switch_us_missed);
  727. dispose(s2);
  728. dispose(s1);
  729. { Add Object File }
  730. if (cs_smartlink in aktswitches) then
  731. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  732. else
  733. current_module^.linkofiles.insert(current_module^.objfilename^);
  734. end;
  735. consume(ID);
  736. consume(SEMICOLON);
  737. consume(_INTERFACE);
  738. { this should be placed after uses !!}
  739. {$ifndef UseNiceNames}
  740. procprefix:='_'+current_module^.modulename^+'$$';
  741. {$else UseNiceNames}
  742. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  743. {$endif UseNiceNames}
  744. parse_only:=true;
  745. { generate now the global symboltable }
  746. p:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  747. refsymtable:=p;
  748. unitst:=punitsymtable(p);
  749. { the unit name must be usable as a unit specifier }
  750. { inside the unit itself (PM) }
  751. { this also forbids to have another symbol }
  752. { with the same name as the unit }
  753. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  754. { set the symbol table for the current unit }
  755. { this must be set later for interdependency }
  756. { current_module^.symtable:=psymtable(p); }
  757. { a unit compiled at command line must be inside the loaded_unit list }
  758. if (compile_level=1) then
  759. begin
  760. loaded_units.insert(current_module);
  761. if cs_unit_to_lib in initswitches then
  762. begin
  763. current_module^.flags:=current_module^.flags or uf_in_library;
  764. if cs_shared_lib in initswitches then
  765. current_module^.flags:=current_module^.flags or uf_shared_library;
  766. end;
  767. end;
  768. { insert qualifier for the system unit (allows system.writeln) }
  769. if not(cs_compilesystem in aktswitches) then
  770. begin
  771. { insert the system unit }
  772. { it is allways the first }
  773. systemunit^.next:=nil;
  774. symtablestack:=systemunit;
  775. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  776. if token=_USES then
  777. begin
  778. unitst^.symtabletype:=unitsymtable;
  779. loadunits;
  780. { has it been compiled at a higher level ?}
  781. if current_module^.compiled then
  782. exit;
  783. unitst^.symtabletype:=globalsymtable;
  784. end;
  785. { ... but insert the symbol table later }
  786. p^.next:=symtablestack;
  787. symtablestack:=p;
  788. end
  789. else
  790. { while compiling a system unit, some types are directly inserted }
  791. begin
  792. p^.next:=symtablestack;
  793. symtablestack:=p;
  794. insert_intern_types(p);
  795. end;
  796. { displaced for inter-dependency considerations }
  797. current_module^.symtable:=psymtable(p);
  798. constsymtable:=symtablestack;
  799. { ... parse the declarations }
  800. read_interface_declarations;
  801. consume(_IMPLEMENTATION);
  802. parse_only:=false;
  803. {$ifdef GDB}
  804. { add all used definitions even for implementation}
  805. if (cs_debuginfo in aktswitches) then
  806. begin
  807. { all types }
  808. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  809. { and all local symbols}
  810. refsymtable^.concatstabto(debuglist);
  811. end;
  812. {$endif GDB}
  813. { generates static symbol table }
  814. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  815. {Generate a procsym.}
  816. make_ref:=false;
  817. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  818. aktprocsym^.definition:=new(Pprocdef,init);
  819. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  820. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  821. make_ref:=true;
  822. {The generated procsym has a local symtable. Discard it and turn
  823. it into the static one.}
  824. dispose(aktprocsym^.definition^.localst,done);
  825. aktprocsym^.definition^.localst:=p;
  826. { remove the globalsymtable from the symtable stack }
  827. { to reinsert it after loading the implementation units }
  828. symtablestack:=unitst^.next;
  829. { number the definitions, so a deref from other units works }
  830. refsymtable^.number_defs;
  831. { Read the implementation units }
  832. parse_implementation_uses(unitst);
  833. numberunits;
  834. { now we can change refsymtable }
  835. refsymtable:=p;
  836. { but reinsert the global symtable as lasts }
  837. unitst^.next:=symtablestack;
  838. symtablestack:=unitst;
  839. {$ifdef DEBUG}
  840. test_symtablestack;
  841. {$endif DEBUG}
  842. constsymtable:=symtablestack;
  843. {$ifdef Splitheap}
  844. if testsplit then
  845. begin
  846. Split_Heap;
  847. allow_special:=true;
  848. Switch_to_temp_heap;
  849. end;
  850. { it will report all crossings }
  851. allow_special:=false;
  852. {$endif Splitheap}
  853. { set some informations }
  854. procinfo.retdef:=voiddef;
  855. procinfo._class:=nil;
  856. procinfo.call_offset:=8;
  857. { for temporary values }
  858. procinfo.framepointer:=frame_pointer;
  859. { clear flags }
  860. procinfo.flags:=0;
  861. { Create a new procedure }
  862. codegen_newprocedure;
  863. { Compile the unit }
  864. names.init;
  865. names.insert(current_module^.modulename^+'_init');
  866. names.insert('INIT$$'+current_module^.modulename^);
  867. compile_proc_body(names,true,false);
  868. names.done;
  869. { Shutdown the codegen for this procedure }
  870. codegen_doneprocedure;
  871. {$ifdef dummy}
  872. if token=_FINALIZATION then
  873. begin
  874. current_module^.flags:=current_module^.flags or uf_finalize;
  875. { clear flags }
  876. procinfo.flags:=0;
  877. {Reset the codegenerator.}
  878. codegen_newprocedure;
  879. names.init;
  880. names.insert(current_module^.modulename^+'_finalize');
  881. names.insert('FINALIZE$$'+current_module^.modulename^);
  882. compile_proc_body(names,true,false);
  883. names.done;
  884. codegen_doneprocedure;
  885. end;
  886. {$endif dummy}
  887. consume(POINT);
  888. { size of the static data }
  889. datasize:=symtablestack^.datasize;
  890. { unsed static symbols ? }
  891. symtablestack^.allsymbolsused;
  892. {$ifdef GDB}
  893. { add all used definitions even for implementation}
  894. if (cs_debuginfo in aktswitches) then
  895. begin
  896. { all types }
  897. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  898. { and all local symbols}
  899. symtablestack^.concatstabto(debuglist);
  900. end;
  901. {$endif GDB}
  902. current_module^.in_implementation:=false;
  903. { deletes all symtables generated in the implementation part }
  904. while symtablestack^.symtabletype<>globalsymtable do
  905. dellexlevel;
  906. { tests, if all forwards are resolved }
  907. symtablestack^.check_forwards;
  908. symtablestack^.symtabletype:=unitsymtable;
  909. punitsymtable(symtablestack)^.is_stab_written:=false;
  910. {Write out the unit if the compile was succesfull.}
  911. if status.errorcount=0 then
  912. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  913. pu:=pused_unit(usedunits.first);
  914. while assigned(pu) do
  915. begin
  916. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  917. pu:=pused_unit(pu^.next);
  918. end;
  919. inc(datasize,symtablestack^.datasize);
  920. { generate imports }
  921. if current_module^.uses_imports then
  922. importlib^.generatelib;
  923. { finish asmlist by adding segment starts }
  924. insertsegment;
  925. end;
  926. procedure proc_program(islibrary : boolean);
  927. var
  928. st : psymtable;
  929. names : Tstringcontainer;
  930. begin
  931. { Trying to compile the system unit... }
  932. { if no unit defined... then issue a }
  933. { fatal error (avoids pointer problems)}
  934. { when referencing the non-existant }
  935. { system unit. }
  936. { System Unit should be compiled using proc_unit !! (PFV) }
  937. { if (cs_compilesystem in aktswitches) then
  938. Begin
  939. if token<>_UNIT then
  940. Message1(scan_f_syn_expected,'UNIT');
  941. consume(_UNIT);
  942. end;}
  943. parse_only:=false;
  944. if islibrary then
  945. begin
  946. consume(_LIBRARY);
  947. stringdispose(current_module^.modulename);
  948. current_module^.modulename:=stringdup(pattern);
  949. consume(ID);
  950. consume(SEMICOLON);
  951. end
  952. else
  953. { is there an program head ? }
  954. if token=_PROGRAM then
  955. begin
  956. consume(_PROGRAM);
  957. stringdispose(current_module^.modulename);
  958. current_module^.modulename:=stringdup(pattern);
  959. consume(ID);
  960. if token=LKLAMMER then
  961. begin
  962. consume(LKLAMMER);
  963. idlist;
  964. consume(RKLAMMER);
  965. end;
  966. consume(SEMICOLON);
  967. end;
  968. { insert after the unit symbol tables the static symbol table }
  969. { of the program }
  970. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  971. {Generate a procsym.}
  972. make_ref:=false;
  973. aktprocsym:=new(Pprocsym,init('main'));
  974. aktprocsym^.definition:=new(Pprocdef,init);
  975. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  976. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  977. make_ref:=true;
  978. {The localst is a local symtable. Change it into the static
  979. symtable.}
  980. dispose(aktprocsym^.definition^.localst,done);
  981. aktprocsym^.definition^.localst:=st;
  982. refsymtable:=st;
  983. { necessary for browser }
  984. loaded_units.insert(current_module);
  985. {Insert the symbols of the system unit into the stack of symbol
  986. tables.}
  987. symtablestack:=systemunit;
  988. systemunit^.next:=nil;
  989. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  990. {Load the units used by the program we compile.}
  991. if token=_USES then
  992. loadunits;
  993. {Insert the name of the main program into the symbol table.}
  994. if current_module^.modulename^<>'' then
  995. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  996. { ...is also constsymtable, this is the symtable where }
  997. { the elements of enumeration types are inserted }
  998. constsymtable:=st;
  999. { set some informations about the main program }
  1000. with procinfo do
  1001. begin
  1002. retdef:=voiddef;
  1003. _class:=nil;
  1004. call_offset:=8;
  1005. framepointer:=frame_pointer;
  1006. flags:=0;
  1007. end;
  1008. procprefix:='';
  1009. in_except_block:=false;
  1010. codegen_newprocedure;
  1011. {The program intialization needs an alias, so it can be called
  1012. from the bootstrap code.}
  1013. names.init;
  1014. names.insert('program_init');
  1015. names.insert('PASCALMAIN');
  1016. names.insert(target_os.cprefix+'main');
  1017. compile_proc_body(names,true,false);
  1018. names.done;
  1019. codegen_doneprocedure;
  1020. consume(POINT);
  1021. if (cs_smartlink in aktswitches) then
  1022. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  1023. else
  1024. current_module^.linkofiles.insert(current_module^.objfilename^);
  1025. { insert heap }
  1026. insertheap;
  1027. { generate imports }
  1028. if current_module^.uses_imports then
  1029. importlib^.generatelib;
  1030. inserttargetspecific;
  1031. datasize:=symtablestack^.datasize;
  1032. { finish asmlist by adding segment starts }
  1033. insertsegment;
  1034. end;
  1035. end.
  1036. {
  1037. $Log$
  1038. Revision 1.33 1998-06-25 11:15:34 pierre
  1039. * ppu files where not closed in newppu !!
  1040. second compilation was impossible due to too many opened files
  1041. (not visible in 'make cycle' as we remove all the ppu files)
  1042. Revision 1.32 1998/06/25 08:48:16 florian
  1043. * first version of rtti support
  1044. Revision 1.31 1998/06/24 14:48:35 peter
  1045. * ifdef newppu -> ifndef oldppu
  1046. Revision 1.30 1998/06/17 14:10:16 peter
  1047. * small os2 fixes
  1048. * fixed interdependent units with newppu (remake3 under linux works now)
  1049. Revision 1.29 1998/06/16 08:56:25 peter
  1050. + targetcpu
  1051. * cleaner pmodules for newppu
  1052. Revision 1.28 1998/06/13 00:10:10 peter
  1053. * working browser and newppu
  1054. * some small fixes against crashes which occured in bp7 (but not in
  1055. fpc?!)
  1056. Revision 1.27 1998/06/11 13:58:08 peter
  1057. * small fix to let newppu compile
  1058. Revision 1.26 1998/06/09 16:01:47 pierre
  1059. + added procedure directive parsing for procvars
  1060. (accepted are popstack cdecl and pascal)
  1061. + added C vars with the following syntax
  1062. var C calias 'true_c_name';(can be followed by external)
  1063. reason is that you must add the Cprefix
  1064. which is target dependent
  1065. Revision 1.25 1998/06/08 22:59:49 peter
  1066. * smartlinking works for win32
  1067. * some defines to exclude some compiler parts
  1068. Revision 1.24 1998/06/08 13:13:44 pierre
  1069. + temporary variables now in temp_gen.pas unit
  1070. because it is processor independent
  1071. * mppc68k.bat modified to undefine i386 and support_mmx
  1072. (which are defaults for i386)
  1073. Revision 1.23 1998/06/05 17:47:29 peter
  1074. * some better uses clauses
  1075. Revision 1.22 1998/06/05 14:37:34 pierre
  1076. * fixes for inline for operators
  1077. * inline procedure more correctly restricted
  1078. Revision 1.21 1998/06/04 23:51:53 peter
  1079. * m68k compiles
  1080. + .def file creation moved to gendef.pas so it could also be used
  1081. for win32
  1082. Revision 1.20 1998/06/04 09:55:42 pierre
  1083. * demangled name of procsym reworked to become independant of the mangling scheme
  1084. Revision 1.19 1998/06/03 23:40:38 peter
  1085. + unlimited file support, release tempclose
  1086. Revision 1.18 1998/06/03 22:49:00 peter
  1087. + wordbool,longbool
  1088. * rename bis,von -> high,low
  1089. * moved some systemunit loading/creating to psystem.pas
  1090. Revision 1.17 1998/05/28 14:40:25 peter
  1091. * fixes for newppu, remake3 works now with it
  1092. Revision 1.16 1998/05/27 19:45:06 peter
  1093. * symtable.pas splitted into includefiles
  1094. * symtable adapted for $ifdef NEWPPU
  1095. Revision 1.15 1998/05/23 01:21:22 peter
  1096. + aktasmmode, aktoptprocessor, aktoutputformat
  1097. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1098. + $LIBNAME to set the library name where the unit will be put in
  1099. * splitted cgi386 a bit (codeseg to large for bp7)
  1100. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1101. Revision 1.14 1998/05/20 09:42:35 pierre
  1102. + UseTokenInfo now default
  1103. * unit in interface uses and implementation uses gives error now
  1104. * only one error for unknown symbol (uses lastsymknown boolean)
  1105. the problem came from the label code !
  1106. + first inlined procedures and function work
  1107. (warning there might be allowed cases were the result is still wrong !!)
  1108. * UseBrower updated gives a global list of all position of all used symbols
  1109. with switch -gb
  1110. Revision 1.13 1998/05/12 10:47:00 peter
  1111. * moved printstatus to verb_def
  1112. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1113. prefix like error: warning: and is included in V_Default
  1114. * fixed some messages
  1115. * first time parameter scan is only for -v and -T
  1116. - removed old style messages
  1117. Revision 1.12 1998/05/11 13:07:56 peter
  1118. + $ifdef NEWPPU for the new ppuformat
  1119. + $define GDB not longer required
  1120. * removed all warnings and stripped some log comments
  1121. * no findfirst/findnext anymore to remove smartlink *.o files
  1122. Revision 1.11 1998/05/06 18:36:53 peter
  1123. * tai_section extended with code,data,bss sections and enumerated type
  1124. * ident 'compiled by FPC' moved to pmodules
  1125. * small fix for smartlink
  1126. Revision 1.10 1998/05/04 17:54:28 peter
  1127. + smartlinking works (only case jumptable left todo)
  1128. * redesign of systems.pas to support assemblers and linkers
  1129. + Unitname is now also in the PPU-file, increased version to 14
  1130. Revision 1.9 1998/05/01 16:38:45 florian
  1131. * handling of private and protected fixed
  1132. + change_keywords_to_tp implemented to remove
  1133. keywords which aren't supported by tp
  1134. * break and continue are now symbols of the system unit
  1135. + widestring, longstring and ansistring type released
  1136. Revision 1.8 1998/04/30 15:59:41 pierre
  1137. * GDB works again better :
  1138. correct type info in one pass
  1139. + UseTokenInfo for better source position
  1140. * fixed one remaining bug in scanner for line counts
  1141. * several little fixes
  1142. Revision 1.7 1998/04/29 10:33:59 pierre
  1143. + added some code for ansistring (not complete nor working yet)
  1144. * corrected operator overloading
  1145. * corrected nasm output
  1146. + started inline procedures
  1147. + added starstarn : use ** for exponentiation (^ gave problems)
  1148. + started UseTokenInfo cond to get accurate positions
  1149. Revision 1.6 1998/04/27 23:10:28 peter
  1150. + new scanner
  1151. * $makelib -> if smartlink
  1152. * small filename fixes pmodule.setfilename
  1153. * moved import from files.pas -> import.pas
  1154. Revision 1.5 1998/04/14 23:27:03 florian
  1155. + exclude/include with constant second parameter added
  1156. Revision 1.4 1998/04/10 14:41:43 peter
  1157. * removed some Hints
  1158. * small speed optimization for AsmLn
  1159. Revision 1.3 1998/04/03 09:51:00 daniel
  1160. * Fixed heap allocation for OS/2.
  1161. }