Пример #1
0
func TestGir(t *testing.T) {
	var (
		conf    config.Config
		context value.Context
		fd      *os.File
		err     error
	)
	context = ctx.NewContext(&conf)
	for _, file := range []string{filepath.Join("testdata", "test.gir"), filepath.Join("testdata", "test1.gir"), filepath.Join("testdata", "test2.gir"), filepath.Join("testdata", "test3.gir"), filepath.Join("testdata", "test4.gir")} {
		fd, err = os.Open(file)
		defer fd.Close()
		if err != nil {
			t.Fatalf("gir: %s\n", err)
		}
		scanner := scan.New(context, file, bufio.NewReader(fd))
		parser := parse.NewParser(file, scanner, context)
		fileDecl := parser.ParseFile()
		t.Log("tree(exprs): ", parse.Tree(fileDecl))

		for _, fnDecl := range fileDecl.Decls {
			ssafn, ok := codegen.BuildSSA(&fnDecl, fileDecl.PkgName, false)
			if ssafn == nil || !ok {
				t.Fatalf("gir: Error building SSA form")
				return
			} else {
				t.Log("ssa:\n", ssafn)
			}
		}
	}

	return
}
Пример #2
0
func runTest(t *testing.T, filename string) {
	context := ctx.NewContext(&conf)
	fd, err := os.Open(filepath.Join("testdata", filename))
	defer fd.Close()
	if err != nil {
		t.Fatalf("gir: %s\n", err)
	}
	scanner := scan.New(context, filename, bufio.NewReader(fd))
	parser := parse.NewParser(filename, scanner, context)
	fileDecl := parser.ParseFile()
	for _, fnDecl := range fileDecl.Decls {
		ssafn, ok := codegen.BuildSSA(&fnDecl, fileDecl.PkgName, false)
		if ssafn == nil || !ok {
			t.Fatalf("gir: Error building SSA form")
			return
		} else {
			t.Log("ssa:\n", ssafn)
		}
	}
}
Пример #3
0
Файл: gir.go Проект: bjwbell/gir
func main() {

	context = ctx.NewContext(&conf)

	var f = flag.String("f", "", "input *.gir file ")
	var o = flag.String("o", "", "output *.s file ")
	var proto = flag.String("proto", "", "output *.go prototype file")
	flag.Parse()

	file := ""
	outfile := ""
	protofile := ""
	log.SetFlags(log.Lshortfile)
	if *f != "" {
		file = *f
	} else {
		log.Fatalf("Error no file provided")
	}
	if *o != "" {
		outfile = *o
	}
	if *proto != "" {
		protofile = *proto
	}
	var fd io.Reader
	var err error
	fd, err = os.Open(file)
	if err != nil {
		fmt.Fprintf(os.Stderr, "gir: %s\n", err)
		os.Exit(1)
	}
	var fd2 io.Reader
	var err2 error
	fd2, err2 = os.Open(file)
	if err2 != nil {
		fmt.Fprintf(os.Stderr, "gir: %s\n", err2)
		os.Exit(1)
	}
	scanner1 := scan.New(context, file, bufio.NewReader(fd))
	scanner2 := scan.New(context, file, bufio.NewReader(fd2))

	fmt.Println("Tokens: ")
	for tok := scanner1.Next(); tok.Type != token.EOF; tok = scanner1.Next() {
		fmt.Println(tok, "type ", tok.Type)
	}
	parser := parse.NewParser(file, scanner2, context)
	fileDecl := parser.ParseFile()
	pkg := fileDecl.PkgName
	fmt.Println("tree(exprs): ", parse.Tree(fileDecl))
	asm := ""
	protos := ""
	for _, fnDecl := range fileDecl.Decls {
		ssafn, ok := codegen.BuildSSA(&fnDecl, fileDecl.PkgName, false)
		if ssafn == nil || !ok {
			fmt.Println("Error building SSA form")
			return
		} else {
			fmt.Println("ssa:\n", ssafn)
			var ok bool
			asm, ok = codegen.GenAsm(ssafn)
			if !ok {
				fmt.Println("Error generating assembly")
			}

			if fnProto, ok := codegen.GenGoProto(ssafn); ok {
				protos += fnProto
			} else {
				fmt.Println("Error generating Go proto file")
			}

		}
	}

	if outfile != "" {
		err = ioutil.WriteFile(outfile, []byte(asm), 0644)
		if err != nil {
			panic(err)
		}
	}
	if protofile != "" {
		protoTxt := "// +build amd64\n\n"
		protoTxt += fmt.Sprintf("package %s\n", pkg)
		protoTxt += protos
		err = ioutil.WriteFile(protofile, []byte(protoTxt), 0644)
		if err != nil {
			panic(err)
		}
	}
}