1/* lzo_asm.h -- LZO assembler stuff
2
3   This file is part of the LZO real-time data compression library.
4
5   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21   All Rights Reserved.
22
23   The LZO library is free software; you can redistribute it and/or
24   modify it under the terms of the GNU General Public License as
25   published by the Free Software Foundation; either version 2 of
26   the License, or (at your option) any later version.
27
28   The LZO library is distributed in the hope that it will be useful,
29   but WITHOUT ANY WARRANTY; without even the implied warranty of
30   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31   GNU General Public License for more details.
32
33   You should have received a copy of the GNU General Public License
34   along with the LZO library; see the file COPYING.
35   If not, write to the Free Software Foundation, Inc.,
36   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37
38   Markus F.X.J. Oberhumer
39   <markus@oberhumer.com>
40   http://www.oberhumer.com/opensource/lzo/
41 */
42
43
44/***********************************************************************
45// <asmconfig.h>
46************************************************************************/
47
48#if !defined(__i386__)
49#  error
50#endif
51
52#if !defined(IN_CONFIGURE)
53#if defined(LZO_HAVE_CONFIG_H)
54#  include <config.h>
55#else
56   /* manual configuration - see defaults below */
57#  if defined(__ELF__)
58#    define MFX_ASM_HAVE_TYPE 1
59#    define MFX_ASM_NAME_NO_UNDERSCORES 1
60#  elif defined(__linux__)              /* Linux a.out */
61#    define MFX_ASM_ALIGN_PTWO 1
62#  elif defined(__DJGPP__)
63#    define MFX_ASM_ALIGN_PTWO 1
64#  elif defined(__GO32__)               /* djgpp v1 */
65#    define MFX_ASM_CANNOT_USE_EBP 1
66#  elif defined(__EMX__)
67#    define MFX_ASM_ALIGN_PTWO 1
68#    define MFX_ASM_CANNOT_USE_EBP 1
69#  endif
70#endif
71#endif
72
73#if 1 && defined(__ELF__)
74.section .note.GNU-stack,"",@progbits
75#endif
76#if 0 && defined(__ELF__)
77#undef i386
78.arch i386
79.code32
80#endif
81
82
83/***********************************************************************
84// name always uses underscores
85// [ OLD: name (default: with underscores) ]
86************************************************************************/
87
88#if !defined(LZO_ASM_NAME)
89#  define LZO_ASM_NAME(n)       _ ## n
90#if 0
91#  if defined(MFX_ASM_NAME_NO_UNDERSCORES)
92#    define LZO_ASM_NAME(n)     n
93#  else
94#    define LZO_ASM_NAME(n)     _ ## n
95#  endif
96#endif
97#endif
98
99
100/***********************************************************************
101// .type (default: do not use)
102************************************************************************/
103
104#if !defined(LZO_PUBLIC)
105#if defined(__LZO_DB__)
106#  define LZO_PUBLIC(func) \
107        .p2align 4 ; .byte 0,0,0,0,0,0,0 ; .ascii "LZO_START"
108#  define LZO_PUBLIC_END(func) \
109        .p2align 4,0x90 ; .ascii "LZO_END"
110#elif defined(MFX_ASM_HAVE_TYPE)
111#  define LZO_PUBLIC(func) \
112        ALIGN3 ; .type LZO_ASM_NAME(func),@function ; \
113        .globl LZO_ASM_NAME(func) ; LZO_ASM_NAME(func):
114#  define LZO_PUBLIC_END(func) \
115        .size LZO_ASM_NAME(func),.-LZO_ASM_NAME(func)
116#else
117#  define LZO_PUBLIC(func) \
118        ALIGN3 ; .globl LZO_ASM_NAME(func) ; LZO_ASM_NAME(func):
119#  define LZO_PUBLIC_END(func)
120#endif
121#endif
122
123
124/***********************************************************************
125// .align (default: bytes)
126************************************************************************/
127
128#if !defined(MFX_ASM_ALIGN_BYTES) && !defined(MFX_ASM_ALIGN_PTWO)
129#  define MFX_ASM_ALIGN_BYTES 1
130#endif
131
132#if !defined(LZO_ASM_ALIGN)
133#  if defined(MFX_ASM_ALIGN_PTWO)
134#    define LZO_ASM_ALIGN(x)    .align x
135#  else
136#    define LZO_ASM_ALIGN(x)    .align (1 << (x))
137#  endif
138#endif
139
140#define ALIGN1              LZO_ASM_ALIGN(1)
141#define ALIGN2              LZO_ASM_ALIGN(2)
142#define ALIGN3              LZO_ASM_ALIGN(3)
143
144
145/***********************************************************************
146// ebp usage (default: can use)
147************************************************************************/
148
149#if !defined(MFX_ASM_CANNOT_USE_EBP)
150#  if 1 && !defined(N_3_EBP) && !defined(N_255_EBP)
151#    define N_3_EBP 1
152#  endif
153#  if 0 && !defined(N_3_EBP) && !defined(N_255_EBP)
154#    define N_255_EBP 1
155#  endif
156#endif
157
158#if defined(N_3_EBP) && defined(N_255_EBP)
159#  error
160#endif
161#if defined(MFX_ASM_CANNOT_USE_EBP)
162#  if defined(N_3_EBP) || defined(N_255_EBP)
163#    error
164#  endif
165#endif
166
167#if !defined(N_3)
168#  if defined(N_3_EBP)
169#    define N_3         %ebp
170#  else
171#    define N_3         $3
172#  endif
173#endif
174
175#if !defined(N_255)
176#  if defined(N_255_EBP)
177#    define N_255       %ebp
178#    define NOTL_3(r)   xorl %ebp,r
179#  else
180#    define N_255       $255
181#  endif
182#endif
183
184#if !defined(NOTL_3)
185#  define NOTL_3(r)     xorl N_3,r
186#endif
187
188
189/***********************************************************************
190//
191************************************************************************/
192
193#ifndef INP
194#define INP      4+36(%esp)
195#define INS      8+36(%esp)
196#define OUTP    12+36(%esp)
197#define OUTS    16+36(%esp)
198#endif
199
200#define INEND         4(%esp)
201#define OUTEND        (%esp)
202
203
204#if defined(LZO_TEST_DECOMPRESS_OVERRUN_INPUT)
205#  define TEST_IP_R(r)      cmpl r,INEND ; jb .L_input_overrun
206#  define TEST_IP(addr,r)   leal addr,r ; TEST_IP_R(r)
207#else
208#  define TEST_IP_R(r)
209#  define TEST_IP(addr,r)
210#endif
211
212#if defined(LZO_TEST_DECOMPRESS_OVERRUN_OUTPUT)
213#  define TEST_OP_R(r)      cmpl r,OUTEND ; jb .L_output_overrun
214#  define TEST_OP(addr,r)   leal addr,r ; TEST_OP_R(r)
215#else
216#  define TEST_OP_R(r)
217#  define TEST_OP(addr,r)
218#endif
219
220#if defined(LZO_TEST_DECOMPRESS_OVERRUN_LOOKBEHIND)
221#  define TEST_LOOKBEHIND(r)    cmpl OUTP,r ; jb .L_lookbehind_overrun
222#else
223#  define TEST_LOOKBEHIND(r)
224#endif
225
226
227/***********************************************************************
228//
229************************************************************************/
230
231#define LODSB           movb (%esi),%al ; incl %esi
232
233#define MOVSB(r1,r2,x)  movb (r1),x ; incl r1 ; movb x,(r2) ; incl r2
234#define MOVSW(r1,r2,x)  movb (r1),x ; movb x,(r2) ; \
235                        movb 1(r1),x ; addl $2,r1 ; \
236                        movb x,1(r2) ; addl $2,r2
237#define MOVSL(r1,r2,x)  movl (r1),x ; addl $4,r1 ; movl x,(r2) ; addl $4,r2
238
239#if defined(LZO_DEBUG)
240#define COPYB_C(r1,r2,x,rc) \
241                        cmpl $0,rc ; jz .L_assert_fail; \
242                        9: MOVSB(r1,r2,x) ; decl rc ; jnz 9b
243#define COPYL_C(r1,r2,x,rc) \
244                        cmpl $0,rc ; jz .L_assert_fail; \
245                        9: MOVSL(r1,r2,x) ; decl rc ; jnz 9b
246#else
247#define COPYB_C(r1,r2,x,rc) \
248                        9: MOVSB(r1,r2,x) ; decl rc ; jnz 9b
249#define COPYL_C(r1,r2,x,rc) \
250                        9: MOVSL(r1,r2,x) ; decl rc ; jnz 9b
251#endif
252
253#define COPYB(r1,r2,x)  COPYB_C(r1,r2,x,%ecx)
254#define COPYL(r1,r2,x)  COPYL_C(r1,r2,x,%ecx)
255
256
257/***********************************************************************
258// not used
259************************************************************************/
260
261#if 0
262
263#if 0
264#define REP_MOVSB(x)    rep ; movsb
265#define REP_MOVSL(x)    shrl $2,%ecx ; rep ; movsl
266#elif 1
267#define REP_MOVSB(x)    COPYB(%esi,%edi,x)
268#define REP_MOVSL(x)    shrl $2,%ecx ; COPYL(%esi,%edi,x)
269#else
270#define REP_MOVSB(x)    rep ; movsb
271#define REP_MOVSL(x)    jmp 9f ; 8: movsb ; decl %ecx ; \
272                        9: testl $3,%edi ; jnz 8b ; \
273                        movl %ecx,x ; shrl $2,%ecx ; andl $3,x ; \
274                        rep ; movsl ; movl x,%ecx ; rep ; movsb
275#endif
276
277#if 1
278#define NEGL(x)         negl x
279#else
280#define NEGL(x)         xorl $-1,x ; incl x
281#endif
282
283#endif
284
285
286
287/*
288vi:ts=4
289*/
290
291