Add a "deref" unary operator.

This commit is contained in:
Nigel Tao committed 2017-10-20 15:44:30 +11:00
1 parent 51bd52239b
commit 2cd6ebed8e
8 files changed
+55 -53

No files matched your search

+6 -11
View File
@@ -362,14 +362,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
} }
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
switch n.ID0().Key() { b.writes(cOpNames[0xFF&n.ID0().Key()])
case t.KeyXUnaryPlus:
b.writeb('+')
case t.KeyXUnaryMinus:
b.writeb('-')
case t.KeyXUnaryNot:
b.writeb('!')
}
return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth) return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth)
} }
@@ -528,9 +521,11 @@ var cOpNames = [256]string{
t.KeyPercentEq: " %= ", t.KeyPercentEq: " %= ",
t.KeyTildePlusEq: " += ", t.KeyTildePlusEq: " += ",
t.KeyXUnaryPlus: "+", t.KeyXUnaryPlus: " + ",
t.KeyXUnaryMinus: "-", t.KeyXUnaryMinus: " - ",
t.KeyXUnaryNot: "!", t.KeyXUnaryNot: " ! ",
t.KeyXUnaryRef: " & ",
t.KeyXUnaryDeref: " * ",
t.KeyXBinaryPlus: " + ", t.KeyXBinaryPlus: " + ",
t.KeyXBinaryMinus: " - ", t.KeyXBinaryMinus: " - ",
+6 -4
View File
@@ -247,22 +247,24 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
name := v.Name().String(g.tm) name := v.Name().String(g.tm)
b.writes("{\n") b.writes("{\n")
// TODO: don't assume that the slice is a slice of u8. // TODO: don't assume that the slice is a slice of u8. In particular,
// the code gen can be subtle if the slice element type has zero size,
// such as the empty struct.
b.printf("puffs_base_slice_u8 %sslice_%s =", iPrefix, name) b.printf("puffs_base_slice_u8 %sslice_%s =", iPrefix, name)
if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil { if err := g.writeExpr(b, v.Value(), replaceCallSuspendibles, parenthesesOptional, 0); err != nil {
return err return err
} }
b.writes(";\n") b.writes(";\n")
b.printf("uint8_t* %sptr_%s = %sslice_%s.ptr;\n", iPrefix, name, iPrefix, name) b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name)
b.printf("uint8_t* %send_%s = %sslice_%s.ptr + %sslice_%s.len;\n", b.printf("uint8_t* %send_%s = %sslice_%s.ptr + %sslice_%s.len;\n",
iPrefix, name, iPrefix, name, iPrefix, name) iPrefix, name, iPrefix, name, iPrefix, name)
b.printf("while (%sptr_%s < %send_%s) {\n", iPrefix, name, iPrefix, name) b.printf("while (%s%s < %send_%s) {\n", vPrefix, name, iPrefix, name)
for _, o := range n.Body() { for _, o := range n.Body() {
if err := g.writeStatement(b, o, depth); err != nil { if err := g.writeStatement(b, o, depth); err != nil {
return err return err
} }
} }
b.printf("%sptr_%s++;\n}\n", iPrefix, name) b.printf("%s%s++;\n}\n", vPrefix, name)
b.writes("}\n") b.writes("}\n")
return nil return nil
+5 -13
View File
@@ -2425,7 +2425,6 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self,
uint32_t v_s1; uint32_t v_s1;
uint32_t v_s2; uint32_t v_s2;
puffs_base_slice_u8 v_remaining; puffs_base_slice_u8 v_remaining;
uint64_t v_i;
v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1)); v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1));
v_s2 = ((self->private_impl.f_state) >> (32 - (16))); v_s2 = ((self->private_impl.f_state) >> (32 - (16)));
@@ -2434,24 +2433,17 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self,
if (((uint64_t)(a_x.len)) > 5552) { if (((uint64_t)(a_x.len)) > 5552) {
v_remaining = puffs_base_slice_u8_subslice_i(a_x, 5552); v_remaining = puffs_base_slice_u8_subslice_i(a_x, 5552);
a_x = puffs_base_slice_u8_subslice_j(a_x, 5552); a_x = puffs_base_slice_u8_subslice_j(a_x, 5552);
if (((uint64_t)(a_x.len)) > 5552) {
return 0;
}
} }
{ {
puffs_base_slice_u8 i_slice_p = a_x; puffs_base_slice_u8 i_slice_p = a_x;
uint8_t* i_ptr_p = i_slice_p.ptr; uint8_t* v_p = i_slice_p.ptr;
uint8_t* i_end_p = i_slice_p.ptr + i_slice_p.len; uint8_t* i_end_p = i_slice_p.ptr + i_slice_p.len;
while (i_ptr_p < i_end_p) { while (v_p < i_end_p) {
i_ptr_p++; v_s1 += ((uint32_t)(*v_p));
v_s2 += v_s1;
v_p++;
} }
} }
v_i = 0;
while (v_i < ((uint64_t)(a_x.len))) {
v_s1 += ((uint32_t)(a_x.ptr[v_i]));
v_s2 += v_s1;
v_i += 1;
}
v_s1 %= 65521; v_s1 %= 65521;
v_s2 %= 65521; v_s2 %= 65521;
a_x = v_remaining; a_x = v_remaining;
+2
View File
@@ -138,6 +138,8 @@ var opStrings = [256]string{
t.KeyXUnaryPlus: "+", t.KeyXUnaryPlus: "+",
t.KeyXUnaryMinus: "-", t.KeyXUnaryMinus: "-",
t.KeyXUnaryNot: "not ", t.KeyXUnaryNot: "not ",
t.KeyXUnaryRef: "ref ",
t.KeyXUnaryDeref: "deref ",
t.KeyXBinaryPlus: " + ", t.KeyXBinaryPlus: " + ",
t.KeyXBinaryMinus: " - ", t.KeyXBinaryMinus: " - ",
+2
View File
@@ -920,6 +920,8 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int
return neg(rMax), neg(rMin), nil return neg(rMax), neg(rMin), nil
case t.KeyXUnaryNot: case t.KeyXUnaryNot:
return zero, one, nil return zero, one, nil
case t.KeyXUnaryRef, t.KeyXUnaryDeref:
return q.bcheckTypeExpr(n.MType())
} }
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key()) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprUnaryOp", n.ID0().Key())
+11
View File
@@ -796,6 +796,17 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
} }
n.SetMType(typeExprBool) n.SetMType(typeExprBool)
return nil return nil
case t.KeyXUnaryRef:
// TODO.
case t.KeyXUnaryDeref:
if rTyp.Decorator().Key() != t.KeyPtr { // TODO: t.KeyNptr?
return fmt.Errorf("check: %q is a dereference of a non-pointer type %q",
n.String(q.tm), rTyp.String(q.tm))
}
n.SetMType(rTyp.Inner())
return nil
} }
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key())
} }
+22 -8
View File
@@ -208,10 +208,12 @@ const (
KeyGreaterThan = Key(IDGreaterThan >> KeyShift) KeyGreaterThan = Key(IDGreaterThan >> KeyShift)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
KeyAnd = Key(IDAnd >> KeyShift) KeyAnd = Key(IDAnd >> KeyShift)
KeyOr = Key(IDOr >> KeyShift) KeyOr = Key(IDOr >> KeyShift)
KeyNot = Key(IDNot >> KeyShift) KeyNot = Key(IDNot >> KeyShift)
KeyAs = Key(IDAs >> KeyShift) KeyAs = Key(IDAs >> KeyShift)
KeyRef = Key(IDRef >> KeyShift)
KeyDeref = Key(IDDeref >> KeyShift)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
KeyFunc = Key(IDFunc >> KeyShift) KeyFunc = Key(IDFunc >> KeyShift)
@@ -302,6 +304,8 @@ const (
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
KeyXUnaryNot = Key(IDXUnaryNot >> KeyShift) KeyXUnaryNot = Key(IDXUnaryNot >> KeyShift)
KeyXUnaryRef = Key(IDXUnaryRef >> KeyShift)
KeyXUnaryDeref = Key(IDXUnaryDeref >> KeyShift)
KeyXBinaryPlus = Key(IDXBinaryPlus >> KeyShift) KeyXBinaryPlus = Key(IDXBinaryPlus >> KeyShift)
KeyXBinaryMinus = Key(IDXBinaryMinus >> KeyShift) KeyXBinaryMinus = Key(IDXBinaryMinus >> KeyShift)
@@ -390,10 +394,12 @@ const (
IDGreaterThan = ID(0x55<<KeyShift | FlagsBinaryOp) IDGreaterThan = ID(0x55<<KeyShift | FlagsBinaryOp)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
IDAnd = ID(0x58<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp) IDAnd = ID(0x58<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDOr = ID(0x59<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp) IDOr = ID(0x59<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
IDNot = ID(0x5A<<KeyShift | FlagsUnaryOp) IDNot = ID(0x5A<<KeyShift | FlagsUnaryOp)
IDAs = ID(0x5B<<KeyShift | FlagsBinaryOp) IDAs = ID(0x5B<<KeyShift | FlagsBinaryOp)
IDRef = ID(0x5C<<KeyShift | FlagsUnaryOp)
IDDeref = ID(0x5D<<KeyShift | FlagsUnaryOp)
// TODO: sort these by name, when the list has stabilized. // TODO: sort these by name, when the list has stabilized.
IDFunc = ID(0x60<<KeyShift | FlagsOther) IDFunc = ID(0x60<<KeyShift | FlagsOther)
@@ -491,6 +497,8 @@ const (
IDXUnaryPlus = ID(0xD0<<KeyShift | FlagsUnaryOp) IDXUnaryPlus = ID(0xD0<<KeyShift | FlagsUnaryOp)
IDXUnaryMinus = ID(0xD1<<KeyShift | FlagsUnaryOp) IDXUnaryMinus = ID(0xD1<<KeyShift | FlagsUnaryOp)
IDXUnaryNot = ID(0xD2<<KeyShift | FlagsUnaryOp) IDXUnaryNot = ID(0xD2<<KeyShift | FlagsUnaryOp)
IDXUnaryRef = ID(0xD3<<KeyShift | FlagsUnaryOp)
IDXUnaryDeref = ID(0xD4<<KeyShift | FlagsUnaryOp)
IDXBinaryPlus = ID(0xD8<<KeyShift | FlagsBinaryOp) IDXBinaryPlus = ID(0xD8<<KeyShift | FlagsBinaryOp)
IDXBinaryMinus = ID(0xD9<<KeyShift | FlagsBinaryOp) IDXBinaryMinus = ID(0xD9<<KeyShift | FlagsBinaryOp)
@@ -593,6 +601,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyNot: {"not", IDNot}, KeyNot: {"not", IDNot},
KeyAs: {"as", IDAs}, KeyAs: {"as", IDAs},
// TODO: do we need a "~as" form of the as operator, similar to "~+". // TODO: do we need a "~as" form of the as operator, similar to "~+".
KeyRef: {"ref", IDRef},
KeyDeref: {"deref", IDDeref},
KeyFunc: {"func", IDFunc}, KeyFunc: {"func", IDFunc},
KeyPtr: {"ptr", IDPtr}, KeyPtr: {"ptr", IDPtr},
@@ -787,6 +797,8 @@ var ambiguousForms = [256]ID{
KeyXUnaryPlus: IDPlus, KeyXUnaryPlus: IDPlus,
KeyXUnaryMinus: IDMinus, KeyXUnaryMinus: IDMinus,
KeyXUnaryNot: IDNot, KeyXUnaryNot: IDNot,
KeyXUnaryRef: IDRef,
KeyXUnaryDeref: IDDeref,
KeyXBinaryPlus: IDPlus, KeyXBinaryPlus: IDPlus,
KeyXBinaryMinus: IDMinus, KeyXBinaryMinus: IDMinus,
@@ -823,6 +835,8 @@ var unaryForms = [256]ID{
KeyPlus: IDXUnaryPlus, KeyPlus: IDXUnaryPlus,
KeyMinus: IDXUnaryMinus, KeyMinus: IDXUnaryMinus,
KeyNot: IDXUnaryNot, KeyNot: IDXUnaryNot,
KeyRef: IDXUnaryRef,
KeyDeref: IDXUnaryDeref,
} }
var binaryForms = [256]ID{ var binaryForms = [256]ID{
+1 -17
View File
@@ -59,27 +59,11 @@ pri func adler32.update(x[] u8)(checksum u32) {
if in.x.length() > 5552 { if in.x.length() > 5552 {
remaining = in.x[5552:] remaining = in.x[5552:]
in.x = in.x[:5552] in.x = in.x[:5552]
// TODO: this redundant if should be unnecessary.
if in.x.length() > 5552 {
return 0 // Unreachable.
}
} }
iterate (p ptr u8:in.x) { iterate (p ptr u8:in.x) {
// TODO: uncomment this loop body, and delete the while loop below. s1 ~+= deref p as u32
// s1 ~+= deref p as u32
// s2 ~+= s1
}
// TODO: can u64 be u32? Does it matter?
var i u64
while i < in.x.length(),
inv in.x.length() <= 5552,
{
assert i < 5552 via "a < b: a < c; c <= b"(c:in.x.length())
s1 ~+= in.x[i] as u32
s2 ~+= s1 s2 ~+= s1
i += 1
} }
s1 %= 65521 s1 %= 65521