This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mywin.inc
244 lines (228 loc) · 4.9 KB
/
mywin.inc
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
include 'win64wxp.inc'
include 'encoding\utf8.inc'
include 'macro\collect.inc'
include 'macro\rstrings.inc'
dummy RSTRING ; zero id
S_FALSE := 1 ; because TRUE/FALSE is so 1900
S_OK := 0
; macros are written to process 64-bit register inputs. yet, sometimes conversion to 32-bit part is convinent.
iterate <reg,rlow>, ax,al, cx,cl, dx,dl, bx,bl, sp,spl, bp,bpl, si,sil, di,dil
define reg32.r#reg? e#reg
;define reg16low.r#reg? reg
;define reg8low.r#reg? rlow
;define reg8hi.r#reg? rlow
end iterate
repeat 8, i:8
define reg32.r#i? r#i#d
;define reg16low.r#i? r#i#w
;define reg8low.r#i? r#i#l
;define reg8hi.r#i? r#i#h
end repeat
; make DLL functions first class:
macro import? name,funs&
local _funs
iterate <F,S>,funs
if % = 1
_funs equ _x_#F,S
else
_funs reequ _funs,_x_#F,S
end if
macro F line&
win64abi _x_#F,line
end macro
end iterate
match any,_funs
import name,any
end match
end macro
; TODO: doing something like "ExitProcess,0" produces a strange error - need to catch that to make the error very verbose?
macro win64abi api*,line&
iterate P,line
repeat 1,I:%%-%+1
indx I
win64abi_parm I,P
end repeat
end iterate
call [api]
end macro
macro call? function,line&
iterate P,line
repeat 1,I:%%-%+1
indx I
win64abi_parm I,P
end repeat
end iterate
call function
end macro
; don't try to overload x86 instructions, irregular types should be built-up prior (i.e. instead of supporting ( SIGNED {size}? [] --> MOVSX ) just do the MOVSX)
macro win64abi_parm index*,arg&
local llabl,word_id,dest
iterate reg, rcx,rdx,r8,r9,rax,r10,r11
if index < 5
indx index
else
indx 5 + ((index-4) mod (%%-4))
end if
match [any],arg
if index < 5
mov reg,[any]
else
push [any]
pop [.P#index]
end if
else match =ADDR? any,arg
if index < 5
lea reg,[any]
else
lea reg,[any]
mov [.P#index],reg
end if
else match =CONST? any,arg
; TODO: batch into granulatity group
collect _CONST_
label llabl
any
end collect
if index < 5
lea reg,[llabl]
else
lea reg,[llabl]
mov [.P#index],reg
end if
else match =_T any,arg
collect _CONST_
align sizeof.TCHAR
label llabl
TCHAR any,0
end collect
if index < 5
lea reg,[llabl]
else
lea reg,[llabl]
mov [.P#index],reg
end if
; TODO: not implemented
else match =_R any,arg
word_id RSTRING any
if index < 5
mov reg32.reg,word_id
else
mov [.P#index],word_id
end if
else
x86.parse_operand@src arg
if @src.type = 'reg'
if index < 5
match =reg?,arg
else
; TODO: reg rax -> XCHG
mov reg,arg
end match
else
mov [.P#index],arg
end if
else if @src.type = 'imm'
if arg = 0
if index < 5
xor reg32.reg,reg32.reg
else
and [.P#index],0
end if
else if arg = -1
if index < 5
or reg,-1
else
or [.P#index],-1
end if
else if -$81 < arg & arg < $80
push arg
if index < 5
pop reg
else
pop [.P#index]
end if
else if -1 < arg & arg < $10000_0000 & index < 5
mov reg32.reg,arg
else if -$8000_0001 < arg & arg < $8000_0000
if index < 5
mov reg,arg
else
mov [.P#index],arg
end if
else ; only use proxy in extreme case
mov reg,arg
if index > 4
mov [.P#index],reg
end if
end if
end if
end match
break
end iterate
end macro
; this version factors out variable terms in address space, and assumes base is infinitely alignable (use wisely)
macro _align? value*,something:db ?
while ($-$$) and (value-1)
something
end while
end macro
macro lea? line&
; NOTE: spaces needed for possible line breaks! Use most general configuration!
match any =, =< values =>,line
local name
match =_T V,values
collect _CONST_
align sizeof.TCHAR
label name
TCHAR V,0
end collect
else match =_A V,values
collect _CONST_
name db V,0
end collect
else match =_W V,values
collect _CONST_
name du V,0
end collect
else
collect _CONST_
label name
values
end collect
end match
lea any,[name]
else
lea line
end match
end macro
macro mov? line&
; NOTE: spaces needed for possible line breaks! Use most general configuration!
match any =, =< values =>,line
local word_id
match =_R V,values
word_id RSTRING V
else
err ; unsupported type
end match
if word_id < $80
push word_id
pop any
else
mov any,word_id
end if
else
mov line
end match
end macro
section '.text' code readable executable
postpone
section '.reloc' fixups data readable discardable
if $=$$
dd 0,8 ; if there are no fixups, generate dummy entry
display 10,9,"No relocations needed."
else
repeat 1,D:PE.NUMBER_OF_RELOCATIONS
display 10,9,`D,' relocations needed.'
end repeat
end if
end postpone