diff options
author | Ayke van Laethem <[email protected]> | 2020-03-18 20:32:46 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2020-03-19 19:56:08 +0100 |
commit | b6314fa6ab02d9e81f9a62cab93b67f60371d4ee (patch) | |
tree | d1e51356f1530910fa0132f0e6d56406ff74744e /transform | |
parent | 945ff4d1604479e234ad7df9e8bd3200553ca512 (diff) | |
download | tinygo-b6314fa6ab02d9e81f9a62cab93b67f60371d4ee.tar.gz tinygo-b6314fa6ab02d9e81f9a62cab93b67f60371d4ee.zip |
compiler: move ApplyFunctionSections to transform package
Diffstat (limited to 'transform')
-rw-r--r-- | transform/globals.go | 20 | ||||
-rw-r--r-- | transform/globals_test.go | 14 | ||||
-rw-r--r-- | transform/testdata/globals-function-sections.ll | 8 | ||||
-rw-r--r-- | transform/testdata/globals-function-sections.out.ll | 8 |
4 files changed, 50 insertions, 0 deletions
diff --git a/transform/globals.go b/transform/globals.go new file mode 100644 index 000000000..c142fed85 --- /dev/null +++ b/transform/globals.go @@ -0,0 +1,20 @@ +package transform + +import "tinygo.org/x/go-llvm" + +// This file implements small transformations on globals (functions and global +// variables) for specific ABIs/architectures. + +// ApplyFunctionSections puts every function in a separate section. This makes +// it possible for the linker to remove dead code. It is the equivalent of +// passing -ffunction-sections to a C compiler. +func ApplyFunctionSections(mod llvm.Module) { + llvmFn := mod.FirstFunction() + for !llvmFn.IsNil() { + if !llvmFn.IsDeclaration() { + name := llvmFn.Name() + llvmFn.SetSection(".text." + name) + } + llvmFn = llvm.NextFunction(llvmFn) + } +} diff --git a/transform/globals_test.go b/transform/globals_test.go new file mode 100644 index 000000000..0dcb0608d --- /dev/null +++ b/transform/globals_test.go @@ -0,0 +1,14 @@ +package transform + +import ( + "testing" + + "tinygo.org/x/go-llvm" +) + +func TestApplyFunctionSections(t *testing.T) { + t.Parallel() + testTransform(t, "testdata/globals-function-sections", func(mod llvm.Module) { + ApplyFunctionSections(mod) + }) +} diff --git a/transform/testdata/globals-function-sections.ll b/transform/testdata/globals-function-sections.ll new file mode 100644 index 000000000..505ba5aa5 --- /dev/null +++ b/transform/testdata/globals-function-sections.ll @@ -0,0 +1,8 @@ +target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "armv7em-none-eabi" + +declare void @foo() + +define void @bar() { + ret void +} diff --git a/transform/testdata/globals-function-sections.out.ll b/transform/testdata/globals-function-sections.out.ll new file mode 100644 index 000000000..e3d03ed07 --- /dev/null +++ b/transform/testdata/globals-function-sections.out.ll @@ -0,0 +1,8 @@ +target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "armv7em-none-eabi" + +declare void @foo() + +define void @bar() section ".text.bar" { + ret void +} |