1 / 8

Building a Java Interpreter

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:

kipp
Télécharger la présentation

Building a Java Interpreter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building a Java Interpreter CS 142 (b) 01/14

  2. Lab Sessions • Monday, Wednesday • 10am – noon • ICS 189 • Send an email or make an appointment with the TA

  3. Java Interpreter • Input: a bytecode instruction stream • Output: a native-code instruction stream • In our project: • Translate each bytecode instruction to C++ code

  4. 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

  5. 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; } }

  6. 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; }

  7. 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; } }

  8. Handling of println • If the target of a static method is System.out.println, just call C++ function printf on the parameter string

More Related