Macroize any compiler-specific code; macros defined in "compiler.h"

Move anything compiler-specific to "compiler.h".

There was an unguarded use of __attribute__(()) in outmacho.c; also
require gcc 4+ for __builtin_ctlz().  Speed up the open-coded version, too.
This commit is contained in:
H. Peter Anvin 2007-04-13 19:58:42 +00:00
parent a6dfa78b78
commit c1494ac5ab
2 changed files with 57 additions and 9 deletions

36
compiler.h Normal file

@ -0,0 +1,36 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 2007 The NASM Authors - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the license given in the file "License"
* distributed in the NASM archive.
*
* ----------------------------------------------------------------------- */
/*
* compiler.h
*
* Compiler-specific macros for NASM. Feel free to add support for
* other compilers in here.
*/
#ifndef COMPILER_H
#define COMPILER_H
#ifdef __GNUC__
# if __GNUC__ >= 4
# define HAVE_GNUC_4
# endif
# if __GNUC__ >= 3
# define HAVE_GNUC_3
# endif
#endif
#ifdef __GNUC__
# define _unused __attribute__((unused))
#else
# define _unused
#endif
#endif

@ -19,6 +19,7 @@
#include "nasm.h"
#include "nasmlib.h"
#include "outform.h"
#include "compiler.h"
#if defined(OF_MACHO)
@ -199,20 +200,31 @@ uint32_t rel_padcnt = 0;
align(x, sizeof(int32_t)) /* align x to int32_t boundary */
static void debug_reloc (struct reloc *);
static void debug_section_relocs (struct section *) __attribute__ ((unused));
static void debug_section_relocs (struct section *) _unused;
static int exact_log2 (uint32_t align) {
if (align != (align & -align)) {
return -1;
static int exact_log2 (uint32_t align)
{
if (align == 0) {
return 0;
} else if (align & (align-1)) {
return -1; /* Not a power of 2 */
} else {
#ifdef __GNUC__
return (align ? __builtin_ctzl (align) : 0);
#ifdef HAVE_GNUC_4
return __builtin_ctzl (align);
#else
uint32_t result = 0;
while (align >>= 1) {
++result;
}
/* We know exactly one bit is set at this point. */
if (align & 0xffff0000)
result |= 16;
if (align & 0xff00ff00)
result |= 8;
if (align & 0xf0f0f0f0)
result |= 4;
if (align & 0xcccccccc)
result |= 2;
if (align & 0xaaaaaaaa)
result |= 1;
return result;
#endif