forked from go-llvm/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.go
76 lines (67 loc) · 2.06 KB
/
alg.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
// Copyright 2013 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 (
"code.google.com/p/go.tools/go/types"
"github.com/axw/gollvm/llvm"
)
type AlgorithmKind int
const (
AlgorithmHash AlgorithmKind = iota
AlgorithmEqual
AlgorithmPrint
AlgorithmCopy
)
// algorithmMap maps types to their runtime algorithms (equality, hash, etc.)
type algorithmMap struct {
module llvm.Module
runtime *runtimeInterface
hashAlgFunctionType,
equalAlgFunctionType,
printAlgFunctionType,
copyAlgFunctionType llvm.Type
}
func newAlgorithmMap(m llvm.Module, runtime *runtimeInterface, target llvm.TargetData) *algorithmMap {
am := &algorithmMap{
module: m,
runtime: runtime,
}
uintptrType := target.IntPtrType()
voidPtrType := llvm.PointerType(llvm.Int8Type(), 0)
boolType := llvm.Int1Type()
params := []llvm.Type{uintptrType, voidPtrType}
am.hashAlgFunctionType = llvm.FunctionType(uintptrType, params, false)
params = []llvm.Type{uintptrType, uintptrType, uintptrType}
am.equalAlgFunctionType = llvm.FunctionType(boolType, params, false)
params = []llvm.Type{uintptrType, voidPtrType}
am.printAlgFunctionType = llvm.FunctionType(llvm.VoidType(), params, false)
params = []llvm.Type{uintptrType, voidPtrType, voidPtrType}
am.copyAlgFunctionType = llvm.FunctionType(llvm.VoidType(), params, false)
return am
}
func (am *algorithmMap) eqalg(t types.Type) llvm.Value {
t = t.Underlying()
if st, ok := t.(*types.Struct); ok && st.NumFields() == 1 {
t = st.Field(0).Type().Underlying()
}
switch t := t.(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
return am.runtime.streqalg.LLVMValue()
case types.Float32:
return am.runtime.f32eqalg.LLVMValue()
case types.Float64:
return am.runtime.f64eqalg.LLVMValue()
case types.Complex64:
return am.runtime.c64eqalg.LLVMValue()
case types.Complex128:
return am.runtime.c128eqalg.LLVMValue()
}
case *types.Struct:
// TODO
}
// TODO(axw) size-specific memequal cases
return am.runtime.memequal.LLVMValue()
}