@Override public JLabel resolveLabel(String name) { JLabel l = labels.get(name); if (l != null) { return l; } else { return parent().resolveLabel(name); } }
void addLabel(String name, JLabel label) { if (current instanceof Scope.Labelled) { Scope.Labelled labelledScope = (Scope.Labelled) current; labelledScope.addLabel(name, label); } else { Logger.error("Cannot add label (" + name + ") to a Scope (" + current.getClass().getName() + ") that doesn't allow labels!"); throw new RuntimeException("Invalid Scope for operation!"); } }
Optional<JLabel> resolveLabel(String id) { JLabel l = current.resolveLabel(id); if (l == null) { return Optional.absent(); } else { return Optional.of(l); } }
@Override public void caseALabeledStatement(ALabeledStatement node) { String labelName = node.getIdentifier().getText(); JLabel label = parent.createLabel(labelName); context.pushStatementScope(); try { context.addLabel(labelName, label); StatementAdapter sa = new StatementAdapter(parent, context); node.getStatement().apply(sa); } finally { context.popScope(); } }
@Override public void caseABreakStatement(ABreakStatement node) { if (node.getIdentifier() != null) { String id = node.getIdentifier().getText(); Optional<JLabel> olabel = context.resolveLabel(id); if (olabel.isPresent()) { parent._break(olabel.get()); } else { Logger.error(context.getFile(), node.getIdentifier(), "Couldn't find label for id. Skipping break target."); parent._break(); } } else { parent._break(); } }
@Override public void caseAContinueStatement(AContinueStatement node) { if (node.getIdentifier() != null) { String id = node.getIdentifier().getText(); Optional<JLabel> olabel = context.resolveLabel(id); if (olabel.isPresent()) { parent._continue(olabel.get()); } else { Logger.error(context.getFile(), node.getIdentifier(), "Couldn't find label for id. Skipping continue target."); parent._continue(); } } else { parent._continue(); } }
private HoldingContainer visitBooleanAnd(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getMajorType()); JLabel label = generator.getEvalBlockLabel("AndOP"); JBlock eval = generator.getEvalBlock().block(); // enter into nested block generator.nestEvalBlock(eval); HoldingContainer arg = null; JExpression e = null; // value of boolean "and" when one side is null // p q p and q // true null null // false null false // null true null // null false false // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = null; if (arg.isOptional()) { earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().ne(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } } else { earlyExit = eval._if(arg.getValue().ne(JExpr.lit(1)))._then(); } if (out.isOptional()) { earlyExit.assign(out.getIsSet(), JExpr.lit(1)); } earlyExit.assign(out.getValue(), JExpr.lit(0)); earlyExit._break(label); } if (out.isOptional()) { assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(1)); } else { assert (e == null); eval.assign(out.getValue(), JExpr.lit(1)) ; } generator.unNestEvalBlock(); // exit from nested block return out; }
private HoldingContainer visitBooleanOr(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getMajorType()); JLabel label = generator.getEvalBlockLabel("OrOP"); JBlock eval = generator.getEvalBlock().block(); generator.nestEvalBlock(eval); // enter into nested block. HoldingContainer arg = null; JExpression e = null; // value of boolean "or" when one side is null // p q p and q // true null true // false null null // null true true // null false null // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = null; if (arg.isOptional()) { earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().eq(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } } else { earlyExit = eval._if(arg.getValue().eq(JExpr.lit(1)))._then(); } if (out.isOptional()) { earlyExit.assign(out.getIsSet(), JExpr.lit(1)); } earlyExit.assign(out.getValue(), JExpr.lit(1)); earlyExit._break(label); } if (out.isOptional()) { assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(0)); } else { assert (e == null); eval.assign(out.getValue(), JExpr.lit(0)) ; } generator.unNestEvalBlock(); // exit from nested block. return out; }
public JLabel getEvalBlockLabel (String prefix) { return getEvalBlock().label(prefix + labelIndex ++); }
private HoldingContainer visitBooleanAnd(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getCompleteType()); JLabel label = generator.getEvalBlockLabel("AndOP"); JBlock eval = generator.createInnerEvalBlock(); generator.nestEvalBlock(eval); // enter into nested block HoldingContainer arg = null; JExpression e = null; // value of boolean "and" when one side is null // p q p and q // true null null // false null false // null true null // null false false // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = null; earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().ne(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } earlyExit.assign(out.getIsSet(), JExpr.lit(1)); earlyExit.assign(out.getValue(), JExpr.lit(0)); earlyExit._break(label); } assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(1)); generator.unNestEvalBlock(); // exit from nested block return out; }
private HoldingContainer visitBooleanOr(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getCompleteType()); JLabel label = generator.getEvalBlockLabel("OrOP"); JBlock eval = generator.createInnerEvalBlock(); generator.nestEvalBlock(eval); // enter into nested block. HoldingContainer arg = null; JExpression e = null; // value of boolean "or" when one side is null // p q p and q // true null true // false null null // null true true // null false null // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().eq(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } earlyExit.assign(out.getIsSet(), JExpr.lit(1)); earlyExit.assign(out.getValue(), JExpr.lit(1)); earlyExit._break(label); } assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(0)); generator.unNestEvalBlock(); // exit from nested block. return out; }
private HoldingContainer visitBooleanAnd(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getMajorType()); JLabel label = generator.getEvalBlockLabel("AndOP"); JBlock eval = generator.createInnerEvalBlock(); generator.nestEvalBlock(eval); // enter into nested block HoldingContainer arg = null; JExpression e = null; // value of boolean "and" when one side is null // p q p and q // true null null // false null false // null true null // null false false // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = null; if (arg.isOptional()) { earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().ne(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } } else { earlyExit = eval._if(arg.getValue().ne(JExpr.lit(1)))._then(); } if (out.isOptional()) { earlyExit.assign(out.getIsSet(), JExpr.lit(1)); } earlyExit.assign(out.getValue(), JExpr.lit(0)); earlyExit._break(label); } if (out.isOptional()) { assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(1)); } else { assert (e == null); eval.assign(out.getValue(), JExpr.lit(1)) ; } generator.unNestEvalBlock(); // exit from nested block return out; }
private HoldingContainer visitBooleanOr(BooleanOperator op, ClassGenerator<?> generator) { HoldingContainer out = generator.declare(op.getMajorType()); JLabel label = generator.getEvalBlockLabel("OrOP"); JBlock eval = generator.createInnerEvalBlock(); generator.nestEvalBlock(eval); // enter into nested block. HoldingContainer arg = null; JExpression e = null; // value of boolean "or" when one side is null // p q p and q // true null true // false null null // null true true // null false null // null null null for (int i = 0; i < op.args.size(); i++) { arg = op.args.get(i).accept(this, generator); JBlock earlyExit = null; if (arg.isOptional()) { earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().eq(JExpr.lit(1))))._then(); if (e == null) { e = arg.getIsSet(); } else { e = e.mul(arg.getIsSet()); } } else { earlyExit = eval._if(arg.getValue().eq(JExpr.lit(1)))._then(); } if (out.isOptional()) { earlyExit.assign(out.getIsSet(), JExpr.lit(1)); } earlyExit.assign(out.getValue(), JExpr.lit(1)); earlyExit._break(label); } if (out.isOptional()) { assert (e != null); JConditional notSetJC = eval._if(e.eq(JExpr.lit(0))); notSetJC._then().assign(out.getIsSet(), JExpr.lit(0)); JBlock setBlock = notSetJC._else().block(); setBlock.assign(out.getIsSet(), JExpr.lit(1)); setBlock.assign(out.getValue(), JExpr.lit(0)); } else { assert (e == null); eval.assign(out.getValue(), JExpr.lit(0)) ; } generator.unNestEvalBlock(); // exit from nested block. return out; }
public JLabel getEvalBlockLabel (String prefix) { return getEvalBlock().label(prefix + labelIndex++); }
@Override public JLabel createLabel(String name) { return block.label(name); }
@Override public void _break(JLabel label) { block._break(label); }
@Override public void _continue(JLabel label) { block._continue(label); }
@Override public void addLabel(String name, JLabel label) { labels.put(name, label); }
@Override public JLabel resolveLabel(String name) { return null; }
@Override public JLabel resolveLabel(String name) { return null; // no point in resolving labels higher than one StatementScope }
public abstract JLabel resolveLabel(String name);
public void addLabel(String name, JLabel label);
public JLabel createLabel(String name);
public void _break(JLabel label);
public void _continue(JLabel label);