<commit_before>// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Generate code using the cps-based IR pipeline.
library code_generator_task;

import '../js_backend.dart';import '../../dart2jslib.dart';
import '../../source_file.dart';
import '../../cps_ir/cps_ir_nodes.dart' as cps;
import '../../cps_ir/cps_ir_builder.dart';
import '../../tree_ir/tree_ir_nodes.dart' as tree_ir;
import '../../tree/tree.dart' as ast;
import '../../scanner/scannerlib.dart' as scanner;
import '../../elements/elements.dart';
import '../../closure.dart';
import '../../js/js.dart' as js;
import '../../source_map_builder.dart';
import '../../tree_ir/tree_ir_builder.dart' as tree_builder;
import '../../dart_backend/backend_ast_emitter.dart' as backend_ast_emitter;
import '../../cps_ir/optimizers.dart';
import '../../tracer.dart';
import '../../dart_backend/statement_rewriter.dart';
import '../../dart_backend/copy_propagator.dart';
import '../../dart_backend/loop_rewriter.dart';
import '../../dart_backend/logical_rewriter.dart';
import '../../js_backend/codegen/codegen.dart';
import '../../ssa/ssa.dart' as ssa;

class CspFunctionCompiler implements FunctionCompiler {

  // TODO(sigurdm): Assign this.
  Tracer tracer;

  String get name => 'CPS Ir pipeline';

  final IrBuilderTask irBuilderTask;

  final ConstantSystem constantSystem;

  final Compiler compiler;

  // Remember to update dart-doc of [compile] when this field is removed.
  final FunctionCompiler fallbackCompiler;

  CspFunctionCompiler(Compiler compiler, JavaScriptBackend backend)
      : irBuilderTask = new IrBuilderTask(compiler),
        fallbackCompiler = new ssa.SsaFunctionCompiler(backend, true),
        constantSystem = backend.constantSystem,
        compiler = compiler;

  /// Generates JavaScript code for `work.element`. First tries to use the
  /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language
  /// features not implemented it will fall back to the ssa pipeline.
  js.Fun compile(CodegenWorkItem work) {
    AstElement element = work.element;
    return compiler.withCurrentElement(element, () {
      try {
        if (tracer != null) {
          tracer.traceCompilation(element.name, null);
        }
        cps.FunctionDefinition cpsFunction = compileToCpsIR(element);
        cpsFunction = optimizeCpsIR(cpsFunction);
        tree_ir.FunctionDefinition treeFunction = compileToTreeIR(cpsFunction);
        treeFunction = optimizeTreeIR(treeFunction);
        return compileToJavaScript(work, treeFunction);
      } on CodegenBailout catch (e) {
        compiler.log('Falling back to SSA compiler for $element'
            ' (${e.message})');
        return fallbackCompiler.compile(work);
      }
    });
  }

  void giveUp(String reason) {
    throw new CodegenBailout(null, reason);
  }

  void traceGraph(String title, var irObject) {
    if (tracer != null) {
      tracer.traceGraph(title, irObject);
    }
  }

  cps.FunctionDefinition compileToCpsIR(AstElement element) {<commit_msg>Only use fallback compiler for platform functions.<commit_after>// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Generate code using the cps-based IR pipeline.
library code_generator_task;

import '../js_backend.dart';import '../../dart2jslib.dart';
import '../../source_file.dart';
import '../../cps_ir/cps_ir_nodes.dart' as cps;
import '../../cps_ir/cps_ir_builder.dart';
import '../../tree_ir/tree_ir_nodes.dart' as tree_ir;
import '../../tree/tree.dart' as ast;
import '../../scanner/scannerlib.dart' as scanner;
import '../../elements/elements.dart';
import '../../closure.dart';
import '../../js/js.dart' as js;
import '../../source_map_builder.dart';
import '../../tree_ir/tree_ir_builder.dart' as tree_builder;
import '../../dart_backend/backend_ast_emitter.dart' as backend_ast_emitter;
import '../../cps_ir/optimizers.dart';
import '../../tracer.dart';
import '../../dart_backend/statement_rewriter.dart';
import '../../dart_backend/copy_propagator.dart';
import '../../dart_backend/loop_rewriter.dart';
import '../../dart_backend/logical_rewriter.dart';
import '../../js_backend/codegen/codegen.dart';
import '../../ssa/ssa.dart' as ssa;

class CspFunctionCompiler implements FunctionCompiler {
  final IrBuilderTask irBuilderTask;
  final ConstantSystem constantSystem;
  final Compiler compiler;

  // TODO(karlklose,sigurm): remove and update dart-doc of [compile].
  final FunctionCompiler fallbackCompiler;

  // TODO(sigurdm): Assign this.
  Tracer tracer;

  CspFunctionCompiler(Compiler compiler, JavaScriptBackend backend)
      : irBuilderTask = new IrBuilderTask(compiler),
        fallbackCompiler = new ssa.SsaFunctionCompiler(backend, true),
        constantSystem = backend.constantSystem,
        compiler = compiler;

  String get name => 'CPS Ir pipeline';

  /// Generates JavaScript code for `work.element`. First tries to use the
  /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language
  /// features not implemented it will fall back to the ssa pipeline (for
  /// platform code) or will cancel compilation (for user code).
  js.Fun compile(CodegenWorkItem work) {
    AstElement element = work.element;
    return compiler.withCurrentElement(element, () {
      try {
        if (tracer != null) {
          tracer.traceCompilation(element.name, null);
        }
        cps.FunctionDefinition cpsFunction = compileToCpsIR(element);
        cpsFunction = optimizeCpsIR(cpsFunction);
        tree_ir.FunctionDefinition treeFunction = compileToTreeIR(cpsFunction);
        treeFunction = optimizeTreeIR(treeFunction);
        return compileToJavaScript(work, treeFunction);
      } on CodegenBailout catch (e) {
        if (element.library.isPlatformLibrary) {
          compiler.log('Falling back to SSA compiler for $element'
              ' (${e.message})');
          return fallbackCompiler.compile(work);
        } else {
          String message = "Unable to compile $element with the new compiler.\n"
              "  Reason: ${e.message}";
          compiler.internalError(element, message);
        }
      }
    });
  }

  void giveUp(String reason) {
    throw new CodegenBailout(null, reason);
  }

  void traceGraph(String title, var irObject) {
    if (tracer != null) {
      tracer.traceGraph(title, irObject);
    }
  }

  cps.FunctionDefinition compileToCpsIR(AstElement element) {
{'fraction_non_alphanumeric': 0.1319880418535127, 'fraction_numerical': 0.0014947683109118087, 'mean_word_length': 3.210824417872876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3394677', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1976, 'n_words': 535}