Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
167 lines (148 loc) · 4.38 KB
/
Copy pathmain.go
File metadata and controls
167 lines (148 loc) · 4.38 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"cuteify/compile"
"cuteify/compile/gc"
packageSys "cuteify/package"
"cuteify/parser"
"flag"
"fmt"
"os"
"reflect"
"strings"
"time"
)
func main() {
startTime := time.Now()
// 定义命令行参数
genAST := flag.Bool("ast", false, "生成 AST 文本表示")
asmOnly := flag.Bool("S", false, "仅编译生成汇编文件,不进行汇编和链接")
asmOnlyLong := flag.Bool("asm", false, "仅编译生成汇编文件(同 -S)")
objOnly := flag.Bool("c", false, "编译并汇编生成目标文件,不进行链接")
objOnlyLong := flag.Bool("obj", false, "编译并汇编生成目标文件(同 -c)")
output := flag.String("o", "", "指定输出文件路径")
flag.Parse()
// 获取路径参数
path := "./test"
if flag.NArg() > 0 {
path = flag.Arg(0)
}
tmp, err := packageSys.GetPackage(path, true)
if err != nil {
panic(err)
}
// GC 摘要分析:包合并 + Check 重试完成后、代码生成之前执行
gc.Build(tmp.AST.(*parser.Node))
fmt.Println("\n=== GC 函数摘要 ===")
if err := gc.DumpAll(os.Stdout); err != nil {
panic(err)
}
fmt.Println("\n=== GC LLR 分析 ===")
fmt.Print(gc.DumpLLRs())
// 写入 GC 摘要文件到测试目录
gcDir := path
if err := os.MkdirAll(gcDir, 0755); err == nil {
// gc_summary.json — 全部函数摘要
if f, err := os.Create(gcDir + "/gc_summary.json"); err == nil {
gc.DumpAll(f)
f.Close()
}
// gc_llr.txt — LLR 分析结果
os.WriteFile(gcDir+"/gc_llr.txt", []byte(gc.DumpLLRs()), 0644)
}
// AST 模式
if *genAST {
fmt.Println("\n=== Text AST ===")
pr(tmp.AST.(*parser.Node), 0)
return
}
// 编译
co := &compile.Compiler{}
code := co.Compile(tmp.AST.(*parser.Node))
// 获取当前工作目录
workDir, err := os.Getwd()
if err != nil {
panic(err)
}
// 确定输出模式
genAsm := *asmOnly || *asmOnlyLong
genObj := *objOnly || *objOnlyLong
var asmPath string
if *output != "" {
asmPath = *output
} else if genAsm {
asmPath = workDir + "/_main.asm"
} else if genObj {
asmPath = workDir + "/_main.o"
} else {
asmPath = workDir + "/_main.asm"
}
// 汇编模式:直接输出汇编文件
if genAsm {
os.WriteFile(asmPath, []byte(code), 0644)
fmt.Println("\033[32mOK\033[0m: Compile completed in", time.Since(startTime))
fmt.Println("\033[32mOK\033[0m: Generated assembly file", asmPath)
return
}
// 目标文件模式:编译 + 汇编(需要 nasm)
if genObj {
// 先写临时汇编文件
tmpAsm := workDir + "/_tmp.asm"
os.WriteFile(tmpAsm, []byte(code), 0644)
// 使用 nasm 汇编
objFormat := "elf32" // 默认 x86 32位 ELF
// TODO: 根据架构选择不同的格式
cmd := fmt.Sprintf("nasm -f %s -o %s %s", objFormat, asmPath, tmpAsm)
fmt.Println("Running:", cmd)
// TODO: 实际执行汇编命令
os.Remove(tmpAsm)
fmt.Println("\033[32mOK\033[0m: Compile + assemble completed in", time.Since(startTime))
fmt.Println("\033[32mOK\033[0m: Generated object file", asmPath)
return
}
// 默认模式:完整编译(当前只到汇编)
os.WriteFile(asmPath, []byte(code), 0644)
fmt.Println("\033[32mOK\033[0m: Compile completed in", time.Since(startTime))
fmt.Println("\033[32mOK\033[0m: Generated", asmPath)
}
func pr(block *parser.Node, tabnum int) {
/*if block.Ignore {
return
}*/
fmt.Println(strings.Repeat("\t", tabnum), reflect.TypeOf(block.Value), block.Value)
/*TODO:
if sb, ok := block.Value.(*parser.StructBlock); ok {
fmt.Printf("\n=== Struct: %s ===\n", sb.Name)
fmt.Println("Fields:")
for _, f := range sb.Fields {
accessStr := "public"
if f.Access == parser.AccessReadOnly {
accessStr = "readonly"
} else if f.Access == parser.AccessWriteOnly {
accessStr = "writeonly"
} else if f.Access == parser.AccessPrivate {
accessStr = "private"
}
defaultStr := ""
if f.DefaultValue != nil {
defaultStr = fmt.Sprintf(" (default: %v)", f.DefaultValue)
}
tagStr := ""
if len(f.Tags) > 0 {
var tagParts []string
for _, t := range f.Tags {
tagParts = append(tagParts, t.Key+":"+t.Value)
}
tagStr = fmt.Sprintf(" (tags: %v)", tagParts)
}
fmt.Printf(" - %s: %v (access: %s)%s%s\n", f.Name, f.Type, accessStr, defaultStr, tagStr)
}
fmt.Println("Methods:")
for _, m := range sb.Methods {
fmt.Printf(" - %s\n", m.Name.String())
}
fmt.Println("=====================\n")
}*/
for _, k := range block.Children {
pr(k, tabnum+1)
}
}