Merge pull request #292 from xushiwei/q

disable gc: llgo -tags nogc
This commit is contained in:
xushiwei
2024-06-08 23:12:34 +08:00
committed by GitHub
11 changed files with 200 additions and 41 deletions

View File

@@ -185,6 +185,17 @@ Here are some examples related to Go syntax:
* [goroutine](_demo/goroutine/goroutine.go): goroutine demo * [goroutine](_demo/goroutine/goroutine.go): goroutine demo
### Garbage Collection (GC)
By default, LLGo implements `gc` based on [bdwgc](https://www.hboehm.info/gc/).
However, you can disable gc by specifying the `nogc` tag. For example:
```sh
llgo run -tags nogc .
```
## Go packages support ## Go packages support
Here are the Go packages that can be imported correctly: Here are the Go packages that can be imported correctly:

View File

@@ -1,6 +1,22 @@
//go:build !linux //go:build !linux
// +build !linux // +build !linux
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c package c
import _ "unsafe" import _ "unsafe"

View File

@@ -1,6 +1,22 @@
//go:build linux //go:build linux
// +build linux // +build linux
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c package c
import _ "unsafe" import _ "unsafe"

View File

@@ -1,3 +1,19 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bdwgc package bdwgc
import ( import (

View File

@@ -1,6 +1,22 @@
//go:build !linux //go:build !linux
// +build !linux // +build !linux
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c package c
import _ "unsafe" import _ "unsafe"

View File

@@ -1,6 +1,22 @@
//go:build linux //go:build linux
// +build linux // +build linux
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package c package c
import _ "unsafe" import _ "unsafe"

View File

@@ -1,27 +0,0 @@
package runtime
type errorString string
func (e errorString) RuntimeError() {}
func (e errorString) Error() string {
return "runtime error: " + string(e)
}
func AssertRuntimeError(b bool, msg string) {
if b {
panic(errorString(msg).Error())
}
}
func AssertNegativeShift(b bool) {
if b {
panic(errorString("negative shift amount").Error())
}
}
func AssertIndexRange(b bool) {
if b {
panic(errorString("index out of range").Error())
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package runtime
type errorString string
func (e errorString) RuntimeError() {}
func (e errorString) Error() string {
return "runtime error: " + string(e)
}
func AssertRuntimeError(b bool, msg string) {
if b {
panic(errorString(msg).Error())
}
}
func AssertNegativeShift(b bool) {
if b {
panic(errorString("negative shift amount").Error())
}
}
func AssertIndexRange(b bool) {
if b {
panic(errorString("index out of range").Error())
}
}

38
internal/runtime/z_gc.go Normal file
View File

@@ -0,0 +1,38 @@
//go:build !nogc
// +build !nogc
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package runtime
import (
"unsafe"
"github.com/goplus/llgo/internal/runtime/bdwgc"
"github.com/goplus/llgo/internal/runtime/c"
)
// AllocU allocates uninitialized memory.
func AllocU(size uintptr) unsafe.Pointer {
return bdwgc.Malloc(size)
}
// AllocZ allocates zero-initialized memory.
func AllocZ(size uintptr) unsafe.Pointer {
ret := bdwgc.Malloc(size)
return c.Memset(ret, 0, size)
}

View File

@@ -1,3 +1,6 @@
//go:build nogc
// +build nogc
/* /*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
* *
@@ -16,9 +19,19 @@
package runtime package runtime
// Defer presents defer statements in a function. import (
type Defer struct { "unsafe"
Bits uintptr
Link *Defer "github.com/goplus/llgo/internal/runtime/c"
Rund int // index of RunDefers )
// AllocU allocates uninitialized memory.
func AllocU(size uintptr) unsafe.Pointer {
return c.Malloc(size)
}
// AllocZ allocates zero-initialized memory.
func AllocZ(size uintptr) unsafe.Pointer {
ret := c.Malloc(size)
return c.Memset(ret, 0, size)
} }

View File

@@ -20,20 +20,19 @@ import (
"unsafe" "unsafe"
"github.com/goplus/llgo/internal/abi" "github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime/bdwgc"
"github.com/goplus/llgo/internal/runtime/c" "github.com/goplus/llgo/internal/runtime/c"
) )
// AllocU allocates uninitialized memory. // -----------------------------------------------------------------------------
func AllocU(size uintptr) unsafe.Pointer {
return bdwgc.Malloc(size) // Defer presents defer statements in a function.
type Defer struct {
Bits uintptr
Link *Defer
Rund int // index of RunDefers
} }
// AllocZ allocates zero-initialized memory. // -----------------------------------------------------------------------------
func AllocZ(size uintptr) unsafe.Pointer {
ret := bdwgc.Malloc(size)
return c.Memset(ret, 0, size)
}
// Zeroinit initializes memory to zero. // Zeroinit initializes memory to zero.
func Zeroinit(p unsafe.Pointer, size uintptr) unsafe.Pointer { func Zeroinit(p unsafe.Pointer, size uintptr) unsafe.Pointer {
@@ -54,3 +53,5 @@ func stringTracef(fp c.FilePtr, format *c.Char, s String) {
cs := c.Alloca(uintptr(s.len) + 1) cs := c.Alloca(uintptr(s.len) + 1)
c.Fprintf(fp, format, CStrCopy(cs, s)) c.Fprintf(fp, format, CStrCopy(cs, s))
} }
// -----------------------------------------------------------------------------