Add comments to WAT

This commit is contained in:
MrLetsplay 2024-11-23 16:38:07 +01:00
parent b5c42a6690
commit 9e39936842
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg

View File

@ -227,7 +227,7 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
ref := lhs.Value.(VariableReferenceExpression)
local := strconv.Itoa(getLocal(c.CurrentBlock, ref.Variable).Index)
exprWAT.add("local.tee $" + local)
exprWAT.add("local.tee $" + local + " (;" + ref.Variable + ";)")
return exprWAT, nil
case Expression_ArrayAccess:
array := lhs.Value.(ArrayAccessExpression)
@ -246,7 +246,7 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
return emptyCode(), err
}
arrayWAT.add("local.set $" + strconv.Itoa(localArray.Index))
arrayWAT.add("local.set $" + strconv.Itoa(localArray.Index) + " (;array base address;)")
indexWAT, err := c.compileExpressionWAT(array.Index)
if err != nil {
@ -262,7 +262,7 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
indexWAT.addAll(cast)
}
indexWAT.add("local.set $" + strconv.Itoa(localIndex.Index))
indexWAT.add("local.set $" + strconv.Itoa(localIndex.Index) + " (;array index;)")
wat := concat(arrayWAT, indexWAT)
@ -270,7 +270,7 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
// Error if index < 0
wat.add(
"block",
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
c.getAddressWATType()+".const 0",
c.getAddressWATType()+".ge_s",
"br_if 0",
@ -281,8 +281,8 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
// Error if index >= array length
wat.add(
"block",
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localArray.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
"local.get $"+strconv.Itoa(localArray.Index)+" (;array base address;)",
"i32.load", // Load array length
c.getAddressWATType()+".lt_s",
"br_if 0",
@ -293,10 +293,10 @@ func (c *Compiler) compileAssignmentExpressionWAT(assignment AssignmentExpressio
elementType := array.Array.ValueType.Value.(ArrayType).ElementType
wat.add(
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
c.getAddressWATType()+".const "+strconv.Itoa(c.getTypeSizeBytes(elementType)),
c.getAddressWATType()+".mul",
"local.get $"+strconv.Itoa(localArray.Index),
"local.get $"+strconv.Itoa(localArray.Index)+" (;array base address;)",
c.getAddressWATType()+".add",
c.getAddressWATType()+".const 4", // first 4 bytes = length
c.getAddressWATType()+".add",
@ -352,24 +352,17 @@ func (c *Compiler) compileAssignmentUpdateExpressionWAT(lhs Expression, updateWA
return emptyCode(), err
}
var tmpLocal Local
wat := exprWAT.clone()
if evaluateToOldValue {
tmpLocal = Local{Name: "", Type: *lhs.ValueType, IsParameter: false, Index: len(c.CurrentFunction.Locals)}
c.CurrentFunction.Locals = append(c.CurrentFunction.Locals, tmpLocal)
wat.add("local.tee $" + strconv.Itoa(tmpLocal.Index))
wat.add("local.get $" + local + " (;" + ref.Variable + ";)")
}
wat.addAll(updateWAT)
if evaluateToOldValue {
wat.add(
"local.set $"+local,
"local.get $"+strconv.Itoa(tmpLocal.Index),
)
wat.add("local.set $" + local + " (;" + ref.Variable + ";)")
} else {
wat.add("local.tee $" + local)
wat.add("local.tee $" + local + " (;" + ref.Variable + ";)")
}
return wat, nil
@ -467,8 +460,10 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
cast = getTypeCast(expr.ValueType.Value.(PrimitiveType))
}
local := strconv.Itoa(getLocal(c.CurrentBlock, ref.Variable).Index)
code := emptyCode()
code.add("local.get $" + strconv.Itoa(getLocal(c.CurrentBlock, ref.Variable).Index))
code.add("local.get $" + local + " (;" + ref.Variable + ";)")
code.addAll(cast)
return code, nil
@ -651,7 +646,7 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
return emptyCode(), err
}
arrayWAT.add("local.set $" + strconv.Itoa(localArray.Index))
arrayWAT.add("local.set $" + strconv.Itoa(localArray.Index) + " (;array base address;)")
indexWAT, err := c.compileExpressionWAT(array.Index)
if err != nil {
@ -667,7 +662,7 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
indexWAT.addAll(cast)
}
indexWAT.add("local.set $" + strconv.Itoa(localIndex.Index))
indexWAT.add("local.set $" + strconv.Itoa(localIndex.Index) + " (;array index;)")
wat := concat(arrayWAT, indexWAT)
@ -675,7 +670,7 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
// Error if index < 0
wat.add(
"block",
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
c.getAddressWATType()+".const 0",
c.getAddressWATType()+".ge_s",
"br_if 0",
@ -686,8 +681,8 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
// Error if index >= array length
wat.add(
"block",
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localArray.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
"local.get $"+strconv.Itoa(localArray.Index)+" (;array base address;)",
"i32.load", // Load array length
c.getAddressWATType()+".lt_s",
"br_if 0",
@ -698,10 +693,10 @@ func (c *Compiler) compileExpressionWAT(expr Expression) (Code, error) {
elementType := array.Array.ValueType.Value.(ArrayType).ElementType
wat.add(
"local.get $"+strconv.Itoa(localIndex.Index),
"local.get $"+strconv.Itoa(localIndex.Index)+" (;array index;)",
c.getAddressWATType()+".const "+strconv.Itoa(c.getTypeSizeBytes(elementType)),
c.getAddressWATType()+".mul",
"local.get $"+strconv.Itoa(localArray.Index),
"local.get $"+strconv.Itoa(localArray.Index)+"(;array base address;)",
c.getAddressWATType()+".add",
c.getAddressWATType()+".const 4", // first 4 bytes = length
c.getAddressWATType()+".add",