Skip to content

Commit 268e646

Browse files
committed
rustc_codegen_llvm: pretty up a few builder methods with a macro.
1 parent cd3f21b commit 268e646

File tree

1 file changed

+34
-138
lines changed

1 file changed

+34
-138
lines changed

src/librustc_codegen_llvm/builder.rs

+34-138
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ impl HasCodegen<'tcx> for Builder<'_, 'll, 'tcx> {
100100
type CodegenCx = CodegenCx<'ll, 'tcx>;
101101
}
102102

103+
macro_rules! builder_methods_for_value_instructions {
104+
($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
105+
$(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
106+
unsafe {
107+
llvm::$llvm_capi(self.llbuilder, $($arg,)* noname())
108+
}
109+
})*
110+
}
111+
}
112+
103113
impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
104114
fn new_block<'b>(
105115
cx: &'a CodegenCx<'ll, 'tcx>,
@@ -227,17 +237,30 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
227237
}
228238
}
229239

230-
/* Arithmetic */
231-
fn add(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
232-
unsafe {
233-
llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
234-
}
235-
}
236-
237-
fn fadd(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
238-
unsafe {
239-
llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
240-
}
240+
builder_methods_for_value_instructions! {
241+
add(a, b) => LLVMBuildAdd,
242+
fadd(a, b) => LLVMBuildFAdd,
243+
sub(a, b) => LLVMBuildSub,
244+
fsub(a, b) => LLVMBuildFSub,
245+
mul(a, b) => LLVMBuildMul,
246+
fmul(a, b) => LLVMBuildFMul,
247+
udiv(a, b) => LLVMBuildUDiv,
248+
exactudiv(a, b) => LLVMBuildExactUDiv,
249+
sdiv(a, b) => LLVMBuildSDiv,
250+
exactsdiv(a, b) => LLVMBuildExactSDiv,
251+
fdiv(a, b) => LLVMBuildFDiv,
252+
urem(a, b) => LLVMBuildURem,
253+
srem(a, b) => LLVMBuildSRem,
254+
frem(a, b) => LLVMBuildFRem,
255+
shl(a, b) => LLVMBuildShl,
256+
lshr(a, b) => LLVMBuildLShr,
257+
ashr(a, b) => LLVMBuildAShr,
258+
and(a, b) => LLVMBuildAnd,
259+
or(a, b) => LLVMBuildOr,
260+
xor(a, b) => LLVMBuildXor,
261+
neg(x) => LLVMBuildNeg,
262+
fneg(x) => LLVMBuildFNeg,
263+
not(x) => LLVMBuildNot,
241264
}
242265

243266
fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
@@ -248,18 +271,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
248271
}
249272
}
250273

251-
fn sub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
252-
unsafe {
253-
llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
254-
}
255-
}
256-
257-
fn fsub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
258-
unsafe {
259-
llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
260-
}
261-
}
262-
263274
fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
264275
unsafe {
265276
let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
@@ -268,18 +279,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
268279
}
269280
}
270281

271-
fn mul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
272-
unsafe {
273-
llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
274-
}
275-
}
276-
277-
fn fmul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
278-
unsafe {
279-
llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
280-
}
281-
}
282-
283282
fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
284283
unsafe {
285284
let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
@@ -288,37 +287,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
288287
}
289288
}
290289

291-
292-
fn udiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
293-
unsafe {
294-
llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
295-
}
296-
}
297-
298-
fn exactudiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
299-
unsafe {
300-
llvm::LLVMBuildExactUDiv(self.llbuilder, lhs, rhs, noname())
301-
}
302-
}
303-
304-
fn sdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
305-
unsafe {
306-
llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
307-
}
308-
}
309-
310-
fn exactsdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
311-
unsafe {
312-
llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
313-
}
314-
}
315-
316-
fn fdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
317-
unsafe {
318-
llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
319-
}
320-
}
321-
322290
fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
323291
unsafe {
324292
let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
@@ -327,24 +295,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
327295
}
328296
}
329297

330-
fn urem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
331-
unsafe {
332-
llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
333-
}
334-
}
335-
336-
fn srem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
337-
unsafe {
338-
llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
339-
}
340-
}
341-
342-
fn frem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
343-
unsafe {
344-
llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
345-
}
346-
}
347-
348298
fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
349299
unsafe {
350300
let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
@@ -353,60 +303,6 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
353303
}
354304
}
355305

356-
fn shl(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
357-
unsafe {
358-
llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
359-
}
360-
}
361-
362-
fn lshr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
363-
unsafe {
364-
llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
365-
}
366-
}
367-
368-
fn ashr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
369-
unsafe {
370-
llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
371-
}
372-
}
373-
374-
fn and(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
375-
unsafe {
376-
llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
377-
}
378-
}
379-
380-
fn or(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
381-
unsafe {
382-
llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
383-
}
384-
}
385-
386-
fn xor(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
387-
unsafe {
388-
llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
389-
}
390-
}
391-
392-
fn neg(&mut self, v: &'ll Value) -> &'ll Value {
393-
unsafe {
394-
llvm::LLVMBuildNeg(self.llbuilder, v, noname())
395-
}
396-
}
397-
398-
fn fneg(&mut self, v: &'ll Value) -> &'ll Value {
399-
unsafe {
400-
llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
401-
}
402-
}
403-
404-
fn not(&mut self, v: &'ll Value) -> &'ll Value {
405-
unsafe {
406-
llvm::LLVMBuildNot(self.llbuilder, v, noname())
407-
}
408-
}
409-
410306
fn checked_binop(
411307
&mut self,
412308
oop: OverflowOp,

0 commit comments

Comments
 (0)