Skip to content

Commit cf0a868

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

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

@@ -1244,7 +1245,7 @@ if_block_cond(
12441245
I1 = jit_riscv32_asm:mv(Temp, Reg),
12451246
Stream1 = StreamModule:append(Stream0, I1),
12461247
State1 = State0#state{stream = Stream1},
1247-
State2 = and_(State1#state{available_regs = AT}, Temp, Mask),
1248+
{State2, Temp} = and_(State1#state{available_regs = AT}, {free, Temp}, Mask),
12481249
Stream2 = State2#state.stream,
12491250
%% Compare Temp with Val and branch if equal (NOT != Val)
12501251
case Val of
@@ -1290,7 +1291,7 @@ if_block_cond(
12901291
) when ?IS_GPR(Reg) ->
12911292
%% RISC-V: AND with mask, then compare with value
12921293
OffsetBefore = StreamModule:offset(Stream0),
1293-
State1 = and_(State0, Reg, Mask),
1294+
{State1, Reg} = and_(State0, RegTuple, Mask),
12941295
Stream1 = State1#state.stream,
12951296
%% Compare Reg with Val and branch if equal (NOT != Val)
12961297
case Val of
@@ -2426,14 +2427,14 @@ get_module_index(
24262427
%% JIT currentl calls this with two values: ?TERM_PRIMARY_CLEAR_MASK (-4) to
24272428
%% clear bits and ?TERM_BOXED_TAG_MASK (0x3F). We can avoid any literal pool
24282429
%% by using BICS for -4.
2429-
and_(#state{stream_module = StreamModule, stream = Stream0} = State0, Reg, 16#FFFFFF) ->
2430+
and_(#state{stream_module = StreamModule, stream = Stream0} = State0, {free, Reg}, 16#FFFFFF) ->
24302431
I1 = jit_riscv32_asm:slli(Reg, Reg, 8),
24312432
I2 = jit_riscv32_asm:srli(Reg, Reg, 8),
24322433
Stream1 = StreamModule:append(Stream0, <<I1/binary, I2/binary>>),
2433-
State0#state{stream = Stream1};
2434+
{State0#state{stream = Stream1}, Reg};
24342435
and_(
24352436
#state{stream_module = StreamModule, available_regs = [Temp | AT]} = State0,
2436-
Reg,
2437+
{free, Reg},
24372438
Val
24382439
) when Val < 0 andalso Val >= -256 ->
24392440
State1 = mov_immediate(State0#state{available_regs = AT}, Temp, bnot (Val)),
@@ -2442,20 +2443,20 @@ and_(
24422443
I1 = jit_riscv32_asm:not_(Temp, Temp),
24432444
I2 = jit_riscv32_asm:and_(Reg, Reg, Temp),
24442445
Stream2 = StreamModule:append(Stream1, <<I1/binary, I2/binary>>),
2445-
State1#state{available_regs = [Temp | AT], stream = Stream2};
2446+
{State1#state{available_regs = [Temp | AT], stream = Stream2}, Reg};
24462447
and_(
24472448
#state{stream_module = StreamModule, available_regs = [Temp | AT]} = State0,
2448-
Reg,
2449+
{free, Reg},
24492450
Val
24502451
) ->
24512452
State1 = mov_immediate(State0#state{available_regs = AT}, Temp, Val),
24522453
Stream1 = State1#state.stream,
24532454
I = jit_riscv32_asm:and_(Reg, Reg, Temp),
24542455
Stream2 = StreamModule:append(Stream1, I),
2455-
State1#state{available_regs = [Temp | AT], stream = Stream2};
2456+
{State1#state{available_regs = [Temp | AT], stream = Stream2}, Reg};
24562457
and_(
24572458
#state{stream_module = StreamModule, available_regs = []} = State0,
2458-
Reg,
2459+
{free, Reg},
24592460
Val
24602461
) when Val < 0 andalso Val >= -256 ->
24612462
% No available registers, use a0 as temp and save it to t3
@@ -2473,10 +2474,10 @@ and_(
24732474
% Restore a0 from t3
24742475
Restore = jit_riscv32_asm:mv(a0, ?IP_REG),
24752476
Stream4 = StreamModule:append(Stream3, Restore),
2476-
State0#state{stream = Stream4};
2477+
{State0#state{stream = Stream4}, Reg};
24772478
and_(
24782479
#state{stream_module = StreamModule, available_regs = []} = State0,
2479-
Reg,
2480+
{free, Reg},
24802481
Val
24812482
) ->
24822483
% No available registers, use a0 as temp and save it to t3
@@ -2493,7 +2494,16 @@ and_(
24932494
% Restore a0 from t3
24942495
Restore = jit_riscv32_asm:mv(a0, ?IP_REG),
24952496
Stream4 = StreamModule:append(Stream3, Restore),
2496-
State0#state{stream = Stream4}.
2497+
{State0#state{stream = Stream4}, Reg};
2498+
and_(
2499+
#state{stream_module = StreamModule, available_regs = [ResultReg | AT], used_regs = UR} =
2500+
State0,
2501+
Reg,
2502+
?TERM_PRIMARY_CLEAR_MASK
2503+
) ->
2504+
I = jit_riscv32_asm:andi(ResultReg, Reg, -4),
2505+
Stream1 = StreamModule:append(State0#state.stream, I),
2506+
{State0#state{stream = Stream1, available_regs = AT, used_regs = [ResultReg | UR]}, ResultReg}.
24972507

24982508
or_(
24992509
#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)