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 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 15 | #include "IceCfg.h" |
| 16 | #include "IceClFlags.h" |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 17 | #include "IceDefs.h" |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 18 | #include "IceGlobalInits.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 19 | #include "IceTargetLowering.h" |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 20 | #include "IceTranslator.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 21 | |
| 22 | using namespace Ice; |
| 23 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 24 | Translator::Translator(GlobalContext *Ctx, const ClFlags &Flags) |
| 25 | : Ctx(Ctx), Flags(Flags), |
| 26 | GlobalLowering(TargetGlobalLowering::createLowering(Ctx)), ErrorStatus() { |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 29 | Translator::~Translator() {} |
| 30 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 31 | IceString Translator::createUnnamedName(const IceString &Prefix, SizeT Index) { |
| 32 | if (Index == 0) |
| 33 | return Prefix; |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 34 | std::string Buffer; |
| 35 | llvm::raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 36 | StrBuf << Prefix << Index; |
| 37 | return StrBuf.str(); |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 38 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 39 | |
| 40 | bool Translator::checkIfUnnamedNameSafe(const IceString &Name, const char *Kind, |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 41 | const IceString &Prefix) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 42 | if (Name.find(Prefix) == 0) { |
| 43 | for (size_t i = Prefix.size(); i < Name.size(); ++i) { |
| 44 | if (!isdigit(Name[i])) { |
| 45 | return false; |
| 46 | } |
| 47 | } |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 48 | OstreamLocker L(Ctx); |
| 49 | Ostream &Stream = Ctx->getStrDump(); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 50 | Stream << "Warning : Default " << Kind << " prefix '" << Prefix |
| 51 | << "' potentially conflicts with name '" << Name << "'.\n"; |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 56 | |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 57 | void Translator::translateFcn(Cfg *Func) { |
| 58 | Ctx->cfgQueueBlockingPush(Func); |
| 59 | if (Ctx->getFlags().NumTranslationThreads == 0) { |
| 60 | Ctx->translateFunctions(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | void Translator::emitConstants() { |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 65 | if (!getErrorStatus()) |
| 66 | GlobalLowering->lowerConstants(Ctx); |
| 67 | } |
| 68 | |
| 69 | void Translator::transferErrorCode() const { |
| 70 | if (getErrorStatus()) |
| 71 | Ctx->getErrorStatus()->assign(getErrorStatus().value()); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 72 | } |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 73 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 74 | void Translator::lowerGlobals( |
| 75 | const VariableDeclarationListType &VariableDeclarations) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 76 | bool DisableTranslation = Ctx->getFlags().DisableTranslation; |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 77 | const bool DumpGlobalVariables = |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 78 | ALLOW_DUMP && Ctx->getVerbose() && Ctx->getFlags().VerboseFocusOn.empty(); |
Jim Stichnoth | e4a8f40 | 2015-01-20 12:52:51 -0800 | [diff] [blame] | 79 | OstreamLocker L(Ctx); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 80 | Ostream &Stream = Ctx->getStrDump(); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 81 | const IceString &TranslateOnly = Ctx->getFlags().TranslateOnly; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 82 | for (const Ice::VariableDeclaration *Global : VariableDeclarations) { |
| 83 | if (DumpGlobalVariables) |
Karl Schimpf | df6f9d1 | 2014-10-20 14:09:00 -0700 | [diff] [blame] | 84 | Global->dump(getContext(), Stream); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 85 | if (!DisableTranslation && |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 86 | GlobalContext::matchSymbolName(Global->getName(), TranslateOnly)) |
| 87 | GlobalLowering->lowerInit(*Global); |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 88 | } |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 89 | } |