Skip to content

Add target for .wrap, dot symbol, .word as instruction (#2163) #2484

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 1 commit 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
2 changes: 2 additions & 0 deletions tools/pioasm/lexer.ll
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ output_fmt [^%\n]+

{comment} { }

"." return yy::parser::make_DOT(loc);

. { throw yy::parser::syntax_error(loc, "invalid character: " + std::string(yytext)); }

%%
Expand Down
7 changes: 5 additions & 2 deletions tools/pioasm/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
DOT_SET ".set"
DOT_OUT ".out"
DOT_IN ".in"
DOT "."

JMP "jmp"
WAIT "wait"
Expand Down Expand Up @@ -203,8 +204,8 @@ directive:
| DOT_OUT value direction autop threshold { pioasm.get_current_program(@1, ".out", true).set_out(@$, $2, $3, $4, $5); }
| DOT_SET value { pioasm.get_current_program(@1, ".set", true).set_set_count(@$, $2); }
| WRAP_TARGET { pioasm.get_current_program(@1, ".wrap_target").set_wrap_target(@$); }
| WRAP expression { pioasm.get_current_program(@1, ".wrap").set_wrap(@$, $2); }
| WRAP { pioasm.get_current_program(@1, ".wrap").set_wrap(@$); }
| WORD value { pioasm.get_current_program(@1, "instruction").add_instruction(std::shared_ptr<instruction>(new instr_word(@$, $2))); }
| LANG_OPT NON_WS NON_WS ASSIGN INT { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, std::to_string($5)); }
| LANG_OPT NON_WS NON_WS ASSIGN STRING { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, $5); }
| LANG_OPT NON_WS NON_WS ASSIGN NON_WS { pioasm.get_current_program(@1, ".lang_opt").add_lang_opt($2, $3, $5); }
Expand All @@ -224,6 +225,7 @@ directive:
/* value is a more limited top level expression... requiring parenthesis */
%type <std::shared_ptr<resolvable>> value;
value: INT { $$ = resolvable_int(@$, $1); }
| DOT { $$ = resolvable_int(@$, pioasm.get_current_program(@1, ".").instructions.size()); }
| ID { $$ = std::shared_ptr<resolvable>(new name_ref(@$, $1)); }
| LPAREN expression RPAREN { $$ = $2; }

Expand Down Expand Up @@ -257,7 +259,8 @@ instruction:

%type <std::shared_ptr<instruction>> base_instruction;
base_instruction:
NOP { $$ = std::shared_ptr<instruction>(new instr_nop(@$)); }
WORD value { $$ = std::shared_ptr<instruction>(new instr_word(@$, $2)); }
| NOP { $$ = std::shared_ptr<instruction>(new instr_nop(@$)); }
| JMP condition comma expression { $$ = std::shared_ptr<instruction>(new instr_jmp(@$, $2, $4)); }
| WAIT value wait_source { $$ = std::shared_ptr<instruction>(new instr_wait(@$, $2, $3)); }
| WAIT wait_source { $$ = std::shared_ptr<instruction>(new instr_wait(@$, resolvable_int(@$, 1), $2)); }
Expand Down
15 changes: 13 additions & 2 deletions tools/pioasm/pio_assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ void program::set_wrap(const yy::location &l) {
wrap = resolvable_int(l, instructions.size() - 1);
}

void program::set_wrap(const yy::location &l, rvalue target) {
set_wrap(l);

if (wrap_target) {
std::stringstream msg;
msg << ".wrap_target was already specified at " << wrap_target->location;
throw syntax_error(l, msg.str());
}
wrap_target = std::move(target);
}

void program::set_wrap_target(const yy::location &l) {
if (wrap_target) {
std::stringstream msg;
Expand Down Expand Up @@ -299,12 +310,12 @@ raw_encoding instruction::raw_encode(program& program) {
throw syntax_error(location, "internal error");
}

uint instr_word::encode(program &program) {
raw_encoding instr_word::raw_encode(program& program) {
uint value = encoding->resolve(program);
if (value > 0xffffu) {
throw syntax_error(location, ".word value must be a positive 16 bit value");
}
return value;
return {inst_type(0), value >> 5, value & 0x1fu};
}

uint instr_mov::get_push_get_index(const program &program, extended_mov index) {
Expand Down
4 changes: 3 additions & 1 deletion tools/pioasm/pio_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ struct program : public src_item {

void set_wrap(const yy::location &l);

void set_wrap(const yy::location &l, rvalue target);

void set_sideset(const yy::location &l, rvalue _sideset, bool optional, bool pindirs) {
sideset = rvalue_loc(_sideset, l);
sideset_opt = optional;
Expand Down Expand Up @@ -486,7 +488,7 @@ struct instr_word : public instruction {

instr_word(const yy::location &l, rvalue encoding) : instruction(l), encoding(std::move(encoding)) {}

uint encode(program &program) override;
raw_encoding raw_encode(program &program) override;
};

#endif
10 changes: 10 additions & 0 deletions tools/pioasm/test/amethyst.pio
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ wait gpio 40
.mov_status txfifo < 12
.mov_status irq next set 3

.program wrap
decrement:
jmp x--, .+1
skip:
.wrap .+1
delay:
jmp x--, .
jump:
.word (.-1) [2]

.program python
.pio_version 1
wait 0 jmppin
Expand Down