Namespaced Wasm Imports so they don't conflict across modules, or reserved LLVM IR (#1661)
wasm: namespaced all of the wasm import functions
Этот коммит содержится в:
родитель
6862942c99
коммит
dcff8b6478
1 изменённых файлов: 32 добавлений и 7 удалений
|
@ -21,12 +21,13 @@ import (
|
||||||
// The linkName value contains a valid link name, even if //go:linkname is not
|
// The linkName value contains a valid link name, even if //go:linkname is not
|
||||||
// present.
|
// present.
|
||||||
type functionInfo struct {
|
type functionInfo struct {
|
||||||
module string // go:wasm-module
|
module string // go:wasm-module
|
||||||
linkName string // go:linkname, go:export
|
importName string // go:linkname, go:export - The name the developer assigns
|
||||||
exported bool // go:export, CGo
|
linkName string // go:linkname, go:export - The name that we map for the particular module -> importName
|
||||||
nobounds bool // go:nobounds
|
exported bool // go:export, CGo
|
||||||
variadic bool // go:variadic (CGo only)
|
nobounds bool // go:nobounds
|
||||||
inline inlineType // go:inline
|
variadic bool // go:variadic (CGo only)
|
||||||
|
inline inlineType // go:inline
|
||||||
}
|
}
|
||||||
|
|
||||||
type inlineType int
|
type inlineType int
|
||||||
|
@ -151,8 +152,16 @@ func (c *compilerContext) getFunction(fn *ssa.Function) llvm.Value {
|
||||||
if info.exported {
|
if info.exported {
|
||||||
// Set the wasm-import-module attribute if the function's module is set.
|
// Set the wasm-import-module attribute if the function's module is set.
|
||||||
if info.module != "" {
|
if info.module != "" {
|
||||||
|
|
||||||
|
// We need to add the wasm-import-module and the wasm-import-name
|
||||||
wasmImportModuleAttr := c.ctx.CreateStringAttribute("wasm-import-module", info.module)
|
wasmImportModuleAttr := c.ctx.CreateStringAttribute("wasm-import-module", info.module)
|
||||||
llvmFn.AddFunctionAttr(wasmImportModuleAttr)
|
llvmFn.AddFunctionAttr(wasmImportModuleAttr)
|
||||||
|
|
||||||
|
// Add the Wasm Import Name, if we are a named wasm import
|
||||||
|
if info.importName != "" {
|
||||||
|
wasmImportNameAttr := c.ctx.CreateStringAttribute("wasm-import-name", info.importName)
|
||||||
|
llvmFn.AddFunctionAttr(wasmImportNameAttr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
nocaptureKind := llvm.AttributeKindID("nocapture")
|
nocaptureKind := llvm.AttributeKindID("nocapture")
|
||||||
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
|
nocapture := c.ctx.CreateEnumAttribute(nocaptureKind, 0)
|
||||||
|
@ -191,6 +200,10 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if decl, ok := f.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil {
|
if decl, ok := f.Syntax().(*ast.FuncDecl); ok && decl.Doc != nil {
|
||||||
|
|
||||||
|
// Our importName for a wasm module (if we are compiling to wasm), or llvm link name
|
||||||
|
var importName string
|
||||||
|
|
||||||
for _, comment := range decl.Doc.List {
|
for _, comment := range decl.Doc.List {
|
||||||
text := comment.Text
|
text := comment.Text
|
||||||
if strings.HasPrefix(text, "//export ") {
|
if strings.HasPrefix(text, "//export ") {
|
||||||
|
@ -207,7 +220,8 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
info.linkName = parts[1]
|
|
||||||
|
importName = parts[1]
|
||||||
info.exported = true
|
info.exported = true
|
||||||
case "//go:wasm-module":
|
case "//go:wasm-module":
|
||||||
// Alternative comment for setting the import module.
|
// Alternative comment for setting the import module.
|
||||||
|
@ -250,6 +264,17 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the importName for our exported function if we have one
|
||||||
|
if importName != "" {
|
||||||
|
if info.module == "" {
|
||||||
|
info.linkName = importName
|
||||||
|
} else {
|
||||||
|
// WebAssembly import
|
||||||
|
info.importName = importName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче