Skip to content

Commit

Permalink
Fix "bast" production for ReturnStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Gleyzer committed Sep 13, 2023
1 parent 17a8121 commit 876c39d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
Expand Up @@ -1385,7 +1385,6 @@ public Argument generateArgument(Context ctx, Code code,
{
regTemp = code.createRegister(idProp.getRefType(ctx.getThisType()));
code.add(new Var_D(regTemp));
// TODO GG: AST for dynamic register
}
else
{
Expand All @@ -1411,7 +1410,6 @@ public Argument generateArgument(Context ctx, Code code,
{
regTemp = code.createRegister(idProp.getRefType(argLeft.getType()));
code.add(new Var_D(regTemp));
// TODO GG: AST for dynamic register
}
code.add(new P_Get(idProp, argLeft, regTemp));

Expand Down
59 changes: 36 additions & 23 deletions javatools/src/main/java/org/xvm/compiler/ast/ReturnStatement.java
Expand Up @@ -13,8 +13,11 @@
import org.xvm.asm.MethodStructure.Code;
import org.xvm.asm.Register;

import org.xvm.asm.ast.BinaryAST;
import org.xvm.asm.ast.ExprAST;
import org.xvm.asm.ast.ReturnStmtAST;
import org.xvm.asm.ast.UnaryOpExprAST;
import org.xvm.asm.ast.UnaryOpExprAST.Operator;

import org.xvm.asm.constants.TypeConstant;

Expand Down Expand Up @@ -332,56 +335,64 @@ protected boolean emit(Context ctx, boolean fReachable, Code code, ErrorListener
AstNode container = getCodeContainer();
boolean fConditional = container.isReturnConditional();

if (container instanceof StatementExpression expr)
if (container instanceof StatementExpression exprStmt)
{
assert !fConditional;

// emit() for a return inside a StatementExpression produces an assignment from the
// expression
// TODO m_fTupleReturn, m_fConditionalTernary, m_fFutureReturn
Assignable[] aLVals = expr.getAssignables();
int cLVals = aLVals.length;
Assignable[] aLVal = exprStmt.getAssignables();
int cLVals = aLVal.length;
ExprAST<Constant>[] aAst = new ExprAST[cLVals];
for (int i = 0, cExprs = exprs == null ? 0 : exprs.size(); i < cExprs; ++i)
{
Expression expr = exprs.get(i);
if (i < cLVals)
{
exprs.get(i).generateAssignment(ctx, code, aLVals[i], errs);
expr.generateAssignment(ctx, code, aLVal[i], errs);
}
else
{
exprs.get(i).generateVoid(ctx, code, errs);
expr.generateVoid(ctx, code, errs);
}
aAst[i] = expr.getExprAST();
}
code.add(new Jump(expr.body.getEndLabel()));
code.add(new Jump(exprStmt.body.getEndLabel()));

// TODO holder <- AST
ctx.getHolder().setAst(this, new ReturnStmtAST<>(aAst));

// "return" does not complete
return false;
}

// first determine what the method declaration indicates the return value is (none, one,
// or multi)
ConstantPool pool = pool();
TypeConstant[] atypeRets = container.getReturnTypes();
int cRets = atypeRets == null ? 0 : atypeRets.length;
List<Expression> listExprs = this.exprs;
int cExprs = listExprs == null ? 0 : listExprs.size();
AstHolder holder = ctx.getHolder();
ConstantPool pool = pool();
TypeConstant[] atypeRets = container.getReturnTypes();
int cRets = atypeRets == null ? 0 : atypeRets.length;
List<Expression> listExprs = this.exprs;
int cExprs = listExprs == null ? 0 : listExprs.size();
BinaryAST<Constant> astResult;

if (m_fTupleReturn)
{
// the return statement has a single expression; the type that the expression has to
// generate is the "tuple of" all of the return types
// TODO cExprs != 1
Argument arg = listExprs.get(0).generateArgument(ctx, code, true, true, errs);
assert cExprs == 1;

// the return statement has a single expression returning a tuple; the caller expects
// multiple returns
Expression expr = listExprs.get(0);
Argument arg = expr.generateArgument(ctx, code, true, true, errs);
code.add(new Return_T(arg));
// TODO holder <- AST

astResult = new UnaryOpExprAST<>(expr.getExprAST(), Operator.Unpack, expr.getType());
}
else if (m_fConditionalTernary)
{
((TernaryExpression) listExprs.get(0)).generateConditionalReturn(ctx, code, errs);
// TODO holder <- AST
TernaryExpression expr = (TernaryExpression) listExprs.get(0);
expr.generateConditionalReturn(ctx, code, errs);

astResult = expr.getExprAST();
}
else
{
Expand All @@ -391,7 +402,7 @@ else if (m_fConditionalTernary)
{
case 0:
code.add(new Return_0());
holder.setAst(this, new ReturnStmtAST<>(null));
astResult = new ReturnStmtAST<>(null);
break;

case 1:
Expand Down Expand Up @@ -476,7 +487,7 @@ else if (m_fConditionalTernary)
}
}
}
holder.setAst(this, new ReturnStmtAST<>(new ExprAST[]{expr.getExprAST()}));
astResult = new ReturnStmtAST<>(new ExprAST[]{expr.getExprAST()});
break;
}

Expand Down Expand Up @@ -510,12 +521,14 @@ else if (m_fConditionalTernary)
code.add(labelFalse);
code.add(new Return_1(pool.valFalse()));
}
holder.setAst(this, new ReturnStmtAST<>(aASTs));
astResult = new ReturnStmtAST<>(aASTs);
break;
}
}
}

ctx.getHolder().setAst(this, astResult);

// return never completes
return false;
}
Expand Down

0 comments on commit 876c39d

Please sign in to comment.