-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathvariadic_amd64.go
84 lines (72 loc) · 1.78 KB
/
variadic_amd64.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
// Copyright 2012 Mikkel Krautz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package variadic
/*
#include <stdlib.h>
#include <dlfcn.h>
void *VariadicCall(void *ctx);
float VariadicCallFloat(void *ctx);
double VariadicCallDouble(void *ctx);
void *LookupSymAddr(char *str) {
return dlsym(RTLD_DEFAULT, str);
}
*/
import "C"
import (
"unsafe"
)
const (
RDI = iota
RDX
RCX
R8
R9
XMM0
XMM1
XMM2
XMM3
XMM4
XMM5
XMM6
XMM7
)
type FunctionCall struct {
Words [14]uintptr
NumFloat int64
NumMemory int64
Memory uintptr
addr unsafe.Pointer
}
// NewFunctionCall creates a new FunctionCall than can be
// used to call the C function named by the name parameter.
func NewFunctionCall(name string) *FunctionCall {
fc := new(FunctionCall)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
fc.addr = C.LookupSymAddr(cname)
return fc
}
// NewFunctionCallAddr creates a new FunctionCall that can be
// used to cll the C function at the address given by the addr
// parameter.
func NewFunctionCallAddr(addr unsafe.Pointer) *FunctionCall {
fc := new(FunctionCall)
fc.addr = addr
return fc
}
// Call calls the FunctionCall's underlying function, returning
// its return value as an uintptr.
func (f *FunctionCall) Call() uintptr {
return uintptr(C.VariadicCall(unsafe.Pointer(f)))
}
// CallFloat32 calls the FunctionCall's underlying function, returning
// its return value as a float32.
func (f *FunctionCall) CallFloat32() float32 {
return float32(C.VariadicCallFloat(unsafe.Pointer(f)))
}
// CallFloat64 calls the FunctionCall's underlying function, returning
// its return value as float64.
func (f *FunctionCall) CallFloat64() float64 {
return float64(C.VariadicCallDouble(unsafe.Pointer(f)))
}