summaryrefslogtreecommitdiffstats
path: root/third_party/wasm2c/src/tools/wat-desugar.cc
blob: cc6a2cba8e6718080812a45ed57383f5f39a3543 (plain)
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
/*
 * Copyright 2016 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cassert>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>

#include "config.h"

#include "src/apply-names.h"
#include "src/common.h"
#include "src/error-formatter.h"
#include "src/feature.h"
#include "src/generate-names.h"
#include "src/ir.h"
#include "src/option-parser.h"
#include "src/stream.h"
#include "src/wast-parser.h"
#include "src/wat-writer.h"

using namespace wabt;

static const char* s_infile;
static const char* s_outfile;
static WriteWatOptions s_write_wat_options;
static bool s_generate_names;
static bool s_debug_parsing;
static Features s_features;

static const char s_description[] =
R"(  read a file in the wasm s-expression format and format it.

examples:
  # write output to stdout
  $ wat-desugar test.wat

  # write output to test2.wat
  $ wat-desugar test.wat -o test2.wat

  # generate names for indexed variables
  $ wat-desugar --generate-names test.wat
)";

static void ParseOptions(int argc, char** argv) {
  OptionParser parser("wat-desugar", s_description);

  parser.AddOption('o', "output", "FILE", "Output file for the formatted file",
                   [](const char* argument) { s_outfile = argument; });
  parser.AddOption("debug-parser", "Turn on debugging the parser of wat files",
                   []() { s_debug_parsing = true; });
  parser.AddOption('f', "fold-exprs", "Write folded expressions where possible",
                   []() { s_write_wat_options.fold_exprs = true; });
  parser.AddOption("inline-exports", "Write all exports inline",
                   []() { s_write_wat_options.inline_export = true; });
  parser.AddOption("inline-imports", "Write all imports inline",
                   []() { s_write_wat_options.inline_import = true; });
  s_features.AddOptions(&parser);
  parser.AddOption(
      "generate-names",
      "Give auto-generated names to non-named functions, types, etc.",
      []() { s_generate_names = true; });

  parser.AddArgument("filename", OptionParser::ArgumentCount::One,
                     [](const char* argument) { s_infile = argument; });
  parser.Parse(argc, argv);
}

int ProgramMain(int argc, char** argv) {
  InitStdio();
  ParseOptions(argc, argv);

  std::vector<uint8_t> file_data;
  Result result = ReadFile(s_infile, &file_data);
  if (Failed(result)) {
    WABT_FATAL("unable to read %s\n", s_infile);
  }

  std::unique_ptr<WastLexer> lexer(WastLexer::CreateBufferLexer(
      s_infile, file_data.data(), file_data.size()));

  Errors errors;
  std::unique_ptr<Script> script;
  WastParseOptions parse_wast_options(s_features);
  result = ParseWastScript(lexer.get(), &script, &errors, &parse_wast_options);
  auto line_finder = lexer->MakeLineFinder();
  FormatErrorsToFile(errors, Location::Type::Text);

  if (Succeeded(result)) {
    Module* module = script->GetFirstModule();
    if (!module) {
      WABT_FATAL("no module in file.\n");
    }

    if (s_generate_names) {
      result = GenerateNames(module);
    }

    if (Succeeded(result)) {
      result = ApplyNames(module);
    }

    if (Succeeded(result)) {
      FileStream stream(s_outfile ? FileStream(s_outfile) : FileStream(stdout));
      result = WriteWat(&stream, module, s_write_wat_options);
    }
  }

  return result != Result::Ok;
}

int main(int argc, char** argv) {
  WABT_TRY
  return ProgramMain(argc, argv);
  WABT_CATCH_BAD_ALLOC_AND_EXIT
}