Revision: 3095
Author: [email protected]
Date: Tue Oct 20 06:37:26 2009
Log: Added support for assignments to global variables in the toplevel code
generator.  We use the normal store IC mechanism with the global
object as the receiver.  The following code is generated for 'x=true'
at toplevel.

======== IA32:

27  mov eax,0xf5d06161          ;; object: 0xf5d06161 <true>
32  mov ecx,0xf5d09c35          ;; object: 0xf5d09c35 <String[1]: x>
37  push [esi+0x17]
40  call StoreIC_Initialize  (0xf5ce75c0)    ;; code: STORE_IC,  
UNINITIALIZED
45  mov [esp],eax

======== X64:

25  movq rax,0x7f867a7b6199    ;; object: 0x7f867a7b6199 <true>
35  movq rcx,0x7f867a7bae71    ;; object: 0x7f867a7bae71 <String[1]: x>
45  push [rsi+0x2f]
49  call StoreIC_Initialize  (0x7f8655929ac0)    ;; code: STORE_IC,  
UNINITIALIZED
54  movq [rsp],rax

======== ARM:

32  e59f0054       ldr r0, [pc, #+84]          ;; object: 0xf5b78161 <true>
36  e59f2054       ldr r2, [pc, #+84]          ;; object: 0xf5b7bc35  
<String[1]: x>
40  e598c017       ldr ip, [r8, #+23]
44  e52dc004       str ip, [sp, #-4]!
48  e1a0e00f       mov lr, pc
52  e59ff048       ldr pc, [pc, #+72]          ;; debug: statement 0
                                                ;; code: STORE_IC,  
UNINITIALIZED
56  e58d0000       str r0, [sp, #+0]

Review URL: http://codereview.chromium.org/305005
http://code.google.com/p/v8/source/detail?r=3095

Added:
  /branches/bleeding_edge/test/mjsunit/compiler/globals.js
Modified:
  /branches/bleeding_edge/src/arm/codegen-arm.h
  /branches/bleeding_edge/src/arm/fast-codegen-arm.cc
  /branches/bleeding_edge/src/arm/virtual-frame-arm.cc
  /branches/bleeding_edge/src/compiler.cc
  /branches/bleeding_edge/src/ia32/codegen-ia32.h
  /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc
  /branches/bleeding_edge/src/x64/codegen-x64.h
  /branches/bleeding_edge/src/x64/fast-codegen-x64.cc

=======================================
--- /dev/null
+++ /branches/bleeding_edge/test/mjsunit/compiler/globals.js    Tue Oct 20  
06:37:26 2009
@@ -0,0 +1,44 @@
+// Copyright 2009 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Test references and assignments to global variables.
+var g = 0;
+
+// Test compilation of a global variable store.
+assertEquals(1, eval('g = 1'));
+// Test that the store worked.
+assertEquals(1, g);
+
+// Test that patching the IC in the compiled code works.
+assertEquals(1, eval('g = 1'));
+assertEquals(1, g);
+assertEquals(1, eval('g = 1'));
+assertEquals(1, g);
+
+// Test a second store.
+assertEquals("2", eval('g = "2"'));
+assertEquals("2", g);
=======================================
--- /branches/bleeding_edge/src/arm/codegen-arm.h       Thu Oct 15 08:27:37 2009
+++ /branches/bleeding_edge/src/arm/codegen-arm.h       Tue Oct 20 06:37:26 2009
@@ -242,7 +242,7 @@
    void LoadReference(Reference* ref);
    void UnloadReference(Reference* ref);

-  MemOperand ContextOperand(Register context, int index) const {
+  static MemOperand ContextOperand(Register context, int index) {
      return MemOperand(context, Context::SlotOffset(index));
    }

@@ -254,7 +254,7 @@
                                                 JumpTarget* slow);

    // Expressions
-  MemOperand GlobalObject() const  {
+  static MemOperand GlobalObject()  {
      return ContextOperand(cp, Context::GLOBAL_INDEX);
    }

@@ -425,6 +425,7 @@
    friend class VirtualFrame;
    friend class JumpTarget;
    friend class Reference;
+  friend class FastCodeGenerator;

    DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
  };
=======================================
--- /branches/bleeding_edge/src/arm/fast-codegen-arm.cc Mon Oct 19 03:36:42  
2009
+++ /branches/bleeding_edge/src/arm/fast-codegen-arm.cc Tue Oct 20 06:37:26  
2009
@@ -160,9 +160,10 @@
    Expression* rhs = expr->value();
    Visit(rhs);

-  // Left-hand side is always a (parameter or local) slot.
+  // Left-hand side can only be a global or a (parameter or local) slot.
    Variable* var = expr->target()->AsVariableProxy()->AsVariable();
-  ASSERT(var != NULL && var->slot() != NULL);
+  ASSERT(var != NULL);
+  ASSERT(var->is_global() || var->slot() != NULL);

    // Complete the assignment based on the location of the right-hand-side
    // value and the desired location of the assignment value.
@@ -171,27 +172,53 @@
    ASSERT(!destination.is_constant());
    ASSERT(!source.is_nowhere());

-  if (source.is_temporary()) {
+  if (var->is_global()) {
+    // Assignment to a global variable, use inline caching.   
Right-hand-side
+    // value is passed in r0, variable name in r2, and the global object on
+    // the stack.
+    if (source.is_temporary()) {
+      __ pop(r0);
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      __ mov(r0, Operand(rhs->AsLiteral()->handle()));
+    }
+    __ mov(r2, Operand(var->name()));
+    __ ldr(ip, CodeGenerator::GlobalObject());
+    __ push(ip);
+    Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
+    __ Call(ic, RelocInfo::CODE_TARGET);
+    // Overwrite the global object on the stack with the result if needed.
      if (destination.is_temporary()) {
-      // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side temporary
-      // on the stack.
-      __ ldr(ip, MemOperand(sp));
+      __ str(r0, MemOperand(sp));
      } else {
        ASSERT(destination.is_nowhere());
-      // Case 'var = temp'.  Discard right-hand-side temporary.
-      __ pop(ip);
-    }
-    __ str(ip, MemOperand(fp, SlotOffset(var->slot())));
+      __ pop();
+    }
+
    } else {
-    ASSERT(source.is_constant());
-    ASSERT(rhs->AsLiteral() != NULL);
-    // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
-    // discarded result.  Always perform the assignment.
-    __ mov(ip, Operand(rhs->AsLiteral()->handle()));
-    __ str(ip, MemOperand(fp, SlotOffset(var->slot())));
-    if (destination.is_temporary()) {
-      // Case 'temp <- (var = constant)'.  Save result.
-      __ push(ip);
+    if (source.is_temporary()) {
+      if (destination.is_temporary()) {
+        // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side
+        // temporary on the stack.
+        __ ldr(ip, MemOperand(sp));
+      } else {
+        ASSERT(destination.is_nowhere());
+        // Case 'var = temp'.  Discard right-hand-side temporary.
+        __ pop(ip);
+      }
+      __ str(ip, MemOperand(fp, SlotOffset(var->slot())));
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
+      // discarded result.  Always perform the assignment.
+      __ mov(ip, Operand(rhs->AsLiteral()->handle()));
+      __ str(ip, MemOperand(fp, SlotOffset(var->slot())));
+      if (destination.is_temporary()) {
+        // Case 'temp <- (var = constant)'.  Save result.
+        __ push(ip);
+      }
      }
    }
  }
=======================================
--- /branches/bleeding_edge/src/arm/virtual-frame-arm.cc        Sun Sep 13  
23:57:24 2009
+++ /branches/bleeding_edge/src/arm/virtual-frame-arm.cc        Tue Oct 20  
06:37:26 2009
@@ -255,7 +255,7 @@


  void VirtualFrame::RawCallCodeObject(Handle<Code> code,
-                                       RelocInfo::Mode rmode) {
+                                     RelocInfo::Mode rmode) {
    ASSERT(cgen()->HasValidEntryRegisters());
    __ Call(code, rmode);
  }
=======================================
--- /branches/bleeding_edge/src/compiler.cc     Tue Oct 20 02:12:53 2009
+++ /branches/bleeding_edge/src/compiler.cc     Tue Oct 20 06:37:26 2009
@@ -649,12 +649,14 @@
    }

    Variable* var = expr->target()->AsVariableProxy()->AsVariable();
-  if (var == NULL || var->is_global()) BAILOUT("non-variable assignment");
-
-  ASSERT(var->slot() != NULL);
-  Slot::Type type = var->slot()->type();
-  if (type != Slot::PARAMETER && type != Slot::LOCAL) {
-    BAILOUT("non-parameter/non-local slot assignment");
+  if (var == NULL) BAILOUT("non-variable assignment");
+
+  if (!var->is_global()) {
+    ASSERT(var->slot() != NULL);
+    Slot::Type type = var->slot()->type();
+    if (type != Slot::PARAMETER && type != Slot::LOCAL) {
+      BAILOUT("non-parameter/non-local slot assignment");
+    }
    }

    Visit(expr->value());
=======================================
--- /branches/bleeding_edge/src/ia32/codegen-ia32.h     Thu Oct 15 08:27:37 2009
+++ /branches/bleeding_edge/src/ia32/codegen-ia32.h     Tue Oct 20 06:37:26 2009
@@ -396,7 +396,7 @@
    void LoadReference(Reference* ref);
    void UnloadReference(Reference* ref);

-  Operand ContextOperand(Register context, int index) const {
+  static Operand ContextOperand(Register context, int index) {
      return Operand(context, Context::SlotOffset(index));
    }

@@ -407,7 +407,7 @@
                                              JumpTarget* slow);

    // Expressions
-  Operand GlobalObject() const {
+  static Operand GlobalObject() {
      return ContextOperand(esi, Context::GLOBAL_INDEX);
    }

@@ -616,6 +616,7 @@
    friend class JumpTarget;
    friend class Reference;
    friend class Result;
+  friend class FastCodeGenerator;

    friend class CodeGeneratorPatcher;  // Used in test-log-stack-tracer.cc

=======================================
--- /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc       Mon Oct 19  
03:36:42 2009
+++ /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc       Tue Oct 20  
06:37:26 2009
@@ -149,9 +149,10 @@
    Expression* rhs = expr->value();
    Visit(rhs);

-  // Left-hand side is always a (parameter or local) slot.
+  // Left-hand side can only be a global or a (parameter or local) slot.
    Variable* var = expr->target()->AsVariableProxy()->AsVariable();
-  ASSERT(var != NULL && var->slot() != NULL);
+  ASSERT(var != NULL);
+  ASSERT(var->is_global() || var->slot() != NULL);

    // Complete the assignment based on the location of the right-hand-side
    // value and the desired location of the assignment value.
@@ -160,27 +161,53 @@
    ASSERT(!destination.is_constant());
    ASSERT(!source.is_nowhere());

-  if (source.is_temporary()) {
+  if (var->is_global()) {
+    // Assignment to a global variable, use inline caching.   
Right-hand-side
+    // value is passed in eax, variable name in ecx, and the global object
+    // on the stack.
+    if (source.is_temporary()) {
+      __ pop(eax);
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      __ mov(eax, rhs->AsLiteral()->handle());
+    }
+    __ mov(ecx, var->name());
+    __ push(CodeGenerator::GlobalObject());
+    Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
+    __ call(ic, RelocInfo::CODE_TARGET);
+    // Overwrite the global object on the stack with the result if needed.
      if (destination.is_temporary()) {
-      // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side temporary
-      // on the stack.
-      __ mov(eax, Operand(esp, 0));
-      __ mov(Operand(ebp, SlotOffset(var->slot())), eax);
+      __ mov(Operand(esp, 0), eax);
      } else {
        ASSERT(destination.is_nowhere());
-      // Case 'var = temp'.  Discard right-hand-side temporary.
-      __ pop(Operand(ebp, SlotOffset(var->slot())));
-    }
+      __ pop(eax);
+    }
+
    } else {
-    ASSERT(source.is_constant());
-    ASSERT(rhs->AsLiteral() != NULL);
-    // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
-    // discarded result.  Always perform the assignment.
-    __ mov(eax, rhs->AsLiteral()->handle());
-    __ mov(Operand(ebp, SlotOffset(var->slot())), eax);
-    if (destination.is_temporary()) {
-      // Case 'temp <- (var = constant)'.  Save result.
-      __ push(eax);
+    // Local or parameter assignment.
+    if (source.is_temporary()) {
+      if (destination.is_temporary()) {
+        // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side
+        // temporary on the stack.
+        __ mov(eax, Operand(esp, 0));
+        __ mov(Operand(ebp, SlotOffset(var->slot())), eax);
+      } else {
+        ASSERT(destination.is_nowhere());
+        // Case 'var = temp'.  Discard right-hand-side temporary.
+        __ pop(Operand(ebp, SlotOffset(var->slot())));
+      }
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
+      // discarded result.  Always perform the assignment.
+      __ mov(eax, rhs->AsLiteral()->handle());
+      __ mov(Operand(ebp, SlotOffset(var->slot())), eax);
+      if (destination.is_temporary()) {
+        // Case 'temp <- (var = constant)'.  Save result.
+        __ push(eax);
+      }
      }
    }
  }
=======================================
--- /branches/bleeding_edge/src/x64/codegen-x64.h       Thu Oct 15 08:27:37 2009
+++ /branches/bleeding_edge/src/x64/codegen-x64.h       Tue Oct 20 06:37:26 2009
@@ -398,7 +398,7 @@
    void LoadReference(Reference* ref);
    void UnloadReference(Reference* ref);

-  Operand ContextOperand(Register context, int index) const {
+  static Operand ContextOperand(Register context, int index) {
      return Operand(context, Context::SlotOffset(index));
    }

@@ -409,7 +409,7 @@
                                              JumpTarget* slow);

    // Expressions
-  Operand GlobalObject() const {
+  static Operand GlobalObject() {
      return ContextOperand(rsi, Context::GLOBAL_INDEX);
    }

@@ -616,6 +616,7 @@
    friend class JumpTarget;
    friend class Reference;
    friend class Result;
+  friend class FastCodeGenerator;

    friend class CodeGeneratorPatcher;  // Used in test-log-stack-tracer.cc

=======================================
--- /branches/bleeding_edge/src/x64/fast-codegen-x64.cc Mon Oct 19 03:36:42  
2009
+++ /branches/bleeding_edge/src/x64/fast-codegen-x64.cc Tue Oct 20 06:37:26  
2009
@@ -166,9 +166,10 @@
    Expression* rhs = expr->value();
    Visit(rhs);

-  // Left-hand side is always a (parameter or local) slot.
+  // Left-hand side can only be a global or a (parameter or local) slot.
    Variable* var = expr->target()->AsVariableProxy()->AsVariable();
-  ASSERT(var != NULL && var->slot() != NULL);
+  ASSERT(var != NULL);
+  ASSERT(var->is_global() || var->slot() != NULL);

    // Complete the assignment based on the location of the right-hand-side
    // value and the desired location of the assignment value.
@@ -177,27 +178,50 @@
    ASSERT(!destination.is_constant());
    ASSERT(!source.is_nowhere());

-  if (source.is_temporary()) {
+  if (var->is_global()) {
+    // Assignment to a global variable, use inline caching.   
Right-hand-side
+    // value is passed in rax, variable name in rcx, and the global object
+    // on the stack.
+    if (source.is_temporary()) {
+      __ pop(rax);
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      __ Move(rax, rhs->AsLiteral()->handle());
+    }
+    __ Move(rcx, var->name());
+    __ push(CodeGenerator::GlobalObject());
+    Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
+    __ Call(ic, RelocInfo::CODE_TARGET);
+    // Overwrite the global object on the stack with the result if needed.
      if (destination.is_temporary()) {
-      // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side temporary
-      // on the stack.
-      __ movq(kScratchRegister, Operand(rsp, 0));
-      __ movq(Operand(rbp, SlotOffset(var->slot())), kScratchRegister);
+      __ movq(Operand(rsp, 0), rax);
      } else {
-      ASSERT(destination.is_nowhere());
-      // Case 'var = temp'.  Discard right-hand-side temporary.
-      __ pop(Operand(rbp, SlotOffset(var->slot())));
+      __ pop(rax);
      }
    } else {
-    ASSERT(source.is_constant());
-    ASSERT(rhs->AsLiteral() != NULL);
-    // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
-    // discarded result.  Always perform the assignment.
-    __ Move(kScratchRegister, rhs->AsLiteral()->handle());
-    __ movq(Operand(rbp, SlotOffset(var->slot())), kScratchRegister);
-    if (destination.is_temporary()) {
-      // Case 'temp <- (var = constant)'.  Save result.
-      __ push(kScratchRegister);
+    if (source.is_temporary()) {
+      if (destination.is_temporary()) {
+        // Case 'temp1 <- (var = temp0)'.  Preserve right-hand-side  
temporary
+        // on the stack.
+        __ movq(kScratchRegister, Operand(rsp, 0));
+        __ movq(Operand(rbp, SlotOffset(var->slot())), kScratchRegister);
+      } else {
+        ASSERT(destination.is_nowhere());
+        // Case 'var = temp'.  Discard right-hand-side temporary.
+        __ pop(Operand(rbp, SlotOffset(var->slot())));
+      }
+    } else {
+      ASSERT(source.is_constant());
+      ASSERT(rhs->AsLiteral() != NULL);
+      // Two cases: 'temp <- (var = constant)', or 'var = constant' with a
+      // discarded result.  Always perform the assignment.
+      __ Move(kScratchRegister, rhs->AsLiteral()->handle());
+      __ movq(Operand(rbp, SlotOffset(var->slot())), kScratchRegister);
+      if (destination.is_temporary()) {
+        // Case 'temp <- (var = constant)'.  Save result.
+        __ push(kScratchRegister);
+      }
      }
    }
  }

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to