Skip to content

Commit

Permalink
Integer globals working
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 committed Aug 12, 2023
1 parent bdece0f commit b3b1735
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 18 deletions.
2 changes: 1 addition & 1 deletion arch/rj32/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func translateLoadStore(it ir.Iter) {
case op.Load:
it.Update(Load, instr.Def(0).Type, instr.Args())
case op.Store:
it.Update(Store, 0, instr.Args())
it.Update(Store, 0, instr.Arg(1), instr.Arg(2), instr.Arg(0))
}
}

Expand Down
2 changes: 1 addition & 1 deletion asm/customasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (CustomASM) PCRelAddress(offsetWords int) string {

func (CustomASM) Word(value string) string {
wordsize := int(sizes.WordSize()) * sizes.MinAddressableBits()
return fmt.Sprintf("#d%d le(%s)", wordsize, value)
return fmt.Sprintf("#d le(%s`%d)", value, wordsize)
}

func (CustomASM) String(val string) string {
Expand Down
8 changes: 8 additions & 0 deletions asm/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func (emit *Emitter) fn(fn *ir.Func) {
}
if blk.NumSuccs() > 0 {
args = append(args, emit.fmter.BlockLabel(blk.Succ(0).IDString()))

// if a conditional block doesn't fall through on the false branch,
// make sure an extra jump is added
if b < fn.NumBlocks()-1 &&
blk.NumSuccs() > 1 &&
blk.Succ(1) != fn.Block(b+1) {
args = append(args, emit.fmter.BlockLabel(blk.Succ(1).IDString()))
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion regalloc/livescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,17 @@ func (ra *RegAlloc) liveInOutScan() error {
// otherwise a value is used somewhere that never
// got defined
if len(ra.info) > 0 && len(ra.info[0].liveIns) > 0 {
hasNonSpecialReg := false
for val := range ra.info[0].liveIns {
if val.ValueIn(fn).Reg().IsSpecialReg() {
continue
}
hasNonSpecialReg = true
fmt.Println("live in:", val.ValueIn(fn))
}
return ErrEntryLiveIns
if hasNonSpecialReg {
return ErrEntryLiveIns
}
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion regalloc/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func Verify(fn *ir.Func) []error {
continue
}

// skip special reg references
if arg.Reg().IsSpecialReg() {
continue
}

// check the value currently residing in the register, if it doesn't
// match, then report it
regidx := regIndex[arg.Reg()]
Expand Down Expand Up @@ -175,7 +180,6 @@ func Verify(fn *ir.Func) []error {
arg := blk.Arg(argoffset + d)

if def.Reg() != arg.Reg() {
// todo: when blk parameter copies are implemented uncomment this
errs = append(errs,
fmt.Errorf("%w: fn %s from blk %s to blk %s: from arg %s to def %s", ErrMissingCopy, fn.Name, blk, succ, arg, def))
regidx := regIndex[arg.Reg()]
Expand Down
4 changes: 3 additions & 1 deletion testdata/000_simple.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ target triple = "msp430-unknown-unknown-elf"

; Function Attrs: noinline nounwind optnone
define dso_local i16 @main() #0 {
%1 = alloca i16, align 2
store i16 0, i16* %1, align 2
ret i16 42
}

attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
Expand Down
13 changes: 7 additions & 6 deletions testdata/001_add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ define dso_local i16 @add(i16 noundef %0, i16 noundef %1) #0 {
; Function Attrs: noinline nounwind optnone
define dso_local i16 @main() #0 {
%1 = alloca i16, align 2
%2 = call i16 @add(i16 noundef 38, i16 noundef 4) #1
store i16 %2, i16* %1, align 2
%3 = load i16, i16* %1, align 2
ret i16 %3
%2 = alloca i16, align 2
store i16 0, i16* %1, align 2
%3 = call i16 @add(i16 noundef 38, i16 noundef 4)
store i16 %3, i16* %2, align 2
%4 = load i16, i16* %2, align 2
ret i16 %4
}

attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nobuiltin "no-builtins" }
attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
Expand Down
9 changes: 5 additions & 4 deletions testdata/002_fib.ll
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ define dso_local i16 @fibonacci(i16 noundef %0) #0 {

; Function Attrs: noinline nounwind optnone
define dso_local i16 @main() #0 {
%1 = call i16 @fibonacci(i16 noundef 7) #1
ret i16 %1
%1 = alloca i16, align 2
store i16 0, i16* %1, align 2
%2 = call i16 @fibonacci(i16 noundef 7)
ret i16 %2
}

attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { nobuiltin "no-builtins" }
attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
Expand Down
7 changes: 7 additions & 0 deletions testdata/003_global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int glb = 39;
int other = 9;

int main() {
other = 3;
return glb+other;
}
26 changes: 26 additions & 0 deletions testdata/003_global.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; ModuleID = '003_global.c'
source_filename = "003_global.c"
target datalayout = "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16"
target triple = "msp430-unknown-unknown-elf"

@glb = dso_local global i16 39, align 2
@other = dso_local global i16 9, align 2

; Function Attrs: noinline nounwind optnone
define dso_local i16 @main() #0 {
%1 = alloca i16, align 2
store i16 0, i16* %1, align 2
store i16 3, i16* @other, align 2
%2 = load i16, i16* @glb, align 2
%3 = load i16, i16* @other, align 2
%4 = add nsw i16 %2, %3
ret i16 %4
}

attributes #0 = { noinline nounwind optnone "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"}
4 changes: 2 additions & 2 deletions testdata/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
TESTS = 000_simple.ll 001_add.ll 002_fib.ll
TESTS = 000_simple.ll 001_add.ll 002_fib.ll 003_global.ll

all: $(TESTS)

%.ll: %.c
clang --target=msp430-unknown-elf -march=msp430 -ffreestanding -fno-builtin -nostdlib -emit-llvm -S $<
clang --target=msp430-unknown-elf -march=msp430 -nostdlib -emit-llvm -S $<

clean:
rm -rf $(TESTS)
7 changes: 7 additions & 0 deletions translate/operands.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (trans *translator) translateOperands(fn llvm.Value) {
if ntyp.Pointer().Element.Kind() == typ.FunctionKind {
otherfn := trans.pkg.Func(operand.Name())
ninstr.InsertArg(0, trans.fn.ValueFor(ntyp.Pointer().Element, otherfn))
} else if !operand.IsAGlobalVariable().IsNil() {
globalName := operand.Name()
glob := trans.pkg.Global(globalName)
glob.Referenced = true
val := trans.fn.ValueFor(translateType(operand.Type()), glob)
ninstr.InsertArg(-1, val)

} else {
instr.Dump()
panic(" other constant pointer")
Expand Down
15 changes: 14 additions & 1 deletion translate/translate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package translate

import (
"log"

"github.com/rj45/llir2asm/ir"
"github.com/rj45/llir2asm/ir/typ"
"tinygo.org/x/go-llvm"
)

Expand Down Expand Up @@ -36,7 +39,17 @@ func (trans *translator) initProgram() {

func (trans *translator) translateGlobals() {
for glob := trans.mod.FirstGlobal(); !glob.IsNil(); glob = llvm.NextGlobal(glob) {
trans.pkg.NewGlobal(glob.Name(), translateType(glob.Type()))
nglob := trans.pkg.NewGlobal(glob.Name(), translateType(glob.Type()).Pointer().Element)
if glob.OperandsCount() > 0 {
value := glob.Operand(0)
switch nglob.Type.Kind() {
case typ.IntegerKind:
nglob.Value = ir.ConstFor(value.SExtValue())
default:
log.Panicf("unknown kind %d", value.Type().TypeKind())
}
}

// todo: set global value?
}
}
34 changes: 34 additions & 0 deletions xform/elaboration/gprelglobals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package elaboration

import (
"github.com/rj45/llir2asm/ir"
"github.com/rj45/llir2asm/ir/op"
"github.com/rj45/llir2asm/ir/reg"
"github.com/rj45/llir2asm/xform"
)

var _ = xform.Register(loadGPRelGlobals,
xform.OnlyPass(xform.Elaboration),
xform.OnOp(op.Load),
)

var _ = xform.Register(storeGPRelGlobals,
xform.OnlyPass(xform.Elaboration),
xform.OnOp(op.Store),
)

func loadGPRelGlobals(it ir.Iter) {
instr := it.Instr()

if instr.Arg(0).IsConst() {
instr.InsertArg(0, instr.Func().ValueFor(0, reg.GP))
}
}

func storeGPRelGlobals(it ir.Iter) {
instr := it.Instr()

if instr.Arg(1).IsConst() {
instr.InsertArg(1, instr.Func().ValueFor(0, reg.GP))
}
}
21 changes: 21 additions & 0 deletions xform/elaboration/storeconst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package elaboration

import (
"github.com/rj45/llir2asm/ir"
"github.com/rj45/llir2asm/ir/op"
"github.com/rj45/llir2asm/xform"
)

var _ = xform.Register(addStoreConstCopies,
xform.OnlyPass(xform.Elaboration),
xform.OnOp(op.Store),
)

func addStoreConstCopies(it ir.Iter) {
instr := it.Instr()

if instr.Arg(0).IsConst() {
cp := it.Insert(op.Copy, instr.Arg(0).Type, instr.Arg(0))
instr.ReplaceArg(0, cp.Def(0))
}
}

0 comments on commit b3b1735

Please sign in to comment.