diff --git a/cl/codebuild.go b/cl/codebuild.go index 8ac7656..5fc260b 100644 --- a/cl/codebuild.go +++ b/cl/codebuild.go @@ -429,6 +429,10 @@ func binaryOp(ctx *blockCtx, op token.Token, v *cast.Node) { adjustIntConst(ctx, ret, ret.Type) } +func compareOp(ctx *blockCtx, op token.Token, src ast.Node) { + ctx.cb.BinaryOp(op, src) +} + func untypedZeroToNil(v *gox.Element) { v.Type = types.Typ[types.UntypedNil] v.Val = &ast.Ident{Name: "nil"} diff --git a/cl/expr.go b/cl/expr.go index 805eabc..a642baa 100644 --- a/cl/expr.go +++ b/cl/expr.go @@ -272,6 +272,11 @@ func compileCallExpr(ctx *blockCtx, v *ast.Node) { compileExpr(ctx, v.Inner[2]) cb.Assign(1) return + case "__builtin_expect": + compileExpr(ctx, v.Inner[1]) + compileExpr(ctx, v.Inner[2]) + compareOp(ctx, token.EQL, ctx.goNode(v)) + return } } for i := 0; i < n; i++ { diff --git a/testdata/operator/operator.c b/testdata/operator/operator.c index 2662ca3..61b1e93 100644 --- a/testdata/operator/operator.c +++ b/testdata/operator/operator.c @@ -1,6 +1,9 @@ #include #include +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + void f() { double a; float b; @@ -14,6 +17,12 @@ int g(int ret) { return ret; } +void test() { + if (likely(g(100))) { + printf("likely(g(100))\n"); + } +} + void h() { g(1) || g(0); g(0) || g(1); @@ -44,5 +53,6 @@ int main() { pfoo->msg[2] = '!', printf("%s\n", foo.msg); h(); printf("%d\n", (int)((-0x2000ULL << (8*sizeof(long)-1)) | (4096ULL -1))); + test(); return 0; }