forked from mmcloughlin/avo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3644ec
commit abd300c
Showing
7 changed files
with
227 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package operand | ||
|
||
import "fmt" | ||
|
||
type Constant interface { | ||
Op | ||
Bytes() int | ||
constant() | ||
} | ||
|
||
//go:generate go run make_const.go -output zconst.go | ||
|
||
// String is a string constant. | ||
type String string | ||
|
||
func (s String) Asm() string { return fmt.Sprintf("$%q", s) } | ||
func (s String) Bytes() int { return len(s) } | ||
func (s String) constant() {} | ||
|
||
// Imm returns an unsigned integer constant with size guessed from x. | ||
func Imm(x uint64) Constant { | ||
// TODO(mbm): remove this function | ||
switch { | ||
case uint64(uint8(x)) == x: | ||
return U8(x) | ||
case uint64(uint16(x)) == x: | ||
return U16(x) | ||
case uint64(uint32(x)) == x: | ||
return U32(x) | ||
} | ||
return U64(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package operand | ||
|
||
import "testing" | ||
|
||
func TestConstants(t *testing.T) { | ||
cases := []struct { | ||
Const Constant | ||
Asm string | ||
Bytes int | ||
}{ | ||
{F32(3.1415), "$(3.1415)", 4}, | ||
{F64(3.1415), "$(3.1415)", 8}, | ||
{U8(42), "$0x2a", 1}, | ||
{U16(42), "$0x002a", 2}, | ||
{U32(42), "$0x0000002a", 4}, | ||
{U64(42), "$0x000000000000002a", 8}, | ||
{I8(-42), "$-42", 1}, | ||
{I16(-42), "$-42", 2}, | ||
{I32(-42), "$-42", 4}, | ||
{I64(-42), "$-42", 8}, | ||
{String("hello"), "$\"hello\"", 5}, | ||
{String("quot:\"q\""), "$\"quot:\\\"q\\\"\"", 8}, | ||
} | ||
for _, c := range cases { | ||
if c.Const.Asm() != c.Asm { | ||
t.Errorf("%v.Asm() = %v; expect %v", c.Const, c.Const.Asm(), c.Asm) | ||
} | ||
if c.Const.Bytes() != c.Bytes { | ||
t.Errorf("%v.Bytes() = %v; expect %v", c.Const, c.Const.Bytes(), c.Bytes) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"go/format" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
output = flag.String("output", "", "path to output file (default stdout)") | ||
) | ||
|
||
func PrintConstType(w io.Writer, name, typ, format string, size int, doc string) { | ||
r := typ[0] | ||
fmt.Fprintf(w, "// %s\n", doc) | ||
fmt.Fprintf(w, "type %s %s\n", name, typ) | ||
fmt.Fprintf(w, "\n") | ||
fmt.Fprintf(w, "func (%c %s) Asm() string { return fmt.Sprintf(\"$%s\", %c) }\n", r, name, format, r) | ||
fmt.Fprintf(w, "func (%c %s) Bytes() int { return %d }\n", r, name, size) | ||
fmt.Fprintf(w, "func (%c %s) constant() {}\n", r, name) | ||
fmt.Fprintf(w, "\n") | ||
} | ||
|
||
func PrintConstTypes(w io.Writer) { | ||
_, self, _, _ := runtime.Caller(0) | ||
fmt.Fprintf(w, "// Code generated by %s. DO NOT EDIT.\n\n", filepath.Base(self)) | ||
fmt.Fprintf(w, "package operand\n\n") | ||
fmt.Fprintf(w, "import \"fmt\"\n\n") | ||
for n := 1; n <= 8; n *= 2 { | ||
bits := n * 8 | ||
bs := strconv.Itoa(bits) | ||
|
||
if n >= 4 { | ||
PrintConstType(w, "F"+bs, "float"+bs, "(%#v)", n, fmt.Sprintf("F%d is a %d-bit floating point constant.", bits, bits)) | ||
} | ||
PrintConstType(w, "I"+bs, "int"+bs, "%+d", n, fmt.Sprintf("I%d is a %d-bit signed integer constant.", bits, bits)) | ||
PrintConstType(w, "U"+bs, "uint"+bs, "%#0"+strconv.Itoa(2*n)+"x", n, fmt.Sprintf("U%d is a %d-bit unsigned integer constant.", bits, bits)) | ||
} | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
w := os.Stdout | ||
if *output != "" { | ||
f, err := os.Create(*output) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer f.Close() | ||
w = f | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
PrintConstTypes(buf) | ||
|
||
src, err := format.Source(buf.Bytes()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, err = w.Write(src) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters