| | @@ -868,19 +868,6 @@ |
| 868 | 868 | return t^.size * 8; |
| 869 | 869 | end; |
| 870 | 870 | |
| 871 | | -(* a div b and a mod b, of type t *) |
| 872 | | -proc foldDiv(op: Scan.Sym; a, b: I64; t: ^Sym.Type; p: Diag.Pos): (I64, ERROR); |
| 873 | | -begin |
| 874 | | - if b = 0 then return 0, Scan.markAt(p, "division by zero"); end; |
| 875 | | - if (t^.form = Sym.fInt) and not t^.signed then |
| 876 | | - if op = Scan.sDiv then return I64(U64(a) div U64(b)), nil; end; |
| 877 | | - return I64(U64(a) mod U64(b)), nil; |
| 878 | | - end; |
| 879 | | - if (b = -1) and (a = I64(-1) shl (bits(t) - 1)) then return 0, Scan.markAt(p, "division overflows"); end; |
| 880 | | - if op = Scan.sDiv then return a div b, nil; end; |
| 881 | | - return a mod b, nil; |
| 882 | | -end; |
| 883 | | - |
| 884 | 871 | proc foldInt(op: Scan.Sym; a, b: I64; t: ^Sym.Type; p: Diag.Pos): (I64, ERROR); |
| 885 | 872 | var u: BOOL; |
| 886 | 873 | begin |
| | @@ -892,8 +879,15 @@ |
| 892 | 879 | | sPlus: return a + b, nil; |
| 893 | 880 | | sMinus: return a - b, nil; |
| 894 | 881 | | sTimes: return a * b, nil; |
| 895 | | - | sDiv: return foldDiv(op, a, b, t, p); |
| 896 | | - | sMod: return foldDiv(op, a, b, t, p); |
| 882 | + | sDiv, sMod: |
| 883 | + if b = 0 then return 0, Scan.markAt(p, "division by zero"); end; |
| 884 | + if u then |
| 885 | + if op = Scan.sDiv then return I64(U64(a) div U64(b)), nil; end; |
| 886 | + return I64(U64(a) mod U64(b)), nil; |
| 887 | + end; |
| 888 | + if (b = -1) and (a = I64(-1) shl (bits(t) - 1)) then return 0, Scan.markAt(p, "division overflows"); end; |
| 889 | + if op = Scan.sDiv then return a div b, nil; end; |
| 890 | + return a mod b, nil; |
| 897 | 891 | | sAmp: return a & b, nil; |
| 898 | 892 | | sBar: return a | b, nil; |
| 899 | 893 | | sXor: return a xor b, nil; |
| | @@ -986,16 +980,6 @@ |
| 986 | 980 | return nil; |
| 987 | 981 | end; |
| 988 | 982 | |
| 989 | | -(* x <= y has no bit of x outside y; x >= y is y <= x *) |
| 990 | | -proc setSubset(op: Scan.Sym); |
| 991 | | -begin |
| 992 | | - if op = Scan.sGeq then Gen.swap(); end; |
| 993 | | - Gen.complement(8, false); |
| 994 | | - Gen.op(Scan.sAmp, Gen.kWord, 8, false); |
| 995 | | - Gen.pushConst(Gen.kWord, 0); |
| 996 | | - Gen.compare(Scan.sEql, Gen.kWord, false); |
| 997 | | -end; |
| 998 | | - |
| 999 | 983 | (* A set's operations, as operations on its bits: the two values on top *) |
| 1000 | 984 | proc setOp(op: Scan.Sym); |
| 1001 | 985 | begin |
| | @@ -1003,8 +987,13 @@ |
| 1003 | 987 | | sPlus: Gen.op(Scan.sBar, Gen.kWord, 8, false); |
| 1004 | 988 | | sTimes: Gen.op(Scan.sAmp, Gen.kWord, 8, false); |
| 1005 | 989 | | sMinus: Gen.complement(8, false); Gen.op(Scan.sAmp, Gen.kWord, 8, false); |
| 1006 | | - | sLeq: setSubset(op); |
| 1007 | | - | sGeq: setSubset(op); |
| 990 | + | sLeq, sGeq: |
| 991 | + (* x <= y has no bit of x outside y *) |
| 992 | + if op = Scan.sGeq then Gen.swap(); end; |
| 993 | + Gen.complement(8, false); |
| 994 | + Gen.op(Scan.sAmp, Gen.kWord, 8, false); |
| 995 | + Gen.pushConst(Gen.kWord, 0); |
| 996 | + Gen.compare(Scan.sEql, Gen.kWord, false); |
| 1008 | 997 | | sIn: |
| 1009 | 998 | Gen.swap(); |
| 1010 | 999 | Gen.op(Scan.sShr, Gen.kWord, 8, false); |