llvm.org GIT mirror llvm / 1940dd1
Add the new union arthmetic instructions to llvm-c and ocaml. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97371 91177308-0d34-0410-b5e6-96231b3b80d8 Erick Tryzelaar 9 years ago
6 changed file(s) with 61 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
3434 | Opaque
3535 | Vector
3636 | Metadata
37 | Union
3738 end
3839
3940 module Linkage = struct
197198 external struct_type : llcontext -> lltype array -> lltype = "llvm_struct_type"
198199 external packed_struct_type : llcontext -> lltype array -> lltype
199200 = "llvm_packed_struct_type"
200 external element_types : lltype -> lltype array = "llvm_element_types"
201 external struct_element_types : lltype -> lltype array
202 = "llvm_struct_element_types"
201203 external is_packed : lltype -> bool = "llvm_is_packed"
204
205 (*--... Operations on union types ..........................................--*)
206 external union_type : llcontext -> lltype array -> lltype = "llvm_union_type"
207 external union_element_types : lltype -> lltype array
208 = "llvm_union_element_types"
202209
203210 (*--... Operations on pointer, vector, and array types .....................--*)
204211 external array_type : lltype -> int -> lltype = "llvm_array_type"
256263 external const_packed_struct : llcontext -> llvalue array -> llvalue
257264 = "llvm_const_packed_struct"
258265 external const_vector : llvalue array -> llvalue = "llvm_const_vector"
266 external const_union : lltype -> llvalue -> llvalue = "LLVMConstUnion"
259267
260268 (*--... Constant expressions ...............................................--*)
261269 external align_of : lltype -> llvalue = "LLVMAlignOf"
891899 | TypeKind.Pointer -> (string_of_lltype (element_type ty)) ^ "*"
892900 | TypeKind.Struct ->
893901 let s = "{ " ^ (concat2 ", " (
894 Array.map string_of_lltype (element_types ty)
902 Array.map string_of_lltype (struct_element_types ty)
895903 )) ^ " }" in
896904 if is_packed ty
897905 then "<" ^ s ^ ">"
898906 else s
907 | TypeKind.Union -> "union { " ^ (concat2 ", " (
908 Array.map string_of_lltype (union_element_types ty)
909 )) ^ " }"
899910 | TypeKind.Array -> "[" ^ (string_of_int (array_length ty)) ^
900911 " x " ^ (string_of_lltype (element_type ty)) ^ "]"
901912 | TypeKind.Vector -> "<" ^ (string_of_int (vector_size ty)) ^
7272 | Opaque
7373 | Vector
7474 | Metadata
75 | Union
7576 end
7677
7778 (** The linkage of a global value, accessed with {!linkage} and
380381 external packed_struct_type : llcontext -> lltype array -> lltype
381382 = "llvm_packed_struct_type"
382383
383 (** [element_types sty] returns the constituent types of the struct type [sty].
384 See the method [llvm::StructType::getElementType]. *)
385 external element_types : lltype -> lltype array = "llvm_element_types"
384 (** [struct_element_types sty] returns the constituent types of the struct type
385 [sty]. See the method [llvm::StructType::getElementType]. *)
386 external struct_element_types : lltype -> lltype array
387 = "llvm_struct_element_types"
386388
387389 (** [is_packed sty] returns [true] if the structure type [sty] is packed,
388390 [false] otherwise. See the method [llvm::StructType::isPacked]. *)
389391 external is_packed : lltype -> bool = "llvm_is_packed"
392
393
394 (** {7 Operations on union types} *)
395
396 (** [union_type context tys] returns the union type in the context [context]
397 containing the types in the array [tys]. See the method
398 [llvm::UnionType::get] *)
399 external union_type : llcontext -> lltype array -> lltype = "llvm_union_type"
400
401 (** [union_element_types uty] returns the constituent types of the union type
402 [uty]. See the method [llvm::UnionType::getElementType]. *)
403 external union_element_types : lltype -> lltype array
404 = "llvm_union_element_types"
390405
391406
392407 (** {7 Operations on pointer, vector, and array types} *)
575590 [vector_type (type_of elts.(0)) (Array.length elts)] and containing the
576591 values [elts]. See the method [llvm::ConstantVector::get]. *)
577592 external const_vector : llvalue array -> llvalue = "llvm_const_vector"
593
594 (** [const_union ty v] returns the union constant of type [union_type tys] and
595 containing the value [v]. See the method [llvm::ConstantUnion::get]. *)
596 external const_union : lltype -> llvalue -> llvalue = "LLVMConstUnion"
578597
579598
580599 (** {7 Constant expressions} *)
282282 }
283283
284284 /* lltype -> lltype array */
285 CAMLprim value llvm_element_types(LLVMTypeRef StructTy) {
285 CAMLprim value llvm_struct_element_types(LLVMTypeRef StructTy) {
286286 value Tys = alloc(LLVMCountStructElementTypes(StructTy), 0);
287287 LLVMGetStructElementTypes(StructTy, (LLVMTypeRef *) Tys);
288288 return Tys;
291291 /* lltype -> bool */
292292 CAMLprim value llvm_is_packed(LLVMTypeRef StructTy) {
293293 return Val_bool(LLVMIsPackedStruct(StructTy));
294 }
295
296 /*--... Operations on union types ..........................................--*/
297
298 /* llcontext -> lltype array -> lltype */
299 CAMLprim LLVMTypeRef llvm_union_type(LLVMContextRef C, value ElementTypes) {
300 return LLVMUnionTypeInContext(C, (LLVMTypeRef *) ElementTypes,
301 Wosize_val(ElementTypes));
302 }
303
304 /* lltype -> lltype array */
305 CAMLprim value llvm_union_element_types(LLVMTypeRef UnionTy) {
306 value Tys = alloc(LLVMCountUnionElementTypes(UnionTy), 0);
307 LLVMGetUnionElementTypes(UnionTy, (LLVMTypeRef *) Tys);
308 return Tys;
294309 }
295310
296311 /*--... Operations on array, pointer, and vector types .....................--*/
550550 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
551551 LLVMBool Packed);
552552 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
553 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val);
553554
554555 /* Constant expressions */
555556 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal);
566566 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
567567 Packed);
568568 }
569
570569 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
571570 return wrap(ConstantVector::get(
572571 unwrap(ScalarConstantVals, Size), Size));
572 }
573 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
574 return wrap(ConstantUnion::get(unwrap(Ty), unwrap(Val)));
573575 }
574576
575577 /*--.. Constant expressions ................................................--*/
287287 ignore (define_global "const_structure" c m);
288288 insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |])
289289 = (type_of c));
290
291 group "union";
292 let t = union_type context [| i1_type; i16_type; i64_type; double_type |] in
293 let c = const_union t one in
294 ignore (define_global "Const_union" c m);
295 insist (t = (type_of c));
290296
291297 (* RUN: grep {const_null.*zeroinit} < %t.ll
292298 *)