198 const std::optional<uint16_t> sStart,
199 const std::optional<uint16_t> sEnd,
200 const std::optional<uint16_t> cPage)
const
204 fmt::print(
"Invalid format");
208 auto [major, minor, patch] =
version();
209 fmt::println(
"Version: {}.{}.{}", major, minor, patch);
210 fmt::println(
"Timestamp: {}",
timestamp());
211 fmt::print(
"SHA256: ");
212 for (
const auto sha =
sha256();
unsigned char h : sha)
213 fmt::print(
"{:02x}", h);
218 if ((sStart.has_value() && !sEnd.has_value()) || (!sStart.has_value() && sEnd.has_value()))
220 fmt::print(fmt::fg(fmt::color::red),
"Both start and end parameter need to be provided together\n");
223 if (sStart.has_value() && sEnd.has_value() && sStart.value() >= sEnd.value())
225 fmt::print(fmt::fg(fmt::color::red),
"Invalid slice start and end arguments\n");
230 const auto vals =
values(syms);
231 const auto code_block =
code(vals);
235 std::size_t size = syms.symbols.size();
236 std::size_t sliceSize = size;
239 if (showSym && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
240 fmt::print(fmt::fg(fmt::color::red),
"Slice start or end can't be greater than the segment size: {}\n", size);
241 else if (showSym && sStart.has_value() && sEnd.has_value())
242 sliceSize = sEnd.value() - sStart.value() + 1;
245 fmt::println(
"{} (length: {})", fmt::styled(
"Symbols table", fmt::fg(fmt::color::cyan)), sliceSize);
247 for (std::size_t j = 0; j < size; ++j)
249 if (
auto start = sStart;
auto end = sEnd)
250 showSym = showSym && (j >= start.value() && j <= end.value());
253 fmt::println(
"{}) {}", j, syms.symbols[j]);
264 std::size_t size = vals.values.size();
265 std::size_t sliceSize = size;
268 if (showVal && sStart.has_value() && sEnd.has_value() && (sStart.value() > size || sEnd.value() > size))
269 fmt::print(fmt::fg(fmt::color::red),
"Slice start or end can't be greater than the segment size: {}\n", size);
270 else if (showVal && sStart.has_value() && sEnd.has_value())
271 sliceSize = sEnd.value() - sStart.value() + 1;
274 fmt::println(
"{} (length: {})", fmt::styled(
"Constants table", fmt::fg(fmt::color::cyan)), sliceSize);
276 for (std::size_t j = 0; j < size; ++j)
278 if (
auto start = sStart;
auto end = sEnd)
279 showVal = showVal && (j >= start.value() && j <= end.value());
283 switch (
const auto val = vals.values[j]; val.valueType())
286 fmt::println(
"{}) (Number) {}", j, val.number());
289 fmt::println(
"{}) (String) {}", j, val.string());
292 fmt::println(
"{}) (PageAddr) {}", j, val.pageAddr());
295 fmt::print(fmt::fg(fmt::color::red),
"Value type not handled: {}\n",
types_to_str[
static_cast<std::size_t
>(val.valueType())]);
307 const auto stringify_value = [](
const Value& val) -> std::string {
308 switch (val.valueType())
311 return fmt::format(
"{} (Number)", val.number());
313 return fmt::format(
"{} (String)", val.string());
315 return fmt::format(
"{} (PageAddr)", val.pageAddr());
335 const std::unordered_map<Instruction, ArgKind> arg_kinds = {
339 {
STORE, ArgKind::Symbol },
342 {
JUMP, ArgKind::Raw },
343 {
CALL, ArgKind::Raw },
347 {
DEL, ArgKind::Symbol },
350 {
PLUGIN, ArgKind::Value },
351 {
LIST, ArgKind::Raw },
358 const auto color_print_inst = [&syms, &vals, &stringify_value](
const std::string& name, std::optional<Arg> arg = std::nullopt) {
359 fmt::print(
"{}", fmt::styled(name, fmt::fg(fmt::color::gold)));
362 switch (
auto [kind, idx] = arg.value(); kind)
364 case ArgKind::Symbol:
365 fmt::print(fmt::fg(fmt::color::green),
" {}\n", syms.symbols[idx]);
368 fmt::print(fmt::fg(fmt::color::magenta),
" {}\n", stringify_value(vals.values[idx]));
370 case ArgKind::Builtin:
374 fmt::print(fmt::fg(fmt::color::red),
" ({})\n", idx);
386 for (
const auto& page : code_block.pages)
388 bool displayCode =
true;
390 if (
auto wanted_page = cPage)
391 displayCode = pp == wanted_page.value();
395 "{} {} (length: {})",
396 fmt::styled(
"Code segment", fmt::fg(fmt::color::magenta)),
397 fmt::styled(pp, fmt::fg(fmt::color::magenta)),
407 if (cPage.value_or(pp) != pp)
411 if (sStart.has_value() && sEnd.has_value() && ((sStart.value() > page.size()) || (sEnd.value() > page.size())))
413 fmt::print(fmt::fg(fmt::color::red),
"Slice start or end can't be greater than the segment size: {}\n", page.size());
417 for (std::size_t j = sStart.value_or(0), end = sEnd.value_or(page.size()); j < end; j += 4)
419 const uint8_t inst = page[j];
421 const uint8_t padding = page[j + 1];
422 const auto arg =
static_cast<uint16_t
>((page[j + 2] << 8) + page[j + 3]);
425 fmt::print(fmt::fg(fmt::color::cyan),
"{:>4}", j / 4);
427 fmt::print(
" {:02x} {:02x} {:02x} {:02x} ", inst, padding, page[j + 2], page[j + 3]);
429 if (
const auto idx =
static_cast<std::size_t
>(inst); idx <
InstructionNames.size())
432 if (
const auto iinst =
static_cast<Instruction>(inst); arg_kinds.contains(iinst))
433 color_print_inst(inst_name, Arg { arg_kinds.at(iinst), arg });
435 color_print_inst(inst_name);
438 fmt::println(
"Unknown instruction");