1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use super::*;
use std::collections::HashMap;
#[derive(Copy, Clone, Debug)]
pub enum VaPart {
Start(CDeclId),
End(CDeclId),
Copy(CDeclId, CDeclId),
}
macro_rules! match_or {
([$e:expr] $p:pat => $r:tt) => {
let $r = match $e {
$p => $r,
_ => return None,
};
};
([$e:expr] $p:pat if $g:expr => $r:tt) => {
let $r = match $e {
$p if $g => $r,
_ => return None,
};
};
}
impl<'c> Translation<'c> {
fn register_va_decls(
&self,
promoted_decl_id: Option<CDeclId>,
copied_decl_ids: IndexSet<CDeclId>,
) {
let mut fn_ctx = self.function_context.borrow_mut();
fn_ctx.copied_va_decls = Some(copied_decl_ids);
if let Some(decl_id) = promoted_decl_id {
fn_ctx.promoted_va_decl = Some(decl_id);
match self.ast_context[decl_id].kind {
CDeclKind::Variable { ref ident, .. } => {
self.renamer
.borrow_mut()
.insert(decl_id, ident)
.expect(&format!("Failed to install variadic function kludge"));
}
_ => panic!("va_arg was not a variable"),
}
}
}
pub fn is_promoted_va_decl(&self, decl_id: CDeclId) -> bool {
let fn_ctx = self.function_context.borrow();
fn_ctx.promoted_va_decl == Some(decl_id)
}
pub fn is_copied_va_decl(&self, decl_id: CDeclId) -> bool {
let fn_ctx = self.function_context.borrow();
if let Some(ref decls) = fn_ctx.copied_va_decls {
decls.contains(&decl_id)
} else {
false
}
}
pub fn get_promoted_va_decl(&self) -> Option<CDeclId> {
self.function_context.borrow().promoted_va_decl
}
pub fn match_vastart(&self, expr: CExprId) -> Option<CDeclId> {
match_or! { [self.ast_context[expr].kind]
CExprKind::ImplicitCast(_, e, _, _, _) => e }
match_or! { [self.ast_context[e].kind]
CExprKind::DeclRef(_, va_id, _) => va_id }
Some(va_id)
}
pub fn match_vaend(&self, expr: CExprId) -> Option<CDeclId> {
self.match_vastart(expr)
}
pub fn match_vacopy(&self, dst_expr: CExprId, src_expr: CExprId) -> Option<(CDeclId, CDeclId)> {
let dst_id = self.match_vastart(dst_expr);
let src_id = self.match_vastart(src_expr);
if let (Some(did), Some(sid)) = (dst_id, src_id) {
return Some((did, sid));
}
None
}
pub fn match_vapart(&self, expr: CExprId) -> Option<VaPart> {
match_or! { [self.ast_context[expr].kind]
CExprKind::Call(_, func, ref args) => (func, args) }
match_or! { [self.ast_context[func].kind]
CExprKind::ImplicitCast(_, fexp, CastKind::BuiltinFnToFnPtr, _, _) => fexp }
match_or! { [self.ast_context[fexp].kind]
CExprKind::DeclRef(_, decl_id, _) => decl_id }
match_or! { [self.ast_context[decl_id].kind]
CDeclKind::Function { ref name, .. } => name }
match name as &str {
"__builtin_va_start" => {
if args.len() != 2 {
return None;
}
self.match_vastart(args[0]).map(VaPart::Start)
}
"__builtin_va_copy" => {
if args.len() != 2 {
return None;
}
self.match_vacopy(args[0], args[1])
.map(|(did, sid)| VaPart::Copy(did, sid))
}
"__builtin_va_end" => {
if args.len() != 1 {
return None;
}
self.match_vaend(args[0]).map(VaPart::End)
}
_ => None,
}
}
pub fn convert_vaarg(
&self,
ctx: ExprContext,
ty: CQualTypeId,
val_id: CExprId,
) -> Result<WithStmts<P<Expr>>, TranslationError> {
if self.tcfg.translate_valist {
let val = self.convert_expr(ctx.used(), val_id)?;
let ty = self.convert_type(ty.ctype)?;
let mut res = val.map(|va| {
let path = mk()
.path_segment_with_args(mk().ident("arg"), mk().angle_bracketed_args(vec![ty]));
mk().method_call_expr(va, path, vec![] as Vec<P<Expr>>)
});
if ctx.is_unused() {
res.stmts.push(mk().expr_stmt(res.val));
res.val = self.panic_or_err("convert_vaarg unused");
}
Ok(res)
} else {
Err(format_err!(
"Variable argument lists are not supported (requires --translate-valist)"
))?
}
}
pub fn is_well_formed_variadic(&self, body: CStmtId) -> bool {
let mut candidates: HashMap<CDeclId, Vec<VaPart>> = HashMap::new();
let mut iter = DFExpr::new(&self.ast_context, body.into());
while let Some(s) = iter.next() {
if let SomeId::Expr(e) = s {
if let Some(part) = self.match_vapart(e) {
let id = match part {
VaPart::Start(va_id) | VaPart::End(va_id) => va_id,
VaPart::Copy(dst_va_id, _src_va_id) => dst_va_id,
};
candidates.entry(id).or_insert(vec![]).push(part);
}
}
}
if candidates.len() == 0 {
return true;
}
let start_called = |k: &CDeclId| {
candidates[k].iter().any(|e| {
if let VaPart::Start(_) = e {
true
} else {
false
}
})
};
let copy_called = |k: &CDeclId| {
candidates[k].iter().any(|e| {
if let VaPart::Copy(_, _) = e {
true
} else {
false
}
})
};
let end_called = |k: &CDeclId| {
candidates[k]
.iter()
.any(|e| if let VaPart::End(_) = e { true } else { false })
};
let copied = candidates
.keys()
.filter_map(|k| {
if copy_called(k) && end_called(k) {
Some(*k)
} else {
None
}
})
.collect::<IndexSet<CDeclId>>();
let promotable = candidates
.keys()
.filter_map(|k| {
if start_called(k) && end_called(k) {
Some(*k)
} else {
None
}
})
.collect::<Vec<CDeclId>>();
if promotable.len() + copied.len() > 0 {
let promoted = match promotable.len() {
0 => None,
1 => Some(promotable[0]),
_ => panic!(
"couldn't determine which va_list to promote in {}",
self.function_context.borrow().get_name()
),
};
self.register_va_decls(promoted, copied);
return true;
}
false
}
}