Beispiel #1
0
  DynamicMethod createMethod(
      RubyModule module, Function function, Type returnType, Type[] parameterTypes) {

    FastIntMethodFactory factory = this;
    IntParameterConverter[] parameterConverters = new IntParameterConverter[parameterTypes.length];
    IntResultConverter resultConverter = factory.getIntResultConverter(returnType);
    for (int i = 0; i < parameterConverters.length; ++i) {
      parameterConverters[i] = factory.getIntParameterConverter(parameterTypes[i]);
    }

    switch (parameterTypes.length) {
      case 0:
        return new FastIntMethodZeroArg(module, function, resultConverter, parameterConverters);
      case 1:
        return new FastIntMethodOneArg(module, function, resultConverter, parameterConverters);
      case 2:
        return new FastIntMethodTwoArg(module, function, resultConverter, parameterConverters);
      case 3:
        return new FastIntMethodThreeArg(module, function, resultConverter, parameterConverters);
      default:
        throw module
            .getRuntime()
            .newRuntimeError("Arity " + parameterTypes.length + " not implemented");
    }
  }
Beispiel #2
0
 protected void init(RubyModule implementationClass, Visibility visibility) {
   this.visibility = visibility;
   this.implementationClass = implementationClass;
   // TODO: Determine whether we should perhaps store non-singleton class
   // in the implementationClass
   this.protectedClass = calculateProtectedClass(implementationClass);
   this.serialNumber = implementationClass.getRuntime().getNextDynamicMethodSerial();
 }
Beispiel #3
0
  public MixedModeIRMethod(IRScope method, Visibility visibility, RubyModule implementationClass) {
    super(implementationClass, visibility, CallConfiguration.FrameNoneScopeNone, method.getName());
    this.method = method;
    getStaticScope().determineModule();
    this.signature = getStaticScope().getSignature();

    // disable JIT if JIT is disabled
    if (!implementationClass.getRuntime().getInstanceConfig().getCompileMode().shouldJIT()) {
      this.box.callCount = -1;
    }
  }
    public void populate(final RubyModule target, final Class clazz) {
      assert clazz == this.clazz : "populator for " + this.clazz + " used for " + clazz;

      // fallback on non-pregenerated logic
      final Ruby runtime = target.getRuntime();
      final MethodFactory methodFactory =
          MethodFactory.createFactory(runtime.getJRubyClassLoader());

      for (Map.Entry<String, List<JavaMethodDescriptor>> entry :
          clumper.getStaticAnnotatedMethods().entrySet()) {
        final String name = entry.getKey();
        final List<JavaMethodDescriptor> methods = entry.getValue();
        target.defineAnnotatedMethod(name, methods, methodFactory);
        addBoundMethodsUnlessOmited(runtime, name, methods);
      }

      for (Map.Entry<String, List<JavaMethodDescriptor>> entry :
          clumper.getAnnotatedMethods().entrySet()) {
        final String name = entry.getKey();
        final List<JavaMethodDescriptor> methods = entry.getValue();
        target.defineAnnotatedMethod(name, methods, methodFactory);
        addBoundMethodsUnlessOmited(runtime, name, methods);
      }
    }
Beispiel #5
0
  private static void setupMakefileConfig(RubyModule configModule, RubyHash mkmfHash) {
    Ruby ruby = configModule.getRuntime();

    RubyHash envHash = (RubyHash) ruby.getObject().fetchConstant("ENV".intern());
    String cc = getRubyEnv(envHash, "CC", "cc");
    String cpp = getRubyEnv(envHash, "CPP", "cc -E");
    String cxx = getRubyEnv(envHash, "CXX", "c++");

    String jflags = " -fno-omit-frame-pointer -fno-strict-aliasing ";
    // String oflags = " -O2  -DNDEBUG";
    // String wflags = " -W -Werror -Wall -Wno-unused -Wno-parentheses ";
    // String picflags = true ? "" : " -fPIC -pthread ";
    // String iflags = " -I\"$(JDK_HOME)/include\" -I\"$(JDK_HOME)/include/$(OS)\"
    // -I\"$(BUILD_DIR)\" ";
    // String soflags = true ? "" : " -shared -static-libgcc -mimpure-text -Wl,-O1 ";

    String cflags = jflags + " -fexceptions" /* + picflags */ + " $(cflags)";
    String cppflags = " $(DEFS) $(cppflags)";
    String cxxflags = cflags + " $(cxxflags)";
    String ldflags = ""; // + soflags;
    String dldflags = "";
    String ldsharedflags = " -shared ";

    String archflags = " -m" + (Platform.IS_64_BIT ? "64" : "32");

    String hdr_dir = new NormalizedFile(normalizedHome, "lib/native/include/").getPath();

    // A few platform specific values
    if (Platform.IS_WINDOWS) {
      ldflags +=
          " -L"
              + new NormalizedFile(
                      normalizedHome,
                      "lib/native/" + (Platform.IS_64_BIT ? "x86_64" : "i386") + "-Windows")
                  .getPath();
      ldflags += " -ljruby-cext";
      ldsharedflags += " $(if $(filter-out -g -g0,$(debugflags)),,-s)";
      dldflags = "-Wl,--enable-auto-image-base,--enable-auto-import $(DEFFILE)";
      archflags += " -march=native -mtune=native";
      setConfig(mkmfHash, "DLEXT", "dll");
    } else if (Platform.IS_MAC) {
      ldsharedflags = " -dynamic -bundle -undefined dynamic_lookup ";
      cflags = " -fPIC -DTARGET_RT_MAC_CFM=0 " + cflags;
      archflags = " -arch " + Platform.ARCH;
      cppflags = " -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE " + cppflags;
      setConfig(mkmfHash, "DLEXT", "bundle");
    } else {
      cflags = " -fPIC " + cflags;
      setConfig(mkmfHash, "DLEXT", "so");
    }

    String libext = "a";
    String objext = "o";

    setConfig(mkmfHash, "configure_args", "");
    setConfig(mkmfHash, "CFLAGS", cflags);
    setConfig(mkmfHash, "CPPFLAGS", cppflags);
    setConfig(mkmfHash, "CXXFLAGS", cxxflags);
    setConfig(mkmfHash, "ARCH_FLAG", archflags);
    setConfig(mkmfHash, "LDFLAGS", ldflags);
    setConfig(mkmfHash, "DLDFLAGS", dldflags);
    setConfig(mkmfHash, "DEFS", "");
    setConfig(mkmfHash, "LIBEXT", libext);
    setConfig(mkmfHash, "OBJEXT", objext);
    setConfig(mkmfHash, "LIBRUBYARG_STATIC", "");
    setConfig(mkmfHash, "LIBRUBYARG_SHARED", "");
    setConfig(mkmfHash, "LIBS", "");
    setConfig(mkmfHash, "DLDLIBS", "");
    setConfig(mkmfHash, "ENABLED_SHARED", "");
    setConfig(mkmfHash, "LIBRUBY", "");
    setConfig(mkmfHash, "LIBRUBY_A", "");
    setConfig(mkmfHash, "LIBRUBYARG", "");
    setConfig(mkmfHash, "prefix", " "); // This must not be empty for some extconf.rb's to work
    setConfig(mkmfHash, "ruby_install_name", jrubyScript());
    setConfig(mkmfHash, "LDSHARED", cc + ldsharedflags);
    setConfig(mkmfHash, "LDSHAREDXX", cxx + ldsharedflags);
    setConfig(mkmfHash, "RUBY_PLATFORM", getOSName());
    setConfig(mkmfHash, "CC", cc);
    setConfig(mkmfHash, "CPP", cpp);
    setConfig(mkmfHash, "CXX", cxx);
    setConfig(mkmfHash, "OUTFLAG", "-o ");
    setConfig(mkmfHash, "COMMON_HEADERS", "ruby.h");
    setConfig(mkmfHash, "PATH_SEPARATOR", ":");
    setConfig(mkmfHash, "INSTALL", "install -c ");
    setConfig(mkmfHash, "RM", "rm -f");
    setConfig(mkmfHash, "CP", "cp ");
    setConfig(mkmfHash, "MAKEDIRS", "mkdir -p ");
    setConfig(mkmfHash, "includedir", hdr_dir);
    setConfig(mkmfHash, "rubyhdrdir", hdr_dir);
    setConfig(mkmfHash, "archdir", hdr_dir);

    ruby.getObject().defineConstant("CROSS_COMPILING", ruby.getNil());

    configModule.defineConstant("MAKEFILE_CONFIG", mkmfHash);
  }
  DynamicMethod createMethod(
      RubyModule module,
      Function function,
      Type returnType,
      Type[] parameterTypes,
      CallingConvention convention,
      IRubyObject enums) {

    FunctionInvoker functionInvoker = getFunctionInvoker(returnType);

    ParameterMarshaller[] marshallers = new ParameterMarshaller[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; ++i) {
      marshallers[i] = getMarshaller(parameterTypes[i], convention, enums);
      if (marshallers[i] == null) {
        throw module
            .getRuntime()
            .newTypeError("Could not create marshaller for " + parameterTypes[i]);
      }
    }

    /*
     * If there is exactly _one_ callback argument to the function,
     * then a block can be given and automatically subsituted for the callback
     * parameter.
     */
    if (marshallers.length > 0) {
      int cbcount = 0, cbindex = -1;
      for (int i = 0; i < marshallers.length; ++i) {
        if (marshallers[i] instanceof CallbackMarshaller) {
          cbcount++;
          cbindex = i;
        }
      }
      if (cbcount == 1) {
        return new CallbackMethodWithBlock(module, function, functionInvoker, marshallers, cbindex);
      }
    }

    //
    // Determine if the parameter might be passed as a 32bit int parameter.
    // This just applies to buffer/pointer types.
    //
    FastIntMethodFactory fastIntFactory = FastIntMethodFactory.getFactory();
    boolean canBeFastInt =
        enums.isNil() && parameterTypes.length <= 3 && fastIntFactory.isFastIntResult(returnType);
    for (int i = 0; canBeFastInt && i < parameterTypes.length; ++i) {
      if (!(parameterTypes[i] instanceof Type.Builtin) || marshallers[i].needsInvocationSession()) {
        canBeFastInt = false;
      } else {
        switch (parameterTypes[i].getNativeType()) {
          case POINTER:
          case BUFFER_IN:
          case BUFFER_OUT:
          case BUFFER_INOUT:
            canBeFastInt = Platform.getPlatform().addressSize() == 32;
            break;
          default:
            canBeFastInt = fastIntFactory.isFastIntParam(parameterTypes[i]);
            break;
        }
      }
    }

    if (!canBeFastInt)
      switch (parameterTypes.length) {
        case 0:
          return new DefaultMethodZeroArg(module, function, functionInvoker);
        case 1:
          return new DefaultMethodOneArg(module, function, functionInvoker, marshallers);
        case 2:
          return new DefaultMethodTwoArg(module, function, functionInvoker, marshallers);
        case 3:
          return new DefaultMethodThreeArg(module, function, functionInvoker, marshallers);
        default:
          return new DefaultMethod(module, function, functionInvoker, marshallers);
      }
    //
    // Set up for potentially fast-int operations
    //

    IntResultConverter resultConverter = fastIntFactory.getIntResultConverter(returnType);
    IntParameterConverter[] intParameterConverters =
        new IntParameterConverter[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; ++i) {
      intParameterConverters[i] = fastIntFactory.getIntParameterConverter(parameterTypes[i]);
    }
    switch (parameterTypes.length) {
      case 0:
        return new FastIntMethodZeroArg(module, function, resultConverter, intParameterConverters);
      case 1:
        return new FastIntPointerMethodOneArg(
            module, function, resultConverter, intParameterConverters, marshallers);
      case 2:
        return new FastIntPointerMethodTwoArg(
            module, function, resultConverter, intParameterConverters, marshallers);
      case 3:
        return new FastIntPointerMethodThreeArg(
            module, function, resultConverter, intParameterConverters, marshallers);
    }
    throw new IllegalArgumentException("Parameter types not supported");
  }