Skip to content

Commit

Permalink
cxgo: use unsafe.Add; resolves #34
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 14, 2021
1 parent 8d378a6 commit 874cdf3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
41 changes: 20 additions & 21 deletions pointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,19 +705,19 @@ func (e *PtrOffset) parts() (GoExpr, BinaryOp, GoExpr) {
if e.X.PtrType(nil).Elem() != nil {
x = call(unsafePtr(), x)
}
x = call(ident("uintptr"), x)
return x, op, y
}

func (e *PtrOffset) AsExpr() GoExpr {
x, tok, y := e.parts()
//if !VirtualPtrs {
x = &ast.BinaryExpr{
X: x, Op: tok.GoToken(), Y: y,
if tok != BinOpAdd {
y = &ast.UnaryExpr{Op: token.SUB, X: y}
}
x = call(ident("unsafe.Add"), x, y)
//if !VirtualPtrs {
to := e.toType()
if to.Elem() != nil {
x = call(unsafePtr(), x)
if to.Elem() == nil {
return x
}
return call(to.GoType(), x)
//}
Expand Down Expand Up @@ -801,13 +801,12 @@ func (e *PtrElemOffset) AsExpr() GoExpr {
Y: call(ident("uintptr"), y),
}
}
x = &ast.BinaryExpr{
X: call(ident("uintptr"), x),
Op: op.GoToken(),
Y: y,
if op == BinOpSub {
y = &ast.UnaryExpr{Op: token.SUB, X: y}
}
if to.Elem() != nil {
x = call(unsafePtr(), x)
x = call(ident("unsafe.Add"), x, y)
if to.Elem() == nil {
return x
}
return call(to.GoType(), x)
//}
Expand Down Expand Up @@ -875,22 +874,22 @@ func (e *PtrVarOffset) AsExpr() GoExpr {
if e.X.PtrType(nil).Elem() != nil {
x = call(unsafePtr(), x)
}
if !e.Ind.CType(nil).Kind().IsInt() {
y = call(ident("uintptr"), y)
}
if mul != 1 {
y = &ast.BinaryExpr{
X: intLit(mul),
Op: token.MUL,
Y: call(ident("uintptr"), y),
Y: y,
}
} else {
y = call(ident("uintptr"), y)
}
x = &ast.BinaryExpr{
X: call(ident("uintptr"), x),
Op: op.GoToken(),
Y: y,
if op == BinOpSub {
y = &ast.UnaryExpr{Op: token.SUB, X: y}
}
if to.Elem() != nil {
x = call(unsafePtr(), x)
x = call(ident("unsafe.Add"), x, y)
if to.Elem() == nil {
return x
}
return call(to.GoType(), x)
//}
Expand Down
24 changes: 12 additions & 12 deletions pointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ void foo() {
exp: `
func foo() {
var a *byte
a = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(a)) + 1))
a = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(a)) - 1))
a = (*byte)(unsafe.Add(unsafe.Pointer(a), 1))
a = (*byte)(unsafe.Add(unsafe.Pointer(a), -1))
}
`,
},
Expand All @@ -192,10 +192,10 @@ func foo() {
a int32 = 2
pa unsafe.Pointer
)
pa = unsafe.Pointer(uintptr(pa) + uintptr(a))
pa = unsafe.Pointer(uintptr(pa) + 3)
pa = unsafe.Pointer(uintptr(pa) + uintptr(a))
pa = unsafe.Pointer(uintptr(pa) + 5)
pa = unsafe.Add(pa, a)
pa = unsafe.Add(pa, 3)
pa = unsafe.Add(pa, a)
pa = unsafe.Add(pa, 5)
}
`,
},
Expand Down Expand Up @@ -329,10 +329,10 @@ func foo() {
a int32 = 2
pa *int32
)
pa = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(pa)) + unsafe.Sizeof(int32(0))*uintptr(a)))
pa = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(pa)) + unsafe.Sizeof(int32(0))*3))
pa = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(pa)) + unsafe.Sizeof(int32(0))*uintptr(a)))
pa = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(pa)) + unsafe.Sizeof(int32(0))*5))
pa = (*int32)(unsafe.Add(unsafe.Pointer(pa), unsafe.Sizeof(int32(0))*uintptr(a)))
pa = (*int32)(unsafe.Add(unsafe.Pointer(pa), unsafe.Sizeof(int32(0))*3))
pa = (*int32)(unsafe.Add(unsafe.Pointer(pa), unsafe.Sizeof(int32(0))*uintptr(a)))
pa = (*int32)(unsafe.Add(unsafe.Pointer(pa), unsafe.Sizeof(int32(0))*5))
}
`,
},
Expand Down Expand Up @@ -621,7 +621,7 @@ func foo() {
*func() *int32 {
p := &a
x := *p
*p = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(*p)) + unsafe.Sizeof(int32(0))*1))
*p = (*int32)(unsafe.Add(unsafe.Pointer(*p), unsafe.Sizeof(int32(0))*1))
return x
}() = b
}
Expand Down Expand Up @@ -1117,7 +1117,7 @@ type A struct {
func foo() {
var ptr *int32
ptr = (*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + unsafe.Sizeof(int32(0))*(20*(unsafe.Sizeof(A{})/2))))
ptr = (*int32)(unsafe.Add(unsafe.Pointer(ptr), unsafe.Sizeof(int32(0))*(20*(unsafe.Sizeof(A{})/2))))
}
`,
},
Expand Down

0 comments on commit 874cdf3

Please sign in to comment.