From c66ae1d5c6b5342ed2dce0bbfa25ce6ba4ebe24b Mon Sep 17 00:00:00 2001 From: Dr-Carlos Date: Sat, 17 Dec 2022 20:31:59 +1030 Subject: [PATCH] _d: use expr_or_range to allow addresses or ranges --- shell/d.cpp | 43 ++++++++++++++++++++++--------------------- shell/parser.cpp | 13 ++++--------- shell/parser.h | 4 ++-- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/shell/d.cpp b/shell/d.cpp index 2468ae1..d688680 100644 --- a/shell/d.cpp +++ b/shell/d.cpp @@ -77,25 +77,25 @@ struct _d_args static _d_args parse_d(Session &session, Parser &parser) { + _d_args args; + if(!session.current_space) return {}; - uint32_t address = session.current_space->cursor; + args.address = session.current_space->cursor; + args.range = {}; if(!parser.at_end()) { auto v = parser.expr_or_range(session.current_space); - if(std::holds_alternative(v)) { - address = std::get(v); - } - else { - // TODO: Use an args struct etc. This is placeholder. - address = std::get(v).start; - } + if(std::holds_alternative(v)) + args.address = std::get(v); + else + args.range = std::get(v); } parser.end(); - return {address, {}}; + return args; } void _d(Session &session, std::optional address_opt, @@ -105,19 +105,8 @@ void _d(Session &session, std::optional address_opt, return; FxOS::Disassembly disasm(*session.current_space); - if(address_opt.has_value()) { - uint32_t address = address_opt.value(); - if(address & 1) { - fmt::print("address 0x{:08x} is odd, starting at 0x{:08x}\n", - address, address + 1); - address++; - } - - disassemble(session, disasm, - {"cfg", "pcrel", /*"constprop",*/ "syscall", "print"}, address); - } - else if(range_opt.has_value()) { + if(range_opt.has_value()) { Range range = range_opt.value(); if(range.start & 1) { fmt::print("address 0x{:08x} is odd, starting at 0x{:08x}\n", @@ -140,6 +129,18 @@ void _d(Session &session, std::optional address_opt, disassemble(session, disasm, {"pcrel", /*"constprop",*/ "syscall", "print"}, -1); } + else if(address_opt.has_value()) { + uint32_t address = address_opt.value(); + + if(address & 1) { + fmt::print("address 0x{:08x} is odd, starting at 0x{:08x}\n", + address, address + 1); + address++; + } + + disassemble(session, disasm, + {"cfg", "pcrel", /*"constprop",*/ "syscall", "print"}, address); + } } //--- diff --git a/shell/parser.cpp b/shell/parser.cpp index 7a7e41d..c826ca2 100644 --- a/shell/parser.cpp +++ b/shell/parser.cpp @@ -306,14 +306,9 @@ void Parser::accept_options() // Parsing rules for expressions //--- -long Parser::atom(std::optional token) +long Parser::atom() { - Token t; - - if(token.has_value()) - t = token.value(); - else - t = expect({'$', '(', '-', T::SYMBOL, T::NUM, T::SYSCALL}); + Token t = expect({'$', '(', '-', T::SYMBOL, T::NUM, T::SYSCALL}); if(t.type == T::SYMBOL) { /* TODO: Specify the space that symbols are taken from */ @@ -397,10 +392,10 @@ long Parser::term() return v; } -long Parser::expr(VirtualSpace *space, std::optional token) +long Parser::expr(VirtualSpace *space) { m_expr_space = space; - long val = atom(token); + long val = atom(); m_expr_space = nullptr; return val; } diff --git a/shell/parser.h b/shell/parser.h index 46660cc..693df9f 100644 --- a/shell/parser.h +++ b/shell/parser.h @@ -142,7 +142,7 @@ public: /* Read a numerical constant, or an expression. Expression uses the space to access the program counter and query symbol values */ long num(); - long expr(VirtualSpace *space, std::optional token={}); + long expr(VirtualSpace *space); /* Read a range; again $ and symbols are interpreted. If (before) and (after) are both specified, a single value will also be accepted, and the range [value-before, value+after) will be returned. */ @@ -194,7 +194,7 @@ private: /* Parsing rules for expressions */ long term(); long factor(); - long atom(std::optional token={}); + long atom(); /* true if we're completing a partial command, false if we're parsing a finished one */