Skip to content

Commit a3ceeeb

Browse files
committed
(engine) more progress on instruction merging (OC backport)
Src-commit: 1d9ca74b91588cd85089b7df407709107cf4e255
1 parent a8a8099 commit a3ceeeb

File tree

1 file changed

+121
-97
lines changed

1 file changed

+121
-97
lines changed

core/engine/absmach_def.pl

Lines changed: 121 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@
877877
fsize_sum_b([X|Xs], BSize) => fsize(X), tk('+'), fsize_sum_b(Xs, BSize).
878878

879879
% Jump to a given instruction keeping the same operand stream
880+
goto_ins('$label'(Lab)) => goto(Lab). % TODO: hack?
880881
goto_ins(X+(Y+Z)) => goto_ins((X+Y)+Z).
881882
goto_ins(Ins) =>
882883
[[ get(ins_spec(Ins).op, Op) ]],
@@ -978,11 +979,40 @@
978979
trail_if_conditional_sva(U),
979980
U <- V.
980981

981-
un_voidr(X), [[ mode(r) ]] =>
982-
(~s) <- ~heap_offset((~s), X).
983-
un_voidr(X), [[ mode(w) ]] =>
984-
localv(intmach, I, cast(ftype_ctype(f_i_signed), X)),
985-
do_while(constr_hva(tk('H')), (tk('--'),I)).
982+
un_voidr(X) =>
983+
( [[ mode(r) ]] ->
984+
(~s) <- ~heap_offset((~s), X)
985+
; localv(intmach, I, cast(ftype_ctype(f_i_signed), X)),
986+
% (assume I>0)
987+
do_while((
988+
cachedreg('H', H),
989+
constr_hva(H)
990+
), (tk('--'),I))
991+
).
992+
% (for iter spec)
993+
unify_void(I), [[ integer(I), I>0 ]] => % (specialized case)
994+
( [[ mode(r) ]] ->
995+
un_voidr(I),
996+
dispatch
997+
; cachedreg('H', H),
998+
constr_hva(H),
999+
( [[ I = 1 ]] ->
1000+
dispatch
1001+
; [[ I1 is I-1 ]],
1002+
goto_ins(unify_void(I1))
1003+
)
1004+
).
1005+
unify_void(N) =>
1006+
( [[ mode(r) ]] ->
1007+
un_voidr(N),
1008+
dispatch
1009+
; localv(intmach, I, cast(ftype_ctype(f_i_signed), N)),
1010+
shiftf(f_i),
1011+
foreach(intmach, revrange(I,4), _I0,
1012+
(cachedreg('H', H),
1013+
constr_hva(H))),
1014+
goto_ins(unify_void(4))
1015+
).
9861016

9871017
un_var(X), [[ mode(r) ]] =>
9881018
ref_heap_next(X).
@@ -1045,32 +1075,36 @@
10451075
(~w)^.local_top <- ~stack_char_offset(E,EnvSize),
10461076
if(~off_stacktop(E,tk('Stack_Warn')), set_event).
10471077

1048-
inittrue => decops([EnvSize]),
1078+
% TODO:[oc-merge] was: collapse cannot be used here b/c initall operand is cframe operand
1079+
alloc_init_cframe(EnvSize) =>
10491080
alloc,
10501081
initfr(EnvSize),
1051-
%
10521082
% code_cframe(~e, EnvSize, ~bc_off((~p), fsize_sum([f_e]))),
10531083
% dispatchf(fsize_sum([f_e])). % (was f_i before)
1054-
goto('firsttrue').
1084+
goto_ins('$label'('firsttrue')).
10551085

1056-
firsttrue_n => decopsf([f_i],[N]),
1057-
localv(intmach, I, cast(ftype_ctype(f_i_signed), N)),
1058-
shiftf(f_i),
1059-
foreach(intmach, revrange(I), _I0, put_yvoid),
1060-
%
1061-
goto('firsttrue'),
1086+
init_cframe =>
1087+
loop_init_all('$label'('firsttrue')),
10621088
label_blk('firsttrue', firsttrue_).
10631089

1090+
% TODO: rename; one arg is relative to P
10641091
% (ins without opcode)
10651092
firsttrue_ => decopsf([f_e],[EnvSize]),
10661093
code_cframe(~e, EnvSize, ~bc_off((~p), fsize_sum([f_e]))),
10671094
dispatchf(fsize_sum([f_e])). % (was f_i before)
10681095

1069-
initcall => decops([_,N]),
1096+
% TODO:[oc-merge] was: collapse cannot be used here b/c initall operand is cframe operand
1097+
alloc_init_fcall(PredPtr,EnvSize) =>
10701098
alloc,
1071-
initfr(N),
1099+
initfr(EnvSize),
1100+
% fcall(PredPtr,EnvSize).
10721101
goto_ins(fcall(f_E,f_e)).
10731102

1103+
loop_init_all(Cont) => decopsf([f_i],[N]),
1104+
localv(intmach, I, cast(ftype_ctype(f_i_signed), N)),
1105+
shiftf(f_i),
1106+
foreach(intmach, revrange(I), _I0, put_yvoid),
1107+
goto_ins(Cont).
10741108
loop_init_until(M, Cont) => decopsf([f_i],[N]), % N>M
10751109
localv(intmach, I, cast(ftype_ctype(f_i_signed), N)),
10761110
shiftf(f_i),
@@ -1122,8 +1156,7 @@
11221156
[[ mode(M) ]],
11231157
setmode(r),
11241158
A <- cfun_eval('BC_MakeBlob', [B]),
1125-
setmode(M),
1126-
dispatch_b(~large_size(B^)).
1159+
setmode(M).
11271160

11281161
ld_str(A,B) =>
11291162
cachedreg('H', H),
@@ -1146,26 +1179,23 @@
11461179
; true % (nothing left to be done)
11471180
).
11481181

1149-
get_large => decops([A,B]),
1150-
unify_large(B,A),
1151-
dispatch_b(~large_size(B^)).
1182+
u_blob(A,B) => unify_large(B,A).
11521183

11531184
% (x0 version: read-mode match has been done during indexing)
1154-
get_large_x0, [[ mode(r) ]] => decops([A]),
1155-
localv(tagged, T0, x(0)),
1156-
unify_large(A,T0),
1157-
dispatch_b(~large_size(A^)).
1158-
get_large_x0, [[ mode(w) ]] => decops([A]),
1159-
setmode(r),
1160-
localv(tagged, T1, cfun_eval('BC_MakeBlob', [A])),
1161-
setmode(w),
1162-
localv(tagged, T0, x(0)),
1163-
if(~tagged_is(hva,T0),
1164-
bind(hva,T0,T1),
1165-
if(T0 /\ tk('TagBitSVA'),
1166-
bind(sva,T0,T1),
1167-
bind(cva,T0,T1))),
1168-
dispatch_b(~large_size(A^)).
1185+
u_blob_x0(A) =>
1186+
( [[ mode(r) ]] ->
1187+
localv(tagged, T0, x(0)),
1188+
unify_large(A,T0)
1189+
; setmode(r),
1190+
localv(tagged, T1, cfun_eval('BC_MakeBlob', [A])),
1191+
setmode(w),
1192+
localv(tagged, T0, x(0)),
1193+
if(~tagged_is(hva,T0),
1194+
bind(hva,T0,T1),
1195+
if(T0 /\ tk('TagBitSVA'),
1196+
bind(sva,T0,T1),
1197+
bind(cva,T0,T1)))
1198+
).
11691199

11701200
get_structure => decops([A,B]),
11711201
unify_structure(B,A,dispatch).
@@ -1269,23 +1299,27 @@
12691299
set_deep,
12701300
if(~choice_younger(~choice_offset(~b,tk('CHOICEPAD')), (~w)^.trail_top),
12711301
cvoid_call('choice_overflow', [2*tk('CHOICEPAD')*sizeof(tagged),~true])),
1272-
set_e((~w)^.local_top),
1273-
dispatch.
1302+
set_e((~w)^.local_top).
12741303

1304+
% TODO: use cutf/1?
12751305
cutf_x => decops([A]),
12761306
(~w)^.previous_choice <- ~choice_from_tagged(A),
12771307
shiftf,
1308+
% do_cut,
1309+
% set_e((~w)^.frame),
12781310
goto_ins(cutf). % TODO: check that pending 'format' after shift is the expected one
1311+
% dispatch.
12791312

12801313
cutf =>
12811314
do_cut,
1282-
set_e((~w)^.frame),
1283-
dispatch.
1315+
set_e((~w)^.frame).
12841316

1285-
cut_y => decops([A]),
1317+
% TODO: continue with cutf?
1318+
cutf(A) =>
12861319
localv(tagged, T1), T1 <- A,
12871320
(~w)^.previous_choice <- ~choice_from_tagged(T1),
12881321
do_cut,
1322+
%
12891323
set_e((~w)^.frame),
12901324
dispatch.
12911325

@@ -1371,48 +1405,24 @@
13711405
(bind(sva,T1,T2), A <- T2),
13721406
bind(cva,T2,T1)).
13731407

1374-
unify_void(I), [[ integer(I), I>0 ]] => % (specialized case)
1375-
( [[ mode(r) ]] ->
1376-
un_voidr(I),
1377-
dispatch
1378-
; cachedreg('H', H),
1379-
constr_hva(H),
1380-
( [[ I = 1 ]] ->
1381-
dispatch
1382-
; [[ I1 is I-1 ]],
1383-
goto_ins(unify_void(I1))
1384-
)
1385-
).
1386-
unify_void(N), [[ mode(r) ]] =>
1387-
un_voidr(N),
1388-
dispatch.
1389-
unify_void(N), [[ mode(w) ]] =>
1390-
localv(intmach, I, cast(ftype_ctype(f_i_signed), N)),
1391-
shiftf(f_i),
1392-
foreach(intmach, revrange(I,4), _I0,
1393-
(cachedreg('H', H),
1394-
constr_hva(H))),
1395-
goto_ins(unify_void(4)).
1396-
13971408
un_cons(A) =>
13981409
( [[ mode(r) ]] ->
13991410
localv(tagged, T1), ref_heap_next(T1),
14001411
unify_heap_atom(A,T1)
14011412
; heap_push(A)
14021413
).
14031414

1404-
unify_large, [[ mode(r) ]] => decops([A]),
1405-
localv(tagged, T1), ref_heap_next(T1),
1406-
unify_heap_large(A,T1),
1407-
dispatch_b(~large_size(A^)).
1408-
unify_large, [[ mode(w) ]] => decops([A]),
1409-
% TODO: try to switch to r mode properly (this code is tricky)
1410-
% (this is 'heap_push and switch to read')
1411-
cachedreg('H', H),
1412-
(~w)^.heap_top <- ~heap_offset(H,1),
1413-
H^ <- cfun_eval('BC_MakeBlob', [A]),
1414-
[[ update(mode(r)) ]],
1415-
dispatch_b(~large_size(A^)).
1415+
un_blob(A) =>
1416+
( [[ mode(r) ]] ->
1417+
localv(tagged, T1), ref_heap_next(T1),
1418+
unify_heap_large(A,T1)
1419+
; % TODO: try to switch to r mode properly (this code is tricky)
1420+
% (this is 'heap_push and switch to read')
1421+
cachedreg('H', H),
1422+
(~w)^.heap_top <- ~heap_offset(H,1),
1423+
H^ <- cfun_eval('BC_MakeBlob', [A]),
1424+
[[ update(mode(r)) ]]
1425+
).
14161426

14171427
unify_structure, [[ mode(r) ]] => decops([A]),
14181428
localv(tagged, T1), ref_heap_next(T1),
@@ -1463,16 +1473,15 @@
14631473
jump_fail.
14641474

14651475
% TODO: PATCH_LIVEINFO requires f_g (we cannot expand to f_l,f_i in instruction format like in OC)
1466-
1467-
heapmargin_call => decopsf([f_l,f_i],[A,B]), % TODO: abstract code to use f_g
1476+
heapmargin_call(_G) =>
1477+
[[ update(format([f_l,f_i])) ]], decops([A,B]), % TODO: abstract code to use f_g
14681478
cachedreg('H',H),
14691479
if(~heap_char_difference(H, tk('Heap_End')) < cast(intmach,A),
14701480
([[ mode(M) ]],
14711481
setmode(r),
14721482
cvoid_call('explicit_heap_overflow', [cast(intmach,A)*2, cast(ftype_ctype(f_i_signed),B)]),
14731483
setmode(M)
1474-
)),
1475-
dispatch.
1484+
)).
14761485

14771486
neck =>
14781487
code_neck,
@@ -2310,10 +2319,10 @@
23102319
if(I\==0, (
23112320
localv(ptr(tagged), Pt1, (~w)^.x),
23122321
localv(ptr(tagged), Pt2, (~w)^.structure),
2313-
do_while(
2314-
push_ref_heap_next(Pt1,Pt2),
2315-
(tk('--'),I)
2316-
)
2322+
% (assume I>0)
2323+
do_while((
2324+
push_ref_heap_next(Pt1,Pt2)
2325+
), (tk('--'),I))
23172326
)),
23182327
jump_switch_on_pred_sub(tk('ei')).
23192328

@@ -2414,7 +2423,7 @@
24142423
HtabNode <- ~sw_on_key_node_from_offset(Htab, T1),
24152424
if(logical_or(HtabNode^.key==T2, not(HtabNode^.key)), break),
24162425
assign(I + sizeof(sw_on_key_node)),
2417-
T1 <- (T1+I) /\ Htab^.mask
2426+
T1 <- paren(T1+I) /\ Htab^.mask % (note: paren is not needed, but C emits readability warning)
24182427
), ~true),
24192428
jump_tryeach(HtabNode^.value.try_chain). % (this will break the loop)
24202429

@@ -2593,6 +2602,7 @@
25932602
% instruction spec/merge macro
25942603
ispec((X+Y)+Z, Fs, As, Code, Cont) => ispec(X+(Y+Z), Fs, As, Code, Cont).
25952604
%
2605+
ispec(init(f_Y)+cframe(f_e), [f_Y,f_e], [], init_cframe, nodecops_dispatch) => true.
25962606
ispec(init(f_Y)+Cont, [f_Y|ContF], [], loop_init_until(8,Cont), nodecops_dispatch) => ispec(Cont, ContF, _, _, dispatch).
25972607
ispec(init(s(I))+Cont, [Fs,ContF], [], loop_init_step(I,Cont), nodecops_dispatch), [[ integer(I) ]] => repf(I, f_y, Fs), ispec(Cont, ContF, _, _, dispatch).
25982608
%
@@ -2609,7 +2619,8 @@
26092619
ispec(kall(Xf,Yf), [Xf,Yf], [X,Y], kall(X,Y), dispatch) => true.
26102620
ispec(execute(Xf), [Xf], [X], execute(X), dispatch) => true.
26112621
%
2612-
ispec(ld_blob(Xf,Yf), [Xf,Yf], [X,Y], ld_blob(X,Y), dispatch) => true.
2622+
ispec(alloc_init_cframe(Xf), [Xf], [X], alloc_init_cframe(X), dispatch) => true.
2623+
ispec(alloc_init_fcall(Xf,Yf), [Xf,Yf], [X,Y], alloc_init_fcall(X,Y), dispatch) => true.
26132624
%
26142625
ispec(retry_instance, [], [], retry_instance, dispatch) => true.
26152626
ispec(proceed, [], [], proceed, dispatch) => true.
@@ -2657,13 +2668,26 @@
26572668
[[ InsCode = (decops(InsArgs),InsCode0) ]]
26582669
; [[ Cont = nodecops_dispatch ]] -> % (no decops, dispatch is explicit in ins)
26592670
[[ InsCode = InsCode0 ]]
2660-
; [[ InsCode = (decops(InsArgs),InsCode0,dispatch) ]]
2671+
; idispatch(InsArgs, Format, Dispatch),
2672+
[[ InsCode = (decops(InsArgs),InsCode0,Dispatch) ]]
26612673
)
26622674
).
26632675

2676+
% TODO: improve blob arg identification
2677+
idispatch([Blob],[f_b],dispatch_b(~large_size(Blob^))) => true.
2678+
idispatch([_,Blob],[_,f_b],dispatch_b(~large_size(Blob^))) => true.
2679+
idispatch(_,_,dispatch) => true.
2680+
26642681
ispeccode1(Ins,InsSpec,Format,i_ec(InsCode,InsCont)) =>
26652682
ispeccode(Ins,InsSpec,Format,_), spec1(InsSpec, InsCode, InsCont).
26662683

2684+
%% (uncomment to disable custom alignment versions)
2685+
%ins_entry_(Ins,Opcode,InsSpec,Export,[q0w|Props]) => ins_entry_(Ins,Opcode,InsSpec,Export,[q0|Props]).
2686+
%ins_entry_(Ins,Opcode,InsSpec,Export,[q0r|Props]) => ins_entry_(Ins,Opcode,InsSpec,Export,[q0|Props]).
2687+
%ins_entry_(Ins,Opcode,InsSpec,Export,[qall|Props]) => ins_entry_(Ins,Opcode,InsSpec,Export,[q0|Props]).
2688+
%ins_entry_(Ins,Opcode,InsSpec,Export,[qqall|Props]) => ins_entry_(Ins,Opcode,InsSpec,Export,[q0|Props]).
2689+
%ins_entry_(Ins,Opcode,InsSpec,Export,[qqall|Props]) => ins_entry_(Ins,Opcode,InsSpec,Export,[q0|Props]).
2690+
%%
26672691
ins_entry_(Ins,Opcode,InsSpec,Export,[q0,1|Props]) => % (see q0)
26682692
ispecnorm(Ins,InsSpec,InsSpec2),
26692693
ispeccode1(Ins,InsSpec,Format,InsCode),
@@ -2795,9 +2819,9 @@
27952819
iset_misc2.
27962820

27972821
iset_init =>
2798-
ins_entry(inittrue, 260, [f_e], [in_mode(w)]),
2799-
ins_entry(firsttrue_n, 261, [f_Y,f_e], [in_mode(w)]),
2800-
ins_entry(initcall, 1, [f_E,f_e], [q0,in_mode(w)]).
2822+
ins_entry(-, 260, alloc_init_cframe(f_e), [in_mode(w)]),
2823+
ins_entry(-, 261, init(f_Y)+cframe(f_e), [in_mode(w)]),
2824+
ins_entry(-, 1, alloc_init_fcall(f_E,f_e), [q0,in_mode(w)]).
28012825

28022826
iset_call =>
28032827
ins_entry(-, 21, init(f_Y)+fcall(f_E,f_e), [q0,in_mode(w)]),
@@ -2871,7 +2895,7 @@
28712895
ins_entry(-, 94, u_fval(f_x,f_y), [in_mode(r)]),
28722896
ins_entry(-, 95, u_val(f_x,f_y), [in_mode(r)]),
28732897
ins_entry(-, 97, u_cons(f_x,f_t), exported(get_constant)+[q0,in_mode(r)]), % (for cterm)
2874-
ins_entry(get_large, 255, [f_x,f_b], exported(get_large)+[q0,in_mode(r)]), % (for cterm)
2898+
ins_entry(-, 255, u_blob(f_x,f_b), exported(get_large)+[q0,in_mode(r)]), % (for cterm)
28752899
ins_entry(get_structure, 99, [f_x,f_f], exported(get_structure)+[q0,in_mode(r)]), % (for cterm)
28762900
ins_entry(-, 100, u_nil(f_x), exported(get_nil)+[in_mode(r)]), % (for cterm)
28772901
ins_entry(get_list, 101, [f_x], exported(get_list)+[in_mode(r)]), % (for cterm)
@@ -2886,10 +2910,10 @@
28862910
ins_entry(cutb_neck_proceed, 213, [], [in_mode(r)]),
28872911
ins_entry(-, 214, cute(f_x), [in_mode(r)]),
28882912
ins_entry(cute_x_neck, 216, [f_x], [in_mode(r)]),
2889-
ins_entry(cute_neck, 217, [], [in_mode(r)]),
2913+
ins_entry(-, 217, cute_neck, [in_mode(r)]),
28902914
ins_entry(cutf_x, 215, [f_x], [in_mode(r)]),
2891-
ins_entry(cutf, 209, [], [in_mode(r)]),
2892-
ins_entry(cut_y, 218, [f_y], [in_mode(r)]).
2915+
ins_entry(-, 209, cutf, [in_mode(r)]),
2916+
ins_entry(-, 218, cutf(f_y), [in_mode(r)]).
28932917

28942918
iset_choice =>
28952919
ins_entry(-, 219, getchoice(f_x)),
@@ -2904,7 +2928,7 @@
29042928

29052929
iset_get2 =>
29062930
ins_entry(-, 105, u_str_x0(f_f), exported(get_structure_x0)+[q0w]), % (for cterm)
2907-
ins_entry(get_large_x0, 257, [f_b], [q0w]),
2931+
ins_entry(-, 257, u_blob_x0(f_b), [q0w]),
29082932
ins_entry(-, 103, u_cons_x0(f_t), exported(get_constant_x0)+[q0w]), % (for cterm)
29092933
ins_entry(-, 106, u_nil_x0, exported(get_nil_x0)+[]), % (for cterm)
29102934
ins_entry(-, 107, u_list_x0, exported(get_list_x0)+[]), % (for cterm)
@@ -2930,7 +2954,7 @@
29302954
ins_entry(-, 125, un_val(f_y), [rw(e(un_lval(f_y),0),all)]),
29312955
ins_entry(-, 126, un_lval(f_y), []),
29322956
ins_entry(-, 128, un_cons(f_t), exported(unify_constant)+[q0r]), % (for cterm)
2933-
ins_entry(unify_large, 259, [f_b], exported(unify_large)+[q0]), % (for cterm)
2957+
ins_entry(-, 259, un_blob(f_b), exported(unify_large)+[q0]), % (for cterm)
29342958
ins_entry(unify_structure, 130, [f_f], exported(unify_structure)+[q0r]), % (for cterm)
29352959
ins_entry(-, 131, un_nil, exported(unify_nil)+[]), % (for cterm)
29362960
ins_entry(unify_list, 132, [], exported(unify_list)+[]), % (for cterm)
@@ -3015,7 +3039,7 @@
30153039
ins_entry(-, 249, bump_counter(f_l), [q0]),
30163040
ins_entry(counted_neck, 251, [f_l,f_l], [q0]),
30173041
ins_entry(fail, 67, [], exported(fail)+[]), % (for ciao_initcode and init_some_bytecode)
3018-
ins_entry(heapmargin_call, 246, [f_g], exported(heapmargin_call)+[q0]), % (for compile_term_aux)
3042+
ins_entry(-, 246, heapmargin_call(f_g), exported(heapmargin_call)+[q0]), % (for compile_term_aux)
30193043
ins_entry(neck, 65, [], []),
30203044
ins_entry(dynamic_neck_proceed, 236, [], exported(dynamic_neck_proceed)+[in_mode(w)]),
30213045
ins_entry(neck_proceed, 66, [], [in_mode(w)]),

0 commit comments

Comments
 (0)