Skip to content

Commit bbfddb9

Browse files
committed
LoongArch: BPF: Avoid declare variables in switch-case
Not all compilers support declare variables in switch-case, so move declarations to the beginning of a function. Otherwise we may get such build errors: arch/loongarch/net/bpf_jit.c: In function ‘emit_atomic’: arch/loongarch/net/bpf_jit.c:362:3: error: a label can only be part of a statement and a declaration is not a statement u8 r0 = regmap[BPF_REG_0]; ^~ arch/loongarch/net/bpf_jit.c: In function ‘build_insn’: arch/loongarch/net/bpf_jit.c:727:3: error: a label can only be part of a statement and a declaration is not a statement u8 t7 = -1; ^~ arch/loongarch/net/bpf_jit.c:778:3: error: a label can only be part of a statement and a declaration is not a statement int ret; ^~~ arch/loongarch/net/bpf_jit.c:779:3: error: expected expression before ‘u64’ u64 func_addr; ^~~ arch/loongarch/net/bpf_jit.c:780:3: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement] bool func_addr_fixed; ^~~~ arch/loongarch/net/bpf_jit.c:784:11: error: ‘func_addr’ undeclared (first use in this function); did you mean ‘in_addr’? &func_addr, &func_addr_fixed); ^~~~~~~~~ in_addr arch/loongarch/net/bpf_jit.c:784:11: note: each undeclared identifier is reported only once for each function it appears in arch/loongarch/net/bpf_jit.c:814:3: error: a label can only be part of a statement and a declaration is not a statement u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm; ^~~ Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 4805a13 commit bbfddb9

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

arch/loongarch/net/bpf_jit.c

+13-18
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static void emit_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
279279
const u8 t1 = LOONGARCH_GPR_T1;
280280
const u8 t2 = LOONGARCH_GPR_T2;
281281
const u8 t3 = LOONGARCH_GPR_T3;
282+
const u8 r0 = regmap[BPF_REG_0];
282283
const u8 src = regmap[insn->src_reg];
283284
const u8 dst = regmap[insn->dst_reg];
284285
const s16 off = insn->off;
@@ -359,8 +360,6 @@ static void emit_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
359360
break;
360361
/* r0 = atomic_cmpxchg(dst + off, r0, src); */
361362
case BPF_CMPXCHG:
362-
u8 r0 = regmap[BPF_REG_0];
363-
364363
move_reg(ctx, t2, r0);
365364
if (isdw) {
366365
emit_insn(ctx, lld, r0, t1, 0);
@@ -390,8 +389,11 @@ static bool is_signed_bpf_cond(u8 cond)
390389

391390
static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extra_pass)
392391
{
393-
const bool is32 = BPF_CLASS(insn->code) == BPF_ALU ||
394-
BPF_CLASS(insn->code) == BPF_JMP32;
392+
u8 tm = -1;
393+
u64 func_addr;
394+
bool func_addr_fixed;
395+
int i = insn - ctx->prog->insnsi;
396+
int ret, jmp_offset;
395397
const u8 code = insn->code;
396398
const u8 cond = BPF_OP(code);
397399
const u8 t1 = LOONGARCH_GPR_T1;
@@ -400,8 +402,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
400402
const u8 dst = regmap[insn->dst_reg];
401403
const s16 off = insn->off;
402404
const s32 imm = insn->imm;
403-
int jmp_offset;
404-
int i = insn - ctx->prog->insnsi;
405+
const u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm;
406+
const bool is32 = BPF_CLASS(insn->code) == BPF_ALU || BPF_CLASS(insn->code) == BPF_JMP32;
405407

406408
switch (code) {
407409
/* dst = src */
@@ -724,24 +726,23 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
724726
case BPF_JMP32 | BPF_JSGE | BPF_K:
725727
case BPF_JMP32 | BPF_JSLT | BPF_K:
726728
case BPF_JMP32 | BPF_JSLE | BPF_K:
727-
u8 t7 = -1;
728729
jmp_offset = bpf2la_offset(i, off, ctx);
729730
if (imm) {
730731
move_imm(ctx, t1, imm, false);
731-
t7 = t1;
732+
tm = t1;
732733
} else {
733734
/* If imm is 0, simply use zero register. */
734-
t7 = LOONGARCH_GPR_ZERO;
735+
tm = LOONGARCH_GPR_ZERO;
735736
}
736737
move_reg(ctx, t2, dst);
737738
if (is_signed_bpf_cond(BPF_OP(code))) {
738-
emit_sext_32(ctx, t7, is32);
739+
emit_sext_32(ctx, tm, is32);
739740
emit_sext_32(ctx, t2, is32);
740741
} else {
741-
emit_zext_32(ctx, t7, is32);
742+
emit_zext_32(ctx, tm, is32);
742743
emit_zext_32(ctx, t2, is32);
743744
}
744-
if (emit_cond_jmp(ctx, cond, t2, t7, jmp_offset) < 0)
745+
if (emit_cond_jmp(ctx, cond, t2, tm, jmp_offset) < 0)
745746
goto toofar;
746747
break;
747748

@@ -775,10 +776,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
775776

776777
/* function call */
777778
case BPF_JMP | BPF_CALL:
778-
int ret;
779-
u64 func_addr;
780-
bool func_addr_fixed;
781-
782779
mark_call(ctx);
783780
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
784781
&func_addr, &func_addr_fixed);
@@ -811,8 +808,6 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
811808

812809
/* dst = imm64 */
813810
case BPF_LD | BPF_IMM | BPF_DW:
814-
u64 imm64 = (u64)(insn + 1)->imm << 32 | (u32)insn->imm;
815-
816811
move_imm(ctx, dst, imm64, is32);
817812
return 1;
818813

0 commit comments

Comments
 (0)