files.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit implements an extended file management and the first loading
  5. and searching of the modules (ppufiles)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit files;
  20. interface
  21. uses
  22. cobjects,globals,ppu;
  23. const
  24. {$ifdef FPC}
  25. maxunits = 1024;
  26. InputFileBufSize=32*1024;
  27. linebufincrease=512;
  28. {$else}
  29. maxunits = 128;
  30. InputFileBufSize=1024;
  31. linebufincrease=64;
  32. {$endif}
  33. type
  34. {$ifdef FPC}
  35. tlongintarr = array[0..1000000] of longint;
  36. {$else}
  37. tlongintarr = array[0..16000] of longint;
  38. {$endif}
  39. plongintarr = ^tlongintarr;
  40. pinputfile = ^tinputfile;
  41. tinputfile = object
  42. path,name : pstring; { path and filename }
  43. next : pinputfile; { next file for reading }
  44. f : file; { current file handle }
  45. is_macro,
  46. endoffile, { still bytes left to read }
  47. closed : boolean; { is the file closed }
  48. buf : pchar; { buffer }
  49. bufstart, { buffer start position in the file }
  50. bufsize, { amount of bytes in the buffer }
  51. maxbufsize : longint; { size in memory for the buffer }
  52. saveinputpointer : pchar; { save fields for scanner variables }
  53. savelastlinepos,
  54. saveline_no : longint;
  55. linebuf : plongintarr; { line buffer to retrieve lines }
  56. maxlinebuf : longint;
  57. ref_count : longint; { to handle the browser refs }
  58. ref_index : longint;
  59. ref_next : pinputfile;
  60. constructor init(const fn:string);
  61. destructor done;
  62. procedure setpos(l:longint);
  63. procedure seekbuf(fpos:longint);
  64. procedure readbuf;
  65. function open:boolean;
  66. procedure close;
  67. procedure tempclose;
  68. function tempopen:boolean;
  69. procedure setmacro(p:pchar;len:longint);
  70. procedure setline(line,linepos:longint);
  71. function getlinestr(l:longint):string;
  72. end;
  73. pfilemanager = ^tfilemanager;
  74. tfilemanager = object
  75. files : pinputfile;
  76. last_ref_index : longint;
  77. constructor init;
  78. destructor done;
  79. procedure register_file(f : pinputfile);
  80. procedure inverse_register_indexes;
  81. function get_file(l:longint) : pinputfile;
  82. function get_file_name(l :longint):string;
  83. function get_file_path(l :longint):string;
  84. end;
  85. type
  86. {$ifndef NEWMAP}
  87. tunitmap = array[0..maxunits-1] of pointer;
  88. punitmap = ^tunitmap;
  89. pmodule = ^tmodule;
  90. {$else NEWMAP}
  91. pmodule = ^tmodule;
  92. tunitmap = array[0..maxunits-1] of pmodule;
  93. punitmap = ^tunitmap;
  94. {$endif NEWMAP}
  95. tmodule = object(tlinkedlist_item)
  96. ppufile : pppufile; { the PPU file }
  97. crc,
  98. flags : longint; { the PPU flags }
  99. compiled, { unit is already compiled }
  100. do_assemble, { only assemble the object, don't recompile }
  101. do_compile, { need to compile the sources }
  102. sources_avail, { if all sources are reachable }
  103. is_unit,
  104. in_second_compile, { is this unit being compiled for the 2nd time? }
  105. in_implementation, { processing the implementation part? }
  106. in_global : boolean; { allow global settings }
  107. map : punitmap; { mapping of all used units }
  108. unitcount : word; { local unit counter }
  109. unit_index : word; { global counter for browser }
  110. globalsymtable, { pointer to the local/static symtable of this unit }
  111. localsymtable : pointer; { pointer to the psymtable of this unit }
  112. scanner : pointer; { scanner object used }
  113. loaded_from : pmodule;
  114. uses_imports : boolean; { Set if the module imports from DLL's.}
  115. imports : plinkedlist;
  116. sourcefiles : pfilemanager;
  117. linksharedlibs,
  118. linkstaticlibs,
  119. linkofiles : tstringcontainer;
  120. used_units : tlinkedlist;
  121. path, { path where the module is find/created }
  122. modulename, { name of the module in uppercase }
  123. objfilename, { fullname of the objectfile }
  124. asmfilename, { fullname of the assemblerfile }
  125. ppufilename, { fullname of the ppufile }
  126. staticlibfilename, { fullname of the static libraryfile }
  127. sharedlibfilename, { fullname of the shared libraryfile }
  128. exefilename, { fullname of the exefile }
  129. asmprefix, { prefix for the smartlink asmfiles }
  130. mainsource : pstring; { name of the main sourcefile }
  131. constructor init(const s:string;_is_unit:boolean);
  132. destructor done;virtual;
  133. procedure reset;
  134. procedure setfilename(const fn:string;allowoutput:boolean);
  135. function openppu:boolean;
  136. function search_unit(const n : string):boolean;
  137. end;
  138. pused_unit = ^tused_unit;
  139. tused_unit = object(tlinkedlist_item)
  140. unitid : word;
  141. name : pstring;
  142. checksum : longint;
  143. loaded : boolean;
  144. in_uses,
  145. in_interface,
  146. is_stab_written : boolean;
  147. u : pmodule;
  148. constructor init(_u : pmodule;intface:boolean);
  149. constructor init_to_load(const n:string;c:longint;intface:boolean);
  150. destructor done;virtual;
  151. end;
  152. var
  153. main_module : pmodule; { Main module of the program }
  154. current_module : pmodule; { Current module which is compiled }
  155. current_ppu : pppufile; { Current ppufile which is read }
  156. global_unit_count : word;
  157. usedunits : tlinkedlist; { Used units for this program }
  158. loaded_units : tlinkedlist; { All loaded units }
  159. implementation
  160. uses
  161. dos,verbose,systems
  162. {$ifndef VER0_99_8}
  163. ,symtable,scanner
  164. {$endif}
  165. ;
  166. {****************************************************************************
  167. TINPUTFILE
  168. ****************************************************************************}
  169. constructor tinputfile.init(const fn:string);
  170. var
  171. p,n,e : string;
  172. begin
  173. FSplit(fn,p,n,e);
  174. name:=stringdup(n+e);
  175. path:=stringdup(p);
  176. next:=nil;
  177. { file info }
  178. is_macro:=false;
  179. endoffile:=false;
  180. closed:=true;
  181. buf:=nil;
  182. bufstart:=0;
  183. bufsize:=0;
  184. maxbufsize:=InputFileBufSize;
  185. { save fields }
  186. saveinputpointer:=nil;
  187. saveline_no:=0;
  188. savelastlinepos:=0;
  189. { indexing refs }
  190. ref_next:=nil;
  191. ref_count:=0;
  192. ref_index:=0;
  193. { line buffer }
  194. linebuf:=nil;
  195. maxlinebuf:=0;
  196. end;
  197. destructor tinputfile.done;
  198. begin
  199. if not closed then
  200. close;
  201. stringdispose(path);
  202. stringdispose(name);
  203. { free memory }
  204. if assigned(linebuf) then
  205. freemem(linebuf,maxlinebuf shl 2);
  206. end;
  207. procedure tinputfile.setpos(l:longint);
  208. begin
  209. bufstart:=l;
  210. end;
  211. procedure tinputfile.seekbuf(fpos:longint);
  212. begin
  213. if closed then
  214. exit;
  215. seek(f,fpos);
  216. bufstart:=fpos;
  217. bufsize:=0;
  218. end;
  219. procedure tinputfile.readbuf;
  220. {$ifdef TP}
  221. var
  222. w : word;
  223. {$endif}
  224. begin
  225. if is_macro then
  226. endoffile:=true;
  227. if closed then
  228. exit;
  229. inc(bufstart,bufsize);
  230. {$ifdef TP}
  231. blockread(f,buf^,maxbufsize-1,w);
  232. bufsize:=w;
  233. {$else}
  234. blockread(f,buf^,maxbufsize-1,bufsize);
  235. {$endif}
  236. buf[bufsize]:=#0;
  237. endoffile:=not(bufsize=maxbufsize-1);
  238. end;
  239. function tinputfile.open:boolean;
  240. var
  241. ofm : byte;
  242. begin
  243. open:=false;
  244. if not closed then
  245. Close;
  246. ofm:=filemode;
  247. filemode:=0;
  248. Assign(f,path^+name^);
  249. {$I-}
  250. reset(f,1);
  251. {$I+}
  252. filemode:=ofm;
  253. if ioresult<>0 then
  254. exit;
  255. { file }
  256. endoffile:=false;
  257. closed:=false;
  258. Getmem(buf,MaxBufsize);
  259. bufstart:=0;
  260. bufsize:=0;
  261. open:=true;
  262. end;
  263. procedure tinputfile.close;
  264. var
  265. i : word;
  266. begin
  267. if is_macro then
  268. begin
  269. Freemem(buf,maxbufsize);
  270. buf:=nil;
  271. {is_macro:=false;
  272. still needed for dispose in scanner PM }
  273. closed:=true;
  274. exit;
  275. end;
  276. if not closed then
  277. begin
  278. {$I-}
  279. system.close(f);
  280. {$I+}
  281. i:=ioresult;
  282. closed:=true;
  283. end;
  284. if assigned(buf) then
  285. begin
  286. Freemem(buf,maxbufsize);
  287. buf:=nil;
  288. end;
  289. bufstart:=0;
  290. end;
  291. procedure tinputfile.tempclose;
  292. var
  293. i : word;
  294. begin
  295. if is_macro then
  296. exit;
  297. if not closed then
  298. begin
  299. {$I-}
  300. system.close(f);
  301. {$I+}
  302. i:=ioresult;
  303. Freemem(buf,maxbufsize);
  304. buf:=nil;
  305. closed:=true;
  306. end;
  307. end;
  308. function tinputfile.tempopen:boolean;
  309. var
  310. ofm : byte;
  311. begin
  312. tempopen:=false;
  313. if is_macro then
  314. begin
  315. tempopen:=true;
  316. exit;
  317. end;
  318. if not closed then
  319. exit;
  320. ofm:=filemode;
  321. filemode:=0;
  322. Assign(f,path^+name^);
  323. {$I-}
  324. reset(f,1);
  325. {$I+}
  326. filemode:=ofm;
  327. if ioresult<>0 then
  328. exit;
  329. closed:=false;
  330. { get new mem }
  331. Getmem(buf,maxbufsize);
  332. { restore state }
  333. seek(f,BufStart);
  334. bufsize:=0;
  335. readbuf;
  336. tempopen:=true;
  337. end;
  338. procedure tinputfile.setmacro(p:pchar;len:longint);
  339. begin
  340. { create new buffer }
  341. getmem(buf,len+1);
  342. move(p^,buf^,len);
  343. buf[len]:=#0;
  344. { reset }
  345. bufstart:=0;
  346. bufsize:=len;
  347. maxbufsize:=len+1;
  348. is_macro:=true;
  349. endoffile:=true;
  350. closed:=true;
  351. end;
  352. procedure tinputfile.setline(line,linepos:longint);
  353. var
  354. oldlinebuf : plongintarr;
  355. begin
  356. if line<1 then
  357. exit;
  358. while (line>=maxlinebuf) do
  359. begin
  360. oldlinebuf:=linebuf;
  361. { create new linebuf and move old info }
  362. getmem(linebuf,(maxlinebuf+linebufincrease) shl 2);
  363. if assigned(oldlinebuf) then
  364. begin
  365. move(oldlinebuf^,linebuf^,maxlinebuf shl 2);
  366. freemem(oldlinebuf,maxlinebuf shl 2);
  367. end;
  368. fillchar(linebuf^[maxlinebuf],linebufincrease shl 2,0);
  369. inc(maxlinebuf,linebufincrease);
  370. end;
  371. linebuf^[line]:=linepos;
  372. end;
  373. function tinputfile.getlinestr(l:longint):string;
  374. var
  375. c : char;
  376. i,
  377. fpos : longint;
  378. p : pchar;
  379. begin
  380. getlinestr:='';
  381. if l<maxlinebuf then
  382. begin
  383. fpos:=linebuf^[l];
  384. { fpos is set negativ if the line was already written }
  385. { but we still know the correct value }
  386. if fpos<0 then
  387. fpos:=-fpos+1;
  388. if closed then
  389. open;
  390. { in current buf ? }
  391. if (fpos<bufstart) or (fpos>bufstart+bufsize) then
  392. begin
  393. seekbuf(fpos);
  394. readbuf;
  395. end;
  396. { the begin is in the buf now simply read until #13,#10 }
  397. i:=0;
  398. p:=@buf[fpos-bufstart];
  399. repeat
  400. c:=p^;
  401. if c=#0 then
  402. begin
  403. readbuf;
  404. p:=buf;
  405. c:=p^;
  406. end;
  407. if c in [#10,#13] then
  408. break;
  409. inc(i);
  410. getlinestr[i]:=c;
  411. inc(longint(p));
  412. until (i=255);
  413. getlinestr[0]:=chr(i);
  414. end;
  415. end;
  416. {****************************************************************************
  417. TFILEMANAGER
  418. ****************************************************************************}
  419. constructor tfilemanager.init;
  420. begin
  421. files:=nil;
  422. last_ref_index:=0;
  423. end;
  424. destructor tfilemanager.done;
  425. var
  426. hp : pinputfile;
  427. begin
  428. hp:=files;
  429. while assigned(hp) do
  430. begin
  431. files:=files^.ref_next;
  432. dispose(hp,done);
  433. hp:=files;
  434. end;
  435. last_ref_index:=0;
  436. end;
  437. procedure tfilemanager.register_file(f : pinputfile);
  438. begin
  439. inc(last_ref_index);
  440. f^.ref_next:=files;
  441. f^.ref_index:=last_ref_index;
  442. files:=f;
  443. {$ifdef FPC}
  444. {$ifdef heaptrc}
  445. writeln(stderr,f^.name^,' index ',current_module^.unit_index*100000+f^.ref_index);
  446. {$endif heaptrc}
  447. {$endif FPC}
  448. end;
  449. { this procedure is necessary after loading the
  450. sources files from a PPU file PM }
  451. procedure tfilemanager.inverse_register_indexes;
  452. var
  453. f : pinputfile;
  454. begin
  455. f:=files;
  456. while assigned(f) do
  457. begin
  458. f^.ref_index:=last_ref_index-f^.ref_index+1;
  459. f:=f^.ref_next;
  460. end;
  461. end;
  462. function tfilemanager.get_file(l :longint) : pinputfile;
  463. var
  464. ff : pinputfile;
  465. begin
  466. ff:=files;
  467. while assigned(ff) and (ff^.ref_index<>l) do
  468. ff:=ff^.ref_next;
  469. get_file:=ff;
  470. end;
  471. function tfilemanager.get_file_name(l :longint):string;
  472. var
  473. hp : pinputfile;
  474. begin
  475. hp:=get_file(l);
  476. if assigned(hp) then
  477. get_file_name:=hp^.name^
  478. else
  479. get_file_name:='';
  480. end;
  481. function tfilemanager.get_file_path(l :longint):string;
  482. var
  483. hp : pinputfile;
  484. begin
  485. hp:=get_file(l);
  486. if assigned(hp) then
  487. get_file_path:=hp^.path^
  488. else
  489. get_file_path:='';
  490. end;
  491. {****************************************************************************
  492. TMODULE
  493. ****************************************************************************}
  494. procedure tmodule.setfilename(const fn:string;allowoutput:boolean);
  495. var
  496. p : dirstr;
  497. n : NameStr;
  498. e : ExtStr;
  499. begin
  500. stringdispose(objfilename);
  501. stringdispose(asmfilename);
  502. stringdispose(ppufilename);
  503. stringdispose(staticlibfilename);
  504. stringdispose(sharedlibfilename);
  505. stringdispose(exefilename);
  506. stringdispose(path);
  507. { Create names }
  508. fsplit(fn,p,n,e);
  509. n:=FixFileName(n);
  510. { set path }
  511. path:=stringdup(FixPath(p));
  512. { obj,asm,ppu names }
  513. p:=path^;
  514. if AllowOutput then
  515. begin
  516. if (OutputUnitDir<>'') then
  517. p:=OutputUnitDir
  518. else
  519. if (OutputExeDir<>'') then
  520. p:=OutputExeDir;
  521. end;
  522. objfilename:=stringdup(p+n+target_info.objext);
  523. asmfilename:=stringdup(p+n+target_info.asmext);
  524. ppufilename:=stringdup(p+n+target_info.unitext);
  525. { lib and exe could be loaded with a file specified with -o }
  526. if AllowOutput and (OutputFile<>'') then
  527. n:=OutputFile;
  528. staticlibfilename:=stringdup(p+target_os.libprefix+n+target_os.staticlibext);
  529. sharedlibfilename:=stringdup(p+target_os.libprefix+n+target_os.sharedlibext);
  530. { output dir of exe can be specified separatly }
  531. if AllowOutput and (OutputExeDir<>'') then
  532. p:=OutputExeDir
  533. else
  534. p:=path^;
  535. exefilename:=stringdup(p+n+target_os.exeext);
  536. end;
  537. function tmodule.openppu:boolean;
  538. var
  539. objfiletime,
  540. ppufiletime,
  541. asmfiletime : longint;
  542. begin
  543. openppu:=false;
  544. Message1(unit_t_ppu_loading,ppufilename^);
  545. { Get ppufile time (also check if the file exists) }
  546. ppufiletime:=getnamedfiletime(ppufilename^);
  547. if ppufiletime=-1 then
  548. exit;
  549. { Open the ppufile }
  550. Message1(unit_u_ppu_name,ppufilename^);
  551. ppufile:=new(pppufile,init(ppufilename^));
  552. if not ppufile^.open then
  553. begin
  554. dispose(ppufile,done);
  555. Message(unit_u_ppu_file_too_short);
  556. exit;
  557. end;
  558. { check for a valid PPU file }
  559. if not ppufile^.CheckPPUId then
  560. begin
  561. dispose(ppufile,done);
  562. Message(unit_u_ppu_invalid_header);
  563. exit;
  564. end;
  565. { check for allowed PPU versions }
  566. if not (ppufile^.GetPPUVersion in [15]) then
  567. begin
  568. dispose(ppufile,done);
  569. Message1(unit_u_ppu_invalid_version,tostr(ppufile^.GetPPUVersion));
  570. exit;
  571. end;
  572. { check the target processor }
  573. if ttargetcpu(ppufile^.header.cpu)<>target_cpu then
  574. begin
  575. dispose(ppufile,done);
  576. Message(unit_u_ppu_invalid_processor);
  577. exit;
  578. end;
  579. { check target }
  580. if ttarget(ppufile^.header.target)<>target_info.target then
  581. begin
  582. dispose(ppufile,done);
  583. Message(unit_u_ppu_invalid_target);
  584. exit;
  585. end;
  586. { Load values to be access easier }
  587. flags:=ppufile^.header.flags;
  588. crc:=ppufile^.header.checksum;
  589. { Show Debug info }
  590. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  591. Message1(unit_u_ppu_flags,tostr(flags));
  592. Message1(unit_u_ppu_crc,tostr(ppufile^.header.checksum));
  593. { check the object and assembler file to see if we need only to
  594. assemble, only if it's not in a library }
  595. do_compile:=false;
  596. if (flags and uf_in_library)=0 then
  597. begin
  598. if ((flags and uf_static_linked)<>0) or
  599. ((flags and uf_smartlink)<>0) then
  600. begin
  601. objfiletime:=getnamedfiletime(staticlibfilename^);
  602. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  603. begin
  604. Message(unit_u_recompile_staticlib_is_older);
  605. do_compile:=true;
  606. exit;
  607. end;
  608. end
  609. else
  610. if (flags and uf_shared_linked)<>0 then
  611. begin
  612. objfiletime:=getnamedfiletime(sharedlibfilename^);
  613. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  614. begin
  615. Message(unit_u_recompile_sharedlib_is_older);
  616. do_compile:=true;
  617. exit;
  618. end;
  619. end
  620. else
  621. begin
  622. { the objectfile should be newer than the ppu file }
  623. objfiletime:=getnamedfiletime(objfilename^);
  624. if (ppufiletime<0) or (objfiletime<0) or (ppufiletime>objfiletime) then
  625. begin
  626. { check if assembler file is older than ppu file }
  627. asmfileTime:=GetNamedFileTime(asmfilename^);
  628. if (asmfiletime<0) or (ppufiletime>asmfiletime) then
  629. begin
  630. Message(unit_u_recompile_obj_and_asm_older);
  631. do_compile:=true;
  632. exit;
  633. end
  634. else
  635. begin
  636. Message(unit_u_recompile_obj_older_than_asm);
  637. if not(cs_asm_extern in aktglobalswitches) then
  638. begin
  639. do_compile:=true;
  640. exit;
  641. end;
  642. end;
  643. end;
  644. end;
  645. end;
  646. openppu:=true;
  647. end;
  648. function tmodule.search_unit(const n : string):boolean;
  649. var
  650. ext : string[8];
  651. singlepathstring,
  652. unitPath,
  653. filename : string;
  654. found : boolean;
  655. start,i : longint;
  656. Function UnitExists(const ext:string):boolean;
  657. begin
  658. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  659. UnitExists:=FileExists(Singlepathstring+FileName+ext);
  660. end;
  661. begin
  662. start:=1;
  663. filename:=FixFileName(n);
  664. unitpath:=UnitSearchPath;
  665. Found:=false;
  666. repeat
  667. { Create current path to check }
  668. i:=pos(';',unitpath);
  669. if i=0 then
  670. i:=length(unitpath)+1;
  671. singlepathstring:=FixPath(copy(unitpath,start,i-start));
  672. delete(unitpath,start,i-start+1);
  673. { Check for PPL file }
  674. if not Found then
  675. begin
  676. Found:=UnitExists(target_info.unitlibext);
  677. if Found then
  678. Begin
  679. SetFileName(SinglePathString+FileName,false);
  680. Found:=OpenPPU;
  681. End;
  682. end;
  683. { Check for PPU file }
  684. if not Found then
  685. begin
  686. Found:=UnitExists(target_info.unitext);
  687. if Found then
  688. Begin
  689. SetFileName(SinglePathString+FileName,false);
  690. Found:=OpenPPU;
  691. End;
  692. end;
  693. { Check for Sources }
  694. if not Found then
  695. begin
  696. ppufile:=nil;
  697. do_compile:=true;
  698. {Check for .pp file}
  699. Found:=UnitExists(target_os.sourceext);
  700. if Found then
  701. Ext:=target_os.sourceext
  702. else
  703. begin
  704. {Check for .pas}
  705. Found:=UnitExists(target_os.pasext);
  706. if Found then
  707. Ext:=target_os.pasext;
  708. end;
  709. stringdispose(mainsource);
  710. if Found then
  711. begin
  712. sources_avail:=true;
  713. {Load Filenames when found}
  714. mainsource:=StringDup(SinglePathString+FileName+Ext);
  715. SetFileName(SinglePathString+FileName,false);
  716. end
  717. else
  718. sources_avail:=false;
  719. end;
  720. until Found or (unitpath='');
  721. search_unit:=Found;
  722. end;
  723. procedure tmodule.reset;
  724. begin
  725. {$ifndef VER0_99_8}
  726. if assigned(scanner) then
  727. pscannerfile(scanner)^.invalid:=true;
  728. if assigned(globalsymtable) then
  729. begin
  730. dispose(punitsymtable(globalsymtable),done);
  731. globalsymtable:=nil;
  732. end;
  733. if assigned(localsymtable) then
  734. begin
  735. dispose(punitsymtable(localsymtable),done);
  736. localsymtable:=nil;
  737. end;
  738. {$endif}
  739. if assigned(map) then
  740. begin
  741. dispose(map);
  742. map:=nil;
  743. end;
  744. if assigned(ppufile) then
  745. begin
  746. dispose(ppufile,done);
  747. ppufile:=nil;
  748. end;
  749. sourcefiles^.done;
  750. sourcefiles^.init;
  751. imports^.done;
  752. imports^.init;
  753. used_units.done;
  754. used_units.init;
  755. linkofiles.done;
  756. linkofiles.init;
  757. linkstaticlibs.done;
  758. linkstaticlibs.init;
  759. linksharedlibs.done;
  760. linksharedlibs.init;
  761. uses_imports:=false;
  762. do_assemble:=false;
  763. do_compile:=false;
  764. { sources_avail:=true;
  765. should not be changed PM }
  766. compiled:=false;
  767. in_implementation:=false;
  768. in_global:=true;
  769. loaded_from:=nil;
  770. flags:=0;
  771. crc:=0;
  772. unitcount:=1;
  773. end;
  774. constructor tmodule.init(const s:string;_is_unit:boolean);
  775. var
  776. p : dirstr;
  777. n : namestr;
  778. e : extstr;
  779. begin
  780. FSplit(s,p,n,e);
  781. { Programs have the name program to don't conflict with dup id's }
  782. if _is_unit then
  783. modulename:=stringdup(Upper(n))
  784. else
  785. modulename:=stringdup('PROGRAM');
  786. mainsource:=stringdup(s);
  787. ppufilename:=nil;
  788. objfilename:=nil;
  789. asmfilename:=nil;
  790. staticlibfilename:=nil;
  791. sharedlibfilename:=nil;
  792. exefilename:=nil;
  793. { Dos has the famous 8.3 limit :( }
  794. {$ifdef tp}
  795. asmprefix:=stringdup(FixFileName('as'));
  796. {$else}
  797. {$ifdef go32v2}
  798. asmprefix:=stringdup(FixFileName('as'));
  799. {$else}
  800. asmprefix:=stringdup(FixFileName(n));
  801. {$endif}
  802. {$endif tp}
  803. path:=nil;
  804. setfilename(p+n,true);
  805. used_units.init;
  806. new(sourcefiles,init);
  807. linkofiles.init;
  808. linkstaticlibs.init;
  809. linksharedlibs.init;
  810. ppufile:=nil;
  811. scanner:=nil;
  812. map:=nil;
  813. globalsymtable:=nil;
  814. localsymtable:=nil;
  815. loaded_from:=nil;
  816. flags:=0;
  817. crc:=0;
  818. unitcount:=1;
  819. inc(global_unit_count);
  820. unit_index:=global_unit_count;
  821. do_assemble:=false;
  822. do_compile:=false;
  823. sources_avail:=true;
  824. compiled:=false;
  825. in_second_compile:=false;
  826. in_implementation:=false;
  827. in_global:=true;
  828. is_unit:=_is_unit;
  829. uses_imports:=false;
  830. imports:=new(plinkedlist,init);
  831. { search the PPU file if it is an unit }
  832. if is_unit then
  833. begin
  834. if (not search_unit(modulename^)) and (length(modulename^)>8) then
  835. search_unit(copy(modulename^,1,8));
  836. end;
  837. end;
  838. destructor tmodule.done;
  839. begin
  840. if assigned(map) then
  841. dispose(map);
  842. if assigned(ppufile) then
  843. dispose(ppufile,done);
  844. if assigned(imports) then
  845. dispose(imports,done);
  846. {$ifndef VER0_99_8}
  847. if assigned(scanner) then
  848. pscannerfile(scanner)^.invalid:=true;
  849. {$endif}
  850. if assigned(sourcefiles) then
  851. dispose(sourcefiles,done);
  852. used_units.done;
  853. linkofiles.done;
  854. linkstaticlibs.done;
  855. linksharedlibs.done;
  856. stringdispose(objfilename);
  857. stringdispose(asmfilename);
  858. stringdispose(ppufilename);
  859. stringdispose(staticlibfilename);
  860. stringdispose(sharedlibfilename);
  861. stringdispose(exefilename);
  862. stringdispose(path);
  863. stringdispose(modulename);
  864. stringdispose(mainsource);
  865. stringdispose(asmprefix);
  866. {$ifndef VER0_99_8}
  867. if assigned(globalsymtable) then
  868. dispose(punitsymtable(globalsymtable),done);
  869. if assigned(localsymtable) then
  870. dispose(punitsymtable(localsymtable),done);
  871. {$endif}
  872. inherited done;
  873. end;
  874. {****************************************************************************
  875. TUSED_UNIT
  876. ****************************************************************************}
  877. constructor tused_unit.init(_u : pmodule;intface:boolean);
  878. begin
  879. u:=_u;
  880. in_interface:=intface;
  881. in_uses:=false;
  882. is_stab_written:=false;
  883. loaded:=true;
  884. name:=stringdup(_u^.modulename^);
  885. checksum:=_u^.crc;
  886. unitid:=0;
  887. end;
  888. constructor tused_unit.init_to_load(const n:string;c:longint;intface:boolean);
  889. begin
  890. u:=nil;
  891. in_interface:=intface;
  892. in_uses:=false;
  893. is_stab_written:=false;
  894. loaded:=false;
  895. name:=stringdup(n);
  896. checksum:=c;
  897. unitid:=0;
  898. end;
  899. destructor tused_unit.done;
  900. begin
  901. stringdispose(name);
  902. inherited done;
  903. end;
  904. end.
  905. {
  906. $Log$
  907. Revision 1.57 1998-10-09 16:36:03 pierre
  908. * some memory leaks specific to usebrowser define fixed
  909. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  910. Revision 1.56 1998/10/09 08:56:26 pierre
  911. * several memory leaks fixed
  912. Revision 1.55 1998/10/08 23:28:54 peter
  913. * -vu shows unit info, -vt shows tried/used files
  914. Revision 1.54 1998/10/08 17:17:19 pierre
  915. * current_module old scanner tagged as invalid if unit is recompiled
  916. + added ppheap for better info on tracegetmem of heaptrc
  917. (adds line column and file index)
  918. * several memory leaks removed ith help of heaptrc !!
  919. Revision 1.53 1998/10/08 13:48:43 peter
  920. * fixed memory leaks for do nothing source
  921. * fixed unit interdependency
  922. Revision 1.52 1998/10/06 22:09:48 peter
  923. * fixed for compiling with 0.99.8 due circular units
  924. Revision 1.51 1998/10/06 17:16:47 pierre
  925. * some memory leaks fixed (thanks to Peter for heaptrc !)
  926. Revision 1.50 1998/09/30 16:43:34 peter
  927. * fixed unit interdependency with circular uses
  928. Revision 1.49 1998/09/28 16:57:20 pierre
  929. * changed all length(p^.value_str^) into str_length(p)
  930. to get it work with and without ansistrings
  931. * changed sourcefiles field of tmodule to a pointer
  932. Revision 1.48 1998/09/24 23:46:34 peter
  933. + outputdir support
  934. Revision 1.47 1998/09/22 17:13:43 pierre
  935. + browsing updated and developed
  936. records and objects fields are also stored
  937. Revision 1.46 1998/09/21 08:45:10 pierre
  938. + added vmt_offset in tobjectdef.write for fututre use
  939. (first steps to have objects without vmt if no virtual !!)
  940. + added fpu_used field for tabstractprocdef :
  941. sets this level to 2 if the functions return with value in FPU
  942. (is then set to correct value at parsing of implementation)
  943. THIS MIGHT refuse some code with FPU expression too complex
  944. that were accepted before and even in some cases
  945. that don't overflow in fact
  946. ( like if f : float; is a forward that finally in implementation
  947. only uses one fpu register !!)
  948. Nevertheless I think that it will improve security on
  949. FPU operations !!
  950. * most other changes only for UseBrowser code
  951. (added symtable references for record and objects)
  952. local switch for refs to args and local of each function
  953. (static symtable still missing)
  954. UseBrowser still not stable and probably broken by
  955. the definition hash array !!
  956. Revision 1.45 1998/09/18 09:58:51 peter
  957. * -s doesn't require the .o to be available, this allows compiling of
  958. everything on other platforms (profiling the windows.pp loading ;)
  959. Revision 1.44 1998/09/10 13:51:32 peter
  960. * tp compiler also uses 'as' as asmprefix
  961. Revision 1.43 1998/09/03 17:08:45 pierre
  962. * better lines for stabs
  963. (no scroll back to if before else part
  964. no return to case line at jump outside case)
  965. + source lines also if not in order
  966. Revision 1.42 1998/09/03 11:24:00 peter
  967. * moved more inputfile things from tscannerfile to tinputfile
  968. * changed ifdef Sourceline to cs_asm_source
  969. Revision 1.41 1998/08/26 15:35:30 peter
  970. * fixed scannerfiles for macros
  971. + $I %<environment>%
  972. Revision 1.40 1998/08/26 10:08:48 peter
  973. * fixed problem with libprefix at the wrong place
  974. * fixed lib generation with smartlinking and no -CS used
  975. Revision 1.39 1998/08/25 16:44:16 pierre
  976. * openppu was true even if the object file is missing
  977. this lead to trying to open a filename without extension
  978. and prevented the 'make cycle' to work for win32
  979. Revision 1.38 1998/08/19 10:06:12 peter
  980. * fixed filenames and removedir which supports slash at the end
  981. Revision 1.37 1998/08/18 20:52:19 peter
  982. * renamed in_main to in_global which is more logical
  983. Revision 1.36 1998/08/17 10:10:07 peter
  984. - removed OLDPPU
  985. Revision 1.35 1998/08/17 09:17:44 peter
  986. * static/shared linking updates
  987. Revision 1.34 1998/08/14 21:56:31 peter
  988. * setting the outputfile using -o works now to create static libs
  989. Revision 1.33 1998/08/11 14:09:08 peter
  990. * fixed some messages and smaller msgtxt.inc
  991. Revision 1.32 1998/08/10 14:49:58 peter
  992. + localswitches, moduleswitches, globalswitches splitting
  993. Revision 1.31 1998/07/14 14:46:48 peter
  994. * released NEWINPUT
  995. Revision 1.30 1998/07/07 11:19:55 peter
  996. + NEWINPUT for a better inputfile and scanner object
  997. Revision 1.29 1998/06/25 10:51:00 pierre
  998. * removed a remaining ifndef NEWPPU
  999. replaced by ifdef OLDPPU
  1000. * added uf_finalize to ppu unit
  1001. Revision 1.28 1998/06/25 08:48:12 florian
  1002. * first version of rtti support
  1003. Revision 1.27 1998/06/24 14:48:34 peter
  1004. * ifdef newppu -> ifndef oldppu
  1005. Revision 1.26 1998/06/17 14:36:19 peter
  1006. * forgot an $ifndef OLDPPU :(
  1007. Revision 1.25 1998/06/17 14:10:11 peter
  1008. * small os2 fixes
  1009. * fixed interdependent units with newppu (remake3 under linux works now)
  1010. Revision 1.24 1998/06/16 08:56:20 peter
  1011. + targetcpu
  1012. * cleaner pmodules for newppu
  1013. Revision 1.23 1998/06/15 14:44:36 daniel
  1014. * BP updates.
  1015. Revision 1.22 1998/06/14 18:25:41 peter
  1016. * small fix with crc in newppu
  1017. Revision 1.21 1998/06/13 00:10:05 peter
  1018. * working browser and newppu
  1019. * some small fixes against crashes which occured in bp7 (but not in
  1020. fpc?!)
  1021. Revision 1.20 1998/06/12 14:50:48 peter
  1022. * removed the tree dependency to types.pas
  1023. * long_fil.pas support (not fully tested yet)
  1024. Revision 1.19 1998/06/12 10:32:26 pierre
  1025. * column problem hopefully solved
  1026. + C vars declaration changed
  1027. Revision 1.18 1998/06/11 13:58:07 peter
  1028. * small fix to let newppu compile
  1029. Revision 1.17 1998/06/09 16:01:40 pierre
  1030. + added procedure directive parsing for procvars
  1031. (accepted are popstack cdecl and pascal)
  1032. + added C vars with the following syntax
  1033. var C calias 'true_c_name';(can be followed by external)
  1034. reason is that you must add the Cprefix
  1035. which is target dependent
  1036. Revision 1.16 1998/06/04 10:42:19 pierre
  1037. * small bug fix in load_ppu or openppu
  1038. Revision 1.15 1998/05/28 14:37:53 peter
  1039. * default programname is PROGRAM (like TP7) to avoid dup id's
  1040. Revision 1.14 1998/05/27 19:45:02 peter
  1041. * symtable.pas splitted into includefiles
  1042. * symtable adapted for $ifndef OLDPPU
  1043. Revision 1.13 1998/05/23 01:21:05 peter
  1044. + aktasmmode, aktoptprocessor, aktoutputformat
  1045. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1046. + $LIBNAME to set the library name where the unit will be put in
  1047. * splitted cgi386 a bit (codeseg to large for bp7)
  1048. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1049. Revision 1.12 1998/05/20 09:42:33 pierre
  1050. + UseTokenInfo now default
  1051. * unit in interface uses and implementation uses gives error now
  1052. * only one error for unknown symbol (uses lastsymknown boolean)
  1053. the problem came from the label code !
  1054. + first inlined procedures and function work
  1055. (warning there might be allowed cases were the result is still wrong !!)
  1056. * UseBrower updated gives a global list of all position of all used symbols
  1057. with switch -gb
  1058. Revision 1.11 1998/05/12 10:46:59 peter
  1059. * moved printstatus to verb_def
  1060. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1061. prefix like error: warning: and is included in V_Default
  1062. * fixed some messages
  1063. * first time parameter scan is only for -v and -T
  1064. - removed old style messages
  1065. Revision 1.10 1998/05/11 13:07:53 peter
  1066. + $ifndef OLDPPU for the new ppuformat
  1067. + $define GDB not longer required
  1068. * removed all warnings and stripped some log comments
  1069. * no findfirst/findnext anymore to remove smartlink *.o files
  1070. Revision 1.9 1998/05/06 15:04:20 pierre
  1071. + when trying to find source files of a ppufile
  1072. check the includepathlist for included files
  1073. the main file must still be in the same directory
  1074. Revision 1.8 1998/05/04 17:54:25 peter
  1075. + smartlinking works (only case jumptable left todo)
  1076. * redesign of systems.pas to support assemblers and linkers
  1077. + Unitname is now also in the PPU-file, increased version to 14
  1078. Revision 1.7 1998/05/01 16:38:44 florian
  1079. * handling of private and protected fixed
  1080. + change_keywords_to_tp implemented to remove
  1081. keywords which aren't supported by tp
  1082. * break and continue are now symbols of the system unit
  1083. + widestring, longstring and ansistring type released
  1084. Revision 1.6 1998/05/01 07:43:53 florian
  1085. + basics for rtti implemented
  1086. + switch $m (generate rtti for published sections)
  1087. Revision 1.5 1998/04/30 15:59:40 pierre
  1088. * GDB works again better :
  1089. correct type info in one pass
  1090. + UseTokenInfo for better source position
  1091. * fixed one remaining bug in scanner for line counts
  1092. * several little fixes
  1093. Revision 1.4 1998/04/29 10:33:52 pierre
  1094. + added some code for ansistring (not complete nor working yet)
  1095. * corrected operator overloading
  1096. * corrected nasm output
  1097. + started inline procedures
  1098. + added starstarn : use ** for exponentiation (^ gave problems)
  1099. + started UseTokenInfo cond to get accurate positions
  1100. Revision 1.3 1998/04/27 23:10:28 peter
  1101. + new scanner
  1102. * $makelib -> if smartlink
  1103. * small filename fixes pmodule.setfilename
  1104. * moved import from files.pas -> import.pas
  1105. Revision 1.2 1998/04/21 10:16:47 peter
  1106. * patches from strasbourg
  1107. * objects is not used anymore in the fpc compiled version
  1108. }