<commit_before>// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "llvm_sim/x86/faucon_lib.h"

#include <iostream>

#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm_sim/x86/constants.h"

namespace exegesis {
namespace simulator {

namespace {

// IACA magic markers.
constexpr char kBeginMagicMarker[] = {0x0f, 0x0b, 0xbb, 0x6f, 0x00,
                                      0x00, 0x00, 0x64, 0x67, 0x90};
constexpr char kEndMagicMarker[] = {0xbb, 0xde, 0x00, 0x00, 0x00,
                                    0x64, 0x67, 0x90, 0x0f, 0x0b};

}  // namespace

std::vector<uint8_t> GetIACAMarkedCode(const std::string& FileName) {
  // Open binary and search for marker.
  auto BinaryOrErr = llvm::object::createBinary(FileName);
  if (!BinaryOrErr) {
    std::cerr << "could not open binary: " << FileName << "\n";
    return {};
  }
  const auto* const Obj =
      llvm::dyn_cast<llvm::object::ObjectFile>(BinaryOrErr.get().getBinary());
  if (!Obj) {
    std::cerr << "not an object file\n";
    return {};
  }
  std::cout << "  format: '" << Obj->getFileFormatName().str() << "'\n";
  std::cout << "  arch: '"
            << llvm::Triple::getArchTypeName(Obj->getArch()).str() << "'\n";

  // Only search code (a.k.a text) sections. Other sections might contains the
  // magic markers for random reasons.<commit_msg>Fixed compilation of faucon_lib under GCC 7.3.0.<commit_after>// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "llvm_sim/x86/faucon_lib.h"

#include <iostream>

#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm_sim/x86/constants.h"

namespace exegesis {
namespace simulator {

namespace {

// IACA magic markers.
// NOTE(ondrasej): This format is required for GCC 7.3.0 which does not accept
// numbers over 127 as char literals. On the other hand, this can't be an
// unsigned char array, because SectionRef::getContents returns the data as
// StringRef and std::search would do char/unsigned char comparisons which are
// ill-defined when char is signed.
constexpr char kBeginMagicMarker[] = {'\x0f', '\x0b', '\xbb', '\x6f', '\x00',
                                      '\x00', '\x00', '\x64', '\x67', '\x90'};
constexpr char kEndMagicMarker[] = {'\xbb', '\xde', '\x00', '\x00', '\x00',
                                    '\x64', '\x67', '\x90', '\x0f', '\x0b'};

}  // namespace

std::vector<uint8_t> GetIACAMarkedCode(const std::string& FileName) {
  // Open binary and search for marker.
  auto BinaryOrErr = llvm::object::createBinary(FileName);
  if (!BinaryOrErr) {
    std::cerr << "could not open binary: " << FileName << "\n";
    return {};
  }
  const auto* const Obj =
      llvm::dyn_cast<llvm::object::ObjectFile>(BinaryOrErr.get().getBinary());
  if (!Obj) {
    std::cerr << "not an object file\n";
    return {};
  }
  std::cout << "  format: '" << Obj->getFileFormatName().str() << "'\n";
  std::cout << "  arch: '"
            << llvm::Triple::getArchTypeName(Obj->getArch()).str() << "'\n";

  // Only search code (a.k.a text) sections. Other sections might contains the
  // magic markers for random reasons.
{'fraction_non_alphanumeric': 0.14911024749437513, 'fraction_numerical': 0.0231131110656576, 'mean_word_length': 3.4093778178539225, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2820621', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1613, 'n_words': 467}