all: replace ReadRegister with AsmFull inline assembly
This makes AsmFull more powerful (by supporting return values) and avoids a compiler builtin.
Этот коммит содержится в:
родитель
9342e73ae1
коммит
6389e45d99
8 изменённых файлов: 66 добавлений и 55 удалений
|
@ -1364,8 +1364,6 @@ func (b *builder) createFunctionCall(instr *ssa.CallCommon) (llvm.Value, error)
|
||||||
return b.createMemoryCopyCall(fn, instr.Args)
|
return b.createMemoryCopyCall(fn, instr.Args)
|
||||||
case name == "runtime.memzero":
|
case name == "runtime.memzero":
|
||||||
return b.createMemoryZeroCall(instr.Args)
|
return b.createMemoryZeroCall(instr.Args)
|
||||||
case name == "device/arm.ReadRegister" || name == "device/riscv.ReadRegister":
|
|
||||||
return b.createReadRegister(name, instr.Args)
|
|
||||||
case name == "device/arm.Asm" || name == "device/avr.Asm" || name == "device/riscv.Asm":
|
case name == "device/arm.Asm" || name == "device/avr.Asm" || name == "device/riscv.Asm":
|
||||||
return b.createInlineAsm(instr.Args)
|
return b.createInlineAsm(instr.Args)
|
||||||
case name == "device/arm.AsmFull" || name == "device/avr.AsmFull" || name == "device/riscv.AsmFull":
|
case name == "device/arm.AsmFull" || name == "device/avr.AsmFull" || name == "device/riscv.AsmFull":
|
||||||
|
|
|
@ -13,27 +13,6 @@ import (
|
||||||
"tinygo.org/x/go-llvm"
|
"tinygo.org/x/go-llvm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is a compiler builtin, which reads the given register by name:
|
|
||||||
//
|
|
||||||
// func ReadRegister(name string) uintptr
|
|
||||||
//
|
|
||||||
// The register name must be a constant, for example "sp".
|
|
||||||
func (b *builder) createReadRegister(name string, args []ssa.Value) (llvm.Value, error) {
|
|
||||||
fnType := llvm.FunctionType(b.uintptrType, []llvm.Type{}, false)
|
|
||||||
regname := constant.StringVal(args[0].(*ssa.Const).Value)
|
|
||||||
var asm string
|
|
||||||
switch name {
|
|
||||||
case "device/arm.ReadRegister":
|
|
||||||
asm = "mov $0, " + regname
|
|
||||||
case "device/riscv.ReadRegister":
|
|
||||||
asm = "mv $0, " + regname
|
|
||||||
default:
|
|
||||||
panic("unknown architecture")
|
|
||||||
}
|
|
||||||
target := llvm.InlineAsm(fnType, asm, "=r", false, false, 0)
|
|
||||||
return b.CreateCall(target, nil, ""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is a compiler builtin, which emits a piece of inline assembly with no
|
// This is a compiler builtin, which emits a piece of inline assembly with no
|
||||||
// operands or return values. It is useful for trivial instructions, like wfi in
|
// operands or return values. It is useful for trivial instructions, like wfi in
|
||||||
// ARM or sleep in AVR.
|
// ARM or sleep in AVR.
|
||||||
|
@ -52,7 +31,7 @@ func (b *builder) createInlineAsm(args []ssa.Value) (llvm.Value, error) {
|
||||||
// This is a compiler builtin, which allows assembly to be called in a flexible
|
// This is a compiler builtin, which allows assembly to be called in a flexible
|
||||||
// way.
|
// way.
|
||||||
//
|
//
|
||||||
// func AsmFull(asm string, regs map[string]interface{})
|
// func AsmFull(asm string, regs map[string]interface{}) uintptr
|
||||||
//
|
//
|
||||||
// The asm parameter must be a constant string. The regs parameter must be
|
// The asm parameter must be a constant string. The regs parameter must be
|
||||||
// provided immediately. For example:
|
// provided immediately. For example:
|
||||||
|
@ -66,7 +45,7 @@ func (b *builder) createInlineAsm(args []ssa.Value) (llvm.Value, error) {
|
||||||
func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error) {
|
func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error) {
|
||||||
asmString := constant.StringVal(instr.Args[0].(*ssa.Const).Value)
|
asmString := constant.StringVal(instr.Args[0].(*ssa.Const).Value)
|
||||||
registers := map[string]llvm.Value{}
|
registers := map[string]llvm.Value{}
|
||||||
registerMap := instr.Args[1].(*ssa.MakeMap)
|
if registerMap, ok := instr.Args[1].(*ssa.MakeMap); ok {
|
||||||
for _, r := range *registerMap.Referrers() {
|
for _, r := range *registerMap.Referrers() {
|
||||||
switch r := r.(type) {
|
switch r := r.(type) {
|
||||||
case *ssa.DebugRef:
|
case *ssa.DebugRef:
|
||||||
|
@ -76,7 +55,6 @@ func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error)
|
||||||
return llvm.Value{}, b.makeError(instr.Pos(), "register value map must be created in the same basic block")
|
return llvm.Value{}, b.makeError(instr.Pos(), "register value map must be created in the same basic block")
|
||||||
}
|
}
|
||||||
key := constant.StringVal(r.Key.(*ssa.Const).Value)
|
key := constant.StringVal(r.Key.(*ssa.Const).Value)
|
||||||
//println("value:", r.Value.(*ssa.MakeInterface).X.String())
|
|
||||||
registers[key] = b.getValue(r.Value.(*ssa.MakeInterface).X)
|
registers[key] = b.getValue(r.Value.(*ssa.MakeInterface).X)
|
||||||
case *ssa.Call:
|
case *ssa.Call:
|
||||||
if r.Common() == instr {
|
if r.Common() == instr {
|
||||||
|
@ -86,12 +64,22 @@ func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error)
|
||||||
return llvm.Value{}, b.makeError(instr.Pos(), "don't know how to handle argument to inline assembly: "+r.String())
|
return llvm.Value{}, b.makeError(instr.Pos(), "don't know how to handle argument to inline assembly: "+r.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// TODO: handle dollar signs in asm string
|
// TODO: handle dollar signs in asm string
|
||||||
registerNumbers := map[string]int{}
|
registerNumbers := map[string]int{}
|
||||||
var err error
|
var err error
|
||||||
argTypes := []llvm.Type{}
|
argTypes := []llvm.Type{}
|
||||||
args := []llvm.Value{}
|
args := []llvm.Value{}
|
||||||
constraints := []string{}
|
constraints := []string{}
|
||||||
|
hasOutput := false
|
||||||
|
asmString = regexp.MustCompile("\\{\\}").ReplaceAllStringFunc(asmString, func(s string) string {
|
||||||
|
hasOutput = true
|
||||||
|
return "$0"
|
||||||
|
})
|
||||||
|
if hasOutput {
|
||||||
|
constraints = append(constraints, "=&r")
|
||||||
|
registerNumbers[""] = 0
|
||||||
|
}
|
||||||
asmString = regexp.MustCompile("\\{[a-zA-Z]+\\}").ReplaceAllStringFunc(asmString, func(s string) string {
|
asmString = regexp.MustCompile("\\{[a-zA-Z]+\\}").ReplaceAllStringFunc(asmString, func(s string) string {
|
||||||
// TODO: skip strings like {r4} etc. that look like ARM push/pop
|
// TODO: skip strings like {r4} etc. that look like ARM push/pop
|
||||||
// instructions.
|
// instructions.
|
||||||
|
@ -121,9 +109,21 @@ func (b *builder) createInlineAsmFull(instr *ssa.CallCommon) (llvm.Value, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return llvm.Value{}, err
|
return llvm.Value{}, err
|
||||||
}
|
}
|
||||||
fnType := llvm.FunctionType(b.ctx.VoidType(), argTypes, false)
|
var outputType llvm.Type
|
||||||
|
if hasOutput {
|
||||||
|
outputType = b.uintptrType
|
||||||
|
} else {
|
||||||
|
outputType = b.ctx.VoidType()
|
||||||
|
}
|
||||||
|
fnType := llvm.FunctionType(outputType, argTypes, false)
|
||||||
target := llvm.InlineAsm(fnType, asmString, strings.Join(constraints, ","), true, false, 0)
|
target := llvm.InlineAsm(fnType, asmString, strings.Join(constraints, ","), true, false, 0)
|
||||||
return b.CreateCall(target, args, ""), nil
|
result := b.CreateCall(target, args, "")
|
||||||
|
if hasOutput {
|
||||||
|
return result, nil
|
||||||
|
} else {
|
||||||
|
// Make sure we return something valid.
|
||||||
|
return llvm.ConstInt(b.uintptrType, 0, false), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a compiler builtin which emits an inline SVCall instruction. It can
|
// This is a compiler builtin which emits an inline SVCall instruction. It can
|
||||||
|
|
|
@ -52,11 +52,10 @@ func Asm(asm string)
|
||||||
// "value": 1
|
// "value": 1
|
||||||
// "result": &dest,
|
// "result": &dest,
|
||||||
// })
|
// })
|
||||||
func AsmFull(asm string, regs map[string]interface{})
|
//
|
||||||
|
// You can use {} in the asm string (which expands to a register) to set the
|
||||||
// ReadRegister returns the contents of the specified register. The register
|
// return value.
|
||||||
// must be a processor register, reachable with the "mov" instruction.
|
func AsmFull(asm string, regs map[string]interface{}) uintptr
|
||||||
func ReadRegister(name string) uintptr
|
|
||||||
|
|
||||||
// Run the following system call (SVCall) with 0 arguments.
|
// Run the following system call (SVCall) with 0 arguments.
|
||||||
func SVCall0(num uintptr) uintptr
|
func SVCall0(num uintptr) uintptr
|
||||||
|
|
|
@ -15,4 +15,7 @@ func Asm(asm string)
|
||||||
// "value": 1
|
// "value": 1
|
||||||
// "result": &dest,
|
// "result": &dest,
|
||||||
// })
|
// })
|
||||||
func AsmFull(asm string, regs map[string]interface{})
|
//
|
||||||
|
// You can use {} in the asm string (which expands to a register) to set the
|
||||||
|
// return value.
|
||||||
|
func AsmFull(asm string, regs map[string]interface{}) uintptr
|
||||||
|
|
|
@ -5,6 +5,17 @@ package riscv
|
||||||
// optimizer.
|
// optimizer.
|
||||||
func Asm(asm string)
|
func Asm(asm string)
|
||||||
|
|
||||||
// ReadRegister returns the contents of the specified register. The register
|
// Run the given inline assembly. The code will be marked as having side
|
||||||
// must be a processor register, reachable with the "mov" instruction.
|
// effects, as it would otherwise be optimized away. The inline assembly string
|
||||||
func ReadRegister(name string) uintptr
|
// recognizes template values in the form {name}, like so:
|
||||||
|
//
|
||||||
|
// arm.AsmFull(
|
||||||
|
// "st {value}, {result}",
|
||||||
|
// map[string]interface{}{
|
||||||
|
// "value": 1
|
||||||
|
// "result": &dest,
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// You can use {} in the asm string (which expands to a register) to set the
|
||||||
|
// return value.
|
||||||
|
func AsmFull(asm string, regs map[string]interface{}) uintptr
|
||||||
|
|
|
@ -15,5 +15,5 @@ func align(ptr uintptr) uintptr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentStackPointer() uintptr {
|
func getCurrentStackPointer() uintptr {
|
||||||
return arm.ReadRegister("sp")
|
return arm.AsmFull("mov {}, sp", nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,5 +17,5 @@ func align(ptr uintptr) uintptr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentStackPointer() uintptr {
|
func getCurrentStackPointer() uintptr {
|
||||||
return arm.ReadRegister("sp")
|
return arm.AsmFull("mov {}, sp", nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,5 +15,5 @@ func align(ptr uintptr) uintptr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentStackPointer() uintptr {
|
func getCurrentStackPointer() uintptr {
|
||||||
return riscv.ReadRegister("sp")
|
return riscv.AsmFull("mv {}, sp", nil)
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче