2002-03-10 00:29:33 +01:00
|
|
|
/*
|
|
|
|
* Small utility functions for winebuild
|
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2000-06-23 22:15:35 +02:00
|
|
|
|
2001-11-06 21:57:11 +01:00
|
|
|
#include "config.h"
|
2002-04-25 23:40:56 +02:00
|
|
|
#include "wine/port.h"
|
2001-11-06 21:57:11 +01:00
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "build.h"
|
|
|
|
|
|
|
|
void *xmalloc (size_t size)
|
|
|
|
{
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
res = malloc (size ? size : 1);
|
|
|
|
if (res == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Virtual memory exhausted.\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc (void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *res = realloc (ptr, size);
|
|
|
|
if (res == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Virtual memory exhausted.\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup( const char *str )
|
|
|
|
{
|
|
|
|
char *res = strdup( str );
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Virtual memory exhausted.\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strupper(char *s)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for (p = s; *p; p++) *p = toupper(*p);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fatal_error( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
va_start( valist, msg );
|
2000-09-28 01:40:43 +02:00
|
|
|
if (input_file_name)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s:", input_file_name );
|
|
|
|
if (current_line)
|
|
|
|
fprintf( stderr, "%d:", current_line );
|
|
|
|
fputc( ' ', stderr );
|
|
|
|
}
|
2000-06-23 22:15:35 +02:00
|
|
|
vfprintf( stderr, msg, valist );
|
|
|
|
va_end( valist );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-10-23 23:33:06 +02:00
|
|
|
void fatal_perror( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
|
|
|
va_start( valist, msg );
|
|
|
|
if (input_file_name)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s:", input_file_name );
|
|
|
|
if (current_line)
|
|
|
|
fprintf( stderr, "%d:", current_line );
|
|
|
|
fputc( ' ', stderr );
|
|
|
|
}
|
|
|
|
vfprintf( stderr, msg, valist );
|
|
|
|
perror( " " );
|
|
|
|
va_end( valist );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-09-28 01:40:43 +02:00
|
|
|
void warning( const char *msg, ... )
|
|
|
|
{
|
|
|
|
va_list valist;
|
2002-03-21 02:38:53 +01:00
|
|
|
|
|
|
|
if (!display_warnings) return;
|
2000-09-28 01:40:43 +02:00
|
|
|
va_start( valist, msg );
|
|
|
|
if (input_file_name)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s:", input_file_name );
|
|
|
|
if (current_line)
|
|
|
|
fprintf( stderr, "%d:", current_line );
|
|
|
|
fputc( ' ', stderr );
|
|
|
|
}
|
|
|
|
fprintf( stderr, "warning: " );
|
|
|
|
vfprintf( stderr, msg, valist );
|
|
|
|
va_end( valist );
|
|
|
|
}
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
/* output a standard header for generated files */
|
|
|
|
void output_standard_file_header( FILE *outfile )
|
|
|
|
{
|
2002-05-12 01:06:32 +02:00
|
|
|
if (input_file_name)
|
|
|
|
fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
|
|
|
|
input_file_name );
|
|
|
|
else
|
|
|
|
fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
|
2002-03-10 00:29:33 +01:00
|
|
|
fprintf( outfile,
|
|
|
|
"/* This file can be copied, modified and distributed without restriction. */\n\n" );
|
|
|
|
}
|
|
|
|
|
2000-06-23 22:15:35 +02:00
|
|
|
/* dump a byte stream into the assembly code */
|
2000-10-25 22:33:58 +02:00
|
|
|
void dump_bytes( FILE *outfile, const unsigned char *data, int len,
|
|
|
|
const char *label, int constant )
|
2000-06-23 22:15:35 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2000-10-25 22:33:58 +02:00
|
|
|
fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
|
|
|
|
constant ? "const " : "", label, len );
|
2000-06-23 22:15:35 +02:00
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2000-10-25 22:33:58 +02:00
|
|
|
if (!(i & 7)) fprintf( outfile, "\n " );
|
|
|
|
fprintf( outfile, "0x%02x", *data++ );
|
|
|
|
if (i < len - 1) fprintf( outfile, "," );
|
2000-06-23 22:15:35 +02:00
|
|
|
}
|
|
|
|
fprintf( outfile, "\n};\n" );
|
|
|
|
}
|
2001-02-13 03:06:38 +01:00
|
|
|
|
2002-05-14 22:54:58 +02:00
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* make_c_identifier
|
|
|
|
*
|
|
|
|
* Map a string to a valid C identifier.
|
|
|
|
*/
|
|
|
|
const char *make_c_identifier( const char *str )
|
|
|
|
{
|
|
|
|
static char buffer[256];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
|
|
|
|
{
|
|
|
|
if (isalnum(*str)) *p = *str;
|
|
|
|
else *p = '_';
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-13 03:06:38 +01:00
|
|
|
/*****************************************************************
|
|
|
|
* Function: get_alignment
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* According to the info page for gas, the .align directive behaves
|
|
|
|
* differently on different systems. On some architectures, the
|
|
|
|
* argument of a .align directive is the number of bytes to pad to, so
|
|
|
|
* to align on an 8-byte boundary you'd say
|
|
|
|
* .align 8
|
|
|
|
* On other systems, the argument is "the number of low-order zero bits
|
|
|
|
* that the location counter must have after advancement." So to
|
|
|
|
* align on an 8-byte boundary you'd say
|
|
|
|
* .align 3
|
|
|
|
*
|
|
|
|
* The reason gas is written this way is that it's trying to mimick
|
|
|
|
* native assemblers for the various architectures it runs on. gas
|
|
|
|
* provides other directives that work consistantly across
|
|
|
|
* architectures, but of course we want to work on all arches with or
|
|
|
|
* without gas. Hence this function.
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
2001-02-13 03:06:38 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* alignBoundary -- the number of bytes to align to.
|
2002-06-01 01:06:46 +02:00
|
|
|
* If we're on an architecture where
|
2001-02-13 03:06:38 +01:00
|
|
|
* the assembler requires a 'number
|
|
|
|
* of low-order zero bits' as a
|
|
|
|
* .align argument, then this number
|
2002-06-01 01:06:46 +02:00
|
|
|
* must be a power of 2.
|
2001-02-13 03:06:38 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
int get_alignment(int alignBoundary)
|
|
|
|
{
|
|
|
|
#ifdef __PPC__
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
switch(alignBoundary)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
n = 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
n = 2;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
n = 4;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
n = 5;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
n = 6;
|
|
|
|
break;
|
|
|
|
case 128:
|
|
|
|
n = 7;
|
|
|
|
break;
|
|
|
|
case 256:
|
|
|
|
n = 8;
|
|
|
|
break;
|
|
|
|
case 512:
|
|
|
|
n = 9;
|
|
|
|
break;
|
|
|
|
case 1024:
|
|
|
|
n = 10;
|
|
|
|
break;
|
|
|
|
case 2048:
|
|
|
|
n = 11;
|
|
|
|
break;
|
|
|
|
case 4096:
|
|
|
|
n = 12;
|
|
|
|
break;
|
|
|
|
case 8192:
|
|
|
|
n = 13;
|
|
|
|
break;
|
|
|
|
case 16384:
|
|
|
|
n = 14;
|
|
|
|
break;
|
|
|
|
case 32768:
|
|
|
|
n = 15;
|
|
|
|
break;
|
|
|
|
case 65536:
|
|
|
|
n = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal_error("Alignment to %d-byte boundary not supported on this architecture.\n",
|
|
|
|
alignBoundary);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
|
|
|
|
#elif defined(__i386__) || defined(__sparc__)
|
|
|
|
|
|
|
|
return alignBoundary;
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "How does the '.align' assembler directive work on your architecture?"
|
|
|
|
#endif
|
|
|
|
}
|