Skip to content

Commit ca2f585

Browse files
committed
riscv32: Implement support for private_append
Signed-off-by: Paul Guyot <[email protected]>
1 parent ba0ecaf commit ca2f585

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

libs/jit/src/jit_riscv32.erl

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
-include_lib("jit.hrl").
8888

8989
-include("primitives.hrl").
90+
-include("term.hrl").
9091

9192
-define(ASSERT(Expr), true = Expr).
9293

@@ -1240,7 +1241,7 @@ if_block_cond(
12401241
I1 = jit_riscv32_asm:mv(Temp, Reg),
12411242
Stream1 = StreamModule:append(Stream0, I1),
12421243
State1 = State0#state{stream = Stream1},
1243-
State2 = and_(State1#state{available_regs = AT}, Temp, Mask),
1244+
{State2, Temp} = and_(State1#state{available_regs = AT}, {free, Temp}, Mask),
12441245
Stream2 = State2#state.stream,
12451246
%% Compare Temp with Val and branch if equal (NOT != Val)
12461247
case Val of
@@ -1286,7 +1287,7 @@ if_block_cond(
12861287
) when ?IS_GPR(Reg) ->
12871288
%% RISC-V: AND with mask, then compare with value
12881289
OffsetBefore = StreamModule:offset(Stream0),
1289-
State1 = and_(State0, Reg, Mask),
1290+
{State1, Reg} = and_(State0, RegTuple, Mask),
12901291
Stream1 = State1#state.stream,
12911292
%% Compare Reg with Val and branch if equal (NOT != Val)
12921293
case Val of
@@ -2433,14 +2434,14 @@ get_module_index(
24332434
%% JIT currentl calls this with two values: ?TERM_PRIMARY_CLEAR_MASK (-4) to
24342435
%% clear bits and ?TERM_BOXED_TAG_MASK (0x3F). We can avoid any literal pool
24352436
%% by using BICS for -4.
2436-
and_(#state{stream_module = StreamModule, stream = Stream0} = State0, Reg, 16#FFFFFF) ->
2437+
and_(#state{stream_module = StreamModule, stream = Stream0} = State0, {free, Reg}, 16#FFFFFF) ->
24372438
I1 = jit_riscv32_asm:slli(Reg, Reg, 8),
24382439
I2 = jit_riscv32_asm:srli(Reg, Reg, 8),
24392440
Stream1 = StreamModule:append(Stream0, <<I1/binary, I2/binary>>),
2440-
State0#state{stream = Stream1};
2441+
{State0#state{stream = Stream1}, Reg};
24412442
and_(
24422443
#state{stream_module = StreamModule, available_regs = [Temp | AT]} = State0,
2443-
Reg,
2444+
{free, Reg},
24442445
Val
24452446
) when Val < 0 andalso Val >= -256 ->
24462447
State1 = mov_immediate(State0#state{available_regs = AT}, Temp, bnot (Val)),
@@ -2449,20 +2450,20 @@ and_(
24492450
I1 = jit_riscv32_asm:not_(Temp, Temp),
24502451
I2 = jit_riscv32_asm:and_(Reg, Reg, Temp),
24512452
Stream2 = StreamModule:append(Stream1, <<I1/binary, I2/binary>>),
2452-
State1#state{available_regs = [Temp | AT], stream = Stream2};
2453+
{State1#state{available_regs = [Temp | AT], stream = Stream2}, Reg};
24532454
and_(
24542455
#state{stream_module = StreamModule, available_regs = [Temp | AT]} = State0,
2455-
Reg,
2456+
{free, Reg},
24562457
Val
24572458
) ->
24582459
State1 = mov_immediate(State0#state{available_regs = AT}, Temp, Val),
24592460
Stream1 = State1#state.stream,
24602461
I = jit_riscv32_asm:and_(Reg, Reg, Temp),
24612462
Stream2 = StreamModule:append(Stream1, I),
2462-
State1#state{available_regs = [Temp | AT], stream = Stream2};
2463+
{State1#state{available_regs = [Temp | AT], stream = Stream2}, Reg};
24632464
and_(
24642465
#state{stream_module = StreamModule, available_regs = []} = State0,
2465-
Reg,
2466+
{free, Reg},
24662467
Val
24672468
) when Val < 0 andalso Val >= -256 ->
24682469
% No available registers, use a0 as temp and save it to t3
@@ -2480,10 +2481,10 @@ and_(
24802481
% Restore a0 from t3
24812482
Restore = jit_riscv32_asm:mv(a0, ?IP_REG),
24822483
Stream4 = StreamModule:append(Stream3, Restore),
2483-
State0#state{stream = Stream4};
2484+
{State0#state{stream = Stream4}, Reg};
24842485
and_(
24852486
#state{stream_module = StreamModule, available_regs = []} = State0,
2486-
Reg,
2487+
{free, Reg},
24872488
Val
24882489
) ->
24892490
% No available registers, use a0 as temp and save it to t3
@@ -2500,7 +2501,16 @@ and_(
25002501
% Restore a0 from t3
25012502
Restore = jit_riscv32_asm:mv(a0, ?IP_REG),
25022503
Stream4 = StreamModule:append(Stream3, Restore),
2503-
State0#state{stream = Stream4}.
2504+
{State0#state{stream = Stream4}, Reg};
2505+
and_(
2506+
#state{stream_module = StreamModule, available_regs = [ResultReg | AT], used_regs = UR} =
2507+
State0,
2508+
Reg,
2509+
?TERM_PRIMARY_CLEAR_MASK
2510+
) ->
2511+
I = jit_riscv32_asm:andi(ResultReg, Reg, -4),
2512+
Stream1 = StreamModule:append(State0#state.stream, I),
2513+
{State0#state{stream = Stream1, available_regs = AT, used_regs = [ResultReg | UR]}, ResultReg}.
25042514

25052515
or_(
25062516
#state{stream_module = StreamModule, available_regs = [Temp | AT]} = State0,

tests/libs/jit/jit_riscv32_tests.erl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ call_primitive_6_args_test() ->
124124
State0 = ?BACKEND:new(?JIT_VARIANT_PIC, jit_stream_binary, jit_stream_binary:new(0)),
125125
% Get bin_ptr from x_reg 0 (similar to get_list_test pattern)
126126
{State1, RegA} = ?BACKEND:move_to_native_register(State0, {x_reg, 0}),
127-
State2 = ?BACKEND:and_(State1, RegA, ?TERM_PRIMARY_CLEAR_MASK),
127+
{State2, RegA} = ?BACKEND:and_(State1, {free, RegA}, ?TERM_PRIMARY_CLEAR_MASK),
128128
% Get another register for the last parameter to test {free, Reg} handling
129129
{State3, OtherReg} = ?BACKEND:move_to_native_register(State2, {x_reg, 1}),
130130
% Call PRIM_BITSTRING_EXTRACT_INTEGER with 6 arguments
@@ -1310,7 +1310,7 @@ call_bif_with_large_literal_integer_test() ->
13101310
get_list_test() ->
13111311
State0 = ?BACKEND:new(?JIT_VARIANT_PIC, jit_stream_binary, jit_stream_binary:new(0)),
13121312
{State1, Reg} = ?BACKEND:move_to_native_register(State0, {x_reg, 0}),
1313-
State2 = ?BACKEND:and_(State1, Reg, ?TERM_PRIMARY_CLEAR_MASK),
1313+
{State2, Reg} = ?BACKEND:and_(State1, {free, Reg}, ?TERM_PRIMARY_CLEAR_MASK),
13141314
State3 = ?BACKEND:move_array_element(State2, Reg, 1, {y_reg, 1}),
13151315
State4 = ?BACKEND:move_array_element(State3, Reg, 0, {y_reg, 0}),
13161316
State5 = ?BACKEND:free_native_registers(State4, [Reg]),
@@ -1343,7 +1343,7 @@ is_integer_test() ->
13431343
?BACKEND:jump_to_label(BSt0, Label)
13441344
end
13451345
),
1346-
MSt2 = ?BACKEND:and_(MSt1, Reg, ?TERM_PRIMARY_CLEAR_MASK),
1346+
{MSt2, Reg} = ?BACKEND:and_(MSt1, {free, Reg}, ?TERM_PRIMARY_CLEAR_MASK),
13471347
MSt3 = ?BACKEND:move_array_element(MSt2, Reg, 0, Reg),
13481348
?BACKEND:if_block(
13491349
MSt3,
@@ -1403,7 +1403,7 @@ is_number_test() ->
14031403
BSt1 = cond_jump_to_label(
14041404
{Reg, '&', ?TERM_PRIMARY_MASK, '!=', ?TERM_PRIMARY_BOXED}, Label, ?BACKEND, BSt0
14051405
),
1406-
BSt2 = ?BACKEND:and_(BSt1, Reg, ?TERM_PRIMARY_CLEAR_MASK),
1406+
{BSt2, Reg} = ?BACKEND:and_(BSt1, {free, Reg}, ?TERM_PRIMARY_CLEAR_MASK),
14071407
BSt3 = ?BACKEND:move_array_element(BSt2, Reg, 0, Reg),
14081408
cond_jump_to_label(
14091409
{'and', [
@@ -1810,7 +1810,7 @@ call_fun_test() ->
18101810
])
18111811
end
18121812
),
1813-
State5 = ?BACKEND:and_(State4, RegCopy, ?TERM_PRIMARY_CLEAR_MASK),
1813+
{State5, RegCopy} = ?BACKEND:and_(State4, {free, RegCopy}, ?TERM_PRIMARY_CLEAR_MASK),
18141814
State6 = ?BACKEND:move_array_element(State5, RegCopy, 0, RegCopy),
18151815
State7 = ?BACKEND:if_block(
18161816
State6, {RegCopy, '&', ?TERM_BOXED_TAG_MASK, '!=', ?TERM_BOXED_FUN}, fun(BSt0) ->
@@ -2814,7 +2814,7 @@ and_register_exhaustion_negative_test() ->
28142814
{State5, t2} = ?BACKEND:move_to_native_register(State4, {x_reg, 4}),
28152815
{StateNoRegs, t1} = ?BACKEND:move_to_native_register(State5, {x_reg, 5}),
28162816
% Test negative immediate (-4) which should use NOT+AND with t0 as temp
2817-
StateResult = ?BACKEND:and_(StateNoRegs, t6, -4),
2817+
{StateResult, t6} = ?BACKEND:and_(StateNoRegs, {free, t6}, -4),
28182818
Stream = ?BACKEND:stream(StateResult),
28192819
ExpectedDump = <<
28202820
" 0: 01852f83 lw t6,24(a0)\n"
@@ -2839,7 +2839,7 @@ and_register_exhaustion_positive_test() ->
28392839
{State5, t2} = ?BACKEND:move_to_native_register(State4, {x_reg, 4}),
28402840
{StateNoRegs, t1} = ?BACKEND:move_to_native_register(State5, {x_reg, 5}),
28412841
% Test positive immediate (0x3F) which should use AND with t0 as temp
2842-
StateResult = ?BACKEND:and_(StateNoRegs, t6, 16#3F),
2842+
{StateResult, t6} = ?BACKEND:and_(StateNoRegs, {free, t6}, 16#3F),
28432843
Stream = ?BACKEND:stream(StateResult),
28442844
ExpectedDump = <<
28452845
" 0: 01852f83 lw t6,24(a0)\n"

0 commit comments

Comments
 (0)