2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
The :mod:`ir` module contains the intermediate representation
|
|
|
|
of the ARTIQ compiler.
|
|
|
|
"""
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
from collections import OrderedDict
|
|
|
|
from pythonparser import ast
|
2015-12-25 15:02:33 +08:00
|
|
|
from . import types, builtins, iodelay
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
# Generic SSA IR classes
|
|
|
|
|
|
|
|
def escape_name(name):
|
2015-07-14 11:44:16 +08:00
|
|
|
if all([str.isalnum(x) or x == "." for x in name]):
|
2015-07-11 23:46:37 +08:00
|
|
|
return name
|
|
|
|
else:
|
|
|
|
return "\"{}\"".format(name.replace("\"", "\\\""))
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class TBasicBlock(types.TMono):
|
2015-07-11 23:46:37 +08:00
|
|
|
def __init__(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__("label")
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-07-21 09:54:34 +08:00
|
|
|
def is_basic_block(typ):
|
|
|
|
return isinstance(typ, TBasicBlock)
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class TOption(types.TMono):
|
2016-04-26 06:05:32 +08:00
|
|
|
def __init__(self, value):
|
|
|
|
super().__init__("option", {"value": value})
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-07-21 09:54:34 +08:00
|
|
|
def is_option(typ):
|
|
|
|
return isinstance(typ, TOption)
|
|
|
|
|
2016-04-26 06:05:32 +08:00
|
|
|
class TKeyword(types.TMono):
|
|
|
|
def __init__(self, value):
|
|
|
|
super().__init__("keyword", {"value": value})
|
|
|
|
|
|
|
|
def is_keyword(typ):
|
|
|
|
return isinstance(typ, TKeyword)
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
class Value:
|
|
|
|
"""
|
|
|
|
An SSA value that keeps track of its uses.
|
|
|
|
|
|
|
|
:ivar type: (:class:`.types.Type`) type of this value
|
|
|
|
:ivar uses: (list of :class:`Value`) values that use this value
|
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, typ):
|
2015-07-18 02:29:06 +08:00
|
|
|
self.uses, self.type = set(), typ.find()
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def replace_all_uses_with(self, value):
|
2015-10-09 06:32:27 +08:00
|
|
|
for user in set(self.uses):
|
2015-07-11 23:46:37 +08:00
|
|
|
user.replace_uses_of(self, value)
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.as_entity(type_printer=types.TypePrinter())
|
|
|
|
|
2015-07-14 11:44:16 +08:00
|
|
|
class Constant(Value):
|
|
|
|
"""
|
|
|
|
A constant value.
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
:ivar value: (Python object) value
|
2015-07-14 11:44:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, value, typ):
|
|
|
|
super().__init__(typ)
|
|
|
|
self.value = value
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_operand(self, type_printer):
|
|
|
|
return self.as_entity(type_printer)
|
2015-07-14 11:44:16 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
|
|
|
return "{} {}".format(type_printer.name(self.type),
|
2015-07-14 11:44:16 +08:00
|
|
|
repr(self.value))
|
|
|
|
|
2015-11-21 03:22:47 +08:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.value)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, Constant) and \
|
|
|
|
other.type == self.type and other.value == self.value
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not (self == other)
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
class NamedValue(Value):
|
|
|
|
"""
|
|
|
|
An SSA value that has a name.
|
|
|
|
|
|
|
|
:ivar name: (string) name of this value
|
|
|
|
:ivar function: (:class:`Function`) function containing this value
|
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, typ, name):
|
2015-07-11 23:46:37 +08:00
|
|
|
super().__init__(typ)
|
|
|
|
self.name, self.function = name, None
|
|
|
|
|
|
|
|
def set_name(self, new_name):
|
|
|
|
if self.function is not None:
|
|
|
|
self.function._remove_name(self.name)
|
|
|
|
self.name = self.function._add_name(new_name)
|
|
|
|
else:
|
|
|
|
self.name = new_name
|
|
|
|
|
|
|
|
def _set_function(self, new_function):
|
|
|
|
if self.function != new_function:
|
|
|
|
if self.function is not None:
|
|
|
|
self.function._remove_name(self.name)
|
|
|
|
self.function = new_function
|
|
|
|
if self.function is not None:
|
|
|
|
self.name = self.function._add_name(self.name)
|
|
|
|
|
|
|
|
def _detach(self):
|
|
|
|
self.function = None
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_operand(self, type_printer):
|
|
|
|
return "{} %{}".format(type_printer.name(self.type),
|
2015-07-11 23:46:37 +08:00
|
|
|
escape_name(self.name))
|
|
|
|
|
|
|
|
class User(NamedValue):
|
|
|
|
"""
|
|
|
|
An SSA value that has operands.
|
|
|
|
|
|
|
|
:ivar operands: (list of :class:`Value`) operands of this value
|
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, operands, typ, name):
|
2015-07-11 23:46:37 +08:00
|
|
|
super().__init__(typ, name)
|
|
|
|
self.operands = []
|
2015-07-22 07:58:59 +08:00
|
|
|
self.set_operands(operands)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def set_operands(self, new_operands):
|
2015-07-22 07:58:59 +08:00
|
|
|
for operand in set(self.operands):
|
2015-07-11 23:46:37 +08:00
|
|
|
operand.uses.remove(self)
|
|
|
|
self.operands = new_operands
|
2015-07-22 07:58:59 +08:00
|
|
|
for operand in set(self.operands):
|
2015-07-11 23:46:37 +08:00
|
|
|
operand.uses.add(self)
|
|
|
|
|
|
|
|
def drop_references(self):
|
|
|
|
self.set_operands([])
|
|
|
|
|
|
|
|
def replace_uses_of(self, value, replacement):
|
2015-10-09 06:32:27 +08:00
|
|
|
assert value in self.operands
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-10-09 06:32:27 +08:00
|
|
|
for index, operand in enumerate(self.operands):
|
2015-07-11 23:46:37 +08:00
|
|
|
if operand == value:
|
2015-10-09 06:32:27 +08:00
|
|
|
self.operands[index] = replacement
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
value.uses.remove(self)
|
|
|
|
replacement.uses.add(self)
|
|
|
|
|
|
|
|
class Instruction(User):
|
|
|
|
"""
|
|
|
|
An SSA instruction.
|
2015-08-10 18:14:52 +08:00
|
|
|
|
|
|
|
:ivar loc: (:class:`pythonparser.source.Range` or None)
|
|
|
|
source location
|
2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, operands, typ, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(operands, list)
|
|
|
|
assert isinstance(typ, types.Type)
|
2015-07-14 01:52:48 +08:00
|
|
|
super().__init__(operands, typ, name)
|
2015-07-11 23:46:37 +08:00
|
|
|
self.basic_block = None
|
2015-07-18 12:49:42 +08:00
|
|
|
self.loc = None
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = self.__class__.__new__(self.__class__)
|
|
|
|
Instruction.__init__(self_copy, list(map(mapper, self.operands)),
|
|
|
|
self.type, self.name)
|
2015-12-25 14:59:28 +08:00
|
|
|
self_copy.loc = self.loc
|
2015-11-23 23:58:37 +08:00
|
|
|
return self_copy
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
def set_basic_block(self, new_basic_block):
|
|
|
|
self.basic_block = new_basic_block
|
|
|
|
if self.basic_block is not None:
|
|
|
|
self._set_function(self.basic_block.function)
|
|
|
|
else:
|
|
|
|
self._set_function(None)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
"""String representation of the opcode."""
|
|
|
|
return "???"
|
|
|
|
|
|
|
|
def _detach(self):
|
|
|
|
self.set_basic_block(None)
|
|
|
|
|
|
|
|
def remove_from_parent(self):
|
|
|
|
if self.basic_block is not None:
|
|
|
|
self.basic_block.remove(self)
|
|
|
|
|
|
|
|
def erase(self):
|
|
|
|
self.remove_from_parent()
|
|
|
|
self.drop_references()
|
2015-07-22 07:58:59 +08:00
|
|
|
# Check this after drop_references in case this
|
|
|
|
# is a self-referencing phi.
|
|
|
|
assert not any(self.uses)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def replace_with(self, value):
|
|
|
|
self.replace_all_uses_with(value)
|
|
|
|
if isinstance(value, Instruction):
|
|
|
|
self.basic_block.replace(self, value)
|
|
|
|
self.drop_references()
|
|
|
|
else:
|
|
|
|
self.erase()
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
|
|
|
return ", ".join([operand.as_operand(type_printer) for operand in self.operands])
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
2015-11-21 03:22:47 +08:00
|
|
|
if builtins.is_none(self.type) and len(self.uses) == 0:
|
2015-07-11 23:46:37 +08:00
|
|
|
prefix = ""
|
|
|
|
else:
|
|
|
|
prefix = "%{} = {} ".format(escape_name(self.name),
|
2015-11-17 05:23:34 +08:00
|
|
|
type_printer.name(self.type))
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
if any(self.operands):
|
2015-11-17 05:23:34 +08:00
|
|
|
return "{}{} {}".format(prefix, self.opcode(),
|
|
|
|
self._operands_as_string(type_printer))
|
2015-07-11 23:46:37 +08:00
|
|
|
else:
|
2015-07-14 11:44:16 +08:00
|
|
|
return "{}{}".format(prefix, self.opcode())
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
class Phi(Instruction):
|
|
|
|
"""
|
|
|
|
An SSA instruction that joins data flow.
|
2015-07-14 01:52:48 +08:00
|
|
|
|
|
|
|
Use :meth:`incoming` and :meth:`add_incoming` instead of
|
|
|
|
directly reading :attr:`operands` or calling :meth:`set_operands`.
|
2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, typ, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__([], typ, name)
|
2015-07-14 01:52:48 +08:00
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "phi"
|
|
|
|
|
|
|
|
def incoming(self):
|
|
|
|
operand_iter = iter(self.operands)
|
|
|
|
while True:
|
2015-12-30 15:33:30 +08:00
|
|
|
try:
|
|
|
|
yield next(operand_iter), next(operand_iter)
|
|
|
|
except StopIteration:
|
|
|
|
return
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def incoming_blocks(self):
|
2015-12-30 16:06:18 +08:00
|
|
|
return (block for (value, block) in self.incoming())
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def incoming_values(self):
|
2015-12-30 16:06:18 +08:00
|
|
|
return (value for (value, block) in self.incoming())
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def incoming_value_for_block(self, target_block):
|
2015-12-30 16:06:18 +08:00
|
|
|
for (value, block) in self.incoming():
|
2015-07-11 23:46:37 +08:00
|
|
|
if block == target_block:
|
|
|
|
return value
|
|
|
|
assert False
|
|
|
|
|
|
|
|
def add_incoming(self, value, block):
|
|
|
|
assert value.type == self.type
|
|
|
|
self.operands.append(value)
|
2015-07-22 07:58:59 +08:00
|
|
|
value.uses.add(self)
|
2015-07-11 23:46:37 +08:00
|
|
|
self.operands.append(block)
|
2015-07-22 07:58:59 +08:00
|
|
|
block.uses.add(self)
|
|
|
|
|
|
|
|
def remove_incoming_value(self, value):
|
|
|
|
index = self.operands.index(value)
|
2015-12-30 16:06:18 +08:00
|
|
|
assert index % 2 == 0
|
2015-07-22 07:58:59 +08:00
|
|
|
self.operands[index].uses.remove(self)
|
|
|
|
self.operands[index + 1].uses.remove(self)
|
|
|
|
del self.operands[index:index + 2]
|
|
|
|
|
|
|
|
def remove_incoming_block(self, block):
|
|
|
|
index = self.operands.index(block)
|
2015-12-30 16:06:18 +08:00
|
|
|
assert index % 2 == 1
|
2015-07-22 07:58:59 +08:00
|
|
|
self.operands[index - 1].uses.remove(self)
|
|
|
|
self.operands[index].uses.remove(self)
|
|
|
|
del self.operands[index - 1:index + 1]
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
2015-07-11 23:46:37 +08:00
|
|
|
if builtins.is_none(self.type):
|
|
|
|
prefix = ""
|
|
|
|
else:
|
|
|
|
prefix = "%{} = {} ".format(escape_name(self.name),
|
2015-11-17 05:23:34 +08:00
|
|
|
type_printer.name(self.type))
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
if any(self.operands):
|
2015-11-17 05:23:34 +08:00
|
|
|
operand_list = ["%{} => {}".format(escape_name(block.name),
|
|
|
|
value.as_operand(type_printer))
|
2015-07-18 02:29:06 +08:00
|
|
|
for value, block in self.incoming()]
|
2015-07-14 11:44:16 +08:00
|
|
|
return "{}{} [{}]".format(prefix, self.opcode(), ", ".join(operand_list))
|
2015-07-18 02:29:06 +08:00
|
|
|
else:
|
|
|
|
return "{}{} [???]".format(prefix, self.opcode())
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
class Terminator(Instruction):
|
|
|
|
"""
|
|
|
|
An SSA instruction that performs control flow.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def successors(self):
|
2015-07-14 01:52:48 +08:00
|
|
|
return [operand for operand in self.operands if isinstance(operand, BasicBlock)]
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
class BasicBlock(NamedValue):
|
|
|
|
"""
|
|
|
|
A block of instructions with no control flow inside it.
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
:ivar instructions: (list of :class:`Instruction`)
|
2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def __init__(self, instructions, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__(TBasicBlock(), name)
|
2015-07-11 23:46:37 +08:00
|
|
|
self.instructions = []
|
|
|
|
self.set_instructions(instructions)
|
|
|
|
|
2015-07-14 01:52:48 +08:00
|
|
|
def set_instructions(self, new_insns):
|
|
|
|
for insn in self.instructions:
|
|
|
|
insn.detach()
|
|
|
|
self.instructions = new_insns
|
|
|
|
for insn in self.instructions:
|
|
|
|
insn.set_basic_block(self)
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
def remove_from_parent(self):
|
|
|
|
if self.function is not None:
|
|
|
|
self.function.remove(self)
|
|
|
|
|
2015-07-19 15:29:14 +08:00
|
|
|
def erase(self):
|
2015-07-22 07:58:59 +08:00
|
|
|
# self.instructions is updated while iterating
|
2015-07-27 15:13:22 +08:00
|
|
|
for insn in reversed(self.instructions):
|
2015-07-19 15:29:14 +08:00
|
|
|
insn.erase()
|
|
|
|
self.remove_from_parent()
|
2015-07-22 07:58:59 +08:00
|
|
|
# Check this after erasing instructions in case the block
|
|
|
|
# loops into itself.
|
|
|
|
assert not any(self.uses)
|
2015-07-19 15:29:14 +08:00
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
def prepend(self, insn):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(insn, Instruction)
|
2015-07-11 23:46:37 +08:00
|
|
|
insn.set_basic_block(self)
|
|
|
|
self.instructions.insert(0, insn)
|
2015-07-18 02:29:06 +08:00
|
|
|
return insn
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def append(self, insn):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(insn, Instruction)
|
2015-07-11 23:46:37 +08:00
|
|
|
insn.set_basic_block(self)
|
|
|
|
self.instructions.append(insn)
|
2015-07-14 11:44:16 +08:00
|
|
|
return insn
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def index(self, insn):
|
|
|
|
return self.instructions.index(insn)
|
|
|
|
|
2015-11-23 23:59:25 +08:00
|
|
|
def insert(self, insn, before):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(insn, Instruction)
|
2015-07-11 23:46:37 +08:00
|
|
|
insn.set_basic_block(self)
|
|
|
|
self.instructions.insert(self.index(before), insn)
|
2015-07-14 11:44:16 +08:00
|
|
|
return insn
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def remove(self, insn):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert insn in self.instructions
|
2015-07-11 23:46:37 +08:00
|
|
|
insn._detach()
|
|
|
|
self.instructions.remove(insn)
|
2015-07-14 11:44:16 +08:00
|
|
|
return insn
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def replace(self, insn, replacement):
|
2015-11-23 23:59:25 +08:00
|
|
|
self.insert(replacement, before=insn)
|
2015-07-11 23:46:37 +08:00
|
|
|
self.remove(insn)
|
|
|
|
|
2015-07-14 11:44:16 +08:00
|
|
|
def is_terminated(self):
|
|
|
|
return any(self.instructions) and isinstance(self.instructions[-1], Terminator)
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
def terminator(self):
|
2015-07-14 11:44:16 +08:00
|
|
|
assert self.is_terminated()
|
2015-07-11 23:46:37 +08:00
|
|
|
return self.instructions[-1]
|
|
|
|
|
|
|
|
def successors(self):
|
|
|
|
return self.terminator().successors()
|
|
|
|
|
|
|
|
def predecessors(self):
|
2015-07-19 01:48:11 +08:00
|
|
|
return [use.basic_block for use in self.uses if isinstance(use, Terminator)]
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
2015-07-18 12:49:42 +08:00
|
|
|
# Header
|
2015-07-11 23:46:37 +08:00
|
|
|
lines = ["{}:".format(escape_name(self.name))]
|
2015-07-18 02:29:06 +08:00
|
|
|
if self.function is not None:
|
|
|
|
lines[0] += " ; predecessors: {}".format(
|
|
|
|
", ".join([escape_name(pred.name) for pred in self.predecessors()]))
|
2015-07-18 12:49:42 +08:00
|
|
|
|
|
|
|
# Annotated instructions
|
|
|
|
loc = None
|
2015-07-11 23:46:37 +08:00
|
|
|
for insn in self.instructions:
|
2015-07-18 12:49:42 +08:00
|
|
|
if loc != insn.loc:
|
|
|
|
loc = insn.loc
|
|
|
|
|
|
|
|
if loc is None:
|
|
|
|
lines.append("; <synthesized>")
|
|
|
|
else:
|
|
|
|
source_lines = loc.source_lines()
|
|
|
|
beg_col, end_col = loc.column(), loc.end().column()
|
|
|
|
source_lines[-1] = \
|
2015-10-04 07:11:17 +08:00
|
|
|
source_lines[-1][:end_col] + "\x1b[0m" + source_lines[-1][end_col:]
|
2015-07-18 12:49:42 +08:00
|
|
|
source_lines[0] = \
|
2015-10-04 07:11:17 +08:00
|
|
|
source_lines[0][:beg_col] + "\x1b[1;32m" + source_lines[0][beg_col:]
|
2015-07-18 12:49:42 +08:00
|
|
|
|
|
|
|
line_desc = "{}:{}".format(loc.source_buffer.name, loc.line())
|
|
|
|
lines += ["; {} {}".format(line_desc, line.rstrip("\n"))
|
|
|
|
for line in source_lines]
|
2015-11-17 05:23:34 +08:00
|
|
|
lines.append(" " + insn.as_entity(type_printer))
|
2015-07-18 12:49:42 +08:00
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
return "\n".join(lines)
|
|
|
|
|
2015-07-19 01:48:52 +08:00
|
|
|
def __repr__(self):
|
2015-07-19 21:32:33 +08:00
|
|
|
return "<artiq.compiler.ir.BasicBlock {}>".format(repr(self.name))
|
2015-07-19 01:48:52 +08:00
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
class Argument(NamedValue):
|
|
|
|
"""
|
|
|
|
A function argument.
|
|
|
|
"""
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
|
|
|
return self.as_operand(type_printer)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-07-18 14:27:34 +08:00
|
|
|
class Function:
|
2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
A function containing SSA IR.
|
2015-07-30 01:35:16 +08:00
|
|
|
|
2015-08-10 18:14:52 +08:00
|
|
|
:ivar loc: (:class:`pythonparser.source.Range` or None)
|
|
|
|
source location of function definition
|
2015-07-30 01:35:16 +08:00
|
|
|
:ivar is_internal:
|
|
|
|
(bool) if True, the function should not be accessible from outside
|
|
|
|
the module it is contained in
|
2016-03-27 09:02:15 +08:00
|
|
|
:ivar is_cold:
|
|
|
|
(bool) if True, the function should be considered rarely called
|
2016-04-03 02:29:36 +08:00
|
|
|
:ivar is_generated:
|
|
|
|
(bool) if True, the function will not appear in backtraces
|
2016-03-29 05:25:40 +08:00
|
|
|
:ivar flags: (set of str) Code generation flags.
|
|
|
|
Flag ``fast-math`` is the equivalent of gcc's ``-ffast-math``.
|
2015-07-11 23:46:37 +08:00
|
|
|
"""
|
|
|
|
|
2015-08-10 18:14:52 +08:00
|
|
|
def __init__(self, typ, name, arguments, loc=None):
|
|
|
|
self.type, self.name, self.loc = typ, name, loc
|
2015-07-18 02:29:06 +08:00
|
|
|
self.names, self.arguments, self.basic_blocks = set(), [], []
|
2015-07-30 02:36:31 +08:00
|
|
|
self.next_name = 1
|
2015-07-14 01:52:48 +08:00
|
|
|
self.set_arguments(arguments)
|
2015-07-30 01:35:16 +08:00
|
|
|
self.is_internal = False
|
2016-03-27 09:02:15 +08:00
|
|
|
self.is_cold = False
|
2016-04-03 02:29:36 +08:00
|
|
|
self.is_generated = False
|
2016-03-29 05:25:40 +08:00
|
|
|
self.flags = {}
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def _remove_name(self, name):
|
|
|
|
self.names.remove(name)
|
|
|
|
|
|
|
|
def _add_name(self, base_name):
|
2015-07-30 02:36:31 +08:00
|
|
|
if base_name == "":
|
2016-03-26 19:38:55 +08:00
|
|
|
name = "UNN.{}".format(self.next_name)
|
2015-07-30 02:36:31 +08:00
|
|
|
self.next_name += 1
|
|
|
|
elif base_name in self.names:
|
|
|
|
name = "{}.{}".format(base_name, self.next_name)
|
|
|
|
self.next_name += 1
|
|
|
|
else:
|
|
|
|
name = base_name
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
self.names.add(name)
|
|
|
|
return name
|
|
|
|
|
|
|
|
def set_arguments(self, new_arguments):
|
|
|
|
for argument in self.arguments:
|
|
|
|
argument._set_function(None)
|
|
|
|
self.arguments = new_arguments
|
|
|
|
for argument in self.arguments:
|
|
|
|
argument._set_function(self)
|
|
|
|
|
|
|
|
def add(self, basic_block):
|
|
|
|
basic_block._set_function(self)
|
2015-07-14 11:44:16 +08:00
|
|
|
self.basic_blocks.append(basic_block)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
|
|
|
def remove(self, basic_block):
|
|
|
|
basic_block._detach()
|
2015-07-18 02:29:06 +08:00
|
|
|
self.basic_blocks.remove(basic_block)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-07-19 15:29:14 +08:00
|
|
|
def entry(self):
|
2015-07-19 01:48:52 +08:00
|
|
|
assert any(self.basic_blocks)
|
|
|
|
return self.basic_blocks[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def exits(self):
|
|
|
|
return [block for block in self.basic_blocks if not any(block.successors())]
|
|
|
|
|
2015-07-19 01:48:52 +08:00
|
|
|
def instructions(self):
|
|
|
|
for basic_block in self.basic_blocks:
|
|
|
|
yield from iter(basic_block.instructions)
|
2015-07-11 23:46:37 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
2015-11-23 21:44:38 +08:00
|
|
|
postorder = []
|
|
|
|
visited = set()
|
|
|
|
def visit(block):
|
|
|
|
visited.add(block)
|
|
|
|
for next_block in block.successors():
|
|
|
|
if next_block not in visited:
|
|
|
|
visit(next_block)
|
|
|
|
postorder.append(block)
|
|
|
|
|
|
|
|
visit(self.entry())
|
|
|
|
|
2015-07-14 01:53:02 +08:00
|
|
|
lines = []
|
|
|
|
lines.append("{} {}({}) {{ ; type: {}".format(
|
2015-11-17 05:23:34 +08:00
|
|
|
type_printer.name(self.type.ret), self.name,
|
|
|
|
", ".join([arg.as_operand(type_printer) for arg in self.arguments]),
|
|
|
|
type_printer.name(self.type)))
|
2015-11-23 23:55:12 +08:00
|
|
|
|
|
|
|
postorder_blocks = list(reversed(postorder))
|
|
|
|
orphan_blocks = [block for block in self.basic_blocks if block not in postorder]
|
|
|
|
for block in postorder_blocks + orphan_blocks:
|
2015-11-17 05:23:34 +08:00
|
|
|
lines.append(block.as_entity(type_printer))
|
2015-11-23 23:55:12 +08:00
|
|
|
|
2015-07-14 01:53:02 +08:00
|
|
|
lines.append("}")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.as_entity(types.TypePrinter())
|
|
|
|
|
2015-07-11 23:46:37 +08:00
|
|
|
# Python-specific SSA IR classes
|
2015-07-14 11:44:16 +08:00
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class TEnvironment(types.TMono):
|
2015-12-18 23:41:51 +08:00
|
|
|
def __init__(self, name, vars, outer=None):
|
|
|
|
assert isinstance(name, str)
|
|
|
|
self.env_name = name # for readable type names in LLVM IR
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
if outer is not None:
|
|
|
|
assert isinstance(outer, TEnvironment)
|
2015-08-23 04:56:17 +08:00
|
|
|
env = OrderedDict({"$outer": outer})
|
2015-07-18 02:29:06 +08:00
|
|
|
env.update(vars)
|
|
|
|
else:
|
|
|
|
env = OrderedDict(vars)
|
|
|
|
|
|
|
|
super().__init__("environment", env)
|
|
|
|
|
|
|
|
def type_of(self, name):
|
|
|
|
if name in self.params:
|
|
|
|
return self.params[name].find()
|
2015-08-23 04:56:17 +08:00
|
|
|
elif "$outer" in self.params:
|
|
|
|
return self.params["$outer"].type_of(name)
|
2015-07-18 02:29:06 +08:00
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
2015-07-18 14:10:41 +08:00
|
|
|
def outermost(self):
|
2015-08-23 04:56:17 +08:00
|
|
|
if "$outer" in self.params:
|
|
|
|
return self.params["$outer"].outermost()
|
2015-07-18 14:10:41 +08:00
|
|
|
else:
|
|
|
|
return self
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
|
|
|
Add a new binding, ensuring hygiene.
|
|
|
|
|
|
|
|
:returns: (string) mangled name
|
|
|
|
"""
|
|
|
|
def add(self, base_name, typ):
|
|
|
|
name, counter = base_name, 1
|
|
|
|
while name in self.params or name == "":
|
|
|
|
if base_name == "":
|
|
|
|
name = str(counter)
|
|
|
|
else:
|
|
|
|
name = "{}.{}".format(name, counter)
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
self.params[name] = typ.find()
|
|
|
|
return name
|
|
|
|
|
|
|
|
def is_environment(typ):
|
|
|
|
return isinstance(typ, TEnvironment)
|
|
|
|
|
2015-07-21 18:45:27 +08:00
|
|
|
class EnvironmentArgument(Argument):
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
|
|
|
A function argument specifying an outer environment.
|
|
|
|
"""
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_operand(self, type_printer):
|
2015-07-18 02:29:06 +08:00
|
|
|
return "environment(...) %{}".format(escape_name(self.name))
|
|
|
|
|
|
|
|
class Alloc(Instruction):
|
|
|
|
"""
|
|
|
|
An instruction that allocates an object specified by
|
|
|
|
the type of the intsruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, operands, typ, name=""):
|
|
|
|
for operand in operands: assert isinstance(operand, Value)
|
|
|
|
super().__init__(operands, typ, name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "alloc"
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_operand(self, type_printer):
|
2015-07-18 02:29:06 +08:00
|
|
|
if is_environment(self.type):
|
|
|
|
# Only show full environment in the instruction itself
|
|
|
|
return "%{}".format(escape_name(self.name))
|
|
|
|
else:
|
2015-11-17 05:23:34 +08:00
|
|
|
return super().as_operand(type_printer)
|
2015-07-18 02:29:06 +08:00
|
|
|
|
|
|
|
class GetLocal(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that loads a local variable from an environment,
|
|
|
|
possibly going through multiple levels of indirection.
|
|
|
|
|
|
|
|
:ivar var_name: (string) variable name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param env: (:class:`Value`) local environment
|
|
|
|
:param var_name: (string) local variable name
|
|
|
|
"""
|
|
|
|
def __init__(self, env, var_name, name=""):
|
2015-07-18 14:10:41 +08:00
|
|
|
assert isinstance(env, Value)
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(env.type, TEnvironment)
|
|
|
|
assert isinstance(var_name, str)
|
|
|
|
super().__init__([env], env.type.type_of(var_name), name)
|
|
|
|
self.var_name = var_name
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.var_name = self.var_name
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
2015-08-15 23:04:12 +08:00
|
|
|
return "getlocal({})".format(repr(self.var_name))
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def environment(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
|
|
|
class SetLocal(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that stores a local variable into an environment,
|
|
|
|
possibly going through multiple levels of indirection.
|
|
|
|
|
|
|
|
:ivar var_name: (string) variable name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param env: (:class:`Value`) local environment
|
|
|
|
:param var_name: (string) local variable name
|
|
|
|
:param value: (:class:`Value`) value to assign
|
|
|
|
"""
|
|
|
|
def __init__(self, env, var_name, value, name=""):
|
2015-07-18 14:10:41 +08:00
|
|
|
assert isinstance(env, Value)
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(env.type, TEnvironment)
|
|
|
|
assert isinstance(var_name, str)
|
|
|
|
assert env.type.type_of(var_name) == value.type
|
|
|
|
assert isinstance(value, Value)
|
|
|
|
super().__init__([env, value], builtins.TNone(), name)
|
|
|
|
self.var_name = var_name
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.var_name = self.var_name
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
2015-08-15 23:04:12 +08:00
|
|
|
return "setlocal({})".format(repr(self.var_name))
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def environment(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
|
|
|
class GetAttr(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that loads an attribute from an object,
|
|
|
|
or extracts a tuple element.
|
|
|
|
|
|
|
|
:ivar attr: (string) variable name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param obj: (:class:`Value`) object or tuple
|
|
|
|
:param attr: (string or integer) attribute or index
|
|
|
|
"""
|
|
|
|
def __init__(self, obj, attr, name=""):
|
|
|
|
assert isinstance(obj, Value)
|
|
|
|
assert isinstance(attr, (str, int))
|
|
|
|
if isinstance(attr, int):
|
|
|
|
assert isinstance(obj.type, types.TTuple)
|
|
|
|
typ = obj.type.elts[attr]
|
2016-03-25 01:56:44 +08:00
|
|
|
elif attr in obj.type.attributes:
|
2015-07-18 02:29:06 +08:00
|
|
|
typ = obj.type.attributes[attr]
|
2016-03-25 01:56:44 +08:00
|
|
|
else:
|
|
|
|
typ = obj.type.constructor.attributes[attr]
|
2016-04-26 06:05:32 +08:00
|
|
|
if types.is_function(typ) or types.is_rpc(typ):
|
2016-03-25 01:56:44 +08:00
|
|
|
typ = types.TMethod(obj.type, typ)
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__([obj], typ, name)
|
|
|
|
self.attr = attr
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.attr = self.attr
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "getattr({})".format(repr(self.attr))
|
|
|
|
|
2015-07-21 09:54:34 +08:00
|
|
|
def object(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
|
|
|
class SetAttr(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that stores an attribute to an object.
|
|
|
|
|
|
|
|
:ivar attr: (string) variable name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param obj: (:class:`Value`) object or tuple
|
|
|
|
:param attr: (string or integer) attribute
|
|
|
|
:param value: (:class:`Value`) value to store
|
|
|
|
"""
|
|
|
|
def __init__(self, obj, attr, value, name=""):
|
|
|
|
assert isinstance(obj, Value)
|
|
|
|
assert isinstance(attr, (str, int))
|
|
|
|
assert isinstance(value, Value)
|
|
|
|
if isinstance(attr, int):
|
2015-08-15 21:45:16 +08:00
|
|
|
assert value.type == obj.type.elts[attr].find()
|
2015-07-18 02:29:06 +08:00
|
|
|
else:
|
2015-08-15 21:45:16 +08:00
|
|
|
assert value.type == obj.type.attributes[attr].find()
|
2015-07-25 10:37:37 +08:00
|
|
|
super().__init__([obj, value], builtins.TNone(), name)
|
2015-07-18 02:29:06 +08:00
|
|
|
self.attr = attr
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.attr = self.attr
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "setattr({})".format(repr(self.attr))
|
|
|
|
|
2015-07-21 09:54:34 +08:00
|
|
|
def object(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
|
|
|
class GetElem(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that loads an element from a list.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param lst: (:class:`Value`) list
|
|
|
|
:param index: (:class:`Value`) index
|
|
|
|
"""
|
|
|
|
def __init__(self, lst, index, name=""):
|
|
|
|
assert isinstance(lst, Value)
|
|
|
|
assert isinstance(index, Value)
|
|
|
|
super().__init__([lst, index], builtins.get_iterable_elt(lst.type), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "getelem"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def list(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def index(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
|
|
|
class SetElem(Instruction):
|
|
|
|
"""
|
|
|
|
An intruction that stores an element into a list.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param lst: (:class:`Value`) list
|
|
|
|
:param index: (:class:`Value`) index
|
|
|
|
:param value: (:class:`Value`) value to store
|
|
|
|
"""
|
|
|
|
def __init__(self, lst, index, value, name=""):
|
|
|
|
assert isinstance(lst, Value)
|
|
|
|
assert isinstance(index, Value)
|
|
|
|
assert isinstance(value, Value)
|
|
|
|
assert builtins.get_iterable_elt(lst.type) == value.type.find()
|
|
|
|
super().__init__([lst, index, value], builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "setelem"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def list(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def index(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[2]
|
|
|
|
|
|
|
|
class Coerce(Instruction):
|
|
|
|
"""
|
|
|
|
A coercion operation for numbers.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, value, typ, name=""):
|
|
|
|
assert isinstance(value, Value)
|
|
|
|
assert isinstance(typ, types.Type)
|
|
|
|
super().__init__([value], typ, name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "coerce"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:51:53 +08:00
|
|
|
class Arith(Instruction):
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
2015-07-19 16:51:53 +08:00
|
|
|
An arithmetic operation on numbers.
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-07-19 16:51:53 +08:00
|
|
|
:ivar op: (:class:`pythonparser.ast.operator`) operation
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param op: (:class:`pythonparser.ast.operator`) operation
|
|
|
|
:param lhs: (:class:`Value`) left-hand operand
|
|
|
|
:param rhs: (:class:`Value`) right-hand operand
|
|
|
|
"""
|
|
|
|
def __init__(self, op, lhs, rhs, name=""):
|
|
|
|
assert isinstance(op, ast.operator)
|
|
|
|
assert isinstance(lhs, Value)
|
|
|
|
assert isinstance(rhs, Value)
|
|
|
|
assert lhs.type == rhs.type
|
|
|
|
super().__init__([lhs, rhs], lhs.type, name)
|
|
|
|
self.op = op
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.op = self.op
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
2015-07-19 16:51:53 +08:00
|
|
|
return "arith({})".format(type(self.op).__name__)
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def lhs(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def rhs(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
|
|
|
class Compare(Instruction):
|
|
|
|
"""
|
|
|
|
A comparison operation on numbers.
|
|
|
|
|
|
|
|
:ivar op: (:class:`pythonparser.ast.cmpop`) operation
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param op: (:class:`pythonparser.ast.cmpop`) operation
|
|
|
|
:param lhs: (:class:`Value`) left-hand operand
|
|
|
|
:param rhs: (:class:`Value`) right-hand operand
|
|
|
|
"""
|
|
|
|
def __init__(self, op, lhs, rhs, name=""):
|
|
|
|
assert isinstance(op, ast.cmpop)
|
|
|
|
assert isinstance(lhs, Value)
|
|
|
|
assert isinstance(rhs, Value)
|
|
|
|
assert lhs.type == rhs.type
|
|
|
|
super().__init__([lhs, rhs], builtins.TBool(), name)
|
|
|
|
self.op = op
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.op = self.op
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "compare({})".format(type(self.op).__name__)
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def lhs(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def rhs(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
|
|
|
class Builtin(Instruction):
|
|
|
|
"""
|
|
|
|
A builtin operation. Similar to a function call that
|
|
|
|
never raises.
|
|
|
|
|
|
|
|
:ivar op: (string) operation name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param op: (string) operation name
|
|
|
|
"""
|
2016-03-26 19:38:55 +08:00
|
|
|
def __init__(self, op, operands, typ, name=None):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(op, str)
|
|
|
|
for operand in operands: assert isinstance(operand, Value)
|
2016-03-26 19:38:55 +08:00
|
|
|
if name is None:
|
|
|
|
name = "BLT.{}".format(op)
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__(operands, typ, name)
|
|
|
|
self.op = op
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.op = self.op
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "builtin({})".format(self.op)
|
|
|
|
|
2015-07-18 14:27:34 +08:00
|
|
|
class Closure(Instruction):
|
|
|
|
"""
|
|
|
|
A closure creation operation.
|
|
|
|
|
|
|
|
:ivar target_function: (:class:`Function`) function to invoke
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param func: (:class:`Function`) function
|
|
|
|
:param env: (:class:`Value`) outer environment
|
|
|
|
"""
|
|
|
|
def __init__(self, func, env, name=""):
|
|
|
|
assert isinstance(func, Function)
|
|
|
|
assert isinstance(env, Value)
|
|
|
|
assert is_environment(env.type)
|
|
|
|
super().__init__([env], func.type, name)
|
|
|
|
self.target_function = func
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.target_function = self.target_function
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 14:27:34 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "closure({})".format(self.target_function.name)
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def environment(self):
|
2015-07-18 14:27:34 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class Call(Instruction):
|
|
|
|
"""
|
|
|
|
A function call operation.
|
2015-10-09 06:32:27 +08:00
|
|
|
|
2015-12-25 15:02:33 +08:00
|
|
|
:ivar arg_exprs: (dict of str to `iodelay.Expr`)
|
|
|
|
iodelay expressions for values of arguments
|
2015-10-09 06:32:27 +08:00
|
|
|
:ivar static_target_function: (:class:`Function` or None)
|
|
|
|
statically resolved callee
|
2016-03-27 09:02:15 +08:00
|
|
|
:ivar is_cold: (bool)
|
|
|
|
the callee function is cold
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param func: (:class:`Value`) function to call
|
|
|
|
:param args: (list of :class:`Value`) function arguments
|
2015-12-25 15:02:33 +08:00
|
|
|
:param arg_exprs: (dict of str to `iodelay.Expr`)
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
2015-12-25 15:02:33 +08:00
|
|
|
def __init__(self, func, args, arg_exprs, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(func, Value)
|
|
|
|
for arg in args: assert isinstance(arg, Value)
|
2015-12-25 15:02:33 +08:00
|
|
|
for arg in arg_exprs:
|
|
|
|
assert isinstance(arg, str)
|
|
|
|
assert isinstance(arg_exprs[arg], iodelay.Expr)
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__([func] + args, func.type.ret, name)
|
2015-12-25 15:02:33 +08:00
|
|
|
self.arg_exprs = arg_exprs
|
2015-10-09 06:32:27 +08:00
|
|
|
self.static_target_function = None
|
2016-03-27 09:02:15 +08:00
|
|
|
self.is_cold = False
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
2015-12-25 15:02:33 +08:00
|
|
|
self_copy.arg_exprs = self.arg_exprs
|
2015-11-23 23:58:37 +08:00
|
|
|
self_copy.static_target_function = self.static_target_function
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "call"
|
|
|
|
|
2015-07-21 18:45:27 +08:00
|
|
|
def target_function(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def arguments(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1:]
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
|
|
|
result = super().as_entity(type_printer)
|
2015-10-09 06:32:27 +08:00
|
|
|
if self.static_target_function is not None:
|
|
|
|
result += " ; calls {}".format(self.static_target_function.name)
|
|
|
|
return result
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class Select(Instruction):
|
|
|
|
"""
|
|
|
|
A conditional select instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param cond: (:class:`Value`) select condition
|
|
|
|
:param if_true: (:class:`Value`) value of select if condition is truthful
|
|
|
|
:param if_false: (:class:`Value`) value of select if condition is falseful
|
|
|
|
"""
|
|
|
|
def __init__(self, cond, if_true, if_false, name=""):
|
|
|
|
assert isinstance(cond, Value)
|
2015-07-22 07:58:59 +08:00
|
|
|
assert builtins.is_bool(cond.type)
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(if_true, Value)
|
|
|
|
assert isinstance(if_false, Value)
|
|
|
|
assert if_true.type == if_false.type
|
|
|
|
super().__init__([cond, if_true, if_false], if_true.type, name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "select"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def condition(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def if_true(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def if_false(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[2]
|
|
|
|
|
2015-08-26 12:56:01 +08:00
|
|
|
class Quote(Instruction):
|
|
|
|
"""
|
|
|
|
A quote operation. Returns a host interpreter value as a constant.
|
|
|
|
|
|
|
|
:ivar value: (string) operation name
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param value: (string) operation name
|
|
|
|
"""
|
|
|
|
def __init__(self, value, typ, name=""):
|
|
|
|
super().__init__([], typ, name)
|
|
|
|
self.value = value
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.value = self.value
|
|
|
|
return self_copy
|
|
|
|
|
2015-08-26 12:56:01 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "quote({})".format(repr(self.value))
|
|
|
|
|
2015-07-14 11:44:16 +08:00
|
|
|
class Branch(Terminator):
|
|
|
|
"""
|
|
|
|
An unconditional branch instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param target: (:class:`BasicBlock`) branch target
|
|
|
|
"""
|
|
|
|
def __init__(self, target, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(target, BasicBlock)
|
2015-07-14 11:44:16 +08:00
|
|
|
super().__init__([target], builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "branch"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def target(self):
|
2015-07-15 03:18:38 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-11-20 00:03:26 +08:00
|
|
|
def set_target(self, new_target):
|
2015-11-23 19:18:58 +08:00
|
|
|
self.operands[0].uses.remove(self)
|
2015-11-20 00:03:26 +08:00
|
|
|
self.operands[0] = new_target
|
2015-11-23 19:18:58 +08:00
|
|
|
self.operands[0].uses.add(self)
|
2015-11-20 00:03:26 +08:00
|
|
|
|
2015-07-14 11:44:16 +08:00
|
|
|
class BranchIf(Terminator):
|
|
|
|
"""
|
|
|
|
A conditional branch instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param cond: (:class:`Value`) branch condition
|
2015-07-18 02:29:06 +08:00
|
|
|
:param if_true: (:class:`BasicBlock`) branch target if condition is truthful
|
|
|
|
:param if_false: (:class:`BasicBlock`) branch target if condition is falseful
|
2015-07-14 11:44:16 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, cond, if_true, if_false, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(cond, Value)
|
2015-07-22 07:58:59 +08:00
|
|
|
assert builtins.is_bool(cond.type)
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(if_true, BasicBlock)
|
|
|
|
assert isinstance(if_false, BasicBlock)
|
2015-07-22 07:58:59 +08:00
|
|
|
assert if_true != if_false # use Branch instead
|
2015-07-14 11:44:16 +08:00
|
|
|
super().__init__([cond, if_true, if_false], builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return "branchif"
|
2015-07-14 11:44:16 +08:00
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def condition(self):
|
2015-07-15 03:18:38 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def if_true(self):
|
2015-07-15 03:18:38 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def if_false(self):
|
2015-07-15 03:18:38 +08:00
|
|
|
return self.operands[2]
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class IndirectBranch(Terminator):
|
|
|
|
"""
|
|
|
|
An indirect branch instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param target: (:class:`Value`) branch target
|
|
|
|
:param destinations: (list of :class:`BasicBlock`) all possible values of `target`
|
|
|
|
"""
|
|
|
|
def __init__(self, target, destinations, name=""):
|
|
|
|
assert isinstance(target, Value)
|
|
|
|
assert all([isinstance(dest, BasicBlock) for dest in destinations])
|
|
|
|
super().__init__([target] + destinations, builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "indirectbranch"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def target(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def destinations(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1:]
|
|
|
|
|
|
|
|
def add_destination(self, destination):
|
2015-10-09 08:10:39 +08:00
|
|
|
destination.uses.add(self)
|
2015-07-18 02:29:06 +08:00
|
|
|
self.operands.append(destination)
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
|
|
|
return "{}, [{}]".format(self.operands[0].as_operand(type_printer),
|
|
|
|
", ".join([dest.as_operand(type_printer)
|
|
|
|
for dest in self.operands[1:]]))
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-07-14 11:44:16 +08:00
|
|
|
class Return(Terminator):
|
|
|
|
"""
|
|
|
|
A return instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param value: (:class:`Value`) return value
|
|
|
|
"""
|
|
|
|
def __init__(self, value, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(value, Value)
|
2015-07-14 11:44:16 +08:00
|
|
|
super().__init__([value], builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "return"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-15 03:18:38 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class Unreachable(Terminator):
|
2015-07-14 11:44:16 +08:00
|
|
|
"""
|
2015-07-18 02:29:06 +08:00
|
|
|
An instruction used to mark unreachable branches.
|
2015-07-14 11:44:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2015-07-18 02:29:06 +08:00
|
|
|
:param target: (:class:`BasicBlock`) branch target
|
2015-07-14 11:44:16 +08:00
|
|
|
"""
|
2015-07-18 02:29:06 +08:00
|
|
|
def __init__(self, name=""):
|
|
|
|
super().__init__([], builtins.TNone(), name)
|
2015-07-14 11:44:16 +08:00
|
|
|
|
|
|
|
def opcode(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return "unreachable"
|
2015-07-14 11:44:16 +08:00
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class Raise(Terminator):
|
|
|
|
"""
|
|
|
|
A raise instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param value: (:class:`Value`) exception value
|
2015-07-27 17:36:21 +08:00
|
|
|
:param exn: (:class:`BasicBlock` or None) exceptional target
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
2015-07-27 17:36:21 +08:00
|
|
|
def __init__(self, value=None, exn=None, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(value, Value)
|
2015-07-27 17:36:21 +08:00
|
|
|
operands = [value]
|
2015-07-27 14:10:20 +08:00
|
|
|
if exn is not None:
|
|
|
|
assert isinstance(exn, BasicBlock)
|
2015-07-27 17:36:21 +08:00
|
|
|
operands.append(exn)
|
2015-07-27 14:10:20 +08:00
|
|
|
super().__init__(operands, builtins.TNone(), name)
|
2015-07-18 02:29:06 +08:00
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "raise"
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def value(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-27 14:10:20 +08:00
|
|
|
def exception_target(self):
|
2015-07-27 15:13:22 +08:00
|
|
|
if len(self.operands) > 1:
|
|
|
|
return self.operands[1]
|
2015-07-27 14:10:20 +08:00
|
|
|
|
2015-07-27 17:36:21 +08:00
|
|
|
class Reraise(Terminator):
|
|
|
|
"""
|
|
|
|
A reraise instruction.
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param exn: (:class:`BasicBlock` or None) exceptional target
|
|
|
|
"""
|
|
|
|
def __init__(self, exn=None, name=""):
|
|
|
|
operands = []
|
|
|
|
if exn is not None:
|
|
|
|
assert isinstance(exn, BasicBlock)
|
|
|
|
operands.append(exn)
|
|
|
|
super().__init__(operands, builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "reraise"
|
|
|
|
|
|
|
|
def exception_target(self):
|
|
|
|
if len(self.operands) > 0:
|
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class Invoke(Terminator):
|
|
|
|
"""
|
|
|
|
A function call operation that supports exception handling.
|
2015-10-09 06:32:27 +08:00
|
|
|
|
2015-12-25 15:02:33 +08:00
|
|
|
:ivar arg_exprs: (dict of str to `iodelay.Expr`)
|
|
|
|
iodelay expressions for values of arguments
|
2015-10-09 06:32:27 +08:00
|
|
|
:ivar static_target_function: (:class:`Function` or None)
|
|
|
|
statically resolved callee
|
2016-03-27 09:02:15 +08:00
|
|
|
:ivar is_cold: (bool)
|
|
|
|
the callee function is cold
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param func: (:class:`Value`) function to call
|
|
|
|
:param args: (list of :class:`Value`) function arguments
|
|
|
|
:param normal: (:class:`BasicBlock`) normal target
|
|
|
|
:param exn: (:class:`BasicBlock`) exceptional target
|
2015-12-25 15:02:33 +08:00
|
|
|
:param arg_exprs: (dict of str to `iodelay.Expr`)
|
2015-07-18 02:29:06 +08:00
|
|
|
"""
|
2015-12-25 15:02:33 +08:00
|
|
|
def __init__(self, func, args, arg_exprs, normal, exn, name=""):
|
2015-07-18 02:29:06 +08:00
|
|
|
assert isinstance(func, Value)
|
|
|
|
for arg in args: assert isinstance(arg, Value)
|
|
|
|
assert isinstance(normal, BasicBlock)
|
|
|
|
assert isinstance(exn, BasicBlock)
|
2015-12-25 15:02:33 +08:00
|
|
|
for arg in arg_exprs:
|
|
|
|
assert isinstance(arg, str)
|
|
|
|
assert isinstance(arg_exprs[arg], iodelay.Expr)
|
2015-07-18 02:29:06 +08:00
|
|
|
super().__init__([func] + args + [normal, exn], func.type.ret, name)
|
2015-12-25 15:02:33 +08:00
|
|
|
self.arg_exprs = arg_exprs
|
2015-10-09 06:32:27 +08:00
|
|
|
self.static_target_function = None
|
2016-03-27 09:02:15 +08:00
|
|
|
self.is_cold = False
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
2015-12-25 15:02:33 +08:00
|
|
|
self_copy.arg_exprs = self.arg_exprs
|
2015-11-23 23:58:37 +08:00
|
|
|
self_copy.static_target_function = self.static_target_function
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "invoke"
|
|
|
|
|
2015-07-25 10:37:37 +08:00
|
|
|
def target_function(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def arguments(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[1:-2]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def normal_target(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[-2]
|
|
|
|
|
2015-07-19 16:44:51 +08:00
|
|
|
def exception_target(self):
|
2015-07-18 02:29:06 +08:00
|
|
|
return self.operands[-1]
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
|
|
|
result = ", ".join([operand.as_operand(type_printer) for operand in self.operands[:-2]])
|
|
|
|
result += " to {} unwind {}".format(self.operands[-2].as_operand(type_printer),
|
|
|
|
self.operands[-1].as_operand(type_printer))
|
2015-07-18 02:29:06 +08:00
|
|
|
return result
|
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def as_entity(self, type_printer):
|
|
|
|
result = super().as_entity(type_printer)
|
2015-10-09 06:32:27 +08:00
|
|
|
if self.static_target_function is not None:
|
|
|
|
result += " ; calls {}".format(self.static_target_function.name)
|
|
|
|
return result
|
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
class LandingPad(Terminator):
|
|
|
|
"""
|
|
|
|
An instruction that gives an incoming exception a name and
|
|
|
|
dispatches it according to its type.
|
|
|
|
|
|
|
|
Once dispatched, the exception should be cast to its proper
|
|
|
|
type by calling the "exncast" builtin on the landing pad value.
|
|
|
|
|
|
|
|
:ivar types: (a list of :class:`builtins.TException`)
|
|
|
|
exception types corresponding to the basic block operands
|
|
|
|
"""
|
|
|
|
|
2015-07-27 17:36:21 +08:00
|
|
|
def __init__(self, cleanup, name=""):
|
|
|
|
super().__init__([cleanup], builtins.TException(), name)
|
2015-07-18 02:29:06 +08:00
|
|
|
self.types = []
|
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.types = list(self.types)
|
|
|
|
return self_copy
|
|
|
|
|
2015-07-27 17:36:21 +08:00
|
|
|
def opcode(self):
|
|
|
|
return "landingpad"
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
return self.operands[0]
|
|
|
|
|
2015-07-25 10:37:37 +08:00
|
|
|
def clauses(self):
|
2015-07-27 17:36:21 +08:00
|
|
|
return zip(self.operands[1:], self.types)
|
2015-07-25 10:37:37 +08:00
|
|
|
|
2015-07-18 02:29:06 +08:00
|
|
|
def add_clause(self, target, typ):
|
|
|
|
assert isinstance(target, BasicBlock)
|
2015-07-25 10:37:37 +08:00
|
|
|
assert typ is None or builtins.is_exception(typ)
|
2015-07-18 02:29:06 +08:00
|
|
|
self.operands.append(target)
|
2015-07-25 10:37:37 +08:00
|
|
|
self.types.append(typ.find() if typ is not None else None)
|
|
|
|
target.uses.add(self)
|
2015-07-18 02:29:06 +08:00
|
|
|
|
2015-11-17 05:23:34 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
2015-07-18 02:29:06 +08:00
|
|
|
table = []
|
2015-07-27 17:36:21 +08:00
|
|
|
for target, typ in self.clauses():
|
2015-07-25 10:37:37 +08:00
|
|
|
if typ is None:
|
2015-11-17 05:23:34 +08:00
|
|
|
table.append("... => {}".format(target.as_operand(type_printer)))
|
2015-07-25 10:37:37 +08:00
|
|
|
else:
|
2015-11-17 05:23:34 +08:00
|
|
|
table.append("{} => {}".format(type_printer.name(typ),
|
|
|
|
target.as_operand(type_printer)))
|
|
|
|
return "cleanup {}, [{}]".format(self.cleanup().as_operand(type_printer),
|
|
|
|
", ".join(table))
|
2015-10-09 08:10:39 +08:00
|
|
|
|
2015-11-17 10:16:43 +08:00
|
|
|
class Delay(Terminator):
|
|
|
|
"""
|
|
|
|
A delay operation. Ties an :class:`iodelay.Expr` to SSA values so that
|
|
|
|
inlining could lead to the expression folding to a constant.
|
|
|
|
|
2015-12-16 13:47:39 +08:00
|
|
|
:ivar interval: (:class:`iodelay.Expr`) expression
|
2015-11-17 10:16:43 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2015-12-16 13:47:39 +08:00
|
|
|
:param interval: (:class:`iodelay.Expr`) expression
|
2015-11-21 03:22:47 +08:00
|
|
|
:param call: (:class:`Call` or ``Constant(None, builtins.TNone())``)
|
|
|
|
the call instruction that caused this delay, if any
|
2015-11-17 10:16:43 +08:00
|
|
|
:param target: (:class:`BasicBlock`) branch target
|
|
|
|
"""
|
2015-12-25 15:02:33 +08:00
|
|
|
def __init__(self, interval, decomposition, target, name=""):
|
2016-07-13 16:48:31 +08:00
|
|
|
assert isinstance(decomposition, Call) or isinstance(decomposition, Invoke) or \
|
2015-11-21 03:22:47 +08:00
|
|
|
isinstance(decomposition, Builtin) and decomposition.op in ("delay", "delay_mu")
|
2015-11-17 10:16:43 +08:00
|
|
|
assert isinstance(target, BasicBlock)
|
2015-12-25 15:02:33 +08:00
|
|
|
super().__init__([decomposition, target], builtins.TNone(), name)
|
2015-12-16 13:47:39 +08:00
|
|
|
self.interval = interval
|
2015-11-17 10:16:43 +08:00
|
|
|
|
2015-11-23 23:58:37 +08:00
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
2015-12-16 13:47:39 +08:00
|
|
|
self_copy.interval = self.interval
|
2015-11-23 23:58:37 +08:00
|
|
|
return self_copy
|
|
|
|
|
2015-11-21 03:22:47 +08:00
|
|
|
def decomposition(self):
|
2015-11-17 10:16:43 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-11-23 19:18:58 +08:00
|
|
|
def set_decomposition(self, new_decomposition):
|
|
|
|
self.operands[0].uses.remove(self)
|
|
|
|
self.operands[0] = new_decomposition
|
|
|
|
self.operands[0].uses.add(self)
|
|
|
|
|
2015-11-21 03:22:47 +08:00
|
|
|
def target(self):
|
|
|
|
return self.operands[1]
|
|
|
|
|
2015-11-20 00:03:26 +08:00
|
|
|
def set_target(self, new_target):
|
2015-11-23 19:18:58 +08:00
|
|
|
self.operands[1].uses.remove(self)
|
2015-11-21 03:22:47 +08:00
|
|
|
self.operands[1] = new_target
|
2015-11-23 19:18:58 +08:00
|
|
|
self.operands[1].uses.add(self)
|
2015-11-20 00:03:26 +08:00
|
|
|
|
2015-11-17 10:16:43 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
2015-12-25 15:02:33 +08:00
|
|
|
result = "decomp {}, to {}".format(self.decomposition().as_operand(type_printer),
|
|
|
|
self.target().as_operand(type_printer))
|
2015-11-21 03:22:47 +08:00
|
|
|
return result
|
2015-11-17 10:16:43 +08:00
|
|
|
|
|
|
|
def opcode(self):
|
2015-12-16 13:47:39 +08:00
|
|
|
return "delay({})".format(self.interval)
|
2015-11-17 10:16:43 +08:00
|
|
|
|
2015-12-16 15:33:15 +08:00
|
|
|
class Loop(Terminator):
|
|
|
|
"""
|
|
|
|
A terminator for loop headers that carries metadata useful
|
|
|
|
for unrolling. It includes an :class:`iodelay.Expr` specifying
|
|
|
|
the trip count, tied to SSA values so that inlining could lead
|
|
|
|
to the expression folding to a constant.
|
|
|
|
|
|
|
|
:ivar trip_count: (:class:`iodelay.Expr`)
|
|
|
|
expression for trip count
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
:param trip_count: (:class:`iodelay.Expr`) expression
|
2015-12-17 00:52:03 +08:00
|
|
|
:param indvar: (:class:`Phi`)
|
|
|
|
phi node corresponding to the induction SSA value,
|
|
|
|
which advances from ``0`` to ``trip_count - 1``
|
2015-12-16 15:33:15 +08:00
|
|
|
:param cond: (:class:`Value`) branch condition
|
|
|
|
:param if_true: (:class:`BasicBlock`) branch target if condition is truthful
|
|
|
|
:param if_false: (:class:`BasicBlock`) branch target if condition is falseful
|
|
|
|
"""
|
2015-12-25 15:02:33 +08:00
|
|
|
def __init__(self, trip_count, indvar, cond, if_true, if_false, name=""):
|
2015-12-17 00:52:03 +08:00
|
|
|
assert isinstance(indvar, Phi)
|
2015-12-16 15:33:15 +08:00
|
|
|
assert isinstance(cond, Value)
|
|
|
|
assert builtins.is_bool(cond.type)
|
|
|
|
assert isinstance(if_true, BasicBlock)
|
|
|
|
assert isinstance(if_false, BasicBlock)
|
2015-12-25 15:02:33 +08:00
|
|
|
super().__init__([indvar, cond, if_true, if_false], builtins.TNone(), name)
|
2015-12-16 15:33:15 +08:00
|
|
|
self.trip_count = trip_count
|
|
|
|
|
|
|
|
def copy(self, mapper):
|
|
|
|
self_copy = super().copy(mapper)
|
|
|
|
self_copy.trip_count = self.trip_count
|
|
|
|
return self_copy
|
|
|
|
|
2015-12-17 00:52:03 +08:00
|
|
|
def induction_variable(self):
|
2015-12-16 15:33:15 +08:00
|
|
|
return self.operands[0]
|
|
|
|
|
2015-12-17 00:52:03 +08:00
|
|
|
def condition(self):
|
2015-12-16 15:33:15 +08:00
|
|
|
return self.operands[1]
|
|
|
|
|
2015-12-17 00:52:03 +08:00
|
|
|
def if_true(self):
|
2015-12-16 15:33:15 +08:00
|
|
|
return self.operands[2]
|
|
|
|
|
2015-12-17 00:52:03 +08:00
|
|
|
def if_false(self):
|
|
|
|
return self.operands[3]
|
|
|
|
|
2015-12-16 15:33:15 +08:00
|
|
|
def _operands_as_string(self, type_printer):
|
2015-12-25 15:02:33 +08:00
|
|
|
result = "indvar {}, if {}, {}, {}".format(
|
|
|
|
*list(map(lambda value: value.as_operand(type_printer), self.operands)))
|
2015-12-16 15:33:15 +08:00
|
|
|
return result
|
|
|
|
|
|
|
|
def opcode(self):
|
|
|
|
return "loop({} times)".format(self.trip_count)
|
|
|
|
|
2016-02-22 21:24:43 +08:00
|
|
|
class Interleave(Terminator):
|
2015-10-09 08:10:39 +08:00
|
|
|
"""
|
|
|
|
An instruction that schedules several threads of execution
|
|
|
|
in parallel.
|
|
|
|
"""
|
|
|
|
|
2015-10-14 22:02:59 +08:00
|
|
|
def __init__(self, destinations, name=""):
|
2015-10-09 08:10:39 +08:00
|
|
|
super().__init__(destinations, builtins.TNone(), name)
|
|
|
|
|
|
|
|
def opcode(self):
|
2016-02-22 21:24:43 +08:00
|
|
|
return "interleave"
|
2015-10-09 08:10:39 +08:00
|
|
|
|
|
|
|
def destinations(self):
|
|
|
|
return self.operands
|
|
|
|
|
|
|
|
def add_destination(self, destination):
|
|
|
|
destination.uses.add(self)
|
|
|
|
self.operands.append(destination)
|