mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 07:23:39 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 28a083633c | |||
| 7e7814a087 |
@@ -142,6 +142,7 @@ var validLinkerFlags = []*regexp.Regexp{
|
|||||||
re(`-L([^@\-].*)`),
|
re(`-L([^@\-].*)`),
|
||||||
re(`-O`),
|
re(`-O`),
|
||||||
re(`-O([^@\-].*)`),
|
re(`-O([^@\-].*)`),
|
||||||
|
re(`--export=(.+)`), // for wasm-ld
|
||||||
re(`-f(no-)?(pic|PIC|pie|PIE)`),
|
re(`-f(no-)?(pic|PIC|pie|PIE)`),
|
||||||
re(`-f(no-)?openmp(-simd)?`),
|
re(`-f(no-)?openmp(-simd)?`),
|
||||||
re(`-fsanitize=([^@\-].*)`),
|
re(`-fsanitize=([^@\-].*)`),
|
||||||
|
|||||||
@@ -1059,11 +1059,12 @@ func (b *builder) createFunctionStart(intrinsic bool) {
|
|||||||
if b.info.section != "" {
|
if b.info.section != "" {
|
||||||
b.llvmFn.SetSection(b.info.section)
|
b.llvmFn.SetSection(b.info.section)
|
||||||
}
|
}
|
||||||
if b.info.exported && strings.HasPrefix(b.Triple, "wasm") {
|
if b.info.exported && b.info.module != "" && strings.HasPrefix(b.Triple, "wasm") {
|
||||||
// Set the exported name. This is necessary for WebAssembly because
|
// Set the exported name. This is necessary for WebAssembly because
|
||||||
// otherwise the function is not exported.
|
// otherwise the function is not exported.
|
||||||
functionAttr := b.ctx.CreateStringAttribute("wasm-export-name", b.info.linkName)
|
b.llvmFn.AddFunctionAttr(b.ctx.CreateStringAttribute("wasm-export-name", b.info.linkName))
|
||||||
b.llvmFn.AddFunctionAttr(functionAttr)
|
// Set the export module.
|
||||||
|
b.llvmFn.AddFunctionAttr(b.ctx.CreateStringAttribute("wasm-export-module", b.info.module))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some functions have a pragma controlling the inlining level.
|
// Some functions have a pragma controlling the inlining level.
|
||||||
|
|||||||
+11
-21
@@ -210,8 +210,9 @@ func (c *compilerContext) getFunction(fn *ssa.Function) llvm.Value {
|
|||||||
// exported.
|
// exported.
|
||||||
func (c *compilerContext) getFunctionInfo(f *ssa.Function) functionInfo {
|
func (c *compilerContext) getFunctionInfo(f *ssa.Function) functionInfo {
|
||||||
info := functionInfo{
|
info := functionInfo{
|
||||||
// Pick the default linkName.
|
module: "env",
|
||||||
linkName: f.RelString(nil),
|
importName: f.Name(),
|
||||||
|
linkName: f.RelString(nil), // pick the default linkName
|
||||||
}
|
}
|
||||||
// Check for //go: pragmas, which may change the link name (among others).
|
// Check for //go: pragmas, which may change the link name (among others).
|
||||||
info.parsePragmas(f)
|
info.parsePragmas(f)
|
||||||
@@ -225,10 +226,6 @@ 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 ") {
|
||||||
@@ -246,7 +243,8 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
importName = parts[1]
|
info.importName = parts[1]
|
||||||
|
info.linkName = parts[1]
|
||||||
info.exported = true
|
info.exported = true
|
||||||
case "//go:interrupt":
|
case "//go:interrupt":
|
||||||
if hasUnsafeImport(f.Pkg.Pkg) {
|
if hasUnsafeImport(f.Pkg.Pkg) {
|
||||||
@@ -254,10 +252,13 @@ func (info *functionInfo) parsePragmas(f *ssa.Function) {
|
|||||||
}
|
}
|
||||||
case "//go:wasm-module":
|
case "//go:wasm-module":
|
||||||
// Alternative comment for setting the import module.
|
// Alternative comment for setting the import module.
|
||||||
if len(parts) != 2 {
|
if len(parts) == 1 {
|
||||||
continue
|
// Function must not be exported outside of the WebAssembly
|
||||||
}
|
// module (but only be made available for linking).
|
||||||
|
info.module = ""
|
||||||
|
} else if len(parts) == 2 {
|
||||||
info.module = parts[1]
|
info.module = parts[1]
|
||||||
|
}
|
||||||
case "//go:inline":
|
case "//go:inline":
|
||||||
info.inline = inlineHint
|
info.inline = inlineHint
|
||||||
case "//go:noinline":
|
case "//go:noinline":
|
||||||
@@ -297,17 +298,6 @@ 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+2
-2
@@ -62,7 +62,7 @@ declare void @main.undefinedFunctionNotInSection(i8*) #0
|
|||||||
|
|
||||||
attributes #0 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
attributes #0 = { "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
||||||
attributes #1 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
attributes #1 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
||||||
attributes #2 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" "wasm-export-name"="extern_func" "wasm-import-module"="env" "wasm-import-name"="extern_func" }
|
attributes #2 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" "wasm-export-module"="env" "wasm-export-name"="extern_func" "wasm-import-module"="env" "wasm-import-name"="extern_func" }
|
||||||
attributes #3 = { inlinehint nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
attributes #3 = { inlinehint nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
||||||
attributes #4 = { noinline nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
attributes #4 = { noinline nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" }
|
||||||
attributes #5 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" "wasm-export-name"="exportedFunctionInSection" "wasm-import-module"="env" "wasm-import-name"="exportedFunctionInSection" }
|
attributes #5 = { nounwind "target-features"="+bulk-memory,+nontrapping-fptoint,+sign-ext" "wasm-export-module"="env" "wasm-export-name"="exportedFunctionInSection" "wasm-import-module"="env" "wasm-import-name"="exportedFunctionInSection" }
|
||||||
|
|||||||
@@ -62,63 +62,57 @@ func growHeap() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The below functions override the default allocator of wasi-libc. This ensures
|
// The below functions override the default allocator of wasi-libc. This ensures
|
||||||
// code linked from other languages can allocate memory on the same heap as the
|
// code linked from other languages can allocate memory without colliding with
|
||||||
// TinyGo heap.
|
// our GC allocations.
|
||||||
|
|
||||||
// Keep track of all the heap allocations while they're in use.
|
var allocs = make(map[uintptr][]byte)
|
||||||
// This is not the most efficient solution but it costs a lot less in code size
|
|
||||||
// compared to a map.
|
|
||||||
var allocs []unsafe.Pointer
|
|
||||||
|
|
||||||
func trackAlloc(ptr unsafe.Pointer) {
|
|
||||||
// Try to find some empty space in the allocs slice.
|
|
||||||
for i, slot := range allocs {
|
|
||||||
if slot == nil {
|
|
||||||
allocs[i] = slot
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Couldn't find this space. Fall back to appending to the end.
|
|
||||||
allocs = append(allocs, ptr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeAlloc(ptr unsafe.Pointer) {
|
|
||||||
// Remove the pointer so it can be garbage collected.
|
|
||||||
for i, slot := range allocs {
|
|
||||||
if ptr == slot {
|
|
||||||
allocs[i] = nil
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//export malloc
|
//export malloc
|
||||||
|
//go:wasm-module
|
||||||
func libc_malloc(size uintptr) unsafe.Pointer {
|
func libc_malloc(size uintptr) unsafe.Pointer {
|
||||||
ptr := alloc(size, nil)
|
buf := make([]byte, size)
|
||||||
trackAlloc(ptr)
|
ptr := unsafe.Pointer(&buf[0])
|
||||||
|
allocs[uintptr(ptr)] = buf
|
||||||
return ptr
|
return ptr
|
||||||
}
|
}
|
||||||
|
|
||||||
//export free
|
//export free
|
||||||
|
//go:wasm-module
|
||||||
func libc_free(ptr unsafe.Pointer) {
|
func libc_free(ptr unsafe.Pointer) {
|
||||||
removeAlloc(ptr)
|
if ptr == nil {
|
||||||
free(ptr)
|
return
|
||||||
|
}
|
||||||
|
if _, ok := allocs[uintptr(ptr)]; ok {
|
||||||
|
delete(allocs, uintptr(ptr))
|
||||||
|
} else {
|
||||||
|
panic("free: invalid pointer")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//export calloc
|
//export calloc
|
||||||
|
//go:wasm-module
|
||||||
func libc_calloc(nmemb, size uintptr) unsafe.Pointer {
|
func libc_calloc(nmemb, size uintptr) unsafe.Pointer {
|
||||||
// Note: we could be even more correct here and check that nmemb * size
|
// No difference between calloc and malloc.
|
||||||
// doesn't overflow. However the current implementation should normally work
|
|
||||||
// fine.
|
|
||||||
return libc_malloc(nmemb * size)
|
return libc_malloc(nmemb * size)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export realloc
|
//export realloc
|
||||||
|
//go:wasm-module
|
||||||
func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
|
func libc_realloc(oldPtr unsafe.Pointer, size uintptr) unsafe.Pointer {
|
||||||
newPtr := realloc(oldPtr, size)
|
// It's hard to optimize this to expand the current buffer with our GC, but
|
||||||
if newPtr != oldPtr {
|
// it is theoretically possible. For now, just always allocate fresh.
|
||||||
removeAlloc(oldPtr)
|
buf := make([]byte, size)
|
||||||
trackAlloc(newPtr)
|
|
||||||
|
if oldPtr != nil {
|
||||||
|
if oldBuf, ok := allocs[uintptr(oldPtr)]; ok {
|
||||||
|
copy(buf, oldBuf)
|
||||||
|
delete(allocs, uintptr(oldPtr))
|
||||||
|
} else {
|
||||||
|
panic("realloc: invalid pointer")
|
||||||
}
|
}
|
||||||
return newPtr
|
}
|
||||||
|
|
||||||
|
ptr := unsafe.Pointer(&buf[0])
|
||||||
|
allocs[uintptr(ptr)] = buf
|
||||||
|
return ptr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ func libc_calloc(nmemb, size uintptr) unsafe.Pointer
|
|||||||
//export realloc
|
//export realloc
|
||||||
func libc_realloc(ptr unsafe.Pointer, size uintptr) unsafe.Pointer
|
func libc_realloc(ptr unsafe.Pointer, size uintptr) unsafe.Pointer
|
||||||
|
|
||||||
func getFilledBuffer_malloc() unsafe.Pointer {
|
func getFilledBuffer_malloc() uintptr {
|
||||||
ptr := libc_malloc(5)
|
ptr := libc_malloc(5)
|
||||||
fillPanda(ptr)
|
fillPanda(ptr)
|
||||||
return ptr
|
return uintptr(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilledBuffer_calloc() unsafe.Pointer {
|
func getFilledBuffer_calloc() uintptr {
|
||||||
ptr := libc_calloc(2, 5)
|
ptr := libc_calloc(2, 5)
|
||||||
fillPanda(ptr)
|
fillPanda(ptr)
|
||||||
*(*byte)(unsafe.Add(ptr, 5)) = 'b'
|
*(*byte)(unsafe.Add(ptr, 5)) = 'b'
|
||||||
@@ -37,23 +37,23 @@ func getFilledBuffer_calloc() unsafe.Pointer {
|
|||||||
*(*byte)(unsafe.Add(ptr, 7)) = 'a'
|
*(*byte)(unsafe.Add(ptr, 7)) = 'a'
|
||||||
*(*byte)(unsafe.Add(ptr, 8)) = 'r'
|
*(*byte)(unsafe.Add(ptr, 8)) = 'r'
|
||||||
*(*byte)(unsafe.Add(ptr, 9)) = 's'
|
*(*byte)(unsafe.Add(ptr, 9)) = 's'
|
||||||
return ptr
|
return uintptr(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilledBuffer_realloc() unsafe.Pointer {
|
func getFilledBuffer_realloc() uintptr {
|
||||||
origPtr := getFilledBuffer_malloc()
|
origPtr := getFilledBuffer_malloc()
|
||||||
ptr := libc_realloc(origPtr, 9)
|
ptr := libc_realloc(unsafe.Pointer(origPtr), 9)
|
||||||
*(*byte)(unsafe.Add(ptr, 5)) = 'b'
|
*(*byte)(unsafe.Add(ptr, 5)) = 'b'
|
||||||
*(*byte)(unsafe.Add(ptr, 6)) = 'e'
|
*(*byte)(unsafe.Add(ptr, 6)) = 'e'
|
||||||
*(*byte)(unsafe.Add(ptr, 7)) = 'a'
|
*(*byte)(unsafe.Add(ptr, 7)) = 'a'
|
||||||
*(*byte)(unsafe.Add(ptr, 8)) = 'r'
|
*(*byte)(unsafe.Add(ptr, 8)) = 'r'
|
||||||
return ptr
|
return uintptr(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFilledBuffer_reallocNil() unsafe.Pointer {
|
func getFilledBuffer_reallocNil() uintptr {
|
||||||
ptr := libc_realloc(nil, 5)
|
ptr := libc_realloc(nil, 5)
|
||||||
fillPanda(ptr)
|
fillPanda(ptr)
|
||||||
return ptr
|
return uintptr(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillPanda(ptr unsafe.Pointer) {
|
func fillPanda(ptr unsafe.Pointer) {
|
||||||
@@ -64,10 +64,10 @@ func fillPanda(ptr unsafe.Pointer) {
|
|||||||
*(*byte)(unsafe.Add(ptr, 4)) = 'a'
|
*(*byte)(unsafe.Add(ptr, 4)) = 'a'
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFilledBuffer(t *testing.T, ptr unsafe.Pointer, content string) {
|
func checkFilledBuffer(t *testing.T, ptr uintptr, content string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
buf := *(*string)(unsafe.Pointer(&reflect.StringHeader{
|
buf := *(*string)(unsafe.Pointer(&reflect.StringHeader{
|
||||||
Data: uintptr(ptr),
|
Data: ptr,
|
||||||
Len: uintptr(len(content)),
|
Len: uintptr(len(content)),
|
||||||
}))
|
}))
|
||||||
if buf != content {
|
if buf != content {
|
||||||
@@ -78,7 +78,7 @@ func checkFilledBuffer(t *testing.T, ptr unsafe.Pointer, content string) {
|
|||||||
func TestMallocFree(t *testing.T) {
|
func TestMallocFree(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
getBuffer func() unsafe.Pointer
|
getBuffer func() uintptr
|
||||||
content string
|
content string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -121,7 +121,7 @@ func TestMallocFree(t *testing.T) {
|
|||||||
|
|
||||||
checkFilledBuffer(t, bufPtr, tt.content)
|
checkFilledBuffer(t, bufPtr, tt.content)
|
||||||
|
|
||||||
libc_free(bufPtr)
|
libc_free(unsafe.Pointer(bufPtr))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user