Skip to content

Commit

Permalink
move exit function to os lib
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Apr 13, 2024
1 parent c03a5d3 commit 010b247
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/cell/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func compilePackage(c *compiler.Compiler, path, name string, options *Options) e
for _, packagePath := range importNode.PackagePaths {

// Is built in to the compiler
if packagePath == "debug" {
if packagePath == "os" {
continue
}
if c.IsPackageImported(packagePath) {
Expand Down
42 changes: 14 additions & 28 deletions compiler/compiler/external_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/llir/llvm/ir"
llvmTypes "github.com/llir/llvm/ir/types"

"github.com/cell-labs/cell-script/compiler/compiler/types"
"github.com/cell-labs/cell-script/compiler/compiler/value"
)

// OSFuncs and the "debug" package contains a mapping to glibc functions.
// OSFuncs and the "os" package contains a mapping to glibc functions.
// These are used to make bootstrapping of the language easier. The end goal is to not depend on glibc.
type OSFuncs struct {
Printf value.Value
Expand All @@ -23,74 +22,61 @@ type OSFuncs struct {
}

func (c *Compiler) createExternalPackage() {
external := NewPkg("debug")

setExternal := func(internalName string, fn *ir.Func, variadic bool) value.Value {
fn.Sig.Variadic = variadic
val := value.Value{
Type: &types.Function{
LlvmReturnType: types.Void,
FuncType: fn.Type(),
IsExternal: true,
},
Value: fn,
}
external.DefinePkgVar(internalName, val)
return val
}

c.osFuncs.Printf = setExternal("Printf", c.module.NewFunc("printf",
debugPkg := NewPkg("debug")
c.osFuncs.Printf = debugPkg.setExternal("Printf", c.module.NewFunc("printf",
i32.LLVM(),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
), true)
c.packages["debug"] = debugPkg

c.osFuncs.Malloc = setExternal("malloc", c.module.NewFunc("malloc",
osPkg := NewPkg("os")
c.osFuncs.Malloc = osPkg.setExternal("malloc", c.module.NewFunc("malloc",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", i64.LLVM()),
), false)

c.osFuncs.Realloc = setExternal("realloc", c.module.NewFunc("realloc",
c.osFuncs.Realloc = osPkg.setExternal("realloc", c.module.NewFunc("realloc",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", i64.LLVM()),
), false)

c.osFuncs.Memcpy = setExternal("memcpy", c.module.NewFunc("memcpy",
c.osFuncs.Memcpy = osPkg.setExternal("memcpy", c.module.NewFunc("memcpy",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("dest", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("src", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("n", i64.LLVM()),
), false)

c.osFuncs.Strcat = setExternal("strcat", c.module.NewFunc("strcat",
c.osFuncs.Strcat = osPkg.setExternal("strcat", c.module.NewFunc("strcat",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
), false)

c.osFuncs.Strcpy = setExternal("strcpy", c.module.NewFunc("strcpy",
c.osFuncs.Strcpy = osPkg.setExternal("strcpy", c.module.NewFunc("strcpy",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
), false)

c.osFuncs.Strncpy = setExternal("strncpy", c.module.NewFunc("strncpy",
c.osFuncs.Strncpy = osPkg.setExternal("strncpy", c.module.NewFunc("strncpy",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", i64.LLVM()),
), false)

c.osFuncs.Strndup = setExternal("strndup", c.module.NewFunc("strndup",
c.osFuncs.Strndup = osPkg.setExternal("strndup", c.module.NewFunc("strndup",
llvmTypes.NewPointer(i8.LLVM()),
ir.NewParam("", llvmTypes.NewPointer(i8.LLVM())),
ir.NewParam("", i64.LLVM()),
), false)

c.osFuncs.Exit = setExternal("exit", c.module.NewFunc("syscall_exit",
c.osFuncs.Exit = osPkg.setExternal("exit", c.module.NewFunc("syscall_exit",
llvmTypes.Void,
ir.NewParam("", i8.LLVM()),
), false)

c.packages["debug"] = external
c.packages["os"] = osPkg
}
10 changes: 5 additions & 5 deletions compiler/compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {

funcRetType, treReturnTypes, llvmParams, treParams, isVariadicFunc, argumentReturnValuesCount := c.funcType(argTypes, retTypes)

isTxFuncs := c.currentPackageName == "tx" && (compiledName == "is_owner_mode" || compiledName == "script_verify" || compiledName == "get_utxo_inputs" || compiledName == "get_utxo_outputs")
isTxFFIFuncs := c.currentPackageName == "tx" && (compiledName == "is_owner_mode" || compiledName == "script_verify" || compiledName == "get_utxo_inputs" || compiledName == "get_utxo_outputs")
var fn *ir.Func
var entry *ir.Block

Expand All @@ -136,8 +136,8 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
c.initGlobalsFunc.Blocks[0].NewCall(fn) // Setup call to init from the global init func
} else {
fn = c.module.NewFunc(compiledName, funcRetType.LLVM(), llvmParams...)
// regiester ffi function definnition for tx package
if isTxFuncs {
// register ffi function definnition for tx package and os package
if isTxFFIFuncs {
// do not generate block
} else {
entry = fn.NewBlock(name.Block())
Expand All @@ -152,9 +152,9 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {
ArgumentTypes: treParams,
}

// regiester ffi function definnition for tx package
// register ffi function definition for tx package
// without generate func body
if isTxFuncs {
if isTxFFIFuncs {
val := value.Value{
Type: typesFunc,
Value: fn,
Expand Down
15 changes: 15 additions & 0 deletions compiler/compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"github.com/cell-labs/cell-script/compiler/compiler/types"
"github.com/cell-labs/cell-script/compiler/compiler/value"
"github.com/llir/llvm/ir"
)

// Representation of a Go package
Expand All @@ -24,6 +25,20 @@ func (p *pkg) DefinePkgVar(name string, val value.Value) {
p.vars[name] = val
}

func (p *pkg) setExternal(internalName string, fn *ir.Func, variadic bool) value.Value {
fn.Sig.Variadic = variadic
val := value.Value{
Type: &types.Function{
LlvmReturnType: types.Void,
FuncType: fn.Type(),
IsExternal: true,
},
Value: fn,
}
p.DefinePkgVar(internalName, val)
return val
}

func (p *pkg) GetPkgVar(name string, inSamePackage bool) (value.Value, bool) {

v, ok := p.vars[name]
Expand Down
3 changes: 2 additions & 1 deletion tests/examples/exit.cell
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "debug"
import "os"

function foo() int64 {
debug.Printf("exit from foo")
debug.exit(123)
os.exit(123)
return 0
}

Expand Down

0 comments on commit 010b247

Please sign in to comment.