compiler: add //go:align pragma

This is sometimes needed for globals. For example, the atsamd21 chip
requires the DMA buffers to be aligned on a 16 byte boundary.
Этот коммит содержится в:
Ayke van Laethem 2019-09-18 19:53:02 +02:00 коммит произвёл Ron Evans
родитель d40fb5bc46
коммит cf2a7b5089

Просмотреть файл

@ -7,6 +7,7 @@ import (
"go/ast"
"go/token"
"go/types"
"strconv"
"strings"
"github.com/tinygo-org/tinygo/loader"
@ -20,6 +21,7 @@ import (
type globalInfo struct {
linkName string // go:extern
extern bool // go:extern
align int // go:align
}
// loadASTComments loads comments on globals from the AST, for use later in the
@ -64,6 +66,9 @@ func (c *Compiler) getGlobal(g *ssa.Global) llvm.Value {
llvmGlobal.SetInitializer(llvm.ConstNull(llvmType))
llvmGlobal.SetLinkage(llvm.InternalLinkage)
}
if info.align > c.targetData.ABITypeAlignment(llvmType) {
llvmGlobal.SetAlignment(info.align)
}
}
return llvmGlobal
}
@ -102,6 +107,11 @@ func (info *globalInfo) parsePragmas(doc *ast.CommentGroup) {
if len(parts) == 2 {
info.linkName = parts[1]
}
case "//go:align":
align, err := strconv.Atoi(parts[1])
if err == nil {
info.align = align
}
}
}
}