home *** CD-ROM | disk | FTP | other *** search
/ Hacker / Hacker.iso / HACKER / DECOMP / DECAF / attrcode.adb < prev    next >
Encoding:
Text File  |  1996-09-19  |  4.3 KB  |  134 lines

  1. --
  2. -- Copyright (C) 1996 Ada Resource Association (ARA), Columbus, Ohio.
  3. -- Author: Gilles Demailly
  4. --
  5. --
  6. -- Permission to use, copy, modify, and distribute this software and its
  7. -- documentation for any purpose and without fee is hereby granted,
  8. -- provided that the above copyright and authorship notice appear in all
  9. -- copies and that both that copyright notice and this permission notice
  10. -- appear in supporting documentation.
  11. -- 
  12. -- The ARA makes no representations about the suitability of this software
  13. -- for any purpose.  It is provided "as is" without express
  14. -- or implied warranty.
  15. -- 
  16.  
  17. with Ada.Text_Io;
  18. with ByteCode;
  19. with Unsigned_16_Io;
  20.  
  21. package body Attribute.Code is
  22.  
  23.    use Byte_Utilities;
  24.      
  25.    procedure Decode (This      : access Code_Attribute;
  26.                      From_File : in Byte_Utilities.File_Type;
  27.                      Context   : in CP.Acc_CP_Infos) is
  28.    begin
  29.  
  30.       -- reads stack size, number of local variables, code length
  31.       Read (From_File, This.Max_Stack);
  32.       Read (From_File, This.Max_Locals);
  33.       Read (From_File, This.Code_Length);
  34.  
  35.       -- if the code length is not null then reads it
  36.       if This.Code_Length > 0 then
  37.  
  38.          -- code allocation
  39.          This.Code := new Bytes (1..This.Code_Length);
  40.  
  41.          -- reads the code
  42.          for I in 1..This.Code_Length loop
  43.             Read (From_File, This.Code (I));
  44.          end loop;
  45.       else
  46.          This.Code := null;
  47.       end if;
  48.  
  49.       -- reads the number of exception handlers
  50.       Read (From_File, This.Exceptions_Count);
  51.       if This.Exceptions_Count > 0 then
  52.  
  53.          -- exception handler table allocation
  54.          This.Exception_Table := new Exception_Infos (1..This.Exceptions_Count);
  55.  
  56.          -- reads the exception handler table
  57.          for I in 1..This.Exceptions_Count loop
  58.             Read (From_File, This.Exception_Table (I).Start_Pc);
  59.             Read (From_File, This.Exception_Table (I).End_Pc);
  60.             Read (From_File, This.Exception_Table (I).Handler_Pc);
  61.             Read (From_File, This.Exception_Table (I).Catch_Type);
  62.          end loop;
  63.       else
  64.          This.Exception_Table := null;
  65.       end if;
  66.       
  67.       -- reads the number of attributes attached to the code
  68.       Read (From_File, This.Attributes_Count);
  69.       if This.Attributes_Count > 0 then
  70.  
  71.          -- attributes array allocations
  72.          This.Attributes := new Attributes (1 .. This.Attributes_Count);
  73.  
  74.          -- reads the attributes
  75.          for J in 1 .. This.Attributes_Count loop
  76.             This.Attributes (J) := Read_Attribute (From_File, Context);
  77.          end loop;
  78.       else
  79.          This.Attributes := null;
  80.       end if;
  81.  
  82.    end Decode;
  83.  
  84.    procedure Display (This    : access Code_Attribute;
  85.                       Context : in CP.Acc_CP_Infos) is
  86.       Index : Unsigned_32 := 1;
  87.    begin
  88.       -- displays stack size and local variables number
  89.       Ada.Text_Io.Put_Line
  90.          ("      // Stack = " &
  91.           Unsigned_16'Image (This.Max_Stack) &
  92.           ", Locals = " &
  93.           Unsigned_16'Image (This.Max_Locals));
  94.  
  95.       -- displays the method instructions
  96.       if This.Code_Length > 0 then
  97.          while Index <= This.Code_Length loop
  98.             ByteCode.Display
  99.                (Index   => Index,
  100.                 Bytes   => This.Code,
  101.                 Context => Context);
  102.          end loop;
  103.       end if;
  104.  
  105.       -- displays the exceptions handler table
  106.       if This.Exceptions_Count > 0 then
  107.          Ada.Text_Io.Put_Line
  108.             ("      Exception table:");
  109.          Ada.Text_Io.Put_Line
  110.             ("         from   to  target type");
  111.          for I in 1..This.Exceptions_Count loop
  112.             Unsigned_16_Io.Put
  113.                (Item => This.Exception_Table (I).Start_Pc, Width => 12);
  114.             Unsigned_16_Io.Put
  115.                (Item => This.Exception_Table (I).End_Pc, Width => 6);
  116.             Unsigned_16_Io.Put
  117.                (Item => This.Exception_Table (I).Handler_Pc, Width => 6);
  118.             if This.Exception_Table (I).Catch_Type = 0 then
  119.                Ada.Text_Io.Put ("   any");
  120.             else
  121.                Ada.Text_Io.Put
  122.                   ("   " &
  123.                    CP.Java_Decoded_String
  124.                       (Context (This.Exception_Table (I).Catch_Type),
  125.                        Context,
  126.                        CP.Class_Name));
  127.             end if;
  128.             Ada.Text_Io.New_Line;
  129.          end loop;
  130.       end if;
  131.    end Display;
  132.  
  133. end Attribute.Code;
  134.