Beispiel #1
0
 @Test
 public void test() {
   TruffleRuntime runtime = Truffle.getRuntime();
   TestRootNode rootNode =
       new TestRootNode(new TestArgumentNode[] {new TestArgumentNode(0), new TestArgumentNode(1)});
   CallTarget target = runtime.createCallTarget(rootNode);
   Object result = target.call(new TestArguments(20, 22));
   Assert.assertEquals(42, result);
 }
/**
 * Slow-path code for a call, used when the polymorphic inline cache exceeded its maximum size. Such
 * calls are not optimized any further, e.g., no method inlining is performed.
 */
final class LlvmGenericDispatchNode extends LlvmAbstractDispatchNode {

  /**
   * {@link IndirectCallNode} is part of the Truffle API and handles all the steps necessary for
   * calling a megamorphic call-site. The Graal specific version of this node performs additional
   * optimizations for the fast access of the SimpleLanguage stack trace.
   */
  @Child private IndirectCallNode callNode = Truffle.getRuntime().createIndirectCallNode();

  @Override
  protected Object executeDispatch(VirtualFrame frame, LlvmFunction function, Object[] arguments) {
    /*
     * Llvm has a quite simple call lookup: just ask the function for the current call target,
     * and call it.
     */
    return callNode.call(frame, function.getCallTarget(), arguments);
  }
}
Beispiel #3
0
 public static PGenerator create(
     String name,
     RootCallTarget callTarget,
     FrameDescriptor frameDescriptor,
     MaterializedFrame declarationFrame,
     Object[] arguments,
     int numOfActiveFlags,
     int numOfGeneratorBlockNode,
     int numOfGeneratorForNode) {
   /** Setting up the persistent frame in {@link #arguments}. */
   GeneratorControlData generatorArgs =
       new GeneratorControlData(numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
   MaterializedFrame generatorFrame =
       Truffle.getRuntime().createMaterializedFrame(PArguments.create(), frameDescriptor);
   PArguments.setDeclarationFrame(arguments, declarationFrame);
   PArguments.setGeneratorFrame(arguments, generatorFrame);
   PArguments.setControlData(arguments, generatorArgs);
   return new PGenerator(name, callTarget, frameDescriptor, arguments);
 }
Beispiel #4
0
public class RContext {

  public static final boolean DEBUG = Utils.getProperty("RConsole.debug.gui", false);
  private static boolean debuggingFormat = false;
  private static boolean usesTruffleOptimizer =
      Truffle.getRuntime().equals("Default Truffle Runtime");
  private static ManageError errorManager = new ManageError(System.err);
  private static Truffleize truffleize = new Truffleize();
  private static final int NCONNECTIONS = 128;
  private static final Connection[] connections = new Connection[NCONNECTIONS];

  static {
    Arrays.fill(connections, null);
  }

  public static boolean usesTruffleOptimizer() {
    return usesTruffleOptimizer;
  }

  public static boolean debuggingFormat() {
    return debuggingFormat;
  }

  public static boolean debuggingFormat(boolean useDebuggingFormat) {
    boolean previous = debuggingFormat;
    debuggingFormat = useDebuggingFormat;
    return previous;
  }

  public static RAny eval(ASTNode expr, boolean useDebuggingFormat) {
    debuggingFormat(useDebuggingFormat);
    return eval(expr);
    // NOTE: cannot reset to the original value of debuggingFormat here, because usually the pretty
    // printer is
    // invoked on the results afterwards by the caller of eval; the pretty printer still depends on
    // the correct
    // setting of debugging format
  }

  public static RAny eval(ASTNode expr) {
    try {
      return (RAny) truffleize.createLazyRootTree(expr).execute(null); // null means top-level
    } catch (RError e) {
      if (DEBUG) {
        e.printStackTrace();
      }
      error(e); // throws an error
    }
    throw new Error("Never reached");
  }

  public static RNode createNode(ASTNode expr) {
    return truffleize.createTree(expr);
  }

  public static RNode createRootNode(ASTNode expr, final RFunction rootEnclosingFunction) {
    return new BaseR(expr) {
      @Child RNode node = adoptChild(truffleize.createTree(ast, rootEnclosingFunction));

      @Override
      public Object execute(Frame frame) {
        return node.execute(frame);
      }
    };
  }

  public static void warning(ASTNode expr, String msg, Object... args) {
    errorManager.warning(expr, String.format(msg, args));
  }

  public static void warning(ASTNode expr, String msg) {
    errorManager.warning(expr, msg);
  }

  public static void warning(RError err) {
    errorManager.warning(err);
  }

  public static void error(ASTNode expr, String msg) {
    errorManager.error(expr, msg);
  }

  public static void error(RError err) {
    errorManager.error(err);
  }

  public static int allocateConnection(Connection connection) {
    for (int i = 0; i < NCONNECTIONS; i++) {
      if (connections[i] == null) {
        connections[i] = connection;
        return i;
      }
    }
    return -1;
  }

  /** Release a connection currently in use. */
  public static void freeConnection(int i) {
    assert Utils.check(connections[i] != null);
    connections[i] = null;
  }

  /** Return a connection or null. */
  public static Connection getConnection(int i) {
    return i >= 0 && i < NCONNECTIONS ? connections[i] : null;
  }

  // note: GNUR currently means not only the GNU-R library, but also some other native code, under
  // licenses compatible with GPL
  private static int hasGNUR = -1;

  public static boolean hasGNUR() {
    if (hasGNUR == -1) {
      try {
        System.loadLibrary("gnurglue");
        hasGNUR = 1;
      } catch (Throwable t) {
        hasGNUR = 0;
      }
    }
    return hasGNUR == 1;
  }

  public static ASTNode parseFile(ANTLRStringStream inputStream) {
    CommonTokenStream tokens = new CommonTokenStream();
    RLexer lexer = new RLexer(inputStream);
    tokens.setTokenSource(lexer);
    RParser parser = new RParser(tokens);

    try {
      return parser.script();
    } catch (RecognitionException e) {
      Console.parseError(parser, e);
      return null;
    }
  }
}
 public GeneralDispatchNode(RubyContext context, String name) {
   super(context);
   assert name != null;
   this.name = name;
   callNode = Truffle.getRuntime().createIndirectCallNode();
 }