3 ms·
From HAKMEM, Item 172: CONS: EXCH A,[EXCH A,[...[PUSHJ P,GC]]] EXCH A,CONS This allocates a cons cell for Lisp on PDP-10, by using the first instruc
by thequux 7y ago
From HAKMEM, Item 172:
CONS: EXCH A,[EXCH A,[...[PUSHJ P,GC]]]
EXCH A,CONS
This allocates a cons cell for Lisp on PDP-10, by using the first instruction in the routine as the head pointer of the free list. The first instruction puts the first word of the free list into A (and then clobbers that word with the original contents of A, initializing the cons cell). The second then swaps the first instruction with the first item in the free list.
The free list is simply a linked list of first instructions, which could be done because the PDP-10 was a 36-bit machine with an 18-bit address space, and the address field of an instruction was the entire right half: the same part of a word that was used as a pointer. So, when interpreted as a pointer, each of these instructions was just the pointer to the next cell.
The beautiful part to me is that, once you ran out of free list, the last word was a call to the garbage collector, which would build a free list of unreferenced cells and return a pointer to the second cell (with the correct opcode field) in A; the second instruction would then finish the cons operation, leaving the address of newly allocated cons cell in A.