[llvm-commits] CVS: llvm/lib/VMCore/AsmWriter.cpp

Chris Lattner clattner at apple.com
Mon May 21 18:30:09 PDT 2007


On May 19, 2007, at 12:25 AM, Reid Spencer wrote:

>
> Adjust how LLVM names are produced:
> 1. Always use % for local and @ for global.
> 2. Replace NameNeedsQuotes with QuoteNameIfNeeded so that any  
> adjustments
>    to the name can be done in one pass.
> 3. Implement generation of hex escapes so we don't get "wonky"  
> characters
>    in the output.

Cool, thanks Reid!

> +static std::string QuoteNameIfNeeded(const std::string &Name) {
> +  std::string result;
> +  bool needsQuotes = Name[0] >= '0' && Name[0] <= '9';
> +  // Scan the name to see if it needs quotes and to replace funky  
> chars with
> +  // their octal equivalent.
>    for (unsigned i = 0, e = Name.size(); i != e; ++i) {
>      char C = Name[i];
>      assert(C != '"' && "Illegal character in LLVM value name!");
> +    if (isalnum(C) || C == '-' || C == '.' || C == '_')
> +      result += C;
> +    else if (isprint(C)) {

Shouldn't this be "isprint(c) && C != '\\'

?

Otherwise, you won't escape the quote in "\10" correctly.

-Chris

> +      needsQuotes = true;
> +      result += C;
> +    } else {
> +      needsQuotes = true;
> +      result += "\\";
> +      char hex1 = C & 0x0F;
> +      if (hex1 < 10)
> +        result += hex1 + '0';
> +      else
> +        result += hex1 - 10 + 'A';
> +      char hex2 = (C >> 4) & 0x0F;
> +      if (hex2 < 10)
> +        result += hex2 + '0';
> +      else
> +        result += hex2 - 10 + 'A';
> +    }
> +  }
> +  if (needsQuotes) {
> +    result.insert(0,"\"");
> +    result += '"';
>    }
> -  return false;
> +  return result;
>  }
>
>  enum PrefixType {
> @@ -202,20 +225,11 @@
>  /// surrounded with ""'s (if it has special chars in it).
>  static std::string getLLVMName(const std::string &Name, PrefixType  
> Prefix) {
>    assert(!Name.empty() && "Cannot get empty name!");
> -
> -  // First character cannot start with a number...
> -  if (NameNeedsQuotes(Name)) {
> -    if (Prefix == GlobalPrefix)
> -      return "@\"" + Name + "\"";
> -    return "\"" + Name + "\"";
> -  }
> -
> -  // If we get here, then the identifier is legal to use as a  
> "VarID".
>    switch (Prefix) {
>    default: assert(0 && "Bad prefix!");
> -  case GlobalPrefix: return '@' + Name;
> -  case LabelPrefix:  return Name;
> -  case LocalPrefix:  return '%' + Name;
> +  case GlobalPrefix: return '@' + QuoteNameIfNeeded(Name);
> +  case LabelPrefix:  return QuoteNameIfNeeded(Name);
> +  case LocalPrefix:  return '%' + QuoteNameIfNeeded(Name);
>    }
>  }
>
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list