Skip to contentSkip to conversation

sparse, llvm: Add support for union types

PATCH New Pekka Enberg 1 participant 1 message Oct 30, 2011 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparse, llvm: Add support for union types

This patch adds support for SYM_UNION in symbol_type(). The LLVM API does not provide support for unions so we treat them as opaque structs.

Cc Christopher Li <[email protected]>
Cc Jeff Garzik <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 sparse-llvm.c              |   20 ++++++++++++++++++++
 validation/backend/union.c |   12 ++++++++++++
 2 files changed, 32 insertions(+), 0 deletions(-)
 create mode 100644 validation/backend/union.c
sparse-llvm.c +20 -0
index a678d9c..7f46c8a 100644
@@ -80,6 +80,23 @@ static LLVMTypeRef sym_struct_type(LLVMModuleRef module, struct symbol *sym)
 	return ret;
 }
 
+static LLVMTypeRef sym_union_type(LLVMModuleRef module, struct symbol *sym)
+{
+	LLVMTypeRef elements;
+	unsigned union_size;
+
+	/*
+	 * There's no union support in the LLVM API so we treat unions as
+	 * opaque structs. The downside is that we lose type information on the
+	 * members but as LLVM doesn't care, neither do we.
+	 */
+	union_size = sym->bit_size / 8;
+
+	elements = LLVMArrayType(LLVMInt8Type(), union_size);
+
+	return LLVMStructType(&elements, 1, 0 /* packed? */);
+}
+
 static LLVMTypeRef sym_ptr_type(LLVMModuleRef module, struct symbol *sym)
 {
 	LLVMTypeRef type = symbol_type(module, sym->ctype.base_type);
@@ -146,6 +163,9 @@ static LLVMTypeRef symbol_type(LLVMModuleRef module, struct symbol *sym)
 	case SYM_PTR:
 		ret = sym_ptr_type(module, sym);
 		break;
+	case SYM_UNION:
+		ret = sym_union_type(module, sym);
+		break;
 	case SYM_STRUCT:
 		ret = sym_struct_type(module, sym);
 		break;
validation/backend/union.c new file +12 -0
new file mode 100644
index 0000000..e155f6a
@@ -0,0 +1,12 @@
+union foo {
+	unsigned long		x;
+	unsigned char		y;
+	char			buf[128];
+};
+
+static union foo foo;
+
+/*
+ * check-name: Union code generation
+ * check-command: ./sparsec -c $file -o tmp.o
+ */
Signature · 1 line
1.7.6.4