2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
33 void *xmalloc (size_t size
)
37 res
= malloc (size
? size
: 1);
40 fprintf (stderr
, "Virtual memory exhausted.\n");
46 void *xrealloc (void *ptr
, size_t size
)
48 void *res
= realloc (ptr
, size
);
49 if (size
&& res
== NULL
)
51 fprintf (stderr
, "Virtual memory exhausted.\n");
57 char *xstrdup( const char *str
)
59 char *res
= strdup( str
);
62 fprintf (stderr
, "Virtual memory exhausted.\n");
68 char *strupper(char *s
)
71 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
75 int strendswith(const char* str
, const char* end
)
79 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
82 void fatal_error( const char *msg
, ... )
85 va_start( valist
, msg
);
88 fprintf( stderr
, "%s:", input_file_name
);
90 fprintf( stderr
, "%d:", current_line
);
93 else fprintf( stderr
, "winebuild: " );
94 vfprintf( stderr
, msg
, valist
);
99 void fatal_perror( const char *msg
, ... )
102 va_start( valist
, msg
);
105 fprintf( stderr
, "%s:", input_file_name
);
107 fprintf( stderr
, "%d:", current_line
);
108 fputc( ' ', stderr
);
110 vfprintf( stderr
, msg
, valist
);
116 void error( const char *msg
, ... )
119 va_start( valist
, msg
);
122 fprintf( stderr
, "%s:", input_file_name
);
124 fprintf( stderr
, "%d:", current_line
);
125 fputc( ' ', stderr
);
127 vfprintf( stderr
, msg
, valist
);
132 void warning( const char *msg
, ... )
136 if (!display_warnings
) return;
137 va_start( valist
, msg
);
140 fprintf( stderr
, "%s:", input_file_name
);
142 fprintf( stderr
, "%d:", current_line
);
143 fputc( ' ', stderr
);
145 fprintf( stderr
, "warning: " );
146 vfprintf( stderr
, msg
, valist
);
150 /* output a standard header for generated files */
151 void output_standard_file_header( FILE *outfile
)
154 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n",
157 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
159 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
162 /* dump a byte stream into the assembly code */
163 void dump_bytes( FILE *outfile
, const unsigned char *data
, int len
,
164 const char *label
, int constant
)
168 fprintf( outfile
, "\nstatic %sunsigned char %s[%d] = {",
169 constant
? "const " : "", label
, len
);
170 for (i
= 0; i
< len
; i
++)
172 if (!(i
& 7)) fprintf( outfile
, "\n " );
173 fprintf( outfile
, "0x%02x", *data
++ );
174 if (i
< len
- 1) fprintf( outfile
, "," );
176 fprintf( outfile
, "\n};\n" );
180 /*******************************************************************
183 * Open a file in the given srcdir and set the input_file_name global variable.
185 FILE *open_input_file( const char *srcdir
, const char *name
)
188 FILE *file
= fopen( name
, "r" );
192 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
193 strcpy( fullname
, srcdir
);
194 strcat( fullname
, "/" );
195 strcat( fullname
, name
);
196 file
= fopen( fullname
, "r" );
198 else fullname
= xstrdup( name
);
200 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
201 input_file_name
= fullname
;
207 /*******************************************************************
210 * Close the current input file (must have been opened with open_input_file).
212 void close_input_file( FILE *file
)
215 free( input_file_name
);
216 input_file_name
= NULL
;
221 /*******************************************************************
222 * remove_stdcall_decoration
224 * Remove a possible @xx suffix from a function name.
225 * Return the numerical value of the suffix, or -1 if none.
227 int remove_stdcall_decoration( char *name
)
229 char *p
, *end
= strrchr( name
, '@' );
230 if (!end
|| !end
[1] || end
== name
) return -1;
231 /* make sure all the rest is digits */
232 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
234 return atoi( end
+ 1 );
238 /*******************************************************************
241 * Create a new dll spec file descriptor
243 DLLSPEC
*alloc_dll_spec(void)
247 spec
= xmalloc( sizeof(*spec
) );
248 spec
->file_name
= NULL
;
249 spec
->dll_name
= NULL
;
250 spec
->owner_name
= NULL
;
251 spec
->init_func
= NULL
;
252 spec
->type
= SPEC_WIN32
;
253 spec
->base
= MAX_ORDINALS
;
255 spec
->stack_size
= 0;
257 spec
->nb_entry_points
= 0;
258 spec
->alloc_entry_points
= 0;
260 spec
->nb_resources
= 0;
261 spec
->characteristics
= 0;
263 spec
->subsystem_major
= 4;
264 spec
->subsystem_minor
= 0;
265 spec
->entry_points
= NULL
;
267 spec
->ordinals
= NULL
;
268 spec
->resources
= NULL
;
273 /*******************************************************************
276 * Free dll spec file descriptor
278 void free_dll_spec( DLLSPEC
*spec
)
282 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
284 ORDDEF
*odp
= &spec
->entry_points
[i
];
286 free( odp
->export_name
);
287 free( odp
->link_name
);
289 free( spec
->file_name
);
290 free( spec
->dll_name
);
291 free( spec
->owner_name
);
292 free( spec
->init_func
);
293 free( spec
->entry_points
);
295 free( spec
->ordinals
);
296 free( spec
->resources
);
301 /*******************************************************************
304 * Map a string to a valid C identifier.
306 const char *make_c_identifier( const char *str
)
308 static char buffer
[256];
311 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
313 if (isalnum(*str
)) *p
= *str
;
321 /*****************************************************************
322 * Function: get_alignment
325 * According to the info page for gas, the .align directive behaves
326 * differently on different systems. On some architectures, the
327 * argument of a .align directive is the number of bytes to pad to, so
328 * to align on an 8-byte boundary you'd say
330 * On other systems, the argument is "the number of low-order zero bits
331 * that the location counter must have after advancement." So to
332 * align on an 8-byte boundary you'd say
335 * The reason gas is written this way is that it's trying to mimick
336 * native assemblers for the various architectures it runs on. gas
337 * provides other directives that work consistantly across
338 * architectures, but of course we want to work on all arches with or
339 * without gas. Hence this function.
343 * align -- the number of bytes to align to. Must be a power of 2.
345 unsigned int get_alignment(unsigned int align
)
349 assert( !(align
& (align
- 1)) );
355 if (target_platform
!= PLATFORM_APPLE
) return align
;
360 while ((1 << n
) != align
) n
++;
368 /* return the page size for the target CPU */
369 unsigned int get_page_size(void)
373 case CPU_x86
: return 4096;
374 case CPU_POWERPC
: return 4096;
375 case CPU_SPARC
: return 8192;
376 case CPU_ALPHA
: return 8192;
383 /* return the assembly name for a C symbol */
384 const char *asm_name( const char *sym
)
386 static char buffer
[256];
388 switch (target_platform
)
391 case PLATFORM_WINDOWS
:
393 strcpy( buffer
+ 1, sym
);
400 /* return an assembly function declaration for a C function name */
401 const char *func_declaration( const char *func
)
403 static char buffer
[256];
405 switch (target_platform
)
409 case PLATFORM_WINDOWS
:
410 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
413 sprintf( buffer
, ".type %s,2", func
);
416 sprintf( buffer
, ".type %s,@function", func
);
422 /* return a size declaration for an assembly function */
423 const char *func_size( const char *func
)
425 static char buffer
[256];
427 switch (target_platform
)
430 case PLATFORM_WINDOWS
:
433 sprintf( buffer
, ".size %s, .-%s", func
, func
);
438 const char *get_asm_string_keyword(void)
440 switch (target_platform
)
450 const char *get_asm_short_keyword(void)
452 switch (target_platform
)
454 case PLATFORM_SVR4
: return ".half";
455 default: return ".short";