From 837b77b0a1383379ea052ff8e763d634d6a4549f Mon Sep 17 00:00:00 2001 From: Parashurama Date: Sat, 19 Aug 2017 09:02:02 +0200 Subject: [PATCH] fixes basic type alignement issue on i386 platform. (#6209) --- compiler/semtypes.nim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 6804a65f78..a7c9244cc1 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