2011-08-30 06:02:02 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "type.h"
|
2012-06-06 17:37:05 -06:00
|
|
|
#include "typekind.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "defs_GOOS_GOARCH.h"
|
|
|
|
#include "os_GOOS.h"
|
2013-06-24 01:17:45 -06:00
|
|
|
#include "zasm_GOOS_GOARCH.h"
|
2011-08-30 06:02:02 -06:00
|
|
|
|
|
|
|
typedef struct Callbacks Callbacks;
|
|
|
|
struct Callbacks {
|
|
|
|
Lock;
|
2013-06-24 01:17:45 -06:00
|
|
|
WinCallbackContext* ctxt[cb_max];
|
|
|
|
int32 n;
|
2011-08-30 06:02:02 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static Callbacks cbs;
|
|
|
|
|
2013-06-24 01:17:45 -06:00
|
|
|
WinCallbackContext** runtime·cbctxts; // to simplify access to cbs.ctxt in sys_windows_*.s
|
|
|
|
|
2011-08-30 06:02:02 -06:00
|
|
|
// Call back from windows dll into go.
|
|
|
|
byte *
|
|
|
|
runtime·compilecallback(Eface fn, bool cleanstack)
|
|
|
|
{
|
|
|
|
FuncType *ft;
|
|
|
|
Type *t;
|
|
|
|
int32 argsize, i, n;
|
2013-06-24 01:17:45 -06:00
|
|
|
WinCallbackContext *c;
|
2011-08-30 06:02:02 -06:00
|
|
|
|
|
|
|
if(fn.type == nil || fn.type->kind != KindFunc)
|
|
|
|
runtime·panicstring("compilecallback: not a function");
|
|
|
|
ft = (FuncType*)fn.type;
|
|
|
|
if(ft->out.len != 1)
|
|
|
|
runtime·panicstring("compilecallback: function must have one output parameter");
|
|
|
|
if(((Type**)ft->out.array)[0]->size != sizeof(uintptr))
|
|
|
|
runtime·panicstring("compilecallback: output parameter size is wrong");
|
|
|
|
argsize = 0;
|
|
|
|
for(i=0; i<ft->in.len; i++) {
|
|
|
|
t = ((Type**)ft->in.array)[i];
|
|
|
|
if(t->size > sizeof(uintptr))
|
|
|
|
runtime·panicstring("compilecallback: input parameter size is wrong");
|
|
|
|
argsize += sizeof(uintptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime·lock(&cbs);
|
2013-06-24 01:17:45 -06:00
|
|
|
if(runtime·cbctxts == nil)
|
|
|
|
runtime·cbctxts = &(cbs.ctxt[0]);
|
|
|
|
n = cbs.n;
|
|
|
|
for(i=0; i<n; i++) {
|
|
|
|
if(cbs.ctxt[i]->gobody == fn.data) {
|
2011-08-30 06:02:02 -06:00
|
|
|
runtime·unlock(&cbs);
|
2013-06-24 01:17:45 -06:00
|
|
|
// runtime·callbackasm is just a series of CALL instructions
|
|
|
|
// (each is 5 bytes long), and we want callback to arrive at
|
|
|
|
// correspondent call instruction instead of start of
|
|
|
|
// runtime·callbackasm.
|
|
|
|
return (byte*)runtime·callbackasm + i * 5;
|
2011-08-30 06:02:02 -06:00
|
|
|
}
|
|
|
|
}
|
2013-06-24 01:17:45 -06:00
|
|
|
if(n >= cb_max)
|
2011-08-30 06:02:02 -06:00
|
|
|
runtime·throw("too many callback functions");
|
2013-06-24 01:17:45 -06:00
|
|
|
c = runtime·mal(sizeof *c);
|
2011-08-30 06:02:02 -06:00
|
|
|
c->gobody = fn.data;
|
2013-06-24 01:17:45 -06:00
|
|
|
c->argsize = argsize;
|
|
|
|
if(cleanstack && argsize!=0)
|
|
|
|
c->restorestack = argsize;
|
|
|
|
else
|
|
|
|
c->restorestack = 0;
|
|
|
|
cbs.ctxt[n] = c;
|
2011-08-30 06:02:02 -06:00
|
|
|
cbs.n++;
|
|
|
|
runtime·unlock(&cbs);
|
|
|
|
|
2013-06-24 01:17:45 -06:00
|
|
|
// as before
|
|
|
|
return (byte*)runtime·callbackasm + n * 5;
|
2011-08-30 06:02:02 -06:00
|
|
|
}
|