blob: c0331a8718df28fe05546d2812995340e76db6ca [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
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070015#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 Schimpf8d7abae2014-07-07 14:50:30 -070022#include "IceCfg.h"
23#include "IceClFlags.h"
Karl Schimpf5ee234a2014-09-12 10:41:40 -070024#include "IceDefs.h"
Karl Schimpf9d98d792014-10-13 15:01:08 -070025#include "IceGlobalInits.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070026#include "IceTargetLowering.h"
Jim Stichnothc4554d72014-09-30 16:49:38 -070027#include "IceTranslator.h"
Karl Schimpf8d7abae2014-07-07 14:50:30 -070028
29using namespace Ice;
30
Jim Stichnoth088b2be2014-10-23 12:02:08 -070031namespace {
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.
35bool matchSymbolName(const IceString &SymbolName, const IceString &Match) {
36 return Match.empty() || Match == SymbolName;
37}
38
39} // end of anonymous namespace
40
Karl Schimpf8d7abae2014-07-07 14:50:30 -070041Translator::~Translator() {}
42
Karl Schimpfe3f64d02014-10-07 10:38:22 -070043IceString Translator::createUnnamedName(const IceString &Prefix, SizeT Index) {
44 if (Index == 0)
45 return Prefix;
Karl Schimpf5ee234a2014-09-12 10:41:40 -070046 std::string Buffer;
47 llvm::raw_string_ostream StrBuf(Buffer);
Karl Schimpfe3f64d02014-10-07 10:38:22 -070048 StrBuf << Prefix << Index;
49 return StrBuf.str();
Karl Schimpf5ee234a2014-09-12 10:41:40 -070050}
Karl Schimpfe3f64d02014-10-07 10:38:22 -070051
52bool Translator::checkIfUnnamedNameSafe(const IceString &Name, const char *Kind,
Jim Stichnothe4a8f402015-01-20 12:52:51 -080053 const IceString &Prefix) {
Karl Schimpfe3f64d02014-10-07 10:38:22 -070054 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 Stichnothe4a8f402015-01-20 12:52:51 -080060 OstreamLocker L(Ctx);
61 Ostream &Stream = Ctx->getStrDump();
Karl Schimpfe3f64d02014-10-07 10:38:22 -070062 Stream << "Warning : Default " << Kind << " prefix '" << Prefix
63 << "' potentially conflicts with name '" << Name << "'.\n";
64 return true;
65 }
66 return false;
67}
Karl Schimpf5ee234a2014-09-12 10:41:40 -070068
Karl Schimpf5ee234a2014-09-12 10:41:40 -070069void Translator::translateFcn(Cfg *Fcn) {
Jim Stichnoth18735602014-09-16 19:59:35 -070070 Ctx->resetStats();
Karl Schimpf8d7abae2014-07-07 14:50:30 -070071 Func.reset(Fcn);
Jim Stichnoth088b2be2014-10-23 12:02:08 -070072 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 Schimpf8d7abae2014-07-07 14:50:30 -070079 Func->dump();
80 } else {
Karl Schimpf8d7abae2014-07-07 14:50:30 -070081 Func->translate();
Karl Schimpf8d7abae2014-07-07 14:50:30 -070082 if (Func->hasError()) {
83 std::cerr << "ICE translation error: " << Func->getError() << "\n";
Karl Schimpfb164d202014-07-11 10:26:34 -070084 ErrorStatus = true;
Karl Schimpf8d7abae2014-07-07 14:50:30 -070085 }
86
Jim Stichnoth70d0a052014-11-14 15:53:46 -080087 if (!ErrorStatus) {
88 if (Ctx->getFlags().UseIntegratedAssembler) {
89 Func->emitIAS();
90 } else {
91 Func->emit();
92 }
Jan Voung0faec4c2014-11-05 17:29:56 -080093 }
Jim Stichnoth18735602014-09-16 19:59:35 -070094 Ctx->dumpStats(Func->getFunctionName());
Karl Schimpf8d7abae2014-07-07 14:50:30 -070095 }
Jim Stichnoth088b2be2014-10-23 12:02:08 -070096
97 Ctx->setVerbose(OldVerboseMask);
Karl Schimpf8d7abae2014-07-07 14:50:30 -070098}
99
100void Translator::emitConstants() {
Jan Voung91a3e2c2015-01-09 13:01:42 -0800101 if (!Ctx->getFlags().DisableTranslation && Func)
102 Func->getTarget()->emitConstants();
Karl Schimpf8d7abae2014-07-07 14:50:30 -0700103}
Karl Schimpf6ff33d22014-09-22 10:28:42 -0700104
Karl Schimpf9d98d792014-10-13 15:01:08 -0700105void Translator::lowerGlobals(
106 const VariableDeclarationListType &VariableDeclarations) {
JF Bastien79f2a032014-11-21 10:02:17 -0800107 std::unique_ptr<TargetGlobalInitLowering> GlobalLowering(
Karl Schimpf9d98d792014-10-13 15:01:08 -0700108 TargetGlobalInitLowering::createLowering(Ctx->getTargetArch(), Ctx));
Karl Schimpfe3f64d02014-10-07 10:38:22 -0700109 bool DisableTranslation = Ctx->getFlags().DisableTranslation;
Karl Schimpfb6c96af2014-11-17 10:58:39 -0800110 const bool DumpGlobalVariables =
111 ALLOW_DUMP && Ctx->isVerbose() && Ctx->getFlags().VerboseFocusOn.empty();
Jim Stichnothe4a8f402015-01-20 12:52:51 -0800112 OstreamLocker L(Ctx);
Karl Schimpf9d98d792014-10-13 15:01:08 -0700113 Ostream &Stream = Ctx->getStrDump();
Jim Stichnoth088b2be2014-10-23 12:02:08 -0700114 const IceString &TranslateOnly = Ctx->getFlags().TranslateOnly;
Karl Schimpf9d98d792014-10-13 15:01:08 -0700115 for (const Ice::VariableDeclaration *Global : VariableDeclarations) {
116 if (DumpGlobalVariables)
Karl Schimpfdf6f9d12014-10-20 14:09:00 -0700117 Global->dump(getContext(), Stream);
Jim Stichnoth088b2be2014-10-23 12:02:08 -0700118 if (!DisableTranslation &&
119 matchSymbolName(Global->getName(), TranslateOnly))
Karl Schimpf9d98d792014-10-13 15:01:08 -0700120 GlobalLowering->lower(*Global);
Karl Schimpf6ff33d22014-09-22 10:28:42 -0700121 }
122 GlobalLowering.reset();
123}