blob: 79d567753a6cd6856bf0f532785b51291133d2d3 [file] [log] [blame]
Jakob Stoklund Olesen764dc752012-06-13 05:15:49 +00001//===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner2c0f2c72003-08-06 04:23:04 +00009//
10// This file provides useful services for TableGen backends...
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer90540ad2012-06-19 17:04:16 +000014#include "llvm/ADT/Twine.h"
Jakob Stoklund Olesen764dc752012-06-13 05:15:49 +000015#include "llvm/Support/raw_ostream.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000016#include "llvm/TableGen/TableGenBackend.h"
Nadav Rotem6ab4cbc2013-01-28 07:35:33 +000017#include <algorithm>
18
Chris Lattner2082ebe2004-08-01 03:55:39 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Nadav Rotem6ab4cbc2013-01-28 07:35:33 +000021const size_t MAX_LINE_LEN = 80U;
22
Benjamin Kramer90540ad2012-06-19 17:04:16 +000023static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill,
24 StringRef Suffix) {
Nadav Rotem6ab4cbc2013-01-28 07:35:33 +000025 size_t Pos = (size_t)OS.tell();
26 assert((MAX_LINE_LEN - Prefix.str().size() - Suffix.size() > 0) &&
27 "header line exceeds max limit");
Benjamin Kramer90540ad2012-06-19 17:04:16 +000028 OS << Prefix;
Nadav Rotem6ab4cbc2013-01-28 07:35:33 +000029 const size_t e = MAX_LINE_LEN - Suffix.size();
30 for (size_t i = (size_t)OS.tell() - Pos; i < e; ++i)
Benjamin Kramer90540ad2012-06-19 17:04:16 +000031 OS << Fill;
32 OS << Suffix << '\n';
33}
34
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000035void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) {
Benjamin Kramer90540ad2012-06-19 17:04:16 +000036 printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\");
37 printLine(OS, "|*", ' ', "*|");
Nadav Rotem6ab4cbc2013-01-28 07:35:33 +000038 size_t Pos = 0U;
39 size_t PosE;
40 StringRef Prefix("|*");
41 StringRef Suffix(" *|");
42 do{
43 size_t PSLen = Suffix.size() + Prefix.size();
44 PosE = Pos + ((MAX_LINE_LEN > (Desc.size() - PSLen)) ?
45 Desc.size() :
46 MAX_LINE_LEN - PSLen);
47 printLine(OS, Prefix + Desc.slice(Pos, PosE), ' ', Suffix);
48 Pos = PosE;
49 } while(Pos < Desc.size());
50 printLine(OS, Prefix, ' ', Suffix);
51 printLine(OS, Prefix + " Automatically generated file, do not edit!", ' ',
52 Suffix);
53 printLine(OS, Prefix, ' ', Suffix);
Benjamin Kramer90540ad2012-06-19 17:04:16 +000054 printLine(OS, "\\*===", '-', "===*/");
55 OS << '\n';
Chris Lattner2c0f2c72003-08-06 04:23:04 +000056}