From 6f083a310ad267cab38eab45116a49b81eceaeff Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Tue, 5 Apr 2016 14:35:50 -0700 Subject: [PATCH] Collect all instructions into instruction groups. --- docs/metaref.rst | 2 ++ meta/cretonne/__init__.py | 45 +++++++++++++++++++++++++++++++++++++++ meta/cretonne/base.py | 6 +++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/docs/metaref.rst b/docs/metaref.rst index 8d58757b81..e9b7c9a6ec 100644 --- a/docs/metaref.rst +++ b/docs/metaref.rst @@ -72,3 +72,5 @@ class. .. autoclass:: Operand .. autoclass:: Instruction +.. autoclass:: InstructionGroup + :members: diff --git a/meta/cretonne/__init__.py b/meta/cretonne/__init__.py index bbf0e74063..a5ebb50f04 100644 --- a/meta/cretonne/__init__.py +++ b/meta/cretonne/__init__.py @@ -150,6 +150,50 @@ class ImmediateType(object): # 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): """ An instruction operand. @@ -202,6 +246,7 @@ class Instruction(object): self.__doc__ = doc self.ins = self._to_operand_tuple(ins) self.outs = self._to_operand_tuple(outs) + InstructionGroup.append(self) @staticmethod def _to_operand_tuple(x): diff --git a/meta/cretonne/base.py b/meta/cretonne/base.py index a926829d7c..604687829e 100644 --- a/meta/cretonne/base.py +++ b/meta/cretonne/base.py @@ -3,10 +3,12 @@ Cretonne base instruction set. 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 immediates import imm64, ieee32, ieee64, immvector +instructions = InstructionGroup("base", "Shared base instruction set") + Int = TypeVar('Int', 'A scalar or vector integer type') iB = TypeVar('iB', 'A scalar integer 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``. """, ins=x, outs=a) + +instructions.close()