Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
implicitCast
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 12, 2022
1 parent b5badec commit 37bc26b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cl/codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ func binaryOp(ctx *blockCtx, op token.Token, v *cast.Node) {
if isInteger(t) { // bool => int
args := stk.GetArgs(2)
if isBool(args[0].Type) {
args[0] = castFromBoolExpr(ctx, t, args[0])
args[0] = castFromBoolExpr(cb, t, args[0])
}
if isBool(args[1].Type) {
args[1] = castFromBoolExpr(ctx, t, args[1])
args[1] = castFromBoolExpr(cb, t, args[1])
}
}
cb.BinaryOp(op, src)
Expand Down Expand Up @@ -348,10 +348,10 @@ func castToBoolExpr(cb *gox.CodeBuilder) {
}
}

func castFromBoolExpr(ctx *blockCtx, typ types.Type, v *gox.Element) *gox.Element {
pkg := ctx.pkg
func castFromBoolExpr(cb *gox.CodeBuilder, typ types.Type, v *gox.Element) *gox.Element {
pkg := cb.Pkg()
results := types.NewTuple(types.NewParam(token.NoPos, pkg.Types, "", typ))
return ctx.cb.NewClosure(nil, results, false).BodyStart(pkg).
return cb.NewClosure(nil, results, false).BodyStart(pkg).
If().Val(v).Then().Val(1).Return(1).
Else().Val(0).Return(1).
End().
Expand Down
18 changes: 16 additions & 2 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,29 @@ func NewPackage(pkgPath, pkgName string, file *ast.Node, conf *Config) (p *gox.P
HandleErr: nil,
NodeInterpreter: nil,
NewBuiltin: nil,
// CanImplicitCast: implicitCast,
CanImplicitCast: implicitCast,
}
p = gox.NewPackage(pkgPath, pkgName, confGox)
err = loadFile(p, file)
return
}

func implicitCast(pkg *gox.Package, V, T types.Type, pv *gox.Element) bool {
log.Panicln("==> implicitCast:", V, T)
switch t := T.(type) {
case *types.Basic:
if (t.Info() & types.IsUntyped) != 0 { // untyped
return false
}
if (t.Info() & types.IsInteger) != 0 { // int type
if isBool(V) {
pv.Type, pv.Val = T, castFromBoolExpr(pkg.CB(), T, pv).Val
return true
}
}
case *types.Pointer:
return false
}
log.Panicln("==> implicitCast:", V, "to:", T)
return false
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/c2go/c2go.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {

if *verbose {
cl.SetDebug(cl.DbgFlagAll)
gox.SetDebug(gox.DbgFlagInstruction)
gox.SetDebug(gox.DbgFlagInstruction | gox.DbgFlagMatch)
}

outfile := infile
Expand Down
4 changes: 4 additions & 0 deletions testdata/atomic/libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func printf(format *int8, args ...interface{}) int32 {
return 0
}

func __atomic_store_n_i32(p *int32, memorder int32, v int32) {
*p = v
}

func __atomic_store_n_i64(p *int64, memorder int32, v int64) {
*p = v
}
Expand Down

0 comments on commit 37bc26b

Please sign in to comment.