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
36 void *xmalloc (size_t size
)
40 res
= malloc (size
? size
: 1);
43 fprintf (stderr
, "Virtual memory exhausted.\n");
49 void *xrealloc (void *ptr
, size_t size
)
51 void *res
= realloc (ptr
, size
);
52 if (size
&& res
== NULL
)
54 fprintf (stderr
, "Virtual memory exhausted.\n");
60 char *xstrdup( const char *str
)
62 char *res
= strdup( str
);
65 fprintf (stderr
, "Virtual memory exhausted.\n");
71 char *strupper(char *s
)
74 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
78 int strendswith(const char* str
, const char* end
)
82 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
85 void fatal_error( const char *msg
, ... )
88 va_start( valist
, msg
);
91 fprintf( stderr
, "%s:", input_file_name
);
93 fprintf( stderr
, "%d:", current_line
);
96 else fprintf( stderr
, "winebuild: " );
97 vfprintf( stderr
, msg
, valist
);
102 void fatal_perror( const char *msg
, ... )
105 va_start( valist
, msg
);
108 fprintf( stderr
, "%s:", input_file_name
);
110 fprintf( stderr
, "%d:", current_line
);
111 fputc( ' ', stderr
);
113 vfprintf( stderr
, msg
, valist
);
119 void error( const char *msg
, ... )
122 va_start( valist
, msg
);
125 fprintf( stderr
, "%s:", input_file_name
);
127 fprintf( stderr
, "%d:", current_line
);
128 fputc( ' ', stderr
);
130 vfprintf( stderr
, msg
, valist
);
135 void warning( const char *msg
, ... )
139 if (!display_warnings
) return;
140 va_start( valist
, msg
);
143 fprintf( stderr
, "%s:", input_file_name
);
145 fprintf( stderr
, "%d:", current_line
);
146 fputc( ' ', stderr
);
148 fprintf( stderr
, "warning: " );
149 vfprintf( stderr
, msg
, valist
);
153 /* output a standard header for generated files */
154 void output_standard_file_header( FILE *outfile
)
157 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n",
160 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
162 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
165 /* dump a byte stream into the assembly code */
166 void dump_bytes( FILE *outfile
, const unsigned char *data
, int len
,
167 const char *label
, int constant
)
171 fprintf( outfile
, "\nstatic %sunsigned char %s[%d] = {",
172 constant
? "const " : "", label
, len
);
173 for (i
= 0; i
< len
; i
++)
175 if (!(i
& 7)) fprintf( outfile
, "\n " );
176 fprintf( outfile
, "0x%02x", *data
++ );
177 if (i
< len
- 1) fprintf( outfile
, "," );
179 fprintf( outfile
, "\n};\n" );
183 /*******************************************************************
186 * Open a file in the given srcdir and set the input_file_name global variable.
188 FILE *open_input_file( const char *srcdir
, const char *name
)
191 FILE *file
= fopen( name
, "r" );
195 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
196 strcpy( fullname
, srcdir
);
197 strcat( fullname
, "/" );
198 strcat( fullname
, name
);
199 file
= fopen( fullname
, "r" );
201 else fullname
= xstrdup( name
);
203 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
204 input_file_name
= fullname
;
210 /*******************************************************************
213 * Close the current input file (must have been opened with open_input_file).
215 void close_input_file( FILE *file
)
218 free( input_file_name
);
219 input_file_name
= NULL
;
224 /*******************************************************************
225 * remove_stdcall_decoration
227 * Remove a possible @xx suffix from a function name.
228 * Return the numerical value of the suffix, or -1 if none.
230 int remove_stdcall_decoration( char *name
)
232 char *p
, *end
= strrchr( name
, '@' );
233 if (!end
|| !end
[1] || end
== name
) return -1;
234 /* make sure all the rest is digits */
235 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
237 return atoi( end
+ 1 );
241 /*******************************************************************
244 * Create a new dll spec file descriptor
246 DLLSPEC
*alloc_dll_spec(void)
250 spec
= xmalloc( sizeof(*spec
) );
251 spec
->file_name
= NULL
;
252 spec
->dll_name
= NULL
;
253 spec
->owner_name
= NULL
;
254 spec
->init_func
= NULL
;
255 spec
->type
= SPEC_WIN32
;
256 spec
->base
= MAX_ORDINALS
;
258 spec
->stack_size
= 0;
260 spec
->nb_entry_points
= 0;
261 spec
->alloc_entry_points
= 0;
263 spec
->nb_resources
= 0;
264 spec
->characteristics
= 0;
266 spec
->subsystem_major
= 4;
267 spec
->subsystem_minor
= 0;
268 spec
->entry_points
= NULL
;
270 spec
->ordinals
= NULL
;
271 spec
->resources
= NULL
;
276 /*******************************************************************
279 * Free dll spec file descriptor
281 void free_dll_spec( DLLSPEC
*spec
)
285 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
287 ORDDEF
*odp
= &spec
->entry_points
[i
];
289 free( odp
->export_name
);
290 free( odp
->link_name
);
292 free( spec
->file_name
);
293 free( spec
->dll_name
);
294 free( spec
->owner_name
);
295 free( spec
->init_func
);
296 free( spec
->entry_points
);
298 free( spec
->ordinals
);
299 free( spec
->resources
);
304 /*******************************************************************
307 * Map a string to a valid C identifier.
309 const char *make_c_identifier( const char *str
)
311 static char buffer
[256];
314 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
316 if (isalnum(*str
)) *p
= *str
;
324 /*****************************************************************
325 * Function: get_alignment
328 * According to the info page for gas, the .align directive behaves
329 * differently on different systems. On some architectures, the
330 * argument of a .align directive is the number of bytes to pad to, so
331 * to align on an 8-byte boundary you'd say
333 * On other systems, the argument is "the number of low-order zero bits
334 * that the location counter must have after advancement." So to
335 * align on an 8-byte boundary you'd say
338 * The reason gas is written this way is that it's trying to mimick
339 * native assemblers for the various architectures it runs on. gas
340 * provides other directives that work consistantly across
341 * architectures, but of course we want to work on all arches with or
342 * without gas. Hence this function.
346 * align -- the number of bytes to align to. Must be a power of 2.
348 unsigned int get_alignment(unsigned int align
)
352 assert( !(align
& (align
- 1)) );
358 if (target_platform
!= PLATFORM_APPLE
) return align
;
363 while ((1 << n
) != align
) n
++;
371 /* return the page size for the target CPU */
372 unsigned int get_page_size(void)
376 case CPU_x86
: return 4096;
377 case CPU_POWERPC
: return 4096;
378 case CPU_SPARC
: return 8192;
379 case CPU_ALPHA
: return 8192;
386 /* return the assembly name for a C symbol */
387 const char *asm_name( const char *sym
)
389 static char buffer
[256];
391 switch (target_platform
)
394 case PLATFORM_WINDOWS
:
396 strcpy( buffer
+ 1, sym
);
403 /* return an assembly function declaration for a C function name */
404 const char *func_declaration( const char *func
)
406 static char buffer
[256];
408 switch (target_platform
)
412 case PLATFORM_WINDOWS
:
413 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
416 sprintf( buffer
, ".type %s,2", func
);
419 sprintf( buffer
, ".type %s,@function", func
);
425 /* return a size declaration for an assembly function */
426 const char *func_size( const char *func
)
428 static char buffer
[256];
430 switch (target_platform
)
433 case PLATFORM_WINDOWS
:
436 sprintf( buffer
, ".size %s, .-%s", func
, func
);
441 const char *get_asm_string_keyword(void)
443 switch (target_platform
)
453 const char *get_asm_short_keyword(void)
455 switch (target_platform
)
457 case PLATFORM_SVR4
: return ".half";
458 default: return ".short";