javaclass

Changeset

156:3270cc49c46f
2005-01-23 Paul Boddie raw files shortlog changelog graph Added a get_bytecode method to the bytecode writer which provides a list of byte values (including finalised "lazy" values). Added missing instruction support to the disassembler program class. Added a hack to avoid issues with apparently strange exception tables.
javaclass/bytecode.py (file)
     1.1 --- a/javaclass/bytecode.py	Sun Jan 23 01:59:37 2005 +0100
     1.2 +++ b/javaclass/bytecode.py	Sun Jan 23 21:32:26 2005 +0100
     1.3 @@ -8,11 +8,10 @@
     1.4  """
     1.5  
     1.6  import classfile
     1.7 -from dis import cmp_op # for access to Python bytecode values and operators
     1.8 +from dis import cmp_op, opname # for access to Python bytecode values and operators
     1.9  try:
    1.10      from dis import opmap
    1.11  except ImportError:
    1.12 -    from dis import opname
    1.13      opmap = {}
    1.14      for i in range(0, len(opname)):
    1.15          opmap[opname[i]] = i
    1.16 @@ -70,9 +69,9 @@
    1.17          # A list of external names.
    1.18          self.external_names = []
    1.19  
    1.20 -    def get_output(self):
    1.21 -
    1.22 -        "Return the output of the writer as a string."
    1.23 +    def get_bytecodes(self):
    1.24 +
    1.25 +        "Return the list of bytecodes written to the writer."
    1.26  
    1.27          output = []
    1.28          for element in self.output:
    1.29 @@ -80,6 +79,15 @@
    1.30                  value = element.value
    1.31              else:
    1.32                  value = element
    1.33 +            output.append(value)
    1.34 +        return output
    1.35 +
    1.36 +    def get_output(self):
    1.37 +
    1.38 +        "Return the output of the writer as a string."
    1.39 +
    1.40 +        output = []
    1.41 +        for value in self.get_bytecodes():
    1.42              # NOTE: ValueError gets raised for bad values here.
    1.43              output.append(chr(value))
    1.44          return "".join(output)
    1.45 @@ -730,6 +738,12 @@
    1.46  
    1.47          for exception in reversed_exception_table:
    1.48  
    1.49 +            # NOTE: Strange case with javac from JDK 1.4 but not JDK 1.3:
    1.50 +            # NOTE: start_pc == handler_pc
    1.51 +
    1.52 +            if exception.start_pc == exception.handler_pc:
    1.53 +                continue
    1.54 +
    1.55              # Index start positions.
    1.56  
    1.57              if not exception_block_start.has_key(exception.start_pc):
    1.58 @@ -1079,6 +1093,10 @@
    1.59          print "(start_handler %s)" % exc_name
    1.60      def pop_block(self):
    1.61          print "(pop_block)"
    1.62 +    def load_const(self, const):
    1.63 +        print "(load_const %s)" % const
    1.64 +    def return_value(self):
    1.65 +        print "(return_value)"
    1.66  
    1.67  class BytecodeTranslator(BytecodeReader):
    1.68  
    1.69 @@ -1958,6 +1976,7 @@
    1.70  def disassemble(class_file, method):
    1.71      disassembler = BytecodeDisassembler(class_file)
    1.72      disassembler.process(method, BytecodeDisassemblerProgram())
    1.73 +    return disassembler
    1.74  
    1.75  class ClassTranslator:
    1.76