From 219aa23d25fa189a744e603de34b0b63c8319e94 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 18 Aug 2014 22:51:54 +0800 Subject: [PATCH] compiler/ir_values: implement sign extension and truncation --- artiq/compiler/ir_values.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/artiq/compiler/ir_values.py b/artiq/compiler/ir_values.py index 355a9a06d..7f925f012 100644 --- a/artiq/compiler/ir_values.py +++ b/artiq/compiler/ir_values.py @@ -57,24 +57,23 @@ class VInt: zero = lc.Constant.int(lc.Type.int(self.nbits), 0) return VBool(llvm_value=builder.icmp(lc.ICMP_NE, self.llvm_value, zero)) - def o_int(self, builder): + def _o_intx(self, target_bits, builder): if builder is None: - return VInt() + return VInt(target_bits) else: - if self.nbits == 32: + if self.nbits == target_bits: return self - else: - raise NotImplementedError + if self.nbits > target_bits: + return VInt(target_bits, llvm_value=builder.trunc(self.llvm_value, lc.Type.int(target_bits))) + if self.nbits < target_bits: + return VInt(target_bits, llvm_value=builder.sext(self.llvm_value, lc.Type.int(target_bits))) + + def o_int(self, builder): + return self._o_intx(32, builder) o_round = o_int def o_int64(self, builder): - if builder is None: - return VInt(64) - else: - if self.nbits == 64: - return self - else: - raise NotImplementedError + return self._o_intx(64, builder) o_round64 = o_int64 def _make_vint_binop_method(builder_name):