| 1 | /* C -> Haskell Compiler: configuration query routines |
| 2 | * |
| 3 | * Author : Manuel M T Chakravarty |
| 4 | * Created: 12 November 1 |
| 5 | * |
| 6 | * Version $Revision: 1.1 $ from $Date: 2004/11/21 21:05:42 $ |
| 7 | * |
| 8 | * Copyright (c) [2001..2002] Manuel M T Chakravarty |
| 9 | * |
| 10 | * This file is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This file is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * DESCRIPTION --------------------------------------------------------------- |
| 21 | * |
| 22 | * Runtime configuration query functions |
| 23 | * |
| 24 | * TODO ---------------------------------------------------------------------- |
| 25 | */ |
| 26 | |
| 27 | #include "c2hs_config.h" |
| 28 | |
| 29 | /* compute the direction in which bitfields are growing |
| 30 | * ==================================================== |
| 31 | */ |
| 32 | |
| 33 | union bitfield_direction_union { |
| 34 | unsigned int allbits; |
| 35 | struct { |
| 36 | unsigned int first_bit : 1; |
| 37 | unsigned int second_bit : 1; |
| 38 | } twobits; |
| 39 | }; |
| 40 | |
| 41 | int bitfield_direction () |
| 42 | { |
| 43 | union bitfield_direction_union v; |
| 44 | |
| 45 | /* if setting the second bit in a bitfield makes the storage unit contain |
| 46 | * the value `2', the direction of bitfields must be increasing towards the |
| 47 | * MSB |
| 48 | */ |
| 49 | v.allbits = 0; |
| 50 | v.twobits.second_bit = 1; |
| 51 | |
| 52 | return (2 == v.allbits ? 1 : -1); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* use padding for overspilling bitfields? |
| 57 | * ======================================= |
| 58 | */ |
| 59 | |
| 60 | union bitfield_padding_union { |
| 61 | struct { |
| 62 | unsigned int allbits1; |
| 63 | unsigned int allbits2; |
| 64 | } allbits; |
| 65 | struct { |
| 66 | unsigned int first_bit : 1; |
| 67 | int full_unit : sizeof (int) * 8; |
| 68 | } somebits; |
| 69 | }; |
| 70 | |
| 71 | int bitfield_padding () |
| 72 | { |
| 73 | union bitfield_padding_union v; |
| 74 | |
| 75 | /* test whether more than one bit of `full_unit' spills over into `allbits2' |
| 76 | */ |
| 77 | v.allbits.allbits1 = 0; |
| 78 | v.allbits.allbits2 = 0; |
| 79 | v.somebits.full_unit = -1; |
| 80 | |
| 81 | return v.allbits.allbits2 == -1; |
| 82 | } |
| 83 | |
| 84 | /* is an `int' bitfield signed? |
| 85 | * ============================ |
| 86 | */ |
| 87 | |
| 88 | union bitfield_int_signed_union { |
| 89 | struct { |
| 90 | unsigned int first_bit : 1; |
| 91 | unsigned int second_bit : 1; |
| 92 | } two_single_bits; |
| 93 | struct { |
| 94 | int two_bits : 2; |
| 95 | } two_bits; |
| 96 | }; |
| 97 | |
| 98 | int bitfield_int_signed () |
| 99 | { |
| 100 | union bitfield_int_signed_union v; |
| 101 | |
| 102 | /* check whether a two bit field with both bits set, gives us a negative |
| 103 | * number; then, `int' bitfields must be signed |
| 104 | */ |
| 105 | v.two_single_bits.first_bit = 1; |
| 106 | v.two_single_bits.second_bit = 1; |
| 107 | |
| 108 | return v.two_bits.two_bits == -1; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /* alignment constraint for bitfields |
| 113 | * ================================== |
| 114 | */ |
| 115 | |
| 116 | struct bitfield_alignment_struct { |
| 117 | char start; |
| 118 | unsigned int bit : 1; |
| 119 | char end; |
| 120 | }; |
| 121 | |
| 122 | int bitfield_alignment () |
| 123 | { |
| 124 | struct bitfield_alignment_struct v; |
| 125 | |
| 126 | return ((int) (&v.end - &v.start)) - 1; |
| 127 | } |