github.com/llir/llvm@v0.3.6/ir/constant/expr_conversion.go (about) 1 package constant 2 3 import ( 4 "fmt" 5 6 "github.com/llir/llvm/ir/types" 7 ) 8 9 // --- [ Conversion expressions ] ---------------------------------------------- 10 11 // ~~~ [ trunc ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 12 13 // ExprTrunc is an LLVM IR trunc expression. 14 type ExprTrunc struct { 15 // Value before conversion. 16 From Constant 17 // Type after conversion. 18 To types.Type 19 } 20 21 // NewTrunc returns a new trunc expression based on the given source value and 22 // target type. 23 func NewTrunc(from Constant, to types.Type) *ExprTrunc { 24 e := &ExprTrunc{From: from, To: to} 25 // Compute type. 26 e.Type() 27 return e 28 } 29 30 // String returns the LLVM syntax representation of the constant expression as a 31 // type-value pair. 32 func (e *ExprTrunc) String() string { 33 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 34 } 35 36 // Type returns the type of the constant expression. 37 func (e *ExprTrunc) Type() types.Type { 38 return e.To 39 } 40 41 // Ident returns the identifier associated with the constant expression. 42 func (e *ExprTrunc) Ident() string { 43 // 'trunc' '(' From=TypeConst 'to' To=Type ')' 44 return fmt.Sprintf("trunc (%s to %s)", e.From, e.To) 45 } 46 47 // ~~~ [ zext ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48 49 // ExprZExt is an LLVM IR zext expression. 50 type ExprZExt struct { 51 // Value before conversion. 52 From Constant 53 // Type after conversion. 54 To types.Type 55 } 56 57 // NewZExt returns a new zext expression based on the given source value and 58 // target type. 59 func NewZExt(from Constant, to types.Type) *ExprZExt { 60 e := &ExprZExt{From: from, To: to} 61 // Compute type. 62 e.Type() 63 return e 64 } 65 66 // String returns the LLVM syntax representation of the constant expression as a 67 // type-value pair. 68 func (e *ExprZExt) String() string { 69 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 70 } 71 72 // Type returns the type of the constant expression. 73 func (e *ExprZExt) Type() types.Type { 74 return e.To 75 } 76 77 // Ident returns the identifier associated with the constant expression. 78 func (e *ExprZExt) Ident() string { 79 // 'zext' '(' From=TypeConst 'to' To=Type ')' 80 return fmt.Sprintf("zext (%s to %s)", e.From, e.To) 81 } 82 83 // ~~~ [ sext ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 84 85 // ExprSExt is an LLVM IR sext expression. 86 type ExprSExt struct { 87 // Value before conversion. 88 From Constant 89 // Type after conversion. 90 To types.Type 91 } 92 93 // NewSExt returns a new sext expression based on the given source value and 94 // target type. 95 func NewSExt(from Constant, to types.Type) *ExprSExt { 96 e := &ExprSExt{From: from, To: to} 97 // Compute type. 98 e.Type() 99 return e 100 } 101 102 // String returns the LLVM syntax representation of the constant expression as a 103 // type-value pair. 104 func (e *ExprSExt) String() string { 105 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 106 } 107 108 // Type returns the type of the constant expression. 109 func (e *ExprSExt) Type() types.Type { 110 return e.To 111 } 112 113 // Ident returns the identifier associated with the constant expression. 114 func (e *ExprSExt) Ident() string { 115 // 'sext' '(' From=TypeConst 'to' To=Type ')' 116 return fmt.Sprintf("sext (%s to %s)", e.From, e.To) 117 } 118 119 // ~~~ [ fptrunc ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 120 121 // ExprFPTrunc is an LLVM IR fptrunc expression. 122 type ExprFPTrunc struct { 123 // Value before conversion. 124 From Constant 125 // Type after conversion. 126 To types.Type 127 } 128 129 // NewFPTrunc returns a new fptrunc expression based on the given source value 130 // and target type. 131 func NewFPTrunc(from Constant, to types.Type) *ExprFPTrunc { 132 e := &ExprFPTrunc{From: from, To: to} 133 // Compute type. 134 e.Type() 135 return e 136 } 137 138 // String returns the LLVM syntax representation of the constant expression as a 139 // type-value pair. 140 func (e *ExprFPTrunc) String() string { 141 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 142 } 143 144 // Type returns the type of the constant expression. 145 func (e *ExprFPTrunc) Type() types.Type { 146 return e.To 147 } 148 149 // Ident returns the identifier associated with the constant expression. 150 func (e *ExprFPTrunc) Ident() string { 151 // 'fptrunc' '(' From=TypeConst 'to' To=Type ')' 152 return fmt.Sprintf("fptrunc (%s to %s)", e.From, e.To) 153 } 154 155 // ~~~ [ fpext ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 156 157 // ExprFPExt is an LLVM IR fpext expression. 158 type ExprFPExt struct { 159 // Value before conversion. 160 From Constant 161 // Type after conversion. 162 To types.Type 163 } 164 165 // NewFPExt returns a new fpext expression based on the given source value and 166 // target type. 167 func NewFPExt(from Constant, to types.Type) *ExprFPExt { 168 e := &ExprFPExt{From: from, To: to} 169 // Compute type. 170 e.Type() 171 return e 172 } 173 174 // String returns the LLVM syntax representation of the constant expression as a 175 // type-value pair. 176 func (e *ExprFPExt) String() string { 177 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 178 } 179 180 // Type returns the type of the constant expression. 181 func (e *ExprFPExt) Type() types.Type { 182 return e.To 183 } 184 185 // Ident returns the identifier associated with the constant expression. 186 func (e *ExprFPExt) Ident() string { 187 // 'fpext' '(' From=TypeConst 'to' To=Type ')' 188 return fmt.Sprintf("fpext (%s to %s)", e.From, e.To) 189 } 190 191 // ~~~ [ fptoui ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 192 193 // ExprFPToUI is an LLVM IR fptoui expression. 194 type ExprFPToUI struct { 195 // Value before conversion. 196 From Constant 197 // Type after conversion. 198 To types.Type 199 } 200 201 // NewFPToUI returns a new fptoui expression based on the given source value and 202 // target type. 203 func NewFPToUI(from Constant, to types.Type) *ExprFPToUI { 204 e := &ExprFPToUI{From: from, To: to} 205 // Compute type. 206 e.Type() 207 return e 208 } 209 210 // String returns the LLVM syntax representation of the constant expression as a 211 // type-value pair. 212 func (e *ExprFPToUI) String() string { 213 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 214 } 215 216 // Type returns the type of the constant expression. 217 func (e *ExprFPToUI) Type() types.Type { 218 return e.To 219 } 220 221 // Ident returns the identifier associated with the constant expression. 222 func (e *ExprFPToUI) Ident() string { 223 // 'fptoui' '(' From=TypeConst 'to' To=Type ')' 224 return fmt.Sprintf("fptoui (%s to %s)", e.From, e.To) 225 } 226 227 // ~~~ [ fptosi ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 228 229 // ExprFPToSI is an LLVM IR fptosi expression. 230 type ExprFPToSI struct { 231 // Value before conversion. 232 From Constant 233 // Type after conversion. 234 To types.Type 235 } 236 237 // NewFPToSI returns a new fptosi expression based on the given source value and 238 // target type. 239 func NewFPToSI(from Constant, to types.Type) *ExprFPToSI { 240 e := &ExprFPToSI{From: from, To: to} 241 // Compute type. 242 e.Type() 243 return e 244 } 245 246 // String returns the LLVM syntax representation of the constant expression as a 247 // type-value pair. 248 func (e *ExprFPToSI) String() string { 249 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 250 } 251 252 // Type returns the type of the constant expression. 253 func (e *ExprFPToSI) Type() types.Type { 254 return e.To 255 } 256 257 // Ident returns the identifier associated with the constant expression. 258 func (e *ExprFPToSI) Ident() string { 259 // 'fptosi' '(' From=TypeConst 'to' To=Type ')' 260 return fmt.Sprintf("fptosi (%s to %s)", e.From, e.To) 261 } 262 263 // ~~~ [ uitofp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 264 265 // ExprUIToFP is an LLVM IR uitofp expression. 266 type ExprUIToFP struct { 267 // Value before conversion. 268 From Constant 269 // Type after conversion. 270 To types.Type 271 } 272 273 // NewUIToFP returns a new uitofp expression based on the given source value and 274 // target type. 275 func NewUIToFP(from Constant, to types.Type) *ExprUIToFP { 276 e := &ExprUIToFP{From: from, To: to} 277 // Compute type. 278 e.Type() 279 return e 280 } 281 282 // String returns the LLVM syntax representation of the constant expression as a 283 // type-value pair. 284 func (e *ExprUIToFP) String() string { 285 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 286 } 287 288 // Type returns the type of the constant expression. 289 func (e *ExprUIToFP) Type() types.Type { 290 return e.To 291 } 292 293 // Ident returns the identifier associated with the constant expression. 294 func (e *ExprUIToFP) Ident() string { 295 // 'uitofp' '(' From=TypeConst 'to' To=Type ')' 296 return fmt.Sprintf("uitofp (%s to %s)", e.From, e.To) 297 } 298 299 // ~~~ [ sitofp ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 300 301 // ExprSIToFP is an LLVM IR sitofp expression. 302 type ExprSIToFP struct { 303 // Value before conversion. 304 From Constant 305 // Type after conversion. 306 To types.Type 307 } 308 309 // NewSIToFP returns a new sitofp expression based on the given source value and 310 // target type. 311 func NewSIToFP(from Constant, to types.Type) *ExprSIToFP { 312 e := &ExprSIToFP{From: from, To: to} 313 // Compute type. 314 e.Type() 315 return e 316 } 317 318 // String returns the LLVM syntax representation of the constant expression as a 319 // type-value pair. 320 func (e *ExprSIToFP) String() string { 321 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 322 } 323 324 // Type returns the type of the constant expression. 325 func (e *ExprSIToFP) Type() types.Type { 326 return e.To 327 } 328 329 // Ident returns the identifier associated with the constant expression. 330 func (e *ExprSIToFP) Ident() string { 331 // 'sitofp' '(' From=TypeConst 'to' To=Type ')' 332 return fmt.Sprintf("sitofp (%s to %s)", e.From, e.To) 333 } 334 335 // ~~~ [ ptrtoint ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 336 337 // ExprPtrToInt is an LLVM IR ptrtoint expression. 338 type ExprPtrToInt struct { 339 // Value before conversion. 340 From Constant 341 // Type after conversion. 342 To types.Type 343 } 344 345 // NewPtrToInt returns a new ptrtoint expression based on the given source value 346 // and target type. 347 func NewPtrToInt(from Constant, to types.Type) *ExprPtrToInt { 348 e := &ExprPtrToInt{From: from, To: to} 349 // Compute type. 350 e.Type() 351 return e 352 } 353 354 // String returns the LLVM syntax representation of the constant expression as a 355 // type-value pair. 356 func (e *ExprPtrToInt) String() string { 357 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 358 } 359 360 // Type returns the type of the constant expression. 361 func (e *ExprPtrToInt) Type() types.Type { 362 return e.To 363 } 364 365 // Ident returns the identifier associated with the constant expression. 366 func (e *ExprPtrToInt) Ident() string { 367 // 'ptrtoint' '(' From=TypeConst 'to' To=Type ')' 368 return fmt.Sprintf("ptrtoint (%s to %s)", e.From, e.To) 369 } 370 371 // ~~~ [ inttoptr ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 372 373 // ExprIntToPtr is an LLVM IR inttoptr expression. 374 type ExprIntToPtr struct { 375 // Value before conversion. 376 From Constant 377 // Type after conversion. 378 To types.Type 379 } 380 381 // NewIntToPtr returns a new inttoptr expression based on the given source value 382 // and target type. 383 func NewIntToPtr(from Constant, to types.Type) *ExprIntToPtr { 384 e := &ExprIntToPtr{From: from, To: to} 385 // Compute type. 386 e.Type() 387 return e 388 } 389 390 // String returns the LLVM syntax representation of the constant expression as a 391 // type-value pair. 392 func (e *ExprIntToPtr) String() string { 393 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 394 } 395 396 // Type returns the type of the constant expression. 397 func (e *ExprIntToPtr) Type() types.Type { 398 return e.To 399 } 400 401 // Ident returns the identifier associated with the constant expression. 402 func (e *ExprIntToPtr) Ident() string { 403 // 'inttoptr' '(' From=TypeConst 'to' To=Type ')' 404 return fmt.Sprintf("inttoptr (%s to %s)", e.From, e.To) 405 } 406 407 // ~~~ [ bitcast ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 408 409 // ExprBitCast is an LLVM IR bitcast expression. 410 type ExprBitCast struct { 411 // Value before conversion. 412 From Constant 413 // Type after conversion. 414 To types.Type 415 } 416 417 // NewBitCast returns a new bitcast expression based on the given source value 418 // and target type. 419 func NewBitCast(from Constant, to types.Type) *ExprBitCast { 420 e := &ExprBitCast{From: from, To: to} 421 // Compute type. 422 e.Type() 423 return e 424 } 425 426 // String returns the LLVM syntax representation of the constant expression as a 427 // type-value pair. 428 func (e *ExprBitCast) String() string { 429 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 430 } 431 432 // Type returns the type of the constant expression. 433 func (e *ExprBitCast) Type() types.Type { 434 return e.To 435 } 436 437 // Ident returns the identifier associated with the constant expression. 438 func (e *ExprBitCast) Ident() string { 439 // 'bitcast' '(' From=TypeConst 'to' To=Type ')' 440 return fmt.Sprintf("bitcast (%s to %s)", e.From, e.To) 441 } 442 443 // ~~~ [ addrspacecast ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 444 445 // ExprAddrSpaceCast is an LLVM IR addrspacecast expression. 446 type ExprAddrSpaceCast struct { 447 // Value before conversion. 448 From Constant 449 // Type after conversion. 450 To types.Type 451 } 452 453 // NewAddrSpaceCast returns a new addrspacecast expression based on the given 454 // source value and target type. 455 func NewAddrSpaceCast(from Constant, to types.Type) *ExprAddrSpaceCast { 456 e := &ExprAddrSpaceCast{From: from, To: to} 457 // Compute type. 458 e.Type() 459 return e 460 } 461 462 // String returns the LLVM syntax representation of the constant expression as a 463 // type-value pair. 464 func (e *ExprAddrSpaceCast) String() string { 465 return fmt.Sprintf("%s %s", e.Type(), e.Ident()) 466 } 467 468 // Type returns the type of the constant expression. 469 func (e *ExprAddrSpaceCast) Type() types.Type { 470 return e.To 471 } 472 473 // Ident returns the identifier associated with the constant expression. 474 func (e *ExprAddrSpaceCast) Ident() string { 475 // 'addrspacecast' '(' From=TypeConst 'to' To=Type ')' 476 return fmt.Sprintf("addrspacecast (%s to %s)", e.From, e.To) 477 }