This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch 4/8] Simplify handling of scalar types
- From: Zack Weinberg <zackw at panix dot com>
- To: Ben Elliston <bje at au1 dot ibm dot com>, Geoff Keating <geoffk at apple dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Sun, 18 Mar 2007 01:57:49 -0700
- Subject: [patch 4/8] Simplify handling of scalar types
- References: <20070318085745.619269000@mrtock.ucsd.edu>
TYPE_SCALARs save their declared types as strings, but the only
thing this is used for is deciding whether to substitute TYPE_STRING
for an array or pointer type in adjust_field_type. So replace it with
a boolean. Also, rearrange data flow a little so that yylex passes
back a string rather than a type_p for the SCALAR terminal (after the
next patch, yylex will deal only in strings).
In case anyone is wondering, scalar_char and scalar_nonchar are fully
initialized at runtime rather than in their definitions because there is
no way in C90 to initialize any member of a union but the first. We
wouldn't gain anything by it anyway - they can't be const because their
pointer_to fields may get filled out at some point.
* gengtype.h (struct type): Replace 'sc' with boolean, scalar_is_char.
(string_type): Don't declare.
(do_scalar_typedef): Declare.
(create_scalar_type): Update prototype.
* gengtype.c (string_type): Make static.
(scalar_nonchar, scalar_char): New.
(do_scalar_typedef): Export. Always use scalar_nonchar for the type.
(resolve_typedef): Use scalar_nonchar for error recovery.
(create_scalar_type): Remove name_len field. Return scalar_char
or scalar_nonchar as appropriate.
(adjust_field_type): Look at scalar_is_char boolean to decide whether
to use string_type.
(throughout): Use scalar_nonchar instead of calling create_scalar_type,
whenever possible.
(main): Initialize scalar_char and scalar_nonchar before calling
gen_rtx_next.
* gengtype-lex.l: Adjust for removal of second argument to
create_scalar_type. Use yylval.s instead of yylval.t when
returning SCALAR.
* gengtype-yacc.y: Type of SCALAR is string. Call
create_scalar_type from type:SCALAR rule. Adjust for removal of
second argument to create_scalar_type.
Index: S-vn-gtsmall/gcc/gengtype.c
===================================================================
--- S-vn-gtsmall.orig/gcc/gengtype.c 2007-03-17 16:08:39.000000000 -0700
+++ S-vn-gtsmall/gcc/gengtype.c 2007-03-17 16:08:44.000000000 -0700
@@ -78,8 +78,18 @@
/* The one and only TYPE_STRING. */
-struct type string_type = {
- TYPE_STRING, NULL, NULL, GC_USED, {0}
+static struct type string_type = {
+ TYPE_STRING, 0, 0, GC_USED, {0}
+};
+
+/* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
+ set to appropriate values at the beginning of main. */
+
+static struct type scalar_nonchar = {
+ TYPE_SCALAR, 0, 0, GC_USED, {0}
+};
+static struct type scalar_char = {
+ TYPE_SCALAR, 0, 0, GC_USED, {0}
};
/* Lists of various things. */
@@ -89,7 +99,6 @@
static type_p param_structs;
static pair_p variables;
-static void do_scalar_typedef (const char *, struct fileloc *);
static type_p find_param_structure
(type_p t, type_p param[NUM_PARAM]);
static type_p adjust_field_tree_exp (type_p t, options_p opt);
@@ -121,12 +130,14 @@
typedefs = p;
}
-/* Define S as a typename of a scalar. */
+/* Define S as a typename of a scalar. Cannot be used to define
+ typedefs of 'char'. Note: is also used for pointer-to-function
+ typedefs (which are therefore not treated as pointers). */
-static void
+void
do_scalar_typedef (const char *s, struct fileloc *pos)
{
- do_typedef (s, create_scalar_type (s, strlen (s)), pos);
+ do_typedef (s, &scalar_nonchar, pos);
}
/* Return the type previously defined for S. Use POS to report errors. */
@@ -139,7 +150,7 @@
if (strcmp (p->name, s) == 0)
return p->type;
error_at_line (pos, "unidentified type `%s'", s);
- return create_scalar_type ("char", 4);
+ return &scalar_nonchar; /* treat as "int" */
}
/* Create and return a new structure with tag NAME (or a union iff
@@ -269,12 +280,12 @@
/* Return a scalar type with name NAME. */
type_p
-create_scalar_type (const char *name, size_t name_len)
+create_scalar_type (const char *name)
{
- type_p r = XCNEW (struct type);
- r->kind = TYPE_SCALAR;
- r->u.sc = (char *) xmemdup (name, name_len, name_len + 1);
- return r;
+ if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
+ return &scalar_char;
+ else
+ return &scalar_nonchar;
}
/* Return a pointer to T. */
@@ -499,7 +510,7 @@
bitmap_tp = create_pointer (find_structure ("bitmap_element_def", 0));
basic_block_tp = create_pointer (find_structure ("basic_block_def", 0));
constant_tp = create_pointer (find_structure ("constant_descriptor_rtx", 0));
- scalar_tp = create_scalar_type ("rtunion scalar", 14);
+ scalar_tp = &scalar_nonchar; /* rtunion int */
{
pair_p note_flds = NULL;
@@ -796,13 +807,11 @@
if (! length_p
&& pointer_p
&& t->u.p->kind == TYPE_SCALAR
- && (strcmp (t->u.p->u.sc, "char") == 0
- || strcmp (t->u.p->u.sc, "unsigned char") == 0))
+ && t->u.p->u.scalar_is_char)
return &string_type;
if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
&& t->u.a.p->u.p->kind == TYPE_SCALAR
- && (strcmp (t->u.a.p->u.p->u.sc, "char") == 0
- || strcmp (t->u.a.p->u.p->u.sc, "unsigned char") == 0))
+ && t->u.a.p->u.p->u.scalar_is_char)
return create_array (&string_type, t->u.a.len);
return t;
@@ -3015,7 +3024,7 @@
if (is_scalar)
{
- t = create_scalar_type (typename, strlen (typename));
+ t = create_scalar_type (typename);
o = 0;
}
else
@@ -3034,7 +3043,7 @@
fields = f;
f = XNEW (struct pair);
- f->type = adjust_field_type (create_scalar_type ("unsigned", 8), 0);
+ f->type = adjust_field_type (create_scalar_type ("unsigned"), 0);
f->name = "alloc";
f->opt = 0;
f->line = *pos;
@@ -3042,7 +3051,7 @@
fields = f;
f = XNEW (struct pair);
- f->type = adjust_field_type (create_scalar_type ("unsigned", 8), 0);
+ f->type = adjust_field_type (create_scalar_type ("unsigned"), 0);
f->name = "num";
f->opt = 0;
f->line = *pos;
@@ -3083,10 +3092,13 @@
static struct fileloc pos = { __FILE__, __LINE__ };
unsigned j;
- gen_rtx_next ();
-
srcdir_len = strlen (srcdir);
+ scalar_char.u.scalar_is_char = true;
+ scalar_nonchar.u.scalar_is_char = false;
+
+ gen_rtx_next ();
+
do_scalar_typedef ("CUMULATIVE_ARGS", &pos);
do_scalar_typedef ("REAL_VALUE_TYPE", &pos);
do_scalar_typedef ("double_int", &pos);
@@ -3101,9 +3113,7 @@
do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos)), &pos);
- do_typedef ("HARD_REG_SET", create_array (
- create_scalar_type ("unsigned long", strlen ("unsigned long")),
- "2"), &pos);
+ do_typedef ("HARD_REG_SET", create_array (&scalar_nonchar, "2"), &pos);
for (i = 0; i < NUM_GT_FILES; i++)
{
Index: S-vn-gtsmall/gcc/gengtype.h
===================================================================
--- S-vn-gtsmall.orig/gcc/gengtype.h 2007-03-17 16:08:39.000000000 -0700
+++ S-vn-gtsmall/gcc/gengtype.h 2007-03-17 16:08:44.000000000 -0700
@@ -90,7 +90,7 @@
lang_bitmap bitmap;
type_p lang_struct;
} s;
- char *sc;
+ bool scalar_is_char;
struct {
type_p p;
const char *len;
@@ -112,9 +112,6 @@
|| (x)->kind == TYPE_STRUCT \
|| (x)->kind == TYPE_LANG_STRUCT)
-/* The one and only TYPE_STRING. */
-extern struct type string_type;
-
/* Variables used to communicate between the lexer and the parser. */
extern int lexer_toplevel_done;
extern struct fileloc lexer_line;
@@ -132,12 +129,13 @@
/* Constructor routines for types. */
extern void do_typedef (const char *s, type_p t, struct fileloc *pos);
+extern void do_scalar_typedef (const char *s, struct fileloc *pos);
extern type_p resolve_typedef (const char *s, struct fileloc *pos);
extern type_p new_structure (const char *name, int isunion,
struct fileloc *pos, pair_p fields,
options_p o);
extern type_p find_structure (const char *s, int isunion);
-extern type_p create_scalar_type (const char *name, size_t name_len);
+extern type_p create_scalar_type (const char *name);
extern type_p create_pointer (type_p t);
extern type_p create_array (type_p t, const char *len);
extern options_p create_option (options_p, const char *name, const void *info);
Index: S-vn-gtsmall/gcc/gengtype-lex.l
===================================================================
--- S-vn-gtsmall.orig/gcc/gengtype-lex.l 2007-03-17 16:08:39.000000000 -0700
+++ S-vn-gtsmall/gcc/gengtype-lex.l 2007-03-17 16:08:44.000000000 -0700
@@ -95,7 +95,6 @@
char *namestart;
size_t namelen;
- struct type *t;
char *typestart;
size_t typelen;
@@ -112,9 +111,10 @@
ISSPACE (typestart[typelen-1]);
typelen--)
;
+ typestart[typelen] = '\0';
- t = create_scalar_type (typestart, typelen);
- do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
+ do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1),
+ create_scalar_type (typestart),
&lexer_line);
update_lineno (yytext, yyleng);
}
@@ -122,7 +122,6 @@
[^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}"(" {
char *namestart;
size_t namelen;
- struct type *t;
for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
;
@@ -130,16 +129,14 @@
;
namestart -= namelen - 1;
- t = create_scalar_type ("function type", sizeof ("function type")-1);
- do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
- &lexer_line);
+ do_scalar_typedef ((const char *) xmemdup (namestart, namelen, namelen+1),
+ &lexer_line);
update_lineno (yytext, yyleng);
}
[^[:alnum:]_]typedef{WS}{ID}{WS}?"*"?{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?"(" {
char *namestart;
size_t namelen;
- struct type *t;
for (namestart = yytext + yyleng - 2; !ISIDNUM (*namestart); namestart--)
;
@@ -147,9 +144,8 @@
;
namestart -= namelen - 1;
- t = create_scalar_type ("function type", sizeof ("function type")-1);
- do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
- &lexer_line);
+ do_scalar_typedef ((const char *) xmemdup (namestart, namelen, namelen+1),
+ &lexer_line);
update_lineno (yytext, yyleng);
}
@@ -264,7 +260,7 @@
for (len = yyleng; ISSPACE (yytext[len-1]); len--)
;
- yylval.t = create_scalar_type (yytext, len);
+ yylval.s = (const char *) xmemdup (yytext, len, len+1);
update_lineno (yytext, yyleng);
return SCALAR;
}
Index: S-vn-gtsmall/gcc/gengtype-yacc.y
===================================================================
--- S-vn-gtsmall.orig/gcc/gengtype-yacc.y 2007-03-17 16:08:39.000000000 -0700
+++ S-vn-gtsmall/gcc/gengtype-yacc.y 2007-03-17 16:08:44.000000000 -0700
@@ -47,7 +47,7 @@
%token NESTED_PTR
%token <s>PARAM_IS
%token NUM
-%token <t>SCALAR
+%token <s>SCALAR
%token <s>ID
%token <s>STRING
%token <s>ARRAY
@@ -158,7 +158,7 @@
;
type: SCALAR
- { $$ = $1; }
+ { $$ = create_scalar_type ($1); }
| ID
{ $$ = resolve_typedef ($1, &lexer_line); }
| VEC_TOKEN '(' ID ',' ID ')'
@@ -175,9 +175,9 @@
| UNION ID
{ $$ = find_structure ($2, 1); }
| ENUM ID
- { $$ = create_scalar_type ($2, strlen ($2)); }
+ { $$ = create_scalar_type ($2); }
| ENUM ID '{' enum_items '}'
- { $$ = create_scalar_type ($2, strlen ($2)); }
+ { $$ = create_scalar_type ($2); }
;
enum_items: /* empty */
--