Skip to content

Commit b7bb723

Browse files
committed
Support _Bool as primitive type
1 parent b99bddc commit b7bb723

22 files changed

+161
-5
lines changed

doc/internals-target.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A machine target is specified by filling in a '''machine_spec''' structure, defi
99
* machine_name: name of the target
1010
* handle_option: a function that gets to inspect options passed to nesc1 and take appropriate action (example: the '''self''' target adjusts double alignment based on the -malign-double gcc flag).
1111
* big_endian: must be true for big-endian targets, false for little-endian ones
12-
* tptr, tfloat, tdouble, tlong_double, tshort, tint, tlong, tlong_long: size and alignment of the corresponding C types.
12+
* tptr, tfloat, tdouble, tlong_double, tshort, tint, tlong, tlong_long, t_Bool: size and alignment of the corresponding C types.
1313
* int1_align, int2_align, int4_align, int8_align: with gcc, you can ask for specific size ints (see gcc's mode attribute, and the '''type_for_mode''' function in types.c). On some platforms, some of these sizes may not correspond to any of the normal basic C types, so you get to specify the alignments for those missing sizes here...
1414
* wchar_t_size: size of the wchar_t type
1515
* size_t_size: size of the size_t type (actually this should be the C type, knowing just the size can cause problems)

nregress/c99c11/bool/BoolTest.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __BOOL_TEST_H__
2+
#define __BOOL_TEST_H__
3+
4+
#include <stdbool.h>
5+
6+
typedef struct {
7+
bool a:1;
8+
bool b;
9+
} bool_test_args;
10+
11+
#endif

nregress/c99c11/bool/BoolTest.nc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "BoolTest.h"
2+
3+
interface BoolTest {
4+
command bool unary(const bool a);
5+
event void unaryDone(bool result, bool arg);
6+
command void binary(const bool_test_args args);
7+
event void binaryDone(bool result, bool_test_args args);
8+
}

nregress/c99c11/bool/BoolTestM.nc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "BoolTest.h"
2+
3+
generic module BoolTestM(bool val) {
4+
provides interface BoolTest;
5+
} implementation {
6+
command bool BoolTest.unary(const bool a) {
7+
const bool result = val ? a : !a;
8+
signal BoolTest.unaryDone(result, a);
9+
return result;
10+
}
11+
12+
command void BoolTest.binary(const bool_test_args args) {
13+
const bool result = val ? args.a | args.b
14+
: args.a ^ args.b;
15+
signal BoolTest.binaryDone(result, args);
16+
}
17+
18+
default event void BoolTest.unaryDone(bool result, bool arg) {
19+
}
20+
21+
default event void BoolTest.binaryDone(bool result, bool_test_args args) {
22+
}
23+
}

nregress/c99c11/bool/TestP.nc

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
3+
#include "BoolTest.h"
4+
5+
module TestP {
6+
uses interface BoolTest as Test1;
7+
uses interface BoolTest as Test2;
8+
} implementation {
9+
event void Test1.unaryDone(bool result, bool arg) {
10+
printf("Test1.unary(%d) -> %d\n", arg, result);
11+
}
12+
13+
event void Test1.binaryDone(bool result, bool_test_args args) {
14+
printf("Test1.binary(%d, %d) -> %d\n", args.a, args.b, result);
15+
}
16+
17+
event void Test2.unaryDone(bool result, bool arg) {
18+
printf("Test2.unary(%d) -> %d\n", arg, result);
19+
}
20+
21+
event void Test2.binaryDone(bool result, bool_test_args args) {
22+
printf("Test2.binary(%d, %d) -> %d\n", args.a, args.b, result);
23+
}
24+
25+
int main() @C() @spontaneous() {
26+
bool_test_args args;
27+
bool r1, r2;
28+
29+
r1 = call Test1.unary(false);
30+
r2 = call Test1.unary(true);
31+
if (r1 == r2)
32+
printf("Test1.unary operation fails\n");
33+
args.a = args.b = false;
34+
call Test1.binary(args);
35+
args.b = true;
36+
call Test1.binary(args);
37+
args.a = true; args.b = false;
38+
call Test1.binary(args);
39+
args.a = args.b = true;
40+
call Test1.binary(args);
41+
42+
r1 = call Test2.unary(false);
43+
r2 = call Test2.unary(true);
44+
if (r1 == r2)
45+
printf("Test2.unary operation fails\n");
46+
args.a = args.b = false;
47+
call Test2.binary(args);
48+
args.b = true;
49+
call Test2.binary(args);
50+
args.a = true; args.b = false;
51+
call Test2.binary(args);
52+
args.a = args.b = true;
53+
call Test2.binary(args);
54+
55+
return 0;
56+
}
57+
}

nregress/c99c11/bool/test.nc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "BoolTest.h"
2+
3+
configuration test {
4+
} implementation {
5+
components TestP;
6+
components new BoolTestM(1 == 1 && 0 < 1) as Test1;
7+
components new BoolTestM(0 == 1 || 0 > 1) as Test2;
8+
9+
TestP.Test1 -> Test1;
10+
TestP.Test2 -> Test2;
11+
}

nregress/c99c11/run1

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
keep=
2+
if [ -z "$NESC1" ]; then
3+
NESC1=../../../src/nesc1
4+
keep=1
5+
fi
6+
cd $1
7+
cfile=/tmp/c99c11.$$.c
8+
exe=/tmp/c99c11.out.$$
9+
$NESC1 -fnesc-separator=__ test.nc -o $cfile && \
10+
gcc -Wall -g -o $exe $cfile && \
11+
$exe
12+
ok=$?
13+
if [ -z "$keep" ]; then
14+
rm -f $cfile $exe
15+
else
16+
echo C file is $cfile, executable is $exe
17+
fi
18+
exit $ok

nregress/ok/c99c11.bool.1

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Test1.unary(0) -> 0
2+
Test1.unary(1) -> 1
3+
Test1.binary(0, 0) -> 0
4+
Test1.binary(0, 1) -> 1
5+
Test1.binary(1, 0) -> 1
6+
Test1.binary(1, 1) -> 1
7+
Test2.unary(0) -> 1
8+
Test2.unary(1) -> 0
9+
Test2.binary(0, 0) -> 0
10+
Test2.binary(0, 1) -> 1
11+
Test2.binary(1, 0) -> 1
12+
Test2.binary(1, 1) -> 0

nregress/ok/c99c11.bool.2

Whitespace-only changes.

nregress/ok/c99c11.bool.exit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

src/c-lex.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum rid
5656
RID_LONG,
5757
RID_SIGNED,
5858
RID_COMPLEX,
59+
RID_BOOL,
5960
RID_LASTTYPE,
6061

6162
RID_INLINE = RID_LASTTYPE,

src/c-parse.gperf

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ __typeof__, TYPEOF, NORID
5050
__volatile, TYPE_QUAL, volatile_qualifier
5151
__volatile__, TYPE_QUAL, volatile_qualifier
5252
__builtin_va_arg, VA_ARG, NORID
53+
_Bool, TYPESPEC, RID_BOOL
5354
asm, ASM_KEYWORD, NORID
5455
auto, SCSPEC, RID_AUTO
5556
break, BREAK, NORID

src/machine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct {
1717

1818
size_t word_size;
1919
machine_type_spec tptr, tfloat, tdouble, tlong_double, tshort, tint,
20-
tlong, tlong_long;
20+
tlong, tlong_long, t_Bool;
2121
size_t int1_align, int2_align, int4_align, int8_align;
2222
size_t wchar_t_size, size_t_size;
2323
bool char_signed, wchar_t_signed;

src/machine/avr.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static machine_spec avr_machine = {
3434
{ 2, 1 }, /* int */
3535
{ 4, 1 }, /* long */
3636
{ 8, 1 }, /* long long (unsupported in avr-gcc) */
37+
{ 1, 1 }, /* _Bool */
3738
1, 1, 1, 1, /* int1/2/4/8 align */
3839
2, 2, /* wchar_t, size_t size */
3940
TRUE, TRUE, /* char, wchar_t signed */

src/machine/env_machine.c

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static machine_spec env_machine = {
4646
{2, 1}, /* int */
4747
{4, 1}, /* long */
4848
{8, 1}, /* long_long */
49+
{1, 1}, /* _Bool */
4950
1, 1, 1, 1, /* int1248_align */
5051
2, 2, /* wchar_size_size */
5152
TRUE, TRUE, /* char_wchar_signed */
@@ -125,6 +126,7 @@ static bool scan_env_machine(machine_spec * machine, const char *envname)
125126
{ "int", &(machine->tint) },
126127
{ "long", &(machine->tlong) },
127128
{ "long_long", &(machine->tlong_long) },
129+
{ "_Bool", &(machine->t_Bool) },
128130
{ NULL, NULL }
129131
};
130132

src/machine/keil.c

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static machine_spec keil_machine = {
9898
{ 2, 1 }, /* int */
9999
{ 4, 1 }, /* long */
100100
{ 8, 1 }, /* long long (unsupported in avr-gcc) */
101+
{ 1, 1 }, /* _Bool */
101102
1, 1, 1, 1, /* int1/2/4/8 align */
102103
2, 2, /* wchar_t, size_t size */
103104
TRUE, TRUE, /* char, wchar_t signed */

src/machine/msp430.c

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static machine_spec msp430_machine = {
3636
{ 2, 2 }, /* int */
3737
{ 4, 2 }, /* long */
3838
{ 8, 2 }, /* long long */
39+
{ 1, 1 }, /* _Bool */
3940
1, 2, 2, 2, /* int1/2/4/8 align */
4041
2, 2, /* wchar_t, size_t size */
4142
TRUE, TRUE, /* char, wchar_t signed */

src/machine/sdcc.c

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static machine_spec sdcc_machine = {
6767
{ 2, 1 }, /* int */
6868
{ 4, 1 }, /* long */
6969
{ 8, 1 }, /* long long */
70+
{ 1, 1 }, /* _Bool */
7071
1, 1, 1, 1, /* int1/2/4/8 align */
7172
2, 2, /* wchar_t, size_t size */
7273
TRUE, TRUE, /* char, wchar_t signed */

src/machine/self.c

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static machine_spec self_machine = {
8484
{ sizeof(int), __alignof__(int) }, /* int */
8585
{ sizeof(long), __alignof__(long) }, /* long */
8686
{ sizeof(long long), __alignof__(long long) }, /* long long */
87+
{ sizeof(_Bool), __alignof__(_Bool) }, /* _Bool */
8788
__alignof__(myint1), __alignof__(myint2),
8889
__alignof__(myint4), __alignof__(myint8), /* int1/2/4/8 align */
8990
sizeof(wchar_t), sizeof(size_t), /* wchar_t, size_t size */

src/semantics.c

+2
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ void parse_declarator(type_element modifiers, declarator d, bool bitfield,
697697
case RID_CHAR: newtype = char_type; break;
698698
case RID_FLOAT: newtype = float_type; break;
699699
case RID_DOUBLE: newtype = double_type; break;
700+
case RID_BOOL: newtype = _Bool_type; break;
700701
case RID_VOID: newtype = void_type; break;
701702
case RID_AUTO: case RID_STATIC: case RID_EXTERN:
702703
case RID_REGISTER: case RID_TYPEDEF: case RID_COMMAND:
@@ -3614,6 +3615,7 @@ static char *rid_name_int(int id)
36143615
case RID_UNSIGNED: return "unsigned";
36153616
case RID_SHORT: return "short";
36163617
case RID_LONG: return "long";
3618+
case RID_BOOL: return "_Bool";
36173619
case RID_AUTO: return "auto";
36183620
case RID_STATIC: return "static";
36193621
case RID_EXTERN: return "extern";

src/types.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct type
8080
tp_short, tp_unsigned_short,
8181
tp_int, tp_unsigned_int,
8282
tp_long, tp_unsigned_long,
83-
tp_long_long, tp_unsigned_long_long,
83+
tp_long_long, tp_unsigned_long_long, tp_Bool,
8484
/* Used as the rep type of enums whose constants are derived
8585
from template arguments and whose size is hence unknown.
8686
The unknown int type has the highest rank (its unsignedness
@@ -192,7 +192,7 @@ type float_type, double_type, long_double_type,
192192
int_type, unsigned_int_type, long_type, unsigned_long_type,
193193
long_long_type, unsigned_long_long_type, short_type, unsigned_short_type,
194194
char_type, char_array_type, wchar_type, wchar_array_type,
195-
unsigned_char_type, signed_char_type, void_type, ptr_void_type,
195+
unsigned_char_type, signed_char_type, _Bool_type, void_type, ptr_void_type,
196196
const_ptr_void_type,
197197
size_t_type, ptrdiff_t_type, intptr_type,
198198
int2_type, uint2_type, int4_type, uint4_type, int8_type, uint8_type,
@@ -463,6 +463,7 @@ void init_types(void)
463463
signed_char_type = make_primitive(tp_signed_char, 1, target->int1_align);
464464
unsigned_char_type = make_primitive(tp_unsigned_char, 1, target->int1_align);
465465
char_type = make_primitive(tp_char, 1, target->int1_align);
466+
_Bool_type = make_primitive(tp_Bool, target->t_Bool.size, target->t_Bool.align);
466467

467468
int2_type = lookup_primitive(tp_int2, 2, target->int2_align, FALSE);
468469
uint2_type = lookup_primitive(tp_uint2, 2, target->int2_align, TRUE);
@@ -1530,6 +1531,9 @@ static type_element primitive2ast(region r, location loc, int primitive,
15301531
break;
15311532
case tp_signed_char:
15321533
return rid2ast(r, loc, RID_SIGNED, rid2ast(r, loc, RID_CHAR, rest));
1534+
case tp_Bool:
1535+
keyword = RID_BOOL;
1536+
break;
15331537
case tp_unsigned_short:
15341538
isunsigned = TRUE;
15351539
case tp_short:

src/types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern type float_type, double_type, long_double_type,
5151
int_type, unsigned_int_type, long_type, unsigned_long_type,
5252
long_long_type, unsigned_long_long_type, short_type, unsigned_short_type,
5353
char_type, char_array_type, wchar_type, wchar_array_type,
54-
unsigned_char_type, signed_char_type, void_type, ptr_void_type,
54+
unsigned_char_type, signed_char_type, _Bool_type, void_type, ptr_void_type,
5555
size_t_type, ptrdiff_t_type, intptr_type, unknown_int_type,
5656
unknown_number_type, const_ptr_void_type;
5757

0 commit comments

Comments
 (0)