summary refs log tree commit diff stats
path: root/lib/arch/arch.nim
blob: 0b3df3d3c558c27abeeb37fd0655146b2278b8e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
#
#            Nim's Runtime Library
#        (c) Copyright 2015 Rokas Kupstys
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#
# Architecture-specific optimizations and features.
# arch.nim can be imported by only a subset of the
# architectures supported by Nim.

when defined(windows):
  const
    ABI* = "ms"
elif defined(unix):
  const
    ABI* = "unix"
else:
  {.error: "Unsupported ABI".}

when defined(amd64):
  when defined(unix):
    # unix (sysv) ABI
    type
      JmpBufReg* {.pure.} = enum
        BX, BP, R12, R13, R14, R15, SP, IP, TOTAL
  elif defined(windows):
    # ms ABI
    type
      JmpBufReg* {.pure.} = enum
        BX, BP, R12, R13, R14, R15, SP, IP, SI, DI, TOTAL
  type
    Reg* {.pure.} = enum
      AX, BX, CX, DX, SI, DI, BP, SP, IP, R8, R9, R10, R11, R12, R13, R14, R15, TOTAL

elif defined(i386) or defined(nimdoc):
    # identical fastcall calling convention on all x86 OS
    type
      JmpBufReg* {.pure.} = enum
        BX, SI, DI, BP, SP, IP, TOTAL

      Reg* {.pure.} = enum
        AX, BX, CX, BP, SP, DI, SI, TOTAL

else:
  {.error: "Unsupported architecture".}

{.compile: "./" & ABI & "_" & hostCPU & ".asm"}

type
  JmpBuf* = array[JmpBufReg.TOTAL, pointer]
  Registers* = array[Reg.TOTAL, pointer]


proc getRegisters*(ctx: var Registers) {.importc: "narch_$1", fastcall.}

proc setjmp*(ctx: var JmpBuf): int {.importc: "narch_$1", fastcall.}
proc longjmp*(ctx: JmpBuf, ret=1) {.importc: "narch_$1", fastcall.}

proc coroSwitchStack*(sp: pointer) {.importc: "narch_$1", fastcall.}
proc coroRestoreStack*() {.importc: "narch_$1", fastcall.}