10b57cec5SDimitry Andric //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file provides useful services for TableGen backends... 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 140b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 16*fe013be4SDimitry Andric #include <algorithm> 175ffd83dbSDimitry Andric #include <cassert> 18*fe013be4SDimitry Andric #include <cstddef> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace llvm; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric const size_t MAX_LINE_LEN = 80U; 230b57cec5SDimitry Andric 24*fe013be4SDimitry Andric namespace llvm::TableGen::Emitter { 25*fe013be4SDimitry Andric ManagedStatic<cl::opt<FnT>, OptCreatorT> Action; 26*fe013be4SDimitry Andric void *OptCreatorT::call() { 27*fe013be4SDimitry Andric return new cl::opt<FnT>(cl::desc("Action to perform:")); 28*fe013be4SDimitry Andric } 29*fe013be4SDimitry Andric } // namespace llvm::TableGen::Emitter 30*fe013be4SDimitry Andric 310b57cec5SDimitry Andric static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill, 320b57cec5SDimitry Andric StringRef Suffix) { 330b57cec5SDimitry Andric size_t Pos = (size_t)OS.tell(); 340b57cec5SDimitry Andric assert((Prefix.str().size() + Suffix.size() <= MAX_LINE_LEN) && 350b57cec5SDimitry Andric "header line exceeds max limit"); 360b57cec5SDimitry Andric OS << Prefix; 370b57cec5SDimitry Andric for (size_t i = (size_t)OS.tell() - Pos, e = MAX_LINE_LEN - Suffix.size(); 380b57cec5SDimitry Andric i < e; ++i) 390b57cec5SDimitry Andric OS << Fill; 400b57cec5SDimitry Andric OS << Suffix << '\n'; 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) { 440b57cec5SDimitry Andric printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\"); 450b57cec5SDimitry Andric StringRef Prefix("|* "); 460b57cec5SDimitry Andric StringRef Suffix(" *|"); 470b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix); 480b57cec5SDimitry Andric size_t PSLen = Prefix.size() + Suffix.size(); 490b57cec5SDimitry Andric assert(PSLen < MAX_LINE_LEN); 500b57cec5SDimitry Andric size_t Pos = 0U; 510b57cec5SDimitry Andric do { 520b57cec5SDimitry Andric size_t Length = std::min(Desc.size() - Pos, MAX_LINE_LEN - PSLen); 530b57cec5SDimitry Andric printLine(OS, Prefix + Desc.substr(Pos, Length), ' ', Suffix); 540b57cec5SDimitry Andric Pos += Length; 550b57cec5SDimitry Andric } while (Pos < Desc.size()); 560b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix); 570b57cec5SDimitry Andric printLine(OS, Prefix + "Automatically generated file, do not edit!", ' ', 580b57cec5SDimitry Andric Suffix); 590b57cec5SDimitry Andric printLine(OS, Prefix, ' ', Suffix); 600b57cec5SDimitry Andric printLine(OS, "\\*===", '-', "===*/"); 610b57cec5SDimitry Andric OS << '\n'; 620b57cec5SDimitry Andric } 63