1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs | FileCheck %s
2
3; Test varargs constructs.
4
5target triple = "wasm32-unknown-emscripten"
6
7; Test va_start.
8
9; TODO: Test va_start.
10; CHECK-LABEL: start:
11; CHECK-NEXT: .functype start (i32, i32) -> ()
12; CHECK-NOT: __stack_pointer
13define void @start(i8** %ap, ...) {
14entry:
15  %0 = bitcast i8** %ap to i8*
16; Store the second argument (the hidden vararg buffer pointer) into ap
17; CHECK: i32.store 0($0), $1
18  call void @llvm.va_start(i8* %0)
19  ret void
20}
21
22; Test va_end.
23
24; CHECK-LABEL: end:
25; CHECK-NEXT: .functype end (i32) -> (){{$}}
26; CHECK-NEXT: return{{$}}
27define void @end(i8** %ap) {
28entry:
29  %0 = bitcast i8** %ap to i8*
30  call void @llvm.va_end(i8* %0)
31  ret void
32}
33
34; Test va_copy.
35
36; CHECK-LABEL: copy:
37; CHECK-NEXT: .functype copy (i32, i32) -> (){{$}}
38; CHECK-NEXT: i32.load  $push0=, 0($1){{$}}
39; CHECK-NEXT: i32.store 0($0), $pop0{{$}}
40; CHECK-NEXT: return{{$}}
41define void @copy(i8** %ap, i8** %bp) {
42entry:
43  %0 = bitcast i8** %ap to i8*
44  %1 = bitcast i8** %bp to i8*
45  call void @llvm.va_copy(i8* %0, i8* %1)
46  ret void
47}
48
49; Test va_arg with an i8 argument.
50
51; CHECK-LABEL: arg_i8:
52; CHECK-NEXT: .functype arg_i8 (i32) -> (i32){{$}}
53; CHECK-NEXT: i32.load   $push[[NUM0:[0-9]+]]=, 0($0){{$}}
54; CHECK-NEXT: local.tee  $push[[NUM1:[0-9]+]]=, $1=, $pop[[NUM0]]{{$}}
55; CHECK-NEXT: i32.const  $push[[NUM2:[0-9]+]]=, 4{{$}}
56; CHECK-NEXT: i32.add    $push[[NUM3:[0-9]+]]=, $pop[[NUM1]], $pop[[NUM2]]{{$}}
57; CHECK-NEXT: i32.store  0($0), $pop[[NUM3]]{{$}}
58; CHECK-NEXT: i32.load   $push[[NUM4:[0-9]+]]=, 0($1){{$}}
59; CHECK-NEXT: return     $pop[[NUM4]]{{$}}
60define i8 @arg_i8(i8** %ap) {
61entry:
62  %t = va_arg i8** %ap, i8
63  ret i8 %t
64}
65
66; Test va_arg with an i32 argument.
67
68; CHECK-LABEL: arg_i32:
69; CHECK-NEXT: .functype arg_i32 (i32) -> (i32){{$}}
70; CHECK-NEXT: i32.load   $push[[NUM0:[0-9]+]]=, 0($0){{$}}
71; CHECK-NEXT: i32.const  $push[[NUM1:[0-9]+]]=, 3{{$}}
72; CHECK-NEXT: i32.add    $push[[NUM2:[0-9]+]]=, $pop[[NUM0]], $pop[[NUM1]]{{$}}
73; CHECK-NEXT: i32.const  $push[[NUM3:[0-9]+]]=, -4{{$}}
74; CHECK-NEXT: i32.and    $push[[NUM4:[0-9]+]]=, $pop[[NUM2]], $pop[[NUM3]]{{$}}
75; CHECK-NEXT: local.tee  $push[[NUM5:[0-9]+]]=, $1=, $pop[[NUM4]]{{$}}
76; CHECK-NEXT: i32.const  $push[[NUM6:[0-9]+]]=, 4{{$}}
77; CHECK-NEXT: i32.add    $push[[NUM7:[0-9]+]]=, $pop[[NUM5]], $pop[[NUM6]]{{$}}
78; CHECK-NEXT: i32.store  0($0), $pop[[NUM7]]{{$}}
79; CHECK-NEXT: i32.load   $push[[NUM8:[0-9]+]]=, 0($1){{$}}
80; CHECK-NEXT: return     $pop[[NUM8]]{{$}}
81define i32 @arg_i32(i8** %ap) {
82entry:
83  %t = va_arg i8** %ap, i32
84  ret i32 %t
85}
86
87; Test va_arg with an i128 argument.
88
89; CHECK-LABEL: arg_i128:
90; CHECK-NEXT: .functype arg_i128 (i32, i32) -> (){{$}}
91; CHECK: i32.and
92; CHECK: i64.load
93; CHECK: i64.load
94; CHECK: return{{$}}
95define i128 @arg_i128(i8** %ap) {
96entry:
97  %t = va_arg i8** %ap, i128
98  ret i128 %t
99}
100
101; Test a varargs call with no actual arguments.
102
103declare void @callee(...)
104
105; CHECK-LABEL: caller_none:
106; CHECK:      i32.const $push0=, 0
107; CHECK-NEXT: call callee, $pop0
108; CHECK-NEXT: return{{$}}
109define void @caller_none() {
110  call void (...) @callee()
111  ret void
112}
113
114; Test a varargs call with some actual arguments.
115; Note that the store of 2.0 is converted to an i64 store; this optimization
116; is not needed on WebAssembly, but there isn't currently a convenient hook for
117; disabling it.
118
119; CHECK-LABEL: caller_some
120; CHECK-DAG: i32.store
121; CHECK-DAG: i64.store
122define void @caller_some() {
123  call void (...) @callee(i32 0, double 2.0)
124  ret void
125}
126
127; Test a va_start call in a non-entry block
128; CHECK-LABEL: startbb:
129; CHECK: .functype startbb (i32, i32, i32) -> ()
130define void @startbb(i1 %cond, i8** %ap, ...) {
131entry:
132  br i1 %cond, label %bb0, label %bb1
133bb0:
134  ret void
135bb1:
136  %0 = bitcast i8** %ap to i8*
137; Store the second argument (the hidden vararg buffer pointer) into ap
138; CHECK: i32.store 0($1), $2
139  call void @llvm.va_start(i8* %0)
140  ret void
141}
142
143; Test a call to a varargs function with a non-legal fixed argument.
144
145declare void @callee_with_nonlegal_fixed(fp128, ...) nounwind
146
147; CHECK-LABEL: call_nonlegal_fixed:
148; CHECK: i64.const       $push[[L0:[0-9]+]]=, 0
149; CHECK: i64.const       $push[[L1:[0-9]+]]=, 0
150; CHECK: i32.const       $push[[L2:[0-9]+]]=, 0
151; CHECK: call            callee_with_nonlegal_fixed, $pop[[L0]], $pop[[L1]], $pop[[L2]]{{$}}
152define void @call_nonlegal_fixed() nounwind {
153  call void (fp128, ...) @callee_with_nonlegal_fixed(fp128 0xL00000000000000000000000000000000)
154  ret void
155}
156
157; Test a definition a varargs function with a non-legal fixed argument.
158
159; CHECK-LABEL: nonlegal_fixed:
160; CHECK-NEXT: .functype nonlegal_fixed (i64, i64, i32) -> (){{$}}
161define void @nonlegal_fixed(fp128 %x, ...) nounwind {
162  ret void
163}
164
165; Test that an fp128 argument is properly aligned and allocated
166; within a vararg buffer.
167
168; CHECK-LABEL: call_fp128_alignment:
169; CHECK:      global.get      $push5=, __stack_pointer
170; CHECK-NEXT: i32.const       $push6=, 32
171; CHECK-NEXT: i32.sub         $push10=, $pop5, $pop6
172; CHECK-NEXT: local.tee       $push9=, $1=, $pop10
173; CHECK-NEXT: global.set      __stack_pointer, $pop9
174; CHECK-NEXT: i32.const       $push0=, 16
175; CHECK-NEXT: i32.add         $push1=, $1, $pop0
176; CHECK-NEXT: i64.const       $push2=, -9223372036854775808
177; CHECK-NEXT: i64.store       0($pop1), $pop2
178; CHECK-NEXT: i64.const       $push3=, 1
179; CHECK-NEXT: i64.store       8($1), $pop3
180; CHECK-NEXT: i32.const       $push4=, 7
181; CHECK-NEXT: i32.store       0($1), $pop4
182; CHECK-NEXT: call            callee, $1
183define void @call_fp128_alignment(i8* %p) {
184entry:
185  call void (...) @callee(i8 7, fp128 0xL00000000000000018000000000000000)
186  ret void
187}
188
189declare void @llvm.va_start(i8*)
190declare void @llvm.va_end(i8*)
191declare void @llvm.va_copy(i8*, i8*)
192