forked from OSchip/llvm-project
[WebAssembly] Add support for externalref to MC and wasm-ld
This allows code for handling externref values to be processed by the assembler and linker. Differential Revision: https://reviews.llvm.org/D81977
This commit is contained in:
parent
a9c478acd0
commit
79aad89d8d
|
@ -0,0 +1,44 @@
|
|||
# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o %t.o %s
|
||||
# RUN: wasm-ld %t.o -o %t.wasm
|
||||
# RUN: obj2yaml %t.wasm | FileCheck %s
|
||||
|
||||
# Tests use of externref type, including storing it in a global
|
||||
|
||||
.globaltype my_global, externref
|
||||
|
||||
.globl read_externref
|
||||
read_externref:
|
||||
.functype read_externref () -> (externref)
|
||||
global.get my_global
|
||||
end_function
|
||||
|
||||
.globl write_externref
|
||||
write_externref:
|
||||
.functype write_externref (externref) -> ()
|
||||
local.get 0
|
||||
global.set my_global
|
||||
end_function
|
||||
|
||||
my_global:
|
||||
|
||||
.globl _start
|
||||
_start:
|
||||
.functype _start () -> ()
|
||||
call read_externref
|
||||
call write_externref
|
||||
end_function
|
||||
|
||||
# CHECK: - Type: GLOBAL
|
||||
# CHECK-NEXT: Globals:
|
||||
# CHECK-NEXT: - Index: 0
|
||||
# CHECK-NEXT: Type: I32
|
||||
# CHECK-NEXT: Mutable: true
|
||||
# CHECK-NEXT: InitExpr:
|
||||
# CHECK-NEXT: Opcode: I32_CONST
|
||||
# CHECK-NEXT: Value: 66560
|
||||
# CHECK-NEXT: - Index: 1
|
||||
# CHECK-NEXT: Type: EXTERNREF
|
||||
# CHECK-NEXT: Mutable: true
|
||||
# CHECK-NEXT: InitExpr:
|
||||
# CHECK-NEXT: Opcode: REF_NULL
|
||||
# CHECK-NEXT: Type: EXTERNREF
|
|
@ -32,6 +32,8 @@ std::string toString(ValType type) {
|
|||
return "v128";
|
||||
case ValType::EXNREF:
|
||||
return "exnref";
|
||||
case ValType::EXTERNREF:
|
||||
return "externref";
|
||||
}
|
||||
llvm_unreachable("Invalid wasm::ValType");
|
||||
}
|
||||
|
@ -155,6 +157,9 @@ void writeInitExpr(raw_ostream &os, const WasmInitExpr &initExpr) {
|
|||
case WASM_OPCODE_GLOBAL_GET:
|
||||
writeUleb128(os, initExpr.Value.Global, "literal (global index)");
|
||||
break;
|
||||
case WASM_OPCODE_REF_NULL:
|
||||
writeValueType(os, ValType::EXTERNREF, "literal (externref type)");
|
||||
break;
|
||||
default:
|
||||
fatal("unknown opcode in init expr: " + Twine(initExpr.Opcode));
|
||||
}
|
||||
|
|
|
@ -232,6 +232,7 @@ enum : unsigned {
|
|||
WASM_TYPE_V128 = 0x7B,
|
||||
WASM_TYPE_FUNCREF = 0x70,
|
||||
WASM_TYPE_EXNREF = 0x68,
|
||||
WASM_TYPE_EXTERNREF = 0x6F,
|
||||
WASM_TYPE_FUNC = 0x60,
|
||||
WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
|
||||
};
|
||||
|
@ -258,6 +259,7 @@ enum : unsigned {
|
|||
WASM_OPCODE_F32_CONST = 0x43,
|
||||
WASM_OPCODE_F64_CONST = 0x44,
|
||||
WASM_OPCODE_I32_ADD = 0x6a,
|
||||
WASM_OPCODE_REF_NULL = 0xd0,
|
||||
};
|
||||
|
||||
// Opcodes used in synthetic functions.
|
||||
|
@ -355,6 +357,7 @@ enum class ValType {
|
|||
F64 = WASM_TYPE_F64,
|
||||
V128 = WASM_TYPE_V128,
|
||||
EXNREF = WASM_TYPE_EXNREF,
|
||||
EXTERNREF = WASM_TYPE_EXTERNREF,
|
||||
};
|
||||
|
||||
struct WasmSignature {
|
||||
|
|
|
@ -833,6 +833,9 @@ void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) {
|
|||
case wasm::WASM_TYPE_F64:
|
||||
writeI64(0);
|
||||
break;
|
||||
case wasm::WASM_TYPE_EXTERNREF:
|
||||
writeValueType(wasm::ValType::EXTERNREF);
|
||||
break;
|
||||
default:
|
||||
llvm_unreachable("unexpected type");
|
||||
}
|
||||
|
@ -1462,6 +1465,9 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
|
|||
case wasm::WASM_TYPE_F64:
|
||||
Global.InitExpr.Opcode = wasm::WASM_OPCODE_F64_CONST;
|
||||
break;
|
||||
case wasm::WASM_TYPE_EXTERNREF:
|
||||
Global.InitExpr.Opcode = wasm::WASM_OPCODE_REF_NULL;
|
||||
break;
|
||||
default:
|
||||
llvm_unreachable("unexpected type");
|
||||
}
|
||||
|
|
|
@ -184,6 +184,14 @@ static Error readInitExpr(wasm::WasmInitExpr &Expr,
|
|||
case wasm::WASM_OPCODE_GLOBAL_GET:
|
||||
Expr.Value.Global = readULEB128(Ctx);
|
||||
break;
|
||||
case wasm::WASM_OPCODE_REF_NULL: {
|
||||
wasm::ValType Ty = static_cast<wasm::ValType>(readULEB128(Ctx));
|
||||
if (Ty != wasm::ValType::EXTERNREF) {
|
||||
return make_error<GenericBinaryError>("Invalid type for ref.null",
|
||||
object_error::parse_failed);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return make_error<GenericBinaryError>("Invalid opcode in init_expr",
|
||||
object_error::parse_failed);
|
||||
|
|
|
@ -433,6 +433,11 @@ void MappingTraits<wasm::WasmInitExpr>::mapping(IO &IO,
|
|||
case wasm::WASM_OPCODE_GLOBAL_GET:
|
||||
IO.mapRequired("Index", Expr.Value.Global);
|
||||
break;
|
||||
case wasm::WASM_OPCODE_REF_NULL: {
|
||||
WasmYAML::ValueType Ty = wasm::WASM_TYPE_EXTERNREF;
|
||||
IO.mapRequired("Type", Ty);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -559,6 +564,8 @@ void ScalarEnumerationTraits<WasmYAML::ValueType>::enumeration(
|
|||
ECase(F64);
|
||||
ECase(V128);
|
||||
ECase(FUNCREF);
|
||||
ECase(EXNREF);
|
||||
ECase(EXTERNREF);
|
||||
ECase(FUNC);
|
||||
#undef ECase
|
||||
}
|
||||
|
@ -583,6 +590,7 @@ void ScalarEnumerationTraits<WasmYAML::Opcode>::enumeration(
|
|||
ECase(F64_CONST);
|
||||
ECase(F32_CONST);
|
||||
ECase(GLOBAL_GET);
|
||||
ECase(REF_NULL);
|
||||
#undef ECase
|
||||
}
|
||||
|
||||
|
|
|
@ -322,6 +322,8 @@ public:
|
|||
return wasm::ValType::V128;
|
||||
if (Type == "exnref")
|
||||
return wasm::ValType::EXNREF;
|
||||
if (Type == "externref")
|
||||
return wasm::ValType::EXTERNREF;
|
||||
return Optional<wasm::ValType>();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj < %s | obj2yaml | FileCheck %s
|
||||
|
||||
# Tests use of externref type, including storing it in a global
|
||||
|
||||
.globaltype my_global, externref
|
||||
|
||||
.globl read_externref
|
||||
read_externref:
|
||||
.functype read_externref () -> (externref)
|
||||
global.get my_global
|
||||
end_function
|
||||
|
||||
.globl write_externref
|
||||
write_externref:
|
||||
.functype write_externref (externref) -> ()
|
||||
local.get 0
|
||||
global.set my_global
|
||||
end_function
|
||||
|
||||
.globl call_with_ref
|
||||
call_with_ref:
|
||||
.functype call_with_ref () -> ()
|
||||
call read_externref
|
||||
call write_externref
|
||||
end_function
|
||||
|
||||
my_global:
|
||||
|
||||
# CHECK: - Type: GLOBAL
|
||||
# CHECK-NEXT: Globals:
|
||||
# CHECK-NEXT: - Index: 0
|
||||
# CHECK-NEXT: Type: EXTERNREF
|
||||
# CHECK-NEXT: Mutable: true
|
||||
# CHECK-NEXT: InitExpr:
|
||||
# CHECK-NEXT: Opcode: REF_NULL
|
||||
# CHECK-NEXT: Type: EXTERNREF
|
Loading…
Reference in New Issue