aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2020-03-18 20:41:19 +0100
committerRon Evans <[email protected]>2020-03-19 19:56:08 +0100
commitc5cb2cec9b7c8e4cee016d4c5c485e83dc872d05 (patch)
tree7f16f0affcc54d0b356aa3caa3450a9ad9d75315 /transform
parentb6314fa6ab02d9e81f9a62cab93b67f60371d4ee (diff)
downloadtinygo-c5cb2cec9b7c8e4cee016d4c5c485e83dc872d05.tar.gz
tinygo-c5cb2cec9b7c8e4cee016d4c5c485e83dc872d05.zip
compiler: move NonConstGlobals pass to transform package
Diffstat (limited to 'transform')
-rw-r--r--transform/globals.go13
-rw-r--r--transform/globals_test.go7
-rw-r--r--transform/testdata/globals-non-const.ll5
-rw-r--r--transform/testdata/globals-non-const.out.ll5
4 files changed, 30 insertions, 0 deletions
diff --git a/transform/globals.go b/transform/globals.go
index c142fed85..89386fd84 100644
--- a/transform/globals.go
+++ b/transform/globals.go
@@ -18,3 +18,16 @@ func ApplyFunctionSections(mod llvm.Module) {
llvmFn = llvm.NextFunction(llvmFn)
}
}
+
+// NonConstGlobals turns all global constants into global variables. This works
+// around a limitation on Harvard architectures (e.g. AVR), where constant and
+// non-constant pointers point to a different address space. Normal pointer
+// behavior is restored by using the data space only, at the cost of RAM for
+// constant global variables.
+func NonConstGlobals(mod llvm.Module) {
+ global := mod.FirstGlobal()
+ for !global.IsNil() {
+ global.SetGlobalConstant(false)
+ global = llvm.NextGlobal(global)
+ }
+}
diff --git a/transform/globals_test.go b/transform/globals_test.go
index 0dcb0608d..14c77e6a6 100644
--- a/transform/globals_test.go
+++ b/transform/globals_test.go
@@ -12,3 +12,10 @@ func TestApplyFunctionSections(t *testing.T) {
ApplyFunctionSections(mod)
})
}
+
+func TestNonConstGlobals(t *testing.T) {
+ t.Parallel()
+ testTransform(t, "testdata/globals-non-const", func(mod llvm.Module) {
+ NonConstGlobals(mod)
+ })
+}
diff --git a/transform/testdata/globals-non-const.ll b/transform/testdata/globals-non-const.ll
new file mode 100644
index 000000000..5f3c5846a
--- /dev/null
+++ b/transform/testdata/globals-non-const.ll
@@ -0,0 +1,5 @@
+target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
+target triple = "armv7em-none-eabi"
+
+@globalIntConst = constant i32 3
+@globalIntVar = global i32 5
diff --git a/transform/testdata/globals-non-const.out.ll b/transform/testdata/globals-non-const.out.ll
new file mode 100644
index 000000000..33b577ebb
--- /dev/null
+++ b/transform/testdata/globals-non-const.out.ll
@@ -0,0 +1,5 @@
+target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
+target triple = "armv7em-none-eabi"
+
+@globalIntConst = global i32 3
+@globalIntVar = global i32 5