wineps: Fix the output of indexed bitmaps in PutImage.
[wine/wine-gecko.git] / tools / winebuild / utils.c
blob2edb9e3cf9ebece667fdca6dcd5f239e451ab044
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 }
64 /* atexit handler to clean tmp files */
65 static void cleanup_tmp_files(void)
67 unsigned int i;
68 for (i = 0; i < nb_tmp_files; i++) if (tmp_files[i]) unlink( tmp_files[i] );
72 void *xmalloc (size_t size)
74 void *res;
76 res = malloc (size ? size : 1);
77 if (res == NULL)
79 fprintf (stderr, "Virtual memory exhausted.\n");
80 exit (1);
82 return res;
85 void *xrealloc (void *ptr, size_t size)
87 void *res = realloc (ptr, size);
88 if (size && res == NULL)
90 fprintf (stderr, "Virtual memory exhausted.\n");
91 exit (1);
93 return res;
96 char *xstrdup( const char *str )
98 char *res = strdup( str );
99 if (!res)
101 fprintf (stderr, "Virtual memory exhausted.\n");
102 exit (1);
104 return res;
107 char *strupper(char *s)
109 char *p;
110 for (p = s; *p; p++) *p = toupper(*p);
111 return s;
114 int strendswith(const char* str, const char* end)
116 int l = strlen(str);
117 int m = strlen(end);
118 return l >= m && strcmp(str + l - m, end) == 0;
121 char *strmake( const char* fmt, ... )
123 int n;
124 size_t size = 100;
125 va_list ap;
127 for (;;)
129 char *p = xmalloc( size );
130 va_start( ap, fmt );
131 n = vsnprintf( p, size, fmt, ap );
132 va_end( ap );
133 if (n == -1) size *= 2;
134 else if ((size_t)n >= size) size = n + 1;
135 else return p;
136 free( p );
140 struct strarray *strarray_init(void)
142 struct strarray *array = xmalloc( sizeof(*array) );
143 array->count = 0;
144 array->max = 16;
145 array->str = xmalloc( array->max * sizeof(*array->str) );
146 return array;
149 static void strarray_add_one( struct strarray *array, const char *str )
151 if (array->count == array->max)
153 array->max *= 2;
154 array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
156 array->str[array->count++] = str;
159 void strarray_add( struct strarray *array, ... )
161 va_list valist;
162 const char *str;
164 va_start( valist, array );
165 while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
166 va_end( valist );
169 void strarray_addv( struct strarray *array, char * const *argv )
171 while (*argv) strarray_add_one( array, *argv++ );
174 void strarray_free( struct strarray *array )
176 free( array->str );
177 free( array );
180 void fatal_error( const char *msg, ... )
182 va_list valist;
183 va_start( valist, msg );
184 if (input_file_name)
186 fprintf( stderr, "%s:", input_file_name );
187 if (current_line)
188 fprintf( stderr, "%d:", current_line );
189 fputc( ' ', stderr );
191 else fprintf( stderr, "winebuild: " );
192 vfprintf( stderr, msg, valist );
193 va_end( valist );
194 exit(1);
197 void fatal_perror( const char *msg, ... )
199 va_list valist;
200 va_start( valist, msg );
201 if (input_file_name)
203 fprintf( stderr, "%s:", input_file_name );
204 if (current_line)
205 fprintf( stderr, "%d:", current_line );
206 fputc( ' ', stderr );
208 vfprintf( stderr, msg, valist );
209 perror( " " );
210 va_end( valist );
211 exit(1);
214 void error( const char *msg, ... )
216 va_list valist;
217 va_start( valist, msg );
218 if (input_file_name)
220 fprintf( stderr, "%s:", input_file_name );
221 if (current_line)
222 fprintf( stderr, "%d:", current_line );
223 fputc( ' ', stderr );
225 vfprintf( stderr, msg, valist );
226 va_end( valist );
227 nb_errors++;
230 void warning( const char *msg, ... )
232 va_list valist;
234 if (!display_warnings) return;
235 va_start( valist, msg );
236 if (input_file_name)
238 fprintf( stderr, "%s:", input_file_name );
239 if (current_line)
240 fprintf( stderr, "%d:", current_line );
241 fputc( ' ', stderr );
243 fprintf( stderr, "warning: " );
244 vfprintf( stderr, msg, valist );
245 va_end( valist );
248 int output( const char *format, ... )
250 int ret;
251 va_list valist;
253 va_start( valist, format );
254 ret = vfprintf( output_file, format, valist );
255 va_end( valist );
256 if (ret < 0) fatal_perror( "Output error" );
257 return ret;
260 void spawn( struct strarray *args )
262 unsigned int i;
263 int status;
265 strarray_add_one( args, NULL );
266 if (verbose)
267 for (i = 0; args->str[i]; i++)
268 fprintf( stderr, "%s%c", args->str[i], args->str[i+1] ? ' ' : '\n' );
270 if ((status = spawnvp( _P_WAIT, args->str[0], args->str )))
272 if (status > 0) fatal_error( "%s failed with status %u\n", args->str[0], status );
273 else fatal_perror( "winebuild" );
274 exit( 1 );
278 /* find a build tool in the path, trying the various names */
279 char *find_tool( const char *name, const char * const *names )
281 static char **dirs;
282 static unsigned int count, maxlen;
284 char *p, *file;
285 const char *alt_names[2];
286 unsigned int i, len;
287 struct stat st;
289 if (target_alias) return strmake( "%s-%s", target_alias, name );
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 file = xmalloc( maxlen + len );
325 for (i = 0; i < count; i++)
327 strcpy( file, dirs[i] );
328 p = file + strlen(file);
329 if (p == file) *p++ = '.';
330 if (p[-1] != '/') *p++ = '/';
331 strcpy( p, *names );
332 strcat( p, EXEEXT );
334 if (!stat( file, &st ) && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return file;
336 free( file );
337 names++;
339 return xstrdup( name );
342 struct strarray *get_as_command(void)
344 struct strarray *args = strarray_init();
346 if (!as_command)
348 static const char * const commands[] = { "gas", "as", NULL };
349 as_command = find_tool( "as", commands );
351 strarray_add_one( args, as_command );
353 if (force_pointer_size)
355 switch (target_platform)
357 case PLATFORM_APPLE:
358 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
359 break;
360 default:
361 strarray_add_one( args, (force_pointer_size == 8) ? "--64" : "--32" );
362 break;
366 if (cpu_option) strarray_add_one( args, strmake("-mcpu=%s", cpu_option) );
367 return args;
370 struct strarray *get_ld_command(void)
372 struct strarray *args = strarray_init();
374 if (!ld_command)
376 static const char * const commands[] = { "ld", "gld", NULL };
377 ld_command = find_tool( "ld", commands );
379 strarray_add_one( args, ld_command );
381 if (force_pointer_size)
383 switch (target_platform)
385 case PLATFORM_APPLE:
386 strarray_add( args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
387 break;
388 case PLATFORM_FREEBSD:
389 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
390 break;
391 default:
392 strarray_add( args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
393 break;
396 return args;
399 const char *get_nm_command(void)
401 if (!nm_command)
403 static const char * const commands[] = { "nm", "gnm", NULL };
404 nm_command = find_tool( "nm", commands );
406 return nm_command;
409 /* get a name for a temp file, automatically cleaned up on exit */
410 char *get_temp_file_name( const char *prefix, const char *suffix )
412 char *name;
413 const char *ext, *basename;
414 int fd;
416 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
418 if (!prefix || !prefix[0]) prefix = "winebuild";
419 if (!suffix) suffix = "";
420 if ((basename = strrchr( prefix, '/' ))) basename++;
421 else basename = prefix;
422 if (!(ext = strchr( basename, '.' ))) ext = prefix + strlen(prefix);
423 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
424 memcpy( name, prefix, ext - prefix );
425 strcpy( name + (ext - prefix), ".XXXXXX" );
426 strcat( name, suffix );
428 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
430 strcpy( name, "/tmp/" );
431 memcpy( name + 5, basename, ext - basename );
432 strcpy( name + 5 + (ext - basename), ".XXXXXX" );
433 strcat( name, suffix );
434 if ((fd = mkstemps( name, strlen(suffix) )) == -1)
435 fatal_error( "could not generate a temp file\n" );
438 close( fd );
439 if (nb_tmp_files >= max_tmp_files)
441 max_tmp_files = max( 2 * max_tmp_files, 8 );
442 tmp_files = xrealloc( tmp_files, max_tmp_files * sizeof(tmp_files[0]) );
444 tmp_files[nb_tmp_files++] = name;
445 return name;
448 /*******************************************************************
449 * buffer management
451 * Function for reading from/writing to a memory buffer.
454 int byte_swapped = 0;
455 const char *input_buffer_filename;
456 const unsigned char *input_buffer;
457 size_t input_buffer_pos;
458 size_t input_buffer_size;
459 unsigned char *output_buffer;
460 size_t output_buffer_pos;
461 size_t output_buffer_size;
463 static void check_output_buffer_space( size_t size )
465 if (output_buffer_pos + size >= output_buffer_size)
467 output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
468 output_buffer = xrealloc( output_buffer, output_buffer_size );
472 void init_input_buffer( const char *file )
474 int fd;
475 struct stat st;
477 if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file );
478 if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file );
479 if (!st.st_size) fatal_error( "%s is an empty file\n", file );
480 #ifdef HAVE_MMAP
481 if ((input_buffer = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 )) == (void*)-1)
482 #endif
484 unsigned char *buffer = xmalloc( st.st_size );
485 if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file );
486 input_buffer = buffer;
488 close( fd );
489 input_buffer_filename = xstrdup( file );
490 input_buffer_size = st.st_size;
491 input_buffer_pos = 0;
492 byte_swapped = 0;
495 void init_output_buffer(void)
497 output_buffer_size = 1024;
498 output_buffer_pos = 0;
499 output_buffer = xmalloc( output_buffer_size );
502 void flush_output_buffer(void)
504 if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
505 fatal_error( "Error writing to %s\n", output_file_name );
506 free( output_buffer );
509 unsigned char get_byte(void)
511 if (input_buffer_pos >= input_buffer_size)
512 fatal_error( "%s is a truncated file\n", input_buffer_filename );
513 return input_buffer[input_buffer_pos++];
516 unsigned short get_word(void)
518 unsigned short ret;
520 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
521 fatal_error( "%s is a truncated file\n", input_buffer_filename );
522 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
523 if (byte_swapped) ret = (ret << 8) | (ret >> 8);
524 input_buffer_pos += sizeof(ret);
525 return ret;
528 unsigned int get_dword(void)
530 unsigned int ret;
532 if (input_buffer_pos + sizeof(ret) > input_buffer_size)
533 fatal_error( "%s is a truncated file\n", input_buffer_filename );
534 memcpy( &ret, input_buffer + input_buffer_pos, sizeof(ret) );
535 if (byte_swapped)
536 ret = ((ret << 24) | ((ret << 8) & 0x00ff0000) | ((ret >> 8) & 0x0000ff00) | (ret >> 24));
537 input_buffer_pos += sizeof(ret);
538 return ret;
541 void put_data( const void *data, size_t size )
543 check_output_buffer_space( size );
544 memcpy( output_buffer + output_buffer_pos, data, size );
545 output_buffer_pos += size;
548 void put_byte( unsigned char val )
550 check_output_buffer_space( 1 );
551 output_buffer[output_buffer_pos++] = val;
554 void put_word( unsigned short val )
556 if (byte_swapped) val = (val << 8) | (val >> 8);
557 put_data( &val, sizeof(val) );
560 void put_dword( unsigned int val )
562 if (byte_swapped)
563 val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
564 put_data( &val, sizeof(val) );
567 void put_qword( unsigned int val )
569 if (byte_swapped)
571 put_dword( 0 );
572 put_dword( val );
574 else
576 put_dword( val );
577 put_dword( 0 );
581 /* pointer-sized word */
582 void put_pword( unsigned int val )
584 if (get_ptr_size() == 8) put_qword( val );
585 else put_dword( val );
588 void align_output( unsigned int align )
590 size_t size = align - (output_buffer_pos % align);
592 if (size == align) return;
593 check_output_buffer_space( size );
594 memset( output_buffer + output_buffer_pos, 0, size );
595 output_buffer_pos += size;
598 /* output a standard header for generated files */
599 void output_standard_file_header(void)
601 if (spec_file_name)
602 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
603 else
604 output( "/* File generated automatically; do not edit! */\n" );
605 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
608 /* dump a byte stream into the assembly code */
609 void dump_bytes( const void *buffer, unsigned int size )
611 unsigned int i;
612 const unsigned char *ptr = buffer;
614 if (!size) return;
615 output( "\t.byte " );
616 for (i = 0; i < size - 1; i++, ptr++)
618 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
619 else output( "0x%02x,", *ptr );
621 output( "0x%02x\n", *ptr );
625 /*******************************************************************
626 * open_input_file
628 * Open a file in the given srcdir and set the input_file_name global variable.
630 FILE *open_input_file( const char *srcdir, const char *name )
632 char *fullname;
633 FILE *file = fopen( name, "r" );
635 if (!file && srcdir)
637 fullname = strmake( "%s/%s", srcdir, name );
638 file = fopen( fullname, "r" );
640 else fullname = xstrdup( name );
642 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
643 input_file_name = fullname;
644 current_line = 1;
645 return file;
649 /*******************************************************************
650 * close_input_file
652 * Close the current input file (must have been opened with open_input_file).
654 void close_input_file( FILE *file )
656 fclose( file );
657 free( input_file_name );
658 input_file_name = NULL;
659 current_line = 0;
663 /*******************************************************************
664 * remove_stdcall_decoration
666 * Remove a possible @xx suffix from a function name.
667 * Return the numerical value of the suffix, or -1 if none.
669 int remove_stdcall_decoration( char *name )
671 char *p, *end = strrchr( name, '@' );
672 if (!end || !end[1] || end == name) return -1;
673 if (target_cpu != CPU_x86) return -1;
674 /* make sure all the rest is digits */
675 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
676 *end = 0;
677 return atoi( end + 1 );
681 /*******************************************************************
682 * assemble_file
684 * Run a file through the assembler.
686 void assemble_file( const char *src_file, const char *obj_file )
688 struct strarray *args = get_as_command();
689 strarray_add( args, "-o", obj_file, src_file, NULL );
690 spawn( args );
691 strarray_free( args );
695 /*******************************************************************
696 * alloc_dll_spec
698 * Create a new dll spec file descriptor
700 DLLSPEC *alloc_dll_spec(void)
702 DLLSPEC *spec;
704 spec = xmalloc( sizeof(*spec) );
705 spec->file_name = NULL;
706 spec->dll_name = NULL;
707 spec->init_func = NULL;
708 spec->main_module = NULL;
709 spec->type = SPEC_WIN32;
710 spec->base = MAX_ORDINALS;
711 spec->limit = 0;
712 spec->stack_size = 0;
713 spec->heap_size = 0;
714 spec->nb_entry_points = 0;
715 spec->alloc_entry_points = 0;
716 spec->nb_names = 0;
717 spec->nb_resources = 0;
718 spec->characteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
719 if (get_ptr_size() > 4)
720 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
721 else
722 spec->characteristics |= IMAGE_FILE_32BIT_MACHINE;
723 spec->dll_characteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
724 spec->subsystem = 0;
725 spec->subsystem_major = 4;
726 spec->subsystem_minor = 0;
727 spec->entry_points = NULL;
728 spec->names = NULL;
729 spec->ordinals = NULL;
730 spec->resources = NULL;
731 return spec;
735 /*******************************************************************
736 * free_dll_spec
738 * Free dll spec file descriptor
740 void free_dll_spec( DLLSPEC *spec )
742 int i;
744 for (i = 0; i < spec->nb_entry_points; i++)
746 ORDDEF *odp = &spec->entry_points[i];
747 free( odp->name );
748 free( odp->export_name );
749 free( odp->link_name );
751 free( spec->file_name );
752 free( spec->dll_name );
753 free( spec->init_func );
754 free( spec->entry_points );
755 free( spec->names );
756 free( spec->ordinals );
757 free( spec->resources );
758 free( spec );
762 /*******************************************************************
763 * make_c_identifier
765 * Map a string to a valid C identifier.
767 const char *make_c_identifier( const char *str )
769 static char buffer[256];
770 char *p;
772 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
774 if (isalnum(*str)) *p = *str;
775 else *p = '_';
777 *p = 0;
778 return buffer;
782 /*******************************************************************
783 * get_stub_name
785 * Generate an internal name for a stub entry point.
787 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
789 static char *buffer;
791 free( buffer );
792 if (odp->name || odp->export_name)
794 char *p;
795 buffer = strmake( "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
796 /* make sure name is a legal C identifier */
797 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
798 if (!*p) return buffer;
799 free( buffer );
801 buffer = strmake( "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
802 return buffer;
805 /* parse a cpu name and return the corresponding value */
806 enum target_cpu get_cpu_from_name( const char *name )
808 unsigned int i;
810 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
811 if (!strcmp( cpu_names[i].name, name )) return cpu_names[i].cpu;
812 return -1;
815 /*****************************************************************
816 * Function: get_alignment
818 * Description:
819 * According to the info page for gas, the .align directive behaves
820 * differently on different systems. On some architectures, the
821 * argument of a .align directive is the number of bytes to pad to, so
822 * to align on an 8-byte boundary you'd say
823 * .align 8
824 * On other systems, the argument is "the number of low-order zero bits
825 * that the location counter must have after advancement." So to
826 * align on an 8-byte boundary you'd say
827 * .align 3
829 * The reason gas is written this way is that it's trying to mimick
830 * native assemblers for the various architectures it runs on. gas
831 * provides other directives that work consistently across
832 * architectures, but of course we want to work on all arches with or
833 * without gas. Hence this function.
836 * Parameters:
837 * align -- the number of bytes to align to. Must be a power of 2.
839 unsigned int get_alignment(unsigned int align)
841 unsigned int n;
843 assert( !(align & (align - 1)) );
845 switch(target_cpu)
847 case CPU_x86:
848 case CPU_x86_64:
849 case CPU_SPARC:
850 if (target_platform != PLATFORM_APPLE) return align;
851 /* fall through */
852 case CPU_POWERPC:
853 case CPU_ARM:
854 n = 0;
855 while ((1u << n) != align) n++;
856 return n;
858 /* unreached */
859 assert(0);
860 return 0;
863 /* return the page size for the target CPU */
864 unsigned int get_page_size(void)
866 switch(target_cpu)
868 case CPU_x86: return 4096;
869 case CPU_x86_64: return 4096;
870 case CPU_POWERPC: return 4096;
871 case CPU_ARM: return 4096;
872 case CPU_SPARC: return 8192;
874 /* unreached */
875 assert(0);
876 return 0;
879 /* return the size of a pointer on the target CPU */
880 unsigned int get_ptr_size(void)
882 switch(target_cpu)
884 case CPU_x86:
885 case CPU_POWERPC:
886 case CPU_SPARC:
887 case CPU_ARM:
888 return 4;
889 case CPU_x86_64:
890 return 8;
892 /* unreached */
893 assert(0);
894 return 0;
897 /* return the total size in bytes of the arguments on the stack */
898 unsigned int get_args_size( const ORDDEF *odp )
900 int i, size;
902 for (i = size = 0; i < odp->u.func.nb_args; i++)
904 switch (odp->u.func.args[i])
906 case ARG_INT64:
907 case ARG_DOUBLE:
908 size += 8;
909 break;
910 case ARG_INT128:
911 /* int128 is passed as pointer on x86_64 */
912 if (target_cpu != CPU_x86_64)
914 size += 16;
915 break;
917 /* fall through */
918 default:
919 size += get_ptr_size();
920 break;
923 return size;
926 /* return the assembly name for a C symbol */
927 const char *asm_name( const char *sym )
929 static char *buffer;
931 switch (target_platform)
933 case PLATFORM_APPLE:
934 case PLATFORM_WINDOWS:
935 if (sym[0] == '.' && sym[1] == 'L') return sym;
936 free( buffer );
937 buffer = strmake( "_%s", sym );
938 return buffer;
939 default:
940 return sym;
944 /* return an assembly function declaration for a C function name */
945 const char *func_declaration( const char *func )
947 static char *buffer;
949 switch (target_platform)
951 case PLATFORM_APPLE:
952 return "";
953 case PLATFORM_WINDOWS:
954 free( buffer );
955 buffer = strmake( ".def _%s; .scl 2; .type 32; .endef", func );
956 break;
957 default:
958 free( buffer );
959 switch(target_cpu)
961 case CPU_ARM:
962 buffer = strmake( ".type %s,%%function", func );
963 break;
964 default:
965 buffer = strmake( ".type %s,@function", func );
966 break;
968 break;
970 return buffer;
973 /* output a size declaration for an assembly function */
974 void output_function_size( const char *name )
976 switch (target_platform)
978 case PLATFORM_APPLE:
979 case PLATFORM_WINDOWS:
980 break;
981 default:
982 output( "\t.size %s, .-%s\n", name, name );
983 break;
987 /* output a .cfi directive */
988 void output_cfi( const char *format, ... )
990 va_list valist;
992 if (!unwind_tables) return;
993 va_start( valist, format );
994 fputc( '\t', output_file );
995 vfprintf( output_file, format, valist );
996 fputc( '\n', output_file );
997 va_end( valist );
1000 /* output the GNU note for non-exec stack */
1001 void output_gnu_stack_note(void)
1003 switch (target_platform)
1005 case PLATFORM_WINDOWS:
1006 case PLATFORM_APPLE:
1007 break;
1008 default:
1009 switch(target_cpu)
1011 case CPU_ARM:
1012 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
1013 break;
1014 default:
1015 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
1016 break;
1018 break;
1022 /* return a global symbol declaration for an assembly symbol */
1023 const char *asm_globl( const char *func )
1025 static char *buffer;
1027 free( buffer );
1028 switch (target_platform)
1030 case PLATFORM_APPLE:
1031 buffer = strmake( "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
1032 break;
1033 case PLATFORM_WINDOWS:
1034 buffer = strmake( "\t.globl _%s\n_%s:", func, func );
1035 break;
1036 default:
1037 buffer = strmake( "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
1038 break;
1040 return buffer;
1043 const char *get_asm_ptr_keyword(void)
1045 switch(get_ptr_size())
1047 case 4: return ".long";
1048 case 8: return ".quad";
1050 assert(0);
1051 return NULL;
1054 const char *get_asm_string_keyword(void)
1056 switch (target_platform)
1058 case PLATFORM_APPLE:
1059 return ".asciz";
1060 default:
1061 return ".string";
1065 const char *get_asm_short_keyword(void)
1067 switch (target_platform)
1069 default: return ".short";
1073 const char *get_asm_rodata_section(void)
1075 switch (target_platform)
1077 case PLATFORM_APPLE: return ".const";
1078 default: return ".section .rodata";
1082 const char *get_asm_string_section(void)
1084 switch (target_platform)
1086 case PLATFORM_APPLE: return ".cstring";
1087 default: return ".section .rodata";