From ea0837f55795b838c2f5f236915c69d891ab99a9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Wed, 22 Aug 2018 16:12:35 +0900 Subject: [PATCH] [enco] Introduce basic implementation of CppCode (#1128) This commit introduces basic implementation of CppCode which emits the basic template of compiled artifact. Signed-off-by: Jonghyun Park --- contrib/enco/core/CMakeLists.txt | 1 + contrib/enco/core/src/CppCode.cpp | 106 +++++++++++++++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/contrib/enco/core/CMakeLists.txt b/contrib/enco/core/CMakeLists.txt index 8187ed6..a4ceaf4 100644 --- a/contrib/enco/core/CMakeLists.txt +++ b/contrib/enco/core/CMakeLists.txt @@ -8,6 +8,7 @@ target_include_directories(enco_core PRIVATE src) target_include_directories(enco_core PUBLIC include) target_link_libraries(enco_core coco_core) target_link_libraries(enco_core coco_generic) +target_link_libraries(enco_core pp) target_link_libraries(enco_core morph) nncc_find_package(GTest QUIET) diff --git a/contrib/enco/core/src/CppCode.cpp b/contrib/enco/core/src/CppCode.cpp index ab7d5aa..a0aacc9 100644 --- a/contrib/enco/core/src/CppCode.cpp +++ b/contrib/enco/core/src/CppCode.cpp @@ -1,10 +1,114 @@ #include "CppCode.h" +#include +#include + +#include #include namespace enco { -void CppCode::dump(std::ostream &os) const { throw std::runtime_error{"Not implemented, yet"}; } +void CppCode::dump(std::ostream &os) const +{ + // + // Generate full C++ source code with code snippet + // + const std::string name{"Network"}; + + pp::LinearDocument includes; + { + includes.append("#include "); + includes.append(); + includes.append("#include "); + includes.append("#include "); + includes.append("#include "); + } + + pp::LinearDocument net_def; + { + net_def.append("struct ", name, " {"); + net_def.indent(); + net_def.append(name, "();"); + net_def.append("~", name, "();"); + net_def.unindent(); + net_def.append("};"); + } + + pp::LinearDocument net_ctor; + { + net_ctor.append("Network::Network() {"); + net_ctor.indent(); + // TODO Implement this + net_ctor.unindent(); + net_ctor.append("}"); + } + + pp::LinearDocument net_dtor; + { + net_dtor.append("Network::~Network() {"); + net_dtor.indent(); + // TODO Implement this + net_dtor.unindent(); + net_dtor.append("}"); + } + + pp::LinearDocument source; + + source.append(includes); + source.append(); + source.append(net_def); + source.append(); + source.append(net_ctor); + source.append(); + source.append(net_dtor); + + source.append(); + source.append(name, " *", name, "_construct() { return new ", name, "{}; }"); + source.append("void ", name, "_destruct(", name, " *net) { delete net; }"); + + source.append(); + + source.append("unsigned ", name, "_input_count(const ", name, " *net) { assert(\"NYI\"); }"); + source.append("const char *", name, "_input_name(const ", name, + " *net, unsigned n) { assert(\"NYI\"); }"); + source.append("unsigned ", name, "_input_rank(const ", name, + " *net, unsigned n) { assert(\"NYI\"); }"); + source.append("unsigned ", name, "_input_dim(const ", name, + " *net, unsigned n, unsigned axis) { assert(\"NYI\"); }"); + source.append("void ", name, "_input_bind(", name, + " *net, unsigned n, const void *ptr, unsigned len) {"); + source.indent(); + source.append("assert(\"NYI\");"); + source.unindent(); + source.append("}"); + + source.append(); + + source.append("unsigned ", name, "_output_count(const ", name, " *net) { assert(\"NYI\"); }"); + source.append("const char *", name, "_output_name(const ", name, + " *net, unsigned n) { assert(\"NYI\"); }"); + source.append("unsigned ", name, "_output_rank(const ", name, + " *net, unsigned n) { assert(\"NYI\"); }"); + source.append( + "unsigned ", name, "_output_dim(const ", name, + " *net, unsigned n, unsigned axis) { return net->outputs.at(n).shape.dims[axis]; }"); + source.append("void ", name, "_output_bind(", name, + " *net, unsigned n, void *ptr, unsigned len) {"); + source.indent(); + source.append("assert(\"NYI\");"); + source.unindent(); + source.append("}"); + + source.append(); + + source.append("void ", name, "_invoke(", name, " *net) {"); + source.indent(); + source.append("assert(\"NYI\");"); + source.unindent(); + source.append("}"); + + os << source; +} } // namespace enco -- 2.7.4