Skip to content

Commit 1b43154

Browse files
author
Gaius Mulley
committed
PR modula2/118998 Rotate of a packetset causes different types to binary operator error
This patch allow a packedset to be rotated by the system module intrinsic procedure function. It ensures that both operands to the tree rotate are of the same type. In turn the result will be the same type and the assignment into the designator (of the same set type) will succeed. gcc/m2/ChangeLog: PR modula2/118998 * gm2-gcc/m2expr.cc (m2expr_BuildLRotate): Convert nBits to the return type. (m2expr_BuildRRotate): Ditto. (m2expr_BuildLogicalRotate): Convert op3 to an integer type. Replace op3 aith rotateCount. Negate rotateCount if it is negative and call rotate right. * gm2-gcc/m2pp.cc (m2pp_bit_and_expr): New function. (m2pp_binary_function): Ditto. (m2pp_simple_expression): BIT_AND_EXPR new case clause. LROTATE_EXPR ditto. RROTATE_EXPR ditto. gcc/testsuite/ChangeLog: PR modula2/118998 * gm2/iso/pass/testrotate.mod: New test. * gm2/pim/fail/tinyconst.mod: New test. * gm2/sets/run/pass/simplepacked.mod: New test. Signed-off-by: Gaius Mulley <[email protected]>
1 parent c7449f1 commit 1b43154

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

gcc/m2/gm2-gcc/m2expr.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ m2expr_BuildLRotate (location_t location, tree op1, tree nBits,
673673

674674
op1 = m2expr_FoldAndStrip (op1);
675675
nBits = m2expr_FoldAndStrip (nBits);
676+
nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);
676677
t = m2expr_build_binary_op (location, LROTATE_EXPR, op1, nBits, needconvert);
677678
return m2expr_FoldAndStrip (t);
678679
}
@@ -688,6 +689,7 @@ m2expr_BuildRRotate (location_t location, tree op1, tree nBits,
688689

689690
op1 = m2expr_FoldAndStrip (op1);
690691
nBits = m2expr_FoldAndStrip (nBits);
692+
nBits = m2convert_BuildConvert (location, TREE_TYPE (op1), nBits, needconvert);
691693
t = m2expr_build_binary_op (location, RROTATE_EXPR, op1, nBits, needconvert);
692694
return m2expr_FoldAndStrip (t);
693695
}
@@ -801,18 +803,17 @@ m2expr_BuildLogicalRotate (location_t location, tree op1, tree op2, tree op3,
801803
{
802804
char *labelElseName = createUniqueLabel ();
803805
char *labelEndName = createUniqueLabel ();
804-
tree is_less = m2expr_BuildLessThan (location,
805-
m2convert_ToInteger (location, op3),
806+
tree rotateCount = m2convert_ToInteger (location, op3);
807+
tree is_less = m2expr_BuildLessThan (location, rotateCount,
806808
m2expr_GetIntegerZero (location));
807809

808810
m2statement_DoJump (location, is_less, NULL, labelElseName);
809-
res = m2expr_BuildLRLn (location, op2, op3, nBits, needconvert);
811+
res = m2expr_BuildLRLn (location, op2, rotateCount, nBits, needconvert);
810812
m2statement_BuildAssignmentTree (location, op1, res);
811813
m2statement_BuildGoto (location, labelEndName);
812814
m2statement_DeclareLabel (location, labelElseName);
813-
res = m2expr_BuildLRRn (location, op2,
814-
m2expr_BuildNegate (location, op3, needconvert),
815-
nBits, needconvert);
815+
rotateCount = m2expr_BuildNegate (location, rotateCount, needconvert);
816+
res = m2expr_BuildLRRn (location, op2, rotateCount, nBits, needconvert);
816817
m2statement_BuildAssignmentTree (location, op1, res);
817818
m2statement_DeclareLabel (location, labelEndName);
818819
}

gcc/m2/gm2-gcc/m2pp.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,14 @@ m2pp_bit_ior_expr (pretty *s, tree t)
19221922
m2pp_binary (s, t, "|");
19231923
}
19241924

1925+
/* m2pp_bit_and_expr generate a C style bit and. */
1926+
1927+
static void
1928+
m2pp_bit_and_expr (pretty *s, tree t)
1929+
{
1930+
m2pp_binary (s, t, "&");
1931+
}
1932+
19251933
/* m2pp_truth_expr. */
19261934

19271935
static void
@@ -1938,6 +1946,21 @@ m2pp_truth_expr (pretty *s, tree t, const char *op)
19381946
m2pp_print (s, ")");
19391947
}
19401948

1949+
/* m2pp_binary_function handle GCC expression tree as a function. */
1950+
1951+
static void
1952+
m2pp_binary_function (pretty *s, tree t, const char *funcname)
1953+
{
1954+
m2pp_print (s, funcname);
1955+
m2pp_needspace (s);
1956+
m2pp_print (s, "(");
1957+
m2pp_expression (s, TREE_OPERAND (t, 0));
1958+
m2pp_print (s, ",");
1959+
m2pp_needspace (s);
1960+
m2pp_expression (s, TREE_OPERAND (t, 1));
1961+
m2pp_print (s, ")");
1962+
}
1963+
19411964
/* m2pp_simple_expression handle GCC expression tree. */
19421965

19431966
static void
@@ -2085,12 +2108,21 @@ m2pp_simple_expression (pretty *s, tree t)
20852108
case BIT_IOR_EXPR:
20862109
m2pp_bit_ior_expr (s, t);
20872110
break;
2111+
case BIT_AND_EXPR:
2112+
m2pp_bit_and_expr (s, t);
2113+
break;
20882114
case TRUTH_ANDIF_EXPR:
20892115
m2pp_truth_expr (s, t, "AND");
20902116
break;
20912117
case TRUTH_ORIF_EXPR:
20922118
m2pp_truth_expr (s, t, "OR");
20932119
break;
2120+
case LROTATE_EXPR:
2121+
m2pp_binary_function (s, t, "LROTATE");
2122+
break;
2123+
case RROTATE_EXPR:
2124+
m2pp_binary_function (s, t, "RROTATE");
2125+
break;
20942126
default:
20952127
m2pp_unknown (s, __FUNCTION__, get_tree_code_name (code));
20962128
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
MODULE testrotate ;
2+
3+
IMPORT SYSTEM;
4+
5+
VAR
6+
v: PACKEDSET OF [0..31];
7+
i: INTEGER;
8+
BEGIN
9+
i := 3;
10+
v := SYSTEM.ROTATE (v, i);
11+
END testrotate.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MODULE tinyconst ;
2+
CONST
3+
Int = 16 ;
4+
Real = 1.0 + Int ;
5+
6+
END tinyconst.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
MODULE simplepacked ;
2+
3+
FROM libc IMPORT printf, exit ;
4+
FROM SYSTEM IMPORT TBITSIZE, ROTATE ;
5+
6+
TYPE
7+
settype = SET OF [0..8] ;
8+
psettype = PACKEDSET OF [0..8] ;
9+
10+
11+
PROCEDURE assert (cond: BOOLEAN; line: CARDINAL; message: ARRAY OF CHAR) ;
12+
BEGIN
13+
IF NOT cond
14+
THEN
15+
printf ("assert failed %s at line %d\n", message, line) ;
16+
exit (1)
17+
END
18+
END assert ;
19+
20+
21+
PROCEDURE testset ;
22+
VAR
23+
a, b: settype ;
24+
BEGIN
25+
a := settype {1} ;
26+
b := a ;
27+
(* assert (TBITSIZE (a) = 4, __LINE__, "TBITSIZE = 4") ; *)
28+
assert (a = b, __LINE__, "comparision between variable sets") ;
29+
assert (a = settype {1}, __LINE__, "comparision between variable and constant sets") ;
30+
assert (b = settype {1}, __LINE__, "comparision between variable and constant sets") ;
31+
assert (settype {1} = settype {1}, __LINE__, "comparision between constant sets") ;
32+
assert (settype {1} # settype {2}, __LINE__, "comparision between constant sets") ;
33+
assert (ROTATE (settype {1}, 1) = ROTATE (settype {1}, 1), __LINE__, "comparision between constant rotated sets") ;
34+
assert (ROTATE (settype {1}, 1) # ROTATE (settype {2}, 1), __LINE__, "comparision between constant rotated sets") ;
35+
assert (ROTATE (a, 1) = settype {2}, __LINE__, "comparision between rotated variable and constant sets") ;
36+
assert (ROTATE (a, -1) = settype {0}, __LINE__, "comparision between rotated variable and constant sets") ;
37+
END testset ;
38+
39+
40+
PROCEDURE testpset ;
41+
VAR
42+
a, b: psettype ;
43+
BEGIN
44+
a := psettype {1} ;
45+
b := a ;
46+
(* assert (TBITSIZE (a) = 4, __LINE__, "TBITSIZE = 4 packed set") ; *)
47+
assert (a = b, __LINE__, "comparision between variable packed sets") ;
48+
assert (a = psettype {1}, __LINE__, "comparision between variable and constant packed sets") ;
49+
assert (b = psettype {1}, __LINE__, "comparision between variable and constant packed sets") ;
50+
assert (psettype {1} = psettype {1}, __LINE__, "comparision between constant packed sets") ;
51+
assert (psettype {1} # psettype {2}, __LINE__, "comparision between constant packed sets") ;
52+
assert (ROTATE (psettype {1}, 1) = ROTATE (psettype {1}, 1), __LINE__, "comparision between constant rotated packed sets") ;
53+
assert (ROTATE (psettype {1}, 1) # ROTATE (psettype {2}, 1), __LINE__, "comparision between constant rotated packed sets") ;
54+
assert (ROTATE (a, 1) = psettype {2}, __LINE__, "comparision between rotated variable and constant packed sets") ;
55+
assert (ROTATE (a, -1) = settype {0}, __LINE__, "comparision between rotated variable and constant packed sets") ;
56+
END testpset ;
57+
58+
59+
BEGIN
60+
testset ;
61+
testpset
62+
END simplepacked.

0 commit comments

Comments
 (0)