Package bytecode

Class CodeProcedure


  • public class CodeProcedure
    extends java.lang.Object
    This class is conceptually analogous to the class CodeFile for the whole program (and to CodeStruct, responsible for records. An instance of this class is to contain a representation of the code of a procedure. Compared to CodeFile, the class CodeProcedure is more complex. A procedure can contain more kinds of code pieces. In particular, all instructions are contained inside procedure.
    • Constructor Summary

      Constructors 
      Constructor Description
      CodeProcedure​(java.lang.String name, bytecode.type.CodeType returnType, CodeFile codeFile)
      The constructor gives back the initially (mostly) empty data structure to store the definition of a procedure.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int addInstruction​(bytecode.instructions.Instruction instruction)
      Adding a instruction.
      void addLocalVariable​(java.lang.String name, bytecode.type.CodeType type)
      Adding a local variable (declaration) to the procedure.
      void addParameter​(java.lang.String name, bytecode.type.CodeType type)  
      int addStringConstant​(java.lang.String value)
      The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
      int fieldNumber​(java.lang.String structName, java.lang.String varName)
      The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
      byte[] getBytecode()
      Extract the actual bytecode as an array of bytes.
      java.lang.String getName()
      A simple get-ter method for the name of the procedure.
      int globalVariableNumber​(java.lang.String name)
      The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
      int procedureNumber​(java.lang.String name)
      The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
      void replaceInstruction​(int place, bytecode.instructions.Instruction instruction)
      Replacing an instruction at some place specified by its index.
      int structNumber​(java.lang.String name)
      The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
      int variableNumber​(java.lang.String name)
      Determine the index (an integer) of a local variable or procedure parameter declared earlier.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CodeProcedure

        public CodeProcedure​(java.lang.String name,
                             bytecode.type.CodeType returnType,
                             CodeFile codeFile)
        The constructor gives back the initially (mostly) empty data structure to store the definition of a procedure. Provided initially as part of the constructure are the name of the procedure (which must be unique) and the return type. Note that the types of the parameters are not initially given, they have to be added later one one after the other, when adding the parameters together with their types. The last parameter is the refence to the "code file", representing the main program. The object requires access to that for some of its tasks (in some call-back-like pattern).
        Parameters:
        name - of the procedure
        returnType - specifies the return type of the procedure
        codeFile - reference to the representation of the main program
    • Method Detail

      • addParameter

        public void addParameter​(java.lang.String name,
                                 bytecode.type.CodeType type)
        Parameters:
        name - of the parameter of the procedure
        type - of the parameter
      • addLocalVariable

        public void addLocalVariable​(java.lang.String name,
                                     bytecode.type.CodeType type)
        Adding a local variable (declaration) to the procedure. The names of local variables have to be unique per procedure, and that includes the parameters as well; in that respect parameters count among the local variables. Internally, local variables are referred to by an index. That's analogous to the treatment of global variables in a CodeFile object.
        Parameters:
        name - of the local variable
        type - of the local variable
      • addInstruction

        public int addInstruction​(bytecode.instructions.Instruction instruction)
        Adding a instruction. The classes representing the instructions are provided by the subpackage bytecode/instructions*. The procedure returns the index of the instruction. That can be used, for instance, to replace the instruction.
        Parameters:
        instruction - being added
        Returns:
        index of the instruction
      • replaceInstruction

        public void replaceInstruction​(int place,
                                       bytecode.instructions.Instruction instruction)
        Replacing an instruction at some place specified by its index.
        Parameters:
        place - i.e., index of the instruction to be replaced
        instruction - that replaces the old one. An situation where a replacement could come in handy are jumps. First, one adds a no-op (NOP) instruction as a placeholder, which is later replaced by the correct instruction, a jump.
      • addStringConstant

        public int addStringConstant​(java.lang.String value)
        The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
        Parameters:
        value - representing the string constant
        Returns:
        the index of the constant.
      • variableNumber

        public int variableNumber​(java.lang.String name)
        Determine the index (an integer) of a local variable or procedure parameter declared earlier. Note that local variables and parameters are stored in separate arrays. That means the official "index" of a local variable is not identical to the array index in the array for local variables. For parameters, there is no such mismatch.
        Parameters:
        name - of the variable (incl. formal parameter)
        Returns:
        the so-called index of a variable (incl. formal parameter).
      • globalVariableNumber

        public int globalVariableNumber​(java.lang.String name)
        The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
        Parameters:
        name - of the global variable
        Returns:
        index of the global variable
      • procedureNumber

        public int procedureNumber​(java.lang.String name)
        The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
        Parameters:
        name - of the procedure.
        Returns:
        index of the procedure.
      • structNumber

        public int structNumber​(java.lang.String name)
        The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
        Parameters:
        name - of the struct.
        Returns:
        index of the struct.
      • fieldNumber

        public int fieldNumber​(java.lang.String structName,
                               java.lang.String varName)
        The method corresponds to the corresponding method in the code file, and indeed an invocation simply delegates the task to the method of the code file.
        Parameters:
        structName - name of the struct.
        varName - name of the field
        Returns:
        index of the field.
      • getName

        public java.lang.String getName()
        A simple get-ter method for the name of the procedure.
        Returns:
        name of the procedure
      • getBytecode

        public byte[] getBytecode()
        Extract the actual bytecode as an array of bytes.
        Returns:
        the byte code of the procedure.