Chromium Code Reviews| Index: runtime/wasm-runtime.cpp |
| diff --git a/runtime/wasm-runtime.cpp b/runtime/wasm-runtime.cpp |
| index 609da7acba41a49aee7834459cce0addd4273945..b2ab465b137337886ca6f7a403fd383aa5d06e2d 100644 |
| --- a/runtime/wasm-runtime.cpp |
| +++ b/runtime/wasm-runtime.cpp |
| @@ -12,13 +12,24 @@ |
| // |
| //===----------------------------------------------------------------------===// |
| +#include <cassert> |
| #include <cmath> |
| +#include <stdint.h> |
|
Jim Stichnoth
2016/04/22 20:48:53
<cstdint> ?
(I think cstdint requires C++11 or lat
Eric Holk
2016/04/22 22:32:47
So far I've been avoiding using C++11 because of s
|
| + |
| +namespace { |
| +int32_t HeapBreak; |
| +const int32_t PageSizeLog2 = 16; |
|
Jim Stichnoth
2016/04/22 20:48:53
constexpr
Jim Stichnoth
2016/04/22 20:48:54
If it's a Log2 value, make it uint32_t
Eric Holk
2016/04/22 22:32:47
Done.
Eric Holk
2016/04/22 22:32:47
See my previous comment about C++11. I added a TOD
|
| +const int32_t PageSize = 1 << PageSizeLog2; // 64KB |
|
Jim Stichnoth
2016/04/22 20:48:53
uint32_t ?
Eric Holk
2016/04/22 22:32:47
Done. I'll replace most of the other int32_t with
|
| +const int32_t StackPtrLoc = 1024; // defined by emscripten |
| + |
| +int32_t pageNum(int32_t Index) { return Index >> PageSizeLog2; } |
| +} // end of anonymous namespace |
| namespace env { |
| double floor(double X) { return std::floor(X); } |
| float floor(float X) { return std::floor(X); } |
| -} |
| +} // end of env namespace |
|
Jim Stichnoth
2016/04/22 20:48:53
end of namespace env
Eric Holk
2016/04/22 22:32:48
Done.
|
| // TODO (eholk): move the C parts outside and use C++ name mangling. |
| extern "C" { |
| @@ -33,6 +44,8 @@ extern "C" { |
| #include <unistd.h> |
| extern char WASM_MEMORY[]; |
| +extern int32_t WASM_DATA_SIZE; |
|
Jim Stichnoth
2016/04/22 20:48:53
uint32_t ?
Eric Holk
2016/04/22 22:32:48
Done.
|
| +extern int32_t WASM_NUM_PAGES; |
| void env$$abort() { |
| fprintf(stderr, "Aborting...\n"); |
| @@ -53,7 +66,11 @@ void env$$_exit(int Status) { env$$exit(Status); } |
| abort(); \ |
| } |
| -UNIMPLEMENTED(sbrk) |
| +int32_t env$$sbrk(int32_t Increment) { |
| + HeapBreak += Increment; |
| + return HeapBreak; |
| +} |
| + |
| UNIMPLEMENTED(setjmp) |
| UNIMPLEMENTED(longjmp) |
| UNIMPLEMENTED(__assert_fail) |
| @@ -83,18 +100,16 @@ UNIMPLEMENTED(__lock) |
| UNIMPLEMENTED(__unlock) |
| UNIMPLEMENTED(__syscall6) // sys_close |
| UNIMPLEMENTED(__syscall140) // sys_llseek |
| -UNIMPLEMENTED(__syscall192) // sys_mmap? |
| UNIMPLEMENTED(__unordtf2) |
| UNIMPLEMENTED(__fixunstfsi) |
| UNIMPLEMENTED(__floatunsitf) |
| UNIMPLEMENTED(__extenddftf2) |
| void *wasmPtr(int Index) { |
| - // TODO (eholk): get the mask from the WASM file. |
| - const int MASK = 0xffffff; |
| - Index &= MASK; |
| - |
| - return WASM_MEMORY + Index; |
| + if (pageNum(Index) < WASM_NUM_PAGES && pageNum(Index) >= 0) { |
| + return WASM_MEMORY + Index; |
| + } |
| + abort(); |
| } |
| extern int __szwasm_main(int, const char **); |
| @@ -102,7 +117,15 @@ extern int __szwasm_main(int, const char **); |
| #define WASM_REF(Type, Index) ((Type *)wasmPtr(Index)) |
| #define WASM_DEREF(Type, Index) (*WASM_REF(Type, Index)) |
| -int main(int argc, const char **argv) { return __szwasm_main(argc, argv); } |
| +int main(int argc, const char **argv) { |
| + // Initialize the break to the nearest page boundary after the data segment |
| + HeapBreak = (WASM_DATA_SIZE + PageSize - 1) & ~(PageSize - 1); |
| + |
| + // Initialize the stack pointer. |
| + WASM_DEREF(int32_t, StackPtrLoc) = WASM_NUM_PAGES * PageSize; |
|
Jim Stichnoth
2016/04/22 20:48:53
Instead of "* PageSize", how about "<< PageSizeLog
Eric Holk
2016/04/22 22:32:47
I like the shift better, so done.
I'd rather keep
|
| + |
| + return __szwasm_main(argc, argv); |
| +} |
| int env$$abs(int a) { return abs(a); } |
| @@ -168,4 +191,14 @@ int env$$__syscall146(int Which, int VarArgs) { |
| } |
| return Count; |
| } |
| + |
| +/// sys_mmap_pgoff |
| +int env$$__syscall192(int Which, int VarArgs) { |
| + (void)Which; |
| + (void)VarArgs; |
| + |
| + // TODO (eholk): figure out how to implement this. |
| + |
| + return -ENOMEM; |
| +} |
| } // end of extern "C" |