From f56fd6ffb6bac7af3cf68c467b6efd3ac85d9068 Mon Sep 17 00:00:00 2001
From: Bill Currie <bill@taniwha.org>
Date: Sun, 2 Jan 2022 11:42:27 +0900
Subject: [PATCH] [qfcc] Preserve requested alignment if larger

build_struct was unconditionally setting the type's alignment. This was
not a problem before because no types were requesting alignments larger
than those requested by their members (for structs). However, with the
upcoming new instruction set, quaternions need to be 4-word aligned.
---
 tools/qfcc/source/struct.c | 4 +++-
 tools/qfcc/source/type.c   | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/qfcc/source/struct.c b/tools/qfcc/source/struct.c
index ef9bd7389..4bd82130c 100644
--- a/tools/qfcc/source/struct.c
+++ b/tools/qfcc/source/struct.c
@@ -171,7 +171,9 @@ build_struct (int su, symbol_t *tag, symtab_t *symtab, type_t *type)
 	if (!type)
 		sym->type = find_type (sym->type);	// checks the tag, not the symtab
 	sym->type->t.symtab = symtab;
-	sym->type->alignment = alignment;
+	if (alignment > sym->type->alignment) {
+		sym->type->alignment = alignment;
+	}
 	if (!type && sym->type->type_def->external)	//FIXME should not be necessary
 		sym->type->type_def = qfo_encode_type (sym->type, pr.type_data);
 	return sym;
diff --git a/tools/qfcc/source/type.c b/tools/qfcc/source/type.c
index 3c6a4a683..bccf431b7 100644
--- a/tools/qfcc/source/type.c
+++ b/tools/qfcc/source/type.c
@@ -74,7 +74,7 @@ type_t      type_function = { ev_func, "function", 1, ty_basic,
 								{{&type_void}} };
 type_t      type_pointer = { ev_pointer, "pointer", 1, ty_basic,
 								{{&type_void}} };
-type_t      type_quaternion = { ev_quat, "quaternion", 1 };
+type_t      type_quaternion = { ev_quat, "quaternion", 4 };
 type_t      type_integer = { ev_integer, "int", 1 };
 type_t      type_uinteger = { ev_uinteger, "uint", 1 };
 type_t      type_short = { ev_short, "short", 1 };