#!/usr/bin/env python3
"""Specialize one SQLite 3.54.1 VDBE program in a copy of the amalgamation."""

import argparse
import hashlib
import re
from pathlib import Path

EXPECTED_SHA256 = "0c657666d819f261cc4007526ad57e1669de9949df7a8ea847aa284a2bf0ee52"
GUARD = """#if defined(SQLITE_ENABLE_VDBE_LOOKUP_REGION) && !defined(SQLITE_DEBUG) \\
    && !defined(SQLITE_TEST) && !defined(SQLITE_VDBE_COVERAGE) && !defined(SQLITE_COVERAGE_TEST) \\
    && !defined(VDBE_PROFILE) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) && !defined(SQLITE_MUTATION_TEST)
# define SQLITE_VDBE_LOOKUP_REGION 1
#endif
"""

LOCAL_INT64_COLUMN = """
#ifdef SQLITE_ENABLE_VDBE_LOOKUP_INT64
      /* Header length 3, NULL rowid placeholder and serial type 6.
      ** The full eight-byte integer must be on the local page. */
      if( p2==1 && pC->nField>=2 && pC->payloadSize==11 && pC->szRow==11
       && pC->aRow[0]==3 && pC->aRow[1]==0 && pC->aRow[2]==6
      ){
        pC->cacheStatus = p->cacheCtr;
        pC->iHdrOffset = 3;
        pC->nHdrParsed = 2;
        pC->aType[0] = 0;
        pC->aType[1] = 6;
        aOffset[0] = aOffset[1] = 3;
        aOffset[2] = 11;
        pDest = &aMem[pOp->p3];
        memAboutToChange(p, pDest);
        assert( sqlite3VdbeCheckMemInvariants(pDest) );
        if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
#if defined(SQLITE_ENABLE_VDBE_LOOKUP_WIDE_LOAD) && defined(__clang__) \\
    && defined(__x86_64__) && defined(__BYTE_ORDER__) \\
    && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
        /* The guard proves that all eight bytes at offsets 3 through 10
        ** exist. The packed, may-alias type imposes only byte alignment. */
        typedef struct __attribute__((packed, may_alias)) {
          u64 value;
        } LookupWideValue;
        pDest->u.i = (i64)__builtin_bswap64(
            ((const LookupWideValue *)(pC->aRow + 3))->value);
#else
        pDest->u.i = (i64)sqlite3Get8byte(pC->aRow + 3);
#endif
        pDest->flags = MEM_Int;
        goto op_column_out;
      }
#endif
"""

# (opcode, P1, P2, P3, P4 type, P5). None denotes a runtime operand.
PROGRAM = [
    ("Init", None, 7, 0, "P4_NOTUSED", 0),
    ("OpenRead", 0, None, 0, "P4_INT32", 0),
    ("Variable", 1, 1, 0, "P4_NOTUSED", 0),
    ("SeekRowid", 0, 6, 1, "P4_NOTUSED", 0),
    ("Column", 0, 1, 2, "P4_NOTUSED", 0),
    ("ResultRow", 2, 1, 0, "P4_NOTUSED", 0),
    ("Halt", 0, 0, 0, "P4_NOTUSED", 0),
    ("Transaction", 0, 0, None, "P4_INT32", 1),
    ("Goto", 0, 1, 0, "P4_NOTUSED", 0),
]


def replace_once(source, old, new):
    if source.count(old) != 1:
        raise ValueError(f"expected exactly one occurrence of {old!r}")
    return source.replace(old, new, 1)


def guarded(source):
    return "#ifdef SQLITE_VDBE_LOOKUP_REGION\n" + source + "#endif\n"


def match_program():
    conditions = [
        "p->nOp==9", "p->nCursor==1", "p->nVar==1", "p->nMem>=3",
        "p->nResColumn==1", "p->readOnly", "p->bIsReader", "!p->explain",
        "p->pProgram==0",
    ]
    for pc, (opcode, *operands) in enumerate(PROGRAM):
        fields = [("opcode", "OP_" + opcode)]
        fields += zip(("p1", "p2", "p3", "p4type", "p5"), operands)
        conditions.extend(
            f"aOp[{pc}].{field}=={value}"
            for field, value in fields if value is not None
        )
    conditions.extend(["aOp[1].p2>=2", "aOp[1].p4.i==2"])
    return (
        "/* The fixed region is valid for this prepared program only. */\n"
        "static int vdbeMatchesLookupRegion(const Vdbe *p){\n"
        "  const Op *aOp = p->aOp;\n"
        "  return " + "\n      && ".join(conditions) + ";\n"
        "}\n"
    )


def case_body(executor, name):
    start = re.search(rf"^case OP_{name}: \{{[^\n]*\n", executor, re.M)
    if not start:
        raise ValueError(f"case body not found for {name}")
    end = executor.index("\n}", start.end())
    return executor[start.end():end]


def finish_with(body, target):
    if not body.endswith("\n  break;"):
        raise ValueError(f"missing final break before {target}")
    return body.removesuffix("\n  break;") + f"\n  goto {target};"


def region_instruction(executor, pc, spec):
    name, *operands = spec
    if name == "OpenRead":
        declarations = case_body(executor, "ReopenIdx").split(
            "\n\n  assert(", 1
        )[0]
        start = executor.index("\ncase OP_OpenWrite:\n") + len("\ncase OP_OpenWrite:\n")
        body = declarations + "\n" + executor[start:executor.index("\n}", start)]
        body = replace_once(body, "\nopen_cursor_set_hints:", "")
        body = finish_with(body, "lookup_pc2")
    else:
        body = case_body(executor, name)
    if name == "Init":
        body = replace_once(body, "    if( pOp->opcode==OP_Trace ) break;\n", "")
        body = replace_once(body, "goto jump_to_p2;", "goto lookup_pc7;")
    elif name == "SeekRowid":
        body = replace_once(
            body,
            "  /* Fall through into OP_NotExists */\n"
            "  /* no break */ deliberate_fall_through\n"
            "case OP_NotExists:          /* jump, in3, ncycle */",
            "  /* Reuse the B-tree seek shared with OP_NotExists. */",
        )
        body = body.replace("goto jump_to_p2;", "goto lookup_pc6;")
        body = finish_with(body, "lookup_pc4")
    elif name == "Column":
        normal, corrupt = body.split("\nop_column_corrupt:", 1)
        if not corrupt.startswith("\n  if( aOp[0].p3>0 )"):
            raise ValueError("unexpected corruption handler")
        fetched = "      assert( pC->szRow<=65536 );  /* Maximum page size is 64KiB */\n"
        normal = replace_once(normal, fetched, fetched + LOCAL_INT64_COLUMN)
        body = finish_with(normal.rstrip(), "lookup_pc5") + (
            "\n\nop_column_corrupt:\n"
            "  /* The matcher requires Init.P3 == 0. */\n"
            "  rc = SQLITE_CORRUPT_BKPT;\n"
            "  goto abort_due_to_error;"
        )
    elif name == "Halt":
        start = body.index("  if( p->pFrame &&")
        end = body.index("  p->rc = pOp->p1;", start)
        body = (
            body[:start].replace("  VdbeFrame *pFrame;\n  int pcx;\n", "")
            + "  assert( p->pFrame==0 );\n" + body[end:]
        )
    elif name == "Goto":
        body = body[body.index("  pOp = &aOp[pOp->p2 - 1];"):]
        body = replace_once(body, "\ncheck_for_interrupt:\n", "\n")
        body = finish_with(body, "lookup_pc1")
    elif name in ("Variable", "Transaction"):
        body = finish_with(body, f"lookup_pc{pc + 1}")

    constants = dict(zip(("p1", "p2", "p3", "p4type", "p5"), operands))
    constants["opcode"] = "OP_" + name
    if name == "OpenRead":
        constants["p4.i"] = 2
    body = re.sub(
        r"\bpOp->(p4\.i|opcode|p4type|p[1235])\b",
        lambda match: str(constants[match[1]])
        if constants.get(match[1]) is not None else match[0],
        body,
    )
    return f"lookup_pc{pc}: {{ /* OP_{name} */\n  pOp = &aOp[{pc}];\n  nVmStep++;\n{body}\n}}\n"


def specialize(source):
    source = replace_once(source, "struct Vdbe {\n", GUARD + "\nstruct Vdbe {\n")
    shape_flag = "  bft lookupRegion:1;    /* This program matches the fixed lookup region */\n"
    field = "  bft haveEqpOps:1;       /* Bytecode supports EXPLAIN QUERY PLAN */\n"
    source = replace_once(source, field, field + guarded(shape_flag))
    make_ready = "SQLITE_PRIVATE void sqlite3VdbeMakeReady(\n"
    source = replace_once(source, make_ready, guarded(match_program()) + "\n" + make_ready)
    init_cursor = "    memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));\n"
    source = replace_once(
        source, init_cursor,
        init_cursor + guarded("    p->lookupRegion = vdbeMatchesLookupRegion(p);\n"),
    )
    call = "    rc = sqlite3VdbeExec(p);\n"
    source = replace_once(
        source, call,
        guarded(
            "    if( p->lookupRegion && p->pFrame==0\n"
            "     && (p->pc==0 || p->pc==6 || p->pc==7)\n"
            "    ){\n"
            "      rc = sqlite3VdbeExecLookup(p);\n"
            "    }else\n"
        ) + "    {\n" + call + "    }\n",
    )
    prototype = "SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);\n"
    source = replace_once(
        source, prototype, prototype + guarded("static int sqlite3VdbeExecLookup(Vdbe*);\n"),
    )
    start = source.index("SQLITE_PRIVATE int sqlite3VdbeExec(\n")
    end = source.index("\n/************** End of vdbe.c", start)
    executor = source[start:end]
    prologue = executor[:executor.index("  for(pOp=&aOp[p->pc];")]
    prologue = replace_once(
        prologue, "SQLITE_PRIVATE int sqlite3VdbeExec(",
        "static SQLITE_NOINLINE int sqlite3VdbeExecLookup(",
    )
    prologue = replace_once(prologue, "  int iCompare = 0;          /* Result of last comparison */\n", "")
    prologue = replace_once(prologue, "  Mem *pIn1 = 0;             /* 1st input operand */\n", "")
    entries = (
        "  /* sqlite3Step has checked the legal resume points. */\n"
        "  if( p->pc==6 ) goto lookup_pc6;\n"
        "  if( p->pc==7 ) goto lookup_pc7;\n"
    )
    instructions = "\n".join(
        region_instruction(executor, pc, PROGRAM[pc]) for pc in [0, 7, 8, 1, 2, 3, 4, 5, 6]
    )
    epilogue = executor[executor.index("  /* If we reach this point,"):]
    section = (
        "/* Fixed, prepare-time-matched lookup region. Opcode semantics and cleanup\n"
        "** are derived from this amalgamation by scripts/apply-vdbe-region. */\n"
    )
    return source[:end] + "\n" + guarded(section + prologue + entries + instructions + epilogue) + source[end:]


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("amalgamation", type=Path, help="unmodified SQLite 3.54.1 sqlite3.c")
    parser.add_argument("output", type=Path, help="a different file for the specialized amalgamation")
    args = parser.parse_args()
    if args.amalgamation.resolve() == args.output.resolve():
        parser.error("the specialized amalgamation requires a separate output path")
    original = args.amalgamation.read_bytes()
    if hashlib.sha256(original).hexdigest() != EXPECTED_SHA256:
        parser.error("the input does not match the pinned SQLite 3.54.1 amalgamation")
    args.output.write_text(specialize(original.decode("utf-8")), encoding="utf-8")


if __name__ == "__main__":
    main()
