This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stas.stas
574 lines (495 loc) · 13.5 KB
/
stas.stas
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
include <std.stas>
reserve strip_assert 1
reserve object_file 1
reserve debug_symbols 1
reserve verbose_mode 1
reserve trace_calls 1
auto backend_type 1
auto target_os 1
const StasBackend.fasm { 1 }
const StasBackend.nasm { 2 }
; (StasBackend -- str len)
fn StasBackend.to_str 1 2 {
dup StasBackend.fasm = if {
"fasm"
} elif dup StasBackend.nasm = {
"nasm"
} else {
0 0 0 assert -> 'unreachable'
}
rot rot drop
}
include 'src/os.stas' ; comptime os defs
include 'src/stringbuffer.stas' ; handling strings
include 'src/formatting.stas' ; basic utility formatting and files
include 'src/tokens.stas' ; stas token definitions
include 'src/util.stas' ; utility functions, error handling
include 'src/scanner.stas' ; lexer/scanner, creates tokens
include 'src/parserdefs.stas' ; stas parser definitions, very large file
include 'src/eval.stas' ; constant evaluation
include 'src/write.stas' ; buffers + writing to files
include 'src/x86.stas' ; stas codegen definitions and reg allocator
include 'src/asm.stas' ; supporting functions for inline assembly blocks
include 'src/parser.stas' ; stas parser, creates IR instructions
include 'src/dce.stas' ; dead code elimination compiler pass
include 'src/gen.stas' ; stas code generator, creates x86_64 asm
fn usage {
OS.get_host OS.to_pretty_str eputs
" stas 0.2.0 Copyright (C) 2022 l-m.dev" eputs
"\n\nUSAGE: stas [OPTIONS] [FILE]\n\n" eputs
" -o <output> Specify '-o -' to dump assembly to stdout\n" eputs
" -g Debug info. Most effective with the `nasm` backend\n" eputs
" -b <backend> Assemblers `nasm` or `fasm` as compiler backend\n" eputs
" -c Create object file for linking, will not call `ld`\n" eputs
" -r Execute file after compiling. Arguments after this\n" eputs
" switch will ignored and passed to the program\n" eputs
" -v Activate verbose mode\n" eputs
" -os <os> `linux` | `freebsd`\n" eputs
" -dump-tok Dump token information after scanning stage\n" eputs
" -dump-ir Dump intermediate representation after parsing stage\n" eputs
" -unsafe Currently only strips assertions\n" eputs
" -comptime-depth <num> Adjust compile time recursion depth for evaluating\n" eputs
" comptime functions. Default: 12\n" eputs
" -trace-calls Compiled executable will have function calls traced\n" eputs
" at runtime. Will not trace inlined calls\n" eputs
" -h, --help Show this message\n" eputs
"\nSUBCOMMANDS\n" eputs
"\n ./stas symlink Will create a symbolic link to the stas compiler\n" eputs
" inside `/usr/local/bin/stas`\n\n" eputs
}
fn help_and_exit {
usage
0 exit
}
fn usage_and_exit {
usage
1 exit
}
fn usage_msg_and_exit 2 0 {
error.generic_fatal_noexit
eendl
usage_and_exit
}
fn parse_backend_type 2 0 {
over over "fasm" streq if {
StasBackend.fasm pop backend_type
} elif over over "nasm" streq {
StasBackend.nasm pop backend_type
} else {
"unknown backend" usage_msg_and_exit
}
drop drop
}
fn symlink_exe {
'/usr/local/bin' path_exists ! if {
'path `/usr/local/bin` not found!'
error.generic_fatal_noexit
1 exit
}
'/usr/local/bin/stas' drop NULL lstat ENOENT errnoeq ! if {
'/usr/local/bin/stas' drop unlink errno? ! if {
'failed to remove existing symlink at path `/usr/local/bin/stas`, missing permissions?'
error.generic_fatal_noexit
1 exit
}
}
get_executable_path drop '/usr/local/bin/stas' drop symlink errno? ! if {
'failed to symlink executable to path `/usr/local/bin/stas`, missing permissions?'
error.generic_fatal_noexit
1 exit
}
}
const sizeof(fasm_arg_buf) { sizeof(u64) 32 * }
; (str len argv[])
fn stas_exec 3 0 {
auto args 1 pop args
auto str 2 pop str
str args true false execute_child? ! if {
error.generic_fatal_begin
"Failed to execute process `" eputs str eputs "`\n" eputs
1 exit
}
verbose_mode r8 if {
log.msg.start
"`" eputs
args argp_print
"`\n" eputs
}
}
; (infile.str infile.len outfile.str outfile.len)
fn execute_backend 4 0 {
auto outfile 2 pop outfile
auto infile 2 pop infile
reserve arg_buf sizeof(fasm_arg_buf)
auto name 2
auto ld 2 NULL 0 pop ld
debug_symbols r8 object_file r8 ! & if {
outfile new_string_view
dup ".o" push_string_view
string_view_to_str
outfile pop ld
pop outfile
}
backend_type StasBackend.fasm = if {
arg_buf dup "fasm" drop w64
sizeof(u64) + dup infile drop w64
sizeof(u64) + dup outfile drop w64
sizeof(u64) + dup "-m" drop w64
sizeof(u64) + dup "1048576" drop w64
sizeof(u64) + NULL w64
"fasm" arg_buf stas_exec
} elif backend_type StasBackend.nasm = {
arg_buf dup "nasm" drop w64
sizeof(u64) + dup infile drop w64
sizeof(u64) + dup "-o" drop w64
sizeof(u64) + dup outfile drop w64
sizeof(u64) + dup "-O0" drop w64
sizeof(u64) + dup "-felf64" drop w64
debug_symbols r8 if {
sizeof(u64) + dup "-Fdwarf" drop w64
sizeof(u64) + dup "-g" drop w64
}
sizeof(u64) + NULL w64
"nasm" arg_buf stas_exec
}
object_file r8 ! if {
ld drop NULL != if {
arg_buf dup "ld" drop w64
sizeof(u64) + dup outfile drop w64
sizeof(u64) + dup "-o" drop w64
sizeof(u64) + dup ld drop w64
sizeof(u64) + NULL w64
"ld" arg_buf stas_exec
} else {
arg_buf dup "chmod" drop w64
sizeof(u64) + dup "+x" drop w64
sizeof(u64) + dup outfile drop w64
sizeof(u64) + NULL w64
"chmod" arg_buf stas_exec
}
}
}
const ArgParseMode.none { 0 }
const ArgParseMode.output { 1 }
const ArgParseMode.backend { 2 }
const ArgParseMode.comptime_dep { 3 }
const ArgParseMode.os { 4 }
auto argparse_mode 1
fn assert_args {
argparse_mode ArgParseMode.none != if {
argparse_mode ArgParseMode.output = if {
"supply output file for `-o`" usage_msg_and_exit
} elif argparse_mode ArgParseMode.backend = {
"supply backend type for `-b`" usage_msg_and_exit
} elif argparse_mode ArgParseMode.comptime_dep = {
"supply integer for `-comptime-depth`" usage_msg_and_exit
} elif argparse_mode ArgParseMode.os = {
"supply os for `-os`" usage_msg_and_exit
}
}
}
fn main {
argc 1 = if {
usage_and_exit
}
argc 2 = if {
1 args[] "symlink" streq if {
symlink_exe
0 exit
}
}
reserve dump_ir 1
reserve dump_tok 1
reserve to_stdout 1
to_stdout 0 w8
dump_ir 0 w8
dump_tok 0 w8
auto run_exec_arg 1
0 pop run_exec_arg
eval.default_recurse_depth pop eval.recurse_depth
UINT64_MAX pop fwrite_buffer.fd_loc
StasBackend.fasm pop backend_type
auto argstr 2
auto out_file 2
auto in_file 2
NULL 0 pop out_file
NULL 0 pop in_file
ArgParseMode.none pop argparse_mode
debug_symbols false w8
OS.get_host pop target_os
1
while dup argc < {
dup args[] pop argstr
argstr "-o" streq if {
argparse_mode ArgParseMode.none != if {
assert_args
}
ArgParseMode.output pop argparse_mode
} elif argstr "-b" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
ArgParseMode.backend pop argparse_mode
} elif argstr "-comptime-depth" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
ArgParseMode.comptime_dep pop argparse_mode
} elif argstr "-os" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
ArgParseMode.os pop argparse_mode
} elif argstr "-g" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
debug_symbols r8 if {
assert_args
}
debug_symbols true w8
} elif argstr "-v" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
verbose_mode r8 if {
usage_and_exit
}
verbose_mode true w8
} elif argstr "-r" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
pop run_exec_arg
argc
} elif argstr "-c" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
object_file r8 if {
usage_and_exit
}
object_file true w8
} elif argstr "--help" streq argstr "-h" streq | {
help_and_exit
} elif argstr "-dump-ir" streq {
argparse_mode ArgParseMode.none != dump_ir r8 | dump_tok r8 | if {
assert_args
}
dump_ir true w8
} elif argstr "-dump-tok" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
dump_ir r8 dump_tok r8 | if {
usage_and_exit
}
dump_tok true w8
} elif argstr "-trace-calls" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
trace_calls r8 if {
usage_and_exit
}
trace_calls true w8
} elif argstr "-unsafe" streq {
argparse_mode ArgParseMode.none != if {
assert_args
}
strip_assert r8 if {
usage_and_exit
}
strip_assert true w8
} else {
argparse_mode ArgParseMode.none = if {
in_file drop NULL != if {
usage_and_exit
}
argstr pop in_file
} elif argparse_mode ArgParseMode.output = {
out_file drop NULL != if {
usage_and_exit
}
argstr pop out_file
} elif argparse_mode ArgParseMode.backend = {
argstr parse_backend_type
} elif argparse_mode ArgParseMode.os = {
argstr OS.from_str? ! if {
"unknown OS for `-os`" usage_msg_and_exit
}
pop target_os
} elif argparse_mode ArgParseMode.comptime_dep = {
argstr string.to_num? ! if {
"invalid integer for `-comptime-depth`" usage_msg_and_exit
}
pop eval.recurse_depth
} else {
0 assert
}
ArgParseMode.none pop argparse_mode
}
++
}
drop
assert_args
in_file drop NULL = if {
"supply stas file" usage_msg_and_exit
}
out_file drop NULL = if {
object_file r8 if {
"a.o" pop out_file
} else {
"a.out" pop out_file
}
} else {
out_file "-" streq if {
to_stdout true w8
}
}
in_file path_file_isread ! if {
error.generic_fatal_begin
"cannot read file `" eputs
in_file eputs
"`\n" eputs
1 exit
}
verbose_mode r8 if {
log.msg.start
"scanning file `" eputs in_file eputs "`\n" eputs
}
log.time.start
in_file stas.scan_file
"scanning took " log.time.end
dump_tok r8 if {
token_stream.dump
ret
}
target_os OS.linux = if {
"TARGET_LINUX" true parse.def_comptime_const
} elif target_os OS.freebsd = {
"TARGET_FREEBSD" true parse.def_comptime_const
} else {
0 assert -> 'unreachable'
}
verbose_mode r8 if {
log.msg.start
"parsing " eputs token_stream.len eputu " tokens\n" eputs
}
log.time.start
stas.parse
"parsing took " log.time.end
verbose_mode r8 if {
log.msg.start
functions.len eputu " functions, " eputs label_c ++ eputu " labels\n" eputs
; log.msg.start
; global_var_context.len eputu " global variables, " eputs toplevel_constants.len eputu " constants\n" eputs
}
dump_ir r8 if {
ir_stream.dump
ret
}
verbose_mode r8 if {
log.msg.start
"dce pass started\n" eputs
}
log.time.start
stas.dce
"dce took " log.time.end
verbose_mode r8 if {
log.msg.start
used_functions eputu " used functions, of which " eputs inlined_functions eputu " are eligible for inline\n" eputs
log.msg.start
slits.len eputu " string literals\n" eputs
}
assert_tmp_dir
auto out_file_asm 2
to_stdout r8 ! if {
get_tmp_dir
{
salloc.now
out_file file_base salloc.push
".asm" salloc.push
salloc.make_str
}
join_path_cstr
pop out_file_asm
out_file_asm create_new_file_fd? ! if {
error.generic_fatal_begin
"cannot create new file `" eputs
out_file_asm eputs
"` for writing\n" eputs
1 exit
}
pop fwrite_buffer.fd_loc
} else {
stdout pop fwrite_buffer.fd_loc
}
verbose_mode r8 if {
log.msg.start
"generating code from " eputs ir_stream.len eputu " IR instructions\n" eputs
}
log.time.start
in_file stas.gen
"gen took " log.time.end
to_stdout r8 if {
ret
}
verbose_mode r8 if {
log.msg.start
"generated " eputs
fwrite_buffer.fd_loc fd.stat_size? assert -> 'unreachable'
dup 1024 / 0 > if {
eputu " KiBs of code\n" eputs
} else {
eputu " bytes of code\n" eputs
}
}
fwrite_buffer.fd_loc close 0 <s if {
"FATAL: Failed to close file descriptor\n" eputs
1 exit
}
verbose_mode r8 if {
log.msg.start
"wrote code to `" eputs out_file_asm eputs "`\n" eputs
}
verbose_mode r8 if {
log.msg.start
"executing assembler backend `" eputs backend_type StasBackend.to_str eputs "`\n" eputs
}
log.time.start
out_file_asm out_file execute_backend ; run_exec_arg 0 !=
"backend took " log.time.end
verbose_mode r8 if {
log.msg.start
"created binary `" eputs out_file eputs "`\n" eputs
}
run_exec_arg 0 != if {
auto argp 1
{
string_buffer string_buffer.len + pop argp
out_file drop string_buffer.generic_append_u64
run_exec_arg 1 +
while dup argc < {
dup sizeof(u64) * argv + r64 string_buffer.generic_append_u64
++
}
drop
NULL string_buffer.generic_append_u64
}
out_file drop
argp
envp
verbose_mode r8 if {
log.msg.start
"execve binary `" eputs
argp
argp_print
"`\n" eputs
}
execve 0 <s if {
"FATAL: Could not execve file\n" eputs
1 exit
}
}
}