Package org.teavm.model

Examples of org.teavm.model.BasicBlock


    }

    @Test
    public void binaryOperation() {
        Program program = new Program();
        BasicBlock block = program.createBasicBlock();
        IntegerConstantInstruction constInsn = new IntegerConstantInstruction();
        constInsn.setConstant(3);
        constInsn.setReceiver(program.createVariable());
        block.getInstructions().add(constInsn);
        constInsn = new IntegerConstantInstruction();
        constInsn.setConstant(2);
        constInsn.setReceiver(program.createVariable());
        block.getInstructions().add(constInsn);
        BinaryInstruction addInsn = new BinaryInstruction(BinaryOperation.ADD, NumericOperandType.INT);
        addInsn.setFirstOperand(program.variableAt(0));
        addInsn.setSecondOperand(program.variableAt(1));
        addInsn.setReceiver(program.createVariable());
        block.getInstructions().add(addInsn);
        BinaryInstruction subInsn = new BinaryInstruction(BinaryOperation.SUBTRACT, NumericOperandType.INT);
        subInsn.setFirstOperand(program.variableAt(2));
        subInsn.setSecondOperand(program.variableAt(0));
        subInsn.setReceiver(program.createVariable());
        block.getInstructions().add(subInsn);

        assertThat(block.getInstructions().size(), is(4));
        assertThat(block.getInstructions().get(2), instanceOf(BinaryInstruction.class));
        assertThat(block.getInstructions().get(3), instanceOf(BinaryInstruction.class));

        addInsn = (BinaryInstruction)block.getInstructions().get(2);
        assertThat(addInsn.getOperation(), is(BinaryOperation.ADD));
        assertThat(addInsn.getOperandType(), is(NumericOperandType.INT));
        assertThat(addInsn.getFirstOperand().getIndex(), is(0));
        assertThat(addInsn.getSecondOperand().getIndex(), is(1));
        assertThat(addInsn.getReceiver().getIndex(), is(2));

        subInsn = (BinaryInstruction)block.getInstructions().get(3);
        assertThat(subInsn.getOperation(), is(BinaryOperation.SUBTRACT));
        assertThat(subInsn.getOperandType(), is(NumericOperandType.INT));
        assertThat(subInsn.getFirstOperand().getIndex(), is(2));
        assertThat(subInsn.getSecondOperand().getIndex(), is(0));
        assertThat(subInsn.getReceiver().getIndex(), is(3));
View Full Code Here


                }
                continue;
            }
            visited[step.block] = true;
            startLocations.set(step.block, step.startLocations);
            BasicBlock block = program.basicBlockAt(step.block);
            InstructionLocation location = step.location;
            boolean started = false;
            for (Instruction insn : block.getInstructions()) {
                if (insn.getLocation() != null) {
                    if (!started) {
                        step.startLocations.add(insn.getLocation());
                    }
                    started = true;
View Full Code Here

*/
public class ProgramIOTest {
    @Test
    public void emptyInstruction() {
        Program program = new Program();
        BasicBlock block = program.createBasicBlock();
        block.getInstructions().add(new EmptyInstruction());

        program = inputOutput(program);
        block = program.basicBlockAt(0);

        assertThat(block.getInstructions().size(), is(1));
        assertThat(block.getInstructions().get(0), instanceOf(EmptyInstruction.class));
    }
View Full Code Here

    }

    @Test
    public void constants() {
        Program program = new Program();
        BasicBlock block = program.createBasicBlock();
        ClassConstantInstruction classConstInsn = new ClassConstantInstruction();
        classConstInsn.setConstant(ValueType.BYTE);
        classConstInsn.setReceiver(program.createVariable());
        block.getInstructions().add(classConstInsn);
        NullConstantInstruction nullConstInsn = new NullConstantInstruction();
        nullConstInsn.setReceiver(program.createVariable());
        block.getInstructions().add(nullConstInsn);
        IntegerConstantInstruction intConsInsn = new IntegerConstantInstruction();
        intConsInsn.setReceiver(program.createVariable());
        intConsInsn.setConstant(23);
        block.getInstructions().add(intConsInsn);
        LongConstantInstruction longConsInsn = new LongConstantInstruction();
        longConsInsn.setReceiver(program.createVariable());
        longConsInsn.setConstant(234);
        block.getInstructions().add(longConsInsn);
        FloatConstantInstruction floatConsInsn = new FloatConstantInstruction();
        floatConsInsn.setReceiver(program.createVariable());
        floatConsInsn.setConstant(3.14f);
        block.getInstructions().add(floatConsInsn);
        DoubleConstantInstruction doubleConsInsn = new DoubleConstantInstruction();
        doubleConsInsn.setReceiver(program.createVariable());
        doubleConsInsn.setConstant(3.14159);
        block.getInstructions().add(doubleConsInsn);
        StringConstantInstruction stringConsInsn = new StringConstantInstruction();
        stringConsInsn.setReceiver(program.createVariable());
        stringConsInsn.setConstant("foo");
        block.getInstructions().add(stringConsInsn);

        program = inputOutput(program);
        block = program.basicBlockAt(0);

        assertThat(block.getInstructions().size(), is(7));
        assertThat(block.getInstructions().get(0), instanceOf(ClassConstantInstruction.class));
        assertThat(block.getInstructions().get(1), instanceOf(NullConstantInstruction.class));
        assertThat(block.getInstructions().get(2), instanceOf(IntegerConstantInstruction.class));
        assertThat(block.getInstructions().get(3), instanceOf(LongConstantInstruction.class));
        assertThat(block.getInstructions().get(4), instanceOf(FloatConstantInstruction.class));
        assertThat(block.getInstructions().get(5), instanceOf(DoubleConstantInstruction.class));
        assertThat(block.getInstructions().get(6), instanceOf(StringConstantInstruction.class));

        classConstInsn = (ClassConstantInstruction)block.getInstructions().get(0);
        assertThat(classConstInsn.getReceiver().getIndex(), is(0));
        assertThat(classConstInsn.getConstant().toString(), is(ValueType.BYTE.toString()));

        nullConstInsn = (NullConstantInstruction)block.getInstructions().get(1);
        assertThat(nullConstInsn.getReceiver().getIndex(), is(1));

        intConsInsn = (IntegerConstantInstruction)block.getInstructions().get(2);
        assertThat(intConsInsn.getConstant(), is(23));
        assertThat(intConsInsn.getReceiver().getIndex(), is(2));

        longConsInsn = (LongConstantInstruction)block.getInstructions().get(3);
        assertThat(longConsInsn.getConstant(), is(234L));
        assertThat(longConsInsn.getReceiver().getIndex(), is(3));

        floatConsInsn = (FloatConstantInstruction)block.getInstructions().get(4);
        assertThat(floatConsInsn.getConstant(), is(3.14f));
        assertThat(floatConsInsn.getReceiver().getIndex(), is(4));

        doubleConsInsn = (DoubleConstantInstruction)block.getInstructions().get(5);
        assertThat(doubleConsInsn.getConstant(), is(3.14159));
        assertThat(doubleConsInsn.getReceiver().getIndex(), is(5));

        stringConsInsn = (StringConstantInstruction)block.getInstructions().get(6);
        assertThat(stringConsInsn.getConstant(), is("foo"));
        assertThat(stringConsInsn.getReceiver().getIndex(), is(6));
    }
View Full Code Here

    public static boolean[] findEscapingVariables(Program program) {
        boolean[] escaping = new boolean[program.variableCount()];
        InstructionAnalyzer analyzer = new InstructionAnalyzer(escaping);
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            BasicBlock block = program.basicBlockAt(i);
            for (Instruction insn : block.getInstructions()) {
                insn.acceptVisitor(analyzer);
            }
        }
        return escaping;
    }
View Full Code Here

        for (int i = 0; i < blockMapping.length; ++i) {
            blockMapping[i] = i;
        }
        int lastNonEmpty = program.basicBlockCount() - 1;
        for (int i = program.basicBlockCount() - 2; i > 0; --i) {
            BasicBlock block = program.basicBlockAt(i);
            if (block.getPhis().isEmpty() && block.getInstructions().size() == 1 &&
                    block.getLastInstruction() instanceof JumpInstruction) {
                JumpInstruction insn = (JumpInstruction)block.getLastInstruction();
                if (insn.getTarget().getIndex() == i + 1) {
                    blockMapping[i] = lastNonEmpty;
                }
            }
            lastNonEmpty = blockMapping[i];
        }
        new BasicBlockMapper() {
            @Override protected BasicBlock map(BasicBlock block) {
                return program.basicBlockAt(blockMapping[block.getIndex()]);
            }
        }.transform(program);
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            if (blockMapping[i] != i) {
                program.deleteBasicBlock(i);
View Full Code Here

            int i = stack.pop();
            if (reachable[i]) {
                continue;
            }
            reachable[i] = true;
            BasicBlock block = program.basicBlockAt(i);
            block.getLastInstruction().acceptVisitor(transitionExtractor);
            for (BasicBlock successor : transitionExtractor.getTargets()) {
                if (!reachable[successor.getIndex()]) {
                    stack.push(successor.getIndex());
                }
            }
            for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
                stack.push(tryCatch.getHandler().getIndex());
            }
        }
        for (int i = 0; i < reachable.length; ++i) {
            if (!reachable[i]) {
View Full Code Here

TOP

Related Classes of org.teavm.model.BasicBlock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.