/usr/bin/env echo ' -*-mode:sh;indent-tabs-mode:nil;tab-width:8;coding:utf-8-*-â
âvi: set net ft=sh ts=2 sts=2 sw=2 fenc=utf-8                               :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2020 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ'>/dev/null #*/
MADEDIRS=

mkdir -p libc/nt/kernel32 &&
  touch libc/nt/kernel32/boop.s &&
  rm -f libc/nt/*/*.s libc/nt/*/*.S ||
    exit

imp() {
  NAME="$1"
  ACTUAL="$2"
  DLL="$3"
  HINT="$4"
  ARITY="$5"
  if [ "$MADEDIRS" != "${MADEDIRS#*$3}" ]; then  # â sre interview question
    mkdir -p "libc/nt/$3"
    MADEDIRS="$MADEDIRS $3"
  fi
  {
    # Generate Portable Executable import data structures
    if [ "$DLL" = "ntdll" ]; then
      echo "#include \"libc/nt/ntdllimport.h\""
      echo ".ntimp	$ACTUAL,$NAME"
    else
      echo "#include \"libc/nt/codegen.h\""
      echo ".imp	$DLL,__imp_$ACTUAL,$ACTUAL,$HINT"
    fi

    # Generate System Five ABI translating thunks
    if [ -n "$NAME" ]; then
      case "$ARITY" in
        0)     thunk0 "$NAME" "$ACTUAL" "$NAME" ;;
        1)     thunk1 "$NAME" "$ACTUAL" "$NAME" ;;
        2|3|4) thunk "$NAME" "$ACTUAL" __sysv2nt "$NAME" ;;
        5|6)   thunk "$NAME" "$ACTUAL" __sysv2nt6 "$NAME" ;;
        7|8)   thunk "$NAME" "$ACTUAL" __sysv2nt8 "$NAME" ;;
        9|10)  thunk "$NAME" "$ACTUAL" __sysv2nt10 "$NAME" ;;
        11|12) thunk "$NAME" "$ACTUAL" __sysv2nt12 "$NAME" ;;
        13|14) thunk "$NAME" "$ACTUAL" __sysv2nt14 "$NAME" ;;
      esac
    fi
  } >libc/nt/$DLL/$ACTUAL.S
}

thunk() {
  printf '
	.text.windows
%s:
#ifdef __x86_64__
	push	%%rbp
	mov	%%rsp,%%rbp
	.profilable
	mov	__imp_%s(%%rip),%%rax
	jmp	%s
#elif defined(__aarch64__)
	mov	x0,#0
	ret
#endif
	.endfn	%s,globl
	.previous
' "$@"
}

thunk0() {
  printf '
	.text.windows
%s:
#ifdef __x86_64__
	push	%%rbp
	mov	%%rsp,%%rbp
	.profilable
	sub	$32,%%rsp
	call	*__imp_%s(%%rip)
	leave
#elif defined(__aarch64__)
	mov	x0,#0
#endif
	ret
	.endfn	%s,globl
	.previous
' "$@"
}

thunk1() {
  printf '
	.text.windows
%s:
#ifdef __x86_64__
	push	%%rbp
	mov	%%rsp,%%rbp
	.profilable
	mov	%%rdi,%%rcx
	sub	$32,%%rsp
	call	*__imp_%s(%%rip)
	leave
#elif defined(__aarch64__)
	mov	x0,#0
#endif
	ret
	.endfn	%s,globl
	.previous
' "$@"
}
