Search in sources :

Example 26 with IntegerConstant

use of org.robovm.compiler.llvm.IntegerConstant in project robovm by robovm.

the class Functions method pushCallbackFrame.

public static void pushCallbackFrame(Function fn, Value env) {
    Variable gwFrame = fn.newVariable(GATEWAY_FRAME_PTR);
    fn.add(new Alloca(gwFrame, GATEWAY_FRAME));
    Value frameAddress = call(fn, LLVM_FRAMEADDRESS, new IntegerConstant(0));
    call(fn, BC_PUSH_CALLBACK_FRAME, env, gwFrame.ref(), frameAddress);
}
Also used : Variable(org.robovm.compiler.llvm.Variable) Alloca(org.robovm.compiler.llvm.Alloca) Value(org.robovm.compiler.llvm.Value) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant)

Example 27 with IntegerConstant

use of org.robovm.compiler.llvm.IntegerConstant in project robovm by robovm.

the class ITable method getStruct.

public StructureConstant getStruct(ModuleBuilder mb, SootClass clazz) {
    if (clazz.isInterface()) {
        throw new IllegalArgumentException("Expected a class got an interface: " + clazz.getName());
    }
    ArrayConstantBuilder table = new ArrayConstantBuilder(I8_PTR);
    for (Entry entry : entries) {
        ResolvedEntry resolvedEntry = entry.resolve(clazz);
        if (resolvedEntry == null) {
            FunctionRef defaultFunctionRef = entry.getFunctionRef();
            if (defaultFunctionRef != null) {
                if (!mb.hasSymbol(defaultFunctionRef.getName())) {
                    mb.addFunctionDeclaration(new FunctionDeclaration(defaultFunctionRef));
                }
                table.add(new ConstantBitcast(defaultFunctionRef, I8_PTR));
            } else {
                table.add(new ConstantBitcast(BC_ABSTRACT_METHOD_CALLED, I8_PTR));
            }
        } else if (Modifier.isAbstract(resolvedEntry.getModifiers())) {
            table.add(new ConstantBitcast(BC_ABSTRACT_METHOD_CALLED, I8_PTR));
        } else if (!Modifier.isPublic(resolvedEntry.getModifiers())) {
            table.add(new ConstantBitcast(BC_NON_PUBLIC_METHOD_CALLED, I8_PTR));
        } else {
            /*
                 * Found a non-abstract method implementation. Either on the
                 * class, in one of its super classes or a default method in an
                 * implemented interface.
                 */
            FunctionRef functionRef = resolvedEntry.getFunctionRef();
            if (!resolvedEntry.declaringClass.equals(clazz.getName())) {
                if (!mb.hasSymbol(functionRef.getName())) {
                    mb.addFunctionDeclaration(new FunctionDeclaration(functionRef));
                }
            }
            table.add(new ConstantBitcast(functionRef, I8_PTR));
        }
    }
    return new StructureConstantBuilder().add(new IntegerConstant((short) entries.length)).add(table.build()).build();
}
Also used : ArrayConstantBuilder(org.robovm.compiler.llvm.ArrayConstantBuilder) FunctionDeclaration(org.robovm.compiler.llvm.FunctionDeclaration) ConstantBitcast(org.robovm.compiler.llvm.ConstantBitcast) StructureConstantBuilder(org.robovm.compiler.llvm.StructureConstantBuilder) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) FunctionRef(org.robovm.compiler.llvm.FunctionRef)

Example 28 with IntegerConstant

use of org.robovm.compiler.llvm.IntegerConstant in project robovm by robovm.

the class NativeMethodCompiler method createNative.

private FunctionRef createNative(ModuleBuilder mb, SootMethod method) {
    String targetInternalName = getInternalName(method.getDeclaringClass());
    String methodName = method.getName();
    String methodDesc = getDescriptor(method);
    FunctionType nativeFunctionType = Types.getNativeFunctionType(methodDesc, method.isStatic());
    String shortName = mangleNativeMethod(targetInternalName, methodName);
    String longName = mangleNativeMethod(targetInternalName, methodName, methodDesc);
    /*
         * To support statically linked native method implementation we create
         * weak stub functions with the same names as the expected JNI functions
         * (long and short names). These will be discarded by the linker if
         * proper functions are available at link time.
         * 
         * The weak stub with the short JNI name just calls the weak stub with
         * the long name.
         * 
         * The weak stub with the long name calls _bcResolveNative() which will
         * try to resolve the native method against dynamically loaded JNI libs.
         * If _bcResolveNative() finds a matching symbol in a dynamic lib or an
         * implementation has previously been registered using JNI
         * RegisterNatives() that will be stored in the native method pointer
         * passed to it and returned. The stub will call the implementation
         * returned by _bcResolveNative(). If no implementation can be found
         * _bcResolveNative() throws an UnsatisfiedLinkError and doesn't return
         * to the stub.
         * 
         * The limitation of this approach is that RegisterNatives() only works
         * for dynamically linked native methods and can only be used prior to
         * the first call of such a method. Native methods can never be rewired
         * or unregistered.
         */
    /*
         * The function with the long JNI name. This is the one that calls
         * _bcResolveNative() and then calls the implementation.
         */
    Function fn = new FunctionBuilder(longName, nativeFunctionType).linkage(weak).build();
    Global g = new Global(Symbols.nativeMethodPtrSymbol(targetInternalName, methodName, methodDesc), new NullConstant(I8_PTR));
    mb.addGlobal(g);
    FunctionRef ldcFn = FunctionBuilder.ldcInternal(targetInternalName).ref();
    Value theClass = call(fn, ldcFn, fn.getParameterRef(0));
    Value implI8Ptr = call(fn, BC_RESOLVE_NATIVE, fn.getParameterRef(0), theClass, mb.getString(methodName), mb.getString(methodDesc), mb.getString(shortName), mb.getString(longName), g.ref());
    Variable nullTest = fn.newVariable(I1);
    fn.add(new Icmp(nullTest, Condition.ne, implI8Ptr, new NullConstant(I8_PTR)));
    Label trueLabel = new Label();
    Label falseLabel = new Label();
    fn.add(new Br(nullTest.ref(), fn.newBasicBlockRef(trueLabel), fn.newBasicBlockRef(falseLabel)));
    fn.newBasicBlock(falseLabel);
    if (fn.getType().getReturnType() instanceof IntegerType) {
        fn.add(new Ret(new IntegerConstant(0, (IntegerType) fn.getType().getReturnType())));
    } else if (fn.getType().getReturnType() instanceof FloatingPointType) {
        fn.add(new Ret(new FloatingPointConstant(0.0, (FloatingPointType) fn.getType().getReturnType())));
    } else if (fn.getType().getReturnType() instanceof PointerType) {
        fn.add(new Ret(new NullConstant((PointerType) fn.getType().getReturnType())));
    } else {
        fn.add(new Ret());
    }
    fn.newBasicBlock(trueLabel);
    Variable impl = fn.newVariable(nativeFunctionType);
    fn.add(new Bitcast(impl, implI8Ptr, impl.getType()));
    Value result = call(fn, impl.ref(), fn.getParameterRefs());
    fn.add(new Ret(result));
    mb.addFunction(fn);
    FunctionRef targetFn = fn.ref();
    if (!isLongNativeFunctionNameRequired(method)) {
        /*
             * Generate a function with the short JNI name. This just calls the
             * function with the long name.
             */
        Function fnShort = new FunctionBuilder(shortName, nativeFunctionType).linkage(weak).build();
        Value resultInner = call(fnShort, fn.ref(), fnShort.getParameterRefs());
        fnShort.add(new Ret(resultInner));
        mb.addFunction(fnShort);
        targetFn = fnShort.ref();
    }
    return targetFn;
}
Also used : Ret(org.robovm.compiler.llvm.Ret) Variable(org.robovm.compiler.llvm.Variable) FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) FunctionType(org.robovm.compiler.llvm.FunctionType) Label(org.robovm.compiler.llvm.Label) NullConstant(org.robovm.compiler.llvm.NullConstant) PointerType(org.robovm.compiler.llvm.PointerType) FloatingPointType(org.robovm.compiler.llvm.FloatingPointType) Global(org.robovm.compiler.llvm.Global) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) Br(org.robovm.compiler.llvm.Br) IntegerType(org.robovm.compiler.llvm.IntegerType) Function(org.robovm.compiler.llvm.Function) Bitcast(org.robovm.compiler.llvm.Bitcast) Value(org.robovm.compiler.llvm.Value) FunctionRef(org.robovm.compiler.llvm.FunctionRef) Icmp(org.robovm.compiler.llvm.Icmp)

Example 29 with IntegerConstant

use of org.robovm.compiler.llvm.IntegerConstant in project robovm by robovm.

the class HashTableGenerator method generate.

public StructureConstant generate() {
    StructureConstantBuilder builder = new StructureConstantBuilder();
    int start = 0;
    builder.add(new IntegerConstant(count));
    builder.add(new IntegerConstant(table.length, INDEX_TYPE));
    builder.add(new IntegerConstant(start, INDEX_TYPE));
    for (int i = 1; i <= table.length; i++) {
        if (table[i - 1] == null) {
            builder.add(new IntegerConstant(start, INDEX_TYPE));
        } else {
            start += table[i - 1].size();
            builder.add(new IntegerConstant(start, INDEX_TYPE));
        }
    }
    for (int i = 0; i < table.length; i++) {
        if (table[i] != null) {
            for (Entry<K, V> entry : table[i]) {
                builder.add(entry.v);
            }
        }
    }
    return builder.build();
}
Also used : StructureConstantBuilder(org.robovm.compiler.llvm.StructureConstantBuilder) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant)

Example 30 with IntegerConstant

use of org.robovm.compiler.llvm.IntegerConstant in project robovm by robovm.

the class MethodCompiler method initializeClassFields.

private void initializeClassFields() {
    for (SootField field : sootMethod.getDeclaringClass().getFields()) {
        if (!field.isStatic()) {
            continue;
        }
        for (Tag tag : field.getTags()) {
            Value value = null;
            if (tag instanceof DoubleConstantValueTag) {
                DoubleConstantValueTag dtag = (DoubleConstantValueTag) tag;
                value = new FloatingPointConstant(dtag.getDoubleValue());
            } else if (tag instanceof FloatConstantValueTag) {
                FloatConstantValueTag ftag = (FloatConstantValueTag) tag;
                value = new FloatingPointConstant(ftag.getFloatValue());
            } else if (tag instanceof IntegerConstantValueTag) {
                IntegerConstantValueTag itag = (IntegerConstantValueTag) tag;
                value = new IntegerConstant(itag.getIntValue());
                IntegerType type = (IntegerType) getType(field.getType());
                if (type.getBits() < 32) {
                    value = new ConstantTrunc((Constant) value, type);
                }
            } else if (tag instanceof LongConstantValueTag) {
                LongConstantValueTag ltag = (LongConstantValueTag) tag;
                value = new IntegerConstant(ltag.getLongValue());
            } else if (tag instanceof StringConstantValueTag) {
                String s = ((StringConstantValueTag) tag).getStringValue();
                value = call(ldcString(s), env);
            }
            if (value != null) {
                FunctionRef fn = FunctionBuilder.setter(field).ref();
                call(fn, env, value);
            }
        }
    }
}
Also used : FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) FloatingPointConstant(org.robovm.compiler.llvm.FloatingPointConstant) Constant(org.robovm.compiler.llvm.Constant) NullConstant(org.robovm.compiler.llvm.NullConstant) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) IntegerConstant(org.robovm.compiler.llvm.IntegerConstant) IntegerType(org.robovm.compiler.llvm.IntegerType) ConstantTrunc(org.robovm.compiler.llvm.ConstantTrunc) Value(org.robovm.compiler.llvm.Value) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SootField(soot.SootField) ArrayCheckTag(soot.jimple.toolkits.annotation.tags.ArrayCheckTag) NullCheckTag(soot.jimple.toolkits.annotation.tags.NullCheckTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) Tag(soot.tagkit.Tag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) FunctionRef(org.robovm.compiler.llvm.FunctionRef)

Aggregations

IntegerConstant (org.robovm.compiler.llvm.IntegerConstant)39 Value (org.robovm.compiler.llvm.Value)24 Variable (org.robovm.compiler.llvm.Variable)14 NullConstant (org.robovm.compiler.llvm.NullConstant)11 ConstantBitcast (org.robovm.compiler.llvm.ConstantBitcast)10 FunctionRef (org.robovm.compiler.llvm.FunctionRef)10 ArrayList (java.util.ArrayList)9 Function (org.robovm.compiler.llvm.Function)9 Ret (org.robovm.compiler.llvm.Ret)9 Constant (org.robovm.compiler.llvm.Constant)8 FunctionType (org.robovm.compiler.llvm.FunctionType)8 Global (org.robovm.compiler.llvm.Global)8 PointerType (org.robovm.compiler.llvm.PointerType)8 StructureConstant (org.robovm.compiler.llvm.StructureConstant)8 StructureConstantBuilder (org.robovm.compiler.llvm.StructureConstantBuilder)8 Invokestatic (org.robovm.compiler.trampoline.Invokestatic)8 Bitcast (org.robovm.compiler.llvm.Bitcast)6 Label (org.robovm.compiler.llvm.Label)6 Load (org.robovm.compiler.llvm.Load)6 Type (org.robovm.compiler.llvm.Type)6