comctl32: Use SetRect() instead of open coding it.
[wine.git] / tools / winebuild / utils.c
blob6e01f1a5268a63aabd1b61d75ca45142abc8b602
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 #if defined(_WIN32) && !defined(__CYGWIN__)
43 # define PATH_SEPARATOR ';'
44 #else
45 # define PATH_SEPARATOR ':'
46 #endif
48 static struct strarray tmp_files;
49 static struct strarray empty_strarray;
51 static const struct
53 const char *name;
54 enum target_cpu cpu;
55 } cpu_names[] =
57 { "i386", CPU_x86 },
58 { "i486", CPU_x86 },
59 { "i586", CPU_x86 },
60 { "i686", CPU_x86 },
61 { "i786", CPU_x86 },
62 { "amd64", CPU_x86_64 },
63 { "x86_64", CPU_x86_64 },
64 { "powerpc", CPU_POWERPC },
65 { "arm", CPU_ARM },
66 { "armv5", CPU_ARM },
67 { "armv6", CPU_ARM },
68 { "armv7", CPU_ARM },
69 { "arm64", CPU_ARM64 },
70 { "aarch64", CPU_ARM64 },
73 /* atexit handler to clean tmp files */
74 void cleanup_tmp_files(void)
76 unsigned int i;
77 for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
81 void *xmalloc (size_t size)
83 void *res;
85 res = malloc (size ? size : 1);
86 if (res == NULL)
88 fprintf (stderr, "Virtual memory exhausted.\n");
89 exit (1);
91 return res;
94 void *xrealloc (void *ptr, size_t size)
96 void *res = realloc (ptr, size);
97 if (size && res == NULL)
99 fprintf (stderr, "Virtual memory exhausted.\n");
100 exit (1);
102 return res;
105 char *xstrdup( const char *str )
107 char *res = strdup( str );
108 if (!res)
110 fprintf (stderr, "Virtual memory exhausted.\n");
111 exit (1);
113 return res;
116 char *strupper(char *s)
118 char *p;
119 for (p = s; *p; p++) *p = toupper(*p);
120 return s;
123 int strendswith(const char* str, const char* end)
125 int l = strlen(str);
126 int m = strlen(end);
127 return l >= m && strcmp(str + l - m, end) == 0;
130 char *strmake( const char* fmt, ... )
132 int n;
133 size_t size = 100;
134 va_list ap;
136 for (;;)
138 char *p = xmalloc( size );
139 va_start( ap, fmt );
140 n = vsnprintf( p, size, fmt, ap );
141 va_end( ap );
142 if (n == -1) size *= 2;
143 else if ((size_t)n >= size) size = n + 1;
144 else return p;
145 free( p );
149 static struct strarray strarray_copy( struct strarray src )
151 struct strarray array;
152 array.count = src.count;
153 array.max = src.max;
154 array.str = xmalloc( array.max * sizeof(*array.str) );
155 memcpy( array.str, src.str, array.count * sizeof(*array.str) );
156 return array;
159 static void strarray_add_one( struct strarray *array, const char *str )
161 if (array->count == array->max)
163 array->max *= 2;
164 if (array->max < 16) array->max = 16;
165 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
167 array->str[array->count++] = str;
170 void strarray_add( struct strarray *array, ... )
172 va_list valist;
173 const char *str;
175 va_start( valist, array );
176 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
177 va_end( valist );
180 void strarray_addv( struct strarray *array, char * const *argv )
182 while (*argv) strarray_add_one( array, *argv++ );
185 void strarray_addall( struct strarray *array, struct strarray args )
187 unsigned int i;
189 for (i = 0; i < args.count; i++) strarray_add_one( array, args.str[i] );
192 struct strarray strarray_fromstring( const char *str, const char *delim )
194 const char *tok;
195 struct strarray array = empty_strarray;
196 char *buf = xstrdup( str );
198 for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
199 strarray_add_one( &array, strdup( tok ));
201 free( buf );
202 return 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" )) fatal_error( "PATH not set, cannot find required tools\n" );
321 path = xstrdup( getenv( "PATH" ));
322 for (p = path, count = 2; *p; p++) if (*p == PATH_SEPARATOR) count++;
323 dirs = xmalloc( count * sizeof(*dirs) );
324 count = 0;
325 dirs[count++] = p = path;
326 while (*p)
328 while (*p && *p != PATH_SEPARATOR) 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))
367 struct strarray ret = empty_strarray;
368 strarray_add_one( &ret, file );
369 return ret;
372 free( file );
373 names++;
375 fatal_error( "cannot find the '%s' tool\n", name );
378 struct strarray get_as_command(void)
380 struct strarray args;
382 if (cc_command.count)
384 args = strarray_copy( cc_command );
385 strarray_add( &args, "-xassembler", "-c", NULL );
386 if (force_pointer_size)
387 strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
388 if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
389 if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
390 return args;
393 if (!as_command.count)
395 static const char * const commands[] = { "gas", "as", NULL };
396 as_command = find_tool( "as", commands );
399 args = strarray_copy( as_command );
401 if (force_pointer_size)
403 switch (target_platform)
405 case PLATFORM_APPLE:
406 strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
407 break;
408 default:
409 switch(target_cpu)
411 case CPU_POWERPC:
412 strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
413 break;
414 default:
415 strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
416 break;
418 break;
422 if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
423 return args;
426 struct strarray get_ld_command(void)
428 struct strarray args;
430 if (!ld_command.count)
432 static const char * const commands[] = { "ld", "gld", NULL };
433 ld_command = find_tool( "ld", commands );
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.count)
468 static const char * const commands[] = { "nm", "gnm", NULL };
469 nm_command = find_tool( "nm", commands );
471 if (nm_command.count > 1)
472 fatal_error( "multiple arguments in nm command not supported yet\n" );
473 return nm_command.str[0];
476 /* get a name for a temp file, automatically cleaned up on exit */
477 char *get_temp_file_name( const char *prefix, const char *suffix )
479 char *name;
480 const char *ext, *basename;
481 int fd;
483 if (!prefix || !prefix[0]) prefix = "winebuild";
484 if (!suffix) suffix = "";
485 if ((basename = strrchr( prefix, '/' ))) basename++;
486 else basename = prefix;
487 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
488 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
489 memcpy( name, prefix, ext - prefix );
490 strcpy( name + (ext - prefix), ".XXXXXX" );
491 strcat( name, suffix );
493 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
495 strcpy( name, "/tmp/" );
496 memcpy( name + 5, basename, ext - basename );
497 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
498 strcat( name, suffix );
499 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
500 fatal_error( "could not generate a temp file\n" );
503 close( fd );
504 strarray_add_one( &tmp_files, name );
505 return name;
508 /*******************************************************************
509 * buffer management
511 * Function for reading from/writing to a memory buffer.
514 int byte_swapped = 0;
515 const char *input_buffer_filename;
516 const unsigned char *input_buffer;
517 size_t input_buffer_pos;
518 size_t input_buffer_size;
519 unsigned char *output_buffer;
520 size_t output_buffer_pos;
521 size_t output_buffer_size;
523 static void check_output_buffer_space( size_t size )
525 if (output_buffer_pos + size >= output_buffer_size)
527 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
528 output_buffer = xrealloc( output_buffer, output_buffer_size );
532 void init_input_buffer( const char *file )
534 int fd;
535 struct stat st;
537 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
538 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
539 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
540 #ifdef HAVE_MMAP
541 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
542 #endif
544 unsigned char *buffer = xmalloc( st.st_size );
545 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
546 input_buffer = buffer;
548 close( fd );
549 input_buffer_filename = xstrdup( file );
550 input_buffer_size = st.st_size;
551 input_buffer_pos = 0;
552 byte_swapped = 0;
555 void init_output_buffer(void)
557 output_buffer_size = 1024;
558 output_buffer_pos = 0;
559 output_buffer = xmalloc( output_buffer_size );
562 void flush_output_buffer(void)
564 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
565 fatal_error( "Error writing to %s\n", output_file_name );
566 free( output_buffer );
569 unsigned char get_byte(void)
571 if (input_buffer_pos >= input_buffer_size)
572 fatal_error( "%s is a truncated file\n", input_buffer_filename );
573 return input_buffer[input_buffer_pos++];
576 unsigned short get_word(void)
578 unsigned short 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) ret = (ret << 8) | (ret >> 8);
584 input_buffer_pos += sizeof(ret);
585 return ret;
588 unsigned int get_dword(void)
590 unsigned int ret;
592 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
593 fatal_error( "%s is a truncated file\n", input_buffer_filename );
594 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
595 if (byte_swapped)
596 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
597 input_buffer_pos += sizeof(ret);
598 return ret;
601 void put_data( const void *data, size_t size )
603 check_output_buffer_space( size );
604 memcpy( output_buffer + output_buffer_pos, data, size );
605 output_buffer_pos += size;
608 void put_byte( unsigned char val )
610 check_output_buffer_space( 1 );
611 output_buffer[output_buffer_pos++] = val;
614 void put_word( unsigned short val )
616 if (byte_swapped) val = (val << 8) | (val >> 8);
617 put_data( &val, sizeof(val) );
620 void put_dword( unsigned int val )
622 if (byte_swapped)
623 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
624 put_data( &val, sizeof(val) );
627 void put_qword( unsigned int val )
629 if (byte_swapped)
631 put_dword( 0 );
632 put_dword( val );
634 else
636 put_dword( val );
637 put_dword( 0 );
641 /* pointer-sized word */
642 void put_pword( unsigned int val )
644 if (get_ptr_size() == 8) put_qword( val );
645 else put_dword( val );
648 void align_output( unsigned int align )
650 size_t size = align - (output_buffer_pos % align);
652 if (size == align) return;
653 check_output_buffer_space( size );
654 memset( output_buffer + output_buffer_pos, 0, size );
655 output_buffer_pos += size;
658 /* output a standard header for generated files */
659 void output_standard_file_header(void)
661 if (spec_file_name)
662 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
663 else
664 output( "/* File generated automatically; do not edit! */\n" );
665 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
668 /* dump a byte stream into the assembly code */
669 void dump_bytes( const void *buffer, unsigned int size )
671 unsigned int i;
672 const unsigned char *ptr = buffer;
674 if (!size) return;
675 output( "\t.byte " );
676 for (i = 0; i < size - 1; i++, ptr++)
678 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
679 else output( "0x%02x,", *ptr );
681 output( "0x%02x\n", *ptr );
685 /*******************************************************************
686 * open_input_file
688 * Open a file in the given srcdir and set the input_file_name global variable.
690 FILE *open_input_file( const char *srcdir, const char *name )
692 char *fullname;
693 FILE *file = fopen( name, "r" );
695 if (!file && srcdir)
697 fullname = strmake( "%s/%s", srcdir, name );
698 file = fopen( fullname, "r" );
700 else fullname = xstrdup( name );
702 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
703 input_file_name = fullname;
704 current_line = 1;
705 return file;
709 /*******************************************************************
710 * close_input_file
712 * Close the current input file (must have been opened with open_input_file).
714 void close_input_file( FILE *file )
716 fclose( file );
717 free( input_file_name );
718 input_file_name = NULL;
719 current_line = 0;
723 /*******************************************************************
724 * remove_stdcall_decoration
726 * Remove a possible @xx suffix from a function name.
727 * Return the numerical value of the suffix, or -1 if none.
729 int remove_stdcall_decoration( char *name )
731 char *p, *end = strrchr( name, '@' );
732 if (!end || !end[1] || end == name) return -1;
733 if (target_cpu != CPU_x86) return -1;
734 /* make sure all the rest is digits */
735 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
736 *end = 0;
737 return atoi( end + 1 );
741 /*******************************************************************
742 * assemble_file
744 * Run a file through the assembler.
746 void assemble_file( const char *src_file, const char *obj_file )
748 struct strarray args = get_as_command();
749 strarray_add( &args, "-o", obj_file, src_file, NULL );
750 spawn( args );
754 /*******************************************************************
755 * alloc_dll_spec
757 * Create a new dll spec file descriptor
759 DLLSPEC *alloc_dll_spec(void)
761 DLLSPEC *spec;
763 spec = xmalloc( sizeof(*spec) );
764 memset( spec, 0, sizeof(*spec) );
765 spec->type = SPEC_WIN32;
766 spec->base = MAX_ORDINALS;
767 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
768 spec->subsystem = 0;
769 spec->subsystem_major = 4;
770 spec->subsystem_minor = 0;
771 if (get_ptr_size() > 4)
772 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
773 else
774 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
775 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
776 return spec;
780 /*******************************************************************
781 * free_dll_spec
783 * Free dll spec file descriptor
785 void free_dll_spec( DLLSPEC *spec )
787 int i;
789 for (i = 0; i < spec->nb_entry_points; i++)
791 ORDDEF *odp = &spec->entry_points[i];
792 free( odp->name );
793 free( odp->export_name );
794 free( odp->link_name );
796 free( spec->file_name );
797 free( spec->dll_name );
798 free( spec->c_name );
799 free( spec->init_func );
800 free( spec->entry_points );
801 free( spec->names );
802 free( spec->ordinals );
803 free( spec->resources );
804 free( spec );
808 /*******************************************************************
809 * make_c_identifier
811 * Map a string to a valid C identifier.
813 char *make_c_identifier( const char *str )
815 char *p, buffer[256];
817 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
819 if (isalnum(*str)) *p = *str;
820 else *p = '_';
822 *p = 0;
823 return xstrdup( buffer );
827 /*******************************************************************
828 * get_stub_name
830 * Generate an internal name for a stub entry point.
832 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
834 static char *buffer;
836 free( buffer );
837 if (odp->name || odp->export_name)
839 char *p;
840 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
841 /* make sure name is a legal C identifier */
842 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
843 if (!*p) return buffer;
844 free( buffer );
846 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
847 return buffer;
850 /* parse a cpu name and return the corresponding value */
851 int get_cpu_from_name( const char *name )
853 unsigned int i;
855 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
856 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
857 return -1;
860 /*****************************************************************
861 * Function: get_alignment
863 * Description:
864 * According to the info page for gas, the .align directive behaves
865 * differently on different systems. On some architectures, the
866 * argument of a .align directive is the number of bytes to pad to, so
867 * to align on an 8-byte boundary you'd say
868 * .align 8
869 * On other systems, the argument is "the number of low-order zero bits
870 * that the location counter must have after advancement." So to
871 * align on an 8-byte boundary you'd say
872 * .align 3
874 * The reason gas is written this way is that it's trying to mimic
875 * native assemblers for the various architectures it runs on. gas
876 * provides other directives that work consistently across
877 * architectures, but of course we want to work on all arches with or
878 * without gas. Hence this function.
881 * Parameters:
882 * align -- the number of bytes to align to. Must be a power of 2.
884 unsigned int get_alignment(unsigned int align)
886 unsigned int n;
888 assert( !(align & (align - 1)) );
890 switch(target_cpu)
892 case CPU_x86:
893 case CPU_x86_64:
894 if (target_platform != PLATFORM_APPLE) return align;
895 /* fall through */
896 case CPU_POWERPC:
897 case CPU_ARM:
898 case CPU_ARM64:
899 n = 0;
900 while ((1u << n) != align) n++;
901 return n;
903 /* unreached */
904 assert(0);
905 return 0;
908 /* return the page size for the target CPU */
909 unsigned int get_page_size(void)
911 switch(target_cpu)
913 case CPU_x86:
914 case CPU_x86_64:
915 case CPU_POWERPC:
916 case CPU_ARM:
917 return 0x1000;
918 case CPU_ARM64:
919 return 0x10000;
921 /* unreached */
922 assert(0);
923 return 0;
926 /* return the size of a pointer on the target CPU */
927 unsigned int get_ptr_size(void)
929 switch(target_cpu)
931 case CPU_x86:
932 case CPU_POWERPC:
933 case CPU_ARM:
934 return 4;
935 case CPU_x86_64:
936 case CPU_ARM64:
937 return 8;
939 /* unreached */
940 assert(0);
941 return 0;
944 /* return the total size in bytes of the arguments on the stack */
945 unsigned int get_args_size( const ORDDEF *odp )
947 int i, size;
949 for (i = size = 0; i < odp->u.func.nb_args; i++)
951 switch (odp->u.func.args[i])
953 case ARG_INT64:
954 case ARG_DOUBLE:
955 size += 8;
956 break;
957 case ARG_INT128:
958 /* int128 is passed as pointer on x86_64 */
959 if (target_cpu != CPU_x86_64)
961 size += 16;
962 break;
964 /* fall through */
965 default:
966 size += get_ptr_size();
967 break;
970 return size;
973 /* return the assembly name for a C symbol */
974 const char *asm_name( const char *sym )
976 static char *buffer;
978 switch (target_platform)
980 case PLATFORM_APPLE:
981 case PLATFORM_WINDOWS:
982 if (sym[0] == '.' && sym[1] == 'L') return sym;
983 free( buffer );
984 buffer = strmake( "_%s", sym );
985 return buffer;
986 default:
987 return sym;
991 /* return an assembly function declaration for a C function name */
992 const char *func_declaration( const char *func )
994 static char *buffer;
996 switch (target_platform)
998 case PLATFORM_APPLE:
999 return "";
1000 case PLATFORM_WINDOWS:
1001 free( buffer );
1002 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
1003 break;
1004 default:
1005 free( buffer );
1006 switch(target_cpu)
1008 case CPU_ARM:
1009 case CPU_ARM64:
1010 buffer = strmake( ".type %s,%%function", func );
1011 break;
1012 default:
1013 buffer = strmake( ".type %s,@function", func );
1014 break;
1016 break;
1018 return buffer;
1021 /* output a size declaration for an assembly function */
1022 void output_function_size( const char *name )
1024 switch (target_platform)
1026 case PLATFORM_APPLE:
1027 case PLATFORM_WINDOWS:
1028 break;
1029 default:
1030 output( "\t.size %s, .-%s\n", name, name );
1031 break;
1035 /* output a .cfi directive */
1036 void output_cfi( const char *format, ... )
1038 va_list valist;
1040 if (!unwind_tables) return;
1041 va_start( valist, format );
1042 fputc( '\t', output_file );
1043 vfprintf( output_file, format, valist );
1044 fputc( '\n', output_file );
1045 va_end( valist );
1048 /* output the GNU note for non-exec stack */
1049 void output_gnu_stack_note(void)
1051 switch (target_platform)
1053 case PLATFORM_WINDOWS:
1054 case PLATFORM_APPLE:
1055 break;
1056 default:
1057 switch(target_cpu)
1059 case CPU_ARM:
1060 case CPU_ARM64:
1061 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1062 break;
1063 default:
1064 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1065 break;
1067 break;
1071 /* return a global symbol declaration for an assembly symbol */
1072 const char *asm_globl( const char *func )
1074 static char *buffer;
1076 free( buffer );
1077 switch (target_platform)
1079 case PLATFORM_APPLE:
1080 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1081 break;
1082 case PLATFORM_WINDOWS:
1083 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1084 break;
1085 default:
1086 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1087 break;
1089 return buffer;
1092 const char *get_asm_ptr_keyword(void)
1094 switch(get_ptr_size())
1096 case 4: return ".long";
1097 case 8: return ".quad";
1099 assert(0);
1100 return NULL;
1103 const char *get_asm_string_keyword(void)
1105 switch (target_platform)
1107 case PLATFORM_APPLE:
1108 return ".asciz";
1109 default:
1110 return ".string";
1114 const char *get_asm_rodata_section(void)
1116 switch (target_platform)
1118 case PLATFORM_APPLE: return ".const";
1119 default: return ".section .rodata";
1123 const char *get_asm_string_section(void)
1125 switch (target_platform)
1127 case PLATFORM_APPLE: return ".cstring";
1128 default: return ".section .rodata";