-
Notifications
You must be signed in to change notification settings - Fork 4
/
tulang.tu
223 lines (219 loc) · 6.36 KB
/
tulang.tu
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use fmt
use os
use std
use compiler.utils
use compiler.compile
use compiler.parser
use asmer.asm
use linker.link
use runtime
//TODO: set by compiler
root = "/usr/local/lib"
version = "1.0.0"
printlat = false
class Compiler {
// origin file
code_files = []
scan_dirs = []
// need asm compile and linker to generate executable file
flag_run = false
// pass args to binary executor
args = []
// compiler || asmer || linker
// default compiler
type = "compiler"
flag_gcc = false
//default executeable name
out = "a.out"
}
Compiler::print_help(){
fmt.println("usage: ./tu [options|file.tu...|dir]\n" +
" build 编译成汇编后进行链接生成二进制可执行文件\n" +
" -s *.tu|dir 编译为tulang代码为linux-amd64汇编文件\n" +
" -c *.s |dir 编译汇编为elf&pecoff跨平台可重定向文件\n" +
" -o *.o |dir 链接elf&pecofff可重定向文件生成最终执行程序\n" +
" -d 开启trace日志打印编译详细过程\n" +
" -gcc 支持通过gcc链接生成可执行程序\n" +
" -g 编译tu文件时带上debug段信息,支持栈回溯\n" +
" -std 编译runtime&std相关内置库代码\n" +
" -v version\n"
)
}
Compiler::commadparse(){
i = 0
while i < std.len(os.argv()) {
match os.argv()[i] {
"build" : {
this.type = "compiler"
this.flag_run = true
this.code_files[] = os.argv()[i + 1]
i += 1
}
"-s" : {
this.type = "compiler"
this.scandir(os.argv()[i+1],".tu")
i += 1
}
"-c" : {
this.type = "asmer"
this.scandir(os.argv()[i+1],".s")
i += 1
}
"-o" : {
this.type = "linker"
this.scandir(os.argv()[i+1],".o")
i += 1
}
"-d" : {
compile.trace = true // trace mode && print log
asm.trace = true
link.trace = true
}
"-g" : compile.debug = true
"-std" : compile.nostd = false
"-gcc" : this.flag_gcc = true
"-lat" : printlat = true
"-v" : {
fmt.printf(
"tu-lang version: %s\n" +
"Target : x86_64 linux\n",
version
)
os.exit(0)
}
_ : {
this.print_help()
fmt.println(utils.print_red(
fmt.sprintf("unkown option [%s]",os.argv()[i])
))
os.exit(-1)
}
}
i += 1
}
}
Compiler::scandir(dir,dstext){
utils.debugf("main.scandir %s ext:%s",dir,dstext)
//check is dir or codefile
if !std.is_dir(dir) {
utils.debugf("main.scandir: %s is file ",dir)
this.code_files[] = dir
return true
}
fd = std.opendir(dir)
loop {
file = fd.readdir()
if !file break
if !file.isFile() continue
filename = file.path
ext = string.sub(filename,std.len(filename) - 2)
if ext == dstext {
utils.debugf("main.scandir: add %s ",file.path)
this.code_files[] = file.path
}
}
}
Compiler::compiler(file){
utils.debug("main.compiler")
utils.msg2(10,"Compiling",fmt.sprintf(
"%s v0.0.0",file
))
if compile.nostd && !this.flag_gcc {
compile.nostd = true
}
compile.genast(file)
compile.editast()
compile.compile()
utils.msg(30,"Compiler generate all Passed")
if this.flag_run {
//By Gcc Link
if this.flag_gcc {
compile.gcclink()
os.shell("rm *.s")
os.shell("chmod 777 a.out")
}else {
//Self Asmer && Linker
this.code_files = []
this.scandir(".",".s")
if !compile.nostd
this.scandir(root + "/coasm/",".s")
this.asmer()
this.code_files = []
this.scandir(".",".o")
if compile.nostd
this.scandir(root + "/colib/",".o")
this.linker()
os.shell("rm *.o *.s")
}
os.shell("chmod 777 a.out")
// args = "./a.out"
// os.shell(args)
}
utils.msg2(100,"Finished",fmt.sprintf(
"%s target(a.out)",file
))
}
Compiler::asmer(){
total = std.len(this.code_files)
if total <= 0 utils.error("please provide at lease one .o file")
//start gen
i = 1
for f : this.code_files {
utils.smsg("[ " + i + "/" + total +"]","Compiling asm file " + f)
eng<asm.Asmer> = new asm.Asmer(f)
eng.execute()
eng.close()
utils.smsg("[ " + i + "/" + total +"]",
fmt.sprintf("Generate %s Passed" ,eng.parser.outname)
)
i += 1
}
utils.msg(100,"Aasmer generate all Passed")
}
Compiler::linker(){
linker = new link.Linker()
total = std.len(this.code_files)
if total <= 0 utils.error("please provide at lease one .o file")
i = 1
for f : this.code_files {
utils.smsg("[ " + i + "/" + total +"]","Reading elf object info " + f)
linker.addElf(f)
i += 1
}
if !linker.link(this.out) {
utils.error("Generate " + this.out + " Failed")
}
utils.msg(100,"Generate " + this.out + " Passed")
}
Compiler::compile(){
if !std.len(this.code_files) {
utils.error("missing code file")
}
match this.type {
"compiler" : return this.compiler(this.code_files[0])
"asmer" : return this.asmer()
"linker" : return this.linker()
_ : {
fmt.println("unknown compile type:")
return this.print_help()
}
}
}
func main() {
start<i64> = std.ntime()
eng = new Compiler()
if os.argc() < 1 {
return eng.print_help()
}
os.set_stack(10.(i8))
eng.commadparse() // handle options
eng.compile()
end<i64> = std.ntime()
if printlat {
latency<i64> = end - start
latency /= 1000000 // ms
utils.msg(0,
fmt.sprintf("latency: %d ms\n",int(latency))
)
}
}