80 likes | 185 Views
Join our lab sessions in CS 142, where we will build a Java interpreter that translates bytecode instruction streams to native C++ code. Our focus includes key instructions such as iconst, iload, istore, iadd, if conditions, and method invocations. The sessions will be held on Mondays and Wednesdays from 10 AM to noon in ICS 189. For assistance, feel free to email or make an appointment with the TA. Discover how to implement core operations through hands-on coding and deepen your understanding of both Java and C++.
E N D
Building a Java Interpreter CS 142 (b) 01/14
Lab Sessions • Monday, Wednesday • 10am – noon • ICS 189 • Send an email or make an appointment with the TA
Java Interpreter • Input: a bytecode instruction stream • Output: a native-code instruction stream • In our project: • Translate each bytecode instruction to C++ code
Instructions We Are Interested in • iconst, iload and istore • iadd • if_icmpne, if_icmpeq, if_icmpgt, if_icmplt, ifeq, ifne, ifgt, iflt • goto • invokestatic • return
Building an Interpreter void interpret(InstructionSeq is){ for (Instruction i = is.first(); i != null; i = next) { intopCode = i.getOpCode(); next = is.nextInstruction(i); switch(opCode){ case iload: int index = i.getOperand(); int value = values.get(index); push(value); break; case istore: int index = i.getOperand(); int value = pop(); values.set(index, value); break; } }
Building an Interpreter (Cond.) for(Instruction i = is.first(); i != null; i = next){ intopCode = i.getOpCode(); next = is.nextInstruction(i); switch(opCode){ case iadd: int value1 = pop(); int value2 = pop(); push(value1 + value2); break; case if_icmpeq: int value1 = pop(); int value2 = pop(); int target = i.getOperand(); if(value1 == value2) next = is.getInstruction(target); break; case goto: int target = i.getOperand(); next = is.getInstruction(target); break; }
Building an Interpreter (Cond.) void interpret(InstructionSeqis){ for(Instruction i = is.first(); i != null; i = next){ … case invokestatic: String mName = ((CallInsruction) i). getTarget(); InstructionSetnewSet = findInstructionSetforMethod(mName); invoke(newSet); break; } }
Handling of println • If the target of a static method is System.out.println, just call C++ function printf on the parameter string