src/wam/bytecode.lisp @ ba205f6b2875

Excise the stupid fucking `set-*` opcodes

The book uses the horribly-confusingly-named `set-*` operations for handling
subterms in query mode.  The author does this because he claims this is both
easier to understand and more performant.  In reality it is neither of these
things.

If you just name the subterm-handling opcodes something not completely stupid,
like `handle-subterm-*` instead of `unify-*` it becomes obvious what they do.

Also, despite the fact that `put-*` instructions now need to set the WAM's
`mode`, we still get about a 10% speedup here, likely from some combination of
reducing the VM loop code size and simplifying the compilation process.  So it's
not even more performant.

TL;DR: Just say "No" to `set-*`.
author Steve Losh <steve@stevelosh.com>
date Sun, 10 Jul 2016 14:21:18 +0000
parents 970e21fa14b0
children 8cd3257c58e3
(in-package #:bones.wam)

;;;; Opcodes
(declaim (inline instruction-size))
(defun* instruction-size ((opcode opcode))
  (:returns (integer 1 3))
  "Return the size of an instruction for the given opcode.

  The size includes one word for the opcode itself and one for each argument.

  "
  (eswitch (opcode)
    ;; TODO: make this thing a jump table somehow...
    (+opcode-noop+ 1)

    (+opcode-get-structure+ 3)
    (+opcode-get-variable-local+ 3)
    (+opcode-get-variable-stack+ 3)
    (+opcode-get-value-local+ 3)
    (+opcode-get-value-stack+ 3)

    (+opcode-put-structure+ 3)
    (+opcode-put-variable-local+ 3)
    (+opcode-put-variable-stack+ 3)
    (+opcode-put-value-local+ 3)
    (+opcode-put-value-stack+ 3)

    (+opcode-unify-variable-local+ 2)
    (+opcode-unify-variable-stack+ 2)
    (+opcode-unify-value-local+ 2)
    (+opcode-unify-value-stack+ 2)
    (+opcode-unify-void+ 2)

    (+opcode-call+ 2)
    (+opcode-dynamic-call+ 1)
    (+opcode-proceed+ 1)
    (+opcode-allocate+ 2)
    (+opcode-deallocate+ 1)
    (+opcode-done+ 1)
    (+opcode-try+ 2)
    (+opcode-retry+ 2)
    (+opcode-trust+ 1)
    (+opcode-cut+ 1)

    (+opcode-get-constant+ 3)
    (+opcode-put-constant+ 3)
    (+opcode-unify-constant+ 2)

    (+opcode-get-list+ 2)
    (+opcode-put-list+ 2)))


(defun* opcode-name ((opcode opcode))
  (:returns string)
  (eswitch (opcode)
    (+opcode-noop+ "NOOP")

    (+opcode-get-structure+ "GET-STRUCTURE")
    (+opcode-get-variable-local+ "GET-VARIABLE")
    (+opcode-get-variable-stack+ "GET-VARIABLE")
    (+opcode-get-value-local+ "GET-VALUE")
    (+opcode-get-value-stack+ "GET-VALUE")

    (+opcode-put-structure+ "PUT-STRUCTURE")
    (+opcode-put-variable-local+ "PUT-VARIABLE")
    (+opcode-put-variable-stack+ "PUT-VARIABLE")
    (+opcode-put-value-local+ "PUT-VALUE")
    (+opcode-put-value-stack+ "PUT-VALUE")

    (+opcode-unify-variable-local+ "UNIFY-VARIABLE")
    (+opcode-unify-variable-stack+ "UNIFY-VARIABLE")
    (+opcode-unify-value-local+ "UNIFY-VALUE")
    (+opcode-unify-value-stack+ "UNIFY-VALUE")
    (+opcode-unify-void+ "UNIFY-VOID")

    (+opcode-call+ "CALL")
    (+opcode-dynamic-call+ "DYNAMIC-CALL")
    (+opcode-proceed+ "PROCEED")
    (+opcode-allocate+ "ALLOCATE")
    (+opcode-deallocate+ "DEALLOCATE")
    (+opcode-done+ "DONE")
    (+opcode-try+ "TRY")
    (+opcode-retry+ "RETRY")
    (+opcode-trust+ "TRUST")
    (+opcode-cut+ "CUT")

    (+opcode-get-constant+ "GET-CONSTANT")
    (+opcode-put-constant+ "PUT-CONSTANT")
    (+opcode-unify-constant+ "UNIFY-CONSTANT")

    (+opcode-get-list+ "GET-LIST")
    (+opcode-put-list+ "PUT-LIST")))

(defun* opcode-short-name ((opcode opcode))
  (:returns string)
  (eswitch (opcode)
    (+opcode-noop+ "NOOP")

    (+opcode-get-structure+ "GETS")
    (+opcode-get-variable-local+ "GVAR")
    (+opcode-get-variable-stack+ "GVAR")
    (+opcode-get-value-local+ "GVLU")
    (+opcode-get-value-stack+ "GVLU")

    (+opcode-put-structure+ "PUTS")
    (+opcode-put-variable-local+ "PVAR")
    (+opcode-put-variable-stack+ "PVAR")
    (+opcode-put-value-local+ "PVLU")
    (+opcode-put-value-stack+ "PVLU")

    (+opcode-unify-variable-local+ "UVAR")
    (+opcode-unify-variable-stack+ "UVAR")
    (+opcode-unify-value-local+ "UVLU")
    (+opcode-unify-value-stack+ "UVLU")
    (+opcode-unify-void+ "UVOI")

    (+opcode-call+ "CALL")
    (+opcode-dynamic-call+ "DYCL")
    (+opcode-proceed+ "PROC")
    (+opcode-allocate+ "ALOC")
    (+opcode-deallocate+ "DEAL")
    (+opcode-done+ "DONE")
    (+opcode-try+ "TRYM")
    (+opcode-retry+ "RTRY")
    (+opcode-trust+ "TRST")
    (+opcode-cut+ "CUTT")

    (+opcode-get-constant+ "GCON")
    (+opcode-put-constant+ "PCON")
    (+opcode-unify-constant+ "UCON")

    (+opcode-get-list+ "GLST")
    (+opcode-put-list+ "PLST")))