//===--- SILNodes.def - Swift SIL Metaprogramming ---------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines macros used for macro-metaprogramming with SIL nodes.
//
//===----------------------------------------------------------------------===//

/// VALUE(Id, Parent)
///   The expression enumerator value is a ValueKind.  The node's class name is
///   Id, and the name of its base class (in the SILValue hierarchy) is Parent.
#ifndef VALUE
#define VALUE(Id, Parent)
#endif

/// INST(Id, Parent, MemBehavior, MayRelease)
///   The expression enumerator value is a ValueKind.  The node's class name is
///   Id, and the name of its base class (in the SILInstruction hierarchy) is
///   Parent. MemBehavior is an enum value that reflects the memory behavior of
///   the instruction. MayRelease indicates whether the execution of the
///   instruction may result in memory being released.
#ifndef INST
#define INST(Id, Parent, MemBehavior, MayRelease) VALUE(Id, Parent)
#endif

/// TERMINATOR(Id, Parent, MemBehavior, MayRelease)
///   Expands for terminator instructions. The expression enumerator value is
///   a ValueKind.  The node's class name is Id, and the name of its base class
///   (in the SILInstruction hierarchy) is Parent. MemBehavior is an enum value
///   that reflects the memory behavior of the instruction. MayRelease
///   indicates whether the execution of the instruction may result in memory
///   being released.
#ifndef TERMINATOR
#define TERMINATOR(Id, Parent, MemBehavior, MayRelease) \
  INST(Id, Parent, MemBehavior, MayRelease)
#endif

/// An abstract instruction is an abstract base class in the hierarchy;
/// it is never a most-derived type, and it does not have an enumerator in
/// ValueKind.
///
/// Most metaprograms do not care about abstract expressions, so the default
/// is to ignore them.
#ifndef ABSTRACT_VALUE
#define ABSTRACT_VALUE(Id, Parent)
#endif

/// A convenience for determining the range of values.  These will always
/// appear immediately after the last member.
#ifndef VALUE_RANGE
#define VALUE_RANGE(Id, First, Last)
#endif

VALUE(SILArgument, ValueBase)
VALUE(SILUndef, ValueBase)

// Please keep the order of instructions consistent with the order of their
// descriptions in the SIL reference in docs/SIL.rst.

ABSTRACT_VALUE(SILInstruction, ValueBase)
  // Allocation instructions.
  ABSTRACT_VALUE(AllocationInst, SILInstruction)
    INST(AllocStackInst, AllocationInst, None, DoesNotRelease)
    INST(AllocRefInst, AllocationInst, None, DoesNotRelease)
    INST(AllocRefDynamicInst, AllocationInst, None, DoesNotRelease)
    INST(AllocValueBufferInst, AllocationInst, None, DoesNotRelease)
    INST(AllocBoxInst, AllocationInst, None, DoesNotRelease)
    INST(AllocExistentialBoxInst, AllocationInst, MayWrite, DoesNotRelease)
    VALUE_RANGE(AllocationInst, AllocStackInst, AllocExistentialBoxInst)

  // Deallocation instructions.
  ABSTRACT_VALUE(DeallocationInst, SILInstruction)
    INST(DeallocStackInst, DeallocationInst, MayHaveSideEffects, DoesNotRelease)
    INST(DeallocRefInst, DeallocationInst, MayHaveSideEffects, DoesNotRelease)
    INST(DeallocPartialRefInst, DeallocationInst, MayHaveSideEffects,
         DoesNotRelease)
    INST(DeallocValueBufferInst, DeallocationInst, MayHaveSideEffects,
         DoesNotRelease)
    INST(DeallocBoxInst, DeallocationInst, MayHaveSideEffects, DoesNotRelease)
    INST(DeallocExistentialBoxInst, DeallocationInst, MayHaveSideEffects,
         DoesNotRelease)
    VALUE_RANGE(DeallocationInst, DeallocStackInst, DeallocExistentialBoxInst)

  // Accessing memory
  INST(LoadInst, SILInstruction, MayRead, DoesNotRelease)
  INST(LoadWeakInst, SILInstruction, MayRead, DoesNotRelease)
  INST(StoreInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(AssignInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(MarkUninitializedInst, SILInstruction, None, DoesNotRelease)
  INST(MarkFunctionEscapeInst, SILInstruction, None, DoesNotRelease)
  INST(DebugValueInst, SILInstruction, None, DoesNotRelease)
  INST(DebugValueAddrInst, SILInstruction, None, DoesNotRelease)
  INST(StoreWeakInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(CopyAddrInst, SILInstruction, MayHaveSideEffects, MayRelease)
  INST(DestroyAddrInst, SILInstruction, MayHaveSideEffects, MayRelease)
  INST(ProjectValueBufferInst, SILInstruction, MayRead, DoesNotRelease)
  INST(ProjectBoxInst, SILInstruction, MayRead, DoesNotRelease)
  ABSTRACT_VALUE(IndexingInst, SILInstruction)
    INST(IndexAddrInst, IndexingInst, None, DoesNotRelease)
    INST(IndexRawPointerInst, IndexingInst, None, DoesNotRelease)
    VALUE_RANGE(IndexingInst, IndexAddrInst, IndexRawPointerInst)

  // Reference Counting
  ABSTRACT_VALUE(RefCountingInst, SILInstruction)
    INST(StrongRetainInst, RefCountingInst, MayHaveSideEffects, DoesNotRelease)
    INST(StrongRetainAutoreleasedInst, RefCountingInst, MayHaveSideEffects,
         DoesNotRelease)
    INST(StrongReleaseInst, RefCountingInst, MayHaveSideEffects, MayRelease)
    INST(StrongRetainUnownedInst, RefCountingInst, MayHaveSideEffects,
         DoesNotRelease)
    INST(UnownedRetainInst, RefCountingInst, MayHaveSideEffects, DoesNotRelease)
    INST(UnownedReleaseInst, RefCountingInst, MayHaveSideEffects,
         MayRelease)
    INST(RetainValueInst, RefCountingInst, MayHaveSideEffects, DoesNotRelease)
    INST(ReleaseValueInst, RefCountingInst, MayHaveSideEffects, MayRelease)
    INST(AutoreleaseValueInst, RefCountingInst, MayHaveSideEffects,
         DoesNotRelease)
    VALUE_RANGE(RefCountingInst, StrongRetainInst, AutoreleaseValueInst)
  // FIXME: Is MayHaveSideEffects appropriate?
  INST(FixLifetimeInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)
  INST(MarkDependenceInst, SILInstruction, None, DoesNotRelease)
  INST(CopyBlockInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)
  INST(StrongPinInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)
  INST(StrongUnpinInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)

  // IsUnique does not actually write to memory but should be modeled
  // as such. Its operand is a pointer to an object reference. The
  // optimizer should not assume that the same object is pointed to after
  // the isUnique instruction. It appears to write a new object reference.
  INST(IsUniqueInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)
  INST(IsUniqueOrPinnedInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)

  // Literals
  ABSTRACT_VALUE(LiteralInst, SILInstruction)
    INST(FunctionRefInst, LiteralInst, None, DoesNotRelease)
    INST(GlobalAddrInst, LiteralInst, None, DoesNotRelease)
    INST(IntegerLiteralInst, LiteralInst, None, DoesNotRelease)
    INST(FloatLiteralInst, LiteralInst, None, DoesNotRelease)
    INST(StringLiteralInst, LiteralInst, None, DoesNotRelease)
    VALUE_RANGE(LiteralInst, FunctionRefInst, StringLiteralInst)

  // Dynamic Dispatch
  ABSTRACT_VALUE(MethodInst, SILInstruction)
    INST(ClassMethodInst, MethodInst, None, DoesNotRelease)
    INST(SuperMethodInst, MethodInst, None, DoesNotRelease)
    INST(WitnessMethodInst, MethodInst, None, DoesNotRelease)
    INST(DynamicMethodInst, MethodInst, None, DoesNotRelease)
    VALUE_RANGE(MethodInst, ClassMethodInst, DynamicMethodInst)

  // Function Application
  INST(ApplyInst, SILInstruction, MayHaveSideEffects, MayRelease)
  INST(PartialApplyInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)
  INST(BuiltinInst, SILInstruction, MayHaveSideEffects, MayRelease)

  // Metatypes
  INST(MetatypeInst, SILInstruction, None, DoesNotRelease)
  INST(ValueMetatypeInst, SILInstruction, None, DoesNotRelease)
  INST(ExistentialMetatypeInst, SILInstruction, None, DoesNotRelease)
  INST(ObjCProtocolInst, SILInstruction, None, DoesNotRelease)

  // Aggregate Types
  INST(TupleInst, SILInstruction, None, DoesNotRelease)
  INST(TupleExtractInst, SILInstruction, None, DoesNotRelease)
  INST(TupleElementAddrInst, SILInstruction, None, DoesNotRelease)
  INST(StructInst, SILInstruction, None, DoesNotRelease)
  INST(StructExtractInst, SILInstruction, None, DoesNotRelease)
  INST(StructElementAddrInst, SILInstruction, None, DoesNotRelease)
  INST(RefElementAddrInst, SILInstruction, None, DoesNotRelease)

  // Enums
  INST(EnumInst, SILInstruction, None, DoesNotRelease)
  INST(UncheckedEnumDataInst, SILInstruction, None, DoesNotRelease)
  INST(InitEnumDataAddrInst, SILInstruction, None, DoesNotRelease)
  INST(UncheckedTakeEnumDataAddrInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(InjectEnumAddrInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(SelectEnumInst, SILInstruction, None, DoesNotRelease)
  INST(SelectEnumAddrInst, SILInstruction, MayRead, DoesNotRelease)
  INST(SelectValueInst, SILInstruction, None, DoesNotRelease)

  // Protocol and Protocol Composition Types
  INST(InitExistentialAddrInst, SILInstruction, MayWrite, DoesNotRelease)
  INST(DeinitExistentialAddrInst, SILInstruction, MayHaveSideEffects,
       DoesNotRelease)
  INST(OpenExistentialAddrInst, SILInstruction, MayRead, DoesNotRelease)
  INST(InitExistentialRefInst, SILInstruction, None, DoesNotRelease)
  INST(OpenExistentialRefInst, SILInstruction, None, DoesNotRelease)
  INST(InitExistentialMetatypeInst, SILInstruction, None, DoesNotRelease)
  INST(OpenExistentialMetatypeInst, SILInstruction, None, DoesNotRelease)
  INST(OpenExistentialBoxInst, SILInstruction, MayRead, DoesNotRelease)

  // Blocks
  INST(ProjectBlockStorageInst, SILInstruction, None, DoesNotRelease)
  INST(InitBlockStorageHeaderInst, SILInstruction, None, DoesNotRelease)

  // Conversions
  ABSTRACT_VALUE(ConversionInst, SILInstruction)
    INST(UpcastInst, ConversionInst, None, DoesNotRelease)
    INST(AddressToPointerInst, ConversionInst, None, DoesNotRelease)
    INST(PointerToAddressInst, ConversionInst, None, DoesNotRelease)
    INST(UncheckedRefCastInst, ConversionInst, None, DoesNotRelease)
    INST(UncheckedAddrCastInst, ConversionInst, None, DoesNotRelease)
    INST(UncheckedTrivialBitCastInst, ConversionInst, None, DoesNotRelease)
    INST(UncheckedBitwiseCastInst, ConversionInst, None, DoesNotRelease)
    INST(RefToRawPointerInst, ConversionInst, None, DoesNotRelease)
    INST(RawPointerToRefInst, ConversionInst, None, DoesNotRelease)
    INST(RefToUnownedInst, ConversionInst, None, DoesNotRelease)
    INST(UnownedToRefInst, ConversionInst, None, DoesNotRelease)
    INST(RefToUnmanagedInst, ConversionInst, None, DoesNotRelease)
    INST(UnmanagedToRefInst, ConversionInst, None, DoesNotRelease)
    INST(ConvertFunctionInst, ConversionInst, None, DoesNotRelease)
    INST(ThinFunctionToPointerInst, ConversionInst, None, DoesNotRelease)
    INST(PointerToThinFunctionInst, ConversionInst, None, DoesNotRelease)
    INST(RefToBridgeObjectInst, ConversionInst, None, DoesNotRelease)
    INST(BridgeObjectToRefInst, ConversionInst, None, DoesNotRelease)
    INST(BridgeObjectToWordInst, ConversionInst, None, DoesNotRelease)
    INST(ThinToThickFunctionInst, ConversionInst, None, DoesNotRelease)
    INST(ThickToObjCMetatypeInst, ConversionInst, None, DoesNotRelease)
    INST(ObjCToThickMetatypeInst, ConversionInst, None, DoesNotRelease)
    INST(ObjCMetatypeToObjectInst, ConversionInst, None, DoesNotRelease)
    INST(ObjCExistentialMetatypeToObjectInst, ConversionInst, None,
         DoesNotRelease)
    INST(UnconditionalCheckedCastInst, ConversionInst, None, DoesNotRelease)
    VALUE_RANGE(ConversionInst, UpcastInst, UnconditionalCheckedCastInst)
  INST(IsNonnullInst, SILInstruction, None, DoesNotRelease)
  INST(UnconditionalCheckedCastAddrInst, SILInstruction, MayHaveSideEffects,
       MayRelease)
  INST(UncheckedRefCastAddrInst, SILInstruction, MayHaveSideEffects,
       DoesNotRelease)

  // Runtime failure
  // FIXME: Special MemBehavior for runtime failure?
  INST(CondFailInst, SILInstruction, MayHaveSideEffects, DoesNotRelease)

  // Terminators
  ABSTRACT_VALUE(TermInst, SILInstruction)
    TERMINATOR(UnreachableInst, TermInst, None, DoesNotRelease)
    TERMINATOR(ReturnInst, TermInst, None, DoesNotRelease)
    TERMINATOR(AutoreleaseReturnInst, TermInst, None, DoesNotRelease)
    TERMINATOR(ThrowInst, TermInst, None, DoesNotRelease)
    TERMINATOR(TryApplyInst, TermInst, MayHaveSideEffects, MayRelease)
    TERMINATOR(BranchInst, TermInst, None, DoesNotRelease)
    TERMINATOR(CondBranchInst, TermInst, None, DoesNotRelease)
    TERMINATOR(SwitchValueInst, TermInst, None, DoesNotRelease)
    TERMINATOR(SwitchEnumInst, TermInst, None, DoesNotRelease)
    TERMINATOR(SwitchEnumAddrInst, TermInst, MayRead, DoesNotRelease)
    TERMINATOR(DynamicMethodBranchInst, TermInst, None, DoesNotRelease)
    TERMINATOR(CheckedCastBranchInst, TermInst, None, DoesNotRelease)
    TERMINATOR(CheckedCastAddrBranchInst, TermInst, MayHaveSideEffects,
               MayRelease)
    VALUE_RANGE(TermInst, UnreachableInst, CheckedCastAddrBranchInst)

  VALUE_RANGE(SILInstruction, AllocStackInst, CheckedCastAddrBranchInst)

#undef VALUE_RANGE
#undef ABSTRACT_VALUE
#undef TERMINATOR
#undef INST
#undef VALUE
