csa-generator.h 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TORQUE_CSA_GENERATOR_H_
#define V8_TORQUE_CSA_GENERATOR_H_

#include <iostream>

#include "src/torque/cfg.h"
#include "src/torque/declarable.h"

namespace v8 {
namespace internal {
namespace torque {

class CSAGenerator {
 public:
  CSAGenerator(const ControlFlowGraph& cfg, std::ostream& out,
               base::Optional<Builtin::Kind> linkage = base::nullopt)
21 22 23 24
      : cfg_(cfg),
        out_(out),
        linkage_(linkage),
        previous_position_(SourcePosition::Invalid()) {}
25 26 27 28 29 30 31 32 33 34 35 36
  base::Optional<Stack<std::string>> EmitGraph(Stack<std::string> parameters);

  static constexpr const char* ARGUMENTS_VARIABLE_STRING = "arguments";

  static void EmitCSAValue(VisitResult result, const Stack<std::string>& values,
                           std::ostream& out);

 private:
  const ControlFlowGraph& cfg_;
  std::ostream& out_;
  size_t fresh_id_ = 0;
  base::Optional<Builtin::Kind> linkage_;
37 38 39
  SourcePosition previous_position_;

  void EmitSourcePosition(SourcePosition pos, bool always_emit = false);
40

41 42 43 44 45 46 47
  std::string PreCallableExceptionPreparation(
      base::Optional<Block*> catch_block);
  void PostCallableExceptionPreparation(const std::string& catch_name,
                                        const Type* return_type,
                                        base::Optional<Block*> catch_block,
                                        Stack<std::string>* stack);

48
  std::string FreshNodeName() { return "tmp" + std::to_string(fresh_id_++); }
49
  std::string FreshCatchName() { return "catch" + std::to_string(fresh_id_++); }
50 51 52 53
  std::string BlockName(const Block* block) {
    return "block" + std::to_string(block->id());
  }

54 55 56 57 58
  void ProcessArgumentsCommon(const TypeVector& parameter_types,
                              std::vector<std::string>* args,
                              std::vector<std::string>* constexpr_arguments,
                              Stack<std::string>* stack);

59 60 61 62 63 64 65 66 67 68 69 70 71 72
  Stack<std::string> EmitBlock(const Block* block);
  void EmitInstruction(const Instruction& instruction,
                       Stack<std::string>* stack);
#define EMIT_INSTRUCTION_DECLARATION(T) \
  void EmitInstruction(const T& instruction, Stack<std::string>* stack);
  TORQUE_INSTRUCTION_LIST(EMIT_INSTRUCTION_DECLARATION)
#undef EMIT_INSTRUCTION_DECLARATION
};

}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_CSA_GENERATOR_H_