From 73e25a571f12d3deaa6f4493a5b4792a9dae39eb Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 24 Sep 2022 22:38:40 +0000 Subject: deploy: 295081130ca1eed6e67dfc035e2df2c9ed49b174 --- src/llvm_kaleidoscope_rs/llvm/value.rs.html | 396 +++++++++++++++++++--------- 1 file changed, 269 insertions(+), 127 deletions(-) (limited to 'src/llvm_kaleidoscope_rs/llvm/value.rs.html') diff --git a/src/llvm_kaleidoscope_rs/llvm/value.rs.html b/src/llvm_kaleidoscope_rs/llvm/value.rs.html index 950eae0..eaea9c3 100644 --- a/src/llvm_kaleidoscope_rs/llvm/value.rs.html +++ b/src/llvm_kaleidoscope_rs/llvm/value.rs.html @@ -1,102 +1,108 @@ -value.rs - source
  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
+value.rs - source
+    
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
@@ -166,12 +172,83 @@
 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
+
#![allow(unused)]
+
 use llvm_sys::{
     analysis::{LLVMVerifierFailureAction, LLVMVerifyFunction},
     core::{
-        LLVMCountBasicBlocks, LLVMCountParams, LLVMDumpValue, LLVMGetParam, LLVMGetReturnType,
-        LLVMGetValueKind, LLVMGetValueName2, LLVMSetValueName2, LLVMTypeOf,
+        LLVMAddIncoming, LLVMAppendExistingBasicBlock, LLVMCountBasicBlocks, LLVMCountParams,
+        LLVMDumpValue, LLVMGetParam, LLVMGetReturnType, LLVMGetValueKind, LLVMGetValueName2,
+        LLVMIsAFunction, LLVMIsAPHINode, LLVMSetValueName2, LLVMTypeOf,
     },
     prelude::LLVMValueRef,
     LLVMTypeKind, LLVMValueKind,
@@ -181,7 +258,8 @@
 use std::marker::PhantomData;
 use std::ops::Deref;
 
-use super::Type;
+use super::BasicBlock;
+use super::Type;
 
 /// Wrapper for a LLVM Value Reference.
 #[derive(Copy, Clone)]
@@ -194,22 +272,34 @@
     /// # Panics
     ///
     /// Panics if `value_ref` is a null pointer.
-    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
         assert!(!value_ref.is_null());
         Value(value_ref, PhantomData)
     }
 
     /// Get the raw LLVM value reference.
     #[inline]
-    pub(super) fn value_ref(&self) -> LLVMValueRef {
+    pub(super) fn value_ref(&self) -> LLVMValueRef {
         self.0
     }
 
     /// Get the LLVM value kind for the given value reference.
-    pub(super) fn kind(&self) -> LLVMValueKind {
+    pub(super) fn kind(&self) -> LLVMValueKind {
         unsafe { LLVMGetValueKind(self.value_ref()) }
     }
 
+    /// Check if value is `function` type.
+    pub(super) fn is_function(&self) -> bool {
+        let cast = unsafe { LLVMIsAFunction(self.value_ref()) };
+        !cast.is_null()
+    }
+
+    /// Check if value is `phinode` type.
+    pub(super) fn is_phinode(&self) -> bool {
+        let cast = unsafe { LLVMIsAPHINode(self.value_ref()) };
+        !cast.is_null()
+    }
+
     /// Dump the LLVM Value to stdout.
     pub fn dump(&self) {
         unsafe { LLVMDumpValue(self.value_ref()) };
@@ -220,7 +310,7 @@
     /// # Panics
     ///
     /// Panics if LLVM API returns a `null` pointer.
-    pub fn type_of(&self) -> Type<'llvm> {
+    pub fn type_of(&self) -> Type<'llvm> {
         let type_ref = unsafe { LLVMTypeOf(self.value_ref()) };
         Type::new(type_ref)
     }
@@ -239,10 +329,10 @@
     /// # Panics
     ///
     /// Panics if LLVM API returns a `null` pointer.
-    pub fn get_name(&self) -> &'llvm str {
+    pub fn get_name(&self) -> &'llvm str {
         let name = unsafe {
             let mut len: libc::size_t = 0;
-            let name = LLVMGetValueName2(self.0, &mut len as _);
+            let name = LLVMGetValueName2(self.0, &mut len as _);
             assert!(!name.is_null());
 
             CStr::from_ptr(name)
@@ -254,13 +344,13 @@
     }
 
     /// Check if value is of `f64` type.
-    pub fn is_f64(&self) -> bool {
-        self.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind
+    pub fn is_f64(&self) -> bool {
+        self.type_of().kind() == LLVMTypeKind::LLVMDoubleTypeKind
     }
 
     /// Check if value is of integer type.
-    pub fn is_int(&self) -> bool {
-        self.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind
+    pub fn is_int(&self) -> bool {
+        self.type_of().kind() == LLVMTypeKind::LLVMIntegerTypeKind
     }
 }
 
@@ -271,7 +361,7 @@
 
 impl<'llvm> Deref for FnValue<'llvm> {
     type Target = Value<'llvm>;
-    fn deref(&self) -> &Self::Target {
+    fn deref(&self) -> &Self::Target {
         &self.0
     }
 }
@@ -282,11 +372,10 @@
     /// # Panics
     ///
     /// Panics if `value_ref` is a null pointer.
-    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
         let value = Value::new(value_ref);
-        debug_assert_eq!(
-            value.kind(),
-            LLVMValueKind::LLVMFunctionValueKind,
+        debug_assert!(
+            value.is_function(),
             "Expected a fn value when constructing FnValue!"
         );
 
@@ -298,13 +387,13 @@
     /// # Panics
     ///
     /// Panics if LLVM API returns a `null` pointer.
-    pub fn ret_type(&self) -> Type<'llvm> {
+    pub fn ret_type(&self) -> Type<'llvm> {
         let type_ref = unsafe { LLVMGetReturnType(LLVMTypeOf(self.value_ref())) };
         Type::new(type_ref)
     }
 
     /// Get the number of function arguments for the given function value.
-    pub fn args(&self) -> usize {
+    pub fn args(&self) -> usize {
         unsafe { LLVMCountParams(self.value_ref()) as usize }
     }
 
@@ -313,7 +402,7 @@
     /// # Panics
     ///
     /// Panics if LLVM API returns a `null` pointer or indexed out of bounds.
-    pub fn arg(&self, idx: usize) -> Value<'llvm> {
+    pub fn arg(&self, idx: usize) -> Value<'llvm> {
         assert!(idx < self.args());
 
         let value_ref = unsafe { LLVMGetParam(self.value_ref(), idx as libc::c_uint) };
@@ -321,21 +410,74 @@
     }
 
     /// Get the number of Basic Blocks for the given function value.
-    pub fn basic_blocks(&self) -> usize {
+    pub fn basic_blocks(&self) -> usize {
         unsafe { LLVMCountBasicBlocks(self.value_ref()) as usize }
     }
 
+    /// Append a Basic Block to the end of the function value.
+    pub fn append_basic_block(&self, bb: BasicBlock<'llvm>) {
+        unsafe {
+            LLVMAppendExistingBasicBlock(self.value_ref(), bb.bb_ref());
+        }
+    }
+
     /// Verify that the given function is valid.
-    pub fn verify(&self) -> bool {
+    pub fn verify(&self) -> bool {
         unsafe {
             LLVMVerifyFunction(
                 self.value_ref(),
                 LLVMVerifierFailureAction::LLVMPrintMessageAction,
-            ) == 0
+            ) == 0
+        }
+    }
+}
+
+/// Wrapper for a LLVM Value Reference specialized for contexts where phi values are needed.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+pub struct PhiValue<'llvm>(Value<'llvm>);
+
+impl<'llvm> Deref for PhiValue<'llvm> {
+    type Target = Value<'llvm>;
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
+impl<'llvm> PhiValue<'llvm> {
+    /// Create a new PhiValue instance.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `value_ref` is a null pointer.
+    pub(super) fn new(value_ref: LLVMValueRef) -> Self {
+        let value = Value::new(value_ref);
+        debug_assert!(
+            value.is_phinode(),
+            "Expected a phinode value when constructing PhiValue!"
+        );
+
+        PhiValue(value)
+    }
+
+    /// Add an incoming value to the end of a PHI list.
+    pub fn add_incoming(&self, ival: Value<'llvm>, ibb: BasicBlock<'llvm>) {
+        debug_assert_eq!(
+            ival.type_of().kind(),
+            self.type_of().kind(),
+            "Type of incoming phi value must be the same as the type used to build the phi node."
+        );
+
+        unsafe {
+            LLVMAddIncoming(
+                self.value_ref(),
+                &mut ival.value_ref() as _,
+                &mut ibb.bb_ref() as _,
+                1,
+            );
         }
     }
 }
-
-
- +
+
\ No newline at end of file -- cgit v1.2.3