From 010b2479b2384a9673928ca1e3c4377ff87c52db Mon Sep 17 00:00:00 2001 From: Cedric <14017092+douyixuan@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:24:57 +0800 Subject: [PATCH] move exit function to os lib --- cmd/cell/build/build.go | 2 +- compiler/compiler/external_funcs.go | 42 ++++++++++------------------- compiler/compiler/func.go | 10 +++---- compiler/compiler/package.go | 15 +++++++++++ tests/examples/exit.cell | 3 ++- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/cmd/cell/build/build.go b/cmd/cell/build/build.go index 5171980..72e0c4a 100644 --- a/cmd/cell/build/build.go +++ b/cmd/cell/build/build.go @@ -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) { diff --git a/compiler/compiler/external_funcs.go b/compiler/compiler/external_funcs.go index 13bd34d..32c4fe7 100644 --- a/compiler/compiler/external_funcs.go +++ b/compiler/compiler/external_funcs.go @@ -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 @@ -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 } diff --git a/compiler/compiler/func.go b/compiler/compiler/func.go index c8e3970..befa9eb 100644 --- a/compiler/compiler/func.go +++ b/compiler/compiler/func.go @@ -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 @@ -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()) @@ -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, diff --git a/compiler/compiler/package.go b/compiler/compiler/package.go index 3bec4c3..c63866e 100644 --- a/compiler/compiler/package.go +++ b/compiler/compiler/package.go @@ -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 @@ -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] diff --git a/tests/examples/exit.cell b/tests/examples/exit.cell index 3dd3b8b..9185218 100644 --- a/tests/examples/exit.cell +++ b/tests/examples/exit.cell @@ -1,8 +1,9 @@ import "debug" +import "os" function foo() int64 { debug.Printf("exit from foo") - debug.exit(123) + os.exit(123) return 0 }