Skip to content

Commit

Permalink
asm: add support for alignment attribute of function definitions
Browse files Browse the repository at this point in the history
Note: this is not a complete fix, but a pragmatic one, as align
is more commonly used for function definitions than return
attributes.

Anyone well versed with LR-1 grammars, feel free to give hints
on how we may resolve this in a proper way.

ref: llir/grammar@71126f6.

Updates #40.
Updates #111.
  • Loading branch information
mewmew committed Dec 4, 2019
1 parent fba5559 commit e68d900
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions asm/asm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func TestParseFile(t *testing.T) {
// parameter attributes.
{path: "testdata/param_attrs.ll"},

// function alignment.
{path: "testdata/func_align.ll"},

// LLVM IR compatibility.
{path: "../testdata/llvm/test/Bitcode/compatibility.ll"},

Expand Down
2 changes: 1 addition & 1 deletion asm/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (gen *generator) irFuncHeader(new *ir.Func, old ast.FuncHeader) error {
if oldFuncAttrs := old.FuncAttrs(); len(oldFuncAttrs) > 0 {
new.FuncAttrs = make([]ir.FuncAttribute, len(oldFuncAttrs))
for i, oldFuncAttr := range oldFuncAttrs {
funcAttr := gen.irFuncAttribute(oldFuncAttr)
funcAttr := gen.irFuncAttributeAndAlign(oldFuncAttr)
new.FuncAttrs[i] = funcAttr
}
}
Expand Down
18 changes: 16 additions & 2 deletions asm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ func (gen *generator) irFuncAttribute(old ast.FuncAttribute) ir.FuncAttribute {
}
}

// irFuncAttributeAndAlign returns the IR function attribute corresponding to
// the given AST function attribute.
func (gen *generator) irFuncAttributeAndAlign(old ast.FuncAttributeAndAlign) ir.FuncAttribute {
switch old := old.(type) {
case *ast.Align:
return ir.Align(uintLit(old.N()))
case ast.FuncAttribute:
return gen.irFuncAttribute(old)
default:
panic(fmt.Errorf("support for function attribute %T not yet implemented", old))
}
}

// irImmutable returns the immutable boolean (constant or global) corresponding
// to the given AST immutable.
func irImmutable(old ast.Immutable) bool {
Expand Down Expand Up @@ -582,8 +595,9 @@ func irReturnAttribute(old ast.ReturnAttribute) ir.ReturnAttribute {
// Key: unquote(old.Key().Text()),
// Value: unquote(old.Val().Text()),
// }
case *ast.Align:
return ir.Align(uintLit(old.N()))
// TODO: re-enable
//case *ast.Align:
// return ir.Align(uintLit(old.N()))
case *ast.Dereferenceable:
return ir.Dereferenceable{N: uintLit(old.N())}
case *ast.DereferenceableOrNull:
Expand Down
4 changes: 4 additions & 0 deletions asm/testdata/func_align.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define void @f() align 2 {
; <label>:0
ret void
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/kr/pretty v0.1.0
github.com/llir/ll v0.0.0-20191127174642-547dd3e0bb7d
github.com/llir/ll v0.0.0-20191204224742-5b40dcb465cd
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2
github.com/mewmew/float v0.0.0-20181121163145-c0f786d7da73
github.com/pkg/errors v0.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/llir/ll v0.0.0-20191127174642-547dd3e0bb7d h1:bULhUX352DfDwqo3i28V8pExOLAaIpL5C4peifi2xQg=
github.com/llir/ll v0.0.0-20191127174642-547dd3e0bb7d/go.mod h1:8W5HJz80PitAyPZUpOcljQxTu6LD5YKW1URTo+OjVoc=
github.com/llir/ll v0.0.0-20191204224742-5b40dcb465cd h1:PNzI1PInJkgFalv/LqxIhpIGHBbSUeQES3XWtBZuwkY=
github.com/llir/ll v0.0.0-20191204224742-5b40dcb465cd/go.mod h1:8W5HJz80PitAyPZUpOcljQxTu6LD5YKW1URTo+OjVoc=
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2 h1:EyTNMdePWaoWsRSGQnXiSoQu0r6RS1eA557AwJhlzHU=
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8+r9zAwpeHJzqRVVMIYnpzD/clwWxyA=
github.com/mewmew/float v0.0.0-20181121163145-c0f786d7da73 h1:bTqCgPsW3TFb9MFtvaOmGFWVhCmN3EmRw02zkchdOHo=
Expand Down
2 changes: 1 addition & 1 deletion testdata
Submodule testdata updated 1 files
+3 −0 .gitignore

0 comments on commit e68d900

Please sign in to comment.