forked from go-llvm/llgo
/
println.go
170 lines (153 loc) · 5.09 KB
/
println.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2011 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package llgo
import (
"fmt"
"code.google.com/p/go.tools/go/exact"
"code.google.com/p/go.tools/go/types"
"github.com/axw/gollvm/llvm"
)
func getPrintf(module llvm.Module) llvm.Value {
printf := module.NamedFunction("printf")
if printf.IsNil() {
charPtr := llvm.PointerType(llvm.Int8Type(), 0)
ftyp := llvm.FunctionType(llvm.Int32Type(), []llvm.Type{charPtr}, true)
printf = llvm.AddFunction(module, "printf", ftyp)
printf.SetFunctionCallConv(llvm.CCallConv)
}
return printf
}
func getFflush(module llvm.Module) llvm.Value {
fflush := module.NamedFunction("fflush")
if fflush.IsNil() {
voidPtr := llvm.PointerType(llvm.Int8Type(), 0)
ftyp := llvm.FunctionType(llvm.Int32Type(), []llvm.Type{voidPtr}, false)
fflush = llvm.AddFunction(module, "fflush", ftyp)
fflush.SetFunctionCallConv(llvm.CCallConv)
}
return fflush
}
func (c *compiler) getBoolString(v llvm.Value) llvm.Value {
startBlock := c.builder.GetInsertBlock()
resultBlock := llvm.InsertBasicBlock(startBlock, "")
resultBlock.MoveAfter(startBlock)
falseBlock := llvm.InsertBasicBlock(resultBlock, "")
CharPtr := llvm.PointerType(llvm.Int8Type(), 0)
falseString := c.builder.CreateGlobalStringPtr("false", "")
falseString = c.builder.CreateBitCast(falseString, CharPtr, "")
trueString := c.builder.CreateGlobalStringPtr("true", "")
trueString = c.builder.CreateBitCast(trueString, CharPtr, "")
c.builder.CreateCondBr(v, resultBlock, falseBlock)
c.builder.SetInsertPointAtEnd(falseBlock)
c.builder.CreateBr(resultBlock)
c.builder.SetInsertPointAtEnd(resultBlock)
result := c.builder.CreatePHI(CharPtr, "")
result.AddIncoming([]llvm.Value{trueString, falseString},
[]llvm.BasicBlock{startBlock, falseBlock})
return result
}
func (c *compiler) printValues(println_ bool, values ...Value) {
var args []llvm.Value = nil
if len(values) > 0 {
format := ""
args = make([]llvm.Value, 0, len(values)+1)
for i, value := range values {
llvm_value := value.LLVMValue()
typ := value.Type().Underlying()
if name, isname := typ.(*types.Named); isname {
typ = name.Underlying()
}
if println_ && i > 0 {
format += " "
}
switch typ := typ.(type) {
case *types.Basic:
switch typ.Kind() {
case types.Uint8:
format += "%hhu"
case types.Uint16:
format += "%hu"
case types.Uint32:
format += "%u"
case types.Uintptr, types.Uint:
format += "%lu"
case types.Uint64:
format += "%llu" // FIXME windows
case types.Int:
format += "%ld"
case types.Int8:
format += "%hhd"
case types.Int16:
format += "%hd"
case types.Int32:
format += "%d"
case types.Int64:
format += "%lld" // FIXME windows
case types.Float32:
llvm_value = c.builder.CreateFPExt(llvm_value, llvm.DoubleType(), "")
fallthrough
case types.Float64:
printfloat := c.runtime.printfloat.LLVMValue()
args := []llvm.Value{llvm_value}
llvm_value = c.builder.CreateCall(printfloat, args, "")
fallthrough
case types.String, types.UntypedString:
ptrval := c.builder.CreateExtractValue(llvm_value, 0, "")
lenval := c.builder.CreateExtractValue(llvm_value, 1, "")
llvm_value = ptrval
args = append(args, lenval)
format += "%.*s"
case types.Bool:
format += "%s"
llvm_value = c.getBoolString(llvm_value)
case types.UnsafePointer:
format += "%p"
default:
panic(fmt.Sprint("Unhandled Basic Kind: ", typ.Kind))
}
case *types.Interface:
format += "(0x%lx,0x%lx)"
ival := c.builder.CreateExtractValue(llvm_value, 0, "")
itype := c.builder.CreateExtractValue(llvm_value, 1, "")
args = append(args, ival)
llvm_value = itype
case *types.Slice, *types.Array:
// If we see a constant array, we either:
// Create an internal constant if it's a constant array, or
// Create space on the stack and store it there.
init_ := value.(*LLVMValue)
init_value := init_.LLVMValue()
llvm_value = c.builder.CreateAlloca(init_value.Type(), "")
c.builder.CreateStore(init_value, llvm_value)
// FIXME don't assume string...
format += "%s"
case *types.Pointer:
format += "0x%lx"
default:
panic(fmt.Sprintf("Unhandled type kind: %s (%T)", typ, typ))
}
args = append(args, llvm_value)
}
if println_ {
format += "\n"
}
formatval := c.builder.CreateGlobalStringPtr(format, "")
args = append([]llvm.Value{formatval}, args...)
} else {
var format string
if println_ {
format = "\n"
}
args = []llvm.Value{c.builder.CreateGlobalStringPtr(format, "")}
}
printf := getPrintf(c.module.Module)
c.NewValue(c.builder.CreateCall(printf, args, ""), types.Typ[types.Int32])
fflush := getFflush(c.module.Module)
fileptr := llvm.ConstNull(fflush.Type().ElementType().ParamTypes()[0])
c.builder.CreateCall(fflush, []llvm.Value{fileptr}, "")
}
func (c *compiler) printf(format string, args ...interface{}) {
s := exact.MakeString(fmt.Sprintf(format, args...))
c.printValues(true, c.NewConstValue(s, types.Typ[types.String]))
}