-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplzdisasm.m
More file actions
153 lines (125 loc) · 4.6 KB
/
plzdisasm.m
File metadata and controls
153 lines (125 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%-----------------------------------------------------------------------%
% Plasma assembler
% vim: ts=4 sw=4 et
%
% Copyright (C) Plasma Team
% Distributed under the terms of the MIT License see ../LICENSE.code
%
% This program disassembles pz intermediate representation.
%
%-----------------------------------------------------------------------%
:- module plzdisasm.
%-----------------------------------------------------------------------%
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
:- implementation.
:- import_module bool.
:- import_module char.
:- import_module cord.
:- import_module getopt.
:- import_module list.
:- import_module maybe.
:- import_module string.
:- import_module constant.
:- import_module pz.
:- import_module pz.pretty.
:- import_module pz.read.
:- import_module util.
:- import_module util.my_exception.
:- import_module util.mercury.
%-----------------------------------------------------------------------%
main(!IO) :-
io.command_line_arguments(Args0, !IO),
process_options(Args0, OptionsResult, !IO),
( OptionsResult = ok(PZDisOpts),
Mode = PZDisOpts ^ pzo_mode,
( Mode = disasm(InputFile),
promise_equivalent_solutions [!:IO] (
run_and_catch(do_dump(InputFile), plzasm,
HadErrors, !IO),
( HadErrors = had_errors,
io.set_exit_status(1, !IO)
; HadErrors = did_not_have_errors
)
)
; Mode = help,
usage(!IO)
; Mode = version,
version("Plasma Abstract Machine Disassembler", !IO)
)
; OptionsResult = error(ErrMsg),
exit_error(ErrMsg, !IO)
).
:- pred do_dump(string::in, io::di, io::uo) is det.
do_dump(InputFile, !IO) :-
read_pz(InputFile, Result, !IO),
( Result = ok(pz_read_result(Type, PZ)),
Pretty =
from_list(["// Plasma file type: ", string(Type), "\n\n"]) ++
pz_pretty(PZ),
write_string(append_list(list(Pretty)), !IO)
; Result = error(Error),
exit_error(Error, !IO)
).
%-----------------------------------------------------------------------%
:- type pzdis_options
---> pzdis_options(
pzo_mode :: pzo_mode
).
:- type pzo_mode
---> disasm(
pzmd_input_file :: string
)
; help
; version.
:- pred process_options(list(string)::in, maybe_error(pzdis_options)::out,
io::di, io::uo) is det.
process_options(Args0, Result, !IO) :-
OptionOpts = option_ops_multi(short_option, long_option, option_default),
getopt.process_options(OptionOpts, Args0, Args, MaybeOptions),
( MaybeOptions = ok(OptionTable),
lookup_bool_option(OptionTable, help, Help),
lookup_bool_option(OptionTable, version, Version),
( if Help = yes then
Result = ok(pzdis_options(help))
else if Version = yes then
Result = ok(pzdis_options(version))
else
( if Args = [InputFile] then
Result = ok(pzdis_options(disasm(InputFile)))
else
Result = error("Error processing command line options: " ++
"Expected exactly one input file")
)
)
; MaybeOptions = error(ErrMsg),
Result = error("Error processing command line options: " ++
option_error_to_string(ErrMsg))
).
:- pred usage(io::di, io::uo) is det.
usage(!IO) :-
io.write_string("Plasma disassembler\n\n", !IO),
io.write_string(
" The Plasma disassembler outputs a text representation of\n" ++
" Plasma bytecode files.\n\n", !IO),
io.write_string("Usage:\n\n", !IO),
io.progname_base("plzdisasm", ProgName, !IO),
io.format(" %s <input>\n", [s(ProgName)], !IO),
io.format(" %s -h | --help\n", [s(ProgName)], !IO),
io.format(" %s --version\n\n", [s(ProgName)], !IO).
:- type option
---> help
; version.
:- pred short_option(char::in, option::out) is semidet.
short_option('h', help).
:- pred long_option(string::in, option::out) is semidet.
long_option("help", help).
long_option("version", version).
:- pred option_default(option::out, option_data::out) is multi.
option_default(help, bool(no)).
option_default(version, bool(no)).
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%