mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 00:52:18 +01:00
Generate C break's and continue's.
This commit is contained in:
1 parent
2ed019b670
commit
bcf6a7ee1d
6 files changed
+115
-24
No files matched your search
+44
-6
@@ -55,16 +55,32 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type gen struct {
|
type gen struct {
|
||||||
buffer bytes.Buffer
|
buffer bytes.Buffer
|
||||||
pkgName string
|
pkgName string
|
||||||
idMap *t.IDMap
|
idMap *t.IDMap
|
||||||
files []*a.File
|
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) printf(format string, args ...interface{}) { fmt.Fprintf(&g.buffer, format, args...) }
|
||||||
func (g *gen) writeb(b byte) { g.buffer.WriteByte(b) }
|
func (g *gen) writeb(b byte) { g.buffer.WriteByte(b) }
|
||||||
func (g *gen) writes(s string) { g.buffer.WriteString(s) }
|
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 {
|
func (g *gen) generate() error {
|
||||||
includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
||||||
g.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
|
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 {
|
func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||||
|
g.jumpTargets = nil
|
||||||
g.writeFuncSignature(n)
|
g.writeFuncSignature(n)
|
||||||
g.writes("{\n")
|
g.writes("{\n")
|
||||||
|
|
||||||
@@ -450,7 +467,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
// TODO.
|
// TODO.
|
||||||
|
|
||||||
case a.KJump:
|
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:
|
case a.KReturn:
|
||||||
// TODO.
|
// TODO.
|
||||||
@@ -470,6 +494,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
|
|
||||||
case a.KWhile:
|
case a.KWhile:
|
||||||
n := n.While()
|
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 (")
|
g.writes("while (")
|
||||||
if err := g.writeExpr(n.Condition(), 0); err != nil {
|
if err := g.writeExpr(n.Condition(), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -481,8 +513,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.writes("}\n")
|
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
|
return nil
|
||||||
// TODO.
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
||||||
|
|||||||
+7
-3
@@ -162,10 +162,14 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
|||||||
v_bits = 0;
|
v_bits = 0;
|
||||||
v_n_bits = 0;
|
v_n_bits = 0;
|
||||||
v_width = (self->f_literal_width + 1);
|
v_width = (self->f_literal_width + 1);
|
||||||
while ((v_n_bits < v_width)) {
|
while (1) {
|
||||||
v_bits |= 0;
|
while ((v_n_bits < v_width)) {
|
||||||
v_n_bits += 8;
|
v_bits |= 0;
|
||||||
|
v_n_bits += 8;
|
||||||
|
}
|
||||||
|
goto label_0_break;
|
||||||
}
|
}
|
||||||
|
label_0_break:;
|
||||||
|
|
||||||
cleanup0:
|
cleanup0:
|
||||||
self->status = status;
|
self->status = status;
|
||||||
|
|||||||
+21
-8
@@ -72,6 +72,8 @@ const (
|
|||||||
FlagsSuspendible = Flags(0x00000002)
|
FlagsSuspendible = Flags(0x00000002)
|
||||||
FlagsPublic = Flags(0x00000004)
|
FlagsPublic = Flags(0x00000004)
|
||||||
FlagsTypeChecked = Flags(0x00000008)
|
FlagsTypeChecked = Flags(0x00000008)
|
||||||
|
FlagsHasBreak = Flags(0x00000010)
|
||||||
|
FlagsHasContinue = Flags(0x00000020)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
@@ -80,6 +82,7 @@ type Node struct {
|
|||||||
|
|
||||||
constValue *big.Int
|
constValue *big.Int
|
||||||
mType *TypeExpr
|
mType *TypeExpr
|
||||||
|
jumpTarget *While
|
||||||
|
|
||||||
filename string
|
filename string
|
||||||
line uint32
|
line uint32
|
||||||
@@ -320,17 +323,24 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// While is "while:ID1 LHS, List0 { List1 }":
|
// 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>
|
// - ID1: <0|label>
|
||||||
// - LHS: <Expr>
|
// - LHS: <Expr>
|
||||||
// - List0: <Assert> asserts
|
// - List0: <Assert> asserts
|
||||||
// - List1: <Statement> loop body
|
// - List1: <Statement> loop body
|
||||||
type While Node
|
type While Node
|
||||||
|
|
||||||
func (n *While) Node() *Node { return (*Node)(n) }
|
func (n *While) Node() *Node { return (*Node)(n) }
|
||||||
func (n *While) Label() t.ID { return n.id1 }
|
func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
|
||||||
func (n *While) Condition() *Expr { return n.lhs.Expr() }
|
func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
|
||||||
func (n *While) Asserts() []*Node { return n.list0 }
|
func (n *While) Label() t.ID { return n.id1 }
|
||||||
func (n *While) Body() []*Node { return n.list1 }
|
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 {
|
func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While {
|
||||||
return &While{
|
return &While{
|
||||||
@@ -384,9 +394,12 @@ func NewReturn(value *Expr) *Return {
|
|||||||
// - ID1: <0|label>
|
// - ID1: <0|label>
|
||||||
type Jump Node
|
type Jump Node
|
||||||
|
|
||||||
func (n *Jump) Node() *Node { return (*Node)(n) }
|
func (n *Jump) Node() *Node { return (*Node)(n) }
|
||||||
func (n *Jump) Keyword() t.ID { return n.id0 }
|
func (n *Jump) JumpTarget() *While { return n.jumpTarget }
|
||||||
func (n *Jump) Label() t.ID { return n.id1 }
|
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 {
|
func NewJump(keyword t.ID, label t.ID) *Jump {
|
||||||
return &Jump{
|
return &Jump{
|
||||||
|
|||||||
@@ -378,5 +378,7 @@ type checker struct {
|
|||||||
errFilename string
|
errFilename string
|
||||||
errLine uint32
|
errLine uint32
|
||||||
|
|
||||||
|
jumpTargets []*a.While
|
||||||
|
|
||||||
facts facts
|
facts facts
|
||||||
}
|
}
|
||||||
+30
-1
@@ -73,7 +73,32 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case a.KJump:
|
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:
|
case a.KReturn:
|
||||||
n := n.Return()
|
n := n.Return()
|
||||||
@@ -113,6 +138,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
}
|
}
|
||||||
o.SetTypeChecked()
|
o.SetTypeChecked()
|
||||||
}
|
}
|
||||||
|
q.jumpTargets = append(q.jumpTargets, n)
|
||||||
|
defer func() {
|
||||||
|
q.jumpTargets = q.jumpTargets[:len(q.jumpTargets)-1]
|
||||||
|
}()
|
||||||
for _, o := range n.Body() {
|
for _, o := range n.Body() {
|
||||||
if err := q.tcheckStatement(o); err != nil {
|
if err := q.tcheckStatement(o); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -17,11 +17,16 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
|
|||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
|
|
||||||
while n_bits < width,
|
while true {
|
||||||
post n_bits >= width,
|
while n_bits < width,
|
||||||
{
|
post n_bits >= width,
|
||||||
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
|
{
|
||||||
bits |= 0 // TODO.
|
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
|
||||||
n_bits += 8
|
bits |= 0 // TODO.
|
||||||
|
n_bits += 8
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: implement.
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user