Skip to content

Commit

Permalink
Add a PUSHACCMANY opcode.
Browse files Browse the repository at this point in the history
Especially for symbols defined inside sections, callee functions might be
invoked with the exact same arguments as caller functions. A similar
situation happens with recursive functions that share most of their
arguments across calls. This new opcode replaces a sequence of repeated
"PUSH; ACC" opcodes with constant indices.
  • Loading branch information
silene committed May 21, 2024
1 parent 86562aa commit febfef7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 12 additions & 0 deletions kernel/byterun/coq_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ value coq_interprete
Next;
}

Instruct(PUSHACCMANY) {
int first = *pc++;
int size = *pc++;
int i;
print_instr("PUSHACCMANY");
sp -= size;
for (i = 1; i < size; ++i) sp[i - 1] = sp[first + i];
sp[size - 1] = accu;
accu = sp[first];
Next;
}

Instruct(POP){
print_instr("POP");
sp += *pc++;
Expand Down
1 change: 1 addition & 0 deletions kernel/genOpcodeFiles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let opcodes =
"PUSHACC6", 0;
"PUSHACC7", 0;
"PUSHACC", 1;
"PUSHACCMANY", 2;
"POP", 1;
"ENVACC0", 0;
"ENVACC1", 0;
Expand Down
18 changes: 14 additions & 4 deletions kernel/vmemitcodes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,20 @@ let rec emit env insns remaining = match insns with
| (first::rest) -> emit env first rest)
(* Peephole optimizations *)
| Kpush :: Kacc n :: c ->
if n = 0 then out env opPUSH
else if n < 8 then out env (opPUSHACC1 + n - 1)
else (out env opPUSHACC; out_int env n);
emit env c remaining
let rec aux n c nb =
match c with
| Kpush :: Kacc j :: c when j = n -> aux n c (nb + 1)
| _ -> (nb, c) in
let (nb, c') = aux n c 1 in
if nb >= 3 || (nb >= 2 && n > 7)
then (
out env opPUSHACCMANY; out_int env n; out_int env nb;
emit env c' remaining)
else (
if n = 0 then out env opPUSH
else if n < 8 then out env (opPUSHACC1 + n - 1)
else (out env opPUSHACC; out_int env n);
emit env c remaining)
| Kpush :: Kenvacc n :: c ->
if n >= 0 && n <= 3
then out env(opPUSHENVACC0 + n)
Expand Down

0 comments on commit febfef7

Please sign in to comment.