summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sexpr-wasm.c50
-rw-r--r--src/wasm-binary-writer.c42
-rw-r--r--src/wasm-internal.h5
-rw-r--r--src/wasm.c42
-rw-r--r--src/wasm.h1
5 files changed, 86 insertions, 54 deletions
diff --git a/src/sexpr-wasm.c b/src/sexpr-wasm.c
index 5a76e46a..921e64a0 100644
--- a/src/sexpr-wasm.c
+++ b/src/sexpr-wasm.c
@@ -158,6 +158,9 @@ static void parse_options(int argc, char** argv) {
}
}
+ if (s_dump_module && s_spec)
+ FATAL("--dump-module flag incompatible with --spec flag\n");
+
if (optind < argc) {
s_infile = argv[optind];
} else {
@@ -178,18 +181,25 @@ int main(int argc, char** argv) {
result = result || parser.errors;
wasm_free_scanner(scanner);
+ WasmScript* script = &parser.script;
+
if (result != WASM_OK) {
- wasm_destroy_script(&parser.script);
+ wasm_destroy_script(script);
return result;
}
- result = wasm_check_script(&parser.script);
+ result = wasm_check_script(script);
if (result != WASM_OK) {
- wasm_destroy_script(&parser.script);
+ wasm_destroy_script(script);
return result;
}
- WasmFileWriter writer = {};
+ WasmMemoryWriter writer = {};
+ if (wasm_init_mem_writer(&writer) != WASM_OK) {
+ wasm_destroy_script(script);
+ FATAL("unable to open memory writer for writing\n");
+ }
+
WasmWriteBinaryOptions options = {};
if (s_spec)
options.spec = 1;
@@ -198,19 +208,35 @@ int main(int argc, char** argv) {
if (s_verbose)
options.log_writes = 1;
+ result = wasm_write_binary(&writer.base, script, &options);
+ wasm_destroy_script(script);
+
+ if (result != WASM_OK) {
+ wasm_close_mem_writer(&writer);
+ return result;
+ }
+
+ if (s_dump_module) {
+ if (s_verbose)
+ printf(";; dump\n");
+ wasm_print_memory(writer.buf.start, writer.buf.size, 0, 0, NULL);
+ }
+
if (s_outfile) {
- if (wasm_init_file_writer(&writer, s_outfile) != WASM_OK) {
- wasm_destroy_script(&parser.script);
+ FILE* f = fopen(s_outfile, "wb");
+ if (!f) {
+ wasm_close_mem_writer(&writer);
FATAL("unable to open %s for writing\n", s_outfile);
}
- result = wasm_write_binary(&writer.base, &parser.script, &options);
- wasm_close_file_writer(&writer);
- } else if (s_verbose) {
- /* don't write file output, but we still write the verbose output */
- result = wasm_write_binary(&writer.base, &parser.script, &options);
+ ssize_t bytes = fwrite(writer.buf.start, 1, writer.buf.size, f);
+ if (bytes != writer.buf.size) {
+ wasm_close_mem_writer(&writer);
+ FATAL("failed to write %zd bytes to %s\n", writer.buf.size, s_outfile);
+ }
+ fclose(f);
}
- wasm_destroy_script(&parser.script);
+ wasm_close_mem_writer(&writer);
return result;
}
diff --git a/src/wasm-binary-writer.c b/src/wasm-binary-writer.c
index 168a1b41..80a254e6 100644
--- a/src/wasm-binary-writer.c
+++ b/src/wasm-binary-writer.c
@@ -1,6 +1,5 @@
#include <alloca.h>
#include <assert.h>
-#include <ctype.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
@@ -12,7 +11,6 @@
#define DEFAULT_MEMORY_EXPORT 1
#define DUMP_OCTETS_PER_LINE 16
-#define DUMP_OCTETS_PER_GROUP 2
#define SEGMENT_SIZE 13
#define SEGMENT_OFFSET_OFFSET 4
@@ -390,44 +388,6 @@ static WasmTypeV8 wasm_type_to_v8_type(WasmType type) {
}
}
-static void dump_memory(const void* start,
- size_t size,
- size_t offset,
- int print_chars,
- const char* desc) {
- /* mimic xxd output */
- const uint8_t* p = start;
- const uint8_t* end = p + size;
- while (p < end) {
- const uint8_t* line = p;
- const uint8_t* line_end = p + DUMP_OCTETS_PER_LINE;
- printf("%07x: ", (int)((void*)p - start + offset));
- while (p < line_end) {
- int i;
- for (i = 0; i < DUMP_OCTETS_PER_GROUP; ++i, ++p) {
- if (p < end) {
- printf("%02x", *p);
- } else {
- putchar(' ');
- putchar(' ');
- }
- }
- putchar(' ');
- }
-
- putchar(' ');
- p = line;
- int i;
- for (i = 0; i < DUMP_OCTETS_PER_LINE && p < end; ++i, ++p)
- if (print_chars)
- printf("%c", isprint(*p) ? *p : '.');
- /* if there are multiple lines, only print the desc on the last one */
- if (p >= end && desc)
- printf(" ; %s", desc);
- putchar('\n');
- }
-}
-
static void print_header(WasmWriteContext* ctx, const char* name, int index) {
if (ctx->options->log_writes)
printf("; %s %d\n", name, index);
@@ -441,7 +401,7 @@ static void out_data_at(WasmWriterState* writer_state,
if (*writer_state->result != WASM_OK)
return;
if (writer_state->log_writes)
- dump_memory(src, size, offset, 0, desc);
+ wasm_print_memory(src, size, offset, 0, desc);
if (writer_state->writer->write_data)
*writer_state->result = writer_state->writer->write_data(
offset, src, size, writer_state->writer->user_data);
diff --git a/src/wasm-internal.h b/src/wasm-internal.h
index 97376685..285b22ca 100644
--- a/src/wasm-internal.h
+++ b/src/wasm-internal.h
@@ -56,5 +56,10 @@ typedef union WasmToken {
#define YYLTYPE WASM_LTYPE
int wasm_lex(WasmToken*, WasmLocation*, WasmScanner, WasmParser*);
+void wasm_print_memory(const void* start,
+ size_t size,
+ size_t offset,
+ int print_chars,
+ const char* desc);
#endif /* WASM_INTERNAL_H */
diff --git a/src/wasm.c b/src/wasm.c
index f7cc412b..b82fc2eb 100644
--- a/src/wasm.c
+++ b/src/wasm.c
@@ -1,9 +1,13 @@
#include "wasm.h"
#include <assert.h>
+#include <ctype.h>
#include <memory.h>
#include <stdlib.h>
+#define DUMP_OCTETS_PER_LINE 16
+#define DUMP_OCTETS_PER_GROUP 2
+
DEFINE_VECTOR(type, WasmType)
DEFINE_VECTOR(var, WasmVar);
DEFINE_VECTOR(expr_ptr, WasmExprPtr);
@@ -422,3 +426,41 @@ void wasm_destroy_command_vector_and_elements(WasmCommandVector* commands) {
void wasm_destroy_script(WasmScript* script) {
DESTROY_VECTOR_AND_ELEMENTS(script->commands, command);
}
+
+void wasm_print_memory(const void* start,
+ size_t size,
+ size_t offset,
+ int print_chars,
+ const char* desc) {
+ /* mimic xxd output */
+ const uint8_t* p = start;
+ const uint8_t* end = p + size;
+ while (p < end) {
+ const uint8_t* line = p;
+ const uint8_t* line_end = p + DUMP_OCTETS_PER_LINE;
+ printf("%07x: ", (int)((void*)p - start + offset));
+ while (p < line_end) {
+ int i;
+ for (i = 0; i < DUMP_OCTETS_PER_GROUP; ++i, ++p) {
+ if (p < end) {
+ printf("%02x", *p);
+ } else {
+ putchar(' ');
+ putchar(' ');
+ }
+ }
+ putchar(' ');
+ }
+
+ putchar(' ');
+ p = line;
+ int i;
+ for (i = 0; i < DUMP_OCTETS_PER_LINE && p < end; ++i, ++p)
+ if (print_chars)
+ printf("%c", isprint(*p) ? *p : '.');
+ /* if there are multiple lines, only print the desc on the last one */
+ if (p >= end && desc)
+ printf(" ; %s", desc);
+ putchar('\n');
+ }
+}
diff --git a/src/wasm.h b/src/wasm.h
index 92aff387..4cfb5685 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -545,7 +545,6 @@ typedef struct WasmScript {
typedef void* WasmScanner;
typedef struct WasmParser {
- WasmScanner scanner;
WasmScript script;
int errors;
} WasmParser;