Skip to content

Commit e3d2716

Browse files
authored
Implement table.copy (#6078)
Helps #5951
1 parent 4fba26a commit e3d2716

29 files changed

+2545
-42
lines changed

scripts/gen-s-parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,9 @@
548548
("table.size", "makeTableSize(s)"),
549549
("table.grow", "makeTableGrow(s)"),
550550
("table.fill", "makeTableFill(s)"),
551+
("table.copy", "makeTableCopy(s)"),
551552
# TODO:
552553
# table.init
553-
# table.fill
554-
# table.copy
555554
#
556555
# exception handling instructions
557556
("try", "makeTry(s)"),

src/gen-s-parser.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,6 +3357,9 @@ switch (buf[0]) {
33573357
switch (buf[1]) {
33583358
case 'a': {
33593359
switch (buf[6]) {
3360+
case 'c':
3361+
if (op == "table.copy"sv) { return makeTableCopy(s); }
3362+
goto parse_error;
33603363
case 'f':
33613364
if (op == "table.fill"sv) { return makeTableFill(s); }
33623365
goto parse_error;
@@ -8619,6 +8622,12 @@ switch (buf[0]) {
86198622
switch (buf[1]) {
86208623
case 'a': {
86218624
switch (buf[6]) {
8625+
case 'c':
8626+
if (op == "table.copy"sv) {
8627+
CHECK_ERR(makeTableCopy(ctx, pos));
8628+
return Ok{};
8629+
}
8630+
goto parse_error;
86228631
case 'f':
86238632
if (op == "table.fill"sv) {
86248633
CHECK_ERR(makeTableFill(ctx, pos));

src/ir/ReFinalize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ void ReFinalize::visitTableSet(TableSet* curr) { curr->finalize(); }
125125
void ReFinalize::visitTableSize(TableSize* curr) { curr->finalize(); }
126126
void ReFinalize::visitTableGrow(TableGrow* curr) { curr->finalize(); }
127127
void ReFinalize::visitTableFill(TableFill* curr) { curr->finalize(); }
128+
void ReFinalize::visitTableCopy(TableCopy* curr) { curr->finalize(); }
128129
void ReFinalize::visitTry(Try* curr) { curr->finalize(); }
129130
void ReFinalize::visitThrow(Throw* curr) { curr->finalize(); }
130131
void ReFinalize::visitRethrow(Rethrow* curr) { curr->finalize(); }

src/ir/cost.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
577577
CostType visitTableFill(TableFill* curr) {
578578
return 6 + visit(curr->dest) + visit(curr->value) + visit(curr->size);
579579
}
580+
CostType visitTableCopy(TableCopy* curr) {
581+
return 6 + visit(curr->dest) + visit(curr->source) + visit(curr->size);
582+
}
580583
CostType visitTry(Try* curr) {
581584
// We assume no exception will be thrown in most cases
582585
return visit(curr->body);

src/ir/effects.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ class EffectAnalyzer {
696696
parent.writesTable = true;
697697
parent.implicitTrap = true;
698698
}
699+
void visitTableCopy(TableCopy* curr) {
700+
parent.readsTable = true;
701+
parent.writesTable = true;
702+
parent.implicitTrap = true;
703+
}
699704
void visitTry(Try* curr) {
700705
if (curr->delegateTarget.is()) {
701706
parent.delegateTargets.insert(curr->delegateTarget);

src/ir/possible-contents.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ struct InfoCollector
662662
void visitTableSize(TableSize* curr) { addRoot(curr); }
663663
void visitTableGrow(TableGrow* curr) { addRoot(curr); }
664664
void visitTableFill(TableFill* curr) { addRoot(curr); }
665+
void visitTableCopy(TableCopy* curr) { addRoot(curr); }
665666

666667
void visitNop(Nop* curr) {}
667668
void visitUnreachable(Unreachable* curr) {}

src/parser/parsers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ template<typename Ctx> Result<> makeTableSet(Ctx&, Index);
118118
template<typename Ctx> Result<> makeTableSize(Ctx&, Index);
119119
template<typename Ctx> Result<> makeTableGrow(Ctx&, Index);
120120
template<typename Ctx> Result<> makeTableFill(Ctx&, Index);
121+
template<typename Ctx> Result<> makeTableCopy(Ctx&, Index);
121122
template<typename Ctx> Result<> makeTry(Ctx&, Index);
122123
template<typename Ctx>
123124
Result<> makeTryOrCatchBody(Ctx&, Index, Type type, bool isTry);
@@ -1264,6 +1265,10 @@ template<typename Ctx> Result<> makeTableFill(Ctx& ctx, Index pos) {
12641265
return ctx.in.err("unimplemented instruction");
12651266
}
12661267

1268+
template<typename Ctx> Result<> makeTableCopy(Ctx& ctx, Index pos) {
1269+
return ctx.in.err("unimplemented instruction");
1270+
}
1271+
12671272
template<typename Ctx> Result<> makeTry(Ctx& ctx, Index pos) {
12681273
return ctx.in.err("unimplemented instruction");
12691274
}

src/passes/Directize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ struct Directize : public Pass {
263263
void visitTableFill(TableFill* curr) {
264264
tablesWithSet.insert(curr->table);
265265
}
266+
void visitTableCopy(TableCopy* curr) {
267+
tablesWithSet.insert(curr->destTable);
268+
}
266269
};
267270

268271
Finder(tablesWithSet).walkFunction(func);

src/passes/Print.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,12 @@ struct PrintExpressionContents
19501950
printMedium(o, "table.fill ");
19511951
printName(curr->table, o);
19521952
}
1953+
void visitTableCopy(TableCopy* curr) {
1954+
printMedium(o, "table.copy ");
1955+
printName(curr->destTable, o);
1956+
o << ' ';
1957+
printName(curr->sourceTable, o);
1958+
}
19531959
void visitTry(Try* curr) {
19541960
printMedium(o, "try");
19551961
if (curr->name.is()) {

src/passes/Unsubtyping.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ struct Unsubtyping
446446
void visitTableFill(TableFill* curr) {
447447
noteSubtype(curr->value->type, getModule()->getTable(curr->table)->type);
448448
}
449+
void visitTableCopy(TableCopy* curr) {
450+
noteSubtype(getModule()->getTable(curr->sourceTable)->type,
451+
getModule()->getTable(curr->destTable)->type);
452+
}
449453
void visitTry(Try* curr) {
450454
noteSubtype(curr->body->type, curr->type);
451455
for (auto* body : curr->catchBodies) {

0 commit comments

Comments
 (0)