This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmake_asm_defines.py
87 lines (79 loc) · 2.38 KB
/
make_asm_defines.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Parse a compiler-generated assembly so that we can see what
kinds of assembly syntax the compiler being used uses, so that certain
assembly macros can be customized to those compilers.
Author: Peter Goodman ([email protected])
Copyright: Copyright 2012-2013 Peter Goodman, all rights reserved.
"""
PREFIX_WITH_UNDERSCORE = False
USE_GLOBL = False
USE_TYPE = False
USE_AT_FUNCTION = False
START_FILE = ".text"
FUNC_ALIGNMENT = ".align 16"
# parse the assembly file looking for little bits of info that should guide how
# we should define some assembly helper macros
with open("scripts/static/asm.S") as lines:
for line in lines:
if "foo" in line:
if "_foo" in line:
PREFIX_WITH_UNDERSCORE = True
if ".globl" in line:
USE_GLOBL = True
if ".type" in line:
USE_TYPE = True
if "@function" in line:
USE_AT_FUNCTION = True
if ".text" in line:
START_FILE = line.strip("\r\n \t")
if ".align" in line:
FUNC_ALIGNMENT = line.strip("\r\n \t")
if "ret" in line:
break
# generate the assembly helper macros
with open("granary/x86/asm_defines.asm", "w") as f:
def W(*args):
f.write("".join(map(str, args)) + "\n")
# function type
type_def = ""
if USE_TYPE:
type_def = ".type SYMBOL(x), "
if USE_AT_FUNCTION:
type_def += "@function"
else:
type_def += "%function"
type_def += "@N@\\\n"
# alignment
align_def = FUNC_ALIGNMENT + ' @N@\\\n'
# export
global_def = ""
if USE_GLOBL:
global_def = ".globl "
else:
global_def = ".global "
global_def += "SYMBOL(x) @N@\\\n"
W('#define START_FILE ', START_FILE)
W('#define END_FILE')
W('#define SYMBOL(x) ', (PREFIX_WITH_UNDERSCORE and "_ ## " or ""), 'x')
W('#define DECLARE_FUNC(x) \\\n', align_def, global_def, type_def, " ")
W('#define GLOBAL_LABEL(x) SYMBOL(x)')
W('#define END_FUNC(x)')
W('#define HEX(v) $0x ## v')
W('#define PUSHF pushf')
W('#define POPF popf')
W('#define REG_XAX rax')
W('#define REG_XBX rbx')
W('#define REG_XCX rcx')
W('#define REG_XDX rdx')
W('#define REG_XSI rsi')
W('#define REG_XDI rdi')
W('#define REG_XBP rbp')
W('#define REG_XSP rsp')
W('#define ARG1 rdi')
W('#define ARG2 rsi')
W('#define ARG3 rdx')
W('#define ARG4 rcx')
W('#define ARG5 r8')
W('#define ARG5_NORETADDR ARG5')
W('#define ARG6 r9')
W('#define ARG6_NORETADDR ARG6')
W('#define ARG7 QWORD [esp]')