mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
Generate C function bodies.
This commit is contained in:
1 parent
509e6b48a4
commit
5d0b664ccf
6 files changed
+285
-24
No files matched your search
+223
-7
@@ -305,9 +305,14 @@ func writeFuncImpl(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Func) error
|
|||||||
}
|
}
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
|
|
||||||
// TODO: generate the function body.
|
// Generate the function body.
|
||||||
|
for _, o := range n.Body() {
|
||||||
|
if err := writeStatement(b, pkgName, m, o, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
|
|
||||||
if cleanup0 {
|
if cleanup0 {
|
||||||
fmt.Fprintf(b, "cleanup0: self->status = status;\n")
|
fmt.Fprintf(b, "cleanup0: self->status = status;\n")
|
||||||
}
|
}
|
||||||
@@ -369,11 +374,7 @@ func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uin
|
|||||||
x := n.Var().XType()
|
x := n.Var().XType()
|
||||||
if k := x.Name().Key(); k < t.Key(len(cTypeNames)) {
|
if k := x.Name().Key(); k < t.Key(len(cTypeNames)) {
|
||||||
if s := cTypeNames[k]; s != "" {
|
if s := cTypeNames[k]; s != "" {
|
||||||
fmt.Fprintf(b, "%s %s;\n", s, n.Var().Name().String(m))
|
fmt.Fprintf(b, "%s v_%s;\n", s, n.Var().Name().String(m))
|
||||||
// TODO: remove this hack for "unused variable" warning. In the
|
|
||||||
// long term, we want unused variables to fail noisily.
|
|
||||||
name := n.Var().Name().String(m)
|
|
||||||
fmt.Fprintf(b, "%s = 0; if (%s) {}\n", name, name)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,6 +392,175 @@ func writeVars(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uin
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeStatement(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Node, depth uint32) error {
|
||||||
|
if depth > a.MaxBodyDepth {
|
||||||
|
return fmt.Errorf("cgen: body recursion depth too large")
|
||||||
|
}
|
||||||
|
depth++
|
||||||
|
|
||||||
|
switch n.Kind() {
|
||||||
|
case a.KAssert:
|
||||||
|
// Assertions only apply at compile-time.
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case a.KAssign:
|
||||||
|
n := n.Assign()
|
||||||
|
if err := writeExpr(b, pkgName, m, n.LHS(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// TODO: does KeyAmpHatEq need special consideration?
|
||||||
|
b.WriteString(cOpNames[0xFF&n.Operator().Key()])
|
||||||
|
if err := writeExpr(b, pkgName, m, n.RHS(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.WriteString(";\n")
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case a.KIf:
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case a.KJump:
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case a.KReturn:
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case a.KVar:
|
||||||
|
n := n.Var()
|
||||||
|
fmt.Fprintf(b, "v_%s = ", n.Name().String(m))
|
||||||
|
if v := n.Value(); v != nil {
|
||||||
|
if err := writeExpr(b, pkgName, m, v, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
b.WriteByte('0')
|
||||||
|
}
|
||||||
|
b.WriteString(";\n")
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case a.KWhile:
|
||||||
|
n := n.While()
|
||||||
|
b.WriteString("while (")
|
||||||
|
if err := writeExpr(b, pkgName, m, n.Condition(), 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.WriteString(") {\n")
|
||||||
|
for _, o := range n.Body() {
|
||||||
|
if err := writeStatement(b, pkgName, m, o, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.WriteString("}\n")
|
||||||
|
return nil
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
}
|
||||||
|
return fmt.Errorf("cgen: unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeExpr(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error {
|
||||||
|
if depth > a.MaxExprDepth {
|
||||||
|
return fmt.Errorf("cgen: expression recursion depth too large")
|
||||||
|
}
|
||||||
|
depth++
|
||||||
|
|
||||||
|
if cv := n.ConstValue(); cv != nil {
|
||||||
|
// TODO: write false/true instead of 0/1 if n.MType() is bool?
|
||||||
|
b.WriteString(cv.String())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
||||||
|
case 0:
|
||||||
|
if err := writeExprOther(b, pkgName, m, n, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case t.FlagsUnaryOp:
|
||||||
|
if err := writeExprUnaryOp(b, pkgName, m, n, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case t.FlagsBinaryOp:
|
||||||
|
if err := writeExprBinaryOp(b, pkgName, m, n, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case t.FlagsAssociativeOp:
|
||||||
|
if err := writeExprAssociativeOp(b, pkgName, m, n, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("cgen: unrecognized token.Key (0x%X) for writeExpr", n.ID0().Key())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeExprOther(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error {
|
||||||
|
switch n.ID0().Key() {
|
||||||
|
case 0:
|
||||||
|
if id1 := n.ID1(); id1.Key() == t.KeyThis {
|
||||||
|
b.WriteString("self")
|
||||||
|
} else {
|
||||||
|
// TODO: don't assume that the v_ prefix is necessary.
|
||||||
|
b.WriteString("v_")
|
||||||
|
b.WriteString(id1.String(m))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case t.KeyOpenParen:
|
||||||
|
// n is a function call.
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case t.KeyOpenBracket:
|
||||||
|
// n is an index.
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case t.KeyColon:
|
||||||
|
// n is a slice.
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case t.KeyDot:
|
||||||
|
if err := writeExpr(b, pkgName, m, n.LHS().Expr(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// TODO: choose between . vs -> operators.
|
||||||
|
//
|
||||||
|
// TODO: don't assume that the f_ prefix is necessary.
|
||||||
|
b.WriteString("->f_")
|
||||||
|
b.WriteString(n.ID1().String(m))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("cgen: unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeExprUnaryOp(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error {
|
||||||
|
// TODO.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeExprBinaryOp(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error {
|
||||||
|
op := n.ID0()
|
||||||
|
if op.Key() == t.KeyXBinaryAs {
|
||||||
|
// TODO.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
b.WriteByte('(')
|
||||||
|
if err := writeExpr(b, pkgName, m, n.LHS().Expr(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// TODO: does KeyXBinaryAmpHat need special consideration?
|
||||||
|
b.WriteString(cOpNames[0xFF&op.Key()])
|
||||||
|
if err := writeExpr(b, pkgName, m, n.RHS().Expr(), depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.WriteByte(')')
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeExprAssociativeOp(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Expr, depth uint32) error {
|
||||||
|
// TODO.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func format(rawSource *bytes.Buffer) ([]byte, error) {
|
func format(rawSource *bytes.Buffer) ([]byte, error) {
|
||||||
stdout := &bytes.Buffer{}
|
stdout := &bytes.Buffer{}
|
||||||
cmd := exec.Command("clang-format", "-style=Chromium")
|
cmd := exec.Command("clang-format", "-style=Chromium")
|
||||||
@@ -415,3 +585,49 @@ var cTypeNames = [...]string{
|
|||||||
t.KeyUsize: "size_t",
|
t.KeyUsize: "size_t",
|
||||||
t.KeyBool: "bool",
|
t.KeyBool: "bool",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cOpNames = [256]string{
|
||||||
|
t.KeyEq: " = ",
|
||||||
|
t.KeyPlusEq: " += ",
|
||||||
|
t.KeyMinusEq: " -= ",
|
||||||
|
t.KeyStarEq: " *= ",
|
||||||
|
t.KeySlashEq: " /= ",
|
||||||
|
t.KeyShiftLEq: " <<= ",
|
||||||
|
t.KeyShiftREq: " >>= ",
|
||||||
|
t.KeyAmpEq: " &= ",
|
||||||
|
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
|
||||||
|
t.KeyPipeEq: " |= ",
|
||||||
|
t.KeyHatEq: " ^= ",
|
||||||
|
|
||||||
|
t.KeyXUnaryPlus: "+",
|
||||||
|
t.KeyXUnaryMinus: "-",
|
||||||
|
t.KeyXUnaryNot: "!",
|
||||||
|
|
||||||
|
t.KeyXBinaryPlus: " + ",
|
||||||
|
t.KeyXBinaryMinus: " - ",
|
||||||
|
t.KeyXBinaryStar: " * ",
|
||||||
|
t.KeyXBinarySlash: " / ",
|
||||||
|
t.KeyXBinaryShiftL: " << ",
|
||||||
|
t.KeyXBinaryShiftR: " >> ",
|
||||||
|
t.KeyXBinaryAmp: " & ",
|
||||||
|
t.KeyXBinaryAmpHat: " no_such_amp_hat_C_operator ",
|
||||||
|
t.KeyXBinaryPipe: " | ",
|
||||||
|
t.KeyXBinaryHat: " ^ ",
|
||||||
|
t.KeyXBinaryNotEq: " != ",
|
||||||
|
t.KeyXBinaryLessThan: " < ",
|
||||||
|
t.KeyXBinaryLessEq: " <= ",
|
||||||
|
t.KeyXBinaryEqEq: " == ",
|
||||||
|
t.KeyXBinaryGreaterEq: " >= ",
|
||||||
|
t.KeyXBinaryGreaterThan: " > ",
|
||||||
|
t.KeyXBinaryAnd: " && ",
|
||||||
|
t.KeyXBinaryOr: " || ",
|
||||||
|
t.KeyXBinaryAs: " no_such_as_C_operator ",
|
||||||
|
|
||||||
|
t.KeyXAssociativePlus: " + ",
|
||||||
|
t.KeyXAssociativeStar: " * ",
|
||||||
|
t.KeyXAssociativeAmp: " & ",
|
||||||
|
t.KeyXAssociativePipe: " | ",
|
||||||
|
t.KeyXAssociativeHat: " ^ ",
|
||||||
|
t.KeyXAssociativeAnd: " && ",
|
||||||
|
t.KeyXAssociativeOr: " || ",
|
||||||
|
}
|
||||||
+10
-11
@@ -141,17 +141,16 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
|||||||
goto cleanup0;
|
goto cleanup0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t bits;
|
uint32_t v_bits;
|
||||||
bits = 0;
|
uint32_t v_n_bits;
|
||||||
if (bits) {
|
uint32_t v_width;
|
||||||
}
|
|
||||||
uint32_t n_bits;
|
v_bits = 0;
|
||||||
n_bits = 0;
|
v_n_bits = 0;
|
||||||
if (n_bits) {
|
v_width = (self->f_literal_width + 1);
|
||||||
}
|
while ((v_n_bits < v_width)) {
|
||||||
uint32_t width;
|
v_bits |= 0;
|
||||||
width = 0;
|
v_n_bits += 8;
|
||||||
if (width) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup0:
|
cleanup0:
|
||||||
|
|||||||
+13
-4
@@ -322,7 +322,7 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
|
|||||||
// While is "while LHS, List0 { List1 }":
|
// While is "while LHS, List0 { List1 }":
|
||||||
// - LHS: <Expr>
|
// - LHS: <Expr>
|
||||||
// - List0: <Assert> asserts
|
// - List0: <Assert> asserts
|
||||||
// - List1: <*> 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) }
|
||||||
@@ -342,8 +342,8 @@ func NewWhile(condition *Expr, asserts []*Node, body []*Node) *While {
|
|||||||
// If is "if LHS { List0 } else RHS" or "if LHS { List0 } else { List1 }":
|
// If is "if LHS { List0 } else RHS" or "if LHS { List0 } else { List1 }":
|
||||||
// - LHS: <Expr>
|
// - LHS: <Expr>
|
||||||
// - RHS: <nil|If>
|
// - RHS: <nil|If>
|
||||||
// - List0: <*> if-true body
|
// - List0: <Statement> if-true body
|
||||||
// - List1: <*> if-false body
|
// - List1: <Statement> if-false body
|
||||||
type If Node
|
type If Node
|
||||||
|
|
||||||
func (n *If) Node() *Node { return (*Node)(n) }
|
func (n *If) Node() *Node { return (*Node)(n) }
|
||||||
@@ -455,7 +455,16 @@ const MaxBodyDepth = 255
|
|||||||
// - LHS: <Struct> in-parameters
|
// - LHS: <Struct> in-parameters
|
||||||
// - RHS: <Struct> out-parameters
|
// - RHS: <Struct> out-parameters
|
||||||
// - List0: <Assert> asserts
|
// - List0: <Assert> asserts
|
||||||
// - List1: <*> function body
|
// - List1: <Statement> function body
|
||||||
|
//
|
||||||
|
// Statement means one of:
|
||||||
|
// - Assert
|
||||||
|
// - Assign
|
||||||
|
// - If
|
||||||
|
// - Jump
|
||||||
|
// - Return
|
||||||
|
// - Var
|
||||||
|
// - While
|
||||||
type Func Node
|
type Func Node
|
||||||
|
|
||||||
func (n *Func) Node() *Node { return (*Node)(n) }
|
func (n *Func) Node() *Node { return (*Node)(n) }
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
// No-op.
|
// No-op.
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind())
|
return fmt.Errorf("check: unrecognized ast.Kind (%s) for checkStatement", n.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+1
-1
@@ -120,7 +120,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
// TODO: check that we're in a for loop.
|
// TODO: check that we're in a for loop.
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind())
|
return fmt.Errorf("check: unrecognized ast.Kind (%s) for checkStatement", n.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
n.SetTypeChecked()
|
n.SetTypeChecked()
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
To manually run this test:
|
||||||
|
|
||||||
|
for cc in clang gcc; do
|
||||||
|
$cc -std=c99 -Wall -Werror lzw.c && ./a.out
|
||||||
|
rm -f a.out
|
||||||
|
done
|
||||||
|
|
||||||
|
Each edition should print "PASS", amongst other information, and exit(0).
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../../../gen/c/gif.c"
|
||||||
|
#include "../testlib/testlib.c"
|
||||||
|
|
||||||
|
const char* test_filename = "gif/lzw.c";
|
||||||
|
|
||||||
|
void test_lzw_decode() {
|
||||||
|
puffs_gif_lzw_decoder dec;
|
||||||
|
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||||
|
puffs_gif_status status = puffs_gif_lzw_decoder_decode(&dec);
|
||||||
|
if (status != puffs_gif_status_ok) {
|
||||||
|
FAIL("test_lzw_decode: status: got %d, want %d", status,
|
||||||
|
puffs_gif_status_ok);
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
cleanup0:
|
||||||
|
puffs_gif_lzw_decoder_destructor(&dec);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The empty comments forces clang-format to place one element per line.
|
||||||
|
test tests[] = {
|
||||||
|
test_lzw_decode, //
|
||||||
|
NULL, //
|
||||||
|
};
|
||||||
Reference in new issue
Block a user