blob: 4198442da454522c5c02a6f898d7dea327533004 [file] [log] [blame]
Karl Schimpf8d7abae2014-07-07 14:50:30 -07001//===- 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 Schimpf8d7abae2014-07-07 14:50:30 -070015#include "IceCfg.h"
16#include "IceClFlags.h"
Karl Schimpf5ee234a2014-09-12 10:41:40 -070017#include "IceDefs.h"
Karl Schimpf9d98d792014-10-13 15:01:08 -070018#include "IceGlobalInits.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070019#include "IceTargetLowering.h"
Jim Stichnothc4554d72014-09-30 16:49:38 -070020#include "IceTranslator.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070021
22using namespace Ice;
23
Jim Stichnothfa4efea2015-01-27 05:06:03 -080024Translator::Translator(GlobalContext *Ctx, const ClFlags &Flags)
25 : Ctx(Ctx), Flags(Flags),
26 GlobalLowering(TargetGlobalLowering::createLowering(Ctx)), ErrorStatus() {
Jim Stichnoth088b2be2014-10-23 12:02:08 -070027}
28
Karl Schimpf8d7abae2014-07-07 14:50:30 -070029Translator::~Translator() {}
30
Karl Schimpfe3f64d02014-10-07 10:38:22 -070031IceString Translator::createUnnamedName(const IceString &Prefix, SizeT Index) {
32 if (Index == 0)
33 return Prefix;
Karl Schimpf5ee234a2014-09-12 10:41:40 -070034 std::string Buffer;
35 llvm::raw_string_ostream StrBuf(Buffer);
Karl Schimpfe3f64d02014-10-07 10:38:22 -070036 StrBuf << Prefix << Index;
37 return StrBuf.str();
Karl Schimpf5ee234a2014-09-12 10:41:40 -070038}
Karl Schimpfe3f64d02014-10-07 10:38:22 -070039
40bool Translator::checkIfUnnamedNameSafe(const IceString &Name, const char *Kind,
Jim Stichnothe4a8f402015-01-20 12:52:51 -080041 const IceString &Prefix) {
Karl Schimpfe3f64d02014-10-07 10:38:22 -070042 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 Stichnothe4a8f402015-01-20 12:52:51 -080048 OstreamLocker L(Ctx);
49 Ostream &Stream = Ctx->getStrDump();
Karl Schimpfe3f64d02014-10-07 10:38:22 -070050 Stream << "Warning : Default " << Kind << " prefix '" << Prefix
51 << "' potentially conflicts with name '" << Name << "'.\n";
52 return true;
53 }
54 return false;
55}
Karl Schimpf5ee234a2014-09-12 10:41:40 -070056
Jim Stichnothfa4efea2015-01-27 05:06:03 -080057void Translator::translateFcn(Cfg *Func) {
58 Ctx->cfgQueueBlockingPush(Func);
59 if (Ctx->getFlags().NumTranslationThreads == 0) {
60 Ctx->translateFunctions();
Karl Schimpf8d7abae2014-07-07 14:50:30 -070061 }
62}
63
64void Translator::emitConstants() {
Jim Stichnothfa4efea2015-01-27 05:06:03 -080065 if (!getErrorStatus())
66 GlobalLowering->lowerConstants(Ctx);
67}
68
69void Translator::transferErrorCode() const {
70 if (getErrorStatus())
71 Ctx->getErrorStatus()->assign(getErrorStatus().value());
Karl Schimpf8d7abae2014-07-07 14:50:30 -070072}
Karl Schimpf6ff33d22014-09-22 10:28:42 -070073
Karl Schimpf9d98d792014-10-13 15:01:08 -070074void Translator::lowerGlobals(
75 const VariableDeclarationListType &VariableDeclarations) {
Karl Schimpfe3f64d02014-10-07 10:38:22 -070076 bool DisableTranslation = Ctx->getFlags().DisableTranslation;
Karl Schimpfb6c96af2014-11-17 10:58:39 -080077 const bool DumpGlobalVariables =
Jim Stichnothfa4efea2015-01-27 05:06:03 -080078 ALLOW_DUMP && Ctx->getVerbose() && Ctx->getFlags().VerboseFocusOn.empty();
Jim Stichnothe4a8f402015-01-20 12:52:51 -080079 OstreamLocker L(Ctx);
Karl Schimpf9d98d792014-10-13 15:01:08 -070080 Ostream &Stream = Ctx->getStrDump();
Jim Stichnoth088b2be2014-10-23 12:02:08 -070081 const IceString &TranslateOnly = Ctx->getFlags().TranslateOnly;
Karl Schimpf9d98d792014-10-13 15:01:08 -070082 for (const Ice::VariableDeclaration *Global : VariableDeclarations) {
83 if (DumpGlobalVariables)
Karl Schimpfdf6f9d12014-10-20 14:09:00 -070084 Global->dump(getContext(), Stream);
Jim Stichnoth088b2be2014-10-23 12:02:08 -070085 if (!DisableTranslation &&
Jim Stichnothfa4efea2015-01-27 05:06:03 -080086 GlobalContext::matchSymbolName(Global->getName(), TranslateOnly))
87 GlobalLowering->lowerInit(*Global);
Karl Schimpf6ff33d22014-09-22 10:28:42 -070088 }
Karl Schimpf6ff33d22014-09-22 10:28:42 -070089}