Fix shift/rotate for all integer types, add tests, make build order work for SYSTEM.c/h changes.

This commit is contained in:
David Brown 2016-09-12 15:55:54 +01:00
parent 74a085dda3
commit 9baf4c9429
217 changed files with 334 additions and 2550 deletions

View file

@ -625,7 +625,7 @@ MODULE OPV; (* J. Templ 16.2.95 / 3.7.96
OPM.WriteInt(-r^.conval^.intval)
ELSE expr(r, MinPrec)
END ;
IF subclass IN {OPT.lsh, OPT.rot} THEN OPM.WriteString(Comma); OPC.Ident(l^.typ^.strobj) END ;
IF subclass IN {OPT.lsh, OPT.rot} THEN OPM.WriteString(Comma); OPM.WriteInt(l.typ.size*8) END;
OPM.Write(CloseParen)
| OPS.eql
.. OPS.geq: IF l^.typ^.form IN {OPT.String, OPT.Comp} THEN

View file

@ -33,14 +33,11 @@
#if defined (__o_64)
#if defined(_WIN64)
typedef unsigned long long size_t;
// typedef long long address;
#else
typedef unsigned long size_t;
// typedef long address;
#endif
#else
typedef unsigned int size_t;
//typedef int address;
#endif
#define _SIZE_T_DECLARED // For FreeBSD
@ -71,14 +68,6 @@ typedef signed char int8;
typedef unsigned char uint8;
// 'address' is a synonym for an int32 of pointer size
#if defined (__o_64)
#define address int64
#else
#define address int32
#endif
// The compiler uses 'import' and 'export' which translate to 'extern' and
// nothing respectively.
@ -98,9 +87,7 @@ typedef unsigned char uint8;
typedef int8 BOOLEAN;
typedef int8 SYSTEM_BYTE;
typedef uint8 uSYSTEM_BYTE;
typedef uint8 CHAR;
typedef uint8 uCHAR;
typedef float REAL;
typedef double LONGREAL;
typedef void* SYSTEM_PTR;
@ -108,6 +95,16 @@ typedef void* SYSTEM_PTR;
#define uSET SET
// 'address' is a synonym for an int of pointer size
#if defined (__o_64)
#define address int64
#else
#define address int32
#endif
// ----------------------------------------------------------------------
@ -200,13 +197,13 @@ static inline int __str_cmp(CHAR *x, CHAR *y){
#define __GET(a, x, t) x=*(t*)(address)(a)
#define __PUT(a, x, t) *(t*)(address)(a)=x
#define __LSHL(x, n, t) ((t)((u##t)(x)<<(n)))
#define __LSHR(x, n, t) ((t)((u##t)(x)>>(n)))
#define __LSH(x, n, t) ((n)>=0? __LSHL(x, n, t): __LSHR(x, -(n), t))
#define __LSHL(x, n, s) ((int##s)((uint##s)(x)<<(n)))
#define __LSHR(x, n, s) ((int##s)((uint##s)(x)>>(n)))
#define __LSH(x, n, s) ((n)>=0? __LSHL(x, n, s): __LSHR(x, -(n), s))
#define __ROTL(x, n, t) ((t)((u##t)(x)<<(n)|(u##t)(x)>>(8*sizeof(t)-(n))))
#define __ROTR(x, n, t) ((t)((u##t)(x)>>(n)|(u##t)(x)<<(8*sizeof(t)-(n))))
#define __ROT(x, n, t) ((n)>=0? __ROTL(x, n, t): __ROTR(x, -(n), t))
#define __ROTL(x, n, s) ((int##s)((uint##s)(x)<<(n)|(uint##s)(x)>>(s-(n))))
#define __ROTR(x, n, s) ((int##s)((uint##s)(x)>>(n)|(uint##s)(x)<<(s-(n))))
#define __ROT(x, n, s) ((n)>=0? __ROTL(x, n, s): __ROTR(x, -(n), s))
#define __ASHL(x, n) ((int64)(x)<<(n))
#define __ASHR(x, n) ((int64)(x)>>(n))

View file

@ -2,8 +2,20 @@ MODULE TestLanguage;
IMPORT SYSTEM, Console;
PROCEDURE TestShiftResult(of, by, actual, expected: LONGINT; msg: ARRAY OF CHAR);
BEGIN
IF actual # expected THEN
Console.String(msg);
Console.String(" of $"); Console.Hex(of);
Console.String(" by "); Console.Int(by,1);
Console.String(" is $"); Console.Hex(actual);
Console.String(" but should be $"); Console.Hex(expected);
Console.Ln;
END
END TestShiftResult;
PROCEDURE Shift;
VAR c: CHAR; b: SYSTEM.BYTE; s,t,u: SHORTINT; h,i,j,k: INTEGER; l,m,n: LONGINT;
VAR c: CHAR; b: SYSTEM.BYTE; s,t,u: SHORTINT; h,i,j,k: INTEGER; l,m,n,r: LONGINT;
(*
Aritmetic shift always returns type LONGINT. Defined as x * 2**n.
LSH and ROT produces results of the same type as the value being shifted.
@ -13,8 +25,8 @@ BEGIN
i := 0; m := 1;
WHILE i < SIZE(LONGINT)*8 DO
l := 1; l := SYSTEM.LSH(l,i); ASSERT(l = m, 16);
l := 1; l := SYSTEM.ROT(l,i); ASSERT(l = m, 17);
l := 1; r := SYSTEM.LSH(l,i); TestShiftResult(l, i, r, m, "LSH");
l := 1; r := SYSTEM.ROT(l,i); TestShiftResult(l, i, r, m, "ROT(1)");
m := m * 2; INC(i);
END;
@ -84,9 +96,55 @@ BEGIN
END;
(* Positive LSH shifts and ROTs with overflow *)
i := 1; m := 1;
WHILE i < SIZE(LONGINT)*8 DO
l := MAX(LONGINT); INC(l); r := SYSTEM.LSH(l,i); TestShiftResult(l, i, r, 0, "LSH");
l := MAX(LONGINT); INC(l); r := SYSTEM.ROT(l,i); TestShiftResult(l, i, r, m, "ROT(2)");
m := m * 2; INC(i);
END;
i := 1; k := 1;
WHILE i < SIZE(INTEGER)*8 DO
j := MAX(INTEGER); INC(j); r := SYSTEM.LSH(j,i); TestShiftResult(j, i, r, 0, "LSH");
j := MAX(INTEGER); INC(j); r := SYSTEM.ROT(j,i); TestShiftResult(j, i, r, k, "ROT(3)");
k := k * 2; INC(i);
END;
i := 1; t := 1;
WHILE i < SIZE(SHORTINT)*8 DO
s := MAX(SHORTINT); INC(s); r := SYSTEM.LSH(s,i); TestShiftResult(s, i, r, 0, "LSH");
s := MAX(SHORTINT); INC(s); r := SYSTEM.ROT(s,i); TestShiftResult(s, i, r, t, "ROT(4)");
t := t * 2; INC(i);
END;
(* Negative LSH shifts and ROTs without overflow *)
i := -1; m := MAX(LONGINT); INC(m);
WHILE i > -SIZE(LONGINT)*8 DO
l := 1; r := SYSTEM.LSH(l,i); TestShiftResult(l, i, r, 0, "LSH");
l := 1; r := SYSTEM.ROT(l,i); TestShiftResult(l, i, r, m, "ROT");
m := SYSTEM.LSH(m,-1); (* m := m DIV 2; *)
DEC(i);
END;
i := -1; k := MAX(INTEGER); INC(k);
WHILE i > -SIZE(INTEGER)*8 DO
j := 1; r := SYSTEM.LSH(j,i); TestShiftResult(j, i, r, 0, "LSH");
j := 1; r := SYSTEM.ROT(j,i); TestShiftResult(j, i, r, k, "ROT");
k := SYSTEM.LSH(k,-1); (* k := k DIV 2; *)
DEC(i);
END;
i := -1; t := MAX(SHORTINT); INC(t);
WHILE i > -SIZE(SHORTINT)*8 DO
s := 1; r := SYSTEM.LSH(s,i); TestShiftResult(s, i, r, 0, "LSH");
s := 1; r := SYSTEM.ROT(s,i); TestShiftResult(s, i, r, t, "ROT");
t := SYSTEM.LSH(t,-1); (* t := t DIV 2; *)
DEC(i);
END;
(* Also need tests that bits that are shifted / rotated off the end
are zeroed or wrapped correctly. *)
(* Also need full tests for CHAR, and poossibly SYSTEM.BYTE. Here's a simple one *)
@ -138,7 +196,6 @@ BEGIN
=> x MOd y = x - ((x DIV y) * y)
*)
i := 4; j := 3; TestValue(i MOD j, i - ((i DIV j) * j), "4 MOD 3");
i := 5; j := 3; TestValue(i MOD j, i - ((i DIV j) * j), "5 MOD 3");
i := 6; j := 3; TestValue(i MOD j, i - ((i DIV j) * j), "6 MOD 3");
@ -161,6 +218,8 @@ BEGIN
END DivMod;
PROCEDURE IntSize;
VAR l: LONGINT;
BEGIN
@ -187,6 +246,9 @@ BEGIN
END;
END IntSize;
BEGIN
Shift;
DivMod;

View file

@ -1 +1 @@
09 Sep 2016 14:20:47
12 Sep 2016 15:40:20

View file

@ -70,6 +70,8 @@ assemble:
SYSTEM.o Configuration.o Platform.o Heap.o Console.o Strings.o Modules.o Files.o \
Reals.o Texts.o vt100.o errors.o OPM.o extTools.o OPS.o OPT.o \
OPC.o OPV.o OPB.o OPP.o
cp src/system/*.[ch] $(BUILDDIR)
@printf "$(VISHAP) created.\n"
@ -79,7 +81,9 @@ compilerfromsavedsource:
@echo Populating clean build directory from bootstrap C sources.
@mkdir -p $(BUILDDIR)
@cp bootstrap/$(PLATFORM)-$(ADRSIZE)$(ALIGNMENT)/* $(BUILDDIR)
@cp bootstrap/*.[ch] $(BUILDDIR)
@make -f src/tools/make/vishap.make -s assemble
@cp bootstrap/*.[ch] $(BUILDDIR)
@ -119,8 +123,6 @@ translate:
cd $(BUILDDIR); $(ROOTDIR)/$(VISHAP) -SsfF -B$(INTSIZE)$(ADRSIZE)$(ALIGNMENT) ../../src/compiler/OPP.Mod
cd $(BUILDDIR); $(ROOTDIR)/$(VISHAP) -Ssfm -B$(INTSIZE)$(ADRSIZE)$(ALIGNMENT) ../../src/compiler/Vishap.Mod
cp src/system/*.[ch] $(BUILDDIR)
@printf "$(BUILDDIR) filled with compiler C source.\n"