-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (87 loc) · 3.1 KB
/
Copy pathmain.go
File metadata and controls
101 lines (87 loc) · 3.1 KB
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
package main
import (
"os"
"slices"
"strings"
"github.com/AsynchronousAI/reasm/compiler"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func main() {
var enableComments bool
var enableTrace bool
var enableAccurate bool
var memorySize int
var mode string
var outputFile string
var mainSymbol string
var importSymbols []string // <- multiple imports
var logIR bool
log.SetFormatter(&log.TextFormatter{
ForceColors: true, // force color output
FullTimestamp: true, // show timestamps
})
log.SetLevel(log.DebugLevel)
var rootCmd = &cobra.Command{
Use: "reasm [input] [output]",
Short: "Compile RISC-V Assembly into Luau",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, inputFiles []string) error {
validModes := []string{"module", "main", "bench"}
modeLower := strings.ToLower(mode)
if !slices.Contains(validModes, modeLower) {
log.Error("invalid mode. Valid modes are: module, main, bench")
return nil
}
if memorySize <= 0 {
log.Error("invalid memory size. --memory must be greater than 0")
return nil
}
/* read input file */
if len(inputFiles) > 1 {
log.Error("Only one input file is supported at the moment, if you want to compile multiple files link before hand using an ELF file.")
return nil
}
file, err := os.Open(inputFiles[0])
if err != nil {
log.Errorf("failed to read input file: %v", err)
return nil
}
defer file.Close()
imports := append([]string{}, importSymbols...)
if !slices.Contains(imports, "stdlib") {
imports = append(imports, "stdlib")
}
/* compile with options */
processed := compiler.Compile(file, compiler.Options{
Comments: enableComments,
Trace: enableTrace,
Accurate: enableAccurate,
Memory: memorySize,
Mode: modeLower,
MainSymbol: mainSymbol,
Imports: imports,
LogIR: logIR,
})
/* write output file */
err = os.WriteFile(outputFile, processed, 0644)
if err != nil {
log.Errorf("failed to write output file: %v", err)
return nil
}
return nil
},
}
// Flags
rootCmd.Flags().BoolVar(&enableComments, "comments", false, "Include debug comments in the output")
rootCmd.Flags().BoolVar(&enableTrace, "trace", false, "Prints out a trace of the PC")
rootCmd.Flags().BoolVar(&enableAccurate, "accurate", false, "Enable more accurate ISA modeling (float32 rounding, 32-bit overflow wrapping)")
rootCmd.Flags().IntVar(&memorySize, "memory", 524288000, "Memory size in bytes for generated Luau RAM buffer (default: 500MB)")
rootCmd.Flags().StringVar(&mode, "mode", "main", "Mode to compile as: module, main, or bench")
rootCmd.Flags().StringVarP(&outputFile, "output", "o", "", "The output luau file.")
rootCmd.Flags().StringVarP(&mainSymbol, "symbol", "e", "main", "The main symbol to start automatically.")
rootCmd.Flags().StringArrayVarP(&importSymbols, "import", "I", []string{}, "Import symbol(s), can be repeated (example: -Imath -Ios)")
rootCmd.Flags().BoolVar(&logIR, "log-ir", false, "Log generated IR as JSON (requires debug log level)")
rootCmd.MarkFlagRequired("o")
rootCmd.Execute()
}