1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include "target/compiler.h"
32 
33 #include <cassert>
34 #include "target/compiler/stub_core.h"
35 #include "target/core_compiler.h"
36 #include "target/engine.h"
37 #include "verilog/analyze/module_info.h"
38 #include "verilog/ast/ast.h"
39 
40 using namespace std;
41 
42 namespace cascade {
43 
Compiler()44 Compiler::Compiler() {
45   fatal_ = false;
46   what_ = "";
47 }
48 
~Compiler()49 Compiler::~Compiler() {
50   for (auto& cc : ccs_) {
51     delete cc.second;
52   }
53 }
54 
set(const string & id,CoreCompiler * c)55 Compiler& Compiler::set(const string& id, CoreCompiler* c) {
56   assert(ccs_.find(id) == ccs_.end());
57   assert(c != nullptr);
58   c->set_compiler(this);
59   ccs_[id] = c;
60   return *this;
61 }
62 
get(const std::string & id)63 CoreCompiler* Compiler::get(const std::string& id) {
64   const auto itr = ccs_.find(id);
65   return (itr == ccs_.end()) ? nullptr : itr->second;
66 }
67 
compile_stub(Engine::Id id,const ModuleDeclaration * md)68 Engine* Compiler::compile_stub(Engine::Id id, const ModuleDeclaration* md) {
69   const auto loc = md->get_attrs()->get<String>("__loc")->get_readable_val();
70   auto* i = get_interface(loc);
71   assert(i != nullptr);
72   return new Engine(id, i, new StubCore(i));
73 }
74 
compile(Engine::Id id,ModuleDeclaration * md)75 Engine* Compiler::compile(Engine::Id id, ModuleDeclaration* md) {
76   const auto loc = md->get_attrs()->get<String>("__loc")->get_readable_val();
77   auto* i = get_interface(loc);
78   if (i == nullptr) {
79     error("Unable to provide an interface for a module with incompatible __loc annotation");
80     delete md;
81     return nullptr;
82   }
83 
84   if (StubCheck().check(md)) {
85     delete md;
86     return new Engine(id, i, new StubCore(i));
87   }
88 
89   const auto target = md->get_attrs()->get<String>("__target")->get_readable_val();
90   auto* cc = ((loc != "remote") && (loc != "local")) ? get("proxy") : get(target);
91   if (cc == nullptr) {
92     error("Unable to locate the required core compiler");
93     delete md;
94     return nullptr;
95   }
96 
97   ids_.insert(id);
98   auto* c = cc->compile(id, md, i);
99   if (c == nullptr) {
100     delete i;
101     return nullptr;
102   }
103 
104   return new Engine(id, i, c);
105 }
106 
stop_compile(Engine::Id id)107 void Compiler::stop_compile(Engine::Id id) {
108   for (auto& cc : ccs_) {
109     cc.second->stop_compile(id);
110   }
111 }
112 
stop_async()113 void Compiler::stop_async() {
114   for (auto& cc : ccs_) {
115     cc.second->stop_async();
116   }
117 }
118 
stop_compile()119 void Compiler::stop_compile() {
120   for (auto& cc : ccs_) {
121     for (auto id : ids_) {
122       cc.second->stop_compile(id);
123     }
124   }
125 }
126 
error(const string & s)127 void Compiler::error(const string& s) {
128   lock_guard<mutex> lg(lock_);
129   what_ = (what_ == "") ? s : what_;
130 }
131 
fatal(const string & s)132 void Compiler::fatal(const string& s) {
133   lock_guard<mutex> lg(lock_);
134   fatal_ = true;
135   what_ = s;
136 }
137 
error()138 bool Compiler::error() {
139   lock_guard<mutex> lg(lock_);
140   return what_ != "";
141 }
142 
what()143 pair<bool, string> Compiler::what() {
144   lock_guard<mutex> lg(lock_);
145   return make_pair(fatal_, what_);
146 }
147 
clear()148 void Compiler::clear() {
149   lock_guard<mutex> lg(lock_);
150   fatal_ = false;
151   what_ = "";
152 }
153 
check(const ModuleDeclaration * md)154 bool Compiler::StubCheck::check(const ModuleDeclaration* md) {
155   ModuleInfo mi(md);
156   if (!mi.inputs().empty() || !mi.outputs().empty()) {
157     return false;
158   }
159   stub_ = true;
160   md->accept(this);
161   return stub_;
162 }
163 
visit(const InitialConstruct * ic)164 void Compiler::StubCheck::visit(const InitialConstruct* ic) {
165   (void) ic;
166   stub_ = false;
167 }
168 
visit(const FinishStatement * fs)169 void Compiler::StubCheck::visit(const FinishStatement* fs) {
170   (void) fs;
171   stub_ = false;
172 }
173 
visit(const RestartStatement * rs)174 void Compiler::StubCheck::visit(const RestartStatement* rs) {
175   (void) rs;
176   stub_ = false;
177 }
178 
visit(const RetargetStatement * rs)179 void Compiler::StubCheck::visit(const RetargetStatement* rs) {
180   (void) rs;
181   stub_ = false;
182 }
183 
visit(const SaveStatement * ss)184 void Compiler::StubCheck::visit(const SaveStatement* ss) {
185   (void) ss;
186   stub_ = false;
187 }
188 
189 } // namespace cascade
190