1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_FAST_CODEGEN_H_
29#define V8_FAST_CODEGEN_H_
30
31#if V8_TARGET_ARCH_IA32
32#include "ia32/fast-codegen-ia32.h"
33#else
34
35#include "v8.h"
36
37#include "ast.h"
38#include "compiler.h"
39#include "list.h"
40
41namespace v8 {
42namespace internal {
43
44class FastCodeGenSyntaxChecker: public AstVisitor {
45 public:
46  explicit FastCodeGenSyntaxChecker()
47      : info_(NULL), has_supported_syntax_(true) {
48  }
49
50  void Check(CompilationInfo* info);
51
52  CompilationInfo* info() { return info_; }
53  bool has_supported_syntax() { return has_supported_syntax_; }
54
55 private:
56  void VisitDeclarations(ZoneList<Declaration*>* decls);
57  void VisitStatements(ZoneList<Statement*>* stmts);
58
59  // AST node visit functions.
60#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
61  AST_NODE_LIST(DECLARE_VISIT)
62#undef DECLARE_VISIT
63
64  CompilationInfo* info_;
65  bool has_supported_syntax_;
66
67  DISALLOW_COPY_AND_ASSIGN(FastCodeGenSyntaxChecker);
68};
69
70
71class FastCodeGenerator: public AstVisitor {
72 public:
73  explicit FastCodeGenerator(MacroAssembler* masm)
74      : masm_(masm), info_(NULL), destination_(no_reg), smi_bits_(0) {
75  }
76
77  static Handle<Code> MakeCode(CompilationInfo* info);
78
79  void Generate(CompilationInfo* compilation_info);
80
81 private:
82  MacroAssembler* masm() { return masm_; }
83  CompilationInfo* info() { return info_; }
84
85  Register destination() { return destination_; }
86  void set_destination(Register reg) { destination_ = reg; }
87
88  FunctionLiteral* function() { return info_->function(); }
89  Scope* scope() { return info_->scope(); }
90
91  // Platform-specific fixed registers, all guaranteed distinct.
92  Register accumulator0();
93  Register accumulator1();
94  Register scratch0();
95  Register scratch1();
96  Register receiver_reg();
97  Register context_reg();
98
99  Register other_accumulator(Register reg) {
100    ASSERT(reg.is(accumulator0()) || reg.is(accumulator1()));
101    return (reg.is(accumulator0())) ? accumulator1() : accumulator0();
102  }
103
104  // Flags are true if the respective register is statically known to hold a
105  // smi.  We do not track every register, only the accumulator registers.
106  bool is_smi(Register reg) {
107    ASSERT(!reg.is(no_reg));
108    return (smi_bits_ & reg.bit()) != 0;
109  }
110  void set_as_smi(Register reg) {
111    ASSERT(!reg.is(no_reg));
112    smi_bits_ = smi_bits_ | reg.bit();
113  }
114  void clear_as_smi(Register reg) {
115    ASSERT(!reg.is(no_reg));
116    smi_bits_ = smi_bits_ & ~reg.bit();
117  }
118
119  // AST node visit functions.
120#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
121  AST_NODE_LIST(DECLARE_VISIT)
122#undef DECLARE_VISIT
123
124  // Emit code to load the receiver from the stack into receiver_reg.
125  void EmitLoadReceiver();
126
127  // Emit code to load a global variable directly from a global property
128  // cell into the destination register.
129  void EmitGlobalVariableLoad(Handle<Object> cell);
130
131  // Emit a store to an own property of this.  The stored value is expected
132  // in accumulator0 and the receiver in receiver_reg.  The receiver
133  // register is preserved and the result (the stored value) is left in the
134  // destination register.
135  void EmitThisPropertyStore(Handle<String> name);
136
137  // Emit a load from an own property of this.  The receiver is expected in
138  // receiver_reg.  The receiver register is preserved and the result is
139  // left in the destination register.
140  void EmitThisPropertyLoad(Handle<String> name);
141
142  // Emit a bitwise or operation.  The left operand is in accumulator1 and
143  // the right is in accumulator0.  The result should be left in the
144  // destination register.
145  void EmitBitOr();
146
147  MacroAssembler* masm_;
148  CompilationInfo* info_;
149  Register destination_;
150  uint32_t smi_bits_;
151
152  DISALLOW_COPY_AND_ASSIGN(FastCodeGenerator);
153};
154
155
156} }  // namespace v8::internal
157
158#endif  // V8_TARGET_ARCH_IA32
159
160#endif  // V8_FAST_CODEGEN_H_
161