Skip to content

remove package and import declaration #1

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 6 commits into
base: pioasm-go-output
Choose a base branch
from
Open
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
63 changes: 25 additions & 38 deletions tools/pioasm/go_output.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
* Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Contributed by the TinyGo authors.
*
* Go generated by this assembler is compatible with TinyGo.
*
* https://tinygo.org
* https://github.com/tinygo-org/tinygo
* https://github.com/tinygo-org/pio
*/

#include <algorithm>
Expand All @@ -24,7 +27,7 @@ struct go_output : public output_format {
go_output() : output_format("go") {}

std::string get_description() override {
return "Go file suitable for use with TinyGo";
return "Go file suitable for use with TinyGo. See https://github.com/tinygo-org/pio.";
}

void output_symbols(FILE *out, std::string prefix, const std::vector<compiled_source::symbol> &symbols) {
Expand Down Expand Up @@ -57,31 +60,23 @@ struct go_output : public output_format {
int output(std::string destination, std::vector<std::string> output_options,
const compiled_source &source) override {

std::string package = "main";
FILE *out = open_single_output(destination);
if (!out) return 1;

header(out, "Code generated by pioasm; DO NOT EDIT.");

// First we give priority to user's code blocks since
// 1. In Go our imports always precede our code.
// 2. We give users the freedom to use their own PIO implementation.
for (const auto &program : source.programs) {
for(const auto &p : program.lang_opts) {
if (p.first.size() >= name.size() && p.first.compare(0, name.size(), name) == 0) {
for (const auto &opt : p.second) {
if (opt.first.compare("package") == 0) {
package.assign(opt.second);
} else {
std::cerr << "warning: " << name << " does not support output option " << opt.first << ", ignored.\n";
}
for(const auto& o : program.code_blocks) {
if (o.first == name) {
for(const auto &contents : o.second) {
fprintf(out, "%s", contents.c_str());
}
}
}
}
FILE *out = open_single_output(destination);
if (!out) return 1;

header(out, "Code generated by pioasm; DO NOT EDIT.");

fprintf(out, "// +build rp2040\n\n");
fprintf(out, "package %s\n\n", package.c_str());
fprintf(out, "import \"machine\"\n\n");

output_symbols(out, "", source.global_symbols);

for (const auto &program : source.programs) {
header(out, program.name);
Expand All @@ -94,8 +89,7 @@ struct go_output : public output_format {

output_symbols(out, prefix, program.symbols);

fprintf(out, "var %sProgram = machine.PIOProgram{\n", prefix.c_str());
fprintf(out, "\tInstructions: []uint16{\n");
fprintf(out, "var %sInstructions = []uint16{\n", prefix.c_str());
for (int i = 0; i < (int)program.instructions.size(); i++) {
const auto &inst = program.instructions[i];
if (i == program.wrap_target) {
Expand All @@ -107,31 +101,24 @@ struct go_output : public output_format {
fprintf(out, "\t\t// .wrap\n");
}
}
fprintf(out, "\t},\n");
fprintf(out, "\tOrigin: %d,\n", program.origin.get());
fprintf(out, "}\n");
fprintf(out, "\n");
fprintf(out, "func %sProgramDefaultConfig(offset uint8) machine.PIOStateMachineConfig {\n", prefix.c_str());
fprintf(out, "\tcfg := machine.DefaultStateMachineConfig()\n");
fprintf(out, "const %sOrigin = %d\n", prefix.c_str(), program.origin.get());

fprintf(out, "func %sProgramDefaultConfig(offset uint8) pio.StateMachineConfig {\n", prefix.c_str());
fprintf(out, "\tcfg := pio.DefaultStateMachineConfig()\n");
fprintf(out, "\tcfg.SetWrap(offset+%sWrapTarget, offset+%sWrap)\n", prefix.c_str(),
prefix.c_str());
if (program.sideset_bits_including_opt.is_specified()) {
fprintf(out, "\tcfg.SetSideSet(%d, %s, %s)\n", program.sideset_bits_including_opt.get(),
fprintf(out, "\tcfg.SetSidesetParams(%d, %s, %s)\n", program.sideset_bits_including_opt.get(),
program.sideset_opt ? "true" : "false",
program.sideset_pindirs ? "true" : "false");
}
fprintf(out, "\treturn cfg;\n");
fprintf(out, "}\n\n");

// todo maybe have some code blocks inside or outside here?
for(const auto& o : program.code_blocks) {
if (o.first == name) {
for(const auto &contents : o.second) {
fprintf(out, "%s", contents.c_str());
}
}
}
}

output_symbols(out, "", source.global_symbols);

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