Tweak the status code int32_t representation.

This commit is contained in:
Nigel Tao committed 2017-07-06 12:27:55 +10:00
1 parent 17b518376f
commit f7f57eaa21
5 files changed
+149 -95

No files matched your search

+4 -4
View File
@@ -93,7 +93,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
if n.Receiver() != 0 {
g.writes("self->private_impl.status;\n")
if n.Public() {
g.writes("if (status & 1) { return status; }")
g.writes("if (status < 0) { return status; }")
}
} else {
g.printf("puffs_%s_status_ok;\n", g.pkgName)
@@ -103,7 +103,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
"status = puffs_%s_error_constructor_not_called; goto exit; }\n", g.pkgName)
}
} else if n.Receiver() != 0 && n.Public() {
g.writes("if (self->private_impl.status & 1) { return; }")
g.writes("if (self->private_impl.status < 0) { return; }")
g.printf("if (self->private_impl.magic != PUFFS_MAGIC) {"+
"self->private_impl.status = puffs_%s_error_constructor_not_called; return; }\n", g.pkgName)
}
@@ -910,8 +910,8 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
if err := g.writeLoadExprDerivedVars(n); err != nil {
return err
}
// TODO: be principled with "if (status&1)" vs "if (status)".
g.writes("if (status&1) { return status; }\n")
// TODO: be principled with "if (status < 0)" vs "if (status)".
g.writes("if (status < 0) { return status; }\n")
} else {
// TODO: fix this.
+61 -32
View File
@@ -19,6 +19,8 @@ import (
"os/exec"
"strings"
"github.com/google/puffs/lang/base38"
"github.com/google/puffs/lang/check"
"github.com/google/puffs/lang/generate"
a "github.com/google/puffs/lang/ast"
@@ -43,10 +45,11 @@ const (
)
func main() {
generate.Main(func(pkgName string, tm *t.Map, files []*a.File) ([]byte, error) {
generate.Main(func(pkgName string, tm *t.Map, c *check.Checker, files []*a.File) ([]byte, error) {
g := &gen{
pkgName: pkgName,
tm: tm,
checker: c,
files: files,
}
if err := g.generate(); err != nil {
@@ -64,19 +67,22 @@ func main() {
})
}
const userDefinedStatusBase = 128
const (
maxNamespacedStatusCode = 255
statusCodeNamespaceMask = 1<<base38.MaxBits - 1
statusCodeNamespaceShift = 10
statusCodeCodeBits = 8
statusCodeDescription = "" +
"// Status codes are int32_t values:\n" +
"// - the sign bit indicates a non-recoverable status code: an error\n" +
"// - bits 10-30 hold the packageid: a namespace\n" +
"// - bits 8-9 are reserved\n" +
"// - bits 0-7 are a package-namespaced numeric code\n"
)
var builtInStatuses = [...]string{
// For API/ABI forwards and backwards compatibility, the very first two
// statuses must be "status ok" (with generated value 0) and "error bad
// version" (with generated value -2 + 1). This lets caller code check the
// constructor return value for "error bad version" even if the caller and
// callee were built with different versions.
"status ok",
"error bad version",
// The order of the remaining statuses is less important, but should remain
// stable for API/ABI backwards compatibility, where additional built in
// status codes don't affect existing ones.
"error bad receiver",
"error bad argument",
"error constructor not called",
@@ -87,7 +93,11 @@ var builtInStatuses = [...]string{
}
func init() {
if len(builtInStatuses) > userDefinedStatusBase {
// The +1 is for the error bit (the sign bit).
if statusCodeNamespaceShift+base38.MaxBits+1 != 32 {
panic("inconsistent status code namespace shift")
}
if len(builtInStatuses) > maxNamespacedStatusCode {
panic("too many builtInStatuses")
}
}
@@ -131,6 +141,7 @@ type gen struct {
buffer bytes.Buffer
pkgName string
tm *t.Map
checker *check.Checker
files []*a.File
statusList []status
statusMap map[t.ID]status
@@ -199,25 +210,36 @@ func (g *gen) genHeader() error {
g.writes("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
g.writes("// ---------------- Status Codes\n\n")
g.writes("// Status codes are non-positive integers.\n")
g.writes("//\n")
g.writes("// The least significant bit indicates a non-recoverable status code: an error.\n")
g.writes("typedef enum {\n")
g.writes(statusCodeDescription)
g.printf("//\n// Do not manipulate these bits directly. Use the API functions such as\n"+
"// puffs_%s_status_is_error instead.\n", g.pkgName)
g.printf("typedef int32_t puffs_%s_status;\n\n", g.pkgName)
pkgID := g.checker.PackageID()
g.printf("#define puffs_%s_packageid %d // %#08x\n\n", g.pkgName, pkgID, pkgID)
for i, s := range builtInStatuses {
nudge := ""
code := uint32(0)
if strings.HasPrefix(s, "error ") {
nudge = "+1"
code |= 1 << 31
}
g.printf("%s = %d%s,\n", g.cName(s), -2*i, nudge)
code |= uint32(i)
g.printf("#define %s %d // %#08x\n", g.cName(s), int32(code), code)
}
g.writes("\n")
if len(g.statusList) > maxNamespacedStatusCode {
return fmt.Errorf("too many status codes")
}
for i, s := range g.statusList {
nudge := ""
code := pkgID << statusCodeNamespaceShift
if s.isError {
nudge = "+1"
code |= 1 << 31
}
g.printf("%s = %d%s,\n", s.name, -2*(userDefinedStatusBase+i), nudge)
code |= uint32(i)
g.printf("#define %s %d // %#08x\n", s.name, int32(code), code)
}
g.printf("} puffs_%s_status;\n\n", g.pkgName)
g.writes("\n")
g.printf("bool puffs_%s_status_is_error(puffs_%s_status s);\n\n", g.pkgName, g.pkgName)
g.printf("const char* puffs_%s_status_string(puffs_%s_status s);\n\n", g.pkgName, g.pkgName)
@@ -252,9 +274,9 @@ func (g *gen) genImpl() error {
g.writes("\n")
g.writes("// ---------------- Status Codes Implementations\n\n")
g.printf("bool puffs_%s_status_is_error(puffs_%s_status s) {"+
"return s & 1; }\n\n", g.pkgName, g.pkgName)
g.printf("const char* puffs_%s_status_strings[%d] = {\n", g.pkgName, len(builtInStatuses)+len(g.statusList))
g.printf("bool puffs_%s_status_is_error(puffs_%s_status s) { return s < 0; }\n\n", g.pkgName, g.pkgName)
g.printf("const char* puffs_%s_status_strings0[%d] = {\n", g.pkgName, len(builtInStatuses))
for _, s := range builtInStatuses {
if strings.HasPrefix(s, "status ") {
s = s[len("status "):]
@@ -264,19 +286,26 @@ func (g *gen) genImpl() error {
s = g.pkgName + ": " + s
g.printf("%q,", s)
}
g.writes("};\n\n")
g.printf("const char* puffs_%s_status_strings1[%d] = {\n", g.pkgName, len(g.statusList))
for _, s := range g.statusList {
g.printf("%q,", g.pkgName+": "+s.msg)
}
g.writes("};\n\n")
g.printf("const char* puffs_%s_status_string(puffs_%s_status s) {\n", g.pkgName, g.pkgName)
g.writes("s = -(s >> 1); if (0 <= s) {\n")
g.printf("if (s < %d) { return puffs_%s_status_strings[s]; }\n",
len(builtInStatuses), g.pkgName)
g.printf("s -= %d;\n", userDefinedStatusBase-len(builtInStatuses))
g.printf("if ((%d <= s) && (s < %d)) { return puffs_%s_status_strings[s]; }\n",
len(builtInStatuses), len(builtInStatuses)+len(g.statusList), g.pkgName)
g.printf("}\nreturn \"%s: unknown status\";\n", g.pkgName)
g.printf("const char** a = NULL;\n")
g.printf("uint32_t n = 0;\n")
g.printf("switch ((s >> %d) & %#x) {\n", statusCodeNamespaceShift, statusCodeNamespaceMask)
g.printf("case 0: a = puffs_%s_status_strings0; n = %d; break;\n",
g.pkgName, len(builtInStatuses))
g.printf("case puffs_%s_packageid: a = puffs_%s_status_strings1; n = %d; break;\n",
g.pkgName, g.pkgName, len(g.statusList))
// TODO: add cases for other packages used by this one.
g.printf("}\n")
g.printf("uint32_t i = s & %#0x;\n", 1<<statusCodeCodeBits-1)
g.printf("return i < n ? a[i] : \"%s: unknown status\";\n", g.pkgName)
g.writes("}\n\n")
g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n")
+51 -36
View File
@@ -78,27 +78,36 @@ extern "C" {
// ---------------- Status Codes
// Status codes are non-positive integers.
// Status codes are int32_t values:
// - the sign bit indicates a non-recoverable status code: an error
// - bits 10-30 hold the packageid: a namespace
// - bits 8-9 are reserved
// - bits 0-7 are a package-namespaced numeric code
//
// The least significant bit indicates a non-recoverable status code: an error.
typedef enum {
puffs_gif_status_ok = 0,
puffs_gif_error_bad_version = -2 + 1,
puffs_gif_error_bad_receiver = -4 + 1,
puffs_gif_error_bad_argument = -6 + 1,
puffs_gif_error_constructor_not_called = -8 + 1,
puffs_gif_error_unexpected_eof = -10 + 1,
puffs_gif_status_short_read = -12,
puffs_gif_status_short_write = -14,
puffs_gif_error_closed_for_writes = -16 + 1,
puffs_gif_error_bad_gif_block = -256 + 1,
puffs_gif_error_bad_gif_extension_label = -258 + 1,
puffs_gif_error_bad_gif_header = -260 + 1,
puffs_gif_error_bad_lzw_literal_width = -262 + 1,
puffs_gif_error_todo_unsupported_local_color_table = -264 + 1,
puffs_gif_error_lzw_code_is_out_of_range = -266 + 1,
puffs_gif_error_lzw_prefix_chain_is_cyclical = -268 + 1,
} puffs_gif_status;
// Do not manipulate these bits directly. Use the API functions such as
// puffs_gif_status_is_error instead.
typedef int32_t puffs_gif_status;
#define puffs_gif_packageid 1017222 // 0x000f8586
#define puffs_gif_status_ok 0 // 0x00000000
#define puffs_gif_error_bad_version -2147483647 // 0x80000001
#define puffs_gif_error_bad_receiver -2147483646 // 0x80000002
#define puffs_gif_error_bad_argument -2147483645 // 0x80000003
#define puffs_gif_error_constructor_not_called -2147483644 // 0x80000004
#define puffs_gif_error_unexpected_eof -2147483643 // 0x80000005
#define puffs_gif_status_short_read 6 // 0x00000006
#define puffs_gif_status_short_write 7 // 0x00000007
#define puffs_gif_error_closed_for_writes -2147483640 // 0x80000008
#define puffs_gif_error_bad_gif_block -1105848320 // 0xbe161800
#define puffs_gif_error_bad_gif_extension_label -1105848319 // 0xbe161801
#define puffs_gif_error_bad_gif_header -1105848318 // 0xbe161802
#define puffs_gif_error_bad_lzw_literal_width -1105848317 // 0xbe161803
#define puffs_gif_error_todo_unsupported_local_color_table \
-1105848316 // 0xbe161804
#define puffs_gif_error_lzw_code_is_out_of_range -1105848315 // 0xbe161805
#define puffs_gif_error_lzw_prefix_chain_is_cyclical -1105848314 // 0xbe161806
bool puffs_gif_status_is_error(puffs_gif_status s);
@@ -264,10 +273,10 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
// ---------------- Status Codes Implementations
bool puffs_gif_status_is_error(puffs_gif_status s) {
return s & 1;
return s < 0;
}
const char* puffs_gif_status_strings[16] = {
const char* puffs_gif_status_strings0[9] = {
"gif: ok",
"gif: bad version",
"gif: bad receiver",
@@ -277,6 +286,9 @@ const char* puffs_gif_status_strings[16] = {
"gif: short read",
"gif: short write",
"gif: closed for writes",
};
const char* puffs_gif_status_strings1[7] = {
"gif: bad GIF block",
"gif: bad GIF extension label",
"gif: bad GIF header",
@@ -287,17 +299,20 @@ const char* puffs_gif_status_strings[16] = {
};
const char* puffs_gif_status_string(puffs_gif_status s) {
s = -(s >> 1);
if (0 <= s) {
if (s < 9) {
return puffs_gif_status_strings[s];
}
s -= 119;
if ((9 <= s) && (s < 16)) {
return puffs_gif_status_strings[s];
}
const char** a = NULL;
uint32_t n = 0;
switch ((s >> 10) & 0x1fffff) {
case 0:
a = puffs_gif_status_strings0;
n = 9;
break;
case puffs_gif_packageid:
a = puffs_gif_status_strings1;
n = 7;
break;
}
return "gif: unknown status";
uint32_t i = s & 0xff;
return i < n ? a[i] : "gif: unknown status";
}
// ---------------- Private Constructor and Destructor Prototypes
@@ -390,7 +405,7 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self,
return puffs_gif_error_bad_receiver;
}
puffs_gif_status status = self->private_impl.status;
if (status & 1) {
if (status < 0) {
return status;
}
if (self->private_impl.magic != PUFFS_MAGIC) {
@@ -970,7 +985,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self,
}
b_rend_src = b_rptr_src + len;
}
if (status & 1) {
if (status < 0) {
return status;
}
}
@@ -1016,7 +1031,7 @@ void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
if (!self) {
return;
}
if (self->private_impl.status & 1) {
if (self->private_impl.status < 0) {
return;
}
if (self->private_impl.magic != PUFFS_MAGIC) {
@@ -1038,7 +1053,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
return puffs_gif_error_bad_receiver;
}
puffs_gif_status status = self->private_impl.status;
if (status & 1) {
if (status < 0) {
return status;
}
if (self->private_impl.magic != PUFFS_MAGIC) {
+29 -20
View File
@@ -78,27 +78,36 @@ extern "C" {
// ---------------- Status Codes
// Status codes are non-positive integers.
// Status codes are int32_t values:
// - the sign bit indicates a non-recoverable status code: an error
// - bits 10-30 hold the packageid: a namespace
// - bits 8-9 are reserved
// - bits 0-7 are a package-namespaced numeric code
//
// The least significant bit indicates a non-recoverable status code: an error.
typedef enum {
puffs_gif_status_ok = 0,
puffs_gif_error_bad_version = -2 + 1,
puffs_gif_error_bad_receiver = -4 + 1,
puffs_gif_error_bad_argument = -6 + 1,
puffs_gif_error_constructor_not_called = -8 + 1,
puffs_gif_error_unexpected_eof = -10 + 1,
puffs_gif_status_short_read = -12,
puffs_gif_status_short_write = -14,
puffs_gif_error_closed_for_writes = -16 + 1,
puffs_gif_error_bad_gif_block = -256 + 1,
puffs_gif_error_bad_gif_extension_label = -258 + 1,
puffs_gif_error_bad_gif_header = -260 + 1,
puffs_gif_error_bad_lzw_literal_width = -262 + 1,
puffs_gif_error_todo_unsupported_local_color_table = -264 + 1,
puffs_gif_error_lzw_code_is_out_of_range = -266 + 1,
puffs_gif_error_lzw_prefix_chain_is_cyclical = -268 + 1,
} puffs_gif_status;
// Do not manipulate these bits directly. Use the API functions such as
// puffs_gif_status_is_error instead.
typedef int32_t puffs_gif_status;
#define puffs_gif_packageid 1017222 // 0x000f8586
#define puffs_gif_status_ok 0 // 0x00000000
#define puffs_gif_error_bad_version -2147483647 // 0x80000001
#define puffs_gif_error_bad_receiver -2147483646 // 0x80000002
#define puffs_gif_error_bad_argument -2147483645 // 0x80000003
#define puffs_gif_error_constructor_not_called -2147483644 // 0x80000004
#define puffs_gif_error_unexpected_eof -2147483643 // 0x80000005
#define puffs_gif_status_short_read 6 // 0x00000006
#define puffs_gif_status_short_write 7 // 0x00000007
#define puffs_gif_error_closed_for_writes -2147483640 // 0x80000008
#define puffs_gif_error_bad_gif_block -1105848320 // 0xbe161800
#define puffs_gif_error_bad_gif_extension_label -1105848319 // 0xbe161801
#define puffs_gif_error_bad_gif_header -1105848318 // 0xbe161802
#define puffs_gif_error_bad_lzw_literal_width -1105848317 // 0xbe161803
#define puffs_gif_error_todo_unsupported_local_color_table \
-1105848316 // 0xbe161804
#define puffs_gif_error_lzw_code_is_out_of_range -1105848315 // 0xbe161805
#define puffs_gif_error_lzw_prefix_chain_is_cyclical -1105848314 // 0xbe161806
bool puffs_gif_status_is_error(puffs_gif_status s);
+4 -3
View File
@@ -19,7 +19,7 @@ var (
packageName = flag.String("package_name", "", "the package name of the Puffs input code")
)
type Generator func(packageName string, tm *token.Map, files []*ast.File) ([]byte, error)
type Generator func(packageName string, tm *token.Map, c *check.Checker, files []*ast.File) ([]byte, error)
func Main(g Generator) {
if err := main1(g); err != nil {
@@ -38,11 +38,12 @@ func main1(g Generator) error {
return err
}
if _, err := check.Check(tm, files...); err != nil {
c, err := check.Check(tm, files...)
if err != nil {
return err
}
out, err := g(*packageName, tm, files)
out, err := g(*packageName, tm, c, files)
if err != nil {
return err
}