source/projects/shoggoth/src/python/ichor/bootstrap.py

156 lines
3.5 KiB
Python
Raw Normal View History

"""Shogoth bootstrap code.
Some utterly trivial functions and types that allow me to begin testing the VM.
Hopefully no "real" interpreter ever uses this code, since it's obviously replaceable.
"""
2022-06-14 02:23:43 +00:00
from ichor.isa import Opcode
from ichor.state import Module
2022-05-31 15:41:10 +00:00
BOOTSTRAP = Module()
2022-06-01 05:08:29 +00:00
NOT1 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";not;bool;bool",
[
Opcode.IF(target=3),
Opcode.FALSE(),
Opcode.RETURN(1),
Opcode.TRUE(),
Opcode.RETURN(1),
],
)
2022-06-01 05:08:29 +00:00
OR2 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";or;bool,bool;bool",
[
Opcode.IF(target=3),
Opcode.TRUE(),
Opcode.RETURN(1),
Opcode.IF(target=6),
Opcode.TRUE(),
Opcode.RETURN(1),
Opcode.FALSE(),
Opcode.RETURN(1)
],
)
2022-06-01 05:08:29 +00:00
OR3 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";or;bool,bool,bool;bool",
2022-06-01 05:08:29 +00:00
[
# A B C
Opcode.IDENTIFIERC(OR2),
Opcode.FUNREF(),
# FIXME: This could be tightened by using ROT maybe...
Opcode.SLOT(0),
Opcode.SLOT(1),
Opcode.SLOT(3),
Opcode.CALLF(2), # A|B
Opcode.SLOT(2),
Opcode.SLOT(3),
Opcode.CALLF(2), # A|B|C
2022-06-01 05:08:29 +00:00
Opcode.RETURN(1),
]
)
AND2 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";and;bool,bool;bool",
[
Opcode.IF(target=3),
Opcode.IF(target=3),
Opcode.GOTO(target=5),
Opcode.FALSE(),
Opcode.RETURN(1),
Opcode.TRUE(),
Opcode.RETURN(1),
],
)
2022-06-01 05:08:29 +00:00
AND3 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";and;bool,bool,bool;bool",
2022-06-01 05:08:29 +00:00
[
# A B C
Opcode.IDENTIFIERC(AND2),
Opcode.FUNREF(),
2022-06-11 21:04:55 +00:00
Opcode.SLOT(0), # C <and2> A B C
Opcode.SLOT(1), # B C <and2> A B C
Opcode.SLOT(3), # <and2> B C <and2> A B C
Opcode.CALLF(2), # B&C <and2> A B C
Opcode.SLOT(2), # A B&C <and2> A B C
Opcode.SLOT(3), # <and2> A B&C <and2> A B C
Opcode.CALLF(2), # A&B&C <and2> A B C
2022-06-01 05:08:29 +00:00
Opcode.RETURN(1),
],
)
XOR2 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";xor;bool,bool;bool",
[
Opcode.IDENTIFIERC(AND2),
Opcode.FUNREF(),
Opcode.IDENTIFIERC(NOT1),
Opcode.FUNREF(),
Opcode.SLOT(0),
Opcode.SLOT(1),
Opcode.DUP(nargs=2),
# !A && B
Opcode.SLOT(3), # not
Opcode.CALLF(1),
Opcode.SLOT(2), # and
Opcode.CALLF(2),
Opcode.IF(target=14),
Opcode.TRUE(),
Opcode.RETURN(1),
# !B && A
Opcode.ROT(2),
Opcode.SLOT(3), # not
Opcode.CALLF(1),
Opcode.SLOT(2), # and
Opcode.CALLF(2),
Opcode.IF(target=22),
Opcode.TRUE(),
Opcode.RETURN(1),
Opcode.FALSE(),
Opcode.RETURN(1),
],
)
2022-06-01 05:08:29 +00:00
XOR3 = BOOTSTRAP.define_function(
2022-06-01 05:17:32 +00:00
";xor;bool,bool,bool;bool",
2022-06-01 05:08:29 +00:00
[
Opcode.IDENTIFIERC(XOR2),
Opcode.FUNREF(),
Opcode.IDENTIFIERC(OR2),
Opcode.FUNREF(),
Opcode.SLOT(0),
Opcode.SLOT(1),
Opcode.SLOT(2),
2022-06-01 05:08:29 +00:00
# A B C
Opcode.ROT(nargs=3), # C A B
Opcode.ROT(nargs=3), # B C A
Opcode.DUP(nargs=1), # B B C A
Opcode.ROT(nargs=4), # A B B C
Opcode.SLOT(3),
Opcode.CALLF(2), # A^B B C
2022-06-01 05:08:29 +00:00
Opcode.ROT(nargs=3), # C A^B B
Opcode.ROT(nargs=3), # B C A^B
Opcode.SLOT(3),
Opcode.CALLF(2), # B^C A^B
Opcode.SLOT(4),
Opcode.CALLF(2), # A^B|B^C
2022-06-01 05:08:29 +00:00
Opcode.RETURN(1),
]
)
BOOL = BOOTSTRAP.define_type(
2022-06-13 17:44:28 +00:00
";bool",
{
";true;": [],
";false;": [],
},
)