Generate C break's and continue's.

This commit is contained in:
Nigel Tao committed 2017-06-01 15:25:01 +10:00
1 parent 2ed019b670
commit bcf6a7ee1d
6 files changed
+115 -24

No files matched your search

+44 -6
View File
@@ -55,16 +55,32 @@ const (
)
type gen struct {
buffer bytes.Buffer
pkgName string
idMap *t.IDMap
files []*a.File
buffer bytes.Buffer
pkgName string
idMap *t.IDMap
files []*a.File
jumpTargets map[*a.While]uint32
}
func (g *gen) printf(format string, args ...interface{}) { fmt.Fprintf(&g.buffer, format, args...) }
func (g *gen) writeb(b byte) { g.buffer.WriteByte(b) }
func (g *gen) writes(s string) { g.buffer.WriteString(s) }
func (g *gen) jumpTarget(n *a.While) (uint32, error) {
if g.jumpTargets == nil {
g.jumpTargets = map[*a.While]uint32{}
}
if jt, ok := g.jumpTargets[n]; ok {
return jt, nil
}
jt := uint32(len(g.jumpTargets))
if jt == 1000000 {
return 0, fmt.Errorf("too many jump targets")
}
g.jumpTargets[n] = jt
return jt, nil
}
func (g *gen) generate() error {
includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H"
g.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
@@ -295,6 +311,7 @@ func (g *gen) writeFuncPrototype(n *a.Func) error {
}
func (g *gen) writeFuncImpl(n *a.Func) error {
g.jumpTargets = nil
g.writeFuncSignature(n)
g.writes("{\n")
@@ -450,7 +467,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
// TODO.
case a.KJump:
// TODO.
n := n.Jump()
jt := g.jumpTargets[n.JumpTarget()]
keyword := "continue"
if n.Keyword().Key() == t.KeyBreak {
keyword = "break"
}
g.printf("goto label_%d_%s;\n", jt, keyword)
return nil
case a.KReturn:
// TODO.
@@ -470,6 +494,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
case a.KWhile:
n := n.While()
if n.HasContinue() {
jt, err := g.jumpTarget(n)
if err != nil {
return err
}
g.printf("label_%d_continue:;\n", jt)
}
g.writes("while (")
if err := g.writeExpr(n.Condition(), 0); err != nil {
return err
@@ -481,8 +513,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
}
}
g.writes("}\n")
if n.HasBreak() {
jt, err := g.jumpTarget(n)
if err != nil {
return err
}
g.printf("label_%d_break:;\n", jt)
}
return nil
// TODO.
}
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
+7 -3
View File
@@ -162,10 +162,14 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
v_bits = 0;
v_n_bits = 0;
v_width = (self->f_literal_width + 1);
while ((v_n_bits < v_width)) {
v_bits |= 0;
v_n_bits += 8;
while (1) {
while ((v_n_bits < v_width)) {
v_bits |= 0;
v_n_bits += 8;
}
goto label_0_break;
}
label_0_break:;
cleanup0:
self->status = status;
+21 -8
View File
@@ -72,6 +72,8 @@ const (
FlagsSuspendible = Flags(0x00000002)
FlagsPublic = Flags(0x00000004)
FlagsTypeChecked = Flags(0x00000008)
FlagsHasBreak = Flags(0x00000010)
FlagsHasContinue = Flags(0x00000020)
)
type Node struct {
@@ -80,6 +82,7 @@ type Node struct {
constValue *big.Int
mType *TypeExpr
jumpTarget *While
filename string
line uint32
@@ -320,17 +323,24 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
}
// While is "while:ID1 LHS, List0 { List1 }":
// - FlagsHasBreak is the while has an explicit break
// - FlagsHasContinue is the while has an explicit continue
// - ID1: <0|label>
// - LHS: <Expr>
// - List0: <Assert> asserts
// - List1: <Statement> loop body
type While Node
func (n *While) Node() *Node { return (*Node)(n) }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.lhs.Expr() }
func (n *While) Asserts() []*Node { return n.list0 }
func (n *While) Body() []*Node { return n.list1 }
func (n *While) Node() *Node { return (*Node)(n) }
func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.lhs.Expr() }
func (n *While) Asserts() []*Node { return n.list0 }
func (n *While) Body() []*Node { return n.list1 }
func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak }
func (n *While) SetHasContinue() { n.flags |= FlagsHasContinue }
func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While {
return &While{
@@ -384,9 +394,12 @@ func NewReturn(value *Expr) *Return {
// - ID1: <0|label>
type Jump Node
func (n *Jump) Node() *Node { return (*Node)(n) }
func (n *Jump) Keyword() t.ID { return n.id0 }
func (n *Jump) Label() t.ID { return n.id1 }
func (n *Jump) Node() *Node { return (*Node)(n) }
func (n *Jump) JumpTarget() *While { return n.jumpTarget }
func (n *Jump) Keyword() t.ID { return n.id0 }
func (n *Jump) Label() t.ID { return n.id1 }
func (n *Jump) SetJumpTarget(o *While) { n.jumpTarget = o }
func NewJump(keyword t.ID, label t.ID) *Jump {
return &Jump{
+2
View File
@@ -378,5 +378,7 @@ type checker struct {
errFilename string
errLine uint32
jumpTargets []*a.While
facts facts
}
+30 -1
View File
@@ -73,7 +73,32 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
case a.KJump:
// TODO: check that we're in a for loop.
n := n.Jump()
jumpTarget := (*a.While)(nil)
if id := n.Label(); id != 0 {
for i := len(q.jumpTargets) - 1; i >= 0; i-- {
if w := q.jumpTargets[i]; w.Label() == id {
jumpTarget = w
break
}
}
} else if nj := len(q.jumpTargets); nj > 0 {
jumpTarget = q.jumpTargets[nj-1]
}
if jumpTarget == nil {
sepStr, labelStr := "", ""
if id := n.Label(); id != 0 {
sepStr, labelStr = ":", id.String(q.idMap)
}
return fmt.Errorf("no matching while statement for %s%s%s",
n.Keyword().String(q.idMap), sepStr, labelStr)
}
if n.Keyword().Key() == t.KeyBreak {
jumpTarget.SetHasBreak()
} else {
jumpTarget.SetHasContinue()
}
n.SetJumpTarget(jumpTarget)
case a.KReturn:
n := n.Return()
@@ -113,6 +138,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
o.SetTypeChecked()
}
q.jumpTargets = append(q.jumpTargets, n)
defer func() {
q.jumpTargets = q.jumpTargets[:len(q.jumpTargets)-1]
}()
for _, o := range n.Body() {
if err := q.tcheckStatement(o); err != nil {
return err
+11 -6
View File
@@ -17,11 +17,16 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
// TODO: implement.
while n_bits < width,
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
bits |= 0 // TODO.
n_bits += 8
while true {
while n_bits < width,
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
bits |= 0 // TODO.
n_bits += 8
}
// TODO: implement.
break
}
}