MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
CxxBridgeGen.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pch.h"
18
22
23namespace mllif {
24 auto Indent(size_t size) -> std::string {
25 std::string indent(size * 4, ' ');
26 return indent;
27 }
28}
29
30bool mllif::cxx::CxxBridgeGen::handleAssemblyBegin(MLLIFContext &context, const AssemblyDecl &node, std::ostream &out, std::size_t indent) {
31 return true;
32}
33
34bool mllif::cxx::CxxBridgeGen::handleAssemblyEnd(MLLIFContext &context, const AssemblyDecl &node, std::ostream &out, std::size_t indent) {
35 return true;
36}
37
38bool mllif::cxx::CxxBridgeGen::handleNamespaceBegin(MLLIFContext &context, const NamespaceDecl &node, std::ostream &out, std::size_t indent) {
39 out << Indent(indent) << "namespace " << node.name() << " {\n";
40 return true;
41}
42
43bool mllif::cxx::CxxBridgeGen::handleNamespaceEnd(MLLIFContext &context, const NamespaceDecl &node, std::ostream &out, std::size_t indent) {
44 out << Indent(indent) << "} // namespace " << node.name() << '\n';
45 return true;
46}
47
48bool mllif::cxx::CxxBridgeGen::handleObjectBegin(MLLIFContext &context, const ObjectDecl &node, std::ostream &out, std::size_t indent) {
49 out << Indent(indent) << "class " << node.name() << " {\n"
50 << Indent(indent + 1) << "char __data[" << node.size() << "];\n"
51 << Indent(indent) << "public:\n";
52 return true;
53}
54
55bool mllif::cxx::CxxBridgeGen::handleObjectEnd(MLLIFContext &context, const ObjectDecl &node, std::ostream &out, std::size_t indent) {
56 out << Indent(indent) << "} __attribute((aligned(" << node.align() << ")));\n";
57 return true;
58}
59
60bool mllif::cxx::CxxBridgeGen::handleFunctionBegin(MLLIFContext &context, const FunctionDecl &node, std::ostream &out, std::size_t indent) {
61 const auto ret = TypeToCxx(node.returns());
62 if (!ret) {
63 context.error(std::format("unrecognized return type of function '{}'", node.symbol()));
64 return false;
65 }
66
67 out << Indent(indent);
68 if (std::dynamic_pointer_cast<ObjectDecl>(node.parent())) {
69 out << "static ";
70 }
71 out << ret.value() << ' ' << node.name() << '(';
72
73 return true;
74}
75
76bool mllif::cxx::CxxBridgeGen::handleFunctionEnd(MLLIFContext &context, const FunctionDecl &node, std::ostream &out, std::size_t indent) {
77 out << ") asm(\"" << node.symbol() << "\");\n";
78 return true;
79}
80
81bool mllif::cxx::CxxBridgeGen::handleMethodBegin(MLLIFContext &context, const MethodDecl &node, std::ostream &out, std::size_t indent) {
82 const auto ret = TypeToCxx(node.returns());
83 if (!ret) {
84 context.error(std::format("unrecognized builtin type '{}'", node.returns().terms()[0]));
85 return false;
86 }
87
88 out << Indent(indent) << ret.value() << ' ' << node.name() << '(';
89 return true;
90}
91
92bool mllif::cxx::CxxBridgeGen::handleParam(MLLIFContext &context, const ParamDecl &node, std::ostream &out, std::size_t indent) {
93 const auto type = TypeToCxx(node.type());
94 if (!type) {
95 context.error(std::format("unrecognized builtin type '{}'", node.type().terms()[0]));
96 return false;
97 }
98
99 out << type.value() << ' ' << node.name();
100 return true;
101}
102
104 os << ", ";
105}
const std::shared_ptr< Decl > & parent() const
Definition Decl.h:51
const std::string & name() const
Definition Decl.h:49
const Type & returns() const
Definition Decl.h:84
const std::string & symbol() const
Definition Decl.h:85
void error(const std::string &what)
Definition Context.h:35
const std::string & align() const
Definition Decl.h:74
const std::string & size() const
Definition Decl.h:73
const Type & type() const
Definition Decl.h:99
const std::vector< std::string > & terms() const
Definition Decl.h:35
bool handleAssemblyBegin(MLLIFContext &context, const AssemblyDecl &node, std::ostream &out, std::size_t indent) override
bool handleParam(MLLIFContext &context, const ParamDecl &node, std::ostream &out, std::size_t indent) override
bool handleMethodBegin(MLLIFContext &context, const MethodDecl &node, std::ostream &out, std::size_t indent) override
void writeParamDelimiter(std::ostream &os) override
bool handleNamespaceEnd(MLLIFContext &context, const NamespaceDecl &node, std::ostream &out, std::size_t indent) override
bool handleNamespaceBegin(MLLIFContext &context, const NamespaceDecl &node, std::ostream &out, std::size_t indent) override
bool handleAssemblyEnd(MLLIFContext &context, const AssemblyDecl &node, std::ostream &out, std::size_t indent) override
bool handleFunctionEnd(MLLIFContext &context, const FunctionDecl &node, std::ostream &out, std::size_t indent) override
bool handleFunctionBegin(MLLIFContext &context, const FunctionDecl &node, std::ostream &out, std::size_t indent) override
bool handleObjectBegin(MLLIFContext &context, const ObjectDecl &node, std::ostream &out, std::size_t indent) override
bool handleObjectEnd(MLLIFContext &context, const ObjectDecl &node, std::ostream &out, std::size_t indent) override
std::optional< std::string > TypeToCxx(const Type &type)
Gets C++-compliant typename of type.
Definition Type.cxx:19
auto Indent(size_t size) -> std::string