1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Debug; use Debug;
37 with Output; use Output;
38 with Unchecked_Conversion;
40 package body Tree_IO is
41 Debug_Flag_Tree : Boolean := False;
42 -- Debug flag for debug output from tree read/write
44 -------------------------------------------
45 -- Compression Scheme Used for Tree File --
46 -------------------------------------------
48 -- We don't just write the data directly, but instead do a mild form
49 -- of compression, since we expect lots of compressible zeroes and
50 -- blanks. The compression scheme is as follows:
52 -- 00nnnnnn followed by nnnnnn bytes (non compressed data)
53 -- 01nnnnnn indicates nnnnnn binary zero bytes
54 -- 10nnnnnn indicates nnnnnn ASCII space bytes
55 -- 11nnnnnn bbbbbbbb indicates nnnnnnnn occurrences of byte bbbbbbbb
57 -- Since we expect many zeroes in trees, and many spaces in sources,
58 -- this compression should be reasonably efficient. We can put in
59 -- something better later on.
61 -- Note that this compression applies to the Write_Tree_Data and
62 -- Read_Tree_Data calls, not to the calls to read and write single
63 -- scalar values, which are written in memory format without any
66 C_Noncomp : constant := 2#00_000000#;
67 C_Zeros : constant := 2#01_000000#;
68 C_Spaces : constant := 2#10_000000#;
69 C_Repeat : constant := 2#11_000000#;
70 -- Codes for compression sequences
72 Max_Count : constant := 63;
73 -- Maximum data length for one compression sequence
75 Max_Comp : constant := Max_Count + 1;
76 -- Maximum length of one compression sequence
78 -- The above compression scheme applies only to data written with the
79 -- Tree_Write routine and read with Tree_Read. Data written using the
80 -- Tree_Write_Char or Tree_Write_Int routines and read using the
81 -- corresponding input routines is not compressed.
83 type Int_Bytes is array (1 .. 4) of Byte;
84 for Int_Bytes'Size use 32;
86 function To_Int_Bytes is new Unchecked_Conversion (Int, Int_Bytes);
87 function To_Int is new Unchecked_Conversion (Int_Bytes, Int);
89 ----------------------
90 -- Global Variables --
91 ----------------------
93 Tree_FD : File_Descriptor;
94 -- File descriptor for tree
96 Buflen : constant Int := 8_192;
97 -- Length of buffer for read and write file data
99 Buf : array (Pos range 1 .. Buflen) of Byte;
100 -- Read/write file data buffer
103 -- Number of bytes read/written from/to buffer
106 -- Total number of bytes in input buffer containing valid data. Used only
107 -- for input operations. There is data left to be processed in the buffer
108 -- if Buft > Bufn. A value of zero for Buft means that the buffer is empty.
110 -----------------------
111 -- Local Subprograms --
112 -----------------------
114 procedure Read_Buffer;
115 -- Reads data into buffer, setting Bufe appropriately
117 function Read_Byte return Byte;
118 pragma Inline (Read_Byte);
119 -- Returns next byte from input file, raises Tree_Format_Error if none left
121 procedure Write_Buffer;
122 -- Writes out current buffer contents
124 procedure Write_Byte (B : Byte);
125 pragma Inline (Write_Byte);
126 -- Write one byte to output buffer, checking for buffer-full condition
132 procedure Read_Buffer is
134 Buft := Int (Read (Tree_FD, Buf (1)'Address, Integer (Buflen)));
137 raise Tree_Format_Error;
147 function Read_Byte return Byte is
161 procedure Tree_Read_Bool (B : out Boolean) is
163 B := Boolean'Val (Read_Byte);
165 if Debug_Flag_Tree then
180 procedure Tree_Read_Char (C : out Character) is
182 C := Character'Val (Read_Byte);
184 if Debug_Flag_Tree then
185 Write_Str ("==> transmitting Character = ");
195 procedure Tree_Read_Data (Addr : Address; Length : Int) is
197 type S is array (Pos) of Byte;
198 -- This is a big array, for which we have to suppress the warning
200 type SP is access all S;
202 function To_SP is new Unchecked_Conversion (Address, SP);
204 Data : constant SP := To_SP (Addr);
205 -- Data buffer to be read as an indexable array of bytes
208 -- Pointer to next byte of data buffer to be read into
215 if Debug_Flag_Tree then
216 Write_Str ("==> transmitting ");
218 Write_Str (" data bytes");
222 -- Verify data length
227 Write_Str ("==> transmitting, expected ");
229 Write_Str (" bytes, found length = ");
232 raise Tree_Format_Error;
237 while OP <= Length loop
239 -- Get compression control character
242 C := B and 2#00_111111#;
243 B := B and 2#11_000000#;
247 if B = C_Noncomp then
248 if Debug_Flag_Tree then
249 Write_Str ("==> uncompressed: ");
251 Write_Str (", starting at ");
257 Data (OP) := Read_Byte;
263 elsif B = C_Zeros then
264 if Debug_Flag_Tree then
265 Write_Str ("==> zeroes: ");
267 Write_Str (", starting at ");
279 elsif B = C_Spaces then
280 if Debug_Flag_Tree then
281 Write_Str ("==> spaces: ");
283 Write_Str (", starting at ");
289 Data (OP) := Character'Pos (' ');
293 -- Specified repeated character
298 if Debug_Flag_Tree then
299 Write_Str ("==> other char: ");
304 Write_Str (", starting at ");
316 -- At end of loop, data item must be exactly filled
318 if OP /= Length + 1 then
319 raise Tree_Format_Error;
324 --------------------------
325 -- Tree_Read_Initialize --
326 --------------------------
328 procedure Tree_Read_Initialize (Desc : File_Descriptor) is
333 Debug_Flag_Tree := Debug_Flag_5;
334 end Tree_Read_Initialize;
340 procedure Tree_Read_Int (N : out Int) is
345 N_Bytes (J) := Read_Byte;
348 N := To_Int (N_Bytes);
350 if Debug_Flag_Tree then
351 Write_Str ("==> transmitting Int = ");
361 procedure Tree_Read_Str (S : out String_Ptr) is
366 S := new String (1 .. Natural (N));
367 Tree_Read_Data (S.all (1)'Address, N);
370 -------------------------
371 -- Tree_Read_Terminate --
372 -------------------------
374 procedure Tree_Read_Terminate is
376 -- Must be at end of input buffer, so we should get Tree_Format_Error
377 -- if we try to read one more byte, if not, we have a format error.
384 when Tree_Format_Error => return;
387 raise Tree_Format_Error;
388 end Tree_Read_Terminate;
390 ---------------------
391 -- Tree_Write_Bool --
392 ---------------------
394 procedure Tree_Write_Bool (B : Boolean) is
396 if Debug_Flag_Tree then
397 Write_Str ("==> transmitting Boolean = ");
408 Write_Byte (Boolean'Pos (B));
411 ---------------------
412 -- Tree_Write_Char --
413 ---------------------
415 procedure Tree_Write_Char (C : Character) is
417 if Debug_Flag_Tree then
418 Write_Str ("==> transmitting Character = ");
423 Write_Byte (Character'Pos (C));
426 ---------------------
427 -- Tree_Write_Data --
428 ---------------------
430 procedure Tree_Write_Data (Addr : Address; Length : Int) is
432 type S is array (Pos) of Byte;
433 -- This is a big array, for which we have to suppress the warning
435 type SP is access all S;
437 function To_SP is new Unchecked_Conversion (Address, SP);
439 Data : constant SP := To_SP (Addr);
440 -- Pointer to data to be written, converted to array type
443 -- Input buffer pointer, next byte to be processed
445 NC : Nat range 0 .. Max_Count := 0;
446 -- Number of bytes of non-compressible sequence
450 procedure Write_Non_Compressed_Sequence;
451 -- Output currently collected sequence of non-compressible data
453 procedure Write_Non_Compressed_Sequence is
456 Write_Byte (C_Noncomp + Byte (NC));
458 if Debug_Flag_Tree then
459 Write_Str ("==> uncompressed: ");
461 Write_Str (", starting at ");
466 for J in reverse 1 .. NC loop
467 Write_Byte (Data (IP - J));
472 end Write_Non_Compressed_Sequence;
474 -- Start of processing for Tree_Write_Data
477 if Debug_Flag_Tree then
478 Write_Str ("==> transmitting ");
480 Write_Str (" data bytes");
484 -- We write the count at the start, so that we can check it on
485 -- the corresponding read to make sure that reads and writes match
487 Tree_Write_Int (Length);
490 -- IP is index of next input character
491 -- NC is number of non-compressible bytes saved up
494 -- If input is completely processed, then we are all done
497 Write_Non_Compressed_Sequence;
501 -- Test for compressible sequence, must be at least three identical
502 -- bytes in a row to be worthwhile compressing.
505 and then Data (IP) = Data (IP + 1)
506 and then Data (IP) = Data (IP + 2)
508 Write_Non_Compressed_Sequence;
510 -- Count length of new compression sequence
516 and then Data (IP) = Data (IP - 1)
517 and then C < Max_Count
523 -- Output compression sequence
525 if Data (IP - 1) = 0 then
526 if Debug_Flag_Tree then
527 Write_Str ("==> zeroes: ");
529 Write_Str (", starting at ");
530 Write_Int (IP - Int (C));
534 Write_Byte (C_Zeros + C);
536 elsif Data (IP - 1) = Character'Pos (' ') then
537 if Debug_Flag_Tree then
538 Write_Str ("==> spaces: ");
540 Write_Str (", starting at ");
541 Write_Int (IP - Int (C));
545 Write_Byte (C_Spaces + C);
548 if Debug_Flag_Tree then
549 Write_Str ("==> other char: ");
552 Write_Int (Int (Data (IP - 1)));
554 Write_Str (", starting at ");
555 Write_Int (IP - Int (C));
559 Write_Byte (C_Repeat + C);
560 Write_Byte (Data (IP - 1));
563 -- No compression possible here
566 -- Output non-compressed sequence if at maximum length
568 if NC = Max_Count then
569 Write_Non_Compressed_Sequence;
579 ---------------------------
580 -- Tree_Write_Initialize --
581 ---------------------------
583 procedure Tree_Write_Initialize (Desc : File_Descriptor) is
588 Debug_Flag_Tree := Debug_Flag_5;
589 end Tree_Write_Initialize;
595 procedure Tree_Write_Int (N : Int) is
596 N_Bytes : constant Int_Bytes := To_Int_Bytes (N);
599 if Debug_Flag_Tree then
600 Write_Str ("==> transmitting Int = ");
606 Write_Byte (N_Bytes (J));
614 procedure Tree_Write_Str (S : String_Ptr) is
616 Tree_Write_Int (S'Length);
617 Tree_Write_Data (S (1)'Address, S'Length);
620 --------------------------
621 -- Tree_Write_Terminate --
622 --------------------------
624 procedure Tree_Write_Terminate is
629 end Tree_Write_Terminate;
635 procedure Write_Buffer is
637 if Integer (Bufn) = Write (Tree_FD, Buf'Address, Integer (Bufn)) then
642 Write_Str ("fatal error: disk full");
651 procedure Write_Byte (B : Byte) is
656 if Bufn = Buflen then