From 52d618a99a56cf536a6ec70e7aea7824533d1edd Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 23 Mar 2012 12:51:20 +0100 Subject: [PATCH] Revert removal of intrinsics Oops. We can't do this yet until the next snapshot. --- Makefile.in | 1 + mk/clean.mk | 2 + mk/install.mk | 1 + mk/target.mk | 11 + src/etc/gen-intrinsics | 27 +++ src/rt/intrinsics/intrinsics.cpp | 174 ++++++++++++++++ src/rt/intrinsics/intrinsics.i386.ll.in | 236 +++++++++++++++++++++ src/rt/intrinsics/intrinsics.ll.bak | 149 ++++++++++++++ src/rt/intrinsics/intrinsics.x86_64.ll.in | 237 ++++++++++++++++++++++ src/rustc/back/link.rs | 64 ++++++ src/rustc/back/upcall.rs | 6 + src/rustc/front/attr.rs | 5 +- src/rustc/metadata/common.rs | 1 + src/rustc/metadata/csearch.rs | 6 + src/rustc/metadata/decoder.rs | 8 + src/rustc/metadata/encoder.rs | 6 +- src/rustc/middle/ast_map.rs | 2 +- src/rustc/middle/lint.rs | 2 +- src/rustc/middle/trans/base.rs | 71 +++++-- src/rustc/middle/trans/closure.rs | 1 + src/rustc/middle/trans/impl.rs | 2 +- src/rustc/middle/trans/native.rs | 39 +++- src/rustc/middle/trans/type_use.rs | 6 + src/rustc/middle/typeck.rs | 12 +- src/rustc/syntax/ast.rs | 1 + 25 files changed, 1038 insertions(+), 32 deletions(-) create mode 100755 src/etc/gen-intrinsics create mode 100644 src/rt/intrinsics/intrinsics.cpp create mode 100644 src/rt/intrinsics/intrinsics.i386.ll.in create mode 100644 src/rt/intrinsics/intrinsics.ll.bak create mode 100644 src/rt/intrinsics/intrinsics.x86_64.ll.in diff --git a/Makefile.in b/Makefile.in index 71aca347d68..075e15320d5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -317,6 +317,7 @@ HSREQ$(1)_H_$(3) = \ TSREQ$(1)_T_$(2)_H_$(3) = \ $$(HSREQ$(1)_H_$(3)) \ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUNTIME) \ + $$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.bc \ $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a # Prerequisites for complete stageN targets diff --git a/mk/clean.mk b/mk/clean.mk index 1971af04481..80337c1d6e3 100644 --- a/mk/clean.mk +++ b/mk/clean.mk @@ -99,6 +99,8 @@ clean$(1)_T_$(2)_H_$(3): $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/$(LIBRUSTC_GLOB) $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/$(CFG_RUSTLLVM) $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libstd.rlib + $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.bc + $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll $(Q)rm -f $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a endef diff --git a/mk/install.mk b/mk/install.mk index 605639c8be1..acd454d1ff4 100644 --- a/mk/install.mk +++ b/mk/install.mk @@ -42,6 +42,7 @@ install-target-$(1)-host-$(2): $$(SREQ$$(ISTAGE)_T_$(1)_H_$(2)) $$(TL$(1)$(2)),$$(PTL$(1)$(2)),$$(STDLIB_GLOB)) $$(Q)$$(call INSTALL_LIB, \ $$(TL$(1)$(2)),$$(PTL$(1)$(2)),$$(LIBRUSTC_GLOB)) + $$(Q)$$(call INSTALL,$$(TL$(1)$(2)),$$(PTL$(1)$(2)),intrinsics.bc) $$(Q)$$(call INSTALL,$$(TL$(1)$(2)),$$(PTL$(1)$(2)),libmorestack.a) endef diff --git a/mk/target.mk b/mk/target.mk index 0f02c153cac..7b3dcab0abd 100644 --- a/mk/target.mk +++ b/mk/target.mk @@ -13,6 +13,17 @@ USE_SNAPSHOT_CORELIB=0 define TARGET_STAGE_N +$$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll: \ + $$(S)src/rt/intrinsics/intrinsics.$(HOST_$(2)).ll.in + @$$(call E, sed: $$@) + $$(Q)sed s/@CFG_TARGET_TRIPLE@/$(2)/ $$< > $$@ + +$$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.bc: \ + $$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll \ + $$(LLVM_CONFIG_$(2)) + @$$(call E, llvms-as: $$@) + $$(Q)$$(LLVM_AS_$(2)) -o $$@ $$< + $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a: \ rt/$(2)/arch/$$(HOST_$(2))/libmorestack.a @$$(call E, cp: $$@) diff --git a/src/etc/gen-intrinsics b/src/etc/gen-intrinsics new file mode 100755 index 00000000000..72785bfbbf6 --- /dev/null +++ b/src/etc/gen-intrinsics @@ -0,0 +1,27 @@ +#!/bin/sh + +# This script generates new definitions for the intrinsics using +# clang. This is not currently in the Makefile to avoid any dependency +# on clang. + +for ARCH in i386 x86_64 +do + if [ $ARCH = "i386" ] + then + BITS=32 + else + BITS=64 + fi + + clang++ -emit-llvm -S -m$BITS -O3 -Isrc/rt/isaac -Isrc/rt/uthash \ + -Isrc/rt/arch/$ARCH -Isrc/rt -fno-stack-protector \ + -o src/rt/intrinsics/intrinsics.$ARCH.ll.in \ + src/rt/intrinsics/intrinsics.cpp + sed -i .orig \ + -e 's/^target datalayout =/; target datalayout =/' \ + src/rt/intrinsics/intrinsics.$ARCH.ll.in + sed -i .orig \ + -e 's/^target triple = "[^"]*"/target triple = "@CFG_TARGET_TRIPLE@"/' \ + src/rt/intrinsics/intrinsics.$ARCH.ll.in + rm src/rt/intrinsics/intrinsics.$ARCH.ll.in.orig +done diff --git a/src/rt/intrinsics/intrinsics.cpp b/src/rt/intrinsics/intrinsics.cpp new file mode 100644 index 00000000000..d36bdf87675 --- /dev/null +++ b/src/rt/intrinsics/intrinsics.cpp @@ -0,0 +1,174 @@ +// Rust intrinsics. These are built into each compilation unit and are +// run on the Rust stack. They should not call C methods because that +// will very likely result in running off the end of the stack. +// Build with the script in src/etc/gen-intrinsics + +#include "../rust_internal.h" +#include "../rust_util.h" +#include +#include + +extern "C" CDECL void +rust_task_yield(rust_task *task, bool *killed); + +extern "C" void +rust_intrinsic_vec_len(size_t *retptr, + void *env, + type_desc *ty, + rust_vec **vp) +{ + *retptr = (*vp)->fill / ty->size; +} + +extern "C" void +rust_intrinsic_ptr_offset(void **retptr, + void *env, + type_desc *ty, + void *ptr, + uintptr_t count) +{ + *retptr = &((uint8_t *)ptr)[ty->size * count]; +} + +extern "C" void +rust_intrinsic_cast(void *retptr, + void *env, + type_desc *t1, + type_desc *t2, + void *src) +{ + // assert t1->size == t2->size + // FIXME: This should be easily expressible in rust + memmove(retptr, src, t1->size); +} + +extern "C" void +rust_intrinsic_addr_of(void **retptr, + void *env, + type_desc *ty, + void *valptr) { + *retptr = valptr; +} + +struct rust_fn { + uintptr_t *fn; + rust_box *env; +}; + +typedef void (*retptr_fn)(void **retptr, + void *env, + void **dptr); +// FIXME (1185): This exists just to get access to the return pointer +extern "C" void +rust_intrinsic_call_with_retptr(void **retptr, + void *env, + type_desc *ty, + rust_fn *recvfn) { + retptr_fn fn = ((retptr_fn)(recvfn->fn)); + ((retptr_fn)(*fn))(NULL, recvfn->env, retptr); +} + +extern "C" void +rust_intrinsic_get_type_desc(void **retptr, + void *env, + type_desc* ty) { + *(type_desc**)retptr = ty; +} + +extern "C" void +rust_intrinsic_task_yield(void **retptr, + void *env, + rust_task *task, + bool *killed) { + rust_task_yield(task, killed); +} + +extern "C" void +rust_intrinsic_memmove(void *retptr, + void *env, + type_desc *ty, + void *dst, + void *src, + uintptr_t count) +{ + memmove(dst, src, ty->size * count); +} + +extern "C" void +rust_intrinsic_memcpy(void *retptr, + void *env, + type_desc *ty, + void *dst, + void *src, + uintptr_t count) +{ + memcpy(dst, src, ty->size * count); +} + +extern "C" void +rust_intrinsic_leak(void *retptr, + void *env, + type_desc *ty, + void *thing) +{ +} + +extern "C" CDECL void * +upcall_shared_realloc(void *ptr, size_t size); + +inline void reserve_vec_fast(rust_vec **vpp, size_t size) { + if (size > (*vpp)->alloc) { + size_t new_size = next_power_of_two(size); + size_t alloc_size = new_size + sizeof(rust_vec); + // Because this is called from an intrinsic we need to use + // the exported API + *vpp = (rust_vec*)upcall_shared_realloc(*vpp, alloc_size); + (*vpp)->alloc = new_size; + } +} + +// Copy elements from one vector to another, +// dealing with reference counts +static inline void +copy_elements(type_desc *elem_t, + void *pdst, void *psrc, size_t n) { + char *dst = (char *)pdst, *src = (char *)psrc; + memmove(dst, src, n); + + // increment the refcount of each element of the vector + if (elem_t->take_glue) { + glue_fn *take_glue = elem_t->take_glue; + size_t elem_size = elem_t->size; + const type_desc **tydescs = elem_t->first_param; + for (char *p = dst; p < dst+n; p += elem_size) { + take_glue(NULL, NULL, tydescs, p); + } + } +} + +// Because this is used so often, and it calls take glue that must run +// on the rust stack, it is statically compiled into every crate. +extern "C" CDECL void +upcall_intrinsic_vec_push(rust_vec** vp, + type_desc* elt_ty, void* elt) { + + size_t new_sz = (*vp)->fill + elt_ty->size; + reserve_vec_fast(vp, new_sz); + rust_vec* v = *vp; + copy_elements(elt_ty, &v->data[0] + v->fill, + elt, elt_ty->size); + v->fill += elt_ty->size; +} + +// FIXME: Transational. Remove +extern "C" CDECL void +upcall_vec_push(rust_vec** vp, + type_desc* elt_ty, void* elt) { + upcall_intrinsic_vec_push(vp, elt_ty, elt); +} + +extern "C" CDECL void +rust_intrinsic_frame_address(void **p, unsigned n) { + *p = __builtin_frame_address(n); +} + diff --git a/src/rt/intrinsics/intrinsics.i386.ll.in b/src/rt/intrinsics/intrinsics.i386.ll.in new file mode 100644 index 00000000000..a03bed03118 --- /dev/null +++ b/src/rt/intrinsics/intrinsics.i386.ll.in @@ -0,0 +1,236 @@ +; ModuleID = 'src/rt/intrinsics/intrinsics.cpp' +; target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32" +target triple = "@CFG_TARGET_TRIPLE@" + +%0 = type { i32, %struct.rust_task**, i32 } +%1 = type { %"struct.hash_map::map_entry"* } +%class.array_list = type { i32, %"struct.memory_region::alloc_header"**, i32 } +%class.boxed_region = type { %class.memory_region*, %struct.rust_opaque_box* } +%class.circular_buffer = type { %class.rust_kernel*, i32, i32, i32, i32, i8* } +%class.context = type { %struct.registers_t, %class.context*, [12 x i8] } +%"class.debug::task_debug_info" = type { %"class.std::map" } +%class.hash_map = type { %"struct.hash_map::map_entry"* } +%class.indexed_list = type { i32 (...)**, %0 } +%class.lock_and_signal = type { i32 (...)**, %struct._opaque_pthread_cond_t, %struct._opaque_pthread_mutex_t, %struct._opaque_pthread_t* } +%class.memory_region = type { i32 (...)**, %class.rust_srv*, %class.memory_region*, i32, %class.array_list, i8, i8, %class.lock_and_signal } +%class.rust_crate_cache = type { %struct.type_desc*, %struct.rust_hashable_dict*, %struct.rust_task_thread*, i32 } +%class.rust_kernel = type { %class.memory_region, %class.rust_log, %class.rust_srv*, %class.lock_and_signal, i32, i32, %1, %class.lock_and_signal, i32, %class.lock_and_signal, i32, %"class.std::map", %"class.std::vector", %struct.rust_env* } +%class.rust_log = type { i32 (...)**, %class.rust_srv*, %struct.rust_task_thread*, i8 } +%class.rust_obstack = type { %struct.rust_obstack_chunk*, %struct.rust_task* } +%class.rust_port = type { i32, i32, %class.rust_kernel*, %struct.rust_task*, i32, %class.circular_buffer, %class.lock_and_signal } +%class.rust_port_selector = type { %class.rust_port**, i32, %class.lock_and_signal } +%class.rust_scheduler = type opaque +%class.rust_srv = type { i32 (...)**, %struct.rust_env*, %class.memory_region } +%class.rust_task_list = type { %class.indexed_list, %struct.rust_task_thread*, i8* } +%class.rust_thread = type { i32 (...)**, %struct._opaque_pthread_t*, i32 } +%"class.std::_Rb_tree" = type { %"struct.std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::_Rb_tree_impl" } +%"class.std::map" = type { %"class.std::_Rb_tree" } +%"class.std::vector" = type { %"struct.std::_Vector_base" } +%struct.UT_hash_bucket = type { %struct.UT_hash_handle*, i32, i32 } +%struct.UT_hash_handle = type { %struct.UT_hash_table*, i8*, i8*, %struct.UT_hash_handle*, %struct.UT_hash_handle*, i8*, i32, i32 } +%struct.UT_hash_table = type { %struct.UT_hash_bucket*, i32, i32, i32, %struct.UT_hash_handle*, i32, i32, i32, i32, i32 } +%struct.__darwin_pthread_handler_rec = type { void (i8*)*, i8*, %struct.__darwin_pthread_handler_rec* } +%struct._opaque_pthread_attr_t = type { i32, [36 x i8] } +%struct._opaque_pthread_cond_t = type { i32, [24 x i8] } +%struct._opaque_pthread_mutex_t = type { i32, [40 x i8] } +%struct._opaque_pthread_t = type { i32, %struct.__darwin_pthread_handler_rec*, [596 x i8] } +%struct.chan_handle = type { i32, i32 } +%"struct.hash_map::map_entry" = type opaque +%"struct.hash_map::map_entry" = type opaque +%"struct.memory_region::alloc_header" = type { i8 } +%struct.randctx = type { i32, [256 x i32], [256 x i32], i32, i32, i32 } +%struct.registers_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, i16, i32, i32, [12 x i8] } +%struct.rust_box = type opaque +%struct.rust_env = type { i32, i32, i32, i8*, i8, i8, i8* } +%struct.rust_fn = type { i32*, %struct.rust_box* } +%struct.rust_hashable_dict = type { %struct.UT_hash_handle, [0 x i8*] } +%struct.rust_obstack_chunk = type { %struct.rust_obstack_chunk*, i32, i32, i32, [0 x i8] } +%struct.rust_opaque_box = type { i32, %struct.type_desc*, %struct.rust_opaque_box*, %struct.rust_opaque_box* } +%struct.rust_shape_tables = type { i8*, i8* } +%struct.rust_task = type { i32, i32, i8, %struct.chan_handle, [12 x i8], %class.context, %struct.stk_seg*, i32, %class.rust_scheduler*, %struct.rust_task_thread*, %class.rust_crate_cache*, %class.rust_kernel*, i8*, %class.rust_task_list*, %"struct.memory_region::alloc_header"*, i8*, %struct.rust_task*, i32, i32, i32*, %class.memory_region, %class.boxed_region, i8, i8, %class.lock_and_signal, %class.hash_map, %class.rust_obstack, i32, %"class.debug::task_debug_info", i32, i8, i8, %struct.stk_seg*, i32, i32, %class.rust_port_selector, [8 x i8] } +%struct.rust_task_thread = type { %class.rust_thread, i32, %class.rust_log, i32, %class.rust_srv*, i8*, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_crate_cache, %struct.randctx, %class.rust_kernel*, %class.rust_scheduler*, i32, i32, %class.lock_and_signal, i32, %struct._opaque_pthread_attr_t, %struct.rust_env*, [4 x i8], %class.context, i8, %struct.stk_seg*, %struct.stk_seg*, [4 x i8] } +%struct.rust_vec = type { i32, i32, [0 x i8] } +%"struct.std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::_Rb_tree_impl" = type { %"struct.memory_region::alloc_header", %"struct.std::_Rb_tree_node_base", i32 } +%"struct.std::_Rb_tree_node_base" = type { i32, %"struct.std::_Rb_tree_node_base"*, %"struct.std::_Rb_tree_node_base"*, %"struct.std::_Rb_tree_node_base"* } +%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base >::_Vector_impl" } +%"struct.std::_Vector_base >::_Vector_impl" = type { i32*, i32*, i32* } +%struct.stk_seg = type { %struct.stk_seg*, %struct.stk_seg*, i32, i32, i32, i32, [0 x i8] } +%struct.type_desc = type { %struct.type_desc**, i32, i32, void (i8*, i8*, %struct.type_desc**, i8*)*, void (i8*, i8*, %struct.type_desc**, i8*)*, void (i8*, i8*, %struct.type_desc**, i8*)*, i8*, void (i8*, i8*, %struct.type_desc**, i8*)*, void (i8*, i8*, %struct.type_desc**, i8*)*, i32, i8*, i8*, %struct.rust_shape_tables*, i32, i32, %struct.UT_hash_handle, i32, [0 x %struct.type_desc*] } + +define void @rust_intrinsic_vec_len(i32* nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, %struct.rust_vec** nocapture %vp) nounwind { + %1 = load %struct.rust_vec** %vp, align 4 + %2 = getelementptr inbounds %struct.rust_vec* %1, i32 0, i32 0 + %3 = load i32* %2, align 4 + %4 = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1 + %5 = load i32* %4, align 4 + %6 = udiv i32 %3, %5 + store i32 %6, i32* %retptr, align 4 + ret void +} + +define void @rust_intrinsic_ptr_offset(i8** nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, i8* %ptr, i32 %count) nounwind { + %1 = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1 + %2 = load i32* %1, align 4 + %3 = mul i32 %2, %count + %4 = getelementptr inbounds i8* %ptr, i32 %3 + store i8* %4, i8** %retptr, align 4 + ret void +} + +define void @rust_intrinsic_cast(i8* nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %t1, %struct.type_desc* nocapture %t2, i8* nocapture %src) nounwind { + %1 = getelementptr inbounds %struct.type_desc* %t1, i32 0, i32 1 + %2 = load i32* %1, align 4 + tail call void @llvm.memmove.p0i8.p0i8.i32(i8* %retptr, i8* %src, i32 %2, i32 1, i1 false) + ret void +} + +declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind + +define void @rust_intrinsic_addr_of(i8** nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, i8* %valptr) nounwind { + store i8* %valptr, i8** %retptr, align 4 + ret void +} + +define void @rust_intrinsic_call_with_retptr(i8** %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, %struct.rust_fn* nocapture %recvfn) { + %1 = getelementptr inbounds %struct.rust_fn* %recvfn, i32 0, i32 0 + %2 = load i32** %1, align 4 + %3 = bitcast i32* %2 to void (i8**, i8*, i8**)* + %4 = getelementptr inbounds %struct.rust_fn* %recvfn, i32 0, i32 1 + %5 = load %struct.rust_box** %4, align 4 + %6 = bitcast %struct.rust_box* %5 to i8* + tail call void %3(i8** null, i8* %6, i8** %retptr) + ret void +} + +define void @rust_intrinsic_get_type_desc(i8** nocapture %retptr, i8* nocapture %env, %struct.type_desc* %ty) nounwind { + %ty.c = bitcast %struct.type_desc* %ty to i8* + store i8* %ty.c, i8** %retptr, align 4 + ret void +} + +define void @rust_intrinsic_task_yield(i8** nocapture %retptr, i8* nocapture %env, %struct.rust_task* %task, i8* %killed) { + tail call void @rust_task_yield(%struct.rust_task* %task, i8* %killed) + ret void +} + +declare void @rust_task_yield(%struct.rust_task*, i8*) + +define void @rust_intrinsic_memmove(i8* nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, i8* nocapture %dst, i8* nocapture %src, i32 %count) nounwind { + %1 = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1 + %2 = load i32* %1, align 4 + %3 = mul i32 %2, %count + tail call void @llvm.memmove.p0i8.p0i8.i32(i8* %dst, i8* %src, i32 %3, i32 1, i1 false) + ret void +} + +define void @rust_intrinsic_memcpy(i8* nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, i8* nocapture %dst, i8* nocapture %src, i32 %count) nounwind { + %1 = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1 + %2 = load i32* %1, align 4 + %3 = mul i32 %2, %count + tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dst, i8* %src, i32 %3, i32 1, i1 false) + ret void +} + +declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind + +define void @rust_intrinsic_leak(i8* nocapture %retptr, i8* nocapture %env, %struct.type_desc* nocapture %ty, i8* nocapture %thing) nounwind readnone { + ret void +} + +define void @upcall_intrinsic_vec_push(%struct.rust_vec** nocapture %vp, %struct.type_desc* nocapture %elt_ty, i8* nocapture %elt) { +;