Jakob Stoklund Olesen | 764dc75 | 2012-06-13 05:15:49 +0000 | [diff] [blame] | 1 | //===- TableGenBackend.cpp - Utilities for TableGen Backends ----*- C++ -*-===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 01d4582 | 2003-10-20 20:20:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2c0f2c7 | 2003-08-06 04:23:04 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file provides useful services for TableGen backends... |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.h" |
Jakob Stoklund Olesen | 764dc75 | 2012-06-13 05:15:49 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Peter Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 16 | #include "llvm/TableGen/TableGenBackend.h" |
Nadav Rotem | 6ab4cbc | 2013-01-28 07:35:33 +0000 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | |
Chris Lattner | 2082ebe | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Nadav Rotem | 6ab4cbc | 2013-01-28 07:35:33 +0000 | [diff] [blame] | 21 | const size_t MAX_LINE_LEN = 80U; |
| 22 | |
Benjamin Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 23 | static void printLine(raw_ostream &OS, const Twine &Prefix, char Fill, |
| 24 | StringRef Suffix) { |
Nadav Rotem | 6ab4cbc | 2013-01-28 07:35:33 +0000 | [diff] [blame] | 25 | 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 Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 28 | OS << Prefix; |
Nadav Rotem | 6ab4cbc | 2013-01-28 07:35:33 +0000 | [diff] [blame] | 29 | const size_t e = MAX_LINE_LEN - Suffix.size(); |
| 30 | for (size_t i = (size_t)OS.tell() - Pos; i < e; ++i) |
Benjamin Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 31 | OS << Fill; |
| 32 | OS << Suffix << '\n'; |
| 33 | } |
| 34 | |
Jakob Stoklund Olesen | 6f36fa9 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 35 | void llvm::emitSourceFileHeader(StringRef Desc, raw_ostream &OS) { |
Benjamin Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 36 | printLine(OS, "/*===- TableGen'erated file ", '-', "*- C++ -*-===*\\"); |
| 37 | printLine(OS, "|*", ' ', "*|"); |
Nadav Rotem | 6ab4cbc | 2013-01-28 07:35:33 +0000 | [diff] [blame] | 38 | 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 Kramer | 90540ad | 2012-06-19 17:04:16 +0000 | [diff] [blame] | 54 | printLine(OS, "\\*===", '-', "===*/"); |
| 55 | OS << '\n'; |
Chris Lattner | 2c0f2c7 | 2003-08-06 04:23:04 +0000 | [diff] [blame] | 56 | } |