Collect all instructions into instruction groups.

This commit is contained in:
Jakob Stoklund Olesen
2016-04-05 14:35:50 -07:00
parent d32d78d16c
commit 6f083a310a
3 changed files with 52 additions and 1 deletions

View File

@@ -72,3 +72,5 @@ class.
.. autoclass:: Operand .. autoclass:: Operand
.. autoclass:: Instruction .. autoclass:: Instruction
.. autoclass:: InstructionGroup
:members:

View File

@@ -150,6 +150,50 @@ class ImmediateType(object):
# Defining instructions. # Defining instructions.
# #
class InstructionGroup(object):
"""
An instruction group.
Every instruction must belong to exactly one instruction group. A given
target architecture can support instructions from multiple groups, and it
does not necessarily support all instructions in a group.
New instructions are automatically added to the currently open instruction
group.
"""
# The currently open instruction group.
_current = None
def open(self):
"""
Open this instruction group such that future new instructions are
added to this group.
"""
assert InstructionGroup._current is None, (
"Can't open {} since {} is already open".format(self, _current))
InstructionGroup._current = self
def close(self):
"""
Close this instruction group. This function should be called before
opening another instruction group.
"""
assert InstructionGroup._current is self, (
"Can't close {}, the open instuction group is {}".format(self, _current))
InstructionGroup._current = None
def __init__(self, name, doc):
self.name = name
self.__doc__ = doc
self.instructions = []
self.open()
@staticmethod
def append(inst):
assert InstructionGroup._current, "Open an instruction group before defining instructions."
InstructionGroup._current.instructions.append(inst)
class Operand(object): class Operand(object):
""" """
An instruction operand. An instruction operand.
@@ -202,6 +246,7 @@ class Instruction(object):
self.__doc__ = doc self.__doc__ = doc
self.ins = self._to_operand_tuple(ins) self.ins = self._to_operand_tuple(ins)
self.outs = self._to_operand_tuple(outs) self.outs = self._to_operand_tuple(outs)
InstructionGroup.append(self)
@staticmethod @staticmethod
def _to_operand_tuple(x): def _to_operand_tuple(x):

View File

@@ -3,10 +3,12 @@ Cretonne base instruction set.
This module defines the basic Cretonne instruction set that all targets support. This module defines the basic Cretonne instruction set that all targets support.
""" """
from . import TypeVar, Operand, Instruction from . import TypeVar, Operand, Instruction, InstructionGroup
from types import i8, f32, f64 from types import i8, f32, f64
from immediates import imm64, ieee32, ieee64, immvector from immediates import imm64, ieee32, ieee64, immvector
instructions = InstructionGroup("base", "Shared base instruction set")
Int = TypeVar('Int', 'A scalar or vector integer type') Int = TypeVar('Int', 'A scalar or vector integer type')
iB = TypeVar('iB', 'A scalar integer type') iB = TypeVar('iB', 'A scalar integer type')
TxN = TypeVar('%Tx%N', 'A SIMD vector type') TxN = TypeVar('%Tx%N', 'A SIMD vector type')
@@ -321,3 +323,5 @@ popcnt = Instruction('popcnt', r"""
Count the number of one bits in ``x``. Count the number of one bits in ``x``.
""", """,
ins=x, outs=a) ins=x, outs=a)
instructions.close()