Skip to content

Commit 12667d8

Browse files
Enable syscalls and do not preload buffer functions.
1 parent 82011d6 commit 12667d8

6 files changed

Lines changed: 52 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
*.elf
33
reasm
44
.DS_Store
5-
main.luau
65
bench
76
sourcemap.json
87
*.c
9-
main
8+
main.*
109
platform.info
1110
profile.*

compiler/abstractions.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,26 @@ func move(w *OutputWriter, command AssemblyCommand) {
6969
src := irArgExpr(w, command.Arguments[1])
7070
Emit(w, IRStmtAssign(dst, src))
7171
}
72+
func ecall(w *OutputWriter, command AssemblyCommand) {
73+
a7 := regVarName(baseRegs["x17"])
74+
WriteIndentedString(w, "local syscall_fn = syscalls[%s]\n", a7)
75+
WriteIndentedString(w, "if syscall_fn then\n")
76+
w.Depth++
77+
WriteIndentedString(w, "syscall_fn()\n")
78+
w.Depth--
79+
WriteIndentedString(w, "else\n")
80+
w.Depth++
81+
WriteIndentedString(w, "error(\"Undefined syscall: \" .. tostring(%s))\n", a7)
82+
w.Depth--
83+
WriteIndentedString(w, "end\n")
84+
}
7285

7386
/* unimplemented */
7487
func ebreak(w *OutputWriter, command AssemblyCommand) {
75-
log.Warn("EBREAK cannot be used (yet).")
76-
}
77-
func ecall(w *OutputWriter, command AssemblyCommand) {
78-
log.Warn("ECALL cannot be used (yet).")
88+
log.Warn("Breakpoints are unsupported")
7989
}
8090
func fence(w *OutputWriter, command AssemblyCommand) {
81-
log.Warn("FENCE cannot be used.")
91+
log.Warn("Fences are unsupported")
8292
}
8393
func nop(w *OutputWriter, command AssemblyCommand) {
8494
if w.Options.Comments {

compiler/boilerplate.luau

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,12 @@ local functions = {
260260
end,
261261
}
262262

263+
local syscalls: {[number]: () -> ()} = {}
264+
263265
-- Extensions
264266
--{extentions here}
265267

266268
-- Localized Functions
267-
--- buffer
268-
local writei8, writei16, writei32 = buffer.writei8, buffer.writei16, buffer.writei32
269-
local readi8, readi16, readi32 = buffer.readi8, buffer.readi16, buffer.readi32
270-
local writeu8, writeu16, writeu32, writestring, fill = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writestring, buffer.fill
271-
local readu8, readu16, readu32, readf32, readf64, writef32, writef64 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readf32, buffer.readf64, buffer.writef32, buffer.writef64
272-
273269
local FUNCS: {[number]: () -> boolean} = {}
274270
---- Auto generated code starts here
275271
--{code here}

compiler/elf.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,36 @@ func ParseFromElf(f *elf.File) []AssemblyCommand {
4747
return symtab[i].Value < symtab[j].Value
4848
})
4949

50-
symStack := make([]elf.Symbol, len(symtab))
51-
copy(symStack, symtab)
52-
53-
/* TODO: parse the actual data of the file */
5450
instructions := make([]AssemblyCommand, 0)
51+
52+
symIndex := 0
5553
for i := 0; i < len(code); i += 4 {
5654
inst, err := riscv64asm.Decode(code[i : i+4])
5755
if err != nil {
5856
fmt.Println("Decode error:", err)
5957
continue
6058
}
6159

60+
addr := text.Addr + uint64(i)
61+
6262
/* any symbols to be added before this */
63-
for _, sym := range symStack {
64-
symStack = symStack[:len(symStack)-1] // pop
63+
for symIndex < len(symtab) && symtab[symIndex].Value < addr {
64+
symIndex++
65+
}
66+
for symIndex < len(symtab) && symtab[symIndex].Value == addr {
67+
instructions = append(instructions, AssemblyCommand{
68+
Type: Label,
69+
Name: symtab[symIndex].Name,
70+
Arguments: nil,
71+
})
72+
symIndex++
73+
}
6574

75+
addrLabel := fmt.Sprintf("%d", addr)
76+
if len(instructions) == 0 || instructions[len(instructions)-1].Name != addrLabel {
6677
instructions = append(instructions, AssemblyCommand{
6778
Type: Label,
68-
Name: sym.Name,
79+
Name: addrLabel,
6980
Arguments: nil,
7081
})
7182
}

compiler/ir.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@ const (
6161
MATH_FLOOR = "math.floor"
6262
MATH_CEIL = "math.ceil"
6363

64-
BUFFER_READI8 = "readi8"
65-
BUFFER_READI16 = "readi16"
66-
BUFFER_READI32 = "readi32"
67-
BUFFER_READU8 = "readu8"
68-
BUFFER_READU16 = "readu16"
69-
BUFFER_READU32 = "readu32"
70-
BUFFER_READF32 = "readf32"
71-
BUFFER_READF64 = "readf64"
72-
BUFFER_WRITEI8 = "writei8"
73-
BUFFER_WRITEI16 = "writei16"
74-
BUFFER_WRITEI32 = "writei32"
75-
BUFFER_WRITEU8 = "writeu8"
76-
BUFFER_WRITEF32 = "writef32"
77-
BUFFER_WRITEF64 = "writef64"
78-
BUFFER_WRITESTR = "writestring"
79-
BUFFER_FILL = "fill"
64+
BUFFER_READI8 = "buffer.readi8"
65+
BUFFER_READI16 = "buffer.readi16"
66+
BUFFER_READI32 = "buffer.readi32"
67+
BUFFER_READU8 = "buffer.readu8"
68+
BUFFER_READU16 = "buffer.readu16"
69+
BUFFER_READU32 = "buffer.readu32"
70+
BUFFER_READF32 = "buffer.readf32"
71+
BUFFER_READF64 = "buffer.readf64"
72+
BUFFER_WRITEI8 = "buffer.writei8"
73+
BUFFER_WRITEI16 = "buffer.writei16"
74+
BUFFER_WRITEI32 = "buffer.writei32"
75+
BUFFER_WRITEU8 = "buffer.writeu8"
76+
BUFFER_WRITEF32 = "buffer.writef32"
77+
BUFFER_WRITEF64 = "buffer.writef64"
78+
BUFFER_WRITESTR = "buffer.writestring"
79+
BUFFER_FILL = "buffer.fill"
8080
BUFFER_LEN = "buffer.len"
8181

8282
RT_I32 = "i32"

main

4.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)