Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Disassembler.cpp
Go to the documentation of this file.
3
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.noLogo)
22 {
23 std::println("Phasor Decompiler\nCopyright (c) 2026 Daniel McGuire");
24 }
25 if (m_args.showHelp)
26 {
27 showHelp();
28 return 0;
29 }
30
31 if (decompileBinary()) {
32 if (!m_args.silent) std::println("Success! Output to {}", m_args.outputFile.string());
33 return 0;
34 } else {
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 else if (arg == "-n" || arg == "--nologo")
66 {
67 m_args.noLogo = true;
68 }
69 if (arg == "-s" || arg == "--silent")
70 {
71 m_args.noLogo = true;
72 m_args.silent = true;
73 }
74 else if (arg[0] == '-')
75 {
76 std::println(std::cerr, "Error: Unknown option: {}", arg);
77 m_args.showHelp = true;
78 return true;
79 }
80 else
81 {
82 if (m_args.inputFile.empty())
83 m_args.inputFile = arg;
84 else
85 {
86 std::println(std::cerr, "Error: Multiple input files specified");
87 m_args.showHelp = true;
88 return true;
89 }
90 }
91 }
92 return false;
93}
94
96{
97 std::println("Usage:\n"
98 " {} [options] <input.phsb>"
99 "Options:"
100 " -o, --output <file> Output file"
101 " -h, --help Show this help message"
102 " -n, --nologo Do not show banner"
103 " -s, --silent Do not print anything except errors (no stdout)", m_args.program.stem().string());
104}
105
107{
108 BytecodeDeserializer bcDeserializer{};
109 PhasorIR phir;
110
111 if (m_args.outputFile.empty())
112 {
113 m_args.outputFile = m_args.inputFile;
114 std::filesystem::path path(m_args.outputFile);
115 path.replace_extension(".phir");
116 m_args.outputFile = path.string();
117 }
118
119 return phir.saveToFile(bcDeserializer.loadFromFile(m_args.inputFile), m_args.outputFile);
120}
121
122} // 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:624
The Phasor Programming Language and Runtime.
Definition AST.hpp:11