201250093 谭子悦

Overview

The major task of Lab 5 includes generating LLVM IR for:

  1. Function definition and function call.
  2. Declaration, definition, and other usages of local variables.

Since type checking has already been done in Lab 3, we can assume that all the codes we will work on are correct and hence concentrate on the building of LLVM IR.

The idea is basically the same as Lab 3: build up the symbol table, traverse the tree, and do the right thing at the right time...

Challenges

When implementing and debugging the code, I met several challenges:

Array Size

My implementation of Lab 4 deals with expression in a dynamic way and is hence compatible with variable computations. However, I found purely dynamic is not enough, since the initialization of the array requires a specific size, which must be decided at compile time.

According to the specification of SysY, the constExpr for array size can include any expressions composed of numbers or constant variables. Therefore, I refactored my LLVMSymbol to store initVal for constant variables and wrote a new visitor called StaticEvalVisitor to evaluate the expression at compile time.

Const Array

Although the lab documentation stated that "we guarantee our test cases won't include const array variable", the truth seems to be the opposite.

My implementation of visitConstDecl asserts every const declaration is not an array and evaluates the init value by:

 int initVal = constDef.constInitVal().constExp().accept(evalVisitor);

However, the online judgment caught NullPointerException at this line, which means there must have been a const array declaration.

Therefore, to pass the judgment, I have to extend my code to support the const array.

Void Return

When translating the call statement, at first I naively name the return value ret in all cases, but soon the problem emerged.