merge upstream

This commit is contained in:
xushiwei
2024-06-17 19:33:27 +08:00
8 changed files with 188 additions and 114 deletions

View File

@@ -32,10 +32,10 @@ jobs:
- name: Install LLVM ${{ matrix.llvm }} and libgc-dev - name: Install LLVM ${{ matrix.llvm }} and libgc-dev
if: startsWith(matrix.os, 'ubuntu') if: startsWith(matrix.os, 'ubuntu')
run: | run: |
echo 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${{ matrix.llvm }} main' | sudo tee /etc/apt/sources.list.d/llvm.list echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{ matrix.llvm }} main" | sudo tee /etc/apt/sources.list.d/llvm.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update sudo apt-get update
sudo apt-get install --no-install-recommends clang-${{ matrix.llvm }} llvm-${{ matrix.llvm }}-dev libgc-dev sudo apt-get install -y llvm-${{ matrix.llvm }}-dev clang-${{ matrix.llvm }} lld-${{ matrix.llvm }} pkg-config libgc-dev libcjson-dev libsqlite3-dev python3.11-dev
echo /usr/lib/llvm-${{ matrix.llvm }}/bin >> $GITHUB_PATH echo /usr/lib/llvm-${{ matrix.llvm }}/bin >> $GITHUB_PATH
- name: Clang information - name: Clang information

View File

@@ -211,6 +211,18 @@ Here are the Go packages that can be imported correctly:
* [sync/atomic](https://pkg.go.dev/sync/atomic) (partially) * [sync/atomic](https://pkg.go.dev/sync/atomic) (partially)
## Dependencies
- [Go 1.20+](https://go.dev) (build only)
- [LLVM 17](https://llvm.org)
- [Clang 17](https://clang.llvm.org)
- [LLD 17](https://lld.llvm.org)
- [pkg-config 0.29+](https://www.freedesktop.org/wiki/Software/pkg-config/)
- [bdwgc/libgc 8.0+](https://www.hboehm.info/gc/)
- [cJSON 1.7+](https://github.com/DaveGamble/cJSON) (optional, for [`github.com/goplus/llgo/c/cjson`](https://pkg.go.dev/github.com/goplus/llgo/c/cjson))
- [SQLite 3](https://www.sqlite.org) (optional, for [`github.com/goplus/llgo/c/sqlite`](https://pkg.go.dev/github.com/goplus/llgo/c/sqlite))
- [Python 3.11+](https://www.python.org) (optional, for [`github.com/goplus/llgo/py`](https://pkg.go.dev/github.com/goplus/llgo/py))
## How to install ## How to install
Follow these steps to generate the `llgo` command (its usage is the same as the `go` command): Follow these steps to generate the `llgo` command (its usage is the same as the `go` command):
@@ -218,20 +230,24 @@ Follow these steps to generate the `llgo` command (its usage is the same as the
### on macOS ### on macOS
```sh ```sh
brew update # execute if needed brew update # execute if needed
brew install libgc brew install llvm@17 pkg-config libgc
brew install llvm@17 brew install cjson sqlite python@3.12 # optional
export PATH=$(brew --prefix llvm@17)/bin:$PATH # you may want to add this to your shell RC file, e.g. ~/.zshrc
export CC=clang CXX=clang++ # only for go build; optional if you have other compatible compilers
go install -v ./... go install -v ./...
``` ```
### on Linux ### on Linux (Debian/Ubuntu)
```sh ```sh
echo 'deb http://apt.llvm.org/focal/ llvm-toolchain-focal-17 main' | sudo tee /etc/apt/sources.list.d/llvm.list echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-17 main" | sudo tee /etc/apt/sources.list.d/llvm.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update # execute if needed sudo apt-get update # execute if needed
sudo apt-get install libgc-dev sudo apt-get install -y llvm-17-dev clang-17 lld-17 pkg-config libgc-dev
sudo apt-get install --no-install-recommends llvm-17-dev sudo apt-get install -y libcjson-dev libsqlite3-dev python3.12-dev # optional
export PATH=/usr/lib/llvm-17/bin:$PATH # you may want to add this to your shell RC file, e.g. ~/.bashrc
export CC=clang CXX=clang++ # only for go build; optional if you have other compatible compilers
go install -v ./... go install -v ./...
``` ```
@@ -251,6 +267,7 @@ TODO
How do I generate these tools? How do I generate these tools?
```sh ```sh
export CC=clang CXX=clang++ # only for go build; optional if you have other compatible compilers
go install -v ./... # compile all tools except pydump go install -v ./... # compile all tools except pydump
cd chore/_xtool cd chore/_xtool
llgo install ./... # compile pydump llgo install ./... # compile pydump

View File

@@ -1,25 +1,33 @@
package main package main
import "github.com/goplus/llgo/c"
type point struct { type point struct {
x int x int
y int y int
} }
type N [2]int
type T *N
type S []int
func main() { func main() {
a := [...]point{{1, 2}, {3, 4}, {5, 6}}[2] a := [...]point{{1, 2}, {3, 4}, {5, 6}}[2]
c.Printf(c.Str("%d %d\n"), a.x, a.y) println(a.x, a.y)
b := [...][2]int{[2]int{1, 2}, [2]int{3, 4}}[1] b := [...][2]int{[2]int{1, 2}, [2]int{3, 4}}[1]
c.Printf(c.Str("%d %d\n"), b[0], b[1]) println(b[0], b[1])
var i int = 2 var i int = 2
n := [...]int{1, 2, 3, 4, 5}[i] println([...]int{1, 2, 3, 4, 5}[i])
c.Printf(c.Str("%d\n"), n)
c.Printf(c.Str("%d\n"), [...]int{1, 2, 3, 4, 5}[i])
s := "123456" s := "123456"
c.Printf(c.Str("%c\n"), s[i]) println(string(s[i]))
c.Printf(c.Str("%c\n"), "123456"[1]) println(string("123456"[1]))
var n = N{1, 2}
var t T = &n
println(t[1])
var s1 = S{1, 2, 3, 4}
println(s1[1])
println([2]int{}[0])
} }

View File

@@ -3,17 +3,12 @@ source_filename = "main"
%main.point = type { i64, i64 } %main.point = type { i64, i64 }
%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 } %"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }
%"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 }
@"main.init$guard" = global i1 false, align 1 @"main.init$guard" = global i1 false, align 1
@__llgo_argc = global i32 0, align 4 @__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8 @__llgo_argv = global ptr null, align 8
@0 = private unnamed_addr constant [7 x i8] c"%d %d\0A\00", align 1 @0 = private unnamed_addr constant [6 x i8] c"123456", align 1
@1 = private unnamed_addr constant [7 x i8] c"%d %d\0A\00", align 1
@2 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@3 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@4 = private unnamed_addr constant [4 x i8] c"%c\0A\00", align 1
@5 = private unnamed_addr constant [6 x i8] c"123456", align 1
@6 = private unnamed_addr constant [4 x i8] c"%c\0A\00", align 1
define void @main.init() { define void @main.init() {
_llgo_0: _llgo_0:
@@ -61,82 +56,112 @@ _llgo_0:
%19 = load i64, ptr %18, align 4 %19 = load i64, ptr %18, align 4
%20 = getelementptr inbounds %main.point, ptr %3, i32 0, i32 1 %20 = getelementptr inbounds %main.point, ptr %3, i32 0, i32 1
%21 = load i64, ptr %20, align 4 %21 = load i64, ptr %20, align 4
%22 = call i32 (ptr, ...) @printf(ptr @0, i64 %19, i64 %21) call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %19)
%23 = alloca [2 x i64], align 8 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
%24 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %23, i64 16) call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %21)
%25 = alloca [2 x [2 x i64]], align 8 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%26 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %25, i64 32) %22 = alloca [2 x i64], align 8
%27 = getelementptr inbounds [2 x i64], ptr %26, i64 0 %23 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %22, i64 16)
%28 = getelementptr inbounds i64, ptr %27, i64 0 %24 = alloca [2 x [2 x i64]], align 8
%29 = getelementptr inbounds i64, ptr %27, i64 1 %25 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %24, i64 32)
%30 = getelementptr inbounds [2 x i64], ptr %26, i64 1 %26 = getelementptr inbounds [2 x i64], ptr %25, i64 0
%31 = getelementptr inbounds i64, ptr %30, i64 0 %27 = getelementptr inbounds i64, ptr %26, i64 0
%32 = getelementptr inbounds i64, ptr %30, i64 1 %28 = getelementptr inbounds i64, ptr %26, i64 1
store i64 1, ptr %28, align 4 %29 = getelementptr inbounds [2 x i64], ptr %25, i64 1
store i64 2, ptr %29, align 4 %30 = getelementptr inbounds i64, ptr %29, i64 0
store i64 3, ptr %31, align 4 %31 = getelementptr inbounds i64, ptr %29, i64 1
store i64 4, ptr %32, align 4 store i64 1, ptr %27, align 4
%33 = load [2 x [2 x i64]], ptr %26, align 4 store i64 2, ptr %28, align 4
%34 = getelementptr inbounds [2 x i64], ptr %26, i64 1 store i64 3, ptr %30, align 4
%35 = load [2 x i64], ptr %34, align 4 store i64 4, ptr %31, align 4
store [2 x i64] %35, ptr %24, align 4 %32 = load [2 x [2 x i64]], ptr %25, align 4
%36 = getelementptr inbounds i64, ptr %24, i64 0 %33 = getelementptr inbounds [2 x i64], ptr %25, i64 1
%37 = load i64, ptr %36, align 4 %34 = load [2 x i64], ptr %33, align 4
%38 = getelementptr inbounds i64, ptr %24, i64 1 store [2 x i64] %34, ptr %23, align 4
%39 = load i64, ptr %38, align 4 %35 = getelementptr inbounds i64, ptr %23, i64 0
%40 = call i32 (ptr, ...) @printf(ptr @1, i64 %37, i64 %39) %36 = load i64, ptr %35, align 4
%41 = alloca [5 x i64], align 8 %37 = getelementptr inbounds i64, ptr %23, i64 1
%42 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %41, i64 40) %38 = load i64, ptr %37, align 4
%43 = getelementptr inbounds i64, ptr %42, i64 0 call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %36)
%44 = getelementptr inbounds i64, ptr %42, i64 1 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
%45 = getelementptr inbounds i64, ptr %42, i64 2 call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %38)
%46 = getelementptr inbounds i64, ptr %42, i64 3 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%47 = getelementptr inbounds i64, ptr %42, i64 4 %39 = alloca [5 x i64], align 8
store i64 1, ptr %43, align 4 %40 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %39, i64 40)
store i64 2, ptr %44, align 4 %41 = getelementptr inbounds i64, ptr %40, i64 0
store i64 3, ptr %45, align 4 %42 = getelementptr inbounds i64, ptr %40, i64 1
store i64 4, ptr %46, align 4 %43 = getelementptr inbounds i64, ptr %40, i64 2
store i64 5, ptr %47, align 4 %44 = getelementptr inbounds i64, ptr %40, i64 3
%48 = load [5 x i64], ptr %42, align 4 %45 = getelementptr inbounds i64, ptr %40, i64 4
%49 = getelementptr inbounds i64, ptr %42, i64 2 store i64 1, ptr %41, align 4
%50 = load i64, ptr %49, align 4 store i64 2, ptr %42, align 4
%51 = call i32 (ptr, ...) @printf(ptr @2, i64 %50) store i64 3, ptr %43, align 4
%52 = alloca [5 x i64], align 8 store i64 4, ptr %44, align 4
%53 = call ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr %52, i64 40) store i64 5, ptr %45, align 4
%54 = getelementptr inbounds i64, ptr %53, i64 0 %46 = load [5 x i64], ptr %40, align 4
%55 = getelementptr inbounds i64, ptr %53, i64 1 %47 = getelementptr inbounds i64, ptr %40, i64 2
%56 = getelementptr inbounds i64, ptr %53, i64 2 %48 = load i64, ptr %47, align 4
%57 = getelementptr inbounds i64, ptr %53, i64 3 call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %48)
%58 = getelementptr inbounds i64, ptr %53, i64 4 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
store i64 1, ptr %54, align 4 %49 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
store i64 2, ptr %55, align 4 %50 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %49, i32 0, i32 0
store i64 3, ptr %56, align 4 store ptr @0, ptr %50, align 8
store i64 4, ptr %57, align 4 %51 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %49, i32 0, i32 1
store i64 5, ptr %58, align 4 store i64 6, ptr %51, align 4
%59 = load [5 x i64], ptr %53, align 4 %52 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %49, align 8
%60 = getelementptr inbounds i64, ptr %53, i64 2 %53 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %52, 0
%61 = load i64, ptr %60, align 4 %54 = getelementptr inbounds i8, ptr %53, i64 2
%62 = call i32 (ptr, ...) @printf(ptr @3, i64 %61) %55 = load i8, ptr %54, align 1
%63 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 %56 = sext i8 %55 to i32
%64 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %63, i32 0, i32 0 %57 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringFromRune"(i32 %56)
store ptr @5, ptr %64, align 8 call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %57)
%65 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %63, i32 0, i32 1 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
store i64 6, ptr %65, align 4 %58 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%66 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %63, align 8 %59 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %58, i32 0, i32 0
%67 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %66, 0 store ptr @0, ptr %59, align 8
%68 = getelementptr inbounds i8, ptr %67, i64 2 %60 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %58, i32 0, i32 1
%69 = load i8, ptr %68, align 1 store i64 6, ptr %60, align 4
%70 = call i32 (ptr, ...) @printf(ptr @4, i8 %69) %61 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %58, align 8
%71 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8 %62 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %61, 0
%72 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %71, i32 0, i32 0 %63 = getelementptr inbounds i8, ptr %62, i64 1
store ptr @5, ptr %72, align 8 %64 = load i8, ptr %63, align 1
%73 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %71, i32 0, i32 1 %65 = sext i8 %64 to i32
store i64 6, ptr %73, align 4 %66 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringFromRune"(i32 %65)
%74 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %71, align 8 call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %66)
%75 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %74, 0 call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%76 = getelementptr inbounds i8, ptr %75, i64 1 %67 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
%77 = load i8, ptr %76, align 1 %68 = getelementptr inbounds i64, ptr %67, i64 0
%78 = call i32 (ptr, ...) @printf(ptr @6, i8 %77) %69 = getelementptr inbounds i64, ptr %67, i64 1
store i64 1, ptr %68, align 4
store i64 2, ptr %69, align 4
%70 = getelementptr inbounds i64, ptr %67, i64 1
%71 = load i64, ptr %70, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %71)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
%72 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 32)
%73 = getelementptr inbounds i64, ptr %72, i64 0
store i64 1, ptr %73, align 4
%74 = getelementptr inbounds i64, ptr %72, i64 1
store i64 2, ptr %74, align 4
%75 = getelementptr inbounds i64, ptr %72, i64 2
store i64 3, ptr %75, align 4
%76 = getelementptr inbounds i64, ptr %72, i64 3
store i64 4, ptr %76, align 4
%77 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
%78 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %77, i32 0, i32 0
store ptr %72, ptr %78, align 8
%79 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %77, i32 0, i32 1
store i64 4, ptr %79, align 4
%80 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %77, i32 0, i32 2
store i64 4, ptr %80, align 4
%81 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %77, align 8
%82 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %81, 0
%83 = getelementptr inbounds i64, ptr %82, i64 1
%84 = load i64, ptr %83, align 4
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %84)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 0)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret i32 0 ret i32 0
} }
@@ -144,4 +169,12 @@ declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64) declare ptr @"github.com/goplus/llgo/internal/runtime.Zeroinit"(ptr, i64)
declare i32 @printf(ptr, ...) declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)
declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)
declare %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringFromRune"(i32)
declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)

View File

@@ -757,10 +757,14 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
case *ssa.Index: case *ssa.Index:
x := p.compileValue(b, v.X) x := p.compileValue(b, v.X)
idx := p.compileValue(b, v.Index) idx := p.compileValue(b, v.Index)
ret = b.Index(x, idx, func(e llssa.Expr) (ret llssa.Expr) { ret = b.Index(x, idx, func(e llssa.Expr) (ret llssa.Expr, zero bool) {
if e == x { if e == x {
if n, ok := v.X.(*ssa.UnOp); ok { switch n := v.X.(type) {
return p.compileValue(b, n.X) case *ssa.Const:
zero = true
return
case *ssa.UnOp:
return p.compileValue(b, n.X), false
} }
} }
panic(fmt.Errorf("todo: addr of %v", e)) panic(fmt.Errorf("todo: addr of %v", e))

View File

@@ -290,7 +290,7 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
if app == "" { if app == "" {
app = filepath.Join(conf.BinPath, name+conf.AppExt) app = filepath.Join(conf.BinPath, name+conf.AppExt)
} }
const N = 5 const N = 6
args := make([]string, N, len(pkg.Imports)+len(llFiles)+(N+1)) args := make([]string, N, len(pkg.Imports)+len(llFiles)+(N+1))
args[0] = "-o" args[0] = "-o"
args[1] = app args[1] = app
@@ -298,11 +298,13 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf
args[3] = "-Xlinker" args[3] = "-Xlinker"
if runtime.GOOS == "darwin" { // ld64.lld (macOS) if runtime.GOOS == "darwin" { // ld64.lld (macOS)
args[4] = "-dead_strip" args[4] = "-dead_strip"
args[5] = "" // It's ok to leave it empty, as we can assume libpthread is built-in on macOS.
} else { // ld.lld (Unix), lld-link (Windows), wasm-ld (WebAssembly) } else { // ld.lld (Unix), lld-link (Windows), wasm-ld (WebAssembly)
args[4] = "--gc-sections" args[4] = "--gc-sections"
args[5] = "-lpthread" // libpthread is built-in since glibc 2.34 (2021-08-01); we need to support earlier versions.
} }
//args[5] = "-fuse-ld=lld" // TODO(xsw): to check lld exists or not //args[6] = "-fuse-ld=lld" // TODO(xsw): to check lld exists or not
//args[6] = "-O2" //args[7] = "-O2"
needRuntime := false needRuntime := false
needPyInit := false needPyInit := false
packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) { packages.Visit([]*packages.Package{pkg}, nil, func(p *packages.Package) {

View File

@@ -163,13 +163,14 @@ func (b Builder) checkIndex(idx Expr) Expr {
// Example printed form: // Example printed form:
// //
// t2 = t0[t1] // t2 = t0[t1]
func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr { func (b Builder) Index(x, idx Expr, addr func(Expr) (Expr, bool)) Expr {
if debugInstr { if debugInstr {
log.Printf("Index %v, %v\n", x.impl, idx.impl) log.Printf("Index %v, %v\n", x.impl, idx.impl)
} }
prog := b.Prog prog := b.Prog
var telem Type var telem Type
var ptr Expr var ptr Expr
var zero bool
switch t := x.raw.Type.Underlying().(type) { switch t := x.raw.Type.Underlying().(type) {
case *types.Basic: case *types.Basic:
if t.Kind() != types.String { if t.Kind() != types.String {
@@ -180,7 +181,7 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
case *types.Array: case *types.Array:
telem = prog.Index(x.Type) telem = prog.Index(x.Type)
if addr != nil { if addr != nil {
ptr = addr(x) ptr, zero = addr(x)
} else { } else {
/* /*
size := SizeOf(prog, telem, t.Len()) size := SizeOf(prog, telem, t.Len())
@@ -190,7 +191,11 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
panic("unreachable") panic("unreachable")
} }
} }
// TODO check range
idx = b.checkIndex(idx) idx = b.checkIndex(idx)
if zero {
return Expr{llvm.ConstNull(telem.ll), telem}
}
pt := prog.Pointer(telem) pt := prog.Pointer(telem)
indices := []llvm.Value{idx.impl} indices := []llvm.Value{idx.impl}
buf := Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, ptr.impl, indices), pt} buf := Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, ptr.impl, indices), pt}

View File

@@ -61,11 +61,16 @@ const (
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
func indexType(t types.Type) types.Type { func indexType(t types.Type) types.Type {
switch t := t.(type) { typ := t
retry:
switch t := typ.(type) {
case *types.Named:
typ = t.Underlying()
goto retry
case *types.Slice: case *types.Slice:
return t.Elem() return t.Elem()
case *types.Pointer: case *types.Pointer:
switch t := t.Elem().(type) { switch t := t.Elem().Underlying().(type) {
case *types.Array: case *types.Array:
return t.Elem() return t.Elem()
} }
@@ -193,7 +198,7 @@ func (p Program) Pointer(typ Type) Type {
} }
func (p Program) Elem(typ Type) Type { func (p Program) Elem(typ Type) Type {
elem := typ.raw.Type.(interface { elem := typ.raw.Type.Underlying().(interface {
Elem() types.Type Elem() types.Type
}).Elem() }).Elem()
return p.rawType(elem) return p.rawType(elem)