Search in sources :

Example 11 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class MethodWriterImpl method visitMultiANewArrayInsn.

@Override
public void visitMultiANewArrayInsn(IType type, int dims) throws BytecodeException {
    this.insnCallback();
    if (dims == 1) {
        final ArrayType arrayType = type.extract(ArrayType.class);
        final IType elementType = arrayType.getElementType();
        if (elementType.isPrimitive()) {
            this.visitIntInsn(Opcodes.NEWARRAY, getNewArrayCode(elementType.getTypecode()));
            return;
        }
        this.visitTypeInsn(Opcodes.ANEWARRAY, elementType.getInternalName());
        return;
    }
    final String extended = type.getExtendedName();
    this.frame.visitNewArray(extended, dims);
    this.mv.visitMultiANewArrayInsn(extended, dims);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType)

Example 12 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class CaseClasses method writeStringAppend.

public static void writeStringAppend(MethodWriter writer, IType type) throws BytecodeException {
    // Write the call to the StringBuilder#append() method that
    // corresponds to the type of the field
    writer.visitLineNumber(0);
    final ArrayType arrayType = (ArrayType) type.extract(ArrayType.class);
    if (arrayType != null) {
        writer.visitInsn(Opcodes.SWAP);
        writer.visitInsn(Opcodes.DUP_X1);
        writeArrayStringAppend(writer, arrayType.getElementType());
        return;
    }
    StringBuilder desc = new StringBuilder().append('(');
    if (type.isPrimitive()) {
        type.appendExtendedName(desc);
    } else if (Types.isExactType(type, Types.STRING)) {
        desc.append("Ljava/lang/String;");
    } else {
        desc.append("Ljava/lang/Object;");
    }
    desc.append(")Ljava/lang/StringBuilder;");
    writer.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", desc.toString(), false);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType)

Example 13 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class CaseClasses method writeIFNE.

public static void writeIFNE(MethodWriter writer, IType type, Label falseLabel) throws BytecodeException {
    if (type.isPrimitive()) {
        switch(type.getTypecode()) {
            case PrimitiveType.BOOLEAN_CODE:
            case PrimitiveType.BYTE_CODE:
            case PrimitiveType.SHORT_CODE:
            case PrimitiveType.CHAR_CODE:
            case PrimitiveType.INT_CODE:
                writer.visitJumpInsn(IF_ICMPNE, falseLabel);
                break;
            case PrimitiveType.LONG_CODE:
                writer.visitJumpInsn(IF_LCMPNE, falseLabel);
                break;
            case PrimitiveType.FLOAT_CODE:
                writer.visitJumpInsn(IF_FCMPNE, falseLabel);
                break;
            case PrimitiveType.DOUBLE_CODE:
                writer.visitJumpInsn(IF_DCMPNE, falseLabel);
                break;
        }
        return;
    }
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType != null) {
        writeArrayEquals(writer, arrayType.getElementType());
    } else if (!NullableType.isNullable(type)) {
        writer.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
    } else {
        writer.visitMethodInsn(INVOKESTATIC, "dyvil/lang/Objects", "equals", "(Ljava/lang/Object;Ljava/lang/Object;)Z", false);
    }
    writer.visitJumpInsn(IFEQ, falseLabel);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType)

Example 14 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class CaseClasses method writeHashCode.

public static void writeHashCode(MethodWriter writer, IType type) throws BytecodeException {
    if (type.isPrimitive()) {
        switch(type.getTypecode()) {
            case PrimitiveType.BOOLEAN_CODE:
                {
                    // Write boolean hashing by using 1231 if the value is true and
                    // 1237 if the value is false
                    Label elseLabel = new Label();
                    Label endLabel = new Label();
                    // if
                    writer.visitJumpInsn(IFEQ, elseLabel);
                    // then
                    writer.visitLdcInsn(1231);
                    writer.visitJumpInsn(GOTO, endLabel);
                    // else
                    writer.visitLabel(elseLabel);
                    writer.visitLdcInsn(1237);
                    writer.visitLabel(endLabel);
                    return;
                }
            case PrimitiveType.BYTE_CODE:
                return;
            case PrimitiveType.SHORT_CODE:
                return;
            case PrimitiveType.CHAR_CODE:
                return;
            case PrimitiveType.INT_CODE:
                return;
            case PrimitiveType.LONG_CODE:
                // Write a long hashing snippet by XORing the value by the value
                // bit-shifted 32 bits to the right, and then converting the
                // result to an integer. l1 = (int) (l ^ (l >>> 32))
                writer.visitInsn(DUP2);
                writer.visitLdcInsn(32);
                writer.visitInsn(LUSHR);
                writer.visitInsn(LOR);
                writer.visitInsn(L2I);
                return;
            case PrimitiveType.FLOAT_CODE:
                // Write a float hashing snippet using Float.floatToIntBits
                writer.visitLineNumber(0);
                writer.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "floatToIntBits", "(F)I", false);
                return;
            case PrimitiveType.DOUBLE_CODE:
                // Write a double hashing snippet using Double.doubleToLongBits
                // and long hashing
                writer.visitLineNumber(0);
                writer.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "doubleToLongBits", "(D)J", false);
                writer.visitInsn(DUP2);
                writer.visitLdcInsn(32);
                writer.visitInsn(LUSHR);
                writer.visitInsn(LOR);
                writer.visitInsn(L2I);
                return;
        }
    }
    Label elseLabel = new Label();
    Label endLabel = new Label();
    // Write an Object hashing snippet
    // if
    writer.visitInsn(DUP);
    writer.visitJumpInsn(IFNULL, elseLabel);
    // then
    writer.visitLineNumber(0);
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType != null) {
        writeArrayHashCode(writer, arrayType.getElementType());
    } else {
        writer.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I", false);
    }
    writer.visitJumpInsn(GOTO, endLabel);
    // else
    writer.visitLabel(elseLabel);
    writer.visitInsn(POP);
    writer.visitLdcInsn(0);
    writer.visitLabel(endLabel);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) Label(dyvilx.tools.asm.Label)

Example 15 with ArrayType

use of dyvilx.tools.compiler.ast.type.compound.ArrayType in project Dyvil by Dyvil.

the class ConstructorCall method resolveCall.

@Override
public IValue resolveCall(MarkerList markers, IContext context, boolean report) {
    if (!this.type.isResolved()) {
        return this;
    }
    final ArrayType arrayType = this.type.extract(ArrayType.class);
    if (arrayType != null) {
        this.resolveArrayConstructor(markers, context, arrayType);
        return this;
    }
    final IClass theClass = this.type.getTheClass();
    if (theClass != null && theClass.isInterface()) {
        if (!report) {
            return null;
        }
        markers.add(Markers.semanticError(this.position, "constructor.access.interface", this.type));
        return this;
    }
    final MatchList<IConstructor> candidates = this.resolveCandidates(context, this.type);
    if (candidates.hasCandidate()) {
        this.checkArguments(markers, context, candidates.getBestMember());
        return this;
    }
    if (report) {
        reportResolve(markers, candidates, this.position, this.type, this.arguments);
        return this;
    }
    return null;
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IConstructor(dyvilx.tools.compiler.ast.constructor.IConstructor) IClass(dyvilx.tools.compiler.ast.classes.IClass)

Aggregations

ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)15 IType (dyvilx.tools.compiler.ast.type.IType)6 SourcePosition (dyvil.source.position.SourcePosition)2 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)2 IValue (dyvilx.tools.compiler.ast.expression.IValue)2 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)2 Label (dyvilx.tools.asm.Label)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 IClass (dyvilx.tools.compiler.ast.classes.IClass)1 IConstructor (dyvilx.tools.compiler.ast.constructor.IConstructor)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 Field (dyvilx.tools.compiler.ast.field.Field)1 IField (dyvilx.tools.compiler.ast.field.IField)1 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)1 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)1 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)1 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)1 Mutability (dyvilx.tools.compiler.ast.type.Mutability)1 Marker (dyvilx.tools.parsing.marker.Marker)1