winebuild: Add an option to use the C compiler to assemble files.
[wine.git] / tools / winebuild / utils.c
blob2028b05745b39a6bb5870e294603807af568f9df
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
35 #endif
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
40 #include "build.h"
42 static const char **tmp_files;
43 static unsigned int nb_tmp_files;
44 static unsigned int max_tmp_files;
46 static const struct
48 const char *name;
49 enum target_cpu cpu;
50 } cpu_names[] =
52 { "i386", CPU_x86 },
53 { "i486", CPU_x86 },
54 { "i586", CPU_x86 },
55 { "i686", CPU_x86 },
56 { "i786", CPU_x86 },
57 { "amd64", CPU_x86_64 },
58 { "x86_64", CPU_x86_64 },
59 { "powerpc", CPU_POWERPC },
60 { "arm", CPU_ARM },
61 { "arm64", CPU_ARM64 },
62 { "aarch64", CPU_ARM64 },
65 /* atexit handler to clean tmp files */
66 void cleanup_tmp_files(void)
68 unsigned int i;
69 for (i = 0; i < nb_tmp_files; i++) if (tmp_files[i]) unlink( tmp_files[i] );
73 void *xmalloc (size_t size)
75 void *res;
77 res = malloc (size ? size : 1);
78 if (res == NULL)
80 fprintf (stderr, "Virtual memory exhausted.\n");
81 exit (1);
83 return res;
86 void *xrealloc (void *ptr, size_t size)
88 void *res = realloc (ptr, size);
89 if (size && res == NULL)
91 fprintf (stderr, "Virtual memory exhausted.\n");
92 exit (1);
94 return res;
97 char *xstrdup( const char *str )
99 char *res = strdup( str );
100 if (!res)
102 fprintf (stderr, "Virtual memory exhausted.\n");
103 exit (1);
105 return res;
108 char *strupper(char *s)
110 char *p;
111 for (p = s; *p; p++) *p = toupper(*p);
112 return s;
115 int strendswith(const char* str, const char* end)
117 int l = strlen(str);
118 int m = strlen(end);
119 return l >= m && strcmp(str + l - m, end) == 0;
122 char *strmake( const char* fmt, ... )
124 int n;
125 size_t size = 100;
126 va_list ap;
128 for (;;)
130 char *p = xmalloc( size );
131 va_start( ap, fmt );
132 n = vsnprintf( p, size, fmt, ap );
133 va_end( ap );
134 if (n == -1) size *= 2;
135 else if ((size_t)n >= size) size = n + 1;
136 else return p;
137 free( p );
141 static struct strarray *strarray_init( const char *str )
143 struct strarray *array = xmalloc( sizeof(*array) );
144 array->count = 0;
145 array->max = 16;
146 array->str = xmalloc( array->max * sizeof(*array->str) );
147 if (str) array->str[array->count++] = str;
148 return array;
151 static struct strarray *strarray_copy( const struct strarray *src )
153 struct strarray *array = xmalloc( sizeof(*array) );
154 array->count = src->count;
155 array->max = src->max;
156 array->str = xmalloc( array->max * sizeof(*array->str) );
157 memcpy( array->str, src->str, array->count * sizeof(*array->str) );
158 return array;
161 static void strarray_add_one( struct strarray *array, const char *str )
163 if (array->count == array->max)
165 array->max *= 2;
166 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
168 array->str[array->count++] = str;
171 void strarray_add( struct strarray *array, ... )
173 va_list valist;
174 const char *str;
176 va_start( valist, array );
177 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
178 va_end( valist );
181 void strarray_addv( struct strarray *array, char * const *argv )
183 while (*argv) strarray_add_one( array, *argv++ );
186 struct strarray *strarray_fromstring( const char *str, const char *delim )
188 const char *tok;
189 struct strarray *array = strarray_init( NULL );
190 char *buf = strdup( str );
192 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
193 strarray_add_one( array, strdup( tok ));
195 free( buf );
196 return array;
199 void strarray_free( struct strarray *array )
201 free( array->str );
202 free( array );
205 void fatal_error( const char *msg, ... )
207 va_list valist;
208 va_start( valist, msg );
209 if (input_file_name)
211 fprintf( stderr, "%s:", input_file_name );
212 if (current_line)
213 fprintf( stderr, "%d:", current_line );
214 fputc( ' ', stderr );
216 else fprintf( stderr, "winebuild: " );
217 vfprintf( stderr, msg, valist );
218 va_end( valist );
219 exit(1);
222 void fatal_perror( const char *msg, ... )
224 va_list valist;
225 va_start( valist, msg );
226 if (input_file_name)
228 fprintf( stderr, "%s:", input_file_name );
229 if (current_line)
230 fprintf( stderr, "%d:", current_line );
231 fputc( ' ', stderr );
233 vfprintf( stderr, msg, valist );
234 perror( " " );
235 va_end( valist );
236 exit(1);
239 void error( const char *msg, ... )
241 va_list valist;
242 va_start( valist, msg );
243 if (input_file_name)
245 fprintf( stderr, "%s:", input_file_name );
246 if (current_line)
247 fprintf( stderr, "%d:", current_line );
248 fputc( ' ', stderr );
250 vfprintf( stderr, msg, valist );
251 va_end( valist );
252 nb_errors++;
255 void warning( const char *msg, ... )
257 va_list valist;
259 if (!display_warnings) return;
260 va_start( valist, msg );
261 if (input_file_name)
263 fprintf( stderr, "%s:", input_file_name );
264 if (current_line)
265 fprintf( stderr, "%d:", current_line );
266 fputc( ' ', stderr );
268 fprintf( stderr, "warning: " );
269 vfprintf( stderr, msg, valist );
270 va_end( valist );
273 int output( const char *format, ... )
275 int ret;
276 va_list valist;
278 va_start( valist, format );
279 ret = vfprintf( output_file, format, valist );
280 va_end( valist );
281 if (ret < 0) fatal_perror( "Output error" );
282 return ret;
285 void spawn( struct strarray *args )
287 unsigned int i;
288 int status;
290 strarray_add_one( args, NULL );
291 if (verbose)
292 for (i = 0; args->str[i]; i++)
293 fprintf( stderr, "%s%c", args->str[i], args->str[i+1] ? ' ' : '\n' );
295 if ((status = _spawnvp( _P_WAIT, args->str[0], args->str )))
297 if (status > 0) fatal_error( "%s failed with status %u\n", args->str[0], status );
298 else fatal_perror( "winebuild" );
299 exit( 1 );
303 /* find a build tool in the path, trying the various names */
304 struct strarray *find_tool( const char *name, const char * const *names )
306 static char **dirs;
307 static unsigned int count, maxlen;
309 char *p, *file;
310 const char *alt_names[2];
311 unsigned int i, len;
312 struct stat st;
314 if (!dirs)
316 char *path;
318 /* split the path in directories */
320 if (!getenv( "PATH" )) return NULL;
321 path = xstrdup( getenv( "PATH" ));
322 for (p = path, count = 2; *p; p++) if (*p == ':') count++;
323 dirs = xmalloc( count * sizeof(*dirs) );
324 count = 0;
325 dirs[count++] = p = path;
326 while (*p)
328 while (*p && *p != ':') p++;
329 if (!*p) break;
330 *p++ = 0;
331 dirs[count++] = p;
333 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
336 if (!names)
338 alt_names[0] = name;
339 alt_names[1] = NULL;
340 names = alt_names;
343 while (*names)
345 len = strlen(*names) + sizeof(EXEEXT) + 1;
346 if (target_alias)
347 len += strlen(target_alias) + 1;
348 file = xmalloc( maxlen + len );
350 for (i = 0; i < count; i++)
352 strcpy( file, dirs[i] );
353 p = file + strlen(file);
354 if (p == file) *p++ = '.';
355 if (p[-1] != '/') *p++ = '/';
356 if (target_alias)
358 strcpy( p, target_alias );
359 p += strlen(p);
360 *p++ = '-';
362 strcpy( p, *names );
363 strcat( p, EXEEXT );
365 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111))
366 return strarray_init( file );
368 free( file );
369 names++;
371 return NULL;
374 struct strarray *get_as_command(void)
376 struct strarray *args;
378 if (cc_command)
380 args = strarray_copy( cc_command );
381 strarray_add( args, "-xassembler", "-c", NULL );
382 if (force_pointer_size)
383 strarray_add_one( args, (force_pointer_size == 8) ? "-m64" : "-m32" );
384 return args;
387 if (!as_command)
389 static const char * const commands[] = { "gas", "as", NULL };
390 as_command = find_tool( "as", commands );
393 if (!as_command)
394 fatal_error( "cannot find suitable assembler\n" );
396 args = strarray_copy( as_command );
398 if (force_pointer_size)
400 switch (target_platform)
402 case PLATFORM_APPLE:
403 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
404 break;
405 default:
406 switch(target_cpu)
408 case CPU_POWERPC:
409 strarray_add_one( args, (force_pointer_size == 8) ? "-a64" : "-a32" );
410 break;
411 default:
412 strarray_add_one( args, (force_pointer_size == 8) ? "--64" : "--32" );
413 break;
415 break;
419 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
420 return args;
423 struct strarray *get_ld_command(void)
425 struct strarray *args;
427 if (!ld_command)
429 static const char * const commands[] = { "ld", "gld", NULL };
430 ld_command = find_tool( "ld", commands );
433 if (!ld_command)
434 fatal_error( "cannot find suitable linker\n" );
436 args = strarray_copy( ld_command );
438 if (force_pointer_size)
440 switch (target_platform)
442 case PLATFORM_APPLE:
443 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
444 break;
445 case PLATFORM_FREEBSD:
446 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
447 break;
448 default:
449 switch(target_cpu)
451 case CPU_POWERPC:
452 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
453 break;
454 default:
455 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
456 break;
458 break;
461 return args;
464 const char *get_nm_command(void)
466 if (!nm_command)
468 static const char * const commands[] = { "nm", "gnm", NULL };
469 nm_command = find_tool( "nm", commands );
472 if (!nm_command)
473 fatal_error( "cannot find suitable name lister\n" );
474 if (nm_command->count > 1)
475 fatal_error( "multiple arguemnts in nm command not supported yet\n" );
476 return nm_command->str[0];
479 /* get a name for a temp file, automatically cleaned up on exit */
480 char *get_temp_file_name( const char *prefix, const char *suffix )
482 char *name;
483 const char *ext, *basename;
484 int fd;
486 if (!prefix || !prefix[0]) prefix = "winebuild";
487 if (!suffix) suffix = "";
488 if ((basename = strrchr( prefix, '/' ))) basename++;
489 else basename = prefix;
490 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
491 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
492 memcpy( name, prefix, ext - prefix );
493 strcpy( name + (ext - prefix), ".XXXXXX" );
494 strcat( name, suffix );
496 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
498 strcpy( name, "/tmp/" );
499 memcpy( name + 5, basename, ext - basename );
500 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
501 strcat( name, suffix );
502 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
503 fatal_error( "could not generate a temp file\n" );
506 close( fd );
507 if (nb_tmp_files >= max_tmp_files)
509 max_tmp_files = max( 2 * max_tmp_files, 8 );
510 tmp_files = xrealloc( tmp_files, max_tmp_files * sizeof(tmp_files[0]) );
512 tmp_files[nb_tmp_files++] = name;
513 return name;
516 /*******************************************************************
517 * buffer management
519 * Function for reading from/writing to a memory buffer.
522 int byte_swapped = 0;
523 const char *input_buffer_filename;
524 const unsigned char *input_buffer;
525 size_t input_buffer_pos;
526 size_t input_buffer_size;
527 unsigned char *output_buffer;
528 size_t output_buffer_pos;
529 size_t output_buffer_size;
531 static void check_output_buffer_space( size_t size )
533 if (output_buffer_pos + size >= output_buffer_size)
535 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
536 output_buffer = xrealloc( output_buffer, output_buffer_size );
540 void init_input_buffer( const char *file )
542 int fd;
543 struct stat st;
545 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
546 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
547 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
548 #ifdef HAVE_MMAP
549 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
550 #endif
552 unsigned char *buffer = xmalloc( st.st_size );
553 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
554 input_buffer = buffer;
556 close( fd );
557 input_buffer_filename = xstrdup( file );
558 input_buffer_size = st.st_size;
559 input_buffer_pos = 0;
560 byte_swapped = 0;
563 void init_output_buffer(void)
565 output_buffer_size = 1024;
566 output_buffer_pos = 0;
567 output_buffer = xmalloc( output_buffer_size );
570 void flush_output_buffer(void)
572 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
573 fatal_error( "Error writing to %s\n", output_file_name );
574 free( output_buffer );
577 unsigned char get_byte(void)
579 if (input_buffer_pos >= input_buffer_size)
580 fatal_error( "%s is a truncated file\n", input_buffer_filename );
581 return input_buffer[input_buffer_pos++];
584 unsigned short get_word(void)
586 unsigned short ret;
588 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
589 fatal_error( "%s is a truncated file\n", input_buffer_filename );
590 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
591 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
592 input_buffer_pos += sizeof(ret);
593 return ret;
596 unsigned int get_dword(void)
598 unsigned int ret;
600 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
601 fatal_error( "%s is a truncated file\n", input_buffer_filename );
602 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
603 if (byte_swapped)
604 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
605 input_buffer_pos += sizeof(ret);
606 return ret;
609 void put_data( const void *data, size_t size )
611 check_output_buffer_space( size );
612 memcpy( output_buffer + output_buffer_pos, data, size );
613 output_buffer_pos += size;
616 void put_byte( unsigned char val )
618 check_output_buffer_space( 1 );
619 output_buffer[output_buffer_pos++] = val;
622 void put_word( unsigned short val )
624 if (byte_swapped) val = (val << 8) | (val >> 8);
625 put_data( &val, sizeof(val) );
628 void put_dword( unsigned int val )
630 if (byte_swapped)
631 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
632 put_data( &val, sizeof(val) );
635 void put_qword( unsigned int val )
637 if (byte_swapped)
639 put_dword( 0 );
640 put_dword( val );
642 else
644 put_dword( val );
645 put_dword( 0 );
649 /* pointer-sized word */
650 void put_pword( unsigned int val )
652 if (get_ptr_size() == 8) put_qword( val );
653 else put_dword( val );
656 void align_output( unsigned int align )
658 size_t size = align - (output_buffer_pos % align);
660 if (size == align) return;
661 check_output_buffer_space( size );
662 memset( output_buffer + output_buffer_pos, 0, size );
663 output_buffer_pos += size;
666 /* output a standard header for generated files */
667 void output_standard_file_header(void)
669 if (spec_file_name)
670 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
671 else
672 output( "/* File generated automatically; do not edit! */\n" );
673 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
676 /* dump a byte stream into the assembly code */
677 void dump_bytes( const void *buffer, unsigned int size )
679 unsigned int i;
680 const unsigned char *ptr = buffer;
682 if (!size) return;
683 output( "\t.byte " );
684 for (i = 0; i < size - 1; i++, ptr++)
686 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
687 else output( "0x%02x,", *ptr );
689 output( "0x%02x\n", *ptr );
693 /*******************************************************************
694 * open_input_file
696 * Open a file in the given srcdir and set the input_file_name global variable.
698 FILE *open_input_file( const char *srcdir, const char *name )
700 char *fullname;
701 FILE *file = fopen( name, "r" );
703 if (!file && srcdir)
705 fullname = strmake( "%s/%s", srcdir, name );
706 file = fopen( fullname, "r" );
708 else fullname = xstrdup( name );
710 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
711 input_file_name = fullname;
712 current_line = 1;
713 return file;
717 /*******************************************************************
718 * close_input_file
720 * Close the current input file (must have been opened with open_input_file).
722 void close_input_file( FILE *file )
724 fclose( file );
725 free( input_file_name );
726 input_file_name = NULL;
727 current_line = 0;
731 /*******************************************************************
732 * remove_stdcall_decoration
734 * Remove a possible @xx suffix from a function name.
735 * Return the numerical value of the suffix, or -1 if none.
737 int remove_stdcall_decoration( char *name )
739 char *p, *end = strrchr( name, '@' );
740 if (!end || !end[1] || end == name) return -1;
741 if (target_cpu != CPU_x86) return -1;
742 /* make sure all the rest is digits */
743 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
744 *end = 0;
745 return atoi( end + 1 );
749 /*******************************************************************
750 * assemble_file
752 * Run a file through the assembler.
754 void assemble_file( const char *src_file, const char *obj_file )
756 struct strarray *args = get_as_command();
757 strarray_add( args, "-o", obj_file, src_file, NULL );
758 spawn( args );
759 strarray_free( args );
763 /*******************************************************************
764 * alloc_dll_spec
766 * Create a new dll spec file descriptor
768 DLLSPEC *alloc_dll_spec(void)
770 DLLSPEC *spec;
772 spec = xmalloc( sizeof(*spec) );
773 spec->file_name = NULL;
774 spec->dll_name = NULL;
775 spec->init_func = NULL;
776 spec->main_module = NULL;
777 spec->type = SPEC_WIN32;
778 spec->base = MAX_ORDINALS;
779 spec->limit = 0;
780 spec->stack_size = 0;
781 spec->heap_size = 0;
782 spec->nb_entry_points = 0;
783 spec->alloc_entry_points = 0;
784 spec->nb_names = 0;
785 spec->nb_resources = 0;
786 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
787 if (get_ptr_size() > 4)
788 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
789 else
790 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
791 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
792 spec->subsystem = 0;
793 spec->subsystem_major = 4;
794 spec->subsystem_minor = 0;
795 spec->entry_points = NULL;
796 spec->names = NULL;
797 spec->ordinals = NULL;
798 spec->resources = NULL;
799 return spec;
803 /*******************************************************************
804 * free_dll_spec
806 * Free dll spec file descriptor
808 void free_dll_spec( DLLSPEC *spec )
810 int i;
812 for (i = 0; i < spec->nb_entry_points; i++)
814 ORDDEF *odp = &spec->entry_points[i];
815 free( odp->name );
816 free( odp->export_name );
817 free( odp->link_name );
819 free( spec->file_name );
820 free( spec->dll_name );
821 free( spec->init_func );
822 free( spec->entry_points );
823 free( spec->names );
824 free( spec->ordinals );
825 free( spec->resources );
826 free( spec );
830 /*******************************************************************
831 * make_c_identifier
833 * Map a string to a valid C identifier.
835 const char *make_c_identifier( const char *str )
837 static char buffer[256];
838 char *p;
840 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
842 if (isalnum(*str)) *p = *str;
843 else *p = '_';
845 *p = 0;
846 return buffer;
850 /*******************************************************************
851 * get_stub_name
853 * Generate an internal name for a stub entry point.
855 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
857 static char *buffer;
859 free( buffer );
860 if (odp->name || odp->export_name)
862 char *p;
863 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
864 /* make sure name is a legal C identifier */
865 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
866 if (!*p) return buffer;
867 free( buffer );
869 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
870 return buffer;
873 /* parse a cpu name and return the corresponding value */
874 enum target_cpu get_cpu_from_name( const char *name )
876 unsigned int i;
878 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
879 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
880 return -1;
883 /*****************************************************************
884 * Function: get_alignment
886 * Description:
887 * According to the info page for gas, the .align directive behaves
888 * differently on different systems. On some architectures, the
889 * argument of a .align directive is the number of bytes to pad to, so
890 * to align on an 8-byte boundary you'd say
891 * .align 8
892 * On other systems, the argument is "the number of low-order zero bits
893 * that the location counter must have after advancement." So to
894 * align on an 8-byte boundary you'd say
895 * .align 3
897 * The reason gas is written this way is that it's trying to mimick
898 * native assemblers for the various architectures it runs on. gas
899 * provides other directives that work consistently across
900 * architectures, but of course we want to work on all arches with or
901 * without gas. Hence this function.
904 * Parameters:
905 * align -- the number of bytes to align to. Must be a power of 2.
907 unsigned int get_alignment(unsigned int align)
909 unsigned int n;
911 assert( !(align & (align - 1)) );
913 switch(target_cpu)
915 case CPU_x86:
916 case CPU_x86_64:
917 if (target_platform != PLATFORM_APPLE) return align;
918 /* fall through */
919 case CPU_POWERPC:
920 case CPU_ARM:
921 case CPU_ARM64:
922 n = 0;
923 while ((1u << n) != align) n++;
924 return n;
926 /* unreached */
927 assert(0);
928 return 0;
931 /* return the page size for the target CPU */
932 unsigned int get_page_size(void)
934 switch(target_cpu)
936 case CPU_x86: return 4096;
937 case CPU_x86_64: return 4096;
938 case CPU_POWERPC: return 4096;
939 case CPU_ARM: return 4096;
940 case CPU_ARM64: return 4096;
942 /* unreached */
943 assert(0);
944 return 0;
947 /* return the size of a pointer on the target CPU */
948 unsigned int get_ptr_size(void)
950 switch(target_cpu)
952 case CPU_x86:
953 case CPU_POWERPC:
954 case CPU_ARM:
955 return 4;
956 case CPU_x86_64:
957 case CPU_ARM64:
958 return 8;
960 /* unreached */
961 assert(0);
962 return 0;
965 /* return the total size in bytes of the arguments on the stack */
966 unsigned int get_args_size( const ORDDEF *odp )
968 int i, size;
970 for (i = size = 0; i < odp->u.func.nb_args; i++)
972 switch (odp->u.func.args[i])
974 case ARG_INT64:
975 case ARG_DOUBLE:
976 size += 8;
977 break;
978 case ARG_INT128:
979 /* int128 is passed as pointer on x86_64 */
980 if (target_cpu != CPU_x86_64)
982 size += 16;
983 break;
985 /* fall through */
986 default:
987 size += get_ptr_size();
988 break;
991 return size;
994 /* return the assembly name for a C symbol */
995 const char *asm_name( const char *sym )
997 static char *buffer;
999 switch (target_platform)
1001 case PLATFORM_APPLE:
1002 case PLATFORM_WINDOWS:
1003 if (sym[0] == '.' && sym[1] == 'L') return sym;
1004 free( buffer );
1005 buffer = strmake( "_%s", sym );
1006 return buffer;
1007 default:
1008 return sym;
1012 /* return an assembly function declaration for a C function name */
1013 const char *func_declaration( const char *func )
1015 static char *buffer;
1017 switch (target_platform)
1019 case PLATFORM_APPLE:
1020 return "";
1021 case PLATFORM_WINDOWS:
1022 free( buffer );
1023 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
1024 break;
1025 default:
1026 free( buffer );
1027 switch(target_cpu)
1029 case CPU_ARM:
1030 case CPU_ARM64:
1031 buffer = strmake( ".type %s,%%function", func );
1032 break;
1033 default:
1034 buffer = strmake( ".type %s,@function", func );
1035 break;
1037 break;
1039 return buffer;
1042 /* output a size declaration for an assembly function */
1043 void output_function_size( const char *name )
1045 switch (target_platform)
1047 case PLATFORM_APPLE:
1048 case PLATFORM_WINDOWS:
1049 break;
1050 default:
1051 output( "\t.size %s, .-%s\n", name, name );
1052 break;
1056 /* output a .cfi directive */
1057 void output_cfi( const char *format, ... )
1059 va_list valist;
1061 if (!unwind_tables) return;
1062 va_start( valist, format );
1063 fputc( '\t', output_file );
1064 vfprintf( output_file, format, valist );
1065 fputc( '\n', output_file );
1066 va_end( valist );
1069 /* output the GNU note for non-exec stack */
1070 void output_gnu_stack_note(void)
1072 switch (target_platform)
1074 case PLATFORM_WINDOWS:
1075 case PLATFORM_APPLE:
1076 break;
1077 default:
1078 switch(target_cpu)
1080 case CPU_ARM:
1081 case CPU_ARM64:
1082 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1083 break;
1084 default:
1085 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1086 break;
1088 break;
1092 /* return a global symbol declaration for an assembly symbol */
1093 const char *asm_globl( const char *func )
1095 static char *buffer;
1097 free( buffer );
1098 switch (target_platform)
1100 case PLATFORM_APPLE:
1101 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1102 break;
1103 case PLATFORM_WINDOWS:
1104 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1105 break;
1106 default:
1107 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1108 break;
1110 return buffer;
1113 const char *get_asm_ptr_keyword(void)
1115 switch(get_ptr_size())
1117 case 4: return ".long";
1118 case 8: return ".quad";
1120 assert(0);
1121 return NULL;
1124 const char *get_asm_string_keyword(void)
1126 switch (target_platform)
1128 case PLATFORM_APPLE:
1129 return ".asciz";
1130 default:
1131 return ".string";
1135 const char *get_asm_rodata_section(void)
1137 switch (target_platform)
1139 case PLATFORM_APPLE: return ".const";
1140 default: return ".section .rodata";
1144 const char *get_asm_string_section(void)
1146 switch (target_platform)
1148 case PLATFORM_APPLE: return ".cstring";
1149 default: return ".section .rodata";