コード例 #1
0
 static {
   if (!BridJ.Switch.StructsByValue.enabled)
     BridJ.warning(
         "Structs by value are not enabled (see "
             + BridJ.Switch.StructsByValue.getFullDescription()
             + ")");
   else BridJ.register();
 }
コード例 #2
0
  static {
    {
      OpenCLProbeLibrary probe = new OpenCLProbeLibrary();
      try {
        if (!probe.isValid()) {
          String alt;
          if (Platform.is64Bits() && BridJ.getNativeLibraryFile(alt = "atiocl64") != null
              || BridJ.getNativeLibraryFile(alt = "atiocl32") != null
              || BridJ.getNativeLibraryFile(alt = "atiocl") != null) {
            log(
                Level.INFO,
                "Hacking around ATI's weird driver bugs (using atiocl library instead of OpenCL)",
                null);
            BridJ.setNativeLibraryActualName("OpenCL", alt);
          }
        }
      } finally {
        probe = null;
        BridJ.unregister(OpenCLProbeLibrary.class);
      }
    }

    if (debug) {
      String debugArgs = System.getenv(JAVACL_DEBUG_COMPILER_FLAGS_PROP);
      if (debugArgs != null) DEBUG_COMPILER_FLAGS = Arrays.asList(debugArgs.split(" "));
      else if (Platform.isMacOSX()) DEBUG_COMPILER_FLAGS = Arrays.asList("-g");
      else DEBUG_COMPILER_FLAGS = Arrays.asList("-O0", "-g");

      int pid = ProcessUtils.getCurrentProcessId();
      log(
          Level.INFO,
          "Debug mode enabled with compiler flags \""
              + StringUtils.implode(DEBUG_COMPILER_FLAGS, " ")
              + "\" (can be overridden with env. var. JAVACL_DEBUG_COMPILER_FLAGS_PROP)");
      log(
          Level.INFO,
          "You can debug your kernels with GDB using one of the following commands :\n"
              + "\tsudo gdb --tui --pid="
              + pid
              + "\n"
              + "\tsudo ddd --debugger \"gdb --pid="
              + pid
              + "\"\n"
              + "More info here :\n"
              + "\thttp://code.google.com/p/javacl/wiki/DebuggingKernels");
    }
    CL = new OpenCLLibrary();
  }
コード例 #3
0
  public void setCallingConvention(Convention.Style style) {
    if (style == null) return;

    if (!Platform.isWindows() || Platform.is64Bits()) return;

    switch (style) {
      case FastCall:
        this.direct = false;
        setDcCallingConvention(
            Platform.isWindows()
                ? DC_CALL_C_X86_WIN32_FAST_MS
                : DC_CALL_C_DEFAULT); // TODO allow GCC-compiled C++ libs on windows
        break;
      case Pascal:
      case StdCall:
        this.direct = false;
        setDcCallingConvention(DC_CALL_C_X86_WIN32_STD);
        break;
      case ThisCall:
        this.direct = false;
        setDcCallingConvention(
            Platform.isWindows() ? DC_CALL_C_X86_WIN32_THIS_MS : DC_CALL_C_DEFAULT);
    }
    if (BridJ.veryVerbose)
      BridJ.info("Setting CC " + style + " (-> " + dcCallingConvention + ") for " + methodName);
  }
コード例 #4
0
  /**
   * Used for C++ virtual indexes and for struct fields ids
   *
   * @param virtualIndex
   */
  public void setVirtualIndex(int virtualIndex) {
    // new RuntimeException("Setting virtualIndex of " + getMethod().getName() + " = " +
    // virtualIndex).printStackTrace();
    this.virtualIndex = virtualIndex;

    if (BridJ.veryVerbose) {
      BridJ.info("\t-> virtualIndex " + virtualIndex);
    }
  }
コード例 #5
0
  // @Ignore
  @Test
  public void testIncrInt() {
    if (!BridJ.Switch.StructsByValue.enabled) return;

    int value = 12345;
    S_int s = new S_int();
    s.a = value;
    BridJ.writeToNative(s);
    assertEquals(value + 1, incr(s));
  }
コード例 #6
0
 LastError(int code, String description) {
   super(
       (description == null ? "?" : description)
           + " (error code = "
           + code
           + ")"); // toString(code));
   this.code = code;
   this.description = description;
   if (BridJ.verbose) BridJ.log(Level.INFO, "Last error detected : " + getMessage());
 }
コード例 #7
0
  @Test
  public void testIncrLong10() {
    if (!BridJ.Switch.StructsByValue.enabled) return;

    long tot = 0;
    S_jlong10 s = new S_jlong10();
    for (int i = 0; i < 10; i++) {
      long v = i + 1;
      tot += v;
      s.a.set(i, v);
    }
    BridJ.writeToNative(s);
    assertEquals(tot, sum(s));
  }
コード例 #8
0
  @Ignore
  @Test
  public void testIncrLong4() {
    if (!BridJ.Switch.StructsByValue.enabled) return;

    long value = 12345;
    S_jlong4 s = new S_jlong4();
    s.a = 10;
    s.b = 100;
    s.c = 1000;
    s.d = 10000;
    BridJ.writeToNative(s);
    assertEquals(s.a + s.b + s.c + s.d, sum(s));
  }
コード例 #9
0
  public MethodCallInfo(Method method, Method definition) {
    this.setMethod(method);
    // this.setDefinition(definition);
    this.setDeclaringClass(method.getDeclaringClass());
    symbolName = methodName;

    int modifiers = method.getModifiers();
    isStatic = Modifier.isStatic(modifiers);
    isVarArgs = method.isVarArgs();
    boolean isNative = Modifier.isNative(modifiers);
    boolean isVirtual = isAnnotationPresent(Virtual.class, definition);
    boolean isDirectModeAllowed =
        getInheritableAnnotation(DisableDirect.class, definition) == null
            && BridJ.isDirectModeEnabled();

    isCPlusPlus = !isStatic && derivesFrom(method.getDeclaringClass(), "org.bridj.cpp.CPPObject");
    isObjCBlock = !isStatic && derivesFrom(method.getDeclaringClass(), "org.bridj.objc.ObjCBlock");

    init(
        method,
        method.getReturnType(),
        method.getGenericReturnType(),
        method.getAnnotations(),
        method.getParameterTypes(),
        method.getGenericParameterTypes(),
        method.getParameterAnnotations(),
        isNative,
        isVirtual,
        isDirectModeAllowed);

    Convention cc = getInheritableAnnotation(Convention.class, definition);
    if (cc != null) {
      setCallingConvention(cc.value());
    }
    List<Class<?>> exceptionTypes = Arrays.asList(definition.getExceptionTypes());
    if (!exceptionTypes.isEmpty()) {
      this.direct = false; // there is no crash / exception protection for direct raw calls
      if (exceptionTypes.contains(LastError.class)) this.bThrowLastError = true;
    }
  }
コード例 #10
0
 static {
   BridJ.register();
 }
コード例 #11
0
  public ValueType getValueType(
      int iParam,
      int nParams,
      Class<?> c,
      Type t,
      AnnotatedElement element,
      Annotation... directAnnotations) {
    boolean isPtr = isAnnotationPresent(Ptr.class, element, directAnnotations);
    boolean isCLong = isAnnotationPresent(org.bridj.ann.CLong.class, element, directAnnotations);
    Constructor cons = getAnnotation(Constructor.class, element, directAnnotations);

    if (isPtr || cons != null || isCLong) {
      if (!(c == Long.class || c == Long.TYPE))
        throw new RuntimeException(
            "Annotation should only be used on a long parameter, not on a " + c.getName());

      if (isPtr) {
        if (!Platform.is64Bits()) direct = false;
      } else if (isCLong) {
        if (Platform.CLONG_SIZE != 8) direct = false;
      } else if (cons != null) {
        isCPlusPlus = true;
        startsWithThis = true;
        if (iParam != 0)
          throw new RuntimeException(
              "Annotation "
                  + Constructor.class.getName()
                  + " cannot have more than one (long) argument");
      }
      return ValueType.eSizeTValue;
    }
    if (c == null || c.equals(Void.TYPE)) return ValueType.eVoidValue;
    if (c == Integer.class || c == Integer.TYPE) return ValueType.eIntValue;
    if (c == Long.class || c == Long.TYPE) {
      return !isPtr || Platform.is64Bits() ? ValueType.eLongValue : ValueType.eIntValue;
    }
    if (c == Short.class || c == Short.TYPE) return ValueType.eShortValue;
    if (c == Byte.class || c == Byte.TYPE) return ValueType.eByteValue;
    if (c == Boolean.class || c == Boolean.TYPE) return ValueType.eBooleanValue;
    if (c == Float.class || c == Float.TYPE) {
      usesFloats();
      return ValueType.eFloatValue;
    }
    if (c == char.class || c == Character.TYPE) {
      if (Platform.WCHAR_T_SIZE != 2) direct = false;
      return ValueType.eWCharValue;
    }
    if (c == Double.class || c == Double.TYPE) {
      usesFloats();
      return ValueType.eDoubleValue;
    }
    if (c == CLong.class) {
      direct = false;
      return ValueType.eCLongObjectValue;
    }
    if (c == SizeT.class) {
      direct = false;
      return ValueType.eSizeTObjectValue;
    }
    if (c == TimeT.class) {
      direct = false;
      return ValueType.eTimeTObjectValue;
    }
    if (Pointer.class.isAssignableFrom(c)) {
      direct = false;
      CallIO cio = CallIO.Utils.createPointerCallIO(c, t);
      if (BridJ.veryVerbose) BridJ.info("CallIO : " + cio);
      addCallIO(cio);
      return ValueType.ePointerValue;
    }
    if (c.isArray() && iParam == nParams - 1) {
      direct = false;
      return ValueType.eEllipsis;
    }
    if (ValuedEnum.class.isAssignableFrom(c)) {
      direct = false;
      CallIO cio =
          CallIO.Utils.createValuedEnumCallIO(
              (Class) Utils.getClass(Utils.getUniqueParameterizedTypeParameter(t)));
      if (BridJ.veryVerbose) BridJ.info("CallIO : " + cio);
      addCallIO(cio);

      return ValueType.eIntFlagSet;
    }
    if (NativeObject.class.isAssignableFrom(c)) {
      Pointer<DCstruct> pStruct = null;
      if (StructObject.class.isAssignableFrom(c)) {
        StructIO io = StructIO.getInstance(c, t);
        try {
          pStruct = DyncallStructs.buildDCstruct(io.desc);
        } catch (Throwable th) {
          BridJ.error(
              "Unable to create low-level struct metadata for "
                  + Utils.toString(t)
                  + " : won't be able to use it as a by-value function argument.",
              th);
        }
      }
      addCallIO(new CallIO.NativeObjectHandler((Class<? extends NativeObject>) c, t, pStruct));
      direct = false;
      return ValueType.eNativeObjectValue;
    }

    throw new NoSuchElementException(
        "No " + ValueType.class.getSimpleName() + " for class " + c.getName());
  }
コード例 #12
0
  protected void init(
      AnnotatedElement annotatedElement,
      Class returnType,
      Type genericReturnType,
      Annotation[] returnAnnotations,
      Class[] parameterTypes,
      Type[] genericParameterTypes,
      Annotation[][] paramsAnnotations,
      boolean prependJNIPointers,
      boolean isVirtual,
      boolean isDirectModeAllowed) {
    assert returnType != null;
    assert genericReturnType != null;
    assert parameterTypes != null;
    assert genericParameterTypes != null;
    assert returnAnnotations != null;
    assert parameterTypes.length == genericParameterTypes.length;
    assert paramsAnnotations.length == genericParameterTypes.length;

    int nParams = genericParameterTypes.length;
    paramsValueTypes = new int[nParams];

    direct =
        isDirectModeAllowed; // TODO on native side : test number of parameters (on 64 bits win :
                             // must be <= 4)

    StringBuilder javaSig = new StringBuilder(64),
        asmSig = new StringBuilder(64),
        dcSig = new StringBuilder(16);
    javaSig.append('(');
    asmSig.append('(');
    if (prependJNIPointers) // !isCPlusPlus)
    dcSig
          .append(DC_SIGCHAR_POINTER)
          .append(DC_SIGCHAR_POINTER); // JNIEnv*, jobject: always present in native-bound functions

    if (BridJ.debug)
      BridJ.info(
          "Analyzing "
              + (declaringClass == null
                  ? "anonymous method"
                  : declaringClass.getName() + "." + methodName));

    if (isObjCBlock)
      appendToSignature(
          0, ValueType.ePointerValue, Pointer.class, Pointer.class, null, dcSig, null);

    for (int iParam = 0; iParam < nParams; iParam++) {
      //            Options paramOptions = paramsOptions[iParam] = new Options();
      Type genericParameterType = genericParameterTypes[iParam];
      Class<?> parameterType = parameterTypes[iParam];

      ValueType paramValueType =
          getValueType(
              iParam,
              nParams,
              parameterType,
              genericParameterType,
              null,
              paramsAnnotations[iParam]);
      if (BridJ.veryVerbose) BridJ.info("\tparam " + paramValueType);
      paramsValueTypes[iParam] = paramValueType.ordinal();

      appendToSignature(
          iParam, paramValueType, parameterType, genericParameterType, javaSig, dcSig, asmSig);
    }
    javaSig.append(')');
    asmSig.append(')');
    dcSig.append(')');

    ValueType retType =
        getValueType(
            -1, nParams, returnType, genericReturnType, annotatedElement, returnAnnotations);
    if (BridJ.veryVerbose) BridJ.info("\treturns " + retType);
    appendToSignature(-1, retType, returnType, genericReturnType, javaSig, dcSig, asmSig);
    returnValueType = retType.ordinal();

    javaSignature = javaSig.toString();
    asmSignature = asmSig.toString();
    dcSignature = dcSig.toString();

    isCPlusPlus = isCPlusPlus || isVirtual;

    if (isCPlusPlus && !isStatic) {
      if (!startsWithThis) direct = false;
      bNeedsThisPointer = true;
      if (Platform.isWindows()) {
        if (!Platform.is64Bits()) setDcCallingConvention(DC_CALL_C_X86_WIN32_THIS_MS);
      } else {
        // if (!Platform.is64Bits())
        //	setDcCallingConvention(DC_CALL_C_X86_WIN32_THIS_GNU);
      }
    }

    if (nParams > Platform.getMaxDirectMappingArgCount()) this.direct = false;

    if (BridJ.veryVerbose) {
      BridJ.info("\t-> direct " + direct);
      BridJ.info("\t-> javaSignature " + javaSignature);
      BridJ.info("\t-> callIOs " + callIOs);
      BridJ.info("\t-> asmSignature " + asmSignature);
      BridJ.info("\t-> dcSignature " + dcSignature);
    }

    if (BridJ.veryVerbose)
      BridJ.info((direct ? "[mappable as direct] " : "[not mappable as direct] ") + method);
  }
コード例 #13
0
 static {
   BridJ.setNativeLibraryActualName("OpenCLProbe", "OpenCL");
   BridJ.register();
 }