Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
247 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
!if "$(ARCH)" == "x64" | ||
ASMFILE=msc_x64_call | ||
!elseif "$(ARCH)" == "x86" | ||
ASMFILE=msc_x86_call | ||
!else | ||
!error set ARCH=x64|x86 | ||
!endif | ||
|
||
all : libcallex.dll | ||
@echo done | ||
|
||
libcallex.dll : libcallex.cxx | ||
cl /LD /EHsc libcallex.cxx libffi.lib | ||
libcallex.dll : libcallex.cxx $(ASMFILE).obj | ||
cl /LD /EHsc libcallex.cxx $(ASMFILE).obj | ||
|
||
msc_x64_call.obj : msc_x64_call.asm | ||
ml64 /c msc_x64_call.asm | ||
|
||
msc_x86_call.obj : msc_x86_call.asm | ||
ml /c msc_x86_call.asm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
|
||
ifeq ($(ARCH), x64) | ||
ASMFILE=mingw_x64_call.s | ||
else | ||
ifeq ($(ARCH), x86) | ||
ASMFILE=mingw_x86_call.s | ||
else | ||
$(error set ARCH=x64|x86) | ||
endif | ||
endif | ||
|
||
all : libcallex.dll | ||
@echo done | ||
|
||
libcallex.dll : libcallex.cxx | ||
g++ -static-libgcc -static-libstdc++ -shared -o libcallex.dll libcallex.cxx -lffi | ||
libcallex.dll : libcallex.cxx $(ASMFILE) | ||
g++ -static -static-libgcc -static-libstdc++ -shared -o libcallex.dll libcallex.cxx $(ASMFILE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.text | ||
.global mingw_x64_call | ||
.def mingw_x64_call | ||
# intptr_t mingw_x64_call(FUNCTION p, long narg, INTPTR_T* args) | ||
args$ = 32 | ||
narg$ = 24 | ||
p$ = 16 | ||
mingw_x64_call: | ||
.cfi_startproc | ||
pushq %rbp | ||
movq %rsp, %rbp | ||
movq %rcx, p$(%rbp) | ||
movq %rdx, narg$(%rbp) | ||
movq %r8, args$(%rbp) | ||
# stacksize is at lease 32 byte, aligned to 16 byte | ||
# cutting corners with (4 + narg * 2) * 8 | ||
leaq 4(,%rdx,2), %rdx | ||
leaq (,%rdx,8), %rdx | ||
sub %rdx, %rsp | ||
# while narg >= 5: | ||
# narg-- | ||
# rsp[narg] = args[narg] | ||
# if narg > 3: | ||
# r9 = args[3] | ||
# if narg > 2: | ||
# r8 = args[2] | ||
# if narg > 1: | ||
# rdx = args[1] | ||
# if narg > 0: | ||
# rcx = args[0] | ||
movq narg$(%rbp), %rcx | ||
argN: | ||
cmpq $5, %rcx | ||
jl arg4 | ||
dec %rcx | ||
movq args$(%rbp), %rax | ||
movq (%rax,%rcx,8), %rax | ||
movq %rax, (%rsp,%rcx,8) | ||
jmp argN | ||
arg4: | ||
cmpq $4, %rcx | ||
jl arg3 | ||
movq args$(%rbp), %rax | ||
movq 24(%rax), %rax | ||
movq %rax, %r9 | ||
arg3: | ||
cmpq $3, %rcx | ||
jl arg2 | ||
movq args$(%rbp), %rax | ||
movq 16(%rax), %rax | ||
movq %rax, %r8 | ||
arg2: | ||
cmpq $2, %rcx | ||
jl arg1 | ||
movq args$(%rbp), %rax | ||
movq 8(%rax), %rax | ||
movq %rax, %rdx | ||
arg1: | ||
cmpq $1, %rcx | ||
jl docall | ||
movq args$(%rbp), %rax | ||
movq (%rax), %rax | ||
movq %rax, %rcx | ||
docall: | ||
call *p$(%rbp) | ||
movq %rbp, %rsp | ||
popq %rbp | ||
ret | ||
.cfi_endproc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.text | ||
.global _mingw_x86_call | ||
.def _mingw_x86_call | ||
# intptr_t mingw_x86_call(FUNCTION p, long narg, INTPTR_T* args) | ||
args$ = 16 | ||
narg$ = 12 | ||
p$ = 8 | ||
_mingw_x86_call: | ||
.cfi_startproc | ||
pushl %ebp | ||
movl %esp, %ebp | ||
# while narg > 0: | ||
# push args[--narg] | ||
movl narg$(%ebp), %ecx | ||
argN: | ||
cmpl $0, %ecx | ||
jle docall | ||
dec %ecx | ||
movl args$(%ebp), %eax | ||
pushl (%eax,%ecx,4) | ||
jmp argN | ||
docall: | ||
call *p$(%ebp) | ||
movl %ebp, %esp | ||
popl %ebp | ||
ret | ||
.cfi_endproc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
PUBLIC msc_x64_call | ||
|
||
_TEXT SEGMENT | ||
; intptr_t msc_x64_call(FUNCTION p, long narg, INTPTR_T* args) | ||
args$ = 32 | ||
narg$ = 24 | ||
p$ = 16 | ||
msc_x64_call proc | ||
push rbp | ||
mov rbp, rsp | ||
mov p$[rbp], rcx | ||
mov narg$[rbp], rdx | ||
mov args$[rbp], r8 | ||
; stacksize is at lease 32 byte, aligned to 16 byte | ||
; cutting corners with (4 + narg * 2) * 8 | ||
lea rdx, [rdx*2+4] | ||
lea rdx, [rdx*8] | ||
sub rsp, rdx | ||
; while narg >= 5: | ||
; narg-- | ||
; rsp[narg] = args[narg] | ||
; if narg > 3: | ||
; r9 = args[3] | ||
; if narg > 2: | ||
; r8 = args[2] | ||
; if narg > 1: | ||
; rdx = args[1] | ||
; if narg > 0: | ||
; rcx = args[0] | ||
mov rcx, narg$[rbp] | ||
argN: | ||
cmp rcx, 5 | ||
jl arg4 | ||
dec rcx | ||
mov rax, args$[rbp] | ||
mov rax, [rax+rcx*8] | ||
mov [rsp+rcx*8], rax | ||
jmp argN | ||
arg4: | ||
cmp rcx, 4 | ||
jl arg3 | ||
mov rax, args$[rbp] | ||
mov rax, [rax+24] | ||
mov r9, rax | ||
arg3: | ||
cmp rcx, 3 | ||
jl arg2 | ||
mov rax, args$[rbp] | ||
mov rax, [rax+16] | ||
mov r8, rax | ||
arg2: | ||
cmp rcx, 2 | ||
jl arg1 | ||
mov rax, args$[rbp] | ||
mov rax, [rax+8] | ||
mov rdx, rax | ||
arg1: | ||
cmp rcx, 1 | ||
jl docall | ||
mov rax, args$[rbp] | ||
mov rax, [rax] | ||
mov rcx, rax | ||
docall: | ||
call qword ptr p$[rbp] | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
msc_x64_call endp | ||
_TEXT ENDS | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.686P | ||
.model flat | ||
|
||
; why need underscore? | ||
PUBLIC _msc_x86_call | ||
|
||
_TEXT SEGMENT | ||
; intptr_t msc_x86_call(FUNCTION p, long narg, INTPTR_T* args) | ||
args$ = 16 | ||
narg$ = 12 | ||
p$ = 8 | ||
_msc_x86_call proc | ||
push ebp | ||
mov ebp, esp | ||
; while narg > 0: | ||
; push args[--narg] | ||
mov ecx, narg$[ebp] | ||
argN: | ||
cmp ecx, 0 | ||
jle docall | ||
dec ecx | ||
mov eax, args$[ebp] | ||
push [eax+ecx*4] | ||
jmp argN | ||
docall: | ||
call dword ptr p$[ebp] | ||
mov esp, ebp | ||
pop ebp | ||
ret | ||
_msc_x86_call endp | ||
_TEXT ENDS | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters