201250093 谭子悦

Overview


This is the last basic lab of this course.

In Lab 7, our goal is to implement the “while” statement for SysY.

The basic idea is virtually the same as the “if” statement.

Challenges


LLVMTypeOf

The first time I submit my answer, I got the error message:

lli-13: 
lli: out.ir:8:21: error: element count must have integer type 
%a0 = alloca i32, [0 x i32] zeroinitializer, align 4 
										^

From the message we can infer that the error is caused by initializing a variable of type “i32” with an array value, but why?

After the reinspection of my code, I found the crux:

private void emptyGlobalVarInit(LLVMSymbol sym) {
    LLVMValueRef valueRef = sym.getValueRef();
    if (LLVMTypeOf(valueRef).equals(i32Type)) {
      LLVMSetInitializer(valueRef, zero);
    } else {
      // Array initializer
			...
    }
  }

Take notice of the “if condition” on line 3: LLVMTypeOf(valueRef).equals(i32Type)

The evaluation of this condition runs correctly on my own test cases, so I guess there might be some tricky cases that break this comparison.

The guarantee the correctness, I changed the condition to *sym*.getDimenSz() == 0, which passed the test.

<aside> 💡 One more thing…

While I’m writing this report, I STFW for the usage of LLVMTypeOf, and found that I didn’t even use it in the right way!

It should be used as:

Instead of comparing it directly, we should compare its type enum!

</aside>

Nested While Loop

When implementing “break” and “continue”, I first simply declared two field variables to store the current basic blocks of “condition” and “break”, which are both assigned at entering the “while” and nullified at exiting the “while”.