Skip to content

Commit db66e64

Browse files
authored
Update text syntax for shared memory limits (#1197)
Following WebAssembly/threads#58 e.g. (memory $0 23 256 shared) is now (memory $0 (shared 23 256))
1 parent b29158d commit db66e64

34 files changed

Lines changed: 52 additions & 54 deletions

auto_update_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@
8383
wasm = os.path.basename(t).replace('.wast', '')
8484
cmd = WASM_OPT + [os.path.join('test', 'print', t), '--print']
8585
print ' ', ' '.join(cmd)
86-
actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
87-
print cmd, actual, err
86+
actual = subprocess.check_output(cmd)
87+
print cmd, actual
8888
with open(os.path.join('test', 'print', wasm + '.txt'), 'w') as o: o.write(actual)
8989
cmd = WASM_OPT + [os.path.join('test', 'print', t), '--print-minified']
9090
print ' ', ' '.join(cmd)
91-
actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
91+
actual = subprocess.check_output(cmd)
9292
with open(os.path.join('test', 'print', wasm + '.minified.txt'), 'w') as o: o.write(actual)
9393

9494
for t in sorted(os.listdir(os.path.join('test', 'passes'))):

src/passes/Print.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
718718
void printTableHeader(Table* curr) {
719719
printOpening(o, "table") << ' ';
720720
o << curr->initial;
721-
if (curr->max != Table::kMaxSize) o << ' ' << curr->max;
721+
if (curr->hasMax()) o << ' ' << curr->max;
722722
o << " anyfunc)";
723723
}
724724
void visitTable(Table *curr) {
@@ -746,9 +746,10 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
746746
void printMemoryHeader(Memory* curr) {
747747
printOpening(o, "memory") << ' ';
748748
printName(curr->name) << ' ';
749+
if (curr->shared) printOpening(o, "shared ");
749750
o << curr->initial;
750-
if (curr->max && curr->max != Memory::kMaxSize) o << ' ' << curr->max;
751-
if (curr->shared) o << " shared";
751+
if (curr->hasMax()) o << ' ' << curr->max;
752+
if (curr->shared) o << ")";
752753
o << ")";
753754
}
754755
void visitMemory(Memory* curr) {

src/wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ class Table {
672672
Table() : exists(false), imported(false), initial(0), max(kMaxSize) {
673673
name = Name::fromInt(0);
674674
}
675+
bool hasMax() { return max != kMaxSize; }
675676
};
676677

677678
class Memory {
@@ -705,6 +706,7 @@ class Memory {
705706
Memory() : initial(0), max(kMaxSize), exists(false), imported(false), shared(false) {
706707
name = Name::fromInt(0);
707708
}
709+
bool hasMax() { return max != kMaxSize; }
708710
};
709711

710712
class Global {

src/wasm/wasm-s-parser.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,17 +1512,9 @@ void SExpressionWasmBuilder::stringToBinary(const char* input, size_t size, std:
15121512
Index SExpressionWasmBuilder::parseMemoryLimits(Element& s, Index i) {
15131513
wasm.memory.initial = getCheckedAddress(s[i++], "excessive memory init");
15141514
if (i == s.size()) return i;
1515-
while (i < s.size() && s[i]->isStr()) {
1516-
auto* curr = s[i]->c_str();
1517-
i++;
1518-
if (strstr(curr, "shared")) {
1519-
wasm.memory.shared = strncmp(curr, "notshared", 9) != 0;
1520-
break;
1521-
}
1522-
uint64_t max = atoll(curr);
1523-
if (max > Memory::kMaxSize) throw ParseException("total memory must be <= 4GB");
1524-
wasm.memory.max = max;
1525-
}
1515+
uint64_t max = atoll(s[i++]->c_str());
1516+
if (max > Memory::kMaxSize) throw ParseException("total memory must be <= 4GB");
1517+
wasm.memory.max = max;
15261518
return i;
15271519
}
15281520

@@ -1557,6 +1549,10 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
15571549
if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col);
15581550
wasm.addImport(im.release());
15591551
i++;
1552+
} else if (inner[0]->str() == "shared") {
1553+
wasm.memory.shared = true;
1554+
parseMemoryLimits(inner, 1);
1555+
i++;
15601556
} else {
15611557
if (!(inner.size() > 0 ? inner[0]->str() != IMPORT : true)) throw ParseException("bad import ending");
15621558
// (memory (data ..)) format
@@ -1565,7 +1561,7 @@ void SExpressionWasmBuilder::parseMemory(Element& s, bool preParseImport) {
15651561
return;
15661562
}
15671563
}
1568-
i = parseMemoryLimits(s, i);
1564+
if (!wasm.memory.shared) i = parseMemoryLimits(s, i);
15691565

15701566
// Parse memory initializers.
15711567
while (i < s.size()) {
@@ -1765,7 +1761,14 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
17651761
}
17661762
// ends with the table element type
17671763
} else if (im->kind == ExternalKind::Memory) {
1768-
j = parseMemoryLimits(inner, j);
1764+
if (inner[j]->isList()) {
1765+
auto& limits = *inner[j];
1766+
if (!(limits[0]->isStr() && limits[0]->str() == "shared")) throw ParseException("bad memory limit declaration");
1767+
wasm.memory.shared = true;
1768+
parseMemoryLimits(limits, 1);
1769+
} else {
1770+
parseMemoryLimits(inner, j);
1771+
}
17691772
}
17701773
if (wasm.getImportOrNull(im->name)) throw ParseException("duplicate import", s.line, s.col);
17711774
wasm.addImport(im.release());

src/wasm/wasm-validator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ static bool checkOffset(Expression* curr, Address add, Address max) {
597597
void WasmValidator::visitMemory(Memory *curr) {
598598
shouldBeFalse(curr->initial > curr->max, "memory", "memory max >= initial");
599599
shouldBeTrue(curr->max <= Memory::kMaxSize, "memory", "max memory must be <= 4GB");
600+
shouldBeTrue(!curr->shared || curr->hasMax(), "memory", "shared memory must have max size");
600601
Index mustBeGreaterOrEqual = 0;
601602
for (auto& segment : curr->segments) {
602603
if (!shouldBeEqual(segment.offset->type, i32, segment.offset, "segment offset should be i32")) continue;

test/atomics.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 23 256 shared)
3+
(memory $0 (shared 23 256))
44
(func $atomic-loadstore (type $0)
55
(local $0 i32)
66
(local $1 i64)

test/atomics.wast.from-wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 23 256 shared)
3+
(memory $0 (shared 23 256))
44
(func $atomic-loadstore (type $0)
55
(local $0 i32)
66
(local $1 i64)

test/atomics.wast.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 23 256 shared)
3+
(memory $0 (shared 23 256))
44
(func $atomic-loadstore (type $0)
55
(local $var$0 i32)
66
(local $var$1 i64)

test/atomics.wast.fromBinary.noDebugInfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
22
(type $0 (func))
3-
(memory $0 23 256 shared)
3+
(memory $0 (shared 23 256))
44
(func $0 (type $0)
55
(local $var$0 i32)
66
(local $var$1 i64)

test/memory-shared.wast

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
(module
2-
(memory $0 23 256 shared)
2+
(memory $0 (shared 23 256))
33
)
4-

0 commit comments

Comments
 (0)