wmiutils: Add support for for parsing and serializing a key list.
[wine.git] / tools / winebuild / utils.c
blob262ff3a0c03a71872cb3c9003383ad3ff928d52b
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 { "sparc", CPU_SPARC },
60 { "powerpc", CPU_POWERPC },
61 { "arm", CPU_ARM },
62 { "arm64", CPU_ARM64 },
63 { "aarch64", CPU_ARM64 },
66 /* atexit handler to clean tmp files */
67 static void cleanup_tmp_files(void)
69 unsigned int i;
70 for (i = 0; i < nb_tmp_files; i++) if (tmp_files[i]) unlink( tmp_files[i] );
74 void *xmalloc (size_t size)
76 void *res;
78 res = malloc (size ? size : 1);
79 if (res == NULL)
81 fprintf (stderr, "Virtual memory exhausted.\n");
82 exit (1);
84 return res;
87 void *xrealloc (void *ptr, size_t size)
89 void *res = realloc (ptr, size);
90 if (size && res == NULL)
92 fprintf (stderr, "Virtual memory exhausted.\n");
93 exit (1);
95 return res;
98 char *xstrdup( const char *str )
100 char *res = strdup( str );
101 if (!res)
103 fprintf (stderr, "Virtual memory exhausted.\n");
104 exit (1);
106 return res;
109 char *strupper(char *s)
111 char *p;
112 for (p = s; *p; p++) *p = toupper(*p);
113 return s;
116 int strendswith(const char* str, const char* end)
118 int l = strlen(str);
119 int m = strlen(end);
120 return l >= m && strcmp(str + l - m, end) == 0;
123 char *strmake( const char* fmt, ... )
125 int n;
126 size_t size = 100;
127 va_list ap;
129 for (;;)
131 char *p = xmalloc( size );
132 va_start( ap, fmt );
133 n = vsnprintf( p, size, fmt, ap );
134 va_end( ap );
135 if (n == -1) size *= 2;
136 else if ((size_t)n >= size) size = n + 1;
137 else return p;
138 free( p );
142 struct strarray *strarray_init(void)
144 struct strarray *array = xmalloc( sizeof(*array) );
145 array->count = 0;
146 array->max = 16;
147 array->str = xmalloc( array->max * sizeof(*array->str) );
148 return array;
151 static void strarray_add_one( struct strarray *array, const char *str )
153 if (array->count == array->max)
155 array->max *= 2;
156 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
158 array->str[array->count++] = str;
161 void strarray_add( struct strarray *array, ... )
163 va_list valist;
164 const char *str;
166 va_start( valist, array );
167 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
168 va_end( valist );
171 void strarray_addv( struct strarray *array, char * const *argv )
173 while (*argv) strarray_add_one( array, *argv++ );
176 void strarray_free( struct strarray *array )
178 free( array->str );
179 free( array );
182 void fatal_error( const char *msg, ... )
184 va_list valist;
185 va_start( valist, msg );
186 if (input_file_name)
188 fprintf( stderr, "%s:", input_file_name );
189 if (current_line)
190 fprintf( stderr, "%d:", current_line );
191 fputc( ' ', stderr );
193 else fprintf( stderr, "winebuild: " );
194 vfprintf( stderr, msg, valist );
195 va_end( valist );
196 exit(1);
199 void fatal_perror( const char *msg, ... )
201 va_list valist;
202 va_start( valist, msg );
203 if (input_file_name)
205 fprintf( stderr, "%s:", input_file_name );
206 if (current_line)
207 fprintf( stderr, "%d:", current_line );
208 fputc( ' ', stderr );
210 vfprintf( stderr, msg, valist );
211 perror( " " );
212 va_end( valist );
213 exit(1);
216 void error( const char *msg, ... )
218 va_list valist;
219 va_start( valist, msg );
220 if (input_file_name)
222 fprintf( stderr, "%s:", input_file_name );
223 if (current_line)
224 fprintf( stderr, "%d:", current_line );
225 fputc( ' ', stderr );
227 vfprintf( stderr, msg, valist );
228 va_end( valist );
229 nb_errors++;
232 void warning( const char *msg, ... )
234 va_list valist;
236 if (!display_warnings) return;
237 va_start( valist, msg );
238 if (input_file_name)
240 fprintf( stderr, "%s:", input_file_name );
241 if (current_line)
242 fprintf( stderr, "%d:", current_line );
243 fputc( ' ', stderr );
245 fprintf( stderr, "warning: " );
246 vfprintf( stderr, msg, valist );
247 va_end( valist );
250 int output( const char *format, ... )
252 int ret;
253 va_list valist;
255 va_start( valist, format );
256 ret = vfprintf( output_file, format, valist );
257 va_end( valist );
258 if (ret < 0) fatal_perror( "Output error" );
259 return ret;
262 void spawn( struct strarray *args )
264 unsigned int i;
265 int status;
267 strarray_add_one( args, NULL );
268 if (verbose)
269 for (i = 0; args->str[i]; i++)
270 fprintf( stderr, "%s%c", args->str[i], args->str[i+1] ? ' ' : '\n' );
272 if ((status = spawnvp( _P_WAIT, args->str[0], args->str )))
274 if (status > 0) fatal_error( "%s failed with status %u\n", args->str[0], status );
275 else fatal_perror( "winebuild" );
276 exit( 1 );
280 /* find a build tool in the path, trying the various names */
281 char *find_tool( const char *name, const char * const *names )
283 static char **dirs;
284 static unsigned int count, maxlen;
286 char *p, *file;
287 const char *alt_names[2];
288 unsigned int i, len;
289 struct stat st;
291 if (!dirs)
293 char *path;
295 /* split the path in directories */
297 if (!getenv( "PATH" )) return NULL;
298 path = xstrdup( getenv( "PATH" ));
299 for (p = path, count = 2; *p; p++) if (*p == ':') count++;
300 dirs = xmalloc( count * sizeof(*dirs) );
301 count = 0;
302 dirs[count++] = p = path;
303 while (*p)
305 while (*p && *p != ':') p++;
306 if (!*p) break;
307 *p++ = 0;
308 dirs[count++] = p;
310 for (i = 0; i < count; i++) maxlen = max( maxlen, strlen(dirs[i])+2 );
313 if (!names)
315 alt_names[0] = name;
316 alt_names[1] = NULL;
317 names = alt_names;
320 while (*names)
322 len = strlen(*names) + sizeof(EXEEXT) + 1;
323 if (target_alias)
324 len += strlen(target_alias) + 1;
325 file = xmalloc( maxlen + len );
327 for (i = 0; i < count; i++)
329 strcpy( file, dirs[i] );
330 p = file + strlen(file);
331 if (p == file) *p++ = '.';
332 if (p[-1] != '/') *p++ = '/';
333 if (target_alias)
335 strcpy( p, target_alias );
336 p += strlen(p);
337 *p++ = '-';
339 strcpy( p, *names );
340 strcat( p, EXEEXT );
342 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
344 free( file );
345 names++;
347 return NULL;
350 struct strarray *get_as_command(void)
352 static int as_is_clang = 0;
353 struct strarray *args = strarray_init();
355 if (!as_command)
357 as_command = find_tool( "clang", NULL );
358 if (as_command) as_is_clang = 1;
361 if (!as_command)
363 static const char * const commands[] = { "gas", "as", NULL };
364 as_command = find_tool( "as", commands );
367 if (!as_command)
368 fatal_error( "cannot find suitable assembler\n" );
370 strarray_add_one( args, as_command );
372 if (as_is_clang)
374 strarray_add( args, "-xassembler", "-c", NULL );
375 if (force_pointer_size)
376 strarray_add_one( args, (force_pointer_size == 8) ? "-m64" : "-m32" );
378 else if (force_pointer_size)
380 switch (target_platform)
382 case PLATFORM_APPLE:
383 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
384 break;
385 default:
386 switch(target_cpu)
388 case CPU_POWERPC:
389 strarray_add_one( args, (force_pointer_size == 8) ? "-a64" : "-a32" );
390 break;
391 default:
392 strarray_add_one( args, (force_pointer_size == 8) ? "--64" : "--32" );
393 break;
395 break;
399 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
400 return args;
403 struct strarray *get_ld_command(void)
405 struct strarray *args = strarray_init();
407 if (!ld_command)
409 static const char * const commands[] = { "ld", "gld", NULL };
410 ld_command = find_tool( "ld", commands );
413 if (!ld_command)
414 fatal_error( "cannot find suitable linker\n" );
416 strarray_add_one( args, ld_command );
418 if (force_pointer_size)
420 switch (target_platform)
422 case PLATFORM_APPLE:
423 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
424 break;
425 case PLATFORM_FREEBSD:
426 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
427 break;
428 default:
429 switch(target_cpu)
431 case CPU_POWERPC:
432 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
433 break;
434 default:
435 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
436 break;
438 break;
441 return args;
444 const char *get_nm_command(void)
446 if (!nm_command)
448 static const char * const commands[] = { "nm", "gnm", NULL };
449 nm_command = find_tool( "nm", commands );
452 if (!nm_command)
453 fatal_error( "cannot find suitable name lister\n" );
454 return nm_command;
457 /* get a name for a temp file, automatically cleaned up on exit */
458 char *get_temp_file_name( const char *prefix, const char *suffix )
460 char *name;
461 const char *ext, *basename;
462 int fd;
464 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
466 if (!prefix || !prefix[0]) prefix = "winebuild";
467 if (!suffix) suffix = "";
468 if ((basename = strrchr( prefix, '/' ))) basename++;
469 else basename = prefix;
470 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
471 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
472 memcpy( name, prefix, ext - prefix );
473 strcpy( name + (ext - prefix), ".XXXXXX" );
474 strcat( name, suffix );
476 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
478 strcpy( name, "/tmp/" );
479 memcpy( name + 5, basename, ext - basename );
480 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
481 strcat( name, suffix );
482 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
483 fatal_error( "could not generate a temp file\n" );
486 close( fd );
487 if (nb_tmp_files >= max_tmp_files)
489 max_tmp_files = max( 2 * max_tmp_files, 8 );
490 tmp_files = xrealloc( tmp_files, max_tmp_files * sizeof(tmp_files[0]) );
492 tmp_files[nb_tmp_files++] = name;
493 return name;
496 /*******************************************************************
497 * buffer management
499 * Function for reading from/writing to a memory buffer.
502 int byte_swapped = 0;
503 const char *input_buffer_filename;
504 const unsigned char *input_buffer;
505 size_t input_buffer_pos;
506 size_t input_buffer_size;
507 unsigned char *output_buffer;
508 size_t output_buffer_pos;
509 size_t output_buffer_size;
511 static void check_output_buffer_space( size_t size )
513 if (output_buffer_pos + size >= output_buffer_size)
515 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
516 output_buffer = xrealloc( output_buffer, output_buffer_size );
520 void init_input_buffer( const char *file )
522 int fd;
523 struct stat st;
525 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
526 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
527 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
528 #ifdef HAVE_MMAP
529 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
530 #endif
532 unsigned char *buffer = xmalloc( st.st_size );
533 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
534 input_buffer = buffer;
536 close( fd );
537 input_buffer_filename = xstrdup( file );
538 input_buffer_size = st.st_size;
539 input_buffer_pos = 0;
540 byte_swapped = 0;
543 void init_output_buffer(void)
545 output_buffer_size = 1024;
546 output_buffer_pos = 0;
547 output_buffer = xmalloc( output_buffer_size );
550 void flush_output_buffer(void)
552 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
553 fatal_error( "Error writing to %s\n", output_file_name );
554 free( output_buffer );
557 unsigned char get_byte(void)
559 if (input_buffer_pos >= input_buffer_size)
560 fatal_error( "%s is a truncated file\n", input_buffer_filename );
561 return input_buffer[input_buffer_pos++];
564 unsigned short get_word(void)
566 unsigned short ret;
568 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
569 fatal_error( "%s is a truncated file\n", input_buffer_filename );
570 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
571 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
572 input_buffer_pos += sizeof(ret);
573 return ret;
576 unsigned int get_dword(void)
578 unsigned int ret;
580 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
581 fatal_error( "%s is a truncated file\n", input_buffer_filename );
582 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
583 if (byte_swapped)
584 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
585 input_buffer_pos += sizeof(ret);
586 return ret;
589 void put_data( const void *data, size_t size )
591 check_output_buffer_space( size );
592 memcpy( output_buffer + output_buffer_pos, data, size );
593 output_buffer_pos += size;
596 void put_byte( unsigned char val )
598 check_output_buffer_space( 1 );
599 output_buffer[output_buffer_pos++] = val;
602 void put_word( unsigned short val )
604 if (byte_swapped) val = (val << 8) | (val >> 8);
605 put_data( &val, sizeof(val) );
608 void put_dword( unsigned int val )
610 if (byte_swapped)
611 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
612 put_data( &val, sizeof(val) );
615 void put_qword( unsigned int val )
617 if (byte_swapped)
619 put_dword( 0 );
620 put_dword( val );
622 else
624 put_dword( val );
625 put_dword( 0 );
629 /* pointer-sized word */
630 void put_pword( unsigned int val )
632 if (get_ptr_size() == 8) put_qword( val );
633 else put_dword( val );
636 void align_output( unsigned int align )
638 size_t size = align - (output_buffer_pos % align);
640 if (size == align) return;
641 check_output_buffer_space( size );
642 memset( output_buffer + output_buffer_pos, 0, size );
643 output_buffer_pos += size;
646 /* output a standard header for generated files */
647 void output_standard_file_header(void)
649 if (spec_file_name)
650 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
651 else
652 output( "/* File generated automatically; do not edit! */\n" );
653 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
656 /* dump a byte stream into the assembly code */
657 void dump_bytes( const void *buffer, unsigned int size )
659 unsigned int i;
660 const unsigned char *ptr = buffer;
662 if (!size) return;
663 output( "\t.byte " );
664 for (i = 0; i < size - 1; i++, ptr++)
666 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
667 else output( "0x%02x,", *ptr );
669 output( "0x%02x\n", *ptr );
673 /*******************************************************************
674 * open_input_file
676 * Open a file in the given srcdir and set the input_file_name global variable.
678 FILE *open_input_file( const char *srcdir, const char *name )
680 char *fullname;
681 FILE *file = fopen( name, "r" );
683 if (!file && srcdir)
685 fullname = strmake( "%s/%s", srcdir, name );
686 file = fopen( fullname, "r" );
688 else fullname = xstrdup( name );
690 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
691 input_file_name = fullname;
692 current_line = 1;
693 return file;
697 /*******************************************************************
698 * close_input_file
700 * Close the current input file (must have been opened with open_input_file).
702 void close_input_file( FILE *file )
704 fclose( file );
705 free( input_file_name );
706 input_file_name = NULL;
707 current_line = 0;
711 /*******************************************************************
712 * remove_stdcall_decoration
714 * Remove a possible @xx suffix from a function name.
715 * Return the numerical value of the suffix, or -1 if none.
717 int remove_stdcall_decoration( char *name )
719 char *p, *end = strrchr( name, '@' );
720 if (!end || !end[1] || end == name) return -1;
721 if (target_cpu != CPU_x86) return -1;
722 /* make sure all the rest is digits */
723 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
724 *end = 0;
725 return atoi( end + 1 );
729 /*******************************************************************
730 * assemble_file
732 * Run a file through the assembler.
734 void assemble_file( const char *src_file, const char *obj_file )
736 struct strarray *args = get_as_command();
737 strarray_add( args, "-o", obj_file, src_file, NULL );
738 spawn( args );
739 strarray_free( args );
743 /*******************************************************************
744 * alloc_dll_spec
746 * Create a new dll spec file descriptor
748 DLLSPEC *alloc_dll_spec(void)
750 DLLSPEC *spec;
752 spec = xmalloc( sizeof(*spec) );
753 spec->file_name = NULL;
754 spec->dll_name = NULL;
755 spec->init_func = NULL;
756 spec->main_module = NULL;
757 spec->type = SPEC_WIN32;
758 spec->base = MAX_ORDINALS;
759 spec->limit = 0;
760 spec->stack_size = 0;
761 spec->heap_size = 0;
762 spec->nb_entry_points = 0;
763 spec->alloc_entry_points = 0;
764 spec->nb_names = 0;
765 spec->nb_resources = 0;
766 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
767 if (get_ptr_size() > 4)
768 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
769 else
770 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
771 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
772 spec->subsystem = 0;
773 spec->subsystem_major = 4;
774 spec->subsystem_minor = 0;
775 spec->entry_points = NULL;
776 spec->names = NULL;
777 spec->ordinals = NULL;
778 spec->resources = NULL;
779 return spec;
783 /*******************************************************************
784 * free_dll_spec
786 * Free dll spec file descriptor
788 void free_dll_spec( DLLSPEC *spec )
790 int i;
792 for (i = 0; i < spec->nb_entry_points; i++)
794 ORDDEF *odp = &spec->entry_points[i];
795 free( odp->name );
796 free( odp->export_name );
797 free( odp->link_name );
799 free( spec->file_name );
800 free( spec->dll_name );
801 free( spec->init_func );
802 free( spec->entry_points );
803 free( spec->names );
804 free( spec->ordinals );
805 free( spec->resources );
806 free( spec );
810 /*******************************************************************
811 * make_c_identifier
813 * Map a string to a valid C identifier.
815 const char *make_c_identifier( const char *str )
817 static char buffer[256];
818 char *p;
820 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
822 if (isalnum(*str)) *p = *str;
823 else *p = '_';
825 *p = 0;
826 return buffer;
830 /*******************************************************************
831 * get_stub_name
833 * Generate an internal name for a stub entry point.
835 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
837 static char *buffer;
839 free( buffer );
840 if (odp->name || odp->export_name)
842 char *p;
843 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
844 /* make sure name is a legal C identifier */
845 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
846 if (!*p) return buffer;
847 free( buffer );
849 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
850 return buffer;
853 /* parse a cpu name and return the corresponding value */
854 enum target_cpu get_cpu_from_name( const char *name )
856 unsigned int i;
858 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
859 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
860 return -1;
863 /*****************************************************************
864 * Function: get_alignment
866 * Description:
867 * According to the info page for gas, the .align directive behaves
868 * differently on different systems. On some architectures, the
869 * argument of a .align directive is the number of bytes to pad to, so
870 * to align on an 8-byte boundary you'd say
871 * .align 8
872 * On other systems, the argument is "the number of low-order zero bits
873 * that the location counter must have after advancement." So to
874 * align on an 8-byte boundary you'd say
875 * .align 3
877 * The reason gas is written this way is that it's trying to mimick
878 * native assemblers for the various architectures it runs on. gas
879 * provides other directives that work consistently across
880 * architectures, but of course we want to work on all arches with or
881 * without gas. Hence this function.
884 * Parameters:
885 * align -- the number of bytes to align to. Must be a power of 2.
887 unsigned int get_alignment(unsigned int align)
889 unsigned int n;
891 assert( !(align & (align - 1)) );
893 switch(target_cpu)
895 case CPU_x86:
896 case CPU_x86_64:
897 case CPU_SPARC:
898 if (target_platform != PLATFORM_APPLE) return align;
899 /* fall through */
900 case CPU_POWERPC:
901 case CPU_ARM:
902 case CPU_ARM64:
903 n = 0;
904 while ((1u << n) != align) n++;
905 return n;
907 /* unreached */
908 assert(0);
909 return 0;
912 /* return the page size for the target CPU */
913 unsigned int get_page_size(void)
915 switch(target_cpu)
917 case CPU_x86: return 4096;
918 case CPU_x86_64: return 4096;
919 case CPU_POWERPC: return 4096;
920 case CPU_ARM: return 4096;
921 case CPU_ARM64: return 4096;
922 case CPU_SPARC: return 8192;
924 /* unreached */
925 assert(0);
926 return 0;
929 /* return the size of a pointer on the target CPU */
930 unsigned int get_ptr_size(void)
932 switch(target_cpu)
934 case CPU_x86:
935 case CPU_POWERPC:
936 case CPU_SPARC:
937 case CPU_ARM:
938 return 4;
939 case CPU_x86_64:
940 case CPU_ARM64:
941 return 8;
943 /* unreached */
944 assert(0);
945 return 0;
948 /* return the total size in bytes of the arguments on the stack */
949 unsigned int get_args_size( const ORDDEF *odp )
951 int i, size;
953 for (i = size = 0; i < odp->u.func.nb_args; i++)
955 switch (odp->u.func.args[i])
957 case ARG_INT64:
958 case ARG_DOUBLE:
959 size += 8;
960 break;
961 case ARG_INT128:
962 /* int128 is passed as pointer on x86_64 */
963 if (target_cpu != CPU_x86_64)
965 size += 16;
966 break;
968 /* fall through */
969 default:
970 size += get_ptr_size();
971 break;
974 return size;
977 /* return the assembly name for a C symbol */
978 const char *asm_name( const char *sym )
980 static char *buffer;
982 switch (target_platform)
984 case PLATFORM_APPLE:
985 case PLATFORM_WINDOWS:
986 if (sym[0] == '.' && sym[1] == 'L') return sym;
987 free( buffer );
988 buffer = strmake( "_%s", sym );
989 return buffer;
990 default:
991 return sym;
995 /* return an assembly function declaration for a C function name */
996 const char *func_declaration( const char *func )
998 static char *buffer;
1000 switch (target_platform)
1002 case PLATFORM_APPLE:
1003 return "";
1004 case PLATFORM_WINDOWS:
1005 free( buffer );
1006 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
1007 break;
1008 default:
1009 free( buffer );
1010 switch(target_cpu)
1012 case CPU_ARM:
1013 case CPU_ARM64:
1014 buffer = strmake( ".type %s,%%function", func );
1015 break;
1016 default:
1017 buffer = strmake( ".type %s,@function", func );
1018 break;
1020 break;
1022 return buffer;
1025 /* output a size declaration for an assembly function */
1026 void output_function_size( const char *name )
1028 switch (target_platform)
1030 case PLATFORM_APPLE:
1031 case PLATFORM_WINDOWS:
1032 break;
1033 default:
1034 output( "\t.size %s, .-%s\n", name, name );
1035 break;
1039 /* output a .cfi directive */
1040 void output_cfi( const char *format, ... )
1042 va_list valist;
1044 if (!unwind_tables) return;
1045 va_start( valist, format );
1046 fputc( '\t', output_file );
1047 vfprintf( output_file, format, valist );
1048 fputc( '\n', output_file );
1049 va_end( valist );
1052 /* output the GNU note for non-exec stack */
1053 void output_gnu_stack_note(void)
1055 switch (target_platform)
1057 case PLATFORM_WINDOWS:
1058 case PLATFORM_APPLE:
1059 break;
1060 default:
1061 switch(target_cpu)
1063 case CPU_ARM:
1064 case CPU_ARM64:
1065 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1066 break;
1067 default:
1068 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1069 break;
1071 break;
1075 /* return a global symbol declaration for an assembly symbol */
1076 const char *asm_globl( const char *func )
1078 static char *buffer;
1080 free( buffer );
1081 switch (target_platform)
1083 case PLATFORM_APPLE:
1084 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1085 break;
1086 case PLATFORM_WINDOWS:
1087 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1088 break;
1089 default:
1090 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1091 break;
1093 return buffer;
1096 const char *get_asm_ptr_keyword(void)
1098 switch(get_ptr_size())
1100 case 4: return ".long";
1101 case 8: return ".quad";
1103 assert(0);
1104 return NULL;
1107 const char *get_asm_string_keyword(void)
1109 switch (target_platform)
1111 case PLATFORM_APPLE:
1112 return ".asciz";
1113 default:
1114 return ".string";
1118 const char *get_asm_short_keyword(void)
1120 switch (target_platform)
1122 default: return ".short";
1126 const char *get_asm_rodata_section(void)
1128 switch (target_platform)
1130 case PLATFORM_APPLE: return ".const";
1131 default: return ".section .rodata";
1135 const char *get_asm_string_section(void)
1137 switch (target_platform)
1139 case PLATFORM_APPLE: return ".cstring";
1140 default: return ".section .rodata";