v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
torque-code-generator.h
Go to the documentation of this file.
1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_TORQUE_TORQUE_CODE_GENERATOR_H_
6#define V8_TORQUE_TORQUE_CODE_GENERATOR_H_
7
8#include <iostream>
9
10#include "src/torque/cfg.h"
12
13namespace v8 {
14namespace internal {
15namespace torque {
16
18 public:
19 TorqueCodeGenerator(const ControlFlowGraph& cfg, std::ostream& out)
20 : cfg_(cfg),
21 out_(&out),
22 out_decls_(&out),
24
25 protected:
27 std::ostream* out_;
28 std::ostream* out_decls_;
29 size_t fresh_id_ = 0;
31 std::map<DefinitionLocation, std::string> location_map_;
32
33 std::string DefinitionToVariable(const DefinitionLocation& location) {
34 if (location.IsPhi()) {
35 std::stringstream stream;
36 stream << "phi_bb" << location.GetPhiBlock()->id() << "_"
37 << location.GetPhiIndex();
38 return stream.str();
39 } else if (location.IsParameter()) {
40 auto it = location_map_.find(location);
41 DCHECK_NE(it, location_map_.end());
42 return it->second;
43 } else {
44 DCHECK(location.IsInstruction());
45 auto it = location_map_.find(location);
46 if (it == location_map_.end()) {
47 it = location_map_.insert(std::make_pair(location, FreshNodeName()))
48 .first;
49 }
50 return it->second;
51 }
52 }
53
55 const std::string& str) {
56 DCHECK_EQ(location_map_.find(definition), location_map_.end());
57 location_map_.insert(std::make_pair(definition, str));
58 }
59
60 std::ostream& out() { return *out_; }
61 std::ostream& decls() { return *out_decls_; }
62
63 static bool IsEmptyInstruction(const Instruction& instruction);
65 bool always_emit = false) = 0;
66
67 std::string FreshNodeName() { return "tmp" + std::to_string(fresh_id_++); }
68 std::string FreshCatchName() { return "catch" + std::to_string(fresh_id_++); }
69 std::string FreshLabelName() { return "label" + std::to_string(fresh_id_++); }
70 std::string BlockName(const Block* block) {
71 return "block" + std::to_string(block->id());
72 }
73
74 void EmitInstruction(const Instruction& instruction,
75 Stack<std::string>* stack);
76
77 template <typename T>
78 void EmitIRAnnotation(const T& instruction, Stack<std::string>* stack) {
79 out() << " // " << instruction
80 << ", starting stack size: " << stack->Size() << "\n";
81 }
82
83#define EMIT_INSTRUCTION_DECLARATION(T) \
84 void EmitInstruction(const T& instruction, Stack<std::string>* stack);
86#undef EMIT_INSTRUCTION_DECLARATION
87
88#define EMIT_INSTRUCTION_DECLARATION(T) \
89 virtual void EmitInstruction(const T& instruction, \
90 Stack<std::string>* stack) = 0;
92#undef EMIT_INSTRUCTION_DECLARATION
93};
94
95} // namespace torque
96} // namespace internal
97} // namespace v8
98
99#endif // V8_TORQUE_TORQUE_CODE_GENERATOR_H_
#define EMIT_INSTRUCTION_DECLARATION(T)
SourcePosition pos
TorqueCodeGenerator(const ControlFlowGraph &cfg, std::ostream &out)
void SetDefinitionVariable(const DefinitionLocation &definition, const std::string &str)
std::map< DefinitionLocation, std::string > location_map_
static bool IsEmptyInstruction(const Instruction &instruction)
std::string DefinitionToVariable(const DefinitionLocation &location)
std::string BlockName(const Block *block)
void EmitIRAnnotation(const T &instruction, Stack< std::string > *stack)
void EmitInstruction(const Instruction &instruction, Stack< std::string > *stack)
virtual void EmitSourcePosition(SourcePosition pos, bool always_emit=false)=0
#define TORQUE_BACKEND_DEPENDENT_INSTRUCTION_LIST(V)
#define TORQUE_BACKEND_AGNOSTIC_INSTRUCTION_LIST(V)
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485