1 // Copyright 2012 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_CODEGEN_MACRO_ASSEMBLER_H_
6 #define V8_CODEGEN_MACRO_ASSEMBLER_H_
7 
8 #include "src/codegen/turbo-assembler.h"
9 #include "src/execution/frames.h"
10 #include "src/heap/heap.h"
11 
12 // Helper types to make boolean flag easier to read at call-site.
13 enum InvokeFlag { CALL_FUNCTION, JUMP_FUNCTION };
14 
15 // Flags used for the AllocateInNewSpace functions.
16 enum AllocationFlags {
17   // No special flags.
18   NO_ALLOCATION_FLAGS = 0,
19   // The content of the result register already contains the allocation top in
20   // new space.
21   RESULT_CONTAINS_TOP = 1 << 0,
22   // Specify that the requested size of the space to allocate is specified in
23   // words instead of bytes.
24   SIZE_IN_WORDS = 1 << 1,
25   // Align the allocation to a multiple of kDoubleSize
26   DOUBLE_ALIGNMENT = 1 << 2,
27   // Directly allocate in old space
28   PRETENURE = 1 << 3,
29 };
30 
31 // This is the only place allowed to include the platform-specific headers.
32 #define INCLUDED_FROM_MACRO_ASSEMBLER_H
33 #if V8_TARGET_ARCH_IA32
34 #include "src/codegen/ia32/macro-assembler-ia32.h"
35 #elif V8_TARGET_ARCH_X64
36 #include "src/codegen/x64/macro-assembler-x64.h"
37 #elif V8_TARGET_ARCH_ARM64
38 #include "src/codegen/arm64/constants-arm64.h"
39 #include "src/codegen/arm64/macro-assembler-arm64.h"
40 #elif V8_TARGET_ARCH_ARM
41 #include "src/codegen/arm/constants-arm.h"
42 #include "src/codegen/arm/macro-assembler-arm.h"
43 #elif V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_PPC64
44 #include "src/codegen/ppc/constants-ppc.h"
45 #include "src/codegen/ppc/macro-assembler-ppc.h"
46 #elif V8_TARGET_ARCH_MIPS
47 #include "src/codegen/mips/constants-mips.h"
48 #include "src/codegen/mips/macro-assembler-mips.h"
49 #elif V8_TARGET_ARCH_MIPS64
50 #include "src/codegen/mips64/constants-mips64.h"
51 #include "src/codegen/mips64/macro-assembler-mips64.h"
52 #elif V8_TARGET_ARCH_S390
53 #include "src/codegen/s390/constants-s390.h"
54 #include "src/codegen/s390/macro-assembler-s390.h"
55 #else
56 #error Unsupported target architecture.
57 #endif
58 #undef INCLUDED_FROM_MACRO_ASSEMBLER_H
59 
60 namespace v8 {
61 namespace internal {
62 
63 // Simulators only support C calls with up to kMaxCParameters parameters.
64 static constexpr int kMaxCParameters = 10;
65 
66 class FrameScope {
67  public:
FrameScope(TurboAssembler * tasm,StackFrame::Type type)68   explicit FrameScope(TurboAssembler* tasm, StackFrame::Type type)
69       : tasm_(tasm), type_(type), old_has_frame_(tasm->has_frame()) {
70     tasm->set_has_frame(true);
71     if (type != StackFrame::MANUAL && type_ != StackFrame::NONE) {
72       tasm->EnterFrame(type);
73     }
74   }
75 
~FrameScope()76   ~FrameScope() {
77     if (type_ != StackFrame::MANUAL && type_ != StackFrame::NONE) {
78       tasm_->LeaveFrame(type_);
79     }
80     tasm_->set_has_frame(old_has_frame_);
81   }
82 
83  private:
84   TurboAssembler* tasm_;
85   StackFrame::Type type_;
86   bool old_has_frame_;
87 };
88 
89 class FrameAndConstantPoolScope {
90  public:
FrameAndConstantPoolScope(MacroAssembler * masm,StackFrame::Type type)91   FrameAndConstantPoolScope(MacroAssembler* masm, StackFrame::Type type)
92       : masm_(masm),
93         type_(type),
94         old_has_frame_(masm->has_frame()),
95         old_constant_pool_available_(FLAG_enable_embedded_constant_pool &&
96                                      masm->is_constant_pool_available()) {
97     masm->set_has_frame(true);
98     if (FLAG_enable_embedded_constant_pool) {
99       masm->set_constant_pool_available(true);
100     }
101     if (type_ != StackFrame::MANUAL && type_ != StackFrame::NONE) {
102       masm->EnterFrame(type, !old_constant_pool_available_);
103     }
104   }
105 
~FrameAndConstantPoolScope()106   ~FrameAndConstantPoolScope() {
107     masm_->LeaveFrame(type_);
108     masm_->set_has_frame(old_has_frame_);
109     if (FLAG_enable_embedded_constant_pool) {
110       masm_->set_constant_pool_available(old_constant_pool_available_);
111     }
112   }
113 
114  private:
115   MacroAssembler* masm_;
116   StackFrame::Type type_;
117   bool old_has_frame_;
118   bool old_constant_pool_available_;
119 
120   DISALLOW_IMPLICIT_CONSTRUCTORS(FrameAndConstantPoolScope);
121 };
122 
123 // Class for scoping the the unavailability of constant pool access.
124 class ConstantPoolUnavailableScope {
125  public:
ConstantPoolUnavailableScope(Assembler * assembler)126   explicit ConstantPoolUnavailableScope(Assembler* assembler)
127       : assembler_(assembler),
128         old_constant_pool_available_(FLAG_enable_embedded_constant_pool &&
129                                      assembler->is_constant_pool_available()) {
130     if (FLAG_enable_embedded_constant_pool) {
131       assembler->set_constant_pool_available(false);
132     }
133   }
~ConstantPoolUnavailableScope()134   ~ConstantPoolUnavailableScope() {
135     if (FLAG_enable_embedded_constant_pool) {
136       assembler_->set_constant_pool_available(old_constant_pool_available_);
137     }
138   }
139 
140  private:
141   Assembler* assembler_;
142   int old_constant_pool_available_;
143 
144   DISALLOW_IMPLICIT_CONSTRUCTORS(ConstantPoolUnavailableScope);
145 };
146 
147 class AllowExternalCallThatCantCauseGC : public FrameScope {
148  public:
AllowExternalCallThatCantCauseGC(MacroAssembler * masm)149   explicit AllowExternalCallThatCantCauseGC(MacroAssembler* masm)
150       : FrameScope(masm, StackFrame::NONE) {}
151 };
152 
153 // Prevent the use of the RootArray during the lifetime of this
154 // scope object.
155 class NoRootArrayScope {
156  public:
NoRootArrayScope(TurboAssembler * masm)157   explicit NoRootArrayScope(TurboAssembler* masm)
158       : masm_(masm), old_value_(masm->root_array_available()) {
159     masm->set_root_array_available(false);
160   }
161 
~NoRootArrayScope()162   ~NoRootArrayScope() { masm_->set_root_array_available(old_value_); }
163 
164  private:
165   TurboAssembler* masm_;
166   bool old_value_;
167 };
168 
169 }  // namespace internal
170 }  // namespace v8
171 
172 #endif  // V8_CODEGEN_MACRO_ASSEMBLER_H_
173