[PATCH] D79991: [TableGen] Fix non-standard escape warnings for braces in InstAlias

Cullen Rhodes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 15 03:02:43 PDT 2020


c-rhodes created this revision.
c-rhodes added reviewers: stoklund, craig.topper.
Herald added a project: LLVM.

TableGen interprets braces ('{}') in the asm string of instruction aliases as
variants but when defining aliases with literal braces they have to be escaped
to prevent them being removed.

Braces are escaped with '\\', for example:

  def FooBraces : InstAlias<"foo \\{$imm\\}", (foo IntOperand:$imm)>;

Although when TableGen is emitting the assembly writer (-gen-asm-writer)
the AsmString that gets emitted is:

  AsmString = "foo \{$\x01\}";

In c/c++ braces don't need to be escaped which causes compilation
warnings:

  warning: use of non-standard escape character '\{' [-Wpedantic]

This patch fixes the issue by unescaping the flattened alias asm string
in the asm writer, by replacing '\{\}' with '{}'.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79991

Files:
  llvm/test/TableGen/AliasAsmString.td
  llvm/utils/TableGen/AsmWriterEmitter.cpp


Index: llvm/utils/TableGen/AsmWriterEmitter.cpp
===================================================================
--- llvm/utils/TableGen/AsmWriterEmitter.cpp
+++ llvm/utils/TableGen/AsmWriterEmitter.cpp
@@ -258,6 +258,8 @@
       case 't': Str[i] = '\t'; break;
       case 'v': Str[i] = '\v'; break;
       case '"': Str[i] = '\"'; break;
+      case '{': Str[i] = '{'; break;
+      case '}': Str[i] = '}'; break;
       case '\'': Str[i] = '\''; break;
       case '\\': Str[i] = '\\'; break;
       }
@@ -803,6 +805,7 @@
 
       std::string FlatAliasAsmString =
           CodeGenInstruction::FlattenAsmStringVariants(CGA.AsmString, Variant);
+      UnescapeString(FlatAliasAsmString);
 
       // Don't emit the alias if it has more operands than what it's aliasing.
       if (NumResultOps < CountNumOperands(FlatAliasAsmString, Variant))
Index: llvm/test/TableGen/AliasAsmString.td
===================================================================
--- /dev/null
+++ llvm/test/TableGen/AliasAsmString.td
@@ -0,0 +1,28 @@
+// RUN: llvm-tblgen -gen-asm-writer -I %p/../../include %s | FileCheck %s
+
+include "llvm/Target/Target.td"
+
+def ArchInstrInfo : InstrInfo { }
+
+def Arch : Target {
+  let InstructionSet = ArchInstrInfo;
+}
+
+def Reg : Register<"reg">;
+
+def RegClass : RegisterClass<"foo", [i32], 0, (add Reg)>;
+
+def IntOperand: Operand<i32>;
+
+def foo : Instruction {
+  let Size = 2;
+  let OutOperandList = (outs);
+  let InOperandList = (ins IntOperand:$imm);
+  let AsmString = "foo $imm";
+  let Namespace = "Arch";
+}
+
+def FooBraces : InstAlias<"foo \\{$imm\\}", (foo IntOperand:$imm)>;
+
+// CHECK: static const char AsmStrings[] =
+// CHECK-NEXT: /* 0 */ "foo {$\x01}\0"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79991.264195.patch
Type: text/x-patch
Size: 1709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200515/3b84c53f/attachment.bin>


More information about the llvm-commits mailing list