Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Disassembler.cpp
Go to the documentation of this file.
3#include <version.h>
4#include "Disassembler.hpp"
5
6#include <filesystem>
7#include <fstream>
8#include <print>
9#include <sstream>
10
11namespace Phasor
12{
13
14Disassembler::Disassembler(int argc, char *argv[])
15{
16 parseArguments(argc, argv);
17}
18
20{
21 if (m_args.showHelp)
22 {
23 showHelp();
24 return 0;
25 }
26
27 if (decompileBinary())
28 {
29 if (!m_args.silent)
30 std::println("Success! Output to {}", m_args.outputFile.string());
31 return 0;
32 }
33 else
34 {
35 std::println(std::cerr, "Failed to disassemble program!");
36 return 1;
37 }
38}
39
40bool Disassembler::parseArguments(int argc, char *argv[])
41{
42 m_args.program = std::filesystem::path(argv[0]);
43 for (int i = 1; i < argc; i++)
44 {
45 std::string arg = argv[i];
46
47 if (arg == "-h" || arg == "--help")
48 {
49 m_args.showHelp = true;
50 return true;
51 }
52 else if (arg == "-o" || arg == "--output")
53 {
54 if (i + 1 < argc)
55 {
56 m_args.outputFile = argv[++i];
57 }
58 else
59 {
60 std::println(std::cerr, "Error: {} requires an argument", arg);
61 m_args.showHelp = true;
62 return true;
63 }
64 }
65 if (arg == "-s" || arg == "--silent")
66 {
67 m_args.silent = true;
68 }
69 else if (arg[0] == '-')
70 {
71 std::println(std::cerr, "Error: Unknown option: {}", arg);
72 m_args.showHelp = true;
73 return true;
74 }
75 else
76 {
77 if (m_args.inputFile.empty())
78 m_args.inputFile = arg;
79 else
80 {
81 std::println(std::cerr, "Error: Multiple input files specified");
82 m_args.showHelp = true;
83 return true;
84 }
85 }
86 }
87 return false;
88}
89
91{
92 std::println("Phasor Disassembler v{}\n"
93 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
94 "Usage:\n"
95 " {} [options] <input.phsb>\n"
96 "Options:\n"
97 " -o, --output <file> Output file\n"
98 " -h, --help Show this help message\n"
99 " -s, --silent Do not print anything except errors (no stdout)",
100 PHASOR_VERSION_STRING, m_args.program.stem().string());
101}
102
104{
105 BytecodeDeserializer bcDeserializer{};
106 PhasorIR phir;
107
108 if (m_args.outputFile.empty())
109 {
110 m_args.outputFile = m_args.inputFile;
111 std::filesystem::path path(m_args.outputFile);
112 path.replace_extension(".phir");
113 m_args.outputFile = path.string();
114 }
115
116 return phir.saveToFile(bcDeserializer.loadFromFile(m_args.inputFile), m_args.outputFile);
117}
118
119} // namespace Phasor
Bytecode binary format deserializer.
Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phsb file.
struct Phasor::Disassembler::Args m_args
bool parseArguments(int argc, char *argv[])
Disassembler(int argc, char *argv[])
Phasor IR Serializer/Deserializer.
Definition PhasorIR.hpp:18
static bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phir file.
Definition PhasorIR.cpp:647
The Phasor Programming Language and Runtime.
Definition AST.hpp:12