summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorParashurama <Rhagdamaziel@ymail.com>2017-08-19 09:02:02 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-08-19 09:02:02 +0200
commit837b77b0a1383379ea052ff8e763d634d6a4549f (patch)
tree70a03de6344d5e9478094690e586ca4cec0306f0 /compiler
parentf64eee3a1ebf1cda39160551d0070eada6ed34c9 (diff)
downloadNim-837b77b0a1383379ea052ff8e763d634d6a4549f.tar.gz
fixes basic type alignement issue on i386 platform. (#6209)
Diffstat (limited to 'compiler')
-rw-r--r--compiler/semtypes.nim19
1 files changed, 18 insertions, 1 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 6804a65f7..a7c9244cc 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -1512,9 +1512,26 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
     dec c.inTypeContext
 
 proc setMagicType(m: PSym, kind: TTypeKind, size: int) =
+  # source : https://en.wikipedia.org/wiki/Data_structure_alignment#x86
   m.typ.kind = kind
-  m.typ.align = size.int16
   m.typ.size = size
+  # this usually works for most basic types
+  # Assuming that since ARM, ARM64  don't support unaligned access
+  # data is aligned to type size
+  m.typ.align = size.int16
+
+  # FIXME: proper support for clongdouble should be added.
+  # long double size can be 8, 10, 12, 16 bytes depending on platform & compiler
+  if targetCPU == cpuI386 and size == 8:
+    #on Linux/BSD i386, double are aligned to 4bytes (except with -malign-double)
+    if kind in {tyFloat64, tyFloat} and
+       targetOS in {osLinux, osAndroid, osNetbsd, osFreebsd, osOpenbsd, osDragonfly}:
+      m.typ.align = 4
+    # on i386, all known compiler, 64bits ints are aligned to 4bytes (except with -malign-double)
+    elif kind in {tyInt, tyUInt, tyInt64, tyUInt64}:
+      m.typ.align = 4
+  else:
+    discard
 
 proc processMagicType(c: PContext, m: PSym) =
   case m.magic