@@ -149,6 +149,53 @@ static char linebuf[LINELEN];
static int line;
static int pos;
+/* Used to determine whether a type can be void (only return types). */
+enum void_status {
+ VOID_NOTOK,
+ VOID_OK
+};
+
+/* Legal base types for an argument or return type. */
+enum basetype {
+ BT_CHAR,
+ BT_SHORT,
+ BT_INT,
+ BT_LONGLONG,
+ BT_FLOAT,
+ BT_DOUBLE,
+ BT_INT128,
+ BT_FLOAT128
+};
+
+/* Ways in which a const int value can be restricted. RES_BITS indicates
+ that the integer is restricted to val1 bits, interpreted as signed or
+ unsigned depending on whether the type is signed or unsigned. RES_RANGE
+ indicates that the integer is restricted to values between val1 and val2,
+ inclusive. RES_VALUES indicates that the integer must have one of the
+ values val1 or val2. */
+enum restriction {
+ RES_NONE,
+ RES_BITS,
+ RES_RANGE,
+ RES_VALUES
+};
+
+/* Type modifiers for an argument or return type. */
+struct typeinfo {
+ char isvoid;
+ char isconst;
+ char isvector;
+ char issigned;
+ char isunsigned;
+ char isbool;
+ char ispixel;
+ char ispointer;
+ basetype base;
+ restriction restr;
+ int val1;
+ int val2;
+};
+
/* Exit codes for the shell. */
enum exit_codes {
EC_INTERR
@@ -268,3 +315,409 @@ match_integer ()
sscanf (buf, "%d", &x);
return x;
}
+
+/* Match one of the allowable base types. Consumes one token unless the
+ token is "long", which must be paired with a second "long". Return 1
+ for success, 0 for failure. */
+static int
+match_basetype (typeinfo *typedata)
+{
+ consume_whitespace ();
+ int oldpos = pos;
+ char *token = match_identifier ();
+ if (!token)
+ {
+ (*diag) ("missing base type in return type at column %d\n", pos + 1);
+ return 0;
+ }
+
+ if (!strcmp (token, "char"))
+ typedata->base = BT_CHAR;
+ else if (!strcmp (token, "short"))
+ typedata->base = BT_SHORT;
+ else if (!strcmp (token, "int"))
+ typedata->base = BT_INT;
+ else if (!strcmp (token, "long"))
+ {
+ consume_whitespace ();
+ char *mustbelong = match_identifier ();
+ if (!mustbelong || strcmp (mustbelong, "long"))
+ {
+ (*diag) ("incomplete 'long long' at column %d\n", oldpos + 1);
+ return 0;
+ }
+ typedata->base = BT_LONGLONG;
+ }
+ else if (!strcmp (token, "float"))
+ typedata->base = BT_FLOAT;
+ else if (!strcmp (token, "double"))
+ typedata->base = BT_DOUBLE;
+ else if (!strcmp (token, "__int128"))
+ typedata->base = BT_INT128;
+ else if (!strcmp (token, "_Float128"))
+ typedata->base = BT_FLOAT128;
+ else
+ {
+ (*diag) ("unrecognized base type at column %d\n", oldpos + 1);
+ return 0;
+ }
+
+ return 1;
+}
+
+/* A const int argument may be restricted to certain values. This is
+ indicated by one of the following occurring after the "int' token:
+
+ <x> restricts the constant to x bits, interpreted as signed or
+ unsigned according to the argument type
+ <x,y> restricts the constant to the inclusive range [x,y]
+ {x,y} restricts the constant to one of two values, x or y.
+
+ Here x and y are integer tokens. Return 1 for success, else 0. */
+static int
+match_const_restriction (typeinfo *typedata)
+{
+ int oldpos = pos;
+ if (linebuf[pos] == '<')
+ {
+ safe_inc_pos ();
+ oldpos = pos;
+ int x = match_integer ();
+ if (x == MININT)
+ {
+ (*diag) ("malformed integer at column %d.\n", oldpos + 1);
+ return 0;
+ }
+ consume_whitespace ();
+ if (linebuf[pos] == '>')
+ {
+ typedata->restr = RES_BITS;
+ typedata->val1 = x;
+ safe_inc_pos ();
+ return 1;
+ }
+ else if (linebuf[pos] != ',')
+ {
+ (*diag) ("malformed restriction at column %d.\n", pos + 1);
+ return 0;
+ }
+ safe_inc_pos ();
+ oldpos = pos;
+ int y = match_integer ();
+ if (y == MININT)
+ {
+ (*diag) ("malformed integer at column %d.\n", oldpos + 1);
+ return 0;
+ }
+ typedata->restr = RES_RANGE;
+ typedata->val1 = x;
+ typedata->val2 = y;
+
+ consume_whitespace ();
+ if (linebuf[pos] != '>')
+ {
+ (*diag) ("malformed restriction at column %d.\n", pos + 1);
+ return 0;
+ }
+ safe_inc_pos ();
+ }
+ else
+ {
+ assert (linebuf[pos] == '{');
+ safe_inc_pos ();
+ oldpos = pos;
+ int x = match_integer ();
+ if (x == MININT)
+ {
+ (*diag) ("malformed integer at column %d.\n", oldpos + 1);
+ return 0;
+ }
+ consume_whitespace ();
+ if (linebuf[pos] != ',')
+ {
+ (*diag) ("missing comma at column %d.\n", pos + 1);
+ return 0;
+ }
+ consume_whitespace ();
+ oldpos = pos;
+ int y = match_integer ();
+ if (y == MININT)
+ {
+ (*diag) ("malformed integer at column %d.\n", oldpos + 1);
+ return 0;
+ }
+ typedata->restr = RES_VALUES;
+ typedata->val1 = x;
+ typedata->val2 = y;
+
+ consume_whitespace ();
+ if (linebuf[pos] != '}')
+ {
+ (*diag) ("malformed restriction at column %d.\n", pos + 1);
+ return 0;
+ }
+ safe_inc_pos ();
+ }
+
+ return 1;
+}
+
+/* Look for a type, which can be terminated by a token that is not part of
+ a type, a comma, or a closing parenthesis. Place information about the
+ type in TYPEDATA. Return 1 for success, 0 for failure. */
+static int
+match_type (typeinfo *typedata, int voidok)
+{
+ /* A legal type is of the form:
+
+ [const] [[signed|unsigned] <basetype> | <vectype>] [*]
+
+ where "const" applies only to a <basetype> of "int". Legal values
+ of <basetype> are (for now):
+
+ char
+ short
+ int
+ long long
+ float
+ double
+ __int128
+ _Float128
+
+ Legal values of <vectype> are as follows, and are shorthand for
+ the associated meaning:
+
+ vsc vector signed char
+ vuc vector unsigned char
+ vbc vector bool char
+ vss vector signed short
+ vus vector unsigned short
+ vbs vector bool short
+ vsi vector signed int
+ vui vector unsigned int
+ vbi vector bool int
+ vsll vector signed long long
+ vull vector unsigned long long
+ vbll vector bool long long
+ vsq vector signed __int128
+ vuq vector unsigned __int128
+ vbq vector bool __int128
+ vp vector pixel
+ vf vector float
+ vd vector double
+
+ For simplicity, We don't support "short int" and "long long int".
+ We don't support a <basetype> of "bool", "long double", or "_Float16",
+ but will add these if builtins require it. "signed" and "unsigned"
+ only apply to integral base types. The optional * indicates a pointer
+ type, and can currently only be used with void. */
+
+ consume_whitespace ();
+ memset (typedata, 0, sizeof(*typedata));
+ int oldpos = pos;
+
+ char *token = match_identifier ();
+ if (!token)
+ return 0;
+
+ if (!strcmp (token, "void"))
+ typedata->isvoid = 1;
+ if (!strcmp (token, "const"))
+ typedata->isconst = 1;
+ else if (!strcmp (token, "vsc"))
+ {
+ typedata->isvector = 1;
+ typedata->issigned = 1;
+ typedata->base = BT_CHAR;
+ return 1;
+ }
+ else if (!strcmp (token, "vuc"))
+ {
+ typedata->isvector = 1;
+ typedata->isunsigned = 1;
+ typedata->base = BT_CHAR;
+ return 1;
+ }
+ else if (!strcmp (token, "vbc"))
+ {
+ typedata->isvector = 1;
+ typedata->isbool = 1;
+ typedata->base = BT_CHAR;
+ return 1;
+ }
+ else if (!strcmp (token, "vss"))
+ {
+ typedata->isvector = 1;
+ typedata->issigned = 1;
+ typedata->base = BT_SHORT;
+ return 1;
+ }
+ else if (!strcmp (token, "vus"))
+ {
+ typedata->isvector = 1;
+ typedata->isunsigned = 1;
+ typedata->base = BT_SHORT;
+ return 1;
+ }
+ else if (!strcmp (token, "vbs"))
+ {
+ typedata->isvector = 1;
+ typedata->isbool = 1;
+ typedata->base = BT_SHORT;
+ return 1;
+ }
+ else if (!strcmp (token, "vsi"))
+ {
+ typedata->isvector = 1;
+ typedata->issigned = 1;
+ typedata->base = BT_INT;
+ return 1;
+ }
+ else if (!strcmp (token, "vui"))
+ {
+ typedata->isvector = 1;
+ typedata->isunsigned = 1;
+ typedata->base = BT_INT;
+ return 1;
+ }
+ else if (!strcmp (token, "vbi"))
+ {
+ typedata->isvector = 1;
+ typedata->isbool = 1;
+ typedata->base = BT_INT;
+ return 1;
+ }
+ else if (!strcmp (token, "vsll"))
+ {
+ typedata->isvector = 1;
+ typedata->issigned = 1;
+ typedata->base = BT_LONGLONG;
+ return 1;
+ }
+ else if (!strcmp (token, "vull"))
+ {
+ typedata->isvector = 1;
+ typedata->isunsigned = 1;
+ typedata->base = BT_LONGLONG;
+ return 1;
+ }
+ else if (!strcmp (token, "vbll"))
+ {
+ typedata->isvector = 1;
+ typedata->isbool = 1;
+ typedata->base = BT_LONGLONG;
+ return 1;
+ }
+ else if (!strcmp (token, "vsq"))
+ {
+ typedata->isvector = 1;
+ typedata->issigned = 1;
+ typedata->base = BT_INT128;
+ return 1;
+ }
+ else if (!strcmp (token, "vuq"))
+ {
+ typedata->isvector = 1;
+ typedata->isunsigned = 1;
+ typedata->base = BT_INT128;
+ return 1;
+ }
+ else if (!strcmp (token, "vbq"))
+ {
+ typedata->isvector = 1;
+ typedata->isbool = 1;
+ typedata->base = BT_INT128;
+ return 1;
+ }
+ else if (!strcmp (token, "vp"))
+ {
+ typedata->isvector = 1;
+ typedata->ispixel = 1;
+ typedata->base = BT_SHORT;
+ return 1;
+ }
+ else if (!strcmp (token, "vf"))
+ {
+ typedata->isvector = 1;
+ typedata->base = BT_FLOAT;
+ return 1;
+ }
+ else if (!strcmp (token, "vd"))
+ {
+ typedata->isvector = 1;
+ typedata->base = BT_DOUBLE;
+ return 1;
+ }
+ else if (!strcmp (token, "signed"))
+ typedata->issigned = 1;
+ else if (!strcmp (token, "unsigned"))
+ typedata->isunsigned = 1;
+ else if (!typedata->isvoid)
+ {
+ pos = oldpos;
+ return match_basetype (typedata);
+ }
+
+ if (typedata->isvoid)
+ {
+ consume_whitespace ();
+ if (linebuf[pos] == '*')
+ {
+ typedata->ispointer = 1;
+ safe_inc_pos ();
+ }
+ else if (!voidok)
+ return 0;
+ return 1;
+ }
+
+ if (typedata->isconst)
+ {
+ consume_whitespace ();
+ oldpos = pos;
+ token = match_identifier ();
+ if (!strcmp (token, "signed"))
+ {
+ typedata->issigned = 1;
+ consume_whitespace ();
+ oldpos = pos;
+ token = match_identifier ();
+ if (strcmp (token, "int"))
+ {
+ (*diag) ("'signed' not followed by 'int' at column %d.\n",
+ oldpos + 1);
+ return 0;
+ }
+ }
+ else if (!strcmp (token, "unsigned"))
+ {
+ typedata->isunsigned = 1;
+ consume_whitespace ();
+ oldpos = pos;
+ token = match_identifier ();
+ if (strcmp (token, "int"))
+ {
+ (*diag) ("'unsigned' not followed by 'int' at column %d.\n",
+ oldpos + 1);
+ return 0;
+ }
+ }
+ else if (strcmp (token, "int"))
+ {
+ (*diag) ("'const' not followed by 'int' at column %d.\n",
+ oldpos + 1);
+ return 0;
+ }
+
+ typedata->base = BT_INT;
+
+ consume_whitespace ();
+ if (linebuf[pos] == '<' || linebuf[pos] == '{')
+ return match_const_restriction (typedata);
+
+ return 1;
+ }
+
+ consume_whitespace ();
+ return match_basetype (typedata);
+}