Skip to content

Commit

Permalink
Add scope suppression to StmtBlockAST
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Gleyzer committed Sep 13, 2023
1 parent 7e74683 commit f921977
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 51 deletions.
7 changes: 3 additions & 4 deletions javatools/src/main/java/org/xvm/asm/MethodStructure.java
Expand Up @@ -715,16 +715,15 @@ public BinaryAST<Constant> getAst()
assert aconstLocal != null;

ConstantResolver<Constant> res = new ConstantRegistry(this, pool);
DataInput in = new DataInputStream(new ByteArrayInputStream(abAst));
try
DataInput in = new DataInputStream(new ByteArrayInputStream(abAst));
try (var ignore = ConstantPool.withPool(pool))
{
m_ast = ast = BinaryAST.readAST(in, res);
return m_ast = BinaryAST.readAST(in, res);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
return ast;
}

return null;
Expand Down
27 changes: 13 additions & 14 deletions javatools/src/main/java/org/xvm/asm/Op.java
Expand Up @@ -873,10 +873,7 @@ public void register(RegisterAST<Constant> reg)
}
else
{
if (reg.isRegIdAssigned())
{
assert regId == m_listRegs.size();
}
assert !reg.isRegIdAssigned() || regId == m_listRegs.size();

regId = m_listRegs.size();
reg.setRegId(regId);
Expand Down Expand Up @@ -908,41 +905,43 @@ public RegisterAST<Constant> getRegister(int regId)
break;

case Op.A_THIS:
assert !f_method.isConstructor() && !f_method.isValidator();
assert !f_method.isStatic();
assert !f_method.isConstructor() && !f_method.isValidator() &&
!f_method.isStatic();
type = typeThis;
name = "this";
break;

case Op.A_TARGET:
assert !f_method.isConstructor() && !f_method.isValidator();
assert !f_method.isStatic();
assert !f_method.isConstructor() && !f_method.isValidator() &&
!f_method.isStatic();
type = typeThis;
name = "this:target";
break;

case Op.A_PUBLIC:
assert !f_method.isConstructor() && !f_method.isValidator();
assert !f_method.isStatic();
assert !f_method.isConstructor() && !f_method.isValidator() &&
!f_method.isStatic();
type = f_pool.ensureAccessTypeConstant(typeThis, Access.PUBLIC);
name = "this:public";
break;

case Op.A_PROTECTED:
assert !f_method.isConstructor() && !f_method.isValidator();
assert !f_method.isStatic();
assert !f_method.isConstructor() && !f_method.isValidator() &&
!f_method.isStatic();
type = f_pool.ensureAccessTypeConstant(typeThis, Access.PROTECTED);
name = "this:protected";
break;

case Op.A_PRIVATE:
assert !f_method.isConstructor() && !f_method.isValidator();
assert !f_method.isStatic();
assert !f_method.isConstructor() && !f_method.isValidator() &&
!f_method.isStatic();
type = f_pool.ensureAccessTypeConstant(typeThis, Access.PRIVATE);
name = "this:private";
break;

case Op.A_STRUCT:
assert f_method.isConstructor() || f_method.isValidator() ||
!f_method.isStatic();
type = f_pool.ensureIntersectionTypeConstant(f_pool.typeStruct(),
f_pool.ensureAccessTypeConstant(typeThis, Access.STRUCT));
name = "this:struct";
Expand Down
15 changes: 10 additions & 5 deletions javatools/src/main/java/org/xvm/asm/ast/BinaryAST.java
Expand Up @@ -156,6 +156,7 @@ public enum NodeType {
NarrowedExpr,

StmtBlock, // {...}, do{...}while(False); etc.
MultiStmt,
IfThenStmt, // if(cond){...}
IfElseStmt, // if(cond){...}else{...}
SwitchStmt, // switch(cond){...}
Expand Down Expand Up @@ -222,7 +223,8 @@ <C> BinaryAST<C> instantiate() {
case TemplateExpr -> new TemplateExprAST<>();
case ThrowExpr -> new ThrowExprAST<>();

case StmtBlock -> new StmtBlockAST<>();
case StmtBlock -> new StmtBlockAST<>(true);
case MultiStmt -> new StmtBlockAST<>(false);
case IfThenStmt -> new IfStmtAST<>(false);
case IfElseStmt -> new IfStmtAST<>(true);
case LoopStmt -> new LoopStmtAST<>();
Expand Down Expand Up @@ -295,7 +297,7 @@ public static <C, N extends BinaryAST<C>> N readAST(DataInput in, ConstantResolv
throws IOException {
N node = (N) (NodeType.valueOf(in.readUnsignedByte())).instantiate();
if (node == null) {
node = (N) new StmtBlockAST<C>(NO_ASTS);
node = (N) new StmtBlockAST<C>(NO_ASTS, false);
} else {
node.readBody(in, res);
}
Expand Down Expand Up @@ -328,11 +330,14 @@ protected static <C> C prepareConst(C constant, ConstantResolver<C> res) {
return constant == null ? null : res.register(constant);
}

public static <C> BinaryAST<C> makeStatement(BinaryAST<C>[] stmts) {
/**
* Make a synthetic statement block for the specified statements without creating a new scope.
*/
public static <C> BinaryAST<C> makeMultiStatement(BinaryAST<C>[] stmts) {
return switch (stmts == null ? 0 : stmts.length) {
case 0 -> new StmtBlockAST<>(NO_ASTS);
case 0 -> new StmtBlockAST<>(NO_ASTS, false);
case 1 -> stmts[0];
default -> new StmtBlockAST<>(stmts);
default -> new StmtBlockAST<>(stmts, false);
};
}

Expand Down
6 changes: 0 additions & 6 deletions javatools/src/main/java/org/xvm/asm/ast/RegAllocAST.java
Expand Up @@ -33,12 +33,6 @@ public class RegAllocAST<C>
reg = named ? NAMED : UNNAMED;
}

// REVIEW
RegAllocAST(RegisterAST<C> reg) {
assert reg != null && !reg.isRegIdSpecial();
this.reg = reg;
}

/**
* Construct a register.
*
Expand Down
43 changes: 30 additions & 13 deletions javatools/src/main/java/org/xvm/asm/ast/StmtBlockAST.java
Expand Up @@ -10,6 +10,7 @@

import static org.xvm.asm.ast.BinaryAST.NodeType.None;
import static org.xvm.asm.ast.BinaryAST.NodeType.StmtBlock;
import static org.xvm.asm.ast.BinaryAST.NodeType.MultiStmt;

import static org.xvm.util.Handy.indentLines;

Expand All @@ -22,35 +23,52 @@ public class StmtBlockAST<C>

private BinaryAST<C>[] stmts;

StmtBlockAST() {}
private final boolean hasScope;

public StmtBlockAST(BinaryAST<C>[] stmts) {
StmtBlockAST(boolean hasScope) {
this.hasScope = hasScope;
}

public StmtBlockAST(BinaryAST<C>[] stmts, Boolean hasScope) {
assert stmts != null && Arrays.stream(stmts).allMatch(Objects::nonNull);
this.stmts = stmts;
this.stmts = stmts;
this.hasScope = hasScope && stmts.length > 0;
}

@Override
public NodeType nodeType() {
return StmtBlock;
public boolean hasScope() {
return hasScope;
}

public BinaryAST<C>[] getStmts() {
return stmts; // note: caller must not modify returned array in any way
}

@Override
public NodeType nodeType() {
return hasScope ? StmtBlock : MultiStmt;
}

@Override
protected void readBody(DataInput in, ConstantResolver<C> res)
throws IOException {
res.enter();
stmts = readASTArray(in, res);
res.exit();
if (hasScope) {
res.enter();
stmts = readASTArray(in, res);
res.exit();
} else {
stmts = readASTArray(in, res);
}
}

@Override
public void prepareWrite(ConstantResolver<C> res) {
res.enter();
prepareASTArray(stmts, res);
res.exit();
if (hasScope) {
res.enter();
prepareASTArray(stmts, res);
res.exit();
} else {
prepareASTArray(stmts, res);
}
}

@Override
Expand Down Expand Up @@ -90,7 +108,6 @@ public String toString() {
if (stmts.length == 0) {
return "{}";
}

return "{ ... " + stmts.length + " statements ... }";
}
}
Expand Up @@ -680,7 +680,7 @@ protected boolean emit(Context ctx, boolean fReachable, Code code, ErrorListener
fBlockReachable = false;

// degenerate case: we don't need to generate neither the "condition" nor "update" AST
holder.setAst(this, new StmtBlockAST<>(aAstInit));
holder.setAst(this, new StmtBlockAST<>(aAstInit, false));
}
else if (fAlwaysTrue)
{
Expand Down Expand Up @@ -733,7 +733,7 @@ else if (fAlwaysTrue)
List<Statement> listUpdate = update;
int cUpdate = listUpdate.size();
Label[] aUpdateLabel = m_alabelUpdateGround;
BinaryAST[] aAstUpdate = new BinaryAST[cUpdate];
BinaryAST[] aAstUpdate = new BinaryAST[cUpdate];
for (int i = 0; i < cUpdate; ++i)
{
Statement stmt = listUpdate.get(i);
Expand Down Expand Up @@ -775,9 +775,9 @@ else if (fAlwaysTrue)
if (!fAlwaysFalse)
{
holder.setAst(this,
new ForStmtAST<>(BinaryAST.makeStatement(aAstInit),
new ForStmtAST<>(BinaryAST.makeMultiStatement(aAstInit),
BinaryAST.makeCondition(aCondAST),
BinaryAST.makeStatement(aAstUpdate),
BinaryAST.makeMultiStatement(aAstUpdate),
astBody));
}
return !fAlwaysTrue && fCompletes;
Expand Down
Expand Up @@ -370,7 +370,7 @@ public boolean compileMethod(RootContext ctx, Code code, ErrorListener errs)
// a void method has an implicit "return;" at the end of it
code.add(new Return_0());

// add the return statement to the AST
// add the return statement to the BinaryAST
if (astRoot instanceof StmtBlockAST astBlock)
{
BinaryAST<Constant>[] oldStmts = astBlock.getStmts();
Expand All @@ -379,7 +379,7 @@ public boolean compileMethod(RootContext ctx, Code code, ErrorListener errs)
BinaryAST<Constant>[] newStmts = new BinaryAST[newSize];
System.arraycopy(oldStmts, 0, newStmts, 0, oldSize);
newStmts[oldSize] = new ReturnStmtAST<>(null);
astRoot = new StmtBlockAST(newStmts);
astRoot = new StmtBlockAST(newStmts, true);
}
}
else
Expand Down Expand Up @@ -590,7 +590,7 @@ else if (fMethod)
asts = listAsts.toArray(BinaryAST.NO_ASTS);
}

holder.setAst(this, new StmtBlockAST<>(asts));
holder.setAst(this, new StmtBlockAST<>(asts, hasScope()));
return fCompletable;
}

Expand Down
Expand Up @@ -426,7 +426,7 @@ protected boolean emit(Context ctx, boolean fReachable, Code code, ErrorListener
fAnyCatchCompletes |= stmtCatch.completes(ctx, fCompletes, code, errs);

aAstCatches[i] = new StmtBlockAST<>(
new BinaryAST[] {aAllocCatch[i], holder.getAst(stmtCatch)});
new BinaryAST[] {aAllocCatch[i], holder.getAst(stmtCatch)}, true);
}
}

Expand Down
Expand Up @@ -581,7 +581,7 @@ protected boolean emit(Context ctx, boolean fReachable, Code code, ErrorListener
BinaryAST<Constant>[] aAst = new BinaryAST[cAllocs + 1];
System.arraycopy(m_aAllocSpecial, 0, aAst, 0, cAllocs);
aAst[cAllocs] = astBody;
holder.setAst(this, new StmtBlockAST<>(aAst));
holder.setAst(this, new StmtBlockAST<>(aAst, true));
}
else
{
Expand Down

0 comments on commit f921977

Please sign in to comment.