github.com/llir/llvm@v0.3.6/ir/ir_test.go (about)

     1  package ir
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/llir/llvm/ir/constant"
     8  	"github.com/llir/llvm/ir/metadata"
     9  	"github.com/llir/llvm/ir/types"
    10  	"github.com/llir/llvm/ir/value"
    11  )
    12  
    13  func TestModuleString(t *testing.T) {
    14  	golden := []struct {
    15  		in   *Module
    16  		want string
    17  	}{
    18  		// Empty module.
    19  		{
    20  			in:   &Module{},
    21  			want: "",
    22  		},
    23  		// Type definition.
    24  		{
    25  			in: &Module{
    26  				TypeDefs: []types.Type{&types.StructType{
    27  					TypeName: "foo",
    28  					Fields:   []types.Type{types.I32},
    29  				}},
    30  			},
    31  			want: "%foo = type { i32 }",
    32  		},
    33  	}
    34  	for _, g := range golden {
    35  		got := strings.TrimSpace(g.in.String())
    36  		if g.want != got {
    37  			t.Errorf("module mismatch; expected `%v`, got `%v`", g.want, got)
    38  		}
    39  	}
    40  }
    41  
    42  // Assert that each constant implements the constant.Constant interface.
    43  var (
    44  	// Constants.
    45  	_ constant.Constant = (*Global)(nil)
    46  	_ constant.Constant = (*Func)(nil)
    47  	_ constant.Constant = (*Alias)(nil)
    48  	_ constant.Constant = (*IFunc)(nil)
    49  )
    50  
    51  // Assert that each instruction implements the ir.Instruction interface.
    52  var (
    53  	// Unary instructions.
    54  	_ Instruction = (*InstFNeg)(nil)
    55  	// Binary instructions.
    56  	_ Instruction = (*InstAdd)(nil)
    57  	_ Instruction = (*InstFAdd)(nil)
    58  	_ Instruction = (*InstSub)(nil)
    59  	_ Instruction = (*InstFSub)(nil)
    60  	_ Instruction = (*InstMul)(nil)
    61  	_ Instruction = (*InstFMul)(nil)
    62  	_ Instruction = (*InstUDiv)(nil)
    63  	_ Instruction = (*InstSDiv)(nil)
    64  	_ Instruction = (*InstFDiv)(nil)
    65  	_ Instruction = (*InstURem)(nil)
    66  	_ Instruction = (*InstSRem)(nil)
    67  	_ Instruction = (*InstFRem)(nil)
    68  	// Bitwise instructions.
    69  	_ Instruction = (*InstShl)(nil)
    70  	_ Instruction = (*InstLShr)(nil)
    71  	_ Instruction = (*InstAShr)(nil)
    72  	_ Instruction = (*InstAnd)(nil)
    73  	_ Instruction = (*InstOr)(nil)
    74  	_ Instruction = (*InstXor)(nil)
    75  	// Vector instructions.
    76  	_ Instruction = (*InstExtractElement)(nil)
    77  	_ Instruction = (*InstInsertElement)(nil)
    78  	_ Instruction = (*InstShuffleVector)(nil)
    79  	// Aggregate instructions.
    80  	_ Instruction = (*InstExtractValue)(nil)
    81  	_ Instruction = (*InstInsertValue)(nil)
    82  	// Memory instructions.
    83  	_ Instruction = (*InstAlloca)(nil)
    84  	_ Instruction = (*InstLoad)(nil)
    85  	_ Instruction = (*InstStore)(nil)
    86  	_ Instruction = (*InstFence)(nil)
    87  	_ Instruction = (*InstCmpXchg)(nil)
    88  	_ Instruction = (*InstAtomicRMW)(nil)
    89  	_ Instruction = (*InstGetElementPtr)(nil)
    90  	// Conversion instructions.
    91  	_ Instruction = (*InstTrunc)(nil)
    92  	_ Instruction = (*InstZExt)(nil)
    93  	_ Instruction = (*InstSExt)(nil)
    94  	_ Instruction = (*InstFPTrunc)(nil)
    95  	_ Instruction = (*InstFPExt)(nil)
    96  	_ Instruction = (*InstFPToUI)(nil)
    97  	_ Instruction = (*InstFPToSI)(nil)
    98  	_ Instruction = (*InstUIToFP)(nil)
    99  	_ Instruction = (*InstSIToFP)(nil)
   100  	_ Instruction = (*InstPtrToInt)(nil)
   101  	_ Instruction = (*InstIntToPtr)(nil)
   102  	_ Instruction = (*InstBitCast)(nil)
   103  	_ Instruction = (*InstAddrSpaceCast)(nil)
   104  	// Other instructions.
   105  	_ Instruction = (*InstICmp)(nil)
   106  	_ Instruction = (*InstFCmp)(nil)
   107  	_ Instruction = (*InstPhi)(nil)
   108  	_ Instruction = (*InstSelect)(nil)
   109  	_ Instruction = (*InstFreeze)(nil)
   110  	_ Instruction = (*InstCall)(nil)
   111  	_ Instruction = (*InstVAArg)(nil)
   112  	_ Instruction = (*InstLandingPad)(nil)
   113  	_ Instruction = (*InstCatchPad)(nil)
   114  	_ Instruction = (*InstCleanupPad)(nil)
   115  )
   116  
   117  // Assert that each terminator implements the ir.Terminator interface.
   118  var (
   119  	// Terminators.
   120  	_ Terminator = (*TermRet)(nil)
   121  	_ Terminator = (*TermBr)(nil)
   122  	_ Terminator = (*TermCondBr)(nil)
   123  	_ Terminator = (*TermSwitch)(nil)
   124  	_ Terminator = (*TermIndirectBr)(nil)
   125  	_ Terminator = (*TermInvoke)(nil)
   126  	_ Terminator = (*TermCallBr)(nil)
   127  	_ Terminator = (*TermResume)(nil)
   128  	_ Terminator = (*TermCatchSwitch)(nil)
   129  	_ Terminator = (*TermCatchRet)(nil)
   130  	_ Terminator = (*TermCleanupRet)(nil)
   131  	_ Terminator = (*TermUnreachable)(nil)
   132  )
   133  
   134  // Assert that each value implements the value.Value interface.
   135  var (
   136  	// Constants.
   137  	// Checked in constant_test.go as ir.Constant embeds value.Value.
   138  	_ value.Value = constant.Constant(nil)
   139  	// Named values.
   140  	// Checked in value_test.go as value.Named embeds value.Value.
   141  	_ value.Value = value.Named(nil)
   142  	// Inline assembler expressions.
   143  	_ value.Value = (*InlineAsm)(nil)
   144  	// Metadata values.
   145  	_ value.Value = (*metadata.Value)(nil)
   146  )
   147  
   148  // Assert that each named value implements the value.Named interface.
   149  var (
   150  	// Other values.
   151  	_ value.Named = (*Global)(nil)
   152  	_ value.Named = (*Func)(nil)
   153  	_ value.Named = (*Param)(nil)
   154  	_ value.Named = (*Block)(nil)
   155  
   156  	// Instructions.
   157  	// Unary instructions.
   158  	_ value.Named = (*InstFNeg)(nil)
   159  	// Binary instructions.
   160  	_ value.Named = (*InstAdd)(nil)
   161  	_ value.Named = (*InstFAdd)(nil)
   162  	_ value.Named = (*InstSub)(nil)
   163  	_ value.Named = (*InstFSub)(nil)
   164  	_ value.Named = (*InstMul)(nil)
   165  	_ value.Named = (*InstFMul)(nil)
   166  	_ value.Named = (*InstUDiv)(nil)
   167  	_ value.Named = (*InstSDiv)(nil)
   168  	_ value.Named = (*InstFDiv)(nil)
   169  	_ value.Named = (*InstURem)(nil)
   170  	_ value.Named = (*InstSRem)(nil)
   171  	_ value.Named = (*InstFRem)(nil)
   172  	// Bitwise instructions.
   173  	_ value.Named = (*InstShl)(nil)
   174  	_ value.Named = (*InstLShr)(nil)
   175  	_ value.Named = (*InstAShr)(nil)
   176  	_ value.Named = (*InstAnd)(nil)
   177  	_ value.Named = (*InstOr)(nil)
   178  	_ value.Named = (*InstXor)(nil)
   179  	// Vector instructions.
   180  	_ value.Named = (*InstExtractElement)(nil)
   181  	_ value.Named = (*InstInsertElement)(nil)
   182  	_ value.Named = (*InstShuffleVector)(nil)
   183  	// Aggregate instructions.
   184  	_ value.Named = (*InstExtractValue)(nil)
   185  	_ value.Named = (*InstInsertValue)(nil)
   186  	// Memory instructions.
   187  	_ value.Named = (*InstAlloca)(nil)
   188  	_ value.Named = (*InstLoad)(nil)
   189  	_ value.Named = (*InstCmpXchg)(nil)
   190  	_ value.Named = (*InstAtomicRMW)(nil)
   191  	_ value.Named = (*InstGetElementPtr)(nil)
   192  	// Conversion instructions.
   193  	_ value.Named = (*InstTrunc)(nil)
   194  	_ value.Named = (*InstZExt)(nil)
   195  	_ value.Named = (*InstSExt)(nil)
   196  	_ value.Named = (*InstFPTrunc)(nil)
   197  	_ value.Named = (*InstFPExt)(nil)
   198  	_ value.Named = (*InstFPToUI)(nil)
   199  	_ value.Named = (*InstFPToSI)(nil)
   200  	_ value.Named = (*InstUIToFP)(nil)
   201  	_ value.Named = (*InstSIToFP)(nil)
   202  	_ value.Named = (*InstPtrToInt)(nil)
   203  	_ value.Named = (*InstIntToPtr)(nil)
   204  	_ value.Named = (*InstBitCast)(nil)
   205  	_ value.Named = (*InstAddrSpaceCast)(nil)
   206  	// Other instructions.
   207  	_ value.Named = (*InstICmp)(nil)
   208  	_ value.Named = (*InstFCmp)(nil)
   209  	_ value.Named = (*InstPhi)(nil)
   210  	_ value.Named = (*InstSelect)(nil)
   211  	_ value.Named = (*InstFreeze)(nil)
   212  	_ value.Named = (*InstCall)(nil)
   213  	_ value.Named = (*InstVAArg)(nil)
   214  	_ value.Named = (*InstLandingPad)(nil)
   215  	_ value.Named = (*InstCatchPad)(nil)
   216  	_ value.Named = (*InstCleanupPad)(nil)
   217  
   218  	// Terminators.
   219  	_ value.Named = (*TermInvoke)(nil)
   220  	_ value.Named = (*TermCallBr)(nil)
   221  	_ value.Named = (*TermCatchSwitch)(nil) // token result used by catchpad
   222  )