| 1 |
#include <cassert> |
|---|
| 2 |
#include <cstdarg> |
|---|
| 3 |
#include <cstdio> |
|---|
| 4 |
#include <cstdlib> |
|---|
| 5 |
#include <fstream> |
|---|
| 6 |
#include <string> |
|---|
| 7 |
#include <iostream> |
|---|
| 8 |
|
|---|
| 9 |
#include "mars.h" |
|---|
| 10 |
|
|---|
| 11 |
#include "llvm/Support/CommandLine.h" |
|---|
| 12 |
|
|---|
| 13 |
#include "llvm/GlobalValue.h" |
|---|
| 14 |
#include "llvm/Support/Casting.h" |
|---|
| 15 |
#include "llvm/Support/raw_os_ostream.h" |
|---|
| 16 |
#include "llvm/Assembly/Writer.h" |
|---|
| 17 |
|
|---|
| 18 |
#include "gen/logger.h" |
|---|
| 19 |
#include "gen/irstate.h" |
|---|
| 20 |
|
|---|
| 21 |
void Stream::writeType(std::ostream& OS, const llvm::Type& Ty) { |
|---|
| 22 |
llvm::raw_os_ostream raw(OS); |
|---|
| 23 |
llvm::WriteTypeSymbolic(raw, &Ty, gIR->module); |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
void Stream::writeValue(std::ostream& OS, const llvm::Value& V) { |
|---|
| 27 |
// Constants don't always get their types pretty-printed. |
|---|
| 28 |
// (Only treat non-global constants like this, so that e.g. global variables |
|---|
| 29 |
// still get their initializers printed) |
|---|
| 30 |
llvm::raw_os_ostream raw(OS); |
|---|
| 31 |
if (llvm::isa<llvm::Constant>(V) && !llvm::isa<llvm::GlobalValue>(V)) |
|---|
| 32 |
llvm::WriteAsOperand(raw, &V, true, gIR->module); |
|---|
| 33 |
else |
|---|
| 34 |
V.print(raw); |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
namespace Logger |
|---|
| 38 |
{ |
|---|
| 39 |
static std::string indent_str; |
|---|
| 40 |
|
|---|
| 41 |
llvm::cl::opt<bool> _enabled("vv", |
|---|
| 42 |
llvm::cl::desc("Very verbose"), |
|---|
| 43 |
llvm::cl::ZeroOrMore); |
|---|
| 44 |
|
|---|
| 45 |
void indent() |
|---|
| 46 |
{ |
|---|
| 47 |
if (_enabled) { |
|---|
| 48 |
indent_str += "* "; |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
void undent() |
|---|
| 52 |
{ |
|---|
| 53 |
if (_enabled) { |
|---|
| 54 |
assert(!indent_str.empty()); |
|---|
| 55 |
indent_str.resize(indent_str.size()-2); |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
Stream cout() |
|---|
| 59 |
{ |
|---|
| 60 |
if (_enabled) |
|---|
| 61 |
return std::cout << indent_str; |
|---|
| 62 |
else |
|---|
| 63 |
return 0; |
|---|
| 64 |
} |
|---|
| 65 |
void println(const char* fmt,...) |
|---|
| 66 |
{ |
|---|
| 67 |
if (_enabled) { |
|---|
| 68 |
printf("%s", indent_str.c_str()); |
|---|
| 69 |
va_list va; |
|---|
| 70 |
va_start(va,fmt); |
|---|
| 71 |
vprintf(fmt,va); |
|---|
| 72 |
va_end(va); |
|---|
| 73 |
printf("\n"); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
void print(const char* fmt,...) |
|---|
| 77 |
{ |
|---|
| 78 |
if (_enabled) { |
|---|
| 79 |
printf("%s", indent_str.c_str()); |
|---|
| 80 |
va_list va; |
|---|
| 81 |
va_start(va,fmt); |
|---|
| 82 |
vprintf(fmt,va); |
|---|
| 83 |
va_end(va); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
void enable() |
|---|
| 87 |
{ |
|---|
| 88 |
_enabled = true; |
|---|
| 89 |
} |
|---|
| 90 |
void disable() |
|---|
| 91 |
{ |
|---|
| 92 |
_enabled = false; |
|---|
| 93 |
} |
|---|
| 94 |
bool enabled() |
|---|
| 95 |
{ |
|---|
| 96 |
return _enabled; |
|---|
| 97 |
} |
|---|
| 98 |
void attention(Loc loc, const char* fmt,...) |
|---|
| 99 |
{ |
|---|
| 100 |
va_list va; |
|---|
| 101 |
va_start(va,fmt); |
|---|
| 102 |
vwarning(loc,fmt,va); |
|---|
| 103 |
va_end(va); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|