Skip to content

Commit c03a5d3

Browse files
committed
add exit function
1 parent a0a5f41 commit c03a5d3

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

compiler/compiler/compiler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"errors"
1616

1717
"github.com/llir/llvm/ir"
18+
"github.com/llir/llvm/ir/constant"
19+
llvmTypes "github.com/llir/llvm/ir/types"
1820
llvmValue "github.com/llir/llvm/ir/value"
1921
)
2022

@@ -201,7 +203,6 @@ func (c *Compiler) addGlobal() {
201203
global.DefinePkgType("uint8", types.U8)
202204
global.DefinePkgType("int16", types.I16)
203205
global.DefinePkgType("uint16", types.U16)
204-
global.DefinePkgType("exit", types.I64)
205206
global.DefinePkgType("int32", types.I32)
206207
global.DefinePkgType("uint32", types.U32)
207208
global.DefinePkgType("int64", types.I64)
@@ -374,7 +375,7 @@ func (c *Compiler) panic(block *ir.Block, message string) {
374375
globMsg := c.module.NewGlobalDef(strings.NextStringName(), strings.Constant("runtime panic: "+message+"\n"))
375376
globMsg.Immutable = true
376377
block.NewCall(c.osFuncs.Printf.Value.(llvmValue.Named), strings.Toi8Ptr(block, globMsg))
377-
// block.NewCall(c.osFuncs.Exit.Value.(llvmValue.Named), constant.NewInt(llvmTypes.I32, 1))
378+
block.NewCall(c.osFuncs.Exit.Value.(llvmValue.Named), constant.NewInt(llvmTypes.I8, 1))
378379
}
379380

380381
type Panic string

compiler/compiler/external_funcs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func (c *Compiler) createExternalPackage() {
8787
ir.NewParam("", i64.LLVM()),
8888
), false)
8989

90-
// c.osFuncs.Exit = setExternal("exit", c.module.NewFunc("exit",
91-
// llvmTypes.Void,
92-
// ir.NewParam("", i32.LLVM()),
93-
// ), false)
90+
c.osFuncs.Exit = setExternal("exit", c.module.NewFunc("syscall_exit",
91+
llvmTypes.Void,
92+
ir.NewParam("", i8.LLVM()),
93+
), false)
9494

9595
c.packages["debug"] = external
9696
}

compiler/parser/parser.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func Parse(input []lexer.Item, debug bool) *FileNode {
4444
"uint256": {},
4545
"uintptr": {},
4646
"string": {},
47-
"exit": {},
4847
},
4948
}
5049

tests/examples/exit.cell

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "debug"
2+
3+
function foo() int64 {
4+
debug.Printf("exit from foo")
5+
debug.exit(123)
6+
return 0
7+
}
8+
9+
function main() {
10+
return foo()
11+
}

third-party/wrapper.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,8 @@ cell_data_t get_utxo_outputs() {
214214
}
215215
return outputs;
216216
}
217+
218+
void syscall_exit(int8_t code) {
219+
ckb_exit(code);
220+
return;
221+
}

0 commit comments

Comments
 (0)