scale.j2s
Class Java2Scribble

java.lang.Object
  extended by scale.j2s.Java2Scribble

public class Java2Scribble
extends java.lang.Object

This class maintains the global (inter-class) information used while converting Java byte codes to Scribble.

$Id: Java2Scribble.java,v 1.29 2007-10-04 19:58:13 burrill Exp $

Copyright 2008 by the Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.


Field Summary
static int ARRAYTYPE
           
static IntegerType byteType
          The Scale representation for the Java byte type.
static PointerType cCharpType
          The Scale representation for a pointer to the Java char type.
static PointerType charpType
          The Scale representation for a pointer to the Java char type.
static CharacterType charType
          The Scale representation for the Java char type.
static int CLASSTYPE
           
 ProcedureDecl createStringProc
          A procedure that constructs a String from an array of shorts.
static FloatType doubleType
          The Scale representation for the Java double type.
 ProcedureDecl findIntMethodProc
          A procedure that finds the address of an interface method.
 FloatLiteral float0
          The literal for 0.0.
 FloatLiteral float1
          The literal for 1.0.
 FloatLiteral float2
          The literal for 2.0.
static FloatType floatType
          The Scale representation for the Java float type.
 VariableDecl globalExceptionVariable
          The global exception variable.
 ProcedureDecl instanceOfProc
          A procedure that checks if a is an instance of b
 IntLiteral int0
          The literal for 0.
 IntLiteral int1
          The literal for 1.
 IntLiteral int2
          The literal for 2.
 IntLiteral int3
          The literal for 3.
 IntLiteral int4
          The literal for 4.
 IntLiteral int5
          The literal for 5.
static int INTERFACETYPE
           
 IntLiteral intm1
          The literal for -1.
static PointerType intpType
          The Scale representation for a pointer to the Java int type.
static IntegerType intType
          The Scale representation for the Java int type.
static IntegerType longType
          The Scale representation for the Java long type.
 ProcedureDecl lookupExceptionProc
          A procedure that maps an exception to an index into the methods exception table.
 ProcedureDecl makeExceptionProc
          A procedure that creates an exception.
 IntLiteral nil
          The literal for null.
static IntegerType shortType
          The Scale representation for the Java short type.
 Type stringpType
          Of type pointer to the String class.
static Type[] typeMap
          Map from Java type specifier (int) to the Scale type.
static PointerType voidp
          The Scale representation for the unspecified pointer type.
 
Constructor Summary
Java2Scribble()
           
 
Method Summary
 void addTopGlobal(Declaration d)
          Specify that the Declaration is a top level declaration so that it is processed by Clef2C later.
static void cleanup()
          Clean up for profiling statistics.
 void convertClass(ClassStuff cs, CallGraph cg)
           
 ProcedureType createMethodType(Type ct, java.lang.String descriptor, boolean isStatic)
          Return a Clef ProcedureType for the method.
 void defineFunctions()
          Create the Declarations for various runtime procedures needed.
 void defineStructures()
          Each class has a virtual function transfer vector (vtable) associated with it.
 FieldDecl findField(ClassStuff cs, java.lang.String fname)
          Return the named FieldDecl in the structure.
 java.lang.String genMethodName(java.lang.String className, java.lang.String mName, java.lang.String descriptor)
          Return the fully qualified name of a method.
 Accessibility getAccess(int accessFlags)
          Convert from Java method access specifier to Scale access specifier.
 FieldDecl getArrayField(java.lang.String fname)
          Return the named FieldDecl in the array structure.
 Type getArrayHeaderType()
          Return the generic array structure type.
 Type getArrayType(int type)
          Return the array type for the specified Java type.
 ClassStuff getClass(java.lang.String cname)
          Return the class information for the specified class.
 VariableDecl getClassDecl(ClassStuff ecs)
          Return the variable defining the class' CLASSENTRY.
 VariableDecl getClassDecl(java.lang.String name)
          Return the variable defining the class' CLASSENTRY.
 java.util.Enumeration<ClassStuff> getClasses()
          Return an enumeration of all the classes.
 Vector<Type> getClefTypes(java.lang.String descriptor)
          Return a vector of AST Types corresponding the the types specified in the descriptor.
 Declaration getGlobal(java.lang.String name)
          Return the top level Declaration for the specified name.
 VariableDecl getGlobalVar(ClassStuff cs, java.lang.String fname)
          Return the VariableDecl for the static field of a Java class.
 Literal getIntLiteral(int value)
          Return return a Clef Literal with the specified value.
 java.lang.Object getLiteral(ClassFile cf, int index)
          Return the Clef Literal for the specified constant value.
 ProcedureDecl getProcedureDecl(Type ct, java.lang.String name, java.lang.String descriptor, boolean isStatic)
          Create a Clef ProcedureDecl for the specified method.
 java.util.Enumeration<Declaration> getTopDecls()
          Return the special top level Declarations.
 int getTypeSpecifier(Type type)
          Return the Java type specifier for the Scale type.
 VariableDecl getVTableDecl(ClassStuff cs)
          Return the virtual table VariableDecl for the class.
 FieldDecl getVTableField(java.lang.String fname)
          Return the named FieldDecl in the virtual table structure.
 VariableDecl makeExceptionTable(ClassFile cf, java.lang.String methodName, ExceptionEntry[] entries)
          Create the run time exception table for a class' method.
 VariableDecl makeStringInit(java.lang.String s)
          This method creates the Clef representation of a Java String as an array of shorts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASSTYPE

public static final int CLASSTYPE
See Also:
Constant Field Values

INTERFACETYPE

public static final int INTERFACETYPE
See Also:
Constant Field Values

ARRAYTYPE

public static final int ARRAYTYPE
See Also:
Constant Field Values

byteType

public static final IntegerType byteType
The Scale representation for the Java byte type.


shortType

public static final IntegerType shortType
The Scale representation for the Java short type.


intType

public static final IntegerType intType
The Scale representation for the Java int type.


longType

public static final IntegerType longType
The Scale representation for the Java long type.


floatType

public static final FloatType floatType
The Scale representation for the Java float type.


doubleType

public static final FloatType doubleType
The Scale representation for the Java double type.


charType

public static final CharacterType charType
The Scale representation for the Java char type.


charpType

public static final PointerType charpType
The Scale representation for a pointer to the Java char type.


cCharpType

public static final PointerType cCharpType
The Scale representation for a pointer to the Java char type.


intpType

public static final PointerType intpType
The Scale representation for a pointer to the Java int type.


voidp

public static final PointerType voidp
The Scale representation for the unspecified pointer type.


typeMap

public static final Type[] typeMap
Map from Java type specifier (int) to the Scale type.


createStringProc

public ProcedureDecl createStringProc
A procedure that constructs a String from an array of shorts.


stringpType

public Type stringpType
Of type pointer to the String class.


findIntMethodProc

public ProcedureDecl findIntMethodProc
A procedure that finds the address of an interface method.


makeExceptionProc

public ProcedureDecl makeExceptionProc
A procedure that creates an exception.


lookupExceptionProc

public ProcedureDecl lookupExceptionProc
A procedure that maps an exception to an index into the methods exception table.


instanceOfProc

public ProcedureDecl instanceOfProc
A procedure that checks if a is an instance of b


globalExceptionVariable

public VariableDecl globalExceptionVariable
The global exception variable. This variable is checked after each method call. If it is non-null, an exception has occured.


intm1

public final IntLiteral intm1
The literal for -1.


int0

public final IntLiteral int0
The literal for 0.


int1

public final IntLiteral int1
The literal for 1.


int2

public final IntLiteral int2
The literal for 2.


int3

public final IntLiteral int3
The literal for 3.


int4

public final IntLiteral int4
The literal for 4.


int5

public final IntLiteral int5
The literal for 5.


float0

public final FloatLiteral float0
The literal for 0.0.


float1

public final FloatLiteral float1
The literal for 1.0.


float2

public final FloatLiteral float2
The literal for 2.0.


nil

public final IntLiteral nil
The literal for null.

Constructor Detail

Java2Scribble

public Java2Scribble()
Method Detail

convertClass

public void convertClass(ClassStuff cs,
                         CallGraph cg)

getTypeSpecifier

public int getTypeSpecifier(Type type)
Return the Java type specifier for the Scale type. Index 0 is used for all address types.


getClasses

public java.util.Enumeration<ClassStuff> getClasses()
Return an enumeration of all the classes.

See Also:
ClassStuff

getGlobal

public Declaration getGlobal(java.lang.String name)
Return the top level Declaration for the specified name.


getClass

public ClassStuff getClass(java.lang.String cname)
Return the class information for the specified class.

Parameters:
cname - is the name of the class

getAccess

public Accessibility getAccess(int accessFlags)
Convert from Java method access specifier to Scale access specifier.


getGlobalVar

public VariableDecl getGlobalVar(ClassStuff cs,
                                 java.lang.String fname)
Return the VariableDecl for the static field of a Java class.

Parameters:
cs - specifies the class with the static field
fname - is the name of the field

addTopGlobal

public void addTopGlobal(Declaration d)
Specify that the Declaration is a top level declaration so that it is processed by Clef2C later.


getTopDecls

public java.util.Enumeration<Declaration> getTopDecls()
Return the special top level Declarations.


defineStructures

public void defineStructures()
Each class has a virtual function transfer vector (vtable) associated with it. Each class instance has a pointer to this vector. Thus, the instance address can be used to access the transfer vector to obtain the address of a class method. The transfer vector contains addresses only for public or protected, non-static methods.

The structure of the vtable is as follows:

 struct VTABLE {
   const CLASS    *class;          // The class structure for this class
   int             interfaceCount; // The number of interfaces implemented by this class
   INTERFACEENTRY *interfaces;     // A list of the interfaces implemented by this class
   void           *methods[0]      // The addresses of the virtual methods - there is at least one.
 };
 
 Each INTERFACEENTRY records a different interface:
 
 struct INTERFACEENTRY {
   int          hash;             // A hash of the interface name
   const CLASS *class;            // The class structure for this interface
   void        *((methods *)[0]); // A pointer to a table of method addresses
 } ;
 
 The CLASS structure is defined as
 
 struct CLASS {
   const unsigned short *name;
   const struct CLASS   *super;
   byte           kind;
 };
 
Note - all the addresses that occur in the array pointed to by the methods field are also in the VTABLE instance for the class.


defineFunctions

public void defineFunctions()
Create the Declarations for various runtime procedures needed.


getClassDecl

public VariableDecl getClassDecl(java.lang.String name)
Return the variable defining the class' CLASSENTRY.

Parameters:
name - is the name of the class

getClassDecl

public VariableDecl getClassDecl(ClassStuff ecs)
Return the variable defining the class' CLASSENTRY.

Parameters:
ecs - is the ClassStuff for the class

getArrayType

public Type getArrayType(int type)
Return the array type for the specified Java type.

Parameters:
type - is the Java type
See Also:
CodeAttribute

getClefTypes

public Vector<Type> getClefTypes(java.lang.String descriptor)
Return a vector of AST Types corresponding the the types specified in the descriptor.

See Also:
Type

genMethodName

public java.lang.String genMethodName(java.lang.String className,
                                      java.lang.String mName,
                                      java.lang.String descriptor)
Return the fully qualified name of a method.

Parameters:
className - is the name of the method's class
mName - is the name of the method
descriptor - is the Java method descriptor

makeStringInit

public VariableDecl makeStringInit(java.lang.String s)
This method creates the Clef representation of a Java String as an array of shorts. The first element of the array is the length of the string.

Returns:
a Clef VariableDecl for the String

makeExceptionTable

public VariableDecl makeExceptionTable(ClassFile cf,
                                       java.lang.String methodName,
                                       ExceptionEntry[] entries)
Create the run time exception table for a class' method.

Parameters:
cf - is the ClassFile for the class
methodName - is the name of the method
entries - is the table of exception handlers

getLiteral

public java.lang.Object getLiteral(ClassFile cf,
                                   int index)
Return the Clef Literal for the specified constant value.

Parameters:
cf - is the class file containing the constant
index - is the index of the constant in the constant pool

getIntLiteral

public Literal getIntLiteral(int value)
Return return a Clef Literal with the specified value.


createMethodType

public ProcedureType createMethodType(Type ct,
                                      java.lang.String descriptor,
                                      boolean isStatic)
Return a Clef ProcedureType for the method.

Parameters:
ct - is the type of the 'this' parameter
descriptor - is the Java method descriptor

getProcedureDecl

public ProcedureDecl getProcedureDecl(Type ct,
                                      java.lang.String name,
                                      java.lang.String descriptor,
                                      boolean isStatic)
Create a Clef ProcedureDecl for the specified method.

Parameters:
ct - is the type of the 'this' parameter
name - is the name of the method
descriptor - is the Java method descriptor
Returns:
a new ProcedureDecl

findField

public FieldDecl findField(ClassStuff cs,
                           java.lang.String fname)
Return the named FieldDecl in the structure.

Parameters:
cs - specifies the class
fname - specifies the name of the field

getArrayField

public FieldDecl getArrayField(java.lang.String fname)
Return the named FieldDecl in the array structure.


getVTableField

public FieldDecl getVTableField(java.lang.String fname)
Return the named FieldDecl in the virtual table structure.


getVTableDecl

public VariableDecl getVTableDecl(ClassStuff cs)
Return the virtual table VariableDecl for the class.


getArrayHeaderType

public Type getArrayHeaderType()
Return the generic array structure type.


cleanup

public static void cleanup()
Clean up for profiling statistics.