Question: Consider the stack-machine instructions (push y through pop to x) to compute the assignment x = y + (z*y)*z in this course note. Translate these

Consider the stack-machine instructions ("push y" through "pop to x") to compute the assignment x = y + (z*y)*z in this course note. Translate these instructions into JVM instructions. Presume that x, y, z are integer variables respectively stored at virtual addresses 0, 1, 2 in the variable area. Use instructions listed in the Selection of Basic JVM Instructions in this course note:

Consider the assignment statement x = y + (z*y)*z. A pure interpreter (VH, HH) would interpret some internal, direct representation of the arithmetic expression, for example, an expression tree like this: The interpreter would somehow implement three "variable boxes" for x, y, z (e.g., by a hash table or an array), traverse the expression tree in some way, and perform the following operations:

lookup value of z box lookup value of y box multiply them and store the result in temp1 box lookup value of temp1 box lookup value of z box multiply them and store the result in temp2 box lookup value of y box lookup value of temp2 box add them and store the result in x box 

As an example of intermediate language, we use stack machine instructions (VI, HI). (The Java Virtual Machine is a stack machine using this type of instructions.) The above assignment statement would be compiled into the following instruction stream; "x", "y", "z" abbreviate the memory cells allocated to the three variables. (We will study stack-machine instructions and how to generate them later in the course.)

push y // read value of memory cell of y and push it onto the stack push z // read value of memory cell of z and push it onto the stack push y // read value of memory cell of y and push it onto the stack mul // pop top two values of the stack, multiply them, push the result onto the stack push z // read value of memory cell of z and push it onto the stack mul // pop top two values of the stack, multiply them, push the result onto the stack add // pop top two values of the stack, add them, push the result onto the stack pop to x // pop top value of the stack and store it in the memory cell of x 

Trace these operations step-by-step by writing diagrams of the contents of the stack and verify that "x" gets the value of y + (z*y)*z. Compared with the above pure interpretation process, the overhead of maintaining the expression tree and the runtime overhead of tree traversal and lookups of variable boxes have been compiled away into a linear sequence of efficient stack operations.

Selection of Basic JVM Instructions In the description of actions, "stack" and "variable area" refer to the operand stack and the variable area, respectively, in the top runtime stack frame.

  • push value onto operand stack
    • iconst_k (0 k 5) : push int constant k onto stack
    • iload_k (0 k 3) : push value at address k in variable area onto stack
    • bipush k : push byte int constant k onto stack
    • iload k : push value at address k in variable area onto stack For long, float, double types, replace prefix "i" by "l", "f", "d", respectively (except for bipush).
  • pop from operand stack
    • istore_k (0 k 3) : pop stack and store into address k in variable area
    • istore k : pop stack and store into address k in variable area For long, float, double types, replace prefix "i" by "l", "f", "d", respectively.
  • arithmetic
    • iadd, isub, imul, idiv : pop top two values from stack, apply operator to stack[top-1] and stack[top], push result onto stack For long, float, double types, replace prefix "i" by "l", "f", "d", respectively.
  • conditional jump
    • if_icmpeq k : pop top two int values from stack; if stack[top1] = stack[top] then go to instruction at address k
    • if_icmpne k : pop top two int values from stack; if stack[top1] stack[top] then go to instruction at address k
    • if_icmplt k : pop top two int values from stack; if stack[top1] < stack[top] then go to instruction at address k
    • if_icmple k : pop top two int values from stack; if stack[top1] stack[top] then go to instruction at address k
    • if_icmpgt k : pop top two int values from stack; if stack[top1] > stack[top] then go to instruction at address k
    • if_icmpge k : pop top two int values from stack; if stack[top1] stack[top] then go to instruction at address k
  • unconditional jump
    • goto k : go to instruction at address k
  • function invocation
    • invokevirtual #k : invoke instance-level function numbered k
    • invokestatic #k : invoke static function numbered k
  • function return
    • return : exit from void-type function
    • ireturn : pop top int value from stack, push it onto caller's stack, and exit from function call For long, float, double types, replace prefix "i" by "l", "f", "d", respectively.
  • Instructions operating on references have the prefix "a", for example: aconst_null, aload, aload_k (0 k 3), astore, astore_k (0 k 3), areturn.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!