Diff
Modified: trunk/Source/_javascript_Core/ChangeLog (189381 => 189382)
--- trunk/Source/_javascript_Core/ChangeLog 2015-09-04 20:59:12 UTC (rev 189381)
+++ trunk/Source/_javascript_Core/ChangeLog 2015-09-04 21:00:56 UTC (rev 189382)
@@ -1,3 +1,29 @@
+2015-09-04 Sukolsak Sakshuwong <[email protected]>
+
+ Implement the GetLocal and SetLocal instructions in WebAssembly
+ https://bugs.webkit.org/show_bug.cgi?id=148793
+
+ Reviewed by Saam Barati.
+
+ This patch implements the GetLocal and SetLocal instructions for locals
+ of type int32 in WebAssembly. A "local" in this context is either an
+ argument or a local variable.
+
+ * tests/stress/wasm-locals.js: Added.
+ (shouldBe):
+ * tests/stress/wasm-locals.wasm: Added.
+ * wasm/WASMFunctionCompiler.h:
+ (JSC::WASMFunctionCompiler::buildSetLocal):
+ (JSC::WASMFunctionCompiler::buildGetLocal):
+ * wasm/WASMFunctionParser.cpp:
+ (JSC::WASMFunctionParser::parseSetLocalStatement):
+ (JSC::WASMFunctionParser::parseExpressionI32):
+ (JSC::WASMFunctionParser::parseGetLocalExpressionI32):
+ * wasm/WASMFunctionParser.h:
+ * wasm/WASMFunctionSyntaxChecker.h:
+ (JSC::WASMFunctionSyntaxChecker::buildSetLocal):
+ (JSC::WASMFunctionSyntaxChecker::buildGetLocal):
+
2015-09-04 Basile Clement <[email protected]>
Unreviewed, add missing copyright to file from r189336
Added: trunk/Source/_javascript_Core/tests/stress/wasm-locals.js (0 => 189382)
--- trunk/Source/_javascript_Core/tests/stress/wasm-locals.js (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm-locals.js 2015-09-04 21:00:56 UTC (rev 189382)
@@ -0,0 +1,38 @@
+//@ skip
+
+function shouldBe(actual, expected) {
+ if (actual !== expected)
+ throw new Error('bad value: ' + actual);
+}
+
+/*
+wasm-locals.wasm is generated by pack-asmjs <https://github.com/WebAssembly/polyfill-prototype-1> from the following script:
+
+function asmModule(global, env, buffer) {
+ "use asm";
+
+ function sum(x, y) {
+ x = x | 0;
+ y = y | 0;
+ var result = 0;
+ result = (x + y) | 0;
+ return result;
+ }
+
+ function add1(x) {
+ x = x | 0;
+ return (x + 1) | 0;
+ }
+
+ return {
+ sum: sum,
+ add1: add1,
+ };
+}
+*/
+
+var module = loadWebAssembly("wasm-locals.wasm");
+
+shouldBe(module.sum(12, 30), 42);
+
+shouldBe(module.add1(42), 43);
Added: trunk/Source/_javascript_Core/tests/stress/wasm-locals.wasm (0 => 189382)
--- trunk/Source/_javascript_Core/tests/stress/wasm-locals.wasm (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/wasm-locals.wasm 2015-09-04 21:00:56 UTC (rev 189382)
@@ -0,0 +1 @@
+wasmB\x81\x82\xC0\xC1\xC0\xA1sumadd1
\ No newline at end of file
Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h (189381 => 189382)
--- trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h 2015-09-04 20:59:12 UTC (rev 189381)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionCompiler.h 2015-09-04 21:00:56 UTC (rev 189382)
@@ -127,6 +127,19 @@
m_codeBlock->capabilityLevel();
}
+ void buildSetLocal(uint32_t localIndex, int, WASMType type)
+ {
+ switch (type) {
+ case WASMType::I32:
+ load32(temporaryAddress(m_tempStackTop - 1), GPRInfo::regT0);
+ m_tempStackTop--;
+ store32(GPRInfo::regT0, localAddress(localIndex));
+ break;
+ default:
+ ASSERT_NOT_REACHED();
+ }
+ }
+
void buildReturn(int, WASMExpressionType returnType)
{
switch (returnType) {
@@ -151,6 +164,20 @@
return UNUSED;
}
+ int buildGetLocal(uint32_t localIndex, WASMType type)
+ {
+ switch (type) {
+ case WASMType::I32:
+ load32(localAddress(localIndex), GPRInfo::regT0);
+ m_tempStackTop++;
+ store32(GPRInfo::regT0, temporaryAddress(m_tempStackTop - 1));
+ break;
+ default:
+ ASSERT_NOT_REACHED();
+ }
+ return UNUSED;
+ }
+
int buildBinaryI32(int, int, WASMOpExpressionI32 op)
{
load32(temporaryAddress(m_tempStackTop - 2), GPRInfo::regT0);
Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp (189381 => 189382)
--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp 2015-09-04 20:59:12 UTC (rev 189381)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.cpp 2015-09-04 21:00:56 UTC (rev 189382)
@@ -210,8 +210,9 @@
{
FAIL_IF_FALSE(localIndex < m_localTypes.size(), "The local variable index is incorrect.");
WASMType type = m_localTypes[localIndex];
- parseExpression(context, WASMExpressionType(type));
- // FIXME: Implement this instruction.
+ ContextExpression _expression_ = parseExpression(context, WASMExpressionType(type));
+ PROPAGATE_ERROR();
+ context.buildSetLocal(localIndex, _expression_, type);
return UNUSED;
}
@@ -425,11 +426,12 @@
switch (op) {
case WASMOpExpressionI32::Immediate:
return parseImmediateExpressionI32(context);
+ case WASMOpExpressionI32::GetLocal:
+ return parseGetLocalExpressionI32(context);
case WASMOpExpressionI32::Add:
case WASMOpExpressionI32::Sub:
return parseBinaryExpressionI32(context, op);
case WASMOpExpressionI32::ConstantPoolIndex:
- case WASMOpExpressionI32::GetLocal:
case WASMOpExpressionI32::GetGlobal:
case WASMOpExpressionI32::SetLocal:
case WASMOpExpressionI32::SetGlobal:
@@ -507,8 +509,9 @@
switch (opWithImmediate) {
case WASMOpExpressionI32WithImmediate::Immediate:
return parseImmediateExpressionI32(context, immediate);
+ case WASMOpExpressionI32WithImmediate::GetLocal:
+ return parseGetLocalExpressionI32(context, immediate);
case WASMOpExpressionI32WithImmediate::ConstantPoolIndex:
- case WASMOpExpressionI32WithImmediate::GetLocal:
// FIXME: Implement these instructions.
FAIL_WITH_MESSAGE("Unsupported instruction.");
default:
@@ -533,6 +536,22 @@
}
template <class Context>
+ContextExpression WASMFunctionParser::parseGetLocalExpressionI32(Context& context, uint32_t localIndex)
+{
+ FAIL_IF_FALSE(localIndex < m_localTypes.size(), "The local index is incorrect.");
+ FAIL_IF_FALSE(m_localTypes[localIndex] == WASMType::I32, "Expected a local of type int32.");
+ return context.buildGetLocal(localIndex, WASMType::I32);
+}
+
+template <class Context>
+ContextExpression WASMFunctionParser::parseGetLocalExpressionI32(Context& context)
+{
+ uint32_t localIndex;
+ READ_COMPACT_UINT32_OR_FAIL(localIndex, "Cannot read the local index.");
+ return parseGetLocalExpressionI32(context, localIndex);
+}
+
+template <class Context>
ContextExpression WASMFunctionParser::parseBinaryExpressionI32(Context& context, WASMOpExpressionI32 op)
{
ContextExpression left = parseExpressionI32(context);
Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h (189381 => 189382)
--- trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h 2015-09-04 20:59:12 UTC (rev 189381)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionParser.h 2015-09-04 21:00:56 UTC (rev 189382)
@@ -80,6 +80,8 @@
template <class Context> ContextExpression parseExpressionI32(Context&);
template <class Context> ContextExpression parseImmediateExpressionI32(Context&, uint32_t immediate);
template <class Context> ContextExpression parseImmediateExpressionI32(Context&);
+ template <class Context> ContextExpression parseGetLocalExpressionI32(Context&, uint32_t localIndex);
+ template <class Context> ContextExpression parseGetLocalExpressionI32(Context&);
template <class Context> ContextExpression parseBinaryExpressionI32(Context&, WASMOpExpressionI32);
JSWASMModule* m_module;
Modified: trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h (189381 => 189382)
--- trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h 2015-09-04 20:59:12 UTC (rev 189381)
+++ trunk/Source/_javascript_Core/wasm/WASMFunctionSyntaxChecker.h 2015-09-04 21:00:56 UTC (rev 189382)
@@ -47,6 +47,11 @@
ASSERT(!m_tempStackTop);
}
+ void buildSetLocal(uint32_t, int, WASMType)
+ {
+ m_tempStackTop--;
+ }
+
void buildReturn(int, WASMExpressionType returnType)
{
if (returnType != WASMExpressionType::Void)
@@ -60,6 +65,13 @@
return UNUSED;
}
+ int buildGetLocal(uint32_t, WASMType)
+ {
+ m_tempStackTop++;
+ updateTempStackHeight();
+ return UNUSED;
+ }
+
int buildBinaryI32(int, int, WASMOpExpressionI32)
{
m_tempStackTop--;