File: make_const.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.0~git20200523.4439b6b-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,304 kB
  • sloc: xml: 71,029; asm: 13,138; sh: 179; makefile: 35
file content (73 lines) | stat: -rw-r--r-- 1,743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// +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)
	}
}