forked from markkurossi/mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
122 lines (111 loc) · 2.36 KB
/
compile.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// main.go
//
// Copyright (c) 2019-2024 Markku Rossi
//
// All rights reserved.
//
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/compiler"
"github.com/markkurossi/mpc/compiler/utils"
)
func compileFiles(files []string, params *utils.Params, inputSizes [][]int,
compile, ssa, dot, svg bool, circFormat string) error {
var circ *circuit.Circuit
var err error
for _, file := range files {
if compile {
params.CircOut, err = makeOutput(file, circFormat)
if err != nil {
return err
}
params.CircFormat = circFormat
if dot {
params.CircDotOut, err = makeOutput(file, "circ.dot")
if err != nil {
return err
}
}
if svg {
params.CircSvgOut, err = makeOutput(file, "circ.svg")
if err != nil {
return err
}
}
}
if circuit.IsFilename(file) {
circ, err = circuit.Parse(file)
if err != nil {
return err
}
if params.CircOut != nil {
if params.Verbose {
fmt.Printf("Serializing circuit...\n")
}
err = circ.MarshalFormat(params.CircOut, params.CircFormat)
if err != nil {
return err
}
}
} else if strings.HasSuffix(file, ".mpcl") {
if ssa {
params.SSAOut, err = makeOutput(file, "ssa")
if err != nil {
return err
}
if dot {
params.SSADotOut, err = makeOutput(file, "ssa.dot")
if err != nil {
return err
}
}
}
circ, _, err = compiler.New(params).CompileFile(file, inputSizes)
if err != nil {
return err
}
} else {
return fmt.Errorf("unknown file type '%s'", file)
}
}
return nil
}
func makeOutput(base, suffix string) (io.WriteCloser, error) {
var path string
idx := strings.LastIndexByte(base, '.')
if idx < 0 {
path = base + "." + suffix
} else {
path = base[:idx+1] + suffix
}
f, err := os.Create(path)
if err != nil {
return nil, err
}
return &OutputFile{
File: f,
Buffered: bufio.NewWriter(f),
}, nil
}
// OutputFile implements a buffered output file.
type OutputFile struct {
File *os.File
Buffered *bufio.Writer
}
func (out *OutputFile) Write(p []byte) (nn int, err error) {
return out.Buffered.Write(p)
}
// Close implements io.Closer.Close for the buffered output file.
func (out *OutputFile) Close() error {
if err := out.Buffered.Flush(); err != nil {
return err
}
return out.File.Close()
}