Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceTranslator.cpp - ICE to machine code ------*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the general driver class for translating ICE to |
| 11 | // machine code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 15 | #include <iostream> |
| 16 | #include <memory> |
| 17 | |
| 18 | #include "llvm/IR/Constant.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 22 | #include "IceCfg.h" |
| 23 | #include "IceClFlags.h" |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 24 | #include "IceDefs.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 25 | #include "IceGlobalInits.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 26 | #include "IceTargetLowering.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 27 | #include "IceTranslator.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace Ice; |
| 30 | |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | // Match a symbol name against a match string. An empty match string |
| 34 | // means match everything. Returns true if there is a match. |
| 35 | bool matchSymbolName(const IceString &SymbolName, const IceString &Match) { |
| 36 | return Match.empty() || Match == SymbolName; |
| 37 | } |
| 38 | |
| 39 | } // end of anonymous namespace |
| 40 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 41 | Translator::~Translator() {} |
| 42 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 43 | IceString Translator::createUnnamedName(const IceString &Prefix, SizeT Index) { |
| 44 | if (Index == 0) |
| 45 | return Prefix; |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 46 | std::string Buffer; |
| 47 | llvm::raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 48 | StrBuf << Prefix << Index; |
| 49 | return StrBuf.str(); |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 50 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 51 | |
| 52 | bool Translator::checkIfUnnamedNameSafe(const IceString &Name, const char *Kind, |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 53 | const IceString &Prefix) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 54 | if (Name.find(Prefix) == 0) { |
| 55 | for (size_t i = Prefix.size(); i < Name.size(); ++i) { |
| 56 | if (!isdigit(Name[i])) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 60 | OstreamLocker L(Ctx); |
| 61 | Ostream &Stream = Ctx->getStrDump(); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 62 | Stream << "Warning : Default " << Kind << " prefix '" << Prefix |
| 63 | << "' potentially conflicts with name '" << Name << "'.\n"; |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 68 | |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 69 | void Translator::translateFcn(Cfg *Fcn) { |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 70 | Ctx->resetStats(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 71 | Func.reset(Fcn); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 72 | VerboseMask OldVerboseMask = Ctx->getVerbose(); |
| 73 | if (!matchSymbolName(Func->getFunctionName(), Ctx->getFlags().VerboseFocusOn)) |
| 74 | Ctx->setVerbose(IceV_None); |
| 75 | |
| 76 | if (Ctx->getFlags().DisableTranslation || |
| 77 | !matchSymbolName(Func->getFunctionName(), |
| 78 | Ctx->getFlags().TranslateOnly)) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 79 | Func->dump(); |
| 80 | } else { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 81 | Func->translate(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 82 | if (Func->hasError()) { |
| 83 | std::cerr << "ICE translation error: " << Func->getError() << "\n"; |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 84 | ErrorStatus = true; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 87 | if (!ErrorStatus) { |
| 88 | if (Ctx->getFlags().UseIntegratedAssembler) { |
| 89 | Func->emitIAS(); |
| 90 | } else { |
| 91 | Func->emit(); |
| 92 | } |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 93 | } |
Jim Stichnoth | 1873560 | 2014-09-16 19:59:35 -0700 | [diff] [blame] | 94 | Ctx->dumpStats(Func->getFunctionName()); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 95 | } |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 96 | |
| 97 | Ctx->setVerbose(OldVerboseMask); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void Translator::emitConstants() { |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 101 | if (!Ctx->getFlags().DisableTranslation && Func) |
| 102 | Func->getTarget()->emitConstants(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 103 | } |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 104 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 105 | void Translator::lowerGlobals( |
| 106 | const VariableDeclarationListType &VariableDeclarations) { |
JF Bastien | 79f2a03 | 2014-11-21 10:02:17 -0800 | [diff] [blame] | 107 | std::unique_ptr<TargetGlobalInitLowering> GlobalLowering( |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 108 | TargetGlobalInitLowering::createLowering(Ctx->getTargetArch(), Ctx)); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 109 | bool DisableTranslation = Ctx->getFlags().DisableTranslation; |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 110 | const bool DumpGlobalVariables = |
| 111 | ALLOW_DUMP && Ctx->isVerbose() && Ctx->getFlags().VerboseFocusOn.empty(); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 112 | OstreamLocker L(Ctx); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 113 | Ostream &Stream = Ctx->getStrDump(); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 114 | const IceString &TranslateOnly = Ctx->getFlags().TranslateOnly; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 115 | for (const Ice::VariableDeclaration *Global : VariableDeclarations) { |
| 116 | if (DumpGlobalVariables) |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 117 | Global->dump(getContext(), Stream); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 118 | if (!DisableTranslation && |
| 119 | matchSymbolName(Global->getName(), TranslateOnly)) |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 120 | GlobalLowering->lower(*Global); |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 121 | } |
| 122 | GlobalLowering.reset(); |
| 123 | } |