Skip to content

Commit 4ca311d

Browse files
authored
Print '(offset ...)` in data and element segments (#6379)
Previously we just printed the offset instruction(s) directly, which is a valid shorthand only when there is a single instruction. In the case of extended constant instructions, there can potentially be multiple instructions, in which case the explicit `offset` clause is required. Print the full clause when necessary.
1 parent f44912b commit 4ca311d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/passes/Print.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ir/iteration.h>
2222
#include <ir/module-utils.h>
2323
#include <ir/table-utils.h>
24+
#include <ir/utils.h>
2425
#include <pass.h>
2526
#include <pretty_printing.h>
2627
#include <support/string.h>
@@ -3113,7 +3114,14 @@ void PrintSExpression::visitElementSegment(ElementSegment* curr) {
31133114
}
31143115

31153116
o << ' ';
3117+
bool needExplicitOffset = Measurer{}.measure(curr->offset) > 1;
3118+
if (needExplicitOffset) {
3119+
o << "(offset ";
3120+
}
31163121
visit(curr->offset);
3122+
if (needExplicitOffset) {
3123+
o << ')';
3124+
}
31173125

31183126
if (usesExpressions || currModule->tables.size() > 1) {
31193127
o << ' ';
@@ -3183,7 +3191,14 @@ void PrintSExpression::visitDataSegment(DataSegment* curr) {
31833191
curr->memory.print(o);
31843192
o << ") ";
31853193
}
3194+
bool needExplicitOffset = Measurer{}.measure(curr->offset) > 1;
3195+
if (needExplicitOffset) {
3196+
o << "(offset ";
3197+
}
31863198
visit(curr->offset);
3199+
if (needExplicitOffset) {
3200+
o << ")";
3201+
}
31873202
o << ' ';
31883203
}
31893204
String::printEscaped(o, {curr->data.data(), curr->data.size()});

test/lit/validation/extended-const.wast

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111
;; EXTENDED: (global.get $gimport$0)
1212
;; EXTENDED: (i32.const 42)
1313
;; EXTENDED: ))
14-
;; EXTENDED: (data $0 (i32.sub
14+
;; EXTENDED: (data $0 (offset (i32.sub
1515
;; EXTENDED: (global.get $gimport$0)
1616
;; EXTENDED: (i32.const 10)
17-
;; EXTENDED: ) "hello world")
17+
;; EXTENDED: )) "hello world")
18+
;; EXTENDED: (elem $0 (offset (i32.sub
19+
;; EXTENDED: (global.get $gimport$0)
20+
;; EXTENDED: (i32.const 10)
21+
;; EXTENDED: )))
1822

1923
(module
2024
(import "env" "global" (global i32))
2125
(memory 1 1)
26+
(table 1 1 funcref)
2227
(global i32 (i32.add (global.get 0) (i32.const 42)))
2328
(data (offset (i32.sub (global.get 0) (i32.const 10))) "hello world")
29+
(elem (offset (i32.sub (global.get 0) (i32.const 10))) func)
2430
)

0 commit comments

Comments
 (0)