Index: llvm/trunk/include/llvm/BinaryFormat/Wasm.h =================================================================== --- llvm/trunk/include/llvm/BinaryFormat/Wasm.h +++ llvm/trunk/include/llvm/BinaryFormat/Wasm.h @@ -65,10 +65,14 @@ } Value; }; -struct WasmGlobal { - uint32_t Index; +struct WasmGlobalType { int32_t Type; bool Mutable; +}; + +struct WasmGlobal { + uint32_t Index; + WasmGlobalType Type; WasmInitExpr InitExpr; }; @@ -78,7 +82,7 @@ uint32_t Kind; union { uint32_t SigIndex; - WasmGlobal Global; + WasmGlobalType Global; WasmTable Table; WasmLimits Memory; }; Index: llvm/trunk/include/llvm/Object/Wasm.h =================================================================== --- llvm/trunk/include/llvm/Object/Wasm.h +++ llvm/trunk/include/llvm/Object/Wasm.h @@ -67,21 +67,26 @@ AltIndex = Index; } - bool isFunction() const { + bool isTypeFunction() const { return Type == WasmSymbol::SymbolType::FUNCTION_IMPORT || Type == WasmSymbol::SymbolType::FUNCTION_EXPORT; } + bool isTypeGlobal() const { + return Type == SymbolType::GLOBAL_IMPORT || + Type == SymbolType::GLOBAL_EXPORT; + } + - bool isWeak() const { + bool isBindingWeak() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK; } - bool isGlobal() const { + bool isBindingGlobal() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL; } - bool isLocal() const { + bool isBindingLocal() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL; } Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/lib/MC/WasmObjectWriter.cpp @@ -135,11 +135,8 @@ // A wasm global to be written into the global section. struct WasmGlobal { - wasm::ValType Type; - bool IsMutable; - bool HasImport; + wasm::WasmGlobalType Type; uint64_t InitialValue; - uint32_t ImportIndex; }; // Information about a single item which is part of a COMDAT. For each data @@ -728,18 +725,11 @@ encodeULEB128(Globals.size(), getStream()); for (const WasmGlobal &Global : Globals) { - writeValueType(Global.Type); - write8(Global.IsMutable); + writeValueType(static_cast(Global.Type.Type)); + write8(Global.Type.Mutable); - if (Global.HasImport) { - assert(Global.InitialValue == 0); - write8(wasm::WASM_OPCODE_GET_GLOBAL); - encodeULEB128(Global.ImportIndex, getStream()); - } else { - assert(Global.ImportIndex == 0); - write8(wasm::WASM_OPCODE_I32_CONST); - encodeSLEB128(Global.InitialValue, getStream()); // offset - } + write8(wasm::WASM_OPCODE_I32_CONST); + encodeSLEB128(Global.InitialValue, getStream()); write8(wasm::WASM_OPCODE_END); } @@ -968,7 +958,7 @@ const MCAsmLayout &Layout) { DEBUG(dbgs() << "WasmObjectWriter::writeObject\n"); MCContext &Ctx = Asm.getContext(); - wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32; + int32_t PtrType = is64Bit() ? wasm::WASM_TYPE_I64 : wasm::WASM_TYPE_I32; // Collect information from the available symbols. SmallVector Functions; @@ -981,64 +971,6 @@ SmallVector DataSegments; uint32_t DataSize = 0; - // In the special .global_variables section, we've encoded global - // variables used by the function. Translate them into the Globals - // list. - MCSectionWasm *GlobalVars = - Ctx.getWasmSection(".global_variables", SectionKind::getMetadata()); - if (!GlobalVars->getFragmentList().empty()) { - if (GlobalVars->getFragmentList().size() != 1) - report_fatal_error("only one .global_variables fragment supported"); - const MCFragment &Frag = *GlobalVars->begin(); - if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) - report_fatal_error("only data supported in .global_variables"); - const auto &DataFrag = cast(Frag); - if (!DataFrag.getFixups().empty()) - report_fatal_error("fixups not supported in .global_variables"); - const SmallVectorImpl &Contents = DataFrag.getContents(); - for (const uint8_t *p = (const uint8_t *)Contents.data(), - *end = (const uint8_t *)Contents.data() + Contents.size(); - p != end; ) { - WasmGlobal G; - if (end - p < 3) - report_fatal_error("truncated global variable encoding"); - G.Type = wasm::ValType(int8_t(*p++)); - G.IsMutable = bool(*p++); - G.HasImport = bool(*p++); - if (G.HasImport) { - G.InitialValue = 0; - - WasmImport Import; - Import.ModuleName = (const char *)p; - const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p); - if (!nul) - report_fatal_error("global module name must be nul-terminated"); - p = nul + 1; - nul = (const uint8_t *)memchr(p, '\0', end - p); - if (!nul) - report_fatal_error("global base name must be nul-terminated"); - Import.FieldName = (const char *)p; - p = nul + 1; - - Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; - Import.Type = int32_t(G.Type); - - G.ImportIndex = NumGlobalImports; - ++NumGlobalImports; - - Imports.push_back(Import); - } else { - unsigned n; - G.InitialValue = decodeSLEB128(p, &n); - G.ImportIndex = 0; - if ((ptrdiff_t)n > end - p) - report_fatal_error("global initial value must be valid SLEB128"); - p += n; - } - Globals.push_back(G); - } - } - // For now, always emit the memory import, since loads and stores are not // valid without it. In the future, we could perhaps be more clever and omit // it if there are no loads or stores. @@ -1088,7 +1020,7 @@ ++NumFunctionImports; } else { Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; - Import.Type = int32_t(PtrType); + Import.Type = PtrType; Import.IsMutable = false; SymbolIndices[&WS] = NumGlobalImports; @@ -1206,11 +1138,9 @@ assert(DataSection.isWasmData()); WasmGlobal Global; - Global.Type = PtrType; - Global.IsMutable = false; - Global.HasImport = false; + Global.Type.Type = PtrType; + Global.Type.Mutable = false; Global.InitialValue = DataSection.getMemoryOffset() + Layout.getSymbolOffset(WS); - Global.ImportIndex = 0; SymbolIndices[&WS] = Index; DEBUG(dbgs() << " -> global index: " << Index << "\n"); Globals.push_back(Global); Index: llvm/trunk/lib/Object/WasmObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/WasmObjectFile.cpp +++ llvm/trunk/lib/Object/WasmObjectFile.cpp @@ -713,8 +713,8 @@ while (Count--) { wasm::WasmGlobal Global; Global.Index = NumImportedGlobals + Globals.size(); - Global.Type = readVarint7(Ptr); - Global.Mutable = readVaruint1(Ptr); + Global.Type.Type = readVarint7(Ptr); + Global.Type.Mutable = readVaruint1(Ptr); if (Error Err = readInitExpr(Global.InitExpr, Ptr)) return Err; Globals.push_back(Global); @@ -883,9 +883,9 @@ const WasmSymbol &Sym = getWasmSymbol(Symb); DEBUG(dbgs() << "getSymbolFlags: ptr=" << &Sym << " " << Sym << "\n"); - if (Sym.isWeak()) + if (Sym.isBindingWeak()) Result |= SymbolRef::SF_Weak; - if (!Sym.isLocal()) + if (!Sym.isBindingLocal()) Result |= SymbolRef::SF_Global; if (Sym.isHidden()) Result |= SymbolRef::SF_Hidden; Index: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h =================================================================== --- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h +++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.h @@ -37,8 +37,6 @@ virtual void emitResult(MCSymbol *Symbol, ArrayRef Types) = 0; /// .local virtual void emitLocal(ArrayRef Types) = 0; - /// .globalvar - virtual void emitGlobal(ArrayRef Globals) = 0; /// .endfunc virtual void emitEndFunc() = 0; /// .functype @@ -64,7 +62,6 @@ void emitParam(MCSymbol *Symbol, ArrayRef Types) override; void emitResult(MCSymbol *Symbol, ArrayRef Types) override; void emitLocal(ArrayRef Types) override; - void emitGlobal(ArrayRef Globals) override; void emitEndFunc() override; void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl &Params, @@ -81,7 +78,6 @@ void emitParam(MCSymbol *Symbol, ArrayRef Types) override; void emitResult(MCSymbol *Symbol, ArrayRef Types) override; void emitLocal(ArrayRef Types) override; - void emitGlobal(ArrayRef Globals) override; void emitEndFunc() override; void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl &Params, @@ -98,7 +94,6 @@ void emitParam(MCSymbol *Symbol, ArrayRef Types) override; void emitResult(MCSymbol *Symbol, ArrayRef Types) override; void emitLocal(ArrayRef Types) override; - void emitGlobal(ArrayRef Globals) override; void emitEndFunc() override; void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl &Params, Index: llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp +++ llvm/trunk/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyTargetStreamer.cpp @@ -87,27 +87,6 @@ } } -void WebAssemblyTargetAsmStreamer::emitGlobal( - ArrayRef Globals) { - if (!Globals.empty()) { - OS << "\t.globalvar \t"; - - bool First = true; - for (const wasm::Global &G : Globals) { - if (First) - First = false; - else - OS << ", "; - OS << WebAssembly::TypeToString(G.Type); - if (!G.InitialModule.empty()) - OS << '=' << G.InitialModule << ':' << G.InitialName; - else - OS << '=' << G.InitialValue; - } - OS << '\n'; - } -} - void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; } void WebAssemblyTargetAsmStreamer::emitIndirectFunctionType( @@ -148,11 +127,6 @@ emitValueType(WebAssembly::toValType(Type)); } -void WebAssemblyTargetELFStreamer::emitGlobal( - ArrayRef Globals) { - llvm_unreachable(".globalvar encoding not yet implemented"); -} - void WebAssemblyTargetELFStreamer::emitEndFunc() { Streamer.EmitIntValue(WebAssembly::End, 1); } @@ -204,31 +178,6 @@ } } -void WebAssemblyTargetWasmStreamer::emitGlobal( - ArrayRef Globals) { - // Encode the globals use by the funciton into the special .global_variables - // section. This will later be decoded and turned into contents for the - // Globals Section. - Streamer.PushSection(); - Streamer.SwitchSection(Streamer.getContext().getWasmSection( - ".global_variables", SectionKind::getMetadata())); - for (const wasm::Global &G : Globals) { - Streamer.EmitIntValue(int32_t(G.Type), 1); - Streamer.EmitIntValue(G.Mutable, 1); - if (G.InitialModule.empty()) { - Streamer.EmitIntValue(0, 1); // indicate that we have an int value - Streamer.EmitSLEB128IntValue(0); - } else { - Streamer.EmitIntValue(1, 1); // indicate that we have a module import - Streamer.EmitBytes(G.InitialModule); - Streamer.EmitIntValue(0, 1); // nul-terminate - Streamer.EmitBytes(G.InitialName); - Streamer.EmitIntValue(0, 1); // nul-terminate - } - } - Streamer.PopSection(); -} - void WebAssemblyTargetWasmStreamer::emitEndFunc() { llvm_unreachable(".end_func is not needed for direct wasm output"); } Index: llvm/trunk/tools/obj2yaml/wasm2yaml.cpp =================================================================== --- llvm/trunk/tools/obj2yaml/wasm2yaml.cpp +++ llvm/trunk/tools/obj2yaml/wasm2yaml.cpp @@ -201,8 +201,8 @@ for (auto &Global : Obj.globals()) { WasmYAML::Global G; G.Index = Global.Index; - G.Type = Global.Type; - G.Mutable = Global.Mutable; + G.Type = Global.Type.Type; + G.Mutable = Global.Type.Mutable; G.InitExpr = Global.InitExpr; GlobalSec->Globals.push_back(G); }