Skip to content

Commit

Permalink
Added missing instructions to the builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswailes committed Jun 3, 2011
1 parent b559397 commit 684ba49
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 8 deletions.
29 changes: 27 additions & 2 deletions lib/llvm/bindings.rb
Expand Up @@ -334,12 +334,17 @@ module C
attach_function :LLVMSizeOf, [:pointer], :pointer
attach_function :LLVMConstNeg, [:pointer], :pointer
attach_function :LLVMConstFNeg, [:pointer], :pointer
attach_function :LLVMConstNSWNeg, [:pointer], :pointer
attach_function :LLVMConstNUWNeg, [:pointer], :pointer
attach_function :LLVMConstNot, [:pointer], :pointer
attach_function :LLVMConstAdd, [:pointer, :pointer], :pointer
attach_function :LLVMConstNSWAdd, [:pointer, :pointer], :pointer
attach_function :LLVMConstFAdd, [:pointer, :pointer], :pointer
attach_function :LLVMConstNSWAdd, [:pointer, :pointer], :pointer
attach_function :LLVMConstNUWAdd, [:pointer, :pointer], :pointer
attach_function :LLVMConstSub, [:pointer, :pointer], :pointer
attach_function :LLVMConstFSub, [:pointer, :pointer], :pointer
attach_function :LLVMConstNSWSub, [:pointer, :pointer], :pointer
attach_function :LLVMConstNUWSub, [:pointer, :pointer], :pointer
attach_function :LLVMConstMul, [:pointer, :pointer], :pointer
attach_function :LLVMConstFMul, [:pointer, :pointer], :pointer
attach_function :LLVMConstUDiv, [:pointer, :pointer], :pointer
Expand Down Expand Up @@ -504,6 +509,7 @@ module C
attach_function :LLVMBuildAggregateRet, [:pointer, :pointer, :uint], :pointer
attach_function :LLVMBuildBr, [:pointer, :pointer], :pointer
attach_function :LLVMBuildCondBr, [:pointer, :pointer, :pointer, :pointer], :pointer
attach_function :LLVMBuildIndirectBr, [:pointer, :pointer, :uint], :pointer
attach_function :LLVMBuildSwitch, [:pointer, :pointer, :pointer, :uint], :pointer
attach_function :LLVMBuildInvoke, [:pointer, :pointer, :pointer, :uint, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildUnwind, [:pointer], :pointer
Expand All @@ -513,27 +519,45 @@ module C
attach_function :LLVMAddCase, [:pointer, :pointer, :pointer], :void

# Arithmetic
attach_function :LLVMBuildBinOp, [:pointer, :pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildAdd, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNSWAdd, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFAdd, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNSWAdd, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNUWAdd, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildSub, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFSub, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNSWSub, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNUWSub, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildMul, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFMul, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNSWMul, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNUWMul, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildUDiv, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildSDiv, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildExactSDiv, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFDiv, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildURem, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildSRem, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFRem, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildShl, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildLShr, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildAShr, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildAnd, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildOr, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildXor, [:pointer, :pointer, :pointer, :string], :pointer

attach_function :LLVMBuildNeg, [:pointer, :pointer, :string], :pointer
attach_function :LLVMBuildFNeg, [:pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNSWNeg, [:pointer, :pointer, :string], :pointer
attach_function :LLVMBuildNUWNeg, [:pointer, :pointer, :string], :pointer

attach_function :LLVMBuildNot, [:pointer, :pointer, :string], :pointer

# Memory
Expand All @@ -551,6 +575,7 @@ module C
attach_function :LLVMBuildGlobalStringPtr, [:pointer, :string, :string], :pointer

# Casts
attach_function :LLVMBuildCast, [:pointer, :pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildTrunc, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildZExt, [:pointer, :pointer, :pointer, :string], :pointer
attach_function :LLVMBuildSExt, [:pointer, :pointer, :pointer, :string], :pointer
Expand Down
107 changes: 101 additions & 6 deletions lib/llvm/core/builder.rb
Expand Up @@ -76,6 +76,15 @@ def br(block)
C.LLVMBuildBr(self, block))
end

# Indirect unconditional branching (i.e. goto)
# @param [LLVM::Value] addr Where to jump
# @param [Fixnum] num_dests The number of destinations that will be added.
# @return [LLVM::Instruction]
# @LLVMinst br
def ibr(addr, num_dests = 10)
Instruction.from_ptr(C.LLVMBuildIndirectBr(self, addr, num_dests))
end

# Conditional branching (i.e. if)
# @param [LLVM::Value] cond The condition
# @param [LLVM::BasicBlock] iftrue Where to jump if condition is true
Expand Down Expand Up @@ -131,6 +140,11 @@ def unwind
def unreachable
Instruction.from_ptr(C.LLVMBuildUnreachable(self))
end

# @LLVMinst binop
def binop(op, lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildBinOp(self, op, lhs, rhs, name))
end

# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
Expand All @@ -141,6 +155,15 @@ def add(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildAdd(self, lhs, rhs, name))
end

# @param [LLVM::Value] lhs Floating point or vector of floating points
# @param [LLVM::Value] rhs Floating point or vector of floating points
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The floating point sum of the two operands
# @LLVMinst fadd
def fadd(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildFAdd(self, lhs, rhs, name))
end

# No signed wrap addition.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
Expand All @@ -151,13 +174,14 @@ def nsw_add(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNSWAdd(self, lhs, rhs, name))
end

# @param [LLVM::Value] lhs Floating point or vector of floating points
# @param [LLVM::Value] rhs Floating point or vector of floating points
# No unsigned wrap addition.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The floating point sum of the two operands
# @LLVMinst fadd
def fadd(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildFAdd(self, lhs, rhs, name))
# @return [LLVM::Instruction] The integer sum of the two operands
# @LLVMinst nuw_add
def nuw_add(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNUWAdd(self, lhs, rhs, name))
end

# @param [LLVM::Value] lhs Integer or vector of integers
Expand All @@ -179,6 +203,26 @@ def fsub(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildFSub(self, lhs, rhs, name))
end

# No signed wrap subtraction.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The integer difference of the two operands
# @LLVMinst nsw_sub
def nsw_sub(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNSWSub(self, lhs, rhs, name))
end

# No unsigned wrap subtraction.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The integer difference of the two operands
# @LLVMinst nuw_sub
def nuw_sub(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNUWSub(self, lhs, rhs, name))
end

# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
Expand All @@ -198,6 +242,26 @@ def fmul(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildFMul(self, lhs, rhs, name))
end

# No signed wrap multiplication.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The integer product of the two operands
# @LLVMinst nsw_mul
def nsw_mul(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNSWMul(self, lhs, rhs, name))
end

# No unsigned wrap multiplication.
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The integer product of the two operands
# @LLVMinst nuw_mul
def nuw_mul(lhs, rhs, name = "")
Instruction.from_ptr(C.LLVMBuildNUWMul(self, lhs, rhs, name))
end

# Unsigned integer division
# @param [LLVM::Value] lhs Integer or vector of integers
# @param [LLVM::Value] rhs Integer or vector of integers
Expand Down Expand Up @@ -332,6 +396,36 @@ def xor(lhs, rhs, name = "")
def neg(arg, name = "")
Instruction.from_ptr(C.LLVMBuildNeg(self, arg, name))
end

# Floating point negation. Implemented as a shortcut to
# the equivalent sub instruction.
# @param [LLVM::Value] arg Float or vector of Floats
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The negated operand
# @LLVMinst fneg
def fneg(arg, name = "")
Instruction.from_ptr(C.LLVMBuildFNeg(self, arg, name))
end

# Not signed wrapped negation. Implemented as a shortcut to
# the equivalent sub instruction.
# @param [LLVM::Value] arg Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The negated operand
# @LLVMinst nsw_neg
def nsw_neg(arg, name = "")
Instruction.from_ptr(C.LLVMBuildNSWNeg(self, arg, name))
end

# Not unsigned wrapped negation. Implemented as a shortcut to
# the equivalent sub instruction.
# @param [LLVM::Value] arg Integer or vector of integers
# @param [String] name Name of the result in LLVM IR
# @return [LLVM::Instruction] The negated operand
# @LLVMinst nuw_neg
def nuw_neg(arg, name = "")
Instruction.from_ptr(C.LLVMBuildNUWNeg(self, arg, name))
end

# Boolean negation.
# @param [LLVM::Value] arg Integer or vector of integers
Expand Down Expand Up @@ -693,6 +787,7 @@ def icmp(pred, lhs, rhs, name = "")
# :uge - unordered and greater than or equal to
# :olt - ordered and less than
# :ule - unordered and less than or equal to
# :ult - unordered and less than
# :oge - ordered and greater than or equal to
# :sge - unordered and greater than or equal to
# :ole - ordered and less than or equal to
Expand Down
41 changes: 41 additions & 0 deletions lib/llvm/core/value.rb
Expand Up @@ -305,6 +305,16 @@ def -@
self.class.from_ptr(C.LLVMConstNeg(self))
end

# "No signed wrap" negation.
def nsw_neg
self.class.from_ptr(C.LLVMConstNSWNeg(self))
end

# "No unsigned wrap" negation.
def nuw_neg
self.class.from_ptr(C.LLVMConstNUWNeg(self))
end

# Boolean negation.
def not
self.class.from_ptr(C.LLVMConstNot(self))
Expand All @@ -320,12 +330,43 @@ def +(rhs)
def nsw_add(rhs)
self.class.from_ptr(C.LLVMConstNSWAdd(self, rhs))
end

# "No unsigned wrap" addition. See
# http://llvm.org/docs/LangRef.html#i_add for discusison.
def nuw_add(rhs)
self.class.from_ptr(C.LLVMConstNUWAdd(self, rhs))
end

# Subtraction.
def -(rhs)
self.class.from_ptr(C.LLVMConstSub(self, rhs))
end

# "No signed wrap" subtraction.
def nsw_sub(rhs)
self.class.from_ptr(C.LLVMConstNSWSub(self, rhs))
end

# "No unsigned wrap" subtraction.
def nuw_sub(rhs)
self.class.from_ptr(C.LLVMConstNUWSub(self, rhs))
end

# Multiplication.
def *(rhs)
self.class.from_ptr(C.LLVMConstMul(self, rhs))
end

# "No signed wrap" Multiplication.
def nsw_sub(rhs)
self.class.from_ptr(C.LLVMConstNSWMul(self, rhs))
end

# "No unsigned wrap" Multiplication.
def nuw_add(rhs)
self.class.from_ptr(C.LLVMConstNUWMul(self, rhs))
end

# Unsigned division.
def udiv(rhs)
self.class.from_ptr(C.LLVMConstUDiv(self, rhs))
Expand Down

0 comments on commit 684ba49

Please sign in to comment.