src/wam/bytecode.lisp @ ce87039ad178

Make L2 work properly

This changes a lot of things.

First, we split apart all the register-using opcodes into local and stack
variants, and tear out the register designator stuff.  This is ugly, but will be
way faster because the check doesn't need to happen at runtime any more.  It's
made slightly less ugly with a real nasty macro.

We also change how the head and first body term in clauses interact.  It turns
out the head needs to respect the arity of the first body clause (if it's
larger), and the two clauses need to share local variable register assignments.
Apparently when HAK says "compiled as one unit" in the book he means this.
Would have been nice if he could have explained that, or at least showed an
example that makes use of it so I have a chance of noticing it.

Still to do before we move on to L3:

* Add a few comments to document the stuff added in this commit.
* Rework the query code store to fall at the beginning of the `CODE` section so
  we can just have one program counter and interpreter function to rule them
  all.
* Consider figuring out the answer extraction process (we basically need to
  modify the query compiler to treat all variables as permanent, and keep that
  mapping so we can extract them from the stack at the very end).
author Steve Losh <steve@stevelosh.com>
date Sun, 17 Apr 2016 21:36:15 +0000
parents 2f0b5c92febe
children 902d171a1a85
(in-package #:bones.wam)

;;;; Opcodes
(defun* instruction-size ((opcode opcode))
  (:returns (integer 0 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)
    (+opcode-get-structure-local+ 3)
    (+opcode-get-structure-stack+ 3)
    (+opcode-unify-variable-local+ 2)
    (+opcode-unify-variable-stack+ 2)
    (+opcode-unify-value-local+ 2)
    (+opcode-unify-value-stack+ 2)
    (+opcode-get-variable-local+ 3)
    (+opcode-get-variable-stack+ 3)
    (+opcode-get-value-local+ 3)
    (+opcode-get-value-stack+ 3)

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

    (+opcode-call+ 2)
    (+opcode-proceed+ 1)
    (+opcode-allocate+ 2)
    (+opcode-deallocate+ 1)))


(defun* opcode-name ((opcode opcode))
  (:returns string)
  (eswitch (opcode)
    (+opcode-get-structure-local+ "GET-STRUCTURE")
    (+opcode-get-structure-stack+ "GET-STRUCTURE")
    (+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-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-local+ "PUT-STRUCTURE")
    (+opcode-put-structure-stack+ "PUT-STRUCTURE")
    (+opcode-set-variable-local+ "SET-VARIABLE")
    (+opcode-set-variable-stack+ "SET-VARIABLE")
    (+opcode-set-value-local+ "SET-VALUE")
    (+opcode-set-value-stack+ "SET-VALUE")
    (+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-call+ "CALL")
    (+opcode-proceed+ "PROCEED")
    (+opcode-allocate+ "ALLOCATE")
    (+opcode-deallocate+ "DEALLOCATE")))

(defun* opcode-short-name ((opcode opcode))
  (:returns string)
  (eswitch (opcode)
    (+opcode-get-structure-local+ "GETS")
    (+opcode-get-structure-stack+ "GETS")
    (+opcode-unify-variable-local+ "UVAR")
    (+opcode-unify-variable-stack+ "UVAR")
    (+opcode-unify-value-local+ "UVLU")
    (+opcode-unify-value-stack+ "UVLU")
    (+opcode-get-variable-local+ "GVAR")
    (+opcode-get-variable-stack+ "GVAR")
    (+opcode-get-value-local+ "GVLU")
    (+opcode-get-value-stack+ "GVLU")

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

    (+opcode-call+ "CALL")
    (+opcode-proceed+ "PROC")
    (+opcode-allocate+ "ALOC")
    (+opcode-deallocate+ "DEAL")))