1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Debug
; use Debug
;
33 with Output
; use Output
;
34 with Unchecked_Conversion
;
36 package body Tree_IO
is
37 Debug_Flag_Tree
: Boolean := False;
38 -- Debug flag for debug output from tree read/write
40 -------------------------------------------
41 -- Compression Scheme Used for Tree File --
42 -------------------------------------------
44 -- We don't just write the data directly, but instead do a mild form
45 -- of compression, since we expect lots of compressible zeroes and
46 -- blanks. The compression scheme is as follows:
48 -- 00nnnnnn followed by nnnnnn bytes (non compressed data)
49 -- 01nnnnnn indicates nnnnnn binary zero bytes
50 -- 10nnnnnn indicates nnnnnn ASCII space bytes
51 -- 11nnnnnn bbbbbbbb indicates nnnnnnnn occurrences of byte bbbbbbbb
53 -- Since we expect many zeroes in trees, and many spaces in sources,
54 -- this compression should be reasonably efficient. We can put in
55 -- something better later on.
57 -- Note that this compression applies to the Write_Tree_Data and
58 -- Read_Tree_Data calls, not to the calls to read and write single
59 -- scalar values, which are written in memory format without any
62 C_Noncomp
: constant := 2#
00_000000#
;
63 C_Zeros
: constant := 2#
01_000000#
;
64 C_Spaces
: constant := 2#
10_000000#
;
65 C_Repeat
: constant := 2#
11_000000#
;
66 -- Codes for compression sequences
68 Max_Count
: constant := 63;
69 -- Maximum data length for one compression sequence
71 -- The above compression scheme applies only to data written with the
72 -- Tree_Write routine and read with Tree_Read. Data written using the
73 -- Tree_Write_Char or Tree_Write_Int routines and read using the
74 -- corresponding input routines is not compressed.
76 type Int_Bytes
is array (1 .. 4) of Byte
;
77 for Int_Bytes
'Size use 32;
79 function To_Int_Bytes
is new Unchecked_Conversion
(Int
, Int_Bytes
);
80 function To_Int
is new Unchecked_Conversion
(Int_Bytes
, Int
);
82 ----------------------
83 -- Global Variables --
84 ----------------------
86 Tree_FD
: File_Descriptor
;
87 -- File descriptor for tree
89 Buflen
: constant Int
:= 8_192
;
90 -- Length of buffer for read and write file data
92 Buf
: array (Pos
range 1 .. Buflen
) of Byte
;
93 -- Read/write file data buffer
96 -- Number of bytes read/written from/to buffer
99 -- Total number of bytes in input buffer containing valid data. Used only
100 -- for input operations. There is data left to be processed in the buffer
101 -- if Buft > Bufn. A value of zero for Buft means that the buffer is empty.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Read_Buffer
;
108 -- Reads data into buffer, setting Bufn appropriately
110 function Read_Byte
return Byte
;
111 pragma Inline
(Read_Byte
);
112 -- Returns next byte from input file, raises Tree_Format_Error if none left
114 procedure Write_Buffer
;
115 -- Writes out current buffer contents
117 procedure Write_Byte
(B
: Byte
);
118 pragma Inline
(Write_Byte
);
119 -- Write one byte to output buffer, checking for buffer-full condition
125 procedure Read_Buffer
is
127 Buft
:= Int
(Read
(Tree_FD
, Buf
(1)'Address, Integer (Buflen
)));
130 raise Tree_Format_Error
;
140 function Read_Byte
return Byte
is
154 procedure Tree_Read_Bool
(B
: out Boolean) is
156 B
:= Boolean'Val (Read_Byte
);
158 if Debug_Flag_Tree
then
173 procedure Tree_Read_Char
(C
: out Character) is
175 C
:= Character'Val (Read_Byte
);
177 if Debug_Flag_Tree
then
178 Write_Str
("==> transmitting Character = ");
188 procedure Tree_Read_Data
(Addr
: Address
; Length
: Int
) is
190 type S
is array (Pos
) of Byte
;
191 -- This is a big array, for which we have to suppress the warning
193 type SP
is access all S
;
195 function To_SP
is new Unchecked_Conversion
(Address
, SP
);
197 Data
: constant SP
:= To_SP
(Addr
);
198 -- Data buffer to be read as an indexable array of bytes
201 -- Pointer to next byte of data buffer to be read into
208 if Debug_Flag_Tree
then
209 Write_Str
("==> transmitting ");
211 Write_Str
(" data bytes");
215 -- Verify data length
220 Write_Str
("==> transmitting, expected ");
222 Write_Str
(" bytes, found length = ");
225 raise Tree_Format_Error
;
230 while OP
<= Length
loop
232 -- Get compression control character
235 C
:= B
and 2#
00_111111#
;
236 B
:= B
and 2#
11_000000#
;
240 if B
= C_Noncomp
then
241 if Debug_Flag_Tree
then
242 Write_Str
("==> uncompressed: ");
244 Write_Str
(", starting at ");
250 Data
(OP
) := Read_Byte
;
256 elsif B
= C_Zeros
then
257 if Debug_Flag_Tree
then
258 Write_Str
("==> zeroes: ");
260 Write_Str
(", starting at ");
272 elsif B
= C_Spaces
then
273 if Debug_Flag_Tree
then
274 Write_Str
("==> spaces: ");
276 Write_Str
(", starting at ");
282 Data
(OP
) := Character'Pos (' ');
286 -- Specified repeated character
291 if Debug_Flag_Tree
then
292 Write_Str
("==> other char: ");
297 Write_Str
(", starting at ");
309 -- At end of loop, data item must be exactly filled
311 if OP
/= Length
+ 1 then
312 raise Tree_Format_Error
;
317 --------------------------
318 -- Tree_Read_Initialize --
319 --------------------------
321 procedure Tree_Read_Initialize
(Desc
: File_Descriptor
) is
326 Debug_Flag_Tree
:= Debug_Flag_5
;
327 end Tree_Read_Initialize
;
333 procedure Tree_Read_Int
(N
: out Int
) is
338 N_Bytes
(J
) := Read_Byte
;
341 N
:= To_Int
(N_Bytes
);
343 if Debug_Flag_Tree
then
344 Write_Str
("==> transmitting Int = ");
354 procedure Tree_Read_Str
(S
: out String_Ptr
) is
359 S
:= new String (1 .. Natural (N
));
360 Tree_Read_Data
(S
.all (1)'Address, N
);
363 -------------------------
364 -- Tree_Read_Terminate --
365 -------------------------
367 procedure Tree_Read_Terminate
is
369 -- Must be at end of input buffer, so we should get Tree_Format_Error
370 -- if we try to read one more byte, if not, we have a format error.
374 pragma Warnings
(Off
, B
);
380 when Tree_Format_Error
=> return;
383 raise Tree_Format_Error
;
384 end Tree_Read_Terminate
;
386 ---------------------
387 -- Tree_Write_Bool --
388 ---------------------
390 procedure Tree_Write_Bool
(B
: Boolean) is
392 if Debug_Flag_Tree
then
393 Write_Str
("==> transmitting Boolean = ");
404 Write_Byte
(Boolean'Pos (B
));
407 ---------------------
408 -- Tree_Write_Char --
409 ---------------------
411 procedure Tree_Write_Char
(C
: Character) is
413 if Debug_Flag_Tree
then
414 Write_Str
("==> transmitting Character = ");
419 Write_Byte
(Character'Pos (C
));
422 ---------------------
423 -- Tree_Write_Data --
424 ---------------------
426 procedure Tree_Write_Data
(Addr
: Address
; Length
: Int
) is
428 type S
is array (Pos
) of Byte
;
429 -- This is a big array, for which we have to suppress the warning
431 type SP
is access all S
;
433 function To_SP
is new Unchecked_Conversion
(Address
, SP
);
435 Data
: constant SP
:= To_SP
(Addr
);
436 -- Pointer to data to be written, converted to array type
439 -- Input buffer pointer, next byte to be processed
441 NC
: Nat
range 0 .. Max_Count
:= 0;
442 -- Number of bytes of non-compressible sequence
446 procedure Write_Non_Compressed_Sequence
;
447 -- Output currently collected sequence of non-compressible data
449 -----------------------------------
450 -- Write_Non_Compressed_Sequence --
451 -----------------------------------
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