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"
32 void *xmalloc (size_t size)
36 res = malloc (size ? size : 1);
39 fprintf (stderr, "Virtual memory exhausted.\n");
45 void *xrealloc (void *ptr, size_t size)
47 void *res = realloc (ptr, size);
50 fprintf (stderr, "Virtual memory exhausted.\n");
56 char *xstrdup( const char *str )
58 char *res = strdup( str );
61 fprintf (stderr, "Virtual memory exhausted.\n");
67 char *strupper(char *s)
70 for (p = s; *p; p++) *p = toupper(*p);
74 void fatal_error( const char *msg, ... )
77 va_start( valist, msg );
80 fprintf( stderr, "%s:", input_file_name );
82 fprintf( stderr, "%d:", current_line );
85 vfprintf( stderr, msg, valist );
90 void fatal_perror( const char *msg, ... )
93 va_start( valist, msg );
96 fprintf( stderr, "%s:", input_file_name );
98 fprintf( stderr, "%d:", current_line );
101 vfprintf( stderr, msg, valist );
107 void warning( const char *msg, ... )
111 if (!display_warnings) return;
112 va_start( valist, msg );
115 fprintf( stderr, "%s:", input_file_name );
117 fprintf( stderr, "%d:", current_line );
118 fputc( ' ', stderr );
120 fprintf( stderr, "warning: " );
121 vfprintf( stderr, msg, valist );
125 /* output a standard header for generated files */
126 void output_standard_file_header( FILE *outfile )
129 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
132 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
134 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
137 /* dump a byte stream into the assembly code */
138 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
139 const char *label, int constant )
143 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
144 constant ? "const " : "", label, len );
145 for (i = 0; i < len; i++)
147 if (!(i & 7)) fprintf( outfile, "\n " );
148 fprintf( outfile, "0x%02x", *data++ );
149 if (i < len - 1) fprintf( outfile, "," );
151 fprintf( outfile, "\n};\n" );
155 /*******************************************************************
158 * Open a file in the given srcdir and set the input_file_name global variable.
160 FILE *open_input_file( const char *srcdir, const char *name )
167 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
168 strcpy( fullname, srcdir );
169 strcat( fullname, "/" );
170 strcat( fullname, name );
172 else fullname = xstrdup( name );
174 if (!(file = fopen( fullname, "r" ))) fatal_error( "Cannot open file '%s'\n", fullname );
175 input_file_name = fullname;
181 /*******************************************************************
184 * Close the current input file (must have been opened with open_input_file).
186 void close_input_file( FILE *file )
189 free( input_file_name );
190 input_file_name = NULL;
195 /*******************************************************************
198 * Map a string to a valid C identifier.
200 const char *make_c_identifier( const char *str )
202 static char buffer[256];
205 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
207 if (isalnum(*str)) *p = *str;
215 /*****************************************************************
216 * Function: get_alignment
219 * According to the info page for gas, the .align directive behaves
220 * differently on different systems. On some architectures, the
221 * argument of a .align directive is the number of bytes to pad to, so
222 * to align on an 8-byte boundary you'd say
224 * On other systems, the argument is "the number of low-order zero bits
225 * that the location counter must have after advancement." So to
226 * align on an 8-byte boundary you'd say
229 * The reason gas is written this way is that it's trying to mimick
230 * native assemblers for the various architectures it runs on. gas
231 * provides other directives that work consistantly across
232 * architectures, but of course we want to work on all arches with or
233 * without gas. Hence this function.
237 * alignBoundary -- the number of bytes to align to.
238 * If we're on an architecture where
239 * the assembler requires a 'number
240 * of low-order zero bits' as a
241 * .align argument, then this number
242 * must be a power of 2.
245 int get_alignment(int alignBoundary)
251 switch(alignBoundary)
302 fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
307 #elif defined(__i386__) || defined(__sparc__)
309 return alignBoundary;
312 #error "How does the '.align' assembler directive work on your architecture?"