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);
}
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();
}
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;
}
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();
}
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);
}
}
}
}
Aggregations