:: Enseignements :: ESIPE :: E5INFO :: 2022-2023 :: Machine Virtuelle (et bazar autour ...) ::
[LOGO]

Lab 2b - Stack Interpreter (the return)


Here is a video of Gil Tene from Azul System explaining the different GC algorithms
Understanding Garbage Collection by Gil Tene

Exercice 1 - InstrRewriter

We now want to directly execute a smalljs script from the command line. For that, we want to complete the code of class InstrRewriter that transforms an AST to a Code (an array of instructions).
In order to simplify the code, the rewrite will be done when the code of a function is discovered. We will see in a later lab how to do that lazily.

  1. In the class InstrRewriter, what is the purpose of the VARIABLE_VISITOR ?
  2. In the InstrRewriter visitor, implement the support of Literal.
    Verify that the test marked "Q2" pass.
  3. Verify that the string literal are also supported by executing the test marked "Q3".
  4. Add the support of the function call by implementing the visit of FunCall.
    Verify that the test marked "Q4" pass.
    Note: When an identifier is not a local variable, a LOOKUP is emitted. Implement the visit of LocalVarAccess to add that behavior. Note2: in case the local variable exists, throw an exception, we will change that later.
  5. Verify that the test marked "Q5" pass.
  6. Verify that the return value are propagated properly so the test marked "Q6" pass.
  7. Also Verify that the test marked "Q7" pass.
  8. Add the support to rewrite the declaration of a variable and the loading of that variable by implementing respectively the visit of LocalVarAssignment and LocalVarAccess.
    Verify that the tests marked "Q8" pass.
  9. Verify that the test marked "Q9" pass.
  10. We now want to support the declaration of user defined function. For that, you have to implement the visit of the Fun and the Return ast nodes.
    Verify that the tests marked "Q10" pass.
    Note: if you don't see the shape of the generated code, you can take a look to the tests marked "Q10" in StackInterpreterInstrTests.
  11. In order to implement the if ... else, implement the visit of If ast node and verify that the tests marked "Q11" pass.
  12. Verify that the tests marked "Q12" pass.

Exercice 2 - Allocation

We now want to implement the allocation of object of the heap.
We first need to implement the support of the opcodes NEW, GET and PUT, in the stack interpreter then add the support of New, FieldAccess, FieldAssignment and MethodCall in the rewriter.

  1. Implement the instruction NEW in the StackInterpreter and verifies that the test maked Q13 in StackInterpreterInsnTest pass.
  2. Verify that you can access to local variables to initialize the field of the object and that fields initialization are interpreted in the right order by running the tests marked Q14.
    Otherwise, change the code accordingly.
  3. Implement the instruction GET in the StackInterpreter and verifies that the tests marked Q15 pass.
  4. Implement the instruction PUT in the StackInterpreter and verifies that the tests marked Q16 pass.
  5. Verify that the test marked Q17 that does a method call works.
  6. Now that the interpreter is working, we need to implement the support of the AST nodes. First, implement the support of the node New in the Rewriter, and verify that the tests marked Q13 and Q14 in StackInterpreterTest pass.
    Note: you may need to change other part of the Rewriter.
  7. Implement the AST node FieldAccess in the Rewriter and verifies that the tests marked Q15 pass.
  8. Implement the AST node FieldAssignment in the Rewriter and verifies that the tests marked Q16 pass.
  9. Implement the AST node MethodCall in the Rewriter and verifies that the tests marked Q17 pass.

Exercice 3 - Heap Bang & StackTrace

We want to detect if the heap is full and print the stacktrace

  1. How to detect that the heap is full when doing a NEW ?
  2. What data structure of the interpreter contains the information necessary to generate a stack trace ?
  3. Write the code that generate a stack trace (without the line numbers) when the heap is full.
    Be careful and verify that the method on top of the stack is the right one.