[lld] r221583 - Add support library.

Shankar Easwaran shankare at codeaurora.org
Mon Nov 10 16:29:01 PST 2014


Yeah, I will move the original code to the linker script reader. As you 
mentioned,  the code handles other suffixes which are relevant for 
linker scripts.

I was trying to use just the parsing functionality to decode numbers 
without extensions support, which I could just use StringRef functionality.

Shankar Easwaran

On 11/10/2014 6:14 PM, Rafael Auler wrote:
> Well, this code understands a few linker script-specific suffixes, such as
> 'K' and 'M' for scales, and also recognizes 'o', 'h', etc... as base
> suffixes.
>
> On Mon, Nov 10, 2014 at 9:09 PM, Michael Spencer <bigcheesegs at gmail.com>
> wrote:
>
>> We already have this functionality in the llvm support library.
>> StringRef::getAs{Uns,S}ignedInteger().
>>
>> - Michael Spencer
>>
>>
>> On Mon, Nov 10, 2014 at 6:54 AM, Shankar Easwaran
>> <shankare at codeaurora.org> wrote:
>>> Author: shankare
>>> Date: Mon Nov 10 08:54:34 2014
>>> New Revision: 221583
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=221583&view=rev
>>> Log:
>>> Add support library.
>>>
>>> The parsing routines in the linker script to parse strings encoded in
>> various
>>> formats(hexadecimal, octal, decimal, etc), is needed by the GNU driver
>> too. This
>>> library provides helper functions for all flavors and flavors to add
>> helper
>>> functions which other flavors may make use of.
>>>
>>> Added:
>>>      lld/trunk/include/lld/Support/
>>>      lld/trunk/include/lld/Support/NumParse.h
>>>      lld/trunk/lib/Support/
>>>      lld/trunk/lib/Support/CMakeLists.txt
>>>      lld/trunk/lib/Support/NumParse.cpp
>>> Modified:
>>>      lld/trunk/lib/CMakeLists.txt
>>>      lld/trunk/lib/Makefile
>>>      lld/trunk/lib/ReaderWriter/CMakeLists.txt
>>>      lld/trunk/lib/ReaderWriter/LinkerScript.cpp
>>>      lld/trunk/tools/lld/Makefile
>>>      lld/trunk/unittests/DriverTests/Makefile
>>>      lld/trunk/utils/linker-script-test/Makefile
>>>
>>> Added: lld/trunk/include/lld/Support/NumParse.h
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Support/NumParse.h?rev=221583&view=auto
>> ==============================================================================
>>> --- lld/trunk/include/lld/Support/NumParse.h (added)
>>> +++ lld/trunk/include/lld/Support/NumParse.h Mon Nov 10 08:54:34 2014
>>> @@ -0,0 +1,45 @@
>>> +//===-- lld/Support/NumParse.h - Number parsing -----------------*- C++
>> -*-===//
>>> +//
>>> +//                     The LLVM Compiler Infrastructure
>>> +//
>>> +// This file is distributed under the University of Illinois Open Source
>>> +// License. See LICENSE.TXT for details.
>>> +//
>>>
>> +//===----------------------------------------------------------------------===//
>>> +///
>>> +/// \file
>>> +/// \brief Parses string in various formats to decimal.
>>> +///
>>>
>> +//===----------------------------------------------------------------------===//
>>> +
>>> +#ifndef LLD_SUPPORT_NUM_PARSE_H
>>> +#define LLD_SUPPORT_NUM_PARSE_H
>>> +
>>> +#include "lld/Core/LLVM.h"
>>> +#include "llvm/ADT/StringSwitch.h"
>>> +#include "llvm/Support/ErrorOr.h"
>>> +#include "llvm/Support/raw_ostream.h"
>>> +#include <memory>
>>> +#include <system_error>
>>> +#include <vector>
>>> +
>>> +namespace lld {
>>> +
>>> +/// \brief Convert a string in decimal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseDecimal(StringRef str);
>>> +
>>> +/// \brief Convert a string in octal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseOctal(StringRef str);
>>> +
>>> +/// \brief Convert a string in Binary to decimal.
>>> +llvm::ErrorOr<uint64_t> parseBinary(StringRef str);
>>> +
>>> +/// \brief Convert a string in Hexadecimal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseHex(StringRef str);
>>> +
>>> +/// \brief Parse a number represested in a string as
>>> +//  Hexadecimal, Octal, Binary or Decimal to decimal
>>> +llvm::ErrorOr<uint64_t> parseNum(StringRef str, bool parseExtensions =
>> true);
>>> +}
>>> +
>>> +#endif // LLD_SUPPORT_NUM_PARSE_H
>>>
>>> Modified: lld/trunk/lib/CMakeLists.txt
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/CMakeLists.txt?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/lib/CMakeLists.txt (original)
>>> +++ lld/trunk/lib/CMakeLists.txt Mon Nov 10 08:54:34 2014
>>> @@ -3,3 +3,4 @@ add_subdirectory(Core)
>>>   add_subdirectory(Driver)
>>>   add_subdirectory(Passes)
>>>   add_subdirectory(ReaderWriter)
>>> +add_subdirectory(Support)
>>>
>>> Modified: lld/trunk/lib/Makefile
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Makefile?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/lib/Makefile (original)
>>> +++ lld/trunk/lib/Makefile Mon Nov 10 08:54:34 2014
>>> @@ -9,7 +9,7 @@
>>>   LLD_LEVEL := ..
>>>
>>>   # ARCMigrate and Rewrite are always needed because of libclang.
>>> -PARALLEL_DIRS = Config Core Driver Passes ReaderWriter
>>> +PARALLEL_DIRS = Config Core Driver Passes ReaderWriter Support
>>>
>>>   include $(LLD_LEVEL)/../../Makefile.config
>>>
>>>
>>> Modified: lld/trunk/lib/ReaderWriter/CMakeLists.txt
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/CMakeLists.txt?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/lib/ReaderWriter/CMakeLists.txt (original)
>>> +++ lld/trunk/lib/ReaderWriter/CMakeLists.txt Mon Nov 10 08:54:34 2014
>>> @@ -18,4 +18,5 @@ add_lld_library(lldReaderWriter
>>>   target_link_libraries(lldReaderWriter ${cmake_2_8_12_INTERFACE}
>>>     lldCore
>>>     lldPasses
>>> +  lldSupport
>>>     )
>>>
>>> Modified: lld/trunk/lib/ReaderWriter/LinkerScript.cpp
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/LinkerScript.cpp?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/lib/ReaderWriter/LinkerScript.cpp (original)
>>> +++ lld/trunk/lib/ReaderWriter/LinkerScript.cpp Mon Nov 10 08:54:34 2014
>>> @@ -13,6 +13,7 @@
>>>
>> //===----------------------------------------------------------------------===//
>>>   #include "lld/ReaderWriter/LinkerScript.h"
>>> +#include "lld/Support/NumParse.h"
>>>
>>>   namespace lld {
>>>   namespace script {
>>> @@ -89,56 +90,7 @@ void Token::dump(raw_ostream &os) const
>>>     os << _range << "\n";
>>>   }
>>>
>>> -static llvm::ErrorOr<uint64_t> parseDecimal(StringRef str) {
>>> -  uint64_t res = 0;
>>> -  for (auto &c : str) {
>>> -    res *= 10;
>>> -    if (c < '0' || c > '9')
>>> -      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> -    res += c - '0';
>>> -  }
>>> -  return res;
>>> -}
>>> -
>>> -static llvm::ErrorOr<uint64_t> parseOctal(StringRef str) {
>>> -  uint64_t res = 0;
>>> -  for (auto &c : str) {
>>> -    res <<= 3;
>>> -    if (c < '0' || c > '7')
>>> -      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> -    res += c - '0';
>>> -  }
>>> -  return res;
>>> -}
>>> -
>>> -static llvm::ErrorOr<uint64_t> parseBinary(StringRef str) {
>>> -  uint64_t res = 0;
>>> -  for (auto &c : str) {
>>> -    res <<= 1;
>>> -    if (c != '0' && c != '1')
>>> -      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> -    res += c - '0';
>>> -  }
>>> -  return res;
>>> -}
>>> -
>>> -static llvm::ErrorOr<uint64_t> parseHex(StringRef str) {
>>> -  uint64_t res = 0;
>>> -  for (auto &c : str) {
>>> -    res <<= 4;
>>> -    if (c >= '0' && c <= '9')
>>> -      res += c - '0';
>>> -    else if (c >= 'a' && c <= 'f')
>>> -      res += c - 'a' + 10;
>>> -    else if (c >= 'A' && c <= 'F')
>>> -      res += c - 'A' + 10;
>>> -    else
>>> -      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> -  }
>>> -  return res;
>>> -}
>>> -
>>> -static bool parseHexToByteStream(StringRef str, std::string &buf) {
>>> +bool parseHexToByteStream(StringRef str, std::string &buf) {
>>>     unsigned char byte = 0;
>>>     bool dumpByte = str.size() % 2;
>>>     for (auto &c : str) {
>>> @@ -178,63 +130,6 @@ static void dumpByteStream(raw_ostream &
>>>     }
>>>   }
>>>
>>> -static llvm::ErrorOr<uint64_t> parseNum(StringRef str) {
>>> -  unsigned multiplier = 1;
>>> -  enum NumKind { decimal, hex, octal, binary };
>>> -  NumKind kind = llvm::StringSwitch<NumKind>(str)
>>> -                     .StartsWith("0x", hex)
>>> -                     .StartsWith("0X", hex)
>>> -                     .StartsWith("0", octal)
>>> -                     .Default(decimal);
>>> -
>>> -  // Parse scale
>>> -  if (str.endswith("K")) {
>>> -    multiplier = 1 << 10;
>>> -    str = str.drop_back();
>>> -  } else if (str.endswith("M")) {
>>> -    multiplier = 1 << 20;
>>> -    str = str.drop_back();
>>> -  }
>>> -
>>> -  // Parse type
>>> -  if (str.endswith_lower("o")) {
>>> -    kind = octal;
>>> -    str = str.drop_back();
>>> -  } else if (str.endswith_lower("h")) {
>>> -    kind = hex;
>>> -    str = str.drop_back();
>>> -  } else if (str.endswith_lower("d")) {
>>> -    kind = decimal;
>>> -    str = str.drop_back();
>>> -  } else if (str.endswith_lower("b")) {
>>> -    kind = binary;
>>> -    str = str.drop_back();
>>> -  }
>>> -
>>> -  llvm::ErrorOr<uint64_t> res(0);
>>> -  switch (kind) {
>>> -  case hex:
>>> -    if (str.startswith_lower("0x"))
>>> -      str = str.drop_front(2);
>>> -    res = parseHex(str);
>>> -    break;
>>> -  case octal:
>>> -    res = parseOctal(str);
>>> -    break;
>>> -  case decimal:
>>> -    res = parseDecimal(str);
>>> -    break;
>>> -  case binary:
>>> -    res = parseBinary(str);
>>> -    break;
>>> -  }
>>> -  if (res.getError())
>>> -    return res;
>>> -
>>> -  *res = *res * multiplier;
>>> -  return res;
>>> -}
>>> -
>>>   bool Lexer::canStartNumber(char c) const {
>>>     return '0' <= c && c <= '9';
>>>   }
>>>
>>> Added: lld/trunk/lib/Support/CMakeLists.txt
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Support/CMakeLists.txt?rev=221583&view=auto
>> ==============================================================================
>>> --- lld/trunk/lib/Support/CMakeLists.txt (added)
>>> +++ lld/trunk/lib/Support/CMakeLists.txt Mon Nov 10 08:54:34 2014
>>> @@ -0,0 +1,3 @@
>>> +add_lld_library(lldSupport
>>> +  NumParse.cpp
>>> +  )
>>>
>>> Added: lld/trunk/lib/Support/NumParse.cpp
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Support/NumParse.cpp?rev=221583&view=auto
>> ==============================================================================
>>> --- lld/trunk/lib/Support/NumParse.cpp (added)
>>> +++ lld/trunk/lib/Support/NumParse.cpp Mon Nov 10 08:54:34 2014
>>> @@ -0,0 +1,133 @@
>>> +//===-- lld/Support/NumParse.cpp - Number parsing ---------------*- C++
>> -*-===//
>>> +//
>>> +//                     The LLVM Compiler Infrastructure
>>> +//
>>> +// This file is distributed under the University of Illinois Open Source
>>> +// License. See LICENSE.TXT for details.
>>> +//
>>>
>> +//===----------------------------------------------------------------------===//
>>> +///
>>> +/// \file
>>> +/// \brief Parses string in various formats to decimal.
>>> +///
>>>
>> +//===----------------------------------------------------------------------===//
>>> +
>>> +#include "lld/Support/NumParse.h"
>>> +
>>> +using namespace llvm;
>>> +
>>> +namespace lld {
>>> +/// \brief Convert a string in decimal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseDecimal(StringRef str) {
>>> +  uint64_t res = 0;
>>> +  for (auto &c : str) {
>>> +    res *= 10;
>>> +    if (c < '0' || c > '9')
>>> +      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> +    res += c - '0';
>>> +  }
>>> +  return res;
>>> +}
>>> +
>>> +/// \brief Convert a string in octal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseOctal(StringRef str) {
>>> +  uint64_t res = 0;
>>> +  for (auto &c : str) {
>>> +    res <<= 3;
>>> +    if (c < '0' || c > '7')
>>> +      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> +    res += c - '0';
>>> +  }
>>> +  return res;
>>> +}
>>> +
>>> +/// \brief Convert a string in Binary to decimal.
>>> +llvm::ErrorOr<uint64_t> parseBinary(StringRef str) {
>>> +  uint64_t res = 0;
>>> +  for (auto &c : str) {
>>> +    res <<= 1;
>>> +    if (c != '0' && c != '1')
>>> +      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> +    res += c - '0';
>>> +  }
>>> +  return res;
>>> +}
>>> +
>>> +/// \brief Convert a string in Hexadecimal to decimal.
>>> +llvm::ErrorOr<uint64_t> parseHex(StringRef str) {
>>> +  uint64_t res = 0;
>>> +  for (auto &c : str) {
>>> +    res <<= 4;
>>> +    if (c >= '0' && c <= '9')
>>> +      res += c - '0';
>>> +    else if (c >= 'a' && c <= 'f')
>>> +      res += c - 'a' + 10;
>>> +    else if (c >= 'A' && c <= 'F')
>>> +      res += c - 'A' + 10;
>>> +    else
>>> +      return
>> llvm::ErrorOr<uint64_t>(std::make_error_code(std::errc::io_error));
>>> +  }
>>> +  return res;
>>> +}
>>> +
>>> +/// \brief Parse a number represested in a string as
>>> +//  Hexadecimal, Octal, Binary or Decimal to decimal
>>> +llvm::ErrorOr<uint64_t> parseNum(StringRef str, bool parseExtensions) {
>>> +  unsigned multiplier = 1;
>>> +  enum NumKind { decimal, hex, octal, binary };
>>> +  NumKind kind = llvm::StringSwitch<NumKind>(str)
>>> +                     .StartsWith("0x", hex)
>>> +                     .StartsWith("0X", hex)
>>> +                     .StartsWith("0", octal)
>>> +                     .Default(decimal);
>>> +
>>> +  if (parseExtensions) {
>>> +    // Parse scale
>>> +    if (str.endswith("K")) {
>>> +      multiplier = 1 << 10;
>>> +      str = str.drop_back();
>>> +    } else if (str.endswith("M")) {
>>> +      multiplier = 1 << 20;
>>> +      str = str.drop_back();
>>> +    }
>>> +
>>> +    // Parse type
>>> +    if (str.endswith_lower("o")) {
>>> +      kind = octal;
>>> +      str = str.drop_back();
>>> +    } else if (str.endswith_lower("h")) {
>>> +      kind = hex;
>>> +      str = str.drop_back();
>>> +    } else if (str.endswith_lower("d")) {
>>> +      kind = decimal;
>>> +      str = str.drop_back();
>>> +    } else if (str.endswith_lower("b")) {
>>> +      kind = binary;
>>> +      str = str.drop_back();
>>> +    }
>>> +  }
>>> +
>>> +  llvm::ErrorOr<uint64_t> res(0);
>>> +  switch (kind) {
>>> +  case hex:
>>> +    if (str.startswith_lower("0x"))
>>> +      str = str.drop_front(2);
>>> +    res = parseHex(str);
>>> +    break;
>>> +  case octal:
>>> +    res = parseOctal(str);
>>> +    break;
>>> +  case decimal:
>>> +    res = parseDecimal(str);
>>> +    break;
>>> +  case binary:
>>> +    res = parseBinary(str);
>>> +    break;
>>> +  }
>>> +  if (res.getError())
>>> +    return res;
>>> +
>>> +  *res = *res * multiplier;
>>> +  return res;
>>> +}
>>> +}
>>>
>>> Modified: lld/trunk/tools/lld/Makefile
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/tools/lld/Makefile?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/tools/lld/Makefile (original)
>>> +++ lld/trunk/tools/lld/Makefile Mon Nov 10 08:54:34 2014
>>> @@ -19,9 +19,9 @@ LEVEL := $(LLD_LEVEL)/../..
>>>   include $(LEVEL)/Makefile.config
>>>
>>>   LINK_COMPONENTS := $(TARGETS_TO_BUILD)
>>> -USEDLIBS = lldDriver.a lldConfig.a \
>>> +USEDLIBS = lldDriver.a lldConfig.a lldSupport.a \
>>>              lldELF.a lldMachO.a lldPasses.a lldPECOFF.a lldYAML.a \
>>> -           lldReaderWriter.a lldCore.a lldNative.a \
>>> +           lldReaderWriter.a lldCore.a lldSupport.a lldNative.a \
>>>              lldHexagonELFTarget.a lldPPCELFTarget.a lldMipsELFTarget.a \
>>>              lldX86ELFTarget.a lldX86_64ELFTarget.a lldAArch64ELFTarget.a
>> \
>>>              LLVMOption.a
>>>
>>> Modified: lld/trunk/unittests/DriverTests/Makefile
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/Makefile?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/unittests/DriverTests/Makefile (original)
>>> +++ lld/trunk/unittests/DriverTests/Makefile Mon Nov 10 08:54:34 2014
>>> @@ -15,5 +15,7 @@ USEDLIBS = lldDriver.a lldConfig.a \
>>>              lldHexagonELFTarget.a lldPPCELFTarget.a lldMipsELFTarget.a \
>>>              lldX86ELFTarget.a lldX86_64ELFTarget.a lldYAML.a \
>>>              LLVMObject.a LLVMMCParser.a LLVMMC.a LLVMBitReader.a \
>>> -           LLVMCore.a LLVMOption.a LLVMSupport.a lldAArch64ELFTarget.a
>>> +           LLVMCore.a LLVMOption.a LLVMSupport.a lldAArch64ELFTarget.a \
>>> +          lldSupport.a
>>> +
>>>   include $(LLD_LEVEL)/unittests/Makefile
>>>
>>> Modified: lld/trunk/utils/linker-script-test/Makefile
>>> URL:
>> http://llvm.org/viewvc/llvm-project/lld/trunk/utils/linker-script-test/Makefile?rev=221583&r1=221582&r2=221583&view=diff
>> ==============================================================================
>>> --- lld/trunk/utils/linker-script-test/Makefile (original)
>>> +++ lld/trunk/utils/linker-script-test/Makefile Mon Nov 10 08:54:34 2014
>>> @@ -19,6 +19,6 @@ LEVEL := $(LLD_LEVEL)/../..
>>>   include $(LEVEL)/Makefile.config
>>>
>>>   LINK_COMPONENTS := $(TARGETS_TO_BUILD)
>>> -USEDLIBS = lldReaderWriter.a LLVMSupport.a
>>> +USEDLIBS = lldReaderWriter.a lldSupport.a LLVMSupport.a
>>>
>>>   include $(LLD_LEVEL)/Makefile
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>


-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by the Linux Foundation




More information about the llvm-commits mailing list