-
Notifications
You must be signed in to change notification settings - Fork 2
/
constant.go
42 lines (37 loc) · 1.09 KB
/
constant.go
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
package irutil
import (
"encoding/binary"
"strings"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
"github.com/llir/llvm/ir/value"
)
// NewZero returns a new zero value of the given type.
func NewZero(typ types.Type) value.Value {
switch typ := typ.(type) {
case *types.IntType:
return constant.NewInt(typ, 0)
case *types.FloatType:
return constant.NewFloat(typ, 0)
default:
return constant.NewZeroInitializer(typ)
}
}
// NewCString returns a new NULL-terminated character array constant based on
// the given UTF-8 string contents.
func NewCString(s string) *constant.CharArray {
return constant.NewCharArrayFromString(s + "\x00")
}
// NewPascalString returns a length-prefixed string, also known as a Pascal
// string. The length prefix is stored as a 32-bit integer with big-endian
// encoding.
func NewPascalString(s string) *constant.CharArray {
var buf strings.Builder
var length [4]byte
binary.BigEndian.PutUint32(length[:], uint32(len(s)))
for _, b := range length {
buf.WriteByte(b)
}
buf.WriteString(s)
return constant.NewCharArrayFromString(buf.String())
}