...
 
Commits (9)
    https://gitcode.net/wa-lang/wa/-/commit/7b220f4b9ae59461977367a89bbf2527cd870d12 简化 std 测试方式 2023-06-30T18:37:46+08:00 chai2010 chaishushan@gmail.com https://gitcode.net/wa-lang/wa/-/commit/48db574ba4e40ae2a90900295d8103b26ca08f5c 去掉内部的 ascii 转化表, 减少输出 wat 体积 2023-06-30T19:08:51+08:00 chai2010 chaishushan@gmail.com https://gitcode.net/wa-lang/wa/-/commit/c957ea0f7d2943a87e2b1b3b50e6bca0fe5c18ab 移植部分 image 实现, 未完 2023-06-30T19:52:55+08:00 chai2010 chaishushan@gmail.com https://gitcode.net/wa-lang/wa/-/commit/43ca4537945d3877fd1f1e4703de8b0206b2dd97 搬运 math/bits 包 2023-06-30T20:14:23+08:00 chai2010 chaishushan@gmail.com https://gitcode.net/wa-lang/wa/-/commit/f018eb7c71a86c316f54b75632b5938fdbf2967d 改进整数打印 2023-07-01T10:06:09+08:00 3dgen 476582@qq.com https://gitcode.net/wa-lang/wa/-/commit/f7145176614cc1393b520444d631404fab0ef63c Merge branch 'master' into backend_wasm 2023-07-01T10:06:52+08:00 3dgen 476582@qq.com https://gitcode.net/wa-lang/wa/-/commit/decb015cda69797f6c37bc43a463968b2a14e26a 添加! ^ 单目操作符 2023-07-01T17:51:00+08:00 3dgen 476582@qq.com https://gitcode.net/wa-lang/wa/-/commit/92ec97e8ab7d8da3e797f9ae7ea3a1de05c1c67d 增加 buildin.ssa:wrapnilchk 2023-07-01T21:34:52+08:00 3dgen 476582@qq.com https://gitcode.net/wa-lang/wa/-/commit/2f5995a2d8c2939bd50afc3c61659c2ace0903d5 panic 参数限制为 string 2023-07-02T14:19:32+08:00 chai2010 chaishushan@gmail.com
...@@ -23,19 +23,7 @@ ci-test-all: ...@@ -23,19 +23,7 @@ ci-test-all:
go test ./... go test ./...
@echo "== std test begin ==" @echo "== std test begin =="
go run main.go test binary go run main.go test std
go run main.go test errors
go run main.go test fmt
go run main.go test image
go run main.go test image/bmp
go run main.go test image/color
go run main.go test io
go run main.go test math
go run main.go test os
go run main.go test regexp
go run main.go test strconv
go run main.go test unicode
go run main.go test unicode/utf8
@echo "== std ok ==" @echo "== std ok =="
go run main.go ./waroot/hello.wa go run main.go ./waroot/hello.wa
......
...@@ -14,9 +14,20 @@ import ( ...@@ -14,9 +14,20 @@ import (
"wa-lang.org/wa/internal/loader" "wa-lang.org/wa/internal/loader"
"wa-lang.org/wa/internal/wabt" "wa-lang.org/wa/internal/wabt"
"wa-lang.org/wa/internal/wazero" "wa-lang.org/wa/internal/wazero"
"wa-lang.org/wa/waroot"
) )
func RunTest(cfg *config.Config, pkgpath string, appArgs ...string) { func RunTest(cfg *config.Config, pkgpath string, appArgs ...string) {
var pkgList = []string{pkgpath}
if pkgpath == "std" {
pkgList = waroot.GetStdPkgList()
}
for _, p := range pkgList {
runTest(cfg, p, appArgs...)
}
}
func runTest(cfg *config.Config, pkgpath string, appArgs ...string) {
cfg.UnitTest = true cfg.UnitTest = true
prog, err := loader.LoadProgram(cfg, pkgpath) prog, err := loader.LoadProgram(cfg, pkgpath)
if err != nil { if err != nil {
......
...@@ -68,10 +68,6 @@ func (g *functionGenerator) getValue(i ssa.Value) valueWrap { ...@@ -68,10 +68,6 @@ func (g *functionGenerator) getValue(i ssa.Value) valueWrap {
return valueWrap{value: wir.NewConst("0", g.module.I32)} return valueWrap{value: wir.NewConst("0", g.module.I32)}
} }
case types.Int:
val, _ := constant.Int64Val(v.Value)
return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.I32)}
case types.Uint8: case types.Uint8:
val, _ := constant.Uint64Val(v.Value) val, _ := constant.Uint64Val(v.Value)
return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.U8)} return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.U8)}
...@@ -88,11 +84,11 @@ func (g *functionGenerator) getValue(i ssa.Value) valueWrap { ...@@ -88,11 +84,11 @@ func (g *functionGenerator) getValue(i ssa.Value) valueWrap {
// val, _ := constant.Int64Val(v.Value) // val, _ := constant.Int64Val(v.Value)
// return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.I16)} // return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.I16)}
case types.Uint32, types.Uintptr: case types.Uint32, types.Uintptr, types.Uint:
val, _ := constant.Uint64Val(v.Value) val, _ := constant.Uint64Val(v.Value)
return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.U32)} return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.U32)}
case types.Int32: case types.Int32, types.Int:
val, _ := constant.Int64Val(v.Value) val, _ := constant.Int64Val(v.Value)
if t.Name() == "rune" { if t.Name() == "rune" {
return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.RUNE)} return valueWrap{value: wir.NewConst(strconv.Itoa(int(val)), g.module.RUNE)}
...@@ -343,7 +339,7 @@ func (g *functionGenerator) genInstruction(inst ssa.Instruction) (insts []wat.In ...@@ -343,7 +339,7 @@ func (g *functionGenerator) genInstruction(inst ssa.Instruction) (insts []wat.In
insts = append(insts, s...) insts = append(insts, s...)
default: default:
logger.Fatal("Todo:", inst.String()) logger.Fatalf("Todo: %[1]v: %[1]T", inst)
} }
insts = append(insts, wat.NewBlank()) insts = append(insts, wat.NewBlank())
return return
...@@ -429,8 +425,16 @@ func (g *functionGenerator) genUnOp(inst *ssa.UnOp) (insts []wat.Inst, ret_type ...@@ -429,8 +425,16 @@ func (g *functionGenerator) genUnOp(inst *ssa.UnOp) (insts []wat.Inst, ret_type
x := g.getValue(inst.X) x := g.getValue(inst.X)
return g.module.EmitUnOp(x.value, wat.OpCodeSub) return g.module.EmitUnOp(x.value, wat.OpCodeSub)
case token.XOR:
x := g.getValue(inst.X)
return g.module.EmitUnOp(x.value, wat.OpCodeXor)
case token.NOT:
x := g.getValue(inst.X)
return g.module.EmitUnOp(x.value, wat.OpCodeNot)
default: default:
logger.Fatal("Todo") logger.Fatalf("Todo: %[1]v: %[1]T", inst)
} }
return return
...@@ -624,10 +628,18 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst, ...@@ -624,10 +628,18 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst,
insts = append(insts, wat.NewInstCall("$runtime.waPrintRune")) insts = append(insts, wat.NewInstCall("$runtime.waPrintRune"))
} }
if avt.Equal(g.module.U8) || avt.Equal(g.module.U16) || if avt.Equal(g.module.I32) {
avt.Equal(g.module.I32) || avt.Equal(g.module.U32) {
insts = append(insts, av.EmitPush()...) insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintI32")) insts = append(insts, wat.NewInstCall("$runtime.waPrintI32"))
} else if avt.Equal(g.module.U8) || avt.Equal(g.module.U16) || avt.Equal(g.module.U32) {
insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintU32"))
} else if avt.Equal(g.module.I64) {
insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintI64"))
} else if avt.Equal(g.module.U64) {
insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintU64"))
} else if avt.Equal(g.module.F32) { } else if avt.Equal(g.module.F32) {
insts = append(insts, av.EmitPush()...) insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintF32")) insts = append(insts, wat.NewInstCall("$runtime.waPrintF32"))
...@@ -679,6 +691,10 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst, ...@@ -679,6 +691,10 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst,
insts = g.module.EmitGenCopy(g.getValue(call.Args[0]).value, g.getValue(call.Args[1]).value) insts = g.module.EmitGenCopy(g.getValue(call.Args[0]).value, g.getValue(call.Args[1]).value)
ret_type = g.module.I32 ret_type = g.module.I32
case "ssa:wrapnilchk":
insts = g.getValue(call.Args[0]).value.EmitPush()
ret_type = g.getValue(call.Args[0]).value.Type()
default: default:
logger.Fatal("Todo:", call.Value) logger.Fatal("Todo:", call.Value)
} }
......
...@@ -36,9 +36,26 @@ func (m *Module) EmitUnOp(x Value, op wat.OpCode) (insts []wat.Inst, ret_type Va ...@@ -36,9 +36,26 @@ func (m *Module) EmitUnOp(x Value, op wat.OpCode) (insts []wat.Inst, ret_type Va
logger.Fatal("Todo") logger.Fatal("Todo")
} }
ret_type = x.Type() ret_type = x.Type()
insts = append(insts, NewConst("0", ret_type).EmitPush()...)
insts = append(insts, x.EmitPush()...) switch op {
insts = append(insts, wat.NewInstSub(toWatType(ret_type))) case wat.OpCodeSub:
insts = append(insts, NewConst("0", ret_type).EmitPush()...)
insts = append(insts, x.EmitPush()...)
insts = append(insts, wat.NewInstSub(toWatType(ret_type)))
case wat.OpCodeXor:
insts = append(insts, NewConst("-1", ret_type).EmitPush()...)
insts = append(insts, x.EmitPush()...)
insts = append(insts, wat.NewInstXor(toWatType(ret_type)))
case wat.OpCodeNot:
insts = append(insts, x.EmitPush()...)
insts = append(insts, wat.NewInstEqz(toWatType(ret_type)))
default:
logger.Fatalf("Todo: %[1]v: %[1]T", op)
}
return return
} }
......
...@@ -92,6 +92,7 @@ const ( ...@@ -92,6 +92,7 @@ const (
OpCodeAnd OpCodeAnd
OpCodeOr OpCodeOr
OpCodeXor OpCodeXor
OpCodeNot
OpCodeShl OpCodeShl
OpCodeShr OpCodeShr
OpCodeAndNot OpCodeAndNot
......
...@@ -2494,19 +2494,19 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl { ...@@ -2494,19 +2494,19 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
scope := ast.NewScope(p.topScope) // function scope scope := ast.NewScope(p.topScope) // function scope
var recv *ast.FieldList var recv *ast.FieldList
if p.wagoMode { //if p.wagoMode {
if p.tok == token.LPAREN { if p.tok == token.LPAREN {
recv = p.parseParameters(scope, false) recv = p.parseParameters(scope, false)
}
} }
//}
ident := p.parseIdent() ident := p.parseIdent()
// func Type.method() // func Type.method()
if !p.wagoMode { if !p.wagoMode {
if recv != nil { //if recv != nil {
panic("unreachable") // panic("unreachable")
} //}
if p.tok == token.PERIOD { if p.tok == token.PERIOD {
thisIdent := &ast.Ident{Name: "this"} thisIdent := &ast.Ident{Name: "this"}
thisField := &ast.Field{ thisField := &ast.Field{
......
...@@ -457,6 +457,20 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -457,6 +457,20 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
} }
case _Panic: case _Panic:
filename := check.fset.Position(x.pos()).Filename
// panic 语义变化: func panic(msg: string)
if strings.HasSuffix(filename, ".wa") {
// 放松检查, bool 变量放到运行时处理
if !isString(x.typ) {
check.invalidArg(x.pos(), "%s is not a string type", x)
return
}
if nargs != 1 {
check.errorf(call.Pos(), "%v expects %d or %d arguments; found %d", call, 1, 2, nargs)
return
}
}
// panic(x) // panic(x)
// record panic call if inside a function with result parameters // record panic call if inside a function with result parameters
// (for use in Checker.isTerminating) // (for use in Checker.isTerminating)
......
// 版权 @2023 凹语言 作者。保留所有权利。
import "math/bits" => _
func main {
println("hello math")
}
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
import ( import (
"errors" "errors"
//"image" "image"
//"image/color" //"image/color"
//"io" "io"
) )
// ErrUnsupported means that the input BMP image uses a valid but unsupported // ErrUnsupported means that the input BMP image uses a valid but unsupported
...@@ -20,7 +20,6 @@ func readUint32(b: []byte) => uint32 { ...@@ -20,7 +20,6 @@ func readUint32(b: []byte) => uint32 {
} }
/*
// decodeRGB reads a 24 bit-per-pixel BMP image from r. // decodeRGB reads a 24 bit-per-pixel BMP image from r.
// If topDown is false, the image rows will be read bottom-up. // If topDown is false, the image rows will be read bottom-up.
...@@ -52,6 +51,7 @@ func decodeRGB(r: io.Reader, c: image.Config, topDown: bool) => (m: image.Image, ...@@ -52,6 +51,7 @@ func decodeRGB(r: io.Reader, c: image.Config, topDown: bool) => (m: image.Image,
} }
/*
// decodePaletted reads an 8 bit-per-pixel BMP image from r. // decodePaletted reads an 8 bit-per-pixel BMP image from r.
// If topDown is false, the image rows will be read bottom-up. // If topDown is false, the image rows will be read bottom-up.
func decodePaletted(r: io.Reader, c: image.Config, topDown: bool) => (m: image.Image, err: error) { func decodePaletted(r: io.Reader, c: image.Config, topDown: bool) => (m: image.Image, err: error) {
......
// 版权 @2023 凹语言 作者。保留所有权利。 // 版权 @2023 凹语言 作者。保留所有权利。
import ( import (
//"image/color" "image/color"
"strconv" "strconv"
) )
...@@ -41,10 +41,9 @@ func Point.In(r: Rectangle) => bool { ...@@ -41,10 +41,9 @@ func Point.In(r: Rectangle) => bool {
r.Min.Y <= this.Y && this.Y < r.Max.Y r.Min.Y <= this.Y && this.Y < r.Max.Y
} }
/*
// Mod returns the point q in r such that p.X-q.X is a multiple of r's width // Mod returns the point q in r such that p.X-q.X is a multiple of r's width
// and p.Y-q.Y is a multiple of r's height. // and p.Y-q.Y is a multiple of r's height.
func (p Point) Mod(r Rectangle) Point { func (p: Point) Mod(r: Rectangle) => Point {
w, h := r.Dx(), r.Dy() w, h := r.Dx(), r.Dy()
p = p.Sub(r.Min) p = p.Sub(r.Min)
p.X = p.X % w p.X = p.X % w
...@@ -59,20 +58,19 @@ func (p Point) Mod(r Rectangle) Point { ...@@ -59,20 +58,19 @@ func (p Point) Mod(r Rectangle) Point {
} }
// Eq reports whether p and q are equal. // Eq reports whether p and q are equal.
func (p Point) Eq(q Point) bool { func (p: Point) Eq(q: Point) => bool {
return p == q return p == q
} }
// ZP is the zero Point. // ZP is the zero Point.
// //
// Deprecated: Use a literal image.Point{} instead. // Deprecated: Use a literal image.Point{} instead.
var ZP Point var ZP: Point
// Pt is shorthand for Point{X, Y}. // Pt is shorthand for Point{X, Y}.
func Pt(X, Y int) Point { func Pt(X, Y: int) => Point {
return Point{X, Y} return Point{X, Y}
} }
*/
// A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y. // A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.
// It is well-formed if Min.X <= Max.X and likewise for Y. Points are always // It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
...@@ -91,19 +89,18 @@ func Rectangle.String => string { ...@@ -91,19 +89,18 @@ func Rectangle.String => string {
return this.Min.String() + "-" + this.Max.String() return this.Min.String() + "-" + this.Max.String()
} }
/*
// Dx returns r's width. // Dx returns r's width.
func (r Rectangle) Dx() int { func (r: Rectangle) Dx => int {
return r.Max.X - r.Min.X return r.Max.X - r.Min.X
} }
// Dy returns r's height. // Dy returns r's height.
func (r Rectangle) Dy() int { func (r: Rectangle) Dy => int {
return r.Max.Y - r.Min.Y return r.Max.Y - r.Min.Y
} }
// Size returns r's width and height. // Size returns r's width and height.
func (r Rectangle) Size() Point { func (r: Rectangle) Size => Point {
return Point{ return Point{
r.Max.X - r.Min.X, r.Max.X - r.Min.X,
r.Max.Y - r.Min.Y, r.Max.Y - r.Min.Y,
...@@ -111,7 +108,7 @@ func (r Rectangle) Size() Point { ...@@ -111,7 +108,7 @@ func (r Rectangle) Size() Point {
} }
// Add returns the rectangle r translated by p. // Add returns the rectangle r translated by p.
func (r Rectangle) Add(p Point) Rectangle { func (r: Rectangle) Add(p: Point) => Rectangle {
return Rectangle{ return Rectangle{
Point{r.Min.X + p.X, r.Min.Y + p.Y}, Point{r.Min.X + p.X, r.Min.Y + p.Y},
Point{r.Max.X + p.X, r.Max.Y + p.Y}, Point{r.Max.X + p.X, r.Max.Y + p.Y},
...@@ -119,7 +116,7 @@ func (r Rectangle) Add(p Point) Rectangle { ...@@ -119,7 +116,7 @@ func (r Rectangle) Add(p Point) Rectangle {
} }
// Sub returns the rectangle r translated by -p. // Sub returns the rectangle r translated by -p.
func (r Rectangle) Sub(p Point) Rectangle { func (r: Rectangle) Sub(p: Point) => Rectangle {
return Rectangle{ return Rectangle{
Point{r.Min.X - p.X, r.Min.Y - p.Y}, Point{r.Min.X - p.X, r.Min.Y - p.Y},
Point{r.Max.X - p.X, r.Max.Y - p.Y}, Point{r.Max.X - p.X, r.Max.Y - p.Y},
...@@ -129,7 +126,7 @@ func (r Rectangle) Sub(p Point) Rectangle { ...@@ -129,7 +126,7 @@ func (r Rectangle) Sub(p Point) Rectangle {
// Inset returns the rectangle r inset by n, which may be negative. If either // Inset returns the rectangle r inset by n, which may be negative. If either
// of r's dimensions is less than 2*n then an empty rectangle near the center // of r's dimensions is less than 2*n then an empty rectangle near the center
// of r will be returned. // of r will be returned.
func (r Rectangle) Inset(n int) Rectangle { func (r: Rectangle) Inset(n: int) => Rectangle {
if r.Dx() < 2*n { if r.Dx() < 2*n {
r.Min.X = (r.Min.X + r.Max.X) / 2 r.Min.X = (r.Min.X + r.Max.X) / 2
r.Max.X = r.Min.X r.Max.X = r.Min.X
...@@ -149,7 +146,7 @@ func (r Rectangle) Inset(n int) Rectangle { ...@@ -149,7 +146,7 @@ func (r Rectangle) Inset(n int) Rectangle {
// Intersect returns the largest rectangle contained by both r and s. If the // Intersect returns the largest rectangle contained by both r and s. If the
// two rectangles do not overlap then the zero rectangle will be returned. // two rectangles do not overlap then the zero rectangle will be returned.
func (r Rectangle) Intersect(s Rectangle) Rectangle { func (r: Rectangle) Intersect(s: Rectangle) => Rectangle {
if r.Min.X < s.Min.X { if r.Min.X < s.Min.X {
r.Min.X = s.Min.X r.Min.X = s.Min.X
} }
...@@ -173,7 +170,7 @@ func (r Rectangle) Intersect(s Rectangle) Rectangle { ...@@ -173,7 +170,7 @@ func (r Rectangle) Intersect(s Rectangle) Rectangle {
} }
// Union returns the smallest rectangle that contains both r and s. // Union returns the smallest rectangle that contains both r and s.
func (r Rectangle) Union(s Rectangle) Rectangle { func (r: Rectangle) Union(s: Rectangle) => Rectangle {
if r.Empty() { if r.Empty() {
return s return s
} }
...@@ -196,25 +193,25 @@ func (r Rectangle) Union(s Rectangle) Rectangle { ...@@ -196,25 +193,25 @@ func (r Rectangle) Union(s Rectangle) Rectangle {
} }
// Empty reports whether the rectangle contains no points. // Empty reports whether the rectangle contains no points.
func (r Rectangle) Empty() bool { func (r: Rectangle) Empty => bool {
return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
} }
// Eq reports whether r and s contain the same set of points. All empty // Eq reports whether r and s contain the same set of points. All empty
// rectangles are considered equal. // rectangles are considered equal.
func (r Rectangle) Eq(s Rectangle) bool { func (r: Rectangle) Eq(s: Rectangle) => bool {
return r == s || r.Empty() && s.Empty() return r == s || r.Empty() && s.Empty()
} }
// Overlaps reports whether r and s have a non-empty intersection. // Overlaps reports whether r and s have a non-empty intersection.
func (r Rectangle) Overlaps(s Rectangle) bool { func (r: Rectangle) Overlaps(s: Rectangle) => bool {
return !r.Empty() && !s.Empty() && return !r.Empty() && !s.Empty() &&
r.Min.X < s.Max.X && s.Min.X < r.Max.X && r.Min.X < s.Max.X && s.Min.X < r.Max.X &&
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
} }
// In reports whether every point in r is in s. // In reports whether every point in r is in s.
func (r Rectangle) In(s Rectangle) bool { func (r: Rectangle) In(s: Rectangle) => bool {
if r.Empty() { if r.Empty() {
return true return true
} }
...@@ -226,7 +223,7 @@ func (r Rectangle) In(s Rectangle) bool { ...@@ -226,7 +223,7 @@ func (r Rectangle) In(s Rectangle) bool {
// Canon returns the canonical version of r. The returned rectangle has minimum // Canon returns the canonical version of r. The returned rectangle has minimum
// and maximum coordinates swapped if necessary so that it is well-formed. // and maximum coordinates swapped if necessary so that it is well-formed.
func (r Rectangle) Canon() Rectangle { func (r: Rectangle) Canon => Rectangle {
if r.Max.X < r.Min.X { if r.Max.X < r.Min.X {
r.Min.X, r.Max.X = r.Max.X, r.Min.X r.Min.X, r.Max.X = r.Max.X, r.Min.X
} }
...@@ -237,40 +234,42 @@ func (r Rectangle) Canon() Rectangle { ...@@ -237,40 +234,42 @@ func (r Rectangle) Canon() Rectangle {
} }
// At implements the Image interface. // At implements the Image interface.
func (r Rectangle) At(x, y int) color.Color { func (r: Rectangle) At(x, y: int) => color.Color {
if (Point{x, y}).In(r) { pt := Point{x, y}
return color.Opaque if (pt).In(r) {
return &color.Opaque
} }
return color.Transparent return &color.Transparent
} }
// RGBA64At implements the RGBA64Image interface. // RGBA64At implements the RGBA64Image interface.
func (r Rectangle) RGBA64At(x, y int) color.RGBA64 { func (r: Rectangle) RGBA64At(x, y: int) => color.RGBA64 {
if (Point{x, y}).In(r) { pt := Point{x, y}
if (pt).In(r) {
return color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff} return color.RGBA64{0xffff, 0xffff, 0xffff, 0xffff}
} }
return color.RGBA64{} return color.RGBA64{}
} }
// Bounds implements the Image interface. // Bounds implements the Image interface.
func (r Rectangle) Bounds() Rectangle { func (r: Rectangle) Bounds => Rectangle {
return r return r
} }
// ColorModel implements the Image interface. // ColorModel implements the Image interface.
func (r Rectangle) ColorModel() color.Model { func (r: Rectangle) ColorModel => color.Model {
return color.Alpha16Model return color.Alpha16Model
} }
// ZR is the zero Rectangle. // ZR is the zero Rectangle.
// //
// Deprecated: Use a literal image.Rectangle{} instead. // Deprecated: Use a literal image.Rectangle{} instead.
var ZR Rectangle var ZR: Rectangle
// Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned // Rect is shorthand for Rectangle{Pt(x0, y0), Pt(x1, y1)}. The returned
// rectangle has minimum and maximum coordinates swapped if necessary so that // rectangle has minimum and maximum coordinates swapped if necessary so that
// it is well-formed. // it is well-formed.
func Rect(x0, y0, x1, y1 int) Rectangle { func Rect(x0, y0, x1, y1: int) => Rectangle {
if x0 > x1 { if x0 > x1 {
x0, x1 = x1, x0 x0, x1 = x1, x0
} }
...@@ -282,10 +281,13 @@ func Rect(x0, y0, x1, y1 int) Rectangle { ...@@ -282,10 +281,13 @@ func Rect(x0, y0, x1, y1 int) Rectangle {
// mul3NonNeg returns (x * y * z), unless at least one argument is negative or // mul3NonNeg returns (x * y * z), unless at least one argument is negative or
// if the computation overflows the int type, in which case it returns -1. // if the computation overflows the int type, in which case it returns -1.
func mul3NonNeg(x int, y int, z int) int { func mul3NonNeg(x: int, y: int, z: int) => int {
if (x < 0) || (y < 0) || (z < 0) { if (x < 0) || (y < 0) || (z < 0) {
return -1 return -1
} }
// todo(chai2010): 补充 bits 包
return 0
/*
hi, lo := bits.Mul64(uint64(x), uint64(y)) hi, lo := bits.Mul64(uint64(x), uint64(y))
if hi != 0 { if hi != 0 {
return -1 return -1
...@@ -299,11 +301,13 @@ func mul3NonNeg(x int, y int, z int) int { ...@@ -299,11 +301,13 @@ func mul3NonNeg(x int, y int, z int) int {
return -1 return -1
} }
return a return a
*/
} }
// add2NonNeg returns (x + y), unless at least one argument is negative or if // add2NonNeg returns (x + y), unless at least one argument is negative or if
// the computation overflows the int type, in which case it returns -1. // the computation overflows the int type, in which case it returns -1.
func add2NonNeg(x int, y int) int { func add2NonNeg(x: int, y: int) => int {
if (x < 0) || (y < 0) { if (x < 0) || (y < 0) {
return -1 return -1
} }
...@@ -313,5 +317,3 @@ func add2NonNeg(x int, y int) int { ...@@ -313,5 +317,3 @@ func add2NonNeg(x int, y int) int {
} }
return a return a
} }
*/
\ No newline at end of file
...@@ -46,6 +46,8 @@ type PalettedImage interface { ...@@ -46,6 +46,8 @@ type PalettedImage interface {
/* /*
*/
// pixelBufferLength returns the length of the []uint8 typed Pix slice field // pixelBufferLength returns the length of the []uint8 typed Pix slice field
// for the NewXxx functions. Conceptually, this is just (bpp * width * height), // for the NewXxx functions. Conceptually, this is just (bpp * width * height),
// but this function panics if at least one of those is negative or if the // but this function panics if at least one of those is negative or if the
...@@ -61,6 +63,7 @@ func pixelBufferLength(bytesPerPixel :int, r :Rectangle, imageTypeName :string) ...@@ -61,6 +63,7 @@ func pixelBufferLength(bytesPerPixel :int, r :Rectangle, imageTypeName :string)
return totalLength return totalLength
} }
// RGBA is an in-memory image whose At method returns color.RGBA values. // RGBA is an in-memory image whose At method returns color.RGBA values.
type RGBA struct { type RGBA struct {
// Pix holds the image's pixels, in R, G, B, A order. The pixel at // Pix holds the image's pixels, in R, G, B, A order. The pixel at
...@@ -73,16 +76,18 @@ type RGBA struct { ...@@ -73,16 +76,18 @@ type RGBA struct {
} }
func (p *RGBA) ColorModel() color.Model { return color.RGBAModel } func (p: *RGBA) ColorModel => color.Model { return color.RGBAModel }
func (p *RGBA) Bounds() Rectangle { return p.Rect } func (p: *RGBA) Bounds => Rectangle { return p.Rect }
func (p *RGBA) At(x, y int) color.Color { func (p: *RGBA) At(x, y: int) => color.Color {
return p.RGBAAt(x, y) c := p.RGBAAt(x, y)
return &c
} }
func (p *RGBA) RGBA64At(x, y int) color.RGBA64 { func (p: *RGBA) RGBA64At(x, y: int) => color.RGBA64 {
if !(Point{x, y}.In(p.Rect)) { pt := Point{x, y}
if !(pt.In(p.Rect)) {
return color.RGBA64{} return color.RGBA64{}
} }
i := p.PixOffset(x, y) i := p.PixOffset(x, y)
...@@ -99,8 +104,9 @@ func (p *RGBA) RGBA64At(x, y int) color.RGBA64 { ...@@ -99,8 +104,9 @@ func (p *RGBA) RGBA64At(x, y int) color.RGBA64 {
} }
} }
func (p *RGBA) RGBAAt(x, y int) color.RGBA { func (p: *RGBA) RGBAAt(x, y: int) => color.RGBA {
if !(Point{x, y}.In(p.Rect)) { pt := Point{x, y}
if !(pt.In(p.Rect)) {
return color.RGBA{} return color.RGBA{}
} }
i := p.PixOffset(x, y) i := p.PixOffset(x, y)
...@@ -110,16 +116,17 @@ func (p *RGBA) RGBAAt(x, y int) color.RGBA { ...@@ -110,16 +116,17 @@ func (p *RGBA) RGBAAt(x, y int) color.RGBA {
// PixOffset returns the index of the first element of Pix that corresponds to // PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y). // the pixel at (x, y).
func (p *RGBA) PixOffset(x, y int) int { func (p: *RGBA) PixOffset(x, y: int) => int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4 return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4
} }
func (p *RGBA) Set(x, y int, c color.Color) { func (p: *RGBA) Set(x, y: int, c: color.Color) {
if !(Point{x, y}.In(p.Rect)) { pt := Point{x, y}
if !(pt.In(p.Rect)) {
return return
} }
i := p.PixOffset(x, y) i := p.PixOffset(x, y)
c1 := color.RGBAModel.Convert(c).(color.RGBA) c1 := color.RGBAModel.Convert(c).(*color.RGBA)
s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857 s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
s[0] = c1.R s[0] = c1.R
s[1] = c1.G s[1] = c1.G
...@@ -127,8 +134,9 @@ func (p *RGBA) Set(x, y int, c color.Color) { ...@@ -127,8 +134,9 @@ func (p *RGBA) Set(x, y int, c color.Color) {
s[3] = c1.A s[3] = c1.A
} }
func (p *RGBA) SetRGBA64(x, y int, c color.RGBA64) { func (p: *RGBA) SetRGBA64(x, y: int, c: color.RGBA64) {
if !(Point{x, y}.In(p.Rect)) { pt := Point{x, y}
if !pt.In(p.Rect) {
return return
} }
i := p.PixOffset(x, y) i := p.PixOffset(x, y)
...@@ -139,8 +147,9 @@ func (p *RGBA) SetRGBA64(x, y int, c color.RGBA64) { ...@@ -139,8 +147,9 @@ func (p *RGBA) SetRGBA64(x, y int, c color.RGBA64) {
s[3] = uint8(c.A >> 8) s[3] = uint8(c.A >> 8)
} }
func (p *RGBA) SetRGBA(x, y int, c color.RGBA) { func (p: *RGBA) SetRGBA(x, y: int, c: color.RGBA) {
if !(Point{x, y}.In(p.Rect)) { pt := Point{x, y}
if !(pt.In(p.Rect)) {
return return
} }
i := p.PixOffset(x, y) i := p.PixOffset(x, y)
...@@ -153,7 +162,7 @@ func (p *RGBA) SetRGBA(x, y int, c color.RGBA) { ...@@ -153,7 +162,7 @@ func (p *RGBA) SetRGBA(x, y int, c color.RGBA) {
// SubImage returns an image representing the portion of the image p visible // SubImage returns an image representing the portion of the image p visible
// through r. The returned value shares pixels with the original image. // through r. The returned value shares pixels with the original image.
func (p *RGBA) SubImage(r Rectangle) Image { func (p: *RGBA) SubImage(r: Rectangle) => Image {
r = r.Intersect(p.Rect) r = r.Intersect(p.Rect)
// If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside // If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside
// either r1 or r2 if the intersection is empty. Without explicitly checking for // either r1 or r2 if the intersection is empty. Without explicitly checking for
...@@ -170,7 +179,7 @@ func (p *RGBA) SubImage(r Rectangle) Image { ...@@ -170,7 +179,7 @@ func (p *RGBA) SubImage(r Rectangle) Image {
} }
// Opaque scans the entire image and reports whether it is fully opaque. // Opaque scans the entire image and reports whether it is fully opaque.
func (p *RGBA) Opaque() bool { func (p: *RGBA) Opaque => bool {
if p.Rect.Empty() { if p.Rect.Empty() {
return true return true
} }
...@@ -188,7 +197,7 @@ func (p *RGBA) Opaque() bool { ...@@ -188,7 +197,7 @@ func (p *RGBA) Opaque() bool {
} }
// NewRGBA returns a new RGBA image with the given bounds. // NewRGBA returns a new RGBA image with the given bounds.
func NewRGBA(r Rectangle) *RGBA { func NewRGBA(r: Rectangle) => *RGBA {
return &RGBA{ return &RGBA{
Pix: make([]uint8, pixelBufferLength(4, r, "RGBA")), Pix: make([]uint8, pixelBufferLength(4, r, "RGBA")),
Stride: 4 * r.Dx(), Stride: 4 * r.Dx(),
...@@ -196,6 +205,9 @@ func NewRGBA(r Rectangle) *RGBA { ...@@ -196,6 +205,9 @@ func NewRGBA(r Rectangle) *RGBA {
} }
} }
/*
// RGBA64 is an in-memory image whose At method returns color.RGBA64 values. // RGBA64 is an in-memory image whose At method returns color.RGBA64 values.
type RGBA64 struct { type RGBA64 struct {
// Pix holds the image's pixels, in R, G, B, A order and big-endian format. The pixel at // Pix holds the image's pixels, in R, G, B, A order and big-endian format. The pixel at
......
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run make_tables.go
const uintSize = 32 << (^uint(0) >> 63) // 32 or 64
// UintSize is the size of a uint in bits.
const UintSize = uintSize
// --- LeadingZeros ---
// LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
func LeadingZeros(x: uint) => int { return UintSize - Len(x) }
// LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
func LeadingZeros8(x: uint8) => int { return 8 - Len8(x) }
// LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
func LeadingZeros16(x: uint16) => int { return 16 - Len16(x) }
// LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
func LeadingZeros32(x: uint32) => int { return 32 - Len32(x) }
// LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
func LeadingZeros64(x: uint64) => int { return 64 - Len64(x) }
// --- TrailingZeros ---
// See http://supertech.csail.mit.edu/papers/debruijn.pdf
const deBruijn32 = 0x077CB531
var deBruijn32tab = [32]byte{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
}
const deBruijn64 = 0x03f79d71b4ca8b09
var deBruijn64tab = [64]byte{
0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
}
// TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
func TrailingZeros(x: uint) => int {
if UintSize == 32 {
return TrailingZeros32(uint32(x))
}
return TrailingZeros64(uint64(x))
}
// TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
func TrailingZeros8(x: uint8) => int {
return int(ntz8tab[x])
}
// TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
func TrailingZeros16(x: uint16) => int {
if x == 0 {
return 16
}
// see comment in TrailingZeros64
return int(deBruijn32tab[uint32(x&-x)*deBruijn32>>(32-5)])
}
// TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
func TrailingZeros32(x: uint32) => int {
if x == 0 {
return 32
}
// see comment in TrailingZeros64
return int(deBruijn32tab[(x&-x)*deBruijn32>>(32-5)])
}
// TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
func TrailingZeros64(x: uint64) => int {
if x == 0 {
return 64
}
// If popcount is fast, replace code below with return popcount(^x & (x - 1)).
//
// x & -x leaves only the right-most bit set in the word. Let k be the
// index of that bit. Since only a single bit is set, the value is two
// to the power of k. Multiplying by a power of two is equivalent to
// left shifting, in this case by k bits. The de Bruijn (64 bit) constant
// is such that all six bit, consecutive substrings are distinct.
// Therefore, if we have a left shifted version of this constant we can
// find by how many bits it was shifted by looking at which six bit
// substring ended up at the top of the word.
// (Knuth, volume 4, section 7.3.1)
return int(deBruijn64tab[(x&-x)*deBruijn64>>(64-6)])
}
// --- OnesCount ---
const m0 = 0x5555555555555555 // 01010101 ...
const m1 = 0x3333333333333333 // 00110011 ...
const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
const m3 = 0x00ff00ff00ff00ff // etc.
const m4 = 0x0000ffff0000ffff
// OnesCount returns the number of one bits ("population count") in x.
func OnesCount(x: uint) => int {
if UintSize == 32 {
return OnesCount32(uint32(x))
}
return OnesCount64(uint64(x))
}
// OnesCount8 returns the number of one bits ("population count") in x.
func OnesCount8(x: uint8) => int {
return int(pop8tab[x])
}
// OnesCount16 returns the number of one bits ("population count") in x.
func OnesCount16(x: uint16) => int {
return int(pop8tab[x>>8] + pop8tab[x&0xff])
}
// OnesCount32 returns the number of one bits ("population count") in x.
func OnesCount32(x: uint32) => int {
return int(pop8tab[x>>24] + pop8tab[x>>16&0xff] + pop8tab[x>>8&0xff] + pop8tab[x&0xff])
}
// OnesCount64 returns the number of one bits ("population count") in x.
func OnesCount64(x: uint64) => int {
// Implementation: Parallel summing of adjacent bits.
// See "Hacker's Delight", Chap. 5: Counting Bits.
// The following pattern shows the general approach:
//
// x = x>>1&(m0&m) + x&(m0&m)
// x = x>>2&(m1&m) + x&(m1&m)
// x = x>>4&(m2&m) + x&(m2&m)
// x = x>>8&(m3&m) + x&(m3&m)
// x = x>>16&(m4&m) + x&(m4&m)
// x = x>>32&(m5&m) + x&(m5&m)
// return int(x)
//
// Masking (& operations) can be left away when there's no
// danger that a field's sum will carry over into the next
// field: Since the result cannot be > 64, 8 bits is enough
// and we can ignore the masks for the shifts by 8 and up.
// Per "Hacker's Delight", the first line can be simplified
// more, but it saves at best one instruction, so we leave
// it alone for clarity.
const m = 1<<64 - 1
x = x>>1&(m0&m) + x&(m0&m)
x = x>>2&(m1&m) + x&(m1&m)
x = (x>>4 + x) & (m2 & m)
x += x >> 8
x += x >> 16
x += x >> 32
return int(x) & (1<<7 - 1)
}
// --- RotateLeft ---
// RotateLeft returns the value of x rotated left by (k mod UintSize) bits.
// To rotate x right by k bits, call RotateLeft(x, -k).
//
// This function's execution time does not depend on the inputs.
func RotateLeft(x: uint, k: int) => uint {
if UintSize == 32 {
return uint(RotateLeft32(uint32(x), k))
}
return uint(RotateLeft64(uint64(x), k))
}
// RotateLeft8 returns the value of x rotated left by (k mod 8) bits.
// To rotate x right by k bits, call RotateLeft8(x, -k).
//
// This function's execution time does not depend on the inputs.
func RotateLeft8(x: uint8, k: int) => uint8 {
const n = 8
s := uint(k) & (n - 1)
return x<<s | x>>(n-s)
}
// RotateLeft16 returns the value of x rotated left by (k mod 16) bits.
// To rotate x right by k bits, call RotateLeft16(x, -k).
//
// This function's execution time does not depend on the inputs.
func RotateLeft16(x: uint16, k: int) => uint16 {
const n = 16
s := uint(k) & (n - 1)
return x<<s | x>>(n-s)
}
// RotateLeft32 returns the value of x rotated left by (k mod 32) bits.
// To rotate x right by k bits, call RotateLeft32(x, -k).
//
// This function's execution time does not depend on the inputs.
func RotateLeft32(x: uint32, k: int) => uint32 {
const n = 32
s := uint(k) & (n - 1)
return x<<s | x>>(n-s)
}
// RotateLeft64 returns the value of x rotated left by (k mod 64) bits.
// To rotate x right by k bits, call RotateLeft64(x, -k).
//
// This function's execution time does not depend on the inputs.
func RotateLeft64(x: uint64, k: int) => uint64 {
const n = 64
s := uint(k) & (n - 1)
return x<<s | x>>(n-s)
}
// --- Reverse ---
// Reverse returns the value of x with its bits in reversed order.
func Reverse(x: uint) => uint {
if UintSize == 32 {
return uint(Reverse32(uint32(x)))
}
return uint(Reverse64(uint64(x)))
}
// Reverse8 returns the value of x with its bits in reversed order.
func Reverse8(x: uint8) => uint8 {
return rev8tab[x]
}
// Reverse16 returns the value of x with its bits in reversed order.
func Reverse16(x: uint16) => uint16 {
return uint16(rev8tab[x>>8]) | uint16(rev8tab[x&0xff])<<8
}
// Reverse32 returns the value of x with its bits in reversed order.
func Reverse32(x: uint32) => uint32 {
const m = 1<<32 - 1
x = x>>1&(m0&m) | x&(m0&m)<<1
x = x>>2&(m1&m) | x&(m1&m)<<2
x = x>>4&(m2&m) | x&(m2&m)<<4
return ReverseBytes32(x)
}
// Reverse64 returns the value of x with its bits in reversed order.
func Reverse64(x: uint64) => uint64 {
const m = 1<<64 - 1
x = x>>1&(m0&m) | x&(m0&m)<<1
x = x>>2&(m1&m) | x&(m1&m)<<2
x = x>>4&(m2&m) | x&(m2&m)<<4
return ReverseBytes64(x)
}
// --- ReverseBytes ---
// ReverseBytes returns the value of x with its bytes in reversed order.
//
// This function's execution time does not depend on the inputs.
func ReverseBytes(x: uint) => uint {
if UintSize == 32 {
return uint(ReverseBytes32(uint32(x)))
}
return uint(ReverseBytes64(uint64(x)))
}
// ReverseBytes16 returns the value of x with its bytes in reversed order.
//
// This function's execution time does not depend on the inputs.
func ReverseBytes16(x: uint16) => uint16 {
return x>>8 | x<<8
}
// ReverseBytes32 returns the value of x with its bytes in reversed order.
//
// This function's execution time does not depend on the inputs.
func ReverseBytes32(x: uint32) => uint32 {
const m = 1<<32 - 1
x = x>>8&(m3&m) | x&(m3&m)<<8
return x>>16 | x<<16
}
// ReverseBytes64 returns the value of x with its bytes in reversed order.
//
// This function's execution time does not depend on the inputs.
func ReverseBytes64(x: uint64) => uint64 {
const m = 1<<64 - 1
x = x>>8&(m3&m) | x&(m3&m)<<8
x = x>>16&(m4&m) | x&(m4&m)<<16
return x>>32 | x<<32
}
// --- Len ---
// Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len(x: uint) => int {
if UintSize == 32 {
return Len32(uint32(x))
}
return Len64(uint64(x))
}
// Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len8(x: uint8) => int {
return int(len8tab[x])
}
// Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len16(x: uint16) => (n: int) {
if x >= 1<<8 {
x >>= 8
n = 8
}
return n + int(len8tab[x])
}
// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len32(x: uint32) => (n: int) {
if x >= 1<<16 {
x >>= 16
n = 16
}
if x >= 1<<8 {
x >>= 8
n += 8
}
return n + int(len8tab[x])
}
// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len64(x: uint64) => (n: int) {
if x >= 1<<32 {
x >>= 32
n = 32
}
if x >= 1<<16 {
x >>= 16
n += 16
}
if x >= 1<<8 {
x >>= 8
n += 8
}
return n + int(len8tab[x])
}
// --- Add with carry ---
// Add returns the sum with carry of x, y and carry: sum = x + y + carry.
// The carry input must be 0 or 1; otherwise the behavior is undefined.
// The carryOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Add(x, y, carry: uint) => (sum, carryOut: uint) {
if UintSize == 32 {
s32, c32 := Add32(uint32(x), uint32(y), uint32(carry))
return uint(s32), uint(c32)
}
s64, c64 := Add64(uint64(x), uint64(y), uint64(carry))
return uint(s64), uint(c64)
}
// Add32 returns the sum with carry of x, y and carry: sum = x + y + carry.
// The carry input must be 0 or 1; otherwise the behavior is undefined.
// The carryOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Add32(x, y, carry: uint32) => (sum, carryOut: uint32) {
sum64 := uint64(x) + uint64(y) + uint64(carry)
sum = uint32(sum64)
carryOut = uint32(sum64 >> 32)
return
}
// Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.
// The carry input must be 0 or 1; otherwise the behavior is undefined.
// The carryOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Add64(x, y, carry: uint64) => (sum, carryOut: uint64) {
sum = x + y + carry
// The sum will overflow if both top bits are set (x & y) or if one of them
// is (x | y), and a carry from the lower place happened. If such a carry
// happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum).
carryOut = ((x & y) | ((x | y) &^ sum)) >> 63
return
}
// --- Subtract with borrow ---
// Sub returns the difference of x, y and borrow: diff = x - y - borrow.
// The borrow input must be 0 or 1; otherwise the behavior is undefined.
// The borrowOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Sub(x, y, borrow: uint) => (diff, borrowOut: uint) {
if UintSize == 32 {
d32, b32 := Sub32(uint32(x), uint32(y), uint32(borrow))
return uint(d32), uint(b32)
}
d64, b64 := Sub64(uint64(x), uint64(y), uint64(borrow))
return uint(d64), uint(b64)
}
// Sub32 returns the difference of x, y and borrow, diff = x - y - borrow.
// The borrow input must be 0 or 1; otherwise the behavior is undefined.
// The borrowOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Sub32(x, y, borrow: uint32) => (diff, borrowOut: uint32) {
diff = x - y - borrow
// The difference will underflow if the top bit of x is not set and the top
// bit of y is set (^x & y) or if they are the same (^(x ^ y)) and a borrow
// from the lower place happens. If that borrow happens, the result will be
// 1 - 1 - 1 = 0 - 0 - 1 = 1 (& diff).
borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 31
return
}
// Sub64 returns the difference of x, y and borrow: diff = x - y - borrow.
// The borrow input must be 0 or 1; otherwise the behavior is undefined.
// The borrowOut output is guaranteed to be 0 or 1.
//
// This function's execution time does not depend on the inputs.
func Sub64(x, y, borrow: uint64) => (diff, borrowOut: uint64) {
diff = x - y - borrow
// See Sub32 for the bit logic.
borrowOut = ((^x & y) | (^(x ^ y) & diff)) >> 63
return
}
// --- Full-width multiply ---
// Mul returns the full-width product of x and y: (hi, lo) = x * y
// with the product bits' upper half returned in hi and the lower
// half returned in lo.
//
// This function's execution time does not depend on the inputs.
func Mul(x, y: uint) => (hi, lo: uint) {
if UintSize == 32 {
h, l := Mul32(uint32(x), uint32(y))
return uint(h), uint(l)
}
h, l := Mul64(uint64(x), uint64(y))
return uint(h), uint(l)
}
// Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y
// with the product bits' upper half returned in hi and the lower
// half returned in lo.
//
// This function's execution time does not depend on the inputs.
func Mul32(x, y: uint32) => (hi, lo: uint32) {
tmp := uint64(x) * uint64(y)
hi, lo = uint32(tmp>>32), uint32(tmp)
return
}
// Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y
// with the product bits' upper half returned in hi and the lower
// half returned in lo.
//
// This function's execution time does not depend on the inputs.
func Mul64(x, y: uint64) => (hi, lo: uint64) {
const mask32 = 1<<32 - 1
x0 := x & mask32
x1 := x >> 32
y0 := y & mask32
y1 := y >> 32
w0 := x0 * y0
t := x1*y0 + w0>>32
w1 := t & mask32
w2 := t >> 32
w1 += x0 * y1
hi = x1*y1 + w2 + w1>>32
lo = x * y
return
}
// --- Full-width divide ---
// Div returns the quotient and remainder of (hi, lo) divided by y:
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
// half in parameter hi and the lower half in parameter lo.
// Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
func Div(hi, lo, y: uint) => (quo, rem: uint) {
if UintSize == 32 {
q, r := Div32(uint32(hi), uint32(lo), uint32(y))
return uint(q), uint(r)
}
q, r := Div64(uint64(hi), uint64(lo), uint64(y))
return uint(q), uint(r)
}
// Div32 returns the quotient and remainder of (hi, lo) divided by y:
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
// half in parameter hi and the lower half in parameter lo.
// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
func Div32(hi, lo, y: uint32) => (quo, rem: uint32) {
if y != 0 && y <= hi {
// panic(overflowError)
}
z := uint64(hi)<<32 | uint64(lo)
quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
return
}
// Div64 returns the quotient and remainder of (hi, lo) divided by y:
// quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper
// half in parameter hi and the lower half in parameter lo.
// Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
func Div64(hi, lo, y: uint64) => (quo, rem: uint64) {
const (
two32 = 1 << 32
mask32 = two32 - 1
)
if y == 0 {
// panic(divideError)
}
if y <= hi {
// panic(overflowError)
}
s := uint(LeadingZeros64(y))
y <<= s
yn1 := y >> 32
yn0 := y & mask32
un32 := hi<<s | lo>>(64-s)
un10 := lo << s
un1 := un10 >> 32
un0 := un10 & mask32
q1 := un32 / yn1
rhat := un32 - q1*yn1
for q1 >= two32 || q1*yn0 > two32*rhat+un1 {
q1--
rhat += yn1
if rhat >= two32 {
break
}
}
un21 := un32*two32 + un1 - q1*y
q0 := un21 / yn1
rhat = un21 - q0*yn1
for q0 >= two32 || q0*yn0 > two32*rhat+un0 {
q0--
rhat += yn1
if rhat >= two32 {
break
}
}
return q1*two32 + q0, (un21*two32 + un0 - q0*y) >> s
}
// Rem returns the remainder of (hi, lo) divided by y. Rem panics for
// y == 0 (division by zero) but, unlike Div, it doesn't panic on a
// quotient overflow.
func Rem(hi, lo, y: uint) => uint {
if UintSize == 32 {
return uint(Rem32(uint32(hi), uint32(lo), uint32(y)))
}
return uint(Rem64(uint64(hi), uint64(lo), uint64(y)))
}
// Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics
// for y == 0 (division by zero) but, unlike Div32, it doesn't panic
// on a quotient overflow.
func Rem32(hi, lo, y: uint32) => uint32 {
return uint32((uint64(hi)<<32 | uint64(lo)) % uint64(y))
}
// Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics
// for y == 0 (division by zero) but, unlike Div64, it doesn't panic
// on a quotient overflow.
func Rem64(hi, lo, y: uint64) => uint64 {
// We scale down hi so that hi < y, then use Div64 to compute the
// rem with the guarantee that it won't panic on quotient overflow.
// Given that
// hi ≡ hi%y (mod y)
// we have
// hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y)
_, rem := Div64(hi%y, lo, y)
return rem
}
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build compiler_bootstrap
// +build compiler_bootstrap
// This version used only for bootstrap (on this path we want
// to avoid use of go:linkname as applied to variables).
type errorString struct {
msg string
}
func (e: errorString) RuntimeError() {}
func (e: errorString) Error => string {
return "runtime error: " + string(e.msg)
}
var overflowError = error(&errorString{"integer overflow"})
var divideError = error(&errorString{"integer divide by zero"})
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by go run make_tables.go. DO NOT EDIT.
const ntz8tab = "" +
"\x08\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x05\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x06\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x05\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x07\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x05\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x06\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x05\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00" +
"\x04\x00\x01\x00\x02\x00\x01\x00\x03\x00\x01\x00\x02\x00\x01\x00"
const pop8tab = "" +
"\x00\x01\x01\x02\x01\x02\x02\x03\x01\x02\x02\x03\x02\x03\x03\x04" +
"\x01\x02\x02\x03\x02\x03\x03\x04\x02\x03\x03\x04\x03\x04\x04\x05" +
"\x01\x02\x02\x03\x02\x03\x03\x04\x02\x03\x03\x04\x03\x04\x04\x05" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x01\x02\x02\x03\x02\x03\x03\x04\x02\x03\x03\x04\x03\x04\x04\x05" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x03\x04\x04\x05\x04\x05\x05\x06\x04\x05\x05\x06\x05\x06\x06\x07" +
"\x01\x02\x02\x03\x02\x03\x03\x04\x02\x03\x03\x04\x03\x04\x04\x05" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x03\x04\x04\x05\x04\x05\x05\x06\x04\x05\x05\x06\x05\x06\x06\x07" +
"\x02\x03\x03\x04\x03\x04\x04\x05\x03\x04\x04\x05\x04\x05\x05\x06" +
"\x03\x04\x04\x05\x04\x05\x05\x06\x04\x05\x05\x06\x05\x06\x06\x07" +
"\x03\x04\x04\x05\x04\x05\x05\x06\x04\x05\x05\x06\x05\x06\x06\x07" +
"\x04\x05\x05\x06\x05\x06\x06\x07\x05\x06\x06\x07\x06\x07\x07\x08"
const rev8tab = "" +
"\x00\x80\x40\xc0\x20\xa0\x60\xe0\x10\x90\x50\xd0\x30\xb0\x70\xf0" +
"\x08\x88\x48\xc8\x28\xa8\x68\xe8\x18\x98\x58\xd8\x38\xb8\x78\xf8" +
"\x04\x84\x44\xc4\x24\xa4\x64\xe4\x14\x94\x54\xd4\x34\xb4\x74\xf4" +
"\x0c\x8c\x4c\xcc\x2c\xac\x6c\xec\x1c\x9c\x5c\xdc\x3c\xbc\x7c\xfc" +
"\x02\x82\x42\xc2\x22\xa2\x62\xe2\x12\x92\x52\xd2\x32\xb2\x72\xf2" +
"\x0a\x8a\x4a\xca\x2a\xaa\x6a\xea\x1a\x9a\x5a\xda\x3a\xba\x7a\xfa" +
"\x06\x86\x46\xc6\x26\xa6\x66\xe6\x16\x96\x56\xd6\x36\xb6\x76\xf6" +
"\x0e\x8e\x4e\xce\x2e\xae\x6e\xee\x1e\x9e\x5e\xde\x3e\xbe\x7e\xfe" +
"\x01\x81\x41\xc1\x21\xa1\x61\xe1\x11\x91\x51\xd1\x31\xb1\x71\xf1" +
"\x09\x89\x49\xc9\x29\xa9\x69\xe9\x19\x99\x59\xd9\x39\xb9\x79\xf9" +
"\x05\x85\x45\xc5\x25\xa5\x65\xe5\x15\x95\x55\xd5\x35\xb5\x75\xf5" +
"\x0d\x8d\x4d\xcd\x2d\xad\x6d\xed\x1d\x9d\x5d\xdd\x3d\xbd\x7d\xfd" +
"\x03\x83\x43\xc3\x23\xa3\x63\xe3\x13\x93\x53\xd3\x33\xb3\x73\xf3" +
"\x0b\x8b\x4b\xcb\x2b\xab\x6b\xeb\x1b\x9b\x5b\xdb\x3b\xbb\x7b\xfb" +
"\x07\x87\x47\xc7\x27\xa7\x67\xe7\x17\x97\x57\xd7\x37\xb7\x77\xf7" +
"\x0f\x8f\x4f\xcf\x2f\xaf\x6f\xef\x1f\x9f\x5f\xdf\x3f\xbf\x7f\xff"
const len8tab = "" +
"\x00\x01\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04\x04\x04\x04\x04" +
"\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05" +
"\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06" +
"\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06" +
"\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07" +
"\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07" +
"\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07" +
"\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07\x07" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08" +
"\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08"
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
// This program generates example_test.go.
package main
import (
"bytes"
"fmt"
"log"
"math/bits"
"os"
)
const header = `// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by go run make_examples.go. DO NOT EDIT.
package bits_test
import (
"fmt"
"math/bits"
)
`
func main() {
w := bytes.NewBuffer([]byte(header))
for _, e := range []struct {
name string
in int
out [4]interface{}
out2 [4]interface{}
}{
{
name: "LeadingZeros",
in: 1,
out: [4]interface{}{bits.LeadingZeros8(1), bits.LeadingZeros16(1), bits.LeadingZeros32(1), bits.LeadingZeros64(1)},
},
{
name: "TrailingZeros",
in: 14,
out: [4]interface{}{bits.TrailingZeros8(14), bits.TrailingZeros16(14), bits.TrailingZeros32(14), bits.TrailingZeros64(14)},
},
{
name: "OnesCount",
in: 14,
out: [4]interface{}{bits.OnesCount8(14), bits.OnesCount16(14), bits.OnesCount32(14), bits.OnesCount64(14)},
},
{
name: "RotateLeft",
in: 15,
out: [4]interface{}{bits.RotateLeft8(15, 2), bits.RotateLeft16(15, 2), bits.RotateLeft32(15, 2), bits.RotateLeft64(15, 2)},
out2: [4]interface{}{bits.RotateLeft8(15, -2), bits.RotateLeft16(15, -2), bits.RotateLeft32(15, -2), bits.RotateLeft64(15, -2)},
},
{
name: "Reverse",
in: 19,
out: [4]interface{}{bits.Reverse8(19), bits.Reverse16(19), bits.Reverse32(19), bits.Reverse64(19)},
},
{
name: "ReverseBytes",
in: 15,
out: [4]interface{}{nil, bits.ReverseBytes16(15), bits.ReverseBytes32(15), bits.ReverseBytes64(15)},
},
{
name: "Len",
in: 8,
out: [4]interface{}{bits.Len8(8), bits.Len16(8), bits.Len32(8), bits.Len64(8)},
},
} {
for i, size := range []int{8, 16, 32, 64} {
if e.out[i] == nil {
continue // function doesn't exist
}
f := fmt.Sprintf("%s%d", e.name, size)
fmt.Fprintf(w, "\nfunc Example%s() {\n", f)
switch e.name {
case "RotateLeft", "Reverse", "ReverseBytes":
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", %d)\n", size, e.in)
if e.name == "RotateLeft" {
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, 2))\n", size, f, e.in)
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, -2))\n", size, f, e.in)
} else {
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d))\n", size, f, e.in)
}
fmt.Fprintf(w, "\t// Output:\n")
fmt.Fprintf(w, "\t// %0*b\n", size, e.in)
fmt.Fprintf(w, "\t// %0*b\n", size, e.out[i])
if e.name == "RotateLeft" && e.out2[i] != nil {
fmt.Fprintf(w, "\t// %0*b\n", size, e.out2[i])
}
default:
fmt.Fprintf(w, "\tfmt.Printf(\"%s(%%0%db) = %%d\\n\", %d, bits.%s(%d))\n", f, size, e.in, f, e.in)
fmt.Fprintf(w, "\t// Output:\n")
fmt.Fprintf(w, "\t// %s(%0*b) = %d\n", f, size, e.in, e.out[i])
}
fmt.Fprintf(w, "}\n")
}
}
if err := os.WriteFile("example_test.go", w.Bytes(), 0666); err != nil {
log.Fatal(err)
}
}
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
// This program generates bits_tables.go.
package main
import (
"bytes"
"fmt"
"go/format"
"io"
"log"
"os"
)
var header = []byte(`// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code generated by go run make_tables.go. DO NOT EDIT.
package bits
`)
func main() {
buf := bytes.NewBuffer(header)
gen(buf, "ntz8tab", ntz8)
gen(buf, "pop8tab", pop8)
gen(buf, "rev8tab", rev8)
gen(buf, "len8tab", len8)
out, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
}
err = os.WriteFile("bits_tables.go", out, 0666)
if err != nil {
log.Fatal(err)
}
}
func gen(w io.Writer, name string, f func(uint8) uint8) {
// Use a const string to allow the compiler to constant-evaluate lookups at constant index.
fmt.Fprintf(w, "const %s = \"\"+\n\"", name)
for i := 0; i < 256; i++ {
fmt.Fprintf(w, "\\x%02x", f(uint8(i)))
if i%16 == 15 && i != 255 {
fmt.Fprint(w, "\"+\n\"")
}
}
fmt.Fprint(w, "\"\n\n")
}
func ntz8(x uint8) (n uint8) {
for x&1 == 0 && n < 8 {
x >>= 1
n++
}
return
}
func pop8(x uint8) (n uint8) {
for x != 0 {
x &= x - 1
n++
}
return
}
func rev8(x uint8) (r uint8) {
for i := 8; i > 0; i-- {
r = r<<1 | x&1
x >>= 1
}
return
}
func len8(x uint8) (n uint8) {
for x != 0 {
x >>= 1
n++
}
return
}
// 版权 @2023 凹语言 作者。保留所有权利。
// 本文件定义凹语言本身依赖的最小函数列表, 内部函数
// runtime 只能依赖 syscall 包, 不得依赖其它包
// 该文件函数都是其它函数包装, 不含复杂实现逻辑
// TODO: 约定命名规则
// malloc/free
// puts/putchar
// args
// envs
// 版权 @2023 凹语言 作者。保留所有权利。 // 版权 @2023 凹语言 作者。保留所有权利。
var assciiTable = [...]string {
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\a", "\b", "\t", "\n", "\v", "\f", "\r", "\x0e", "\x0f", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "\u007f",
"\x80", "\x81", "\x82", "\x83", "\x84", "\x85", "\x86", "\x87", "\x88", "\x89", "\x8a", "\x8b", "\x8c", "\x8d", "\x8e", "\x8f", "\x90", "\x91", "\x92", "\x93", "\x94", "\x95", "\x96", "\x97", "\x98", "\x99", "\x9a", "\x9b", "\x9c", "\x9d", "\x9e", "\x9f", "\xa0", "\xa1", "\xa2", "\xa3", "\xa4", "\xa5", "\xa6", "\xa7", "\xa8", "\xa9", "\xaa", "\xab", "\xac", "\xad", "\xae", "\xaf", "\xb0", "\xb1", "\xb2", "\xb3", "\xb4", "\xb5", "\xb6", "\xb7", "\xb8", "\xb9", "\xba", "\xbb", "\xbc", "\xbd", "\xbe", "\xbf", "\xc0", "\xc1", "\xc2", "\xc3", "\xc4", "\xc5", "\xc6", "\xc7", "\xc8", "\xc9", "\xca", "\xcb", "\xcc", "\xcd", "\xce", "\xcf", "\xd0", "\xd1", "\xd2", "\xd3", "\xd4", "\xd5", "\xd6", "\xd7", "\xd8", "\xd9", "\xda", "\xdb", "\xdc", "\xdd", "\xde", "\xdf", "\xe0", "\xe1", "\xe2", "\xe3", "\xe4", "\xe5", "\xe6", "\xe7", "\xe8", "\xe9", "\xea", "\xeb", "\xec", "\xed", "\xee", "\xef", "\xf0", "\xf1", "\xf2", "\xf3", "\xf4", "\xf5", "\xf6", "\xf7", "\xf8", "\xf9", "\xfa", "\xfb", "\xfc", "\xfd", "\xfe", "\xff",
}
func bytes2str(b: []byte) => string {
var s: string
for _, x := range b {
s += assciiTable[x]
}
return s
}
func getCStr(b: []byte, start: i32) => string { func getCStr(b: []byte, start: i32) => string {
var end: i32 = i32(len(b)) var end: i32 = i32(len(b))
for i := start; i < end; i++ { for i := start; i < end; i++ {
...@@ -21,7 +8,7 @@ func getCStr(b: []byte, start: i32) => string { ...@@ -21,7 +8,7 @@ func getCStr(b: []byte, start: i32) => string {
break break
} }
} }
return bytes2str(b[start:end]) return string(b[start:end])
} }
func os_get_args() => []string { func os_get_args() => []string {
......
...@@ -69,6 +69,11 @@ func waPrintI32(i: i32) { ...@@ -69,6 +69,11 @@ func waPrintI32(i: i32) {
print_i32(i) print_i32(i)
} }
#wa:linkname $runtime.waPrintU32
func waPrintU32(i: u32) {
print_u32(i)
}
#wa:linkname $runtime.waPrintRune #wa:linkname $runtime.waPrintRune
func waPrintRune(ch: i32) { func waPrintRune(ch: i32) {
putchar(ch) putchar(ch)
...@@ -96,3 +101,12 @@ func print_i32(i: i32) { ...@@ -96,3 +101,12 @@ func print_i32(i: i32) {
print_i32(i/10) print_i32(i/10)
print_i32(i%10) print_i32(i%10)
} }
func print_u32(i: u32) {
if i < 10 {
putchar(i32(i)+'0')
return
}
print_u32(i/10)
print_u32(i%10)
}
...@@ -99,8 +99,11 @@ func IsStdPkg(pkgpath string) bool { ...@@ -99,8 +99,11 @@ func IsStdPkg(pkgpath string) bool {
return false return false
} }
func GetStdPkgList() []string {
return append([]string{}, stdPkgs...)
}
var stdPkgs = []string{ var stdPkgs = []string{
"arduino",
"binary", "binary",
"bytes", "bytes",
"errors", "errors",
...@@ -113,6 +116,7 @@ var stdPkgs = []string{ ...@@ -113,6 +116,7 @@ var stdPkgs = []string{
"image/color/palette", "image/color/palette",
"io", "io",
"math", "math",
"math/bits",
"os", "os",
"reflect", "reflect",
"regexp", "regexp",
...@@ -121,7 +125,7 @@ var stdPkgs = []string{ ...@@ -121,7 +125,7 @@ var stdPkgs = []string{
"strconv", "strconv",
"strings", "strings",
"syscall", "syscall",
"text", "text/template",
"unicode", "unicode",
"unicode/utf8", "unicode/utf8",
} }
......