Skip to content

Commit 1de108f

Browse files
committed
wasm2asm: use support/command-line.h
1 parent 5f3c6cf commit 1de108f

2 files changed

Lines changed: 40 additions & 36 deletions

File tree

src/wasm2asm-main.cpp

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,57 @@
1818
// wasm2asm console tool
1919
//
2020

21-
#include "wasm2asm.h"
21+
22+
#include "support/colors.h"
23+
#include "support/command-line.h"
24+
#include "support/file.h"
2225
#include "wasm-s-parser.h"
26+
#include "wasm2asm.h"
2327

2428
using namespace cashew;
2529
using namespace wasm;
2630

27-
namespace wasm {
28-
int debug = 0;
29-
}
30-
31-
int main(int argc, char **argv) {
32-
debug = getenv("WASM2ASM_DEBUG") ? getenv("WASM2ASM_DEBUG")[0] - '0' : 0;
33-
34-
char *infile = argv[1];
31+
int main(int argc, const char *argv[]) {
32+
Options options("wasm2asm", "Transform .wast files to asm.js");
33+
options
34+
.add("--output", "-o", "Output file (stdout if not specified)",
35+
Options::Arguments::One,
36+
[](Options *o, const std::string &argument) {
37+
o->extra["output"] = argument;
38+
Colors::disable();
39+
})
40+
.add_positional("INFILE", Options::Arguments::One,
41+
[](Options *o, const std::string &argument) {
42+
o->extra["infile"] = argument;
43+
});
44+
options.parse(argc, argv);
3545

36-
if (debug) std::cerr << "loading '" << infile << "'...\n";
37-
FILE *f = fopen(argv[1], "r");
38-
assert(f);
39-
fseek(f, 0, SEEK_END);
40-
int size = ftell(f);
41-
char *input = new char[size+1];
42-
rewind(f);
43-
int num = fread(input, 1, size, f);
44-
// On Windows, ftell() gives the byte position (\r\n counts as two bytes), but when
45-
// reading, fread() returns the number of characters read (\r\n is read as one char \n, and counted as one),
46-
// so return value of fread can be less than size reported by ftell, and that is normal.
47-
assert((num > 0 || size == 0) && num <= size);
48-
fclose(f);
49-
input[num] = 0;
46+
auto input(
47+
read_file<std::vector<char>>(options.extra["infile"], options.debug));
5048

51-
if (debug) std::cerr << "s-parsing...\n";
52-
SExpressionParser parser(input);
53-
Element& root = *parser.root;
49+
if (options.debug) std::cerr << "s-parsing..." << std::endl;
50+
SExpressionParser parser(input.data());
51+
Element &root = *parser.root;
5452

55-
if (debug) std::cerr << "w-parsing...\n";
53+
if (options.debug) std::cerr << "w-parsing..." << std::endl;
5654
AllocatingModule wasm;
5755
SExpressionWasmBuilder builder(wasm, *root[0], [&]() { abort(); });
5856

59-
if (debug) std::cerr << "asming...\n";
60-
Wasm2AsmBuilder wasm2asm;
57+
if (options.debug) std::cerr << "asming..." << std::endl;
58+
Wasm2AsmBuilder wasm2asm(options.debug);
6159
Ref asmjs = wasm2asm.processWasm(&wasm);
6260

63-
if (debug) {
64-
std::cerr << "a-printing...\n";
61+
if (options.debug) {
62+
std::cerr << "a-printing..." << std::endl;
6563
asmjs->stringify(std::cout, true);
6664
std::cout << '\n';
6765
}
6866

69-
if (debug) std::cerr << "j-printing...\n";
67+
if (options.debug) std::cerr << "j-printing..." << std::endl;
7068
JSPrinter jser(true, true, asmjs);
7169
jser.printAst();
72-
std::cout << jser.buffer << "\n";
70+
Output output(options.extra["output"], options.debug);
71+
output << jser.buffer << std::endl;
7372

74-
if (debug) std::cerr << "done.\n";
73+
if (options.debug) std::cerr << "done." << std::endl;
7574
}

src/wasm2asm.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
namespace wasm {
3434

35-
extern int debug;
36-
3735
using namespace cashew;
3836

3937
IString ASM_FUNC("asmFunc"),
@@ -106,6 +104,8 @@ void flattenAppend(Ref ast, Ref extra) {
106104

107105
class Wasm2AsmBuilder {
108106
public:
107+
Wasm2AsmBuilder(bool debug) : debug(debug), tableSize(-1) {}
108+
109109
Ref processWasm(Module* wasm);
110110
Ref processFunction(Function* func);
111111

@@ -171,6 +171,7 @@ class Wasm2AsmBuilder {
171171
}
172172

173173
private:
174+
bool debug;
174175
// How many temp vars we need
175176
std::vector<size_t> temps; // type => num temps
176177
// Which are currently free to use
@@ -186,6 +187,10 @@ class Wasm2AsmBuilder {
186187
void addImport(Ref ast, Import *import);
187188
void addTables(Ref ast, Module *wasm);
188189
void addExports(Ref ast, Module *wasm);
190+
191+
Wasm2AsmBuilder() = delete;
192+
Wasm2AsmBuilder(const Wasm2AsmBuilder &) = delete;
193+
Wasm2AsmBuilder &operator=(const Wasm2AsmBuilder &) = delete;
189194
};
190195

191196
Ref Wasm2AsmBuilder::processWasm(Module* wasm) {

0 commit comments

Comments
 (0)