Index: src/wasm/wasm-macro-gen.h |
diff --git a/src/wasm/wasm-macro-gen.h b/src/wasm/wasm-macro-gen.h |
index e40a2d4aaccc5633115d99c74aad5dfb66bb7b17..a56ad435b07b2e4f71c05c782c6be436902f1cdb 100644 |
--- a/src/wasm/wasm-macro-gen.h |
+++ b/src/wasm/wasm-macro-gen.h |
@@ -282,23 +282,108 @@ |
#define MASK_21 ((1 << 21) - 1) |
#define MASK_28 ((1 << 28) - 1) |
-#define U32V_1(x) static_cast<byte>(x & MASK_7) |
+#define U32V_1(x) static_cast<byte>((x)&MASK_7) |
bradnelson
2016/03/02 21:51:15
inconsistent spacing around operator
titzer
2016/03/02 22:43:41
Somehow that's what git cl format has done. Fixed
|
#define U32V_2(x) \ |
- static_cast<byte>((x & MASK_7) | 0x80), static_cast<byte>((x >> 7) & MASK_7) |
-#define U32V_3(x) \ |
- static_cast<byte>((x & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ |
- static_cast<byte>((x >> 14) & MASK_7) |
-#define U32V_4(x) \ |
- static_cast<byte>((x & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ |
- static_cast<byte>((x >> 21) & MASK_7) |
-#define U32V_5(x) \ |
- static_cast<byte>((x & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ |
- static_cast<byte>(((x >> 21) & MASK_7) | 0x80), \ |
- static_cast<byte>((x >> 28) & 0xF) |
+ static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7) |
+#define U32V_3(x) \ |
+ static_cast<byte>(((x)&MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
bradnelson
2016/03/02 21:51:15
yeah not having those parens was muy mal.
titzer
2016/03/02 22:43:41
Acknowledged.
|
+ static_cast<byte>(((x) >> 14) & MASK_7) |
+#define U32V_4(x) \ |
+ static_cast<byte>(((x)&MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
+ static_cast<byte>(((x) >> 21) & MASK_7) |
+#define U32V_5(x) \ |
+ static_cast<byte>(((x)&MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
+ static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \ |
+ static_cast<byte>(((x) >> 28) & 0xF) |
+#define WASM_NO_LOCALS 0 |
+ |
+#define WASM_DECL_LOCALS(count, type) \ |
+ static_cast<byte>(count), WasmOpcodes::LocalTypeCodeFor(type) |
+ |
+namespace v8 { |
+namespace internal { |
+namespace wasm { |
+ |
+// A helper for encoding local declarations prepended to the body of a |
+// function. |
+class LocalDeclEncoder { |
+ public: |
+ // Prepend local declarations by creating a new buffer and copying data |
+ // over. The new buffer must be delete[]'d by the caller. |
+ void Prepend(const byte** start, const byte** end) const { |
+ size_t size = (*end - *start); |
+ byte* buffer = new byte[5 + 6 * local_decls.size() + size]; |
binji
2016/03/02 22:51:38
name the magic numbers?
titzer
2016/03/02 23:03:28
Fixed to use Size(), which should be more robust.
|
+ size_t pos = Emit(buffer); |
+ memcpy(buffer + pos, *start, size); |
+ pos += size; |
+ *start = buffer; |
+ *end = buffer + pos; |
+ } |
+ |
+ size_t Emit(byte* buffer) const { |
+ size_t pos = 0; |
+ pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size())); |
+ for (size_t i = 0; i < local_decls.size(); i++) { |
+ pos = WriteUint32v(buffer, pos, local_decls[i].first); |
+ buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); |
+ } |
+ return pos; |
+ } |
+ |
+ // Add locals declarations to this helper. Return the index of the newly added |
+ // local(s), with an optional adjustment for the parameters. |
+ uint32_t AddLocals(uint32_t count, LocalType type, |
+ FunctionSig* sig = nullptr) { |
+ if (count == 0) return static_cast<uint32_t>(local_decls.size()); |
binji
2016/03/02 22:51:38
include sig->parameter_count() if sig is non-null?
titzer
2016/03/02 23:03:28
Done.
|
+ size_t pos = local_decls.size(); |
+ if (local_decls.size() > 0 && local_decls.back().second == type) { |
+ count += local_decls.back().first; |
+ local_decls.pop_back(); |
+ } |
+ local_decls.push_back(std::pair<uint32_t, LocalType>(count, type)); |
+ return static_cast<uint32_t>(pos + (sig ? sig->parameter_count() : 0)); |
+ } |
+ |
+ size_t Size() const { |
+ size_t size = SizeofUint32v(local_decls.size()); |
+ for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); |
+ return size; |
+ } |
+ |
+ private: |
+ std::vector<std::pair<uint32_t, LocalType>> local_decls; |
+ |
+ size_t SizeofUint32v(size_t val) const { |
bradnelson
2016/03/02 21:51:15
This will move out too I assume?
titzer
2016/03/02 22:43:41
Yeah, it needs to find a nice common home.
binji
2016/03/02 22:51:38
should be uint32_t val, right?
titzer
2016/03/02 23:03:28
Done.
|
+ size_t size = 1; |
+ while (true) { |
+ byte b = val & MASK_7; |
+ if (b == val) return size; |
+ size++; |
+ val = val >> 7; |
+ } |
+ } |
+ |
+ // TODO(titzer): lift encoding of u32v to a common place. |
+ size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const { |
+ while (true) { |
+ byte b = val & MASK_7; |
+ if (b == val) { |
+ buffer[pos++] = b; |
+ break; |
+ } |
+ buffer[pos++] = 0x80 | b; |
+ val = val >> 7; |
+ } |
+ return pos; |
+ } |
+}; |
+} // namespace wasm |
+} // namespace internal |
+} // namespace v8 |
#endif // V8_WASM_MACRO_GEN_H_ |