Skip to content

Commit a6fee51

Browse files
committed
waforthc: Fix compilation against newer wasm2c
1 parent 93da58b commit a6fee51

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

src/waforthc/Makefile

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ WABT_DIR = $(shell brew --prefix)
2929
WABT_INCLUDE_DIR = $(WABT_DIR)/include
3030
WABT_LIB_DIR = $(WABT_DIR)/lib
3131
WABT_DATA_DIR = $(WABT_DIR)/share/wabt
32-
LIBS := -mmacosx-version-min=13.0
32+
LIBS := -mmacosx-version-min=13.0 /opt/homebrew/lib/libcrypto.a
3333
else # $(UNAME_S)
3434
GCC_TARGET_MACHINE = $(shell gcc -dumpmachine)
3535
WABT_INCLUDE_DIR ?= /usr/lib/include
@@ -69,7 +69,7 @@ all: waforthc
6969
waforthc: $(OBJECTS)
7070
$(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)
7171

72-
waforthc.o: waforth_core.h waforth_rt.h waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt-impl_h.h waforth_wabt_wasm-rt_h.h
72+
waforthc.o: waforth_core.h waforth_rt.h waforth_wabt_wasm-rt_h.h waforth_wabt_wasm-rt-impl_h.h waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt-impl-tableops_inc.h waforth_wabt_wasm-rt-exceptions_h.h waforth_wabt_wasm-rt-exceptions-impl_c.h waforth_wabt_wasm-rt-mem-impl_c.h waforth_wabt_wasm-rt-mem-impl-helper_inc.h
7373

7474
%.o: %.cpp
7575
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
@@ -80,13 +80,28 @@ waforth_core.h: waforth_core.wasm
8080
waforth_rt.h: rt.c
8181
$(BIN2H) $< $@
8282

83+
waforth_wabt_wasm-rt_h.h: $(WABT_INCLUDE_DIR)/wasm-rt.h
84+
$(BIN2H) $< $@
85+
8386
waforth_wabt_wasm-rt-impl_c.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-impl.c
8487
$(BIN2H) $< $@
8588

8689
waforth_wabt_wasm-rt-impl_h.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-impl.h
8790
$(BIN2H) $< $@
8891

89-
waforth_wabt_wasm-rt_h.h: $(WABT_INCLUDE_DIR)/wasm-rt.h
92+
waforth_wabt_wasm-rt-impl-tableops_inc.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-impl-tableops.inc
93+
$(BIN2H) $< $@
94+
95+
waforth_wabt_wasm-rt-exceptions_h.h: $(WABT_INCLUDE_DIR)/wasm-rt-exceptions.h
96+
$(BIN2H) $< $@
97+
98+
waforth_wabt_wasm-rt-exceptions-impl_c.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-exceptions-impl.c
99+
$(BIN2H) $< $@
100+
101+
waforth_wabt_wasm-rt-mem-impl_c.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-mem-impl.c
102+
$(BIN2H) $< $@
103+
104+
waforth_wabt_wasm-rt-mem-impl-helper_inc.h: $(WABT_DATA_DIR)/wasm2c/wasm-rt-mem-impl-helper.inc
90105
$(BIN2H) $< $@
91106

92107
waforth_core.wasm: ../waforth.wat
@@ -111,4 +126,4 @@ clean:
111126
-rm -rf waforthc *.exe *.o *.tgz *.zip \
112127
waforth_core.wasm waforth_core.h \
113128
waforth_rt.h \
114-
waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt_h.h waforth_wabt_wasm-rt-impl_h.h
129+
waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt-impl_c.h waforth_wabt_wasm-rt_h.h waforth_wabt_wasm-rt-impl_h.h waforth_wabt_wasm-rt-impl-tableops_inc.h

src/waforthc/rt.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdlib.h>
44
#include <termios.h>
55

6+
#include "wasm-rt-exceptions.h"
67
#include "wasm-rt-impl.h"
78

89
#include "_waforth.h"
@@ -18,15 +19,15 @@
1819

1920
size_t initOffset = 0;
2021

21-
struct Z_shell_instance_t {
22+
struct w2c_shell {
2223
wasm_rt_memory_t *memory;
2324
};
2425

25-
void Z_shellZ_emit(struct Z_shell_instance_t *mod, u32 c) {
26+
void w2c_shell_emit(struct w2c_shell *mod, u32 c) {
2627
putchar(c);
2728
}
2829

29-
u32 Z_shellZ_read(struct Z_shell_instance_t *mod, u32 addr_, u32 len_) {
30+
u32 w2c_shell_read(struct w2c_shell *mod, u32 addr_, u32 len_) {
3031
size_t len = len_;
3132
char *addr = (char *)&mod->memory->data[addr_];
3233
int n = 0;
@@ -52,7 +53,7 @@ u32 Z_shellZ_read(struct Z_shell_instance_t *mod, u32 addr_, u32 len_) {
5253
return n;
5354
}
5455

55-
u32 Z_shellZ_key(struct Z_shell_instance_t *mod) {
56+
u32 w2c_shell_key(struct w2c_shell *mod) {
5657
struct termios old, current;
5758
tcgetattr(0, &old);
5859
current = old;
@@ -64,27 +65,27 @@ u32 Z_shellZ_key(struct Z_shell_instance_t *mod) {
6465
return ch;
6566
}
6667

67-
u32 Z_shellZ_random(struct Z_shell_instance_t *mod) {
68+
u32 w2c_shell_random(struct w2c_shell *mod) {
6869
return random();
6970
}
7071

71-
void Z_shellZ_call(struct Z_shell_instance_t *mod) {
72+
void w2c_shell_call(struct w2c_shell *mod) {
7273
printf("`call` not available in native compiled mode\n");
7374
wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
7475
}
7576

76-
void Z_shellZ_load(struct Z_shell_instance_t *mod, u32 addr, u32 len) {
77+
void w2c_shell_load(struct w2c_shell *mod, u32 addr, u32 len) {
7778
printf("Compilation is not available in native compiled mode\n");
7879
wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
7980
}
8081

81-
int run(Z_waforth_instance_t *mod) {
82+
int run(w2c_waforth *mod) {
8283
u32 err;
8384

8485
wasm_rt_trap_t code = wasm_rt_impl_try();
8586
if (code == WASM_RT_TRAP_UNREACHABLE) {
8687
trap:
87-
err = Z_waforthZ_error(mod);
88+
err = w2c_waforth_error(mod);
8889
switch (err) {
8990
case ERR_QUIT:
9091
case ERR_ABORT:
@@ -102,20 +103,19 @@ int run(Z_waforth_instance_t *mod) {
102103
printf("trap %d\n", code);
103104
return -1;
104105
}
105-
Z_waforthZ_run(mod, sizeof(waforth_init) > 0);
106+
w2c_waforth_run(mod, sizeof(waforth_init) > 0);
106107
goto trap;
107108
}
108109

109110
int main(int argc, char *argv[]) {
110-
struct Z_shell_instance_t shell;
111-
Z_waforth_instance_t mod;
111+
struct w2c_shell shell;
112+
w2c_waforth mod;
112113

113114
wasm_rt_init();
114-
Z_waforth_init_module();
115-
Z_waforth_instantiate(&mod, &shell);
116-
shell.memory = Z_waforthZ_memory(&mod);
115+
wasm2c_waforth_instantiate(&mod, &shell);
116+
shell.memory = w2c_waforth_memory(&mod);
117117
int ret = run(&mod);
118-
Z_waforth_free(&mod);
118+
wasm2c_waforth_free(&mod);
119119
wasm_rt_free();
120120
return ret;
121121
}

src/waforthc/waforthc.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131

3232
#include "waforth_core.h"
3333
#include "waforth_rt.h"
34+
#include "waforth_wabt_wasm-rt-exceptions-impl_c.h"
35+
#include "waforth_wabt_wasm-rt-exceptions_h.h"
36+
#include "waforth_wabt_wasm-rt-impl-tableops_inc.h"
3437
#include "waforth_wabt_wasm-rt-impl_c.h"
3538
#include "waforth_wabt_wasm-rt-impl_h.h"
39+
#include "waforth_wabt_wasm-rt-mem-impl-helper_inc.h"
40+
#include "waforth_wabt_wasm-rt-mem-impl_c.h"
3641
#include "waforth_wabt_wasm-rt_h.h"
3742

3843
namespace fs = std::filesystem;
@@ -121,7 +126,7 @@ wabt::Result compileToNative(wabt::Module &mod, const std::string &init, const s
121126
std::ostringstream wds;
122127
wds << "waforthc." << rand();
123128
auto wd = fs::temp_directory_path() / wds.str();
124-
defer _(nullptr, [=](...) { fs::remove_all(wd); });
129+
// defer _(nullptr, [=](...) { fs::remove_all(wd); });
125130
if (!fs::exists(wd) && !fs::create_directory(wd)) {
126131
std::cerr << "error creating working directory " << wd << std::endl;
127132
return wabt::Result::Error;
@@ -144,14 +149,31 @@ wabt::Result compileToNative(wabt::Module &mod, const std::string &init, const s
144149
wcopt.module_name = "waforth";
145150
wabt::FileStream c_stream((wd / "_waforth.c").string());
146151
wabt::FileStream h_stream((wd / "_waforth.h").string());
147-
CHECK_RESULT(WriteC(&c_stream, &h_stream, "_waforth.h", &mod, wcopt));
152+
CHECK_RESULT(WriteC({&c_stream}, &h_stream, &c_stream, "_waforth.h", "", &mod, wcopt));
148153
wabt::FileStream((wd / "_waforth_rt.c").string()).WriteData(waforth_rt, sizeof(waforth_rt));
149-
wabt::FileStream((wd / "wasm-rt-impl.c").string()).WriteData(waforth_wabt_wasm_rt_impl_c, sizeof(waforth_wabt_wasm_rt_impl_c));
150-
wabt::FileStream((wd / "wasm-rt-impl.h").string()).WriteData(waforth_wabt_wasm_rt_impl_h, sizeof(waforth_wabt_wasm_rt_impl_h));
151154
wabt::FileStream((wd / "wasm-rt.h").string()).WriteData(waforth_wabt_wasm_rt_h, sizeof(waforth_wabt_wasm_rt_h));
155+
wabt::FileStream((wd / "wasm-rt-impl.h").string()).WriteData(waforth_wabt_wasm_rt_impl_h, sizeof(waforth_wabt_wasm_rt_impl_h));
156+
wabt::FileStream((wd / "wasm-rt-impl.c").string()).WriteData(waforth_wabt_wasm_rt_impl_c, sizeof(waforth_wabt_wasm_rt_impl_c));
157+
wabt::FileStream((wd / "wasm-rt-impl-tableops.inc").string())
158+
.WriteData(waforth_wabt_wasm_rt_impl_tableops_inc, sizeof(waforth_wabt_wasm_rt_impl_tableops_inc));
159+
wabt::FileStream((wd / "wasm-rt-exceptions.h").string()).WriteData(waforth_wabt_wasm_rt_exceptions_h, sizeof(waforth_wabt_wasm_rt_exceptions_h));
160+
wabt::FileStream((wd / "wasm-rt-exceptions-impl.c").string())
161+
.WriteData(waforth_wabt_wasm_rt_exceptions_impl_c, sizeof(waforth_wabt_wasm_rt_exceptions_impl_c));
162+
wabt::FileStream((wd / "wasm-rt-mem-impl.c").string()).WriteData(waforth_wabt_wasm_rt_mem_impl_c, sizeof(waforth_wabt_wasm_rt_mem_impl_c));
163+
wabt::FileStream((wd / "wasm-rt-mem-impl-helper.inc").string())
164+
.WriteData(waforth_wabt_wasm_rt_mem_impl_helper_inc, sizeof(waforth_wabt_wasm_rt_mem_impl_helper_inc));
152165
}
153166

154-
std::vector<std::string> cmd = {cc, "-o", outfile, (wd / "_waforth_rt.c").string(), (wd / "_waforth.c").string(), (wd / "wasm-rt-impl.c").string()};
167+
std::vector<std::string> cmd = {
168+
cc,
169+
"-o",
170+
outfile,
171+
(wd / "_waforth_rt.c").string(),
172+
(wd / "_waforth.c").string(),
173+
(wd / "wasm-rt-impl.c").string(),
174+
(wd / "wasm-rt-exceptions-impl.c").string(),
175+
(wd / "wasm-rt-mem-impl.c").string(),
176+
};
155177
cmd.insert(cmd.end(), cflags.begin(), cflags.end());
156178
if (runChild(cmd) != 0) {
157179
std::cerr << "error compiling";
@@ -346,21 +368,21 @@ wabt::Result compileToModule(std::vector<wabt::Module> &words, const std::vector
346368
wabt::Module &compiled, wabt::Errors &errors) {
347369
CHECK_RESULT(readModule("waforth.wasm", waforth_core, sizeof(waforth_core), compiled, errors));
348370

349-
auto dsf = wabt::MakeUnique<wabt::DataSegmentModuleField>();
371+
auto dsf = std::make_unique<wabt::DataSegmentModuleField>();
350372
wabt::DataSegment &ds = dsf->data_segment;
351373
ds.memory_var = wabt::Var(0, wabt::Location());
352-
ds.offset.push_back(wabt::MakeUnique<wabt::ConstExpr>(wabt::Const::I32(dataOffset)));
374+
ds.offset.push_back(std::make_unique<wabt::ConstExpr>(wabt::Const::I32(dataOffset)));
353375
ds.data = data;
354376
compiled.AppendField(std::move(dsf));
355377

356-
compiled.globals[HERE_GLOBAL_INDEX]->init_expr = wabt::ExprList{wabt::MakeUnique<wabt::ConstExpr>(wabt::Const::I32(dataOffset + data.size()))};
357-
compiled.globals[LATEST_GLOBAL_INDEX]->init_expr = wabt::ExprList{wabt::MakeUnique<wabt::ConstExpr>(wabt::Const::I32(latest))};
378+
compiled.globals[HERE_GLOBAL_INDEX]->init_expr = wabt::ExprList{std::make_unique<wabt::ConstExpr>(wabt::Const::I32(dataOffset + data.size()))};
379+
compiled.globals[LATEST_GLOBAL_INDEX]->init_expr = wabt::ExprList{std::make_unique<wabt::ConstExpr>(wabt::Const::I32(latest))};
358380

359381
for (auto &word : words) {
360382
assert(word.funcs.size() == 1);
361383
// compiled.funcs.push_back(word.funcs[0]);
362384

363-
auto ff = wabt::MakeUnique<wabt::FuncModuleField>();
385+
auto ff = std::make_unique<wabt::FuncModuleField>();
364386
auto &f = ff->func;
365387
f.name = word.funcs[0]->name;
366388
f.decl = word.funcs[0]->decl;
@@ -371,15 +393,15 @@ wabt::Result compileToModule(std::vector<wabt::Module> &words, const std::vector
371393

372394
assert(word.elem_segments.size() == 1);
373395
auto elem = word.elem_segments[0];
374-
auto esf = wabt::MakeUnique<wabt::ElemSegmentModuleField>();
396+
auto esf = std::make_unique<wabt::ElemSegmentModuleField>();
375397
wabt::ElemSegment &es = esf->elem_segment;
376398
es.kind = elem->kind;
377399
es.name = elem->name;
378400
es.table_var = elem->table_var;
379401
es.elem_type = elem->elem_type;
380402
assert(elem->elem_type = wabt::Type::FuncRef);
381403
es.offset = std::move(elem->offset);
382-
es.elem_exprs.push_back(wabt::ExprList{wabt::MakeUnique<wabt::RefFuncExpr>(wabt::Var(compiled.funcs.size() - 1, wabt::Location()))});
404+
es.elem_exprs.push_back(wabt::ExprList{std::make_unique<wabt::RefFuncExpr>(wabt::Var(compiled.funcs.size() - 1, wabt::Location()))});
383405
compiled.AppendField(std::move(esf));
384406
}
385407

0 commit comments

Comments
 (0)