Skip to content

Commit 793863a

Browse files
committed
import kinds
1 parent 2e2e024 commit 793863a

8 files changed

Lines changed: 84 additions & 28 deletions

File tree

src/asm2wasm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) {
524524
allocateGlobal(name, type, true, import->module, import->base);
525525
delete import;
526526
} else {
527+
import->kind = Import::Function;
527528
wasm.addImport(import);
528529
}
529530
};
@@ -1087,6 +1088,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
10871088
import->module = ASM2WASM;
10881089
import->base = F64_REM;
10891090
import->type = ensureFunctionType("ddd", &wasm);
1091+
import->kind = Import::Function;
10901092
wasm.addImport(import);
10911093
}
10921094
return call;
@@ -1112,6 +1114,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
11121114
import->module = ASM2WASM;
11131115
import->base = call->target;
11141116
import->type = ensureFunctionType("iii", &wasm);
1117+
import->kind = Import::Function;
11151118
wasm.addImport(import);
11161119
}
11171120
return call;
@@ -1150,6 +1153,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
11501153
import->module = ASM2WASM;
11511154
import->base = DEBUGGER;
11521155
import->type = ensureFunctionType("v", &wasm);
1156+
import->kind = Import::Function;
11531157
wasm.addImport(import);
11541158
}
11551159
return call;
@@ -1262,6 +1266,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
12621266
import->module = ASM2WASM;
12631267
import->base = F64_TO_INT;
12641268
import->type = ensureFunctionType("id", &wasm);
1269+
import->kind = Import::Function;
12651270
wasm.addImport(import);
12661271
}
12671272
return ret;

src/binaryen-c.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ BinaryenImportRef BinaryenAddImport(BinaryenModuleRef module, const char* intern
718718
ret->module = externalModuleName;
719719
ret->base = externalBaseName;
720720
ret->type = (FunctionType*)type;
721+
ret->kind = Import::Function;
721722
wasm->addImport(ret);
722723
return ret;
723724
}

src/binaryen-c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void BinaryenModuleOptimize(BinaryenModuleRef module);
373373
void BinaryenModuleAutoDrop(BinaryenModuleRef module);
374374

375375
// Serialize a module into binary form.
376-
// @return how many bytes were written. This will be less than or equal to bufferSize
376+
// @return how many bytes were written. This will be less than or equal to outputSize
377377
size_t BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize);
378378

379379
// Deserialize a module from binary form.

src/passes/Print.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
534534
}
535535
void visitImport(Import *curr) {
536536
printOpening(o, "import ");
537+
switch (curr->kind) {
538+
case Export::Function: break;
539+
case Export::Table: o << "table "; break;
540+
case Export::Memory: o << "memory "; break;
541+
case Export::Global: o << "global "; break;
542+
default: WASM_UNREACHABLE();
543+
}
537544
printName(curr->name) << ' ';
538545
printText(o, curr->module.str) << ' ';
539546
printText(o, curr->base.str);

src/wasm-binary.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,14 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
572572
o << U32LEB(wasm->imports.size());
573573
for (auto& import : wasm->imports) {
574574
if (debug) std::cerr << "write one" << std::endl;
575-
o << U32LEB(getFunctionTypeIndex(import->type->name));
575+
o << U32LEB(import->kind);
576+
switch (import->kind) {
577+
case Export::Function: o << U32LEB(getFunctionTypeIndex(import->type->name));
578+
case Export::Table: break;
579+
case Export::Memory: break;
580+
case Export::Global: break;
581+
default: WASM_UNREACHABLE();
582+
}
576583
writeInlineString(import->module.str);
577584
writeInlineString(import->base.str);
578585
}
@@ -1492,10 +1499,20 @@ class WasmBinaryBuilder {
14921499
if (debug) std::cerr << "read one" << std::endl;
14931500
auto curr = new Import;
14941501
curr->name = Name(std::string("import$") + std::to_string(i));
1495-
auto index = getU32LEB();
1496-
assert(index < wasm.functionTypes.size());
1497-
curr->type = wasm.getFunctionType(index);
1498-
assert(curr->type->name.is());
1502+
curr->kind = (Import::Kind)getU32LEB();
1503+
switch (curr->kind) {
1504+
case Export::Function: {
1505+
auto index = getU32LEB();
1506+
assert(index < wasm.functionTypes.size());
1507+
curr->type = wasm.getFunctionType(index);
1508+
assert(curr->type->name.is());
1509+
break;
1510+
}
1511+
case Export::Table: break;
1512+
case Export::Memory: break;
1513+
case Export::Global: break;
1514+
default: WASM_UNREACHABLE();
1515+
}
14991516
curr->module = getInlineString();
15001517
curr->base = getInlineString();
15011518
wasm.addImport(curr);

src/wasm-linker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void Linker::ensureImport(Name target, std::string signature) {
5050
import->name = import->base = target;
5151
import->module = ENV;
5252
import->type = ensureFunctionType(signature, &out.wasm);
53+
import->kind = Import::Function;
5354
out.wasm.addImport(import);
5455
}
5556
}
@@ -339,6 +340,7 @@ void Linker::emscriptenGlue(std::ostream& o) {
339340
import->name = import->base = curr->target;
340341
import->module = ENV;
341342
import->type = ensureFunctionType(getSig(curr), &parent->out.wasm);
343+
import->kind = Import::Function;
342344
parent->out.wasm.addImport(import);
343345
}
344346
}

src/wasm-s-parser.h

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,33 +1440,49 @@ class SExpressionWasmBuilder {
14401440
im->name = Name::fromInt(importCounter);
14411441
}
14421442
importCounter++;
1443+
if (!s[i]->quoted()) {
1444+
if (s[i]->str() == MEMORY) {
1445+
im->kind = Import::Memory;
1446+
} else if (s[2]->str() == TABLE) {
1447+
im->kind = Import::Table;
1448+
} else if (s[2]->str() == GLOBAL) {
1449+
im->kind = Import::Table;
1450+
} else {
1451+
WASM_UNREACHABLE();
1452+
}
1453+
i++;
1454+
} else {
1455+
im->kind = Import::Function;
1456+
}
14431457
im->module = s[i++]->str();
14441458
if (!s[i]->isStr()) throw ParseException("no name for import");
14451459
im->base = s[i++]->str();
1446-
std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
1447-
if (s.size() > i) {
1448-
Element& params = *s[i];
1449-
IString id = params[0]->str();
1450-
if (id == PARAM) {
1451-
for (size_t i = 1; i < params.size(); i++) {
1452-
type->params.push_back(stringToWasmType(params[i]->str()));
1460+
if (im->kind == Import::Function) {
1461+
std::unique_ptr<FunctionType> type = make_unique<FunctionType>();
1462+
if (s.size() > i) {
1463+
Element& params = *s[i];
1464+
IString id = params[0]->str();
1465+
if (id == PARAM) {
1466+
for (size_t i = 1; i < params.size(); i++) {
1467+
type->params.push_back(stringToWasmType(params[i]->str()));
1468+
}
1469+
} else if (id == RESULT) {
1470+
type->result = stringToWasmType(params[1]->str());
1471+
} else if (id == TYPE) {
1472+
IString name = params[1]->str();
1473+
if (!wasm.checkFunctionType(name)) throw ParseException("bad function type for import");
1474+
*type = *wasm.getFunctionType(name);
1475+
} else {
1476+
throw ParseException("bad import element");
1477+
}
1478+
if (s.size() > i+1) {
1479+
Element& result = *s[i+1];
1480+
assert(result[0]->str() == RESULT);
1481+
type->result = stringToWasmType(result[1]->str());
14531482
}
1454-
} else if (id == RESULT) {
1455-
type->result = stringToWasmType(params[1]->str());
1456-
} else if (id == TYPE) {
1457-
IString name = params[1]->str();
1458-
if (!wasm.checkFunctionType(name)) throw ParseException("bad function type for import");
1459-
*type = *wasm.getFunctionType(name);
1460-
} else {
1461-
throw ParseException("bad import element");
1462-
}
1463-
if (s.size() > i+1) {
1464-
Element& result = *s[i+1];
1465-
assert(result[0]->str() == RESULT);
1466-
type->result = stringToWasmType(result[1]->str());
14671483
}
1484+
im->type = ensureFunctionType(getSig(type.get()), &wasm);
14681485
}
1469-
im->type = ensureFunctionType(getSig(type.get()), &wasm);
14701486
wasm.addImport(im.release());
14711487
}
14721488

src/wasm.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,10 +1430,18 @@ class Function {
14301430

14311431
class Import {
14321432
public:
1433+
enum Kind {
1434+
Function = 0,
1435+
Table = 1,
1436+
Memory = 2,
1437+
Global = 3,
1438+
};
1439+
14331440
Import() : type(nullptr) {}
14341441

14351442
Name name, module, base; // name = module.base
1436-
FunctionType* type;
1443+
Kind kind;
1444+
FunctionType* type; // for Function imports
14371445
};
14381446

14391447
class Export {

0 commit comments

Comments
 (0)