github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/callback_windows_amd64.c (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include "runtime.h"
     6  #include "type.h"
     7  #include "typekind.h"
     8  #include "defs_GOOS_GOARCH.h"
     9  #include "os_GOOS.h"
    10  
    11  // Will keep all callbacks in a linked list, so they don't get garbage collected.
    12  typedef	struct	Callback	Callback;
    13  struct	Callback {
    14  	Callback*	link;
    15  	void*		gobody;
    16  	byte		asmbody;
    17  };
    18  
    19  typedef	struct	Callbacks	Callbacks;
    20  struct	Callbacks {
    21  	Lock;
    22  	Callback*	link;
    23  	int32		n;
    24  };
    25  
    26  static	Callbacks	cbs;
    27  
    28  // Call back from windows dll into go.
    29  byte *
    30  runtime·compilecallback(Eface fn, bool /*cleanstack*/)
    31  {
    32  	FuncType *ft;
    33  	Type *t;
    34  	int32 argsize, i, n;
    35  	byte *p;
    36  	Callback *c;
    37  
    38  	if(fn.type == nil || fn.type->kind != KindFunc)
    39  		runtime·panicstring("compilecallback: not a function");
    40  	ft = (FuncType*)fn.type;
    41  	if(ft->out.len != 1)
    42  		runtime·panicstring("compilecallback: function must have one output parameter");
    43  	if(((Type**)ft->out.array)[0]->size != sizeof(uintptr))
    44  		runtime·panicstring("compilecallback: output parameter size is wrong");
    45  	argsize = 0;
    46  	for(i=0; i<ft->in.len; i++) {
    47  		t = ((Type**)ft->in.array)[i];
    48  		if(t->size > sizeof(uintptr))
    49  			runtime·panicstring("compilecallback: input parameter size is wrong");
    50  		argsize += sizeof(uintptr);
    51  	}
    52  
    53  	// compute size of new fn.
    54  	// must match code laid out below.
    55  	n  = 2+8+1; // MOVQ fn, AX           / PUSHQ AX
    56  	n += 2+8+1; // MOVQ argsize, AX      / PUSHQ AX
    57  	n += 2+8;   // MOVQ callbackasm, AX
    58  	n += 2;     // JMP  AX
    59  
    60  	runtime·lock(&cbs);
    61  	for(c = cbs.link; c != nil; c = c->link) {
    62  		if(c->gobody == fn.data) {
    63  			runtime·unlock(&cbs);
    64  			return &c->asmbody;
    65  		}
    66  	}
    67  	if(cbs.n >= 2000)
    68  		runtime·throw("too many callback functions");
    69  	c = runtime·mal(sizeof *c + n);
    70  	c->gobody = fn.data;
    71  	c->link = cbs.link;
    72  	cbs.link = c;
    73  	cbs.n++;
    74  	runtime·unlock(&cbs);
    75  
    76  	p = &c->asmbody;
    77  
    78  	// MOVQ fn, AX
    79  	*p++ = 0x48;
    80  	*p++ = 0xb8;
    81  	*(uint64*)p = (uint64)(fn.data);
    82  	p += 8;
    83  	// PUSH AX
    84  	*p++ = 0x50;
    85  
    86  	// MOVQ argsize, AX
    87  	*p++ = 0x48;
    88  	*p++ = 0xb8;
    89  	*(uint64*)p = argsize;
    90  	p += 8;
    91  	// PUSH AX
    92  	*p++ = 0x50;
    93  
    94  	// MOVQ callbackasm, AX
    95  	*p++ = 0x48;
    96  	*p++ = 0xb8;
    97  	*(uint64*)p = (uint64)runtime·callbackasm;
    98  	p += 8;
    99  
   100  	// JMP AX
   101  	*p++ = 0xFF;
   102  	*p = 0xE0;
   103  
   104  	return &c->asmbody;
   105  }