Skip to content

Add pioasm c++ constexpr output target #2400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/pioasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_executable(pioasm
target_sources(pioasm PRIVATE c_sdk_output.cpp)
target_sources(pioasm PRIVATE python_output.cpp)
target_sources(pioasm PRIVATE hex_output.cpp)
target_sources(pioasm PRIVATE constexpr_output.cpp)
target_sources(pioasm PRIVATE json_output.cpp)
target_sources(pioasm PRIVATE ada_output.cpp)
target_sources(pioasm PRIVATE go_output.cpp)
Expand Down
74 changes: 74 additions & 0 deletions tools/pioasm/constexpr_output.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "output_format.h"
#include "pio_disassembler.h"

struct constexpr_output : public output_format {
struct factory {
factory() { output_format::add(new constexpr_output()); }
};

constexpr_output() : output_format("constexpr") {}

std::string get_description() {
return "c++ constexpr array output (only raw programs are output)\n"
" "
"-p namespace=some_namespace add optional namespace";
}

virtual int output(std::string destination,
std::vector<std::string> output_options,
const compiled_source &source) {

std::string generated_namespace = "";

for (const auto &o_opt : output_options) {
const std::string prefix = "namespace=";
if (o_opt.find(prefix) == 0) {
generated_namespace = o_opt.substr(prefix.size());
}
}

FILE *out = open_single_output(destination);
if (!out)
return 1;

fprintf(out, "#pragma once\n\n");
fprintf(out, "#include <array>\n");
fprintf(out, "#include <cstdint>\n\n");

fprintf(out, "namespace %s {\n\n", generated_namespace.c_str());

for (auto &p : source.programs) {
fprintf(out, "inline constexpr std::array<uint16_t, %lu> %s = {\n",
p.instructions.size(), p.name.c_str());
for (auto i = 0u; i < p.instructions.size(); ++i) {
const auto &inst = p.instructions[i];
if (i == p.wrap_target) {
fprintf(out, " // .wrap_target\n");
}
fprintf(out, " 0x%04x, // %2d: %s\n", inst, i,
disassemble(inst, p.sideset_bits_including_opt.get(),
p.sideset_opt)
.c_str());
if (i == p.wrap) {
fprintf(out, " // .wrap\n");
}
}
fprintf(out, "};\n\n");
}

fprintf(out, "}\n");

if (out != stdout) {
fclose(out);
}
return 0;
}
};

static constexpr_output::factory creator;