Skip to content

Commit

Permalink
Add thiscall calling convention, mostly for hooking purpose (#14466)
Browse files Browse the repository at this point in the history
* Add thiscall calling convention, mostly for hooking purpose
* add changelog and documentation
  • Loading branch information
ba0f3 authored May 27, 2020
1 parent cc65ae6 commit 00fa7a5
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ proc mydiv(a, b): int {.raises: [].} =
The reason for this is that `DivByZeroDefect` inherits from `Defect` and
with `--panics:on` `Defects` become unrecoverable errors.

- Added `thiscall` calling convention as specified by Microsoft, mostly for hooking purpose

## Compiler changes

- Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`.
Expand Down
3 changes: 2 additions & 1 deletion compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type
ccInline, # proc should be inlined
ccNoInline, # proc should not be inlined
ccFastCall, # fastcall (pass parameters in registers)
ccThisCall, # thiscall (parameters are pushed right-to-left)
ccClosure, # proc has a closure
ccNoConvention # needed for generating proper C procs sometimes

const
CallingConvToStr*: array[TCallingConvention, string] = ["", "stdcall",
"cdecl", "safecall", "syscall", "inline", "noinline", "fastcall",
"cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "thiscall",
"closure", "noconv"]

type
Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ const
"N_STDCALL", "N_CDECL", "N_SAFECALL",
"N_SYSCALL", # this is probably not correct for all platforms,
# but one can #define it to what one wants
"N_INLINE", "N_NOINLINE", "N_FASTCALL", "N_CLOSURE", "N_NOCONV"]
"N_INLINE", "N_NOINLINE", "N_FASTCALL", "N_THISCALL", "N_CLOSURE", "N_NOCONV"]

proc cacheGetType(tab: TypeCache; sig: SigHash): Rope =
# returns nil if we need to declare this type
Expand Down
4 changes: 2 additions & 2 deletions compiler/wordrecg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type
wLineDir, wStackTrace, wLineTrace, wLink, wCompile,
wLinksys, wDeprecated, wVarargs, wCallconv, wDebugger,
wNimcall, wStdcall, wCdecl, wSafecall, wSyscall, wInline, wNoInline,
wFastcall, wClosure, wNoconv, wOn, wOff, wChecks, wRangeChecks,
wFastcall, wThiscall, wClosure, wNoconv, wOn, wOff, wChecks, wRangeChecks,
wBoundChecks, wOverflowChecks, wNilChecks,
wFloatChecks, wNanChecks, wInfChecks, wStyleChecks, wStaticBoundchecks,
wNonReloadable, wExecuteOnReload,
Expand Down Expand Up @@ -139,7 +139,7 @@ const
"push", "pop", "define", "undef", "linedir", "stacktrace", "linetrace",
"link", "compile", "linksys", "deprecated", "varargs",
"callconv", "debugger", "nimcall", "stdcall",
"cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "closure",
"cdecl", "safecall", "syscall", "inline", "noinline", "fastcall", "thiscall", "closure",
"noconv", "on", "off", "checks", "rangechecks", "boundchecks",
"overflowchecks", "nilchecks",
"floatchecks", "nanchecks", "infchecks", "stylechecks", "staticboundchecks",
Expand Down
4 changes: 4 additions & 0 deletions doc/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,10 @@ Nim supports these `calling conventions`:idx:\:
Fastcall means different things to different C compilers. One gets whatever
the C ``__fastcall`` means.

`thiscall`:idx:
This is thiscall calling convention as specified by Microsoft, used on C++
class member functions on the x86 architecture

`syscall`:idx:
The syscall convention is the same as ``__syscall`` in C. It is used for
interrupts.
Expand Down
2 changes: 2 additions & 0 deletions lib/nimbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ __AVR__
# define N_STDCALL(rettype, name) rettype __stdcall name
# define N_SYSCALL(rettype, name) rettype __syscall name
# define N_FASTCALL(rettype, name) rettype __fastcall name
# define N_THISCALL(rettype, name) rettype __thiscall name
# define N_SAFECALL(rettype, name) rettype __stdcall name
/* function pointers with calling convention: */
# define N_CDECL_PTR(rettype, name) rettype (__cdecl *name)
# define N_STDCALL_PTR(rettype, name) rettype (__stdcall *name)
# define N_SYSCALL_PTR(rettype, name) rettype (__syscall *name)
# define N_FASTCALL_PTR(rettype, name) rettype (__fastcall *name)
# define N_THISCALL_PTR(rettype, name) rettype (__thiscall *name)
# define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)

# ifdef __cplusplus
Expand Down

0 comments on commit 00fa7a5

Please sign in to comment.