summaryrefslogtreecommitdiff
path: root/misc/proc32.ash
blob: f513b733d885bb955db9afca1cc5eb2d65257c3c (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
;--------=========xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=========--------
;
;   Copyright (C) 1999 by Andrew Zabolotny
;   Miscelaneous NASM macros that makes use of new preprocessor features
; 
;   This library is free software; you can redistribute it and/or
;   modify it under the terms of the GNU Library General Public
;   License as published by the Free Software Foundation; either
;   version 2 of the License, or (at your option) any later version.
; 
;   This library is distributed in the hope that it will be useful,
;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;   Library General Public License for more details.
; 
;   You should have received a copy of the GNU Library General Public
;   License along with this library; if not, write to the Free
;   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;
;--------=========xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=========--------

;   The macros in this file provides support for writing 32-bit C-callable
;   NASM routines. For a short description of every macros see the
;   corresponding comment before every one. Simple usage example:
;
;	proc	sin,1
;		targ	%$angle
;		fld	%$angle
;		fsin
;	endproc	sin

%ifndef __PROC32_ASH__
%define __PROC32_ASH__

[WARNING -macro-selfref]

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Mangle a name to be compatible with the C compiler
; Arguments:
;   The name
; Example:
;		cname (my_func)
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%ifdef EXTERNC_UNDERSCORE
		%define	cname(x) _ %+ x
%else
		%define	cname(x) x
%endif

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Import an external C procedure definition
; Arguments:
;   The name of external C procedure
; Example:
;		cextern	printf
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		cextern	1
		%xdefine %1 cname(%1)
	%ifidni __OUTPUT_FORMAT__,obj
		extern	%1:wrt FLAT
	%else
		extern	%1
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Export an C procedure definition
; Arguments:
;   The name of C procedure
; Example:
;		cglobal	my_printf
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		cglobal	1
		%xdefine %1 cname(%1)
		global	%1
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Misc macros to deal with PIC shared libraries
; Comment:
;   Note that we have a different syntax for working with and without
;   PIC shared libraries. In a PIC environment we should load first
;   the address of the variable into a register and then work through
;   that address, i.e: mov eax,myvar; mov [eax],1
;   In a non-PIC environment we should directly write: mov myvar,1
; Example:
;		extvar	myvar
;		GetGOT
;	%ifdef PIC
;		mov	ebx,myvar	; get offset of myvar into ebx
;	%else
;		lea	ebx,myvar
;	%endif
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%ifdef PIC
		cextern	_GLOBAL_OFFSET_TABLE_
	%macro	GetGOT	0
		%ifdef .$proc.stkofs
			%assign .$proc.stkofs .$proc.stkofs+4
		%endif
		call	%$Get_GOT
	%$Get_GOT:
		pop	ebx
		add	ebx,_GLOBAL_OFFSET_TABLE_ + $$ - %$Get_GOT wrt ..gotpc
	%endmacro
	%macro	extvar	1
		cextern	%1
		%xdefine %1 [ebx+%1 wrt ..got]
	%endmacro
%else
	%define	GetGOT
	%macro	extvar	1
		cextern	%1
	%endmacro
%endif

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Begin a procedure definition
;   For performance reasons we don't use stack frame pointer EBP,
;   instead we're using the [esp+xx] addressing. Because of this
;   you should be careful when you work with stack pointer.
;   The push/pop instructions are macros that are defined to
;   deal correctly with these issues.
; Arguments:
;   First argument - the procedure name
;   Second optional argument - the number of bytes for local variables
;   The following arguments could specify the registers that should be
;   pushed at beginning of procedure and popped before exiting
; Example:
;   proc	MyTestProc
;   proc	MyTestProc,4,ebx,esi,edi
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		proc	1-3+ 0
		cglobal	%1
		%push	%1
		align	16
%1:
		%xdefine %$proc.name %1
	; total size of local arguments
		%assign %$proc.locsize (%2+3) & 0xFFFC
	; offset from esp to argument
		%assign	%$proc.argofs 4+%$proc.locsize
	; additional offset to args (tracks push/pops)
		%assign	.$proc.stkofs 0
	; offset from esp to local arguments
		%assign %$proc.locofs 0
	; Now push the registers that we should save
		%define %$proc.save %3
	%if %$proc.locsize != 0
		sub	esp,%$proc.locsize
	%endif
		push	%$proc.save
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Declare an argument passed on stack
;   This macro defines two additional macros:
;     first (with the name given by first argument) - [esp+xx]
;     second (with a underscore appended to first argument) - esp+xx
; Arguments:
;   First argument defines the procedure argument name
;   Second optional parameter defines the size of the argument
;   Default value is 4 (a double word)
; Example:
;		arg	.my_float
;		arg	.my_double,8
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		arg	1-2 4
	%ifndef %$proc.argofs
		%error	"`arg' not in a proc context"
	%else
	; Trick: temporary undefine .$proc.stkofs so that it won't be expanded
		%assign	%%. .$proc.stkofs
		%undef .$proc.stkofs
		%xdefine %{1}_ esp+%$proc.argofs+.$proc.stkofs
		%xdefine %1 [esp+%$proc.argofs+.$proc.stkofs]
		%assign .$proc.stkofs %%.
		%assign %$proc.argofs %2+%$proc.argofs
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Declare an local variable
;     first (with the name given by first argument) - [esp+xx]
;     second (with  a slash prefixing the first argument) - esp+xx
; Arguments:
;   First argument defines the procedure argument name
;   Second optional parameter defines the size of the argument
;   Default value is 4 (a double word)
; Example:
;		loc	.int_value
;		loc	.double_value,8
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		loc	1-2 4
	%ifndef %$proc.locofs
		%error	"`loc' not in a proc context"
	%elif %$proc.locofs + %2 > %$proc.locsize
		%error	"local stack space exceeded"
	%else
		%assign	%%. .$proc.stkofs
		%undef .$proc.stkofs
		%xdefine %{1}_ esp+%$proc.locofs+.$proc.stkofs
		%xdefine %1 [esp+%$proc.locofs+.$proc.stkofs]
		%assign .$proc.stkofs %%.
		%assign %$proc.locofs %$proc.locofs+%2
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Get the type of given size into context-local variable %$type
; Arguments:
;   Size of type we want (1,2,4,8 or 10)
; Example:
;		type	4	; gives "dword"
;		type	10	; gives "tword"
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		type	1
	%if %1 = 1
		%define	%$type byte
	%elif %1 = 2
		%define	%$type word
	%elif %1 = 4
		%define	%$type dword
	%elif %1 = 8
		%define	%$type qword
	%elif %1 = 10
		%define	%$type tword
	%else
		%define %$. %1
		%error "unknown type for argument size %$."
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Same as `arg' but prepends "word", "dword" etc (typed arg)
;     first (with the name given by first argument) - dword [esp+xx]
;     second (with  a slash prefixing the first argument) - esp+xx
; Arguments:
;   Same as for `arg'
; Example:
;		targ	.my_float	; .my_float is now "dword [esp+xxx]"
;		targ	.my_double,8	; .my_double is now "qword [esp+xxx]"
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		targ	1-2 4
	%ifndef %$proc.argofs
		%error	"`targ' not in a proc context"
	%else
		arg	%1,%2
		type	%2
		%assign	%%. .$proc.stkofs
		%undef .$proc.stkofs
		%xdefine %1 %$type %1
		%assign .$proc.stkofs %%.
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Same as `loc' but prepends "word", "dword" etc (typed loc)
;     first (with the name given by first argument) - dword [esp+xx]
;     second (with  a slash prefixing the first argument) - esp+xx
; Arguments:
;   Same as for `loc'
; Example:
;		tloc	int_value
;		tloc	double_value,8
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		tloc	1-2 4
	%ifndef %$proc.locofs
		%error	"`tloc' not in a proc context"
	%else
		loc	%1,%2
		type	%2
		%assign	%%. .$proc.stkofs
		%undef .$proc.stkofs
		%xdefine %1 %$type %1
		%assign .$proc.stkofs %%.
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Finish a procedure
;   Gives an error if proc/endproc pairs mismatch
;   Defines an label called __end_(procedure name)
;   which is useful for calculating function size
; Arguments:
;   (optional) The name of procedure
; Example:
;   endproc	MyTestProc
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%push	tmp	; trick: define a dummy context to avoid error in next line
%macro		endproc	0-1 %$proc.name
	%ifndef %$proc.argofs
		%error "`endproc' not in a proc context"
	%elifnidn %$proc.name,%1
		%define %$. %1
		%error "endproc names mismatch: expected `%$proc.name'"
		%error "but got `%$.' instead"
	%elif %$proc.locofs < %$proc.locsize
		%error	"unused local space declared (used %$proc.locofs, requested %$proc.locsize)"
	%else
%$exit:
	; Now pop the registers that we should restore on exit
		pop	%$proc.save
		%if %$proc.locsize != 0
		add	esp,%$proc.locsize
		%endif
		ret
__end_%1:
		%pop
	%endif
%endmacro
%pop

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   A replacement for "push" for use within procedures
; Arguments:
;   any number of registers which will be push'ed successively
; Example:
;		push	eax,ebx,ecx,edx
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		push	0-*
; dummy comment to avoid problems with "push" on the same line with a label
	%rep	%0
		push	%1
		%rotate	1
		%assign .$proc.stkofs .$proc.stkofs+4
	%endrep
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   A replacement for "pop" for use within procedures
; Arguments:
;   any number of registers which will be pop'ed in reverse order
; Example:
;		pop	eax,ebx,ecx,edx
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		pop	0-*
; dummy comment to avoid problems with "pop" on the same line with a label
	%rep	%0
		%rotate	-1
		pop	%1
		%assign .$proc.stkofs .$proc.stkofs-4
	%endrep
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Replacements for "pushfd" and "popfd" that takes care of esp
; Example:
;		pushfd
;		popfd
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		pushfd	0
		pushfd
		%assign .$proc.stkofs .$proc.stkofs+4
%endmacro
%macro		popfd	0
		popfd
		%assign .$proc.stkofs .$proc.stkofs-4
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Exit from current procedure (optionally on given condition)
; Arguments:
;   Either none or a condition code
; Example:
;		exit
;		exit	nz
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		exit	0-1 mp
		j%1	near %$exit
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   start an conditional branch
; Arguments:
;   A condition code
;   second (optional) argument - "short" (by default - "near")
; Example:
;		if	nz
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		if	1-2 near
; dummy comment to avoid problems with "if" on the same line with a label
		%push	if
		j%-1	%2 %$elseif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   define the "else" branch of a conditional statement
; Arguments:
;   optionaly: "short" if jmp to endif is less than 128 bytes away
; Example:
;		else
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		else	0-1
	%ifnctx if
		%error	"`else' without matching `if'"
	%else
		jmp	%1 %$endif
%$elseif:
		%define	%$elseif_defined
	%endif
%endmacro

;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
; Summary:
;   Finish am conditional statement
; Arguments:
;   none
; Example:
;		endif
;-----======xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx======-----
%macro		endif	0
	%ifnctx if
		%error	"`endif' without matching `if'"
	%else
		%ifndef %$elseif_defined
%$elseif:
		%endif
%$endif:
		%pop
	%endif
%endmacro

%endif ; __PROC32_ASH__