shithub: scc

Download patch

ref: bce263baaa569ae0a2eaa83e6483e456e172e56d
parent: 323563334c17544e1c9134dc8cd8e3cbdb33c1c9
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Apr 11 12:04:53 EDT 2014

Add logical bit operation

--- a/cc.h
+++ b/cc.h
@@ -220,7 +220,7 @@
 enum {
 	OCAST, OPTR, OADD, OARY, OSIZE, OMUL, OSUB,
 	OINC, ODEC, OPINC, OPDEC, ODIV, OMOD, OSHL,
-	OSHR, OLT, OGT, OGE, OLE, OEQ, ONE
+	OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND
 };
 
 extern void
--- a/code.c
+++ b/code.c
@@ -23,7 +23,8 @@
 	[OGE] = "]",
 	[OLE] =  "[",
 	[OEQ] = "=",
-	[ONE] = "!"
+	[ONE] = "!",
+	[OBAND] = "&"
 };
 
 Node *
--- a/expr.c
+++ b/expr.c
@@ -36,12 +36,17 @@
 	return np;
 }
 
-void
+static Node *
+promote(Node *np)
+{
+}
+
+static void
 intconv(Node **np1, Node **np2)
 {
 }
 
-void
+static void
 floatconv(Node **np1, Node **np2)
 {
 }
@@ -57,9 +62,6 @@
 	tp1 = UNQUAL(np1->type), tp2 = UNQUAL(np2->type);
 	t1 = tp1->op, t2 = tp2->op;
 
-	if ((op == OSHL || op == OSHR) && (t1 != INT || t2 != INT))
-		goto bad_shift;
-
 	switch (t1) {
 	case INT:
 		switch (t2) {
@@ -105,10 +107,11 @@
 				tp2 = mktype(tp2->type, PTR, NULL, 0);
 			else if (t2 != PTR)
 				goto incorrect;
+			/* FIX: result of comparisions is integer type */
 			break;
 		case OADD: OSUB:
-			tp3 = tp1->type;
-			if (!tp3->defined)
+			tp3 = tp1->type; /* TODO: I think tp3 is not needed */
+			if (!tp1->defined)
 				goto nocomplete;
 			if (t1 == ARY)
 				tp1 = mktype(tp1->type, PTR, NULL, 0);
@@ -128,9 +131,6 @@
 
 	return bincode(op, tp1, np1, np2);
 
-bad_shift:
-	err = "invalid operands to shift operator";
-	goto error;
 nocomplete:
 	err = "invalid use of indefined type";
 	goto error;
@@ -307,6 +307,21 @@
 }
 
 static Node *
+bitlogic(char op, Node *np1, Node *np2)
+{
+	Type *tp1, *tp2;
+	uint8_t t1, t2;
+
+	tp1 = UNQUAL(np1->type), tp2 = UNQUAL(np2->type);
+	t1 = tp1->op, t2 = tp2->op;
+
+	if (t1 != INT || t2 != INT)
+		error("No integer operand in bit logical operation");
+	intconv(&np1, &np2);
+	return bincode(op, np1->type, np1, np2);
+}
+
+static Node *
 shift(void)
 {
 	register char op;
@@ -320,7 +335,7 @@
 		default:  return np;
 		}
 		next();
-		np = arithmetic(op, np, add());
+		np = bitlogic(op, np, add());
 	}
 }
 
@@ -362,6 +377,19 @@
 	}
 }
 
+static Node *
+bit_and(void)
+{
+	register Node *np;
+
+	np = eq();
+	while (yytoken == '&') {
+		next();
+		np = bitlogic(OBAND, np, eq());
+	}
+	return np;
+}
+
 Node *
 expr(void)
 {
@@ -368,7 +396,7 @@
 	register Node *np;
 
 	do
-		np = eq();
+		np = bit_and();
 	while (yytoken == ',');
 	return np;
 }