aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-10-31 10:44:21 +0100
committerAyke <[email protected]>2024-11-01 09:10:25 +0100
commit4e49ba597dc20e0bfd13fc39e0a1c89959576ec5 (patch)
treeb11997b2f404bf852927713820b2397d4df8b389
parent4b706ae25c2ab52f0aabdb78baeaef4cdcf3578c (diff)
downloadtinygo-4e49ba597dc20e0bfd13fc39e0a1c89959576ec5.tar.gz
tinygo-4e49ba597dc20e0bfd13fc39e0a1c89959576ec5.zip
interrupt: fix bug in interrupt lowering
The alignment wasn't set, so defaulted to 4 (for a 32-bit int). LLVM saw this, and therefore assumed that a ptrtoint of the pointer would have had the lowest bits unset. That's an entirely valid optimization, except that we are using these globals for arbitrary values (and aren't actually using these globals). Fixed by setting alignment to 1. It works, though long-term we should maybe find a different solution for this.
-rw-r--r--compiler/interrupt.go3
1 files changed, 3 insertions, 0 deletions
diff --git a/compiler/interrupt.go b/compiler/interrupt.go
index 6ba031819..68c8a3605 100644
--- a/compiler/interrupt.go
+++ b/compiler/interrupt.go
@@ -41,6 +41,8 @@ func (b *builder) createInterruptGlobal(instr *ssa.CallCommon) (llvm.Value, erro
// Create a new global of type runtime/interrupt.handle. Globals of this
// type are lowered in the interrupt lowering pass.
+ // It must have an alignment of 1, otherwise LLVM thinks a ptrtoint of the
+ // global has the lower bits unset.
globalType := b.program.ImportedPackage("runtime/interrupt").Type("handle").Type()
globalLLVMType := b.getLLVMType(globalType)
globalName := b.fn.Package().Pkg.Path() + "$interrupt" + strconv.FormatInt(id.Int64(), 10)
@@ -48,6 +50,7 @@ func (b *builder) createInterruptGlobal(instr *ssa.CallCommon) (llvm.Value, erro
global.SetVisibility(llvm.HiddenVisibility)
global.SetGlobalConstant(true)
global.SetUnnamedAddr(true)
+ global.SetAlignment(1)
initializer := llvm.ConstNull(globalLLVMType)
initializer = b.CreateInsertValue(initializer, funcContext, 0, "")
initializer = b.CreateInsertValue(initializer, funcPtr, 1, "")