SECD machine is a relatively basic design of virtual machines. is generally the underlying virtual machine as a functional language.
SECD Machine's "SECD" four letters refer to the core Stack of such virtual machines, Environment, Control, DUMP, commonly referred to as registers.
SECD machine is a stack-based vm.
Stack is usually the data and results of the operation
Environment is an environment variable, usually a parameter in a function call and a local variable.
Control saves a virtual machine directive
Dump holds the call frame data when the function is called.
SECD machine commonly used directives are as follows.
- Nil push a nil into the stack
s e (NIL.c) d => (nil.s) e c d
- LDC push a constant into the stack, if the differentiated type can also be extended to multiple LD directives
s e (LDC x.c) d => (x.s) e c d
- ld Loads a variable push into the stack from the environment, because Env is layered, so it is generally a similar form of LD.
s e (LD [y x].c) d => (locate([y x], e).s) e c d
- sel This command is to achieve the conditional selection, the SEL has two parameters, the execution will pop out of a stack value, if it is true, the control register is set to the first parameter, otherwise it is set to the second parameter. The values in the current C register are also push to the dump register.
(x.s) e (SEL ct cf.c) d => s e c? (c.d)where c? is (if (not= x false) ct cf)
- join and Sel are common, when the SEL is finished executing a SELECT statement, the SEL instruction is push to the instruction pop in the D register, and then set the C register.
-
s e (JOIN.c) (c‘.d) => s e c‘ d
- The LDF loads its arguments into the stack as a function, which is added to the current environment when the function is created.
s e (LDF f.c) => ([f e].s) e c d
- The ap pops a closure object and a function parameter object from the stack, the SEC register is push to the D register, the Env carried by closure replaces the E register, the function body replaces the C register, and the parameter list is push to Env,
([f e‘] v.s) e (AP.c) d => nil (v.e‘) f (s e c.d)
- Rtn pops the return value from the stack, pops the SEC value stored in the D register, assigns it to the SEC register, and then push the return value into the stack
(x.z) e‘ (RTN.q) (s e c.d) => (x.s) e c d
- Dum push an empty environment into the current environment
s e (DUM.c) d => s (nil.e) c d
- Rap and AP are different, is the parameter will be replaced Dum empty environment, Dum and Rap union can achieve recursion.
([f (nil.e)] v.s) (nil.e) (RAP.c) d=>nil (rplaca((nil.e), v).e) f (s e c.d)
Refer to Wiki and PYSECD and this code.
SECD Machine Introduction