mharoush created this revision.
mharoush added a project: clang-c.
Herald added a subscriber: eraman.

This patch enables the usage of constant Enum identifiers within Microsoft 
style inline assembly statements.

part 1 out of 2.


Repository:
  rL LLVM

https://reviews.llvm.org/D33277

Files:
  lib/Parse/ParseStmtAsm.cpp
  test/CodeGen/x86-ms-inline-asm-enum_feature.cpp


Index: lib/Parse/ParseStmtAsm.cpp
===================================================================
--- lib/Parse/ParseStmtAsm.cpp
+++ lib/Parse/ParseStmtAsm.cpp
@@ -94,6 +94,23 @@
     return Info.OpDecl;
   }
 
+  // Try to evaluate the inline asm identifier lookup result as an
+  // enumerated type. This method expects LookupResult to be the
+  // result of a call to LookupInlineAsmIdentifier.
+  bool EvaluateLookupAsEnum(void *LookupResult, int64_t &Result) {
+    if (!LookupResult) return false;
+    Expr *Res = static_cast<Expr*> (LookupResult);
+    if (Res && isa<clang::EnumType>(Res->getType())) {
+      Expr::EvalResult EvlResult;
+      if (Res->EvaluateAsRValue(EvlResult,
+          TheParser.getActions().getASTContext())) {
+        Result = EvlResult.Val.getInt().getExtValue();
+        return true;
+      }
+    }
+    return false;
+  }
+
   StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM,
                                  llvm::SMLoc Location,
                                  bool Create) override {
Index: test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
===================================================================
--- test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
+++ test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
@@ -0,0 +1,50 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 %s -fasm-blocks -emit-llvm -o - | FileCheck %s
+namespace x {
+enum { A = 12 };
+namespace y {
+enum { A = 17 };
+}
+}
+
+void x86_enum_only(){
+  const int a = 0;
+  // CHECK-NOT: mov eax, [$$0]
+  __asm mov eax, [a]
+}
+
+void x86_enum_namespaces() {
+  enum { A = 1 };
+  // CHECK: mov eax, $$12
+  __asm mov eax, x::A
+  // CHECK: mov eax, $$17
+  __asm mov eax, x::y::A
+  // CHECK: mov eax, $$1
+  __asm mov eax, A
+}
+
+void x86_enum_arithmethic() {
+  enum { A = 1, B };
+  // CHECK: mov eax, $$21
+  __asm mov eax, (A + 9) * 2 + A
+  // CHECK: mov eax, $$4
+  __asm mov eax, A << 2
+  // CHECK: mov eax, $$2
+  __asm mov eax, B & 3
+  // CHECK: mov eax, $$5
+  __asm mov eax, 3 + (B & 3)
+  // CHECK: mov eax, $$8
+  __asm mov eax, 2 << A * B
+}
+
+void x86_enum_mem() {
+  int arr[4];
+  enum { A = 4, B };
+  
+  // CHECK: mov eax, [($$12 + $$9) + $$4 * $$5 + $$3 + $$3 + eax]
+  __asm { mov eax, [(x::A + 9) + A * B + 3 + 3 + eax] }
+  // CHECK: mov eax, dword ptr $$4$0
+  __asm { mov eax, [arr + A] }
+  // CHECK: mov eax, dword ptr $$8$0
+  __asm { mov eax, A[arr + A] }
+}
\ No newline at end of file


Index: lib/Parse/ParseStmtAsm.cpp
===================================================================
--- lib/Parse/ParseStmtAsm.cpp
+++ lib/Parse/ParseStmtAsm.cpp
@@ -94,6 +94,23 @@
     return Info.OpDecl;
   }
 
+  // Try to evaluate the inline asm identifier lookup result as an
+  // enumerated type. This method expects LookupResult to be the
+  // result of a call to LookupInlineAsmIdentifier.
+  bool EvaluateLookupAsEnum(void *LookupResult, int64_t &Result) {
+    if (!LookupResult) return false;
+    Expr *Res = static_cast<Expr*> (LookupResult);
+    if (Res && isa<clang::EnumType>(Res->getType())) {
+      Expr::EvalResult EvlResult;
+      if (Res->EvaluateAsRValue(EvlResult,
+          TheParser.getActions().getASTContext())) {
+        Result = EvlResult.Val.getInt().getExtValue();
+        return true;
+      }
+    }
+    return false;
+  }
+
   StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM,
                                  llvm::SMLoc Location,
                                  bool Create) override {
Index: test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
===================================================================
--- test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
+++ test/CodeGen/x86-ms-inline-asm-enum_feature.cpp
@@ -0,0 +1,50 @@
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 %s -fasm-blocks -emit-llvm -o - | FileCheck %s
+namespace x {
+enum { A = 12 };
+namespace y {
+enum { A = 17 };
+}
+}
+
+void x86_enum_only(){
+  const int a = 0;
+  // CHECK-NOT: mov eax, [$$0]
+  __asm mov eax, [a]
+}
+
+void x86_enum_namespaces() {
+  enum { A = 1 };
+  // CHECK: mov eax, $$12
+  __asm mov eax, x::A
+  // CHECK: mov eax, $$17
+  __asm mov eax, x::y::A
+  // CHECK: mov eax, $$1
+  __asm mov eax, A
+}
+
+void x86_enum_arithmethic() {
+  enum { A = 1, B };
+  // CHECK: mov eax, $$21
+  __asm mov eax, (A + 9) * 2 + A
+  // CHECK: mov eax, $$4
+  __asm mov eax, A << 2
+  // CHECK: mov eax, $$2
+  __asm mov eax, B & 3
+  // CHECK: mov eax, $$5
+  __asm mov eax, 3 + (B & 3)
+  // CHECK: mov eax, $$8
+  __asm mov eax, 2 << A * B
+}
+
+void x86_enum_mem() {
+  int arr[4];
+  enum { A = 4, B };
+  
+  // CHECK: mov eax, [($$12 + $$9) + $$4 * $$5 + $$3 + $$3 + eax]
+  __asm { mov eax, [(x::A + 9) + A * B + 3 + 3 + eax] }
+  // CHECK: mov eax, dword ptr $$4$0
+  __asm { mov eax, [arr + A] }
+  // CHECK: mov eax, dword ptr $$8$0
+  __asm { mov eax, A[arr + A] }
+}
\ No newline at end of file
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to