diff --git a/README.md b/README.md index cd20a497..d4ed6535 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,17 @@ Here are some examples related to Go syntax: * [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 Here are the Go packages that can be imported correctly: diff --git a/c/c_default.go b/c/c_default.go index cc98cc44..45286fe9 100644 --- a/c/c_default.go +++ b/c/c_default.go @@ -1,6 +1,22 @@ //go: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 import _ "unsafe" diff --git a/c/c_linux.go b/c/c_linux.go index a6a21c39..70cc9bea 100644 --- a/c/c_linux.go +++ b/c/c_linux.go @@ -1,6 +1,22 @@ //go: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 import _ "unsafe" diff --git a/internal/runtime/bdwgc/bdwgc.go b/internal/runtime/bdwgc/bdwgc.go index 907b3217..6b947962 100644 --- a/internal/runtime/bdwgc/bdwgc.go +++ b/internal/runtime/bdwgc/bdwgc.go @@ -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 import ( diff --git a/internal/runtime/c/c_default.go b/internal/runtime/c/c_default.go index cc98cc44..45286fe9 100644 --- a/internal/runtime/c/c_default.go +++ b/internal/runtime/c/c_default.go @@ -1,6 +1,22 @@ //go: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 import _ "unsafe" diff --git a/internal/runtime/c/c_linux.go b/internal/runtime/c/c_linux.go index a6a21c39..70cc9bea 100644 --- a/internal/runtime/c/c_linux.go +++ b/internal/runtime/c/c_linux.go @@ -1,6 +1,22 @@ //go: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 import _ "unsafe" diff --git a/internal/runtime/error.go b/internal/runtime/error.go deleted file mode 100644 index 8f46aff4..00000000 --- a/internal/runtime/error.go +++ /dev/null @@ -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()) - } -} diff --git a/internal/runtime/z_error.go b/internal/runtime/z_error.go new file mode 100644 index 00000000..e08d8f8b --- /dev/null +++ b/internal/runtime/z_error.go @@ -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()) + } +} diff --git a/internal/runtime/z_gc.go b/internal/runtime/z_gc.go new file mode 100644 index 00000000..3eb1c856 --- /dev/null +++ b/internal/runtime/z_gc.go @@ -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) +} diff --git a/internal/runtime/z_defer.go b/internal/runtime/z_nogc.go similarity index 65% rename from internal/runtime/z_defer.go rename to internal/runtime/z_nogc.go index fce4230e..1c591387 100644 --- a/internal/runtime/z_defer.go +++ b/internal/runtime/z_nogc.go @@ -1,3 +1,6 @@ +//go:build nogc +// +build nogc + /* * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. * @@ -16,9 +19,19 @@ package runtime -// Defer presents defer statements in a function. -type Defer struct { - Bits uintptr - Link *Defer - Rund int // index of RunDefers +import ( + "unsafe" + + "github.com/goplus/llgo/internal/runtime/c" +) + +// 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) } diff --git a/internal/runtime/z_c.go b/internal/runtime/z_rt.go similarity index 77% rename from internal/runtime/z_c.go rename to internal/runtime/z_rt.go index 29eb16ff..4d178efe 100644 --- a/internal/runtime/z_c.go +++ b/internal/runtime/z_rt.go @@ -20,20 +20,19 @@ import ( "unsafe" "github.com/goplus/llgo/internal/abi" - "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) +// ----------------------------------------------------------------------------- + +// 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. 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) c.Fprintf(fp, format, CStrCopy(cs, s)) } + +// -----------------------------------------------------------------------------