From 22bef74b550b2b08338ebb43363f3d09df6413b9 Mon Sep 17 00:00:00 2001
From: Marijn Haverbeke <marijnh@gmail.com>
Date: Thu, 15 Mar 2012 17:42:33 +0100
Subject: [PATCH] Remove shared tydescs

All tydescs are static now, there's no need to worry about
marshalling them between threads anymore.
---
 mk/target.mk                      |  2 +-
 src/rt/rust_cc.cpp                |  9 ----
 src/rt/rust_task.cpp              |  1 -
 src/rt/rust_upcall.cpp            | 73 -------------------------------
 src/rt/rust_upcall.h              |  1 -
 src/rt/rustrt.def.in              |  2 -
 src/rustc/back/upcall.rs          |  7 ---
 src/rustc/metadata/tydecode.rs    |  1 -
 src/rustc/metadata/tyencode.rs    |  1 -
 src/rustc/middle/trans/base.rs    | 18 +-------
 src/rustc/middle/trans/closure.rs | 39 ++++-------------
 src/rustc/middle/trans/shape.rs   |  1 -
 src/rustc/middle/trans/type_of.rs |  2 +-
 src/rustc/middle/ty.rs            | 19 +++-----
 14 files changed, 18 insertions(+), 158 deletions(-)

diff --git a/mk/target.mk b/mk/target.mk
index 34470e99f64..3fe36852353 100644
--- a/mk/target.mk
+++ b/mk/target.mk
@@ -8,7 +8,7 @@
 # (resp.  corelib), set this flag to 1.  It will cause stage1 to use
 # the snapshot runtime (resp. corelib) rather than the runtime
 # (resp. corelib) from the working directory.
-USE_SNAPSHOT_RUNTIME=0
+USE_SNAPSHOT_RUNTIME=1
 USE_SNAPSHOT_CORELIB=0
 
 # Do not use --enforce-mut-vars in stage0, for now, as the snapshot
diff --git a/src/rt/rust_cc.cpp b/src/rt/rust_cc.cpp
index 95208b311ee..de454fc2f84 100644
--- a/src/rt/rust_cc.cpp
+++ b/src/rt/rust_cc.cpp
@@ -18,9 +18,6 @@
 // collection.
 #define RUST_CC_FREQUENCY   5000
 
-// defined in rust_upcall.cpp:
-void upcall_s_free_shared_type_desc(type_desc *td);
-
 using namespace std;
 
 namespace cc {
@@ -534,9 +531,6 @@ class sweep : public shape::data<sweep,shape::ptr> {
                   // free closed over data:
                   shape::data<sweep,shape::ptr>::walk_fn_contents1();
                   
-                  // now free the embedded type descr:
-                  upcall_s_free_shared_type_desc((type_desc*)pair.env->td);
-                  
                   // now free the ptr:
                   task->kernel->free(pair.env);
               }
@@ -563,12 +557,9 @@ class sweep : public shape::data<sweep,shape::ptr> {
     }
 
     void walk_tydesc2(char kind) {
-        type_desc *td = *(type_desc **)dp;
         switch(kind) {
           case shape::SHAPE_TYDESC:
-            break;
           case shape::SHAPE_SEND_TYDESC:
-            upcall_s_free_shared_type_desc(td);
             break;
           default: abort();
         }
diff --git a/src/rt/rust_task.cpp b/src/rt/rust_task.cpp
index 32f1e0eb887..039989cbf31 100644
--- a/src/rt/rust_task.cpp
+++ b/src/rt/rust_task.cpp
@@ -192,7 +192,6 @@ void task_start_wrapper(spawn_args *a)
         // free the environment (which should be a unique closure).
         const type_desc *td = env->td;
         td->drop_glue(NULL, NULL, td->first_param, box_body(env));
-        upcall_free_shared_type_desc(env->td);
         upcall_shared_free(env);
     }
 
diff --git a/src/rt/rust_upcall.cpp b/src/rt/rust_upcall.cpp
index 28a8e0a4716..dfeeda34994 100644
--- a/src/rt/rust_upcall.cpp
+++ b/src/rt/rust_upcall.cpp
@@ -288,79 +288,6 @@ upcall_shared_realloc(void *ptr, size_t size) {
     return args.retval;
 }
 
-/**********************************************************************
- * Called to deep copy a type descriptor onto the exchange heap.
- * Used when sending closures.  It's possible that we should have
- * a central hashtable to avoid copying and re-copying the same 
- * type descriptors.
- */
-
-struct s_create_shared_type_desc_args {
-    const type_desc *td;
-    type_desc *res;
-};
-
-void upcall_s_create_shared_type_desc(s_create_shared_type_desc_args *args)
-{
-    rust_task *task = rust_task_thread::get_task();
-    LOG_UPCALL_ENTRY(task);
-
-    // Copy the main part of the type descriptor:
-    const type_desc *td = args->td;
-    int n_params = td->n_params;
-    size_t sz = sizeof(type_desc) + sizeof(type_desc*) * (n_params+1);
-    args->res = (type_desc*) task->kernel->malloc(sz, "create_shared_type_desc");
-    memcpy(args->res, td, sizeof(type_desc));
-
-    // Recursively copy any referenced descriptors:
-    if (n_params == 0) {
-        args->res->first_param = NULL;
-    } else {
-        args->res->first_param = &args->res->descs[1];
-        args->res->descs[0] = args->res;
-        for (int i = 0; i < n_params; i++) {
-            s_create_shared_type_desc_args rec_args = {
-                td->first_param[i], 0
-            };
-            upcall_s_create_shared_type_desc(&rec_args);
-            args->res->first_param[i] = rec_args.res;
-        }
-    }
-}
-
-extern "C" CDECL type_desc *
-upcall_create_shared_type_desc(type_desc *td) {
-    s_create_shared_type_desc_args args = { td, 0 };
-    UPCALL_SWITCH_STACK(&args, upcall_s_create_shared_type_desc);
-    return args.res;
-}
-
-/**********************************************************************
- * Called to deep free a type descriptor from the exchange heap.
- */
-
-void upcall_s_free_shared_type_desc(type_desc *td)
-{ // n.b.: invoked from rust_cc.cpp as well as generated code
-    rust_task *task = rust_task_thread::get_task();
-    LOG_UPCALL_ENTRY(task);
-
-    if (td) {
-        // Recursively free any referenced descriptors:
-        for (unsigned i = 0; i < td->n_params; i++) {
-            upcall_s_free_shared_type_desc((type_desc*) td->first_param[i]);
-        }
-
-        task->kernel->free(td);
-    }
-}
-
-extern "C" CDECL void
-upcall_free_shared_type_desc(type_desc *td) {
-    if (td) {
-        UPCALL_SWITCH_STACK(td, upcall_s_free_shared_type_desc);
-    }
-}
-
 /**********************************************************************/
 
 struct s_vec_grow_args {
diff --git a/src/rt/rust_upcall.h b/src/rt/rust_upcall.h
index 46676497d63..344730e2171 100644
--- a/src/rt/rust_upcall.h
+++ b/src/rt/rust_upcall.h
@@ -3,5 +3,4 @@
 // Upcalls used from C code on occasion:
 
 extern "C" CDECL void upcall_shared_free(void* ptr);
-extern "C" CDECL void upcall_free_shared_type_desc(type_desc *td);
 
diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in
index 67542eb93dd..4d2f7b99f72 100644
--- a/src/rt/rustrt.def.in
+++ b/src/rt/rustrt.def.in
@@ -59,8 +59,6 @@ upcall_cmp_type
 upcall_fail
 upcall_free
 upcall_validate_box
-upcall_create_shared_type_desc
-upcall_free_shared_type_desc
 upcall_log_type
 upcall_malloc
 upcall_rust_personality
diff --git a/src/rustc/back/upcall.rs b/src/rustc/back/upcall.rs
index 4a2f6bc2379..a0bb2c5937d 100644
--- a/src/rustc/back/upcall.rs
+++ b/src/rustc/back/upcall.rs
@@ -16,8 +16,6 @@ type upcalls =
      shared_free: ValueRef,
      shared_realloc: ValueRef,
      mark: ValueRef,
-     create_shared_type_desc: ValueRef,
-     free_shared_type_desc: ValueRef,
      vec_grow: ValueRef,
      vec_push: ValueRef,
      cmp_type: ValueRef,
@@ -65,11 +63,6 @@ fn declare_upcalls(targ_cfg: @session::config,
               d("shared_realloc", [T_ptr(T_i8()), size_t], T_ptr(T_i8())),
           mark:
               d("mark", [T_ptr(T_i8())], int_t),
-          create_shared_type_desc:
-              d("create_shared_type_desc", [T_ptr(tydesc_type)],
-                T_ptr(tydesc_type)),
-          free_shared_type_desc:
-              dv("free_shared_type_desc", [T_ptr(tydesc_type)]),
           vec_grow:
               dv("vec_grow", [T_ptr(T_ptr(opaque_vec_t)), int_t]),
           vec_push:
diff --git a/src/rustc/metadata/tydecode.rs b/src/rustc/metadata/tydecode.rs
index 37bb1e66f27..eebbe51c665 100644
--- a/src/rustc/metadata/tydecode.rs
+++ b/src/rustc/metadata/tydecode.rs
@@ -267,7 +267,6 @@ fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
       }
       'X' { ret ty::mk_var(st.tcx, parse_int(st)); }
       'Y' { ret ty::mk_type(st.tcx); }
-      'y' { ret ty::mk_send_type(st.tcx); }
       'C' {
         let ck = alt check next(st) {
           '&' { ty::ck_block }
diff --git a/src/rustc/metadata/tyencode.rs b/src/rustc/metadata/tyencode.rs
index 31270053078..ebe9342f87a 100644
--- a/src/rustc/metadata/tyencode.rs
+++ b/src/rustc/metadata/tyencode.rs
@@ -203,7 +203,6 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
         w.write_char(']');
       }
       ty::ty_type { w.write_char('Y'); }
-      ty::ty_send_type { w.write_char('y'); }
       ty::ty_opaque_closure_ptr(ty::ck_block) { w.write_str("C&"); }
       ty::ty_opaque_closure_ptr(ty::ck_box) { w.write_str("C@"); }
       ty::ty_opaque_closure_ptr(ty::ck_uniq) { w.write_str("C~"); }
diff --git a/src/rustc/middle/trans/base.rs b/src/rustc/middle/trans/base.rs
index 221a9537045..23fc3f69e96 100644
--- a/src/rustc/middle/trans/base.rs
+++ b/src/rustc/middle/trans/base.rs
@@ -559,14 +559,6 @@ fn make_take_glue(cx: block, v: ValueRef, t: ty::t) {
         Store(r.bcx, r.val, v);
         r.bcx
       }
-      ty::ty_send_type {
-        // sendable type descriptors are basically unique pointers,
-        // they must be cloned when copied:
-        let r = Load(bcx, v);
-        let s = Call(bcx, bcx.ccx().upcalls.create_shared_type_desc, [r]);
-        Store(bcx, s, v);
-        bcx
-      }
       ty::ty_fn(_) {
         closure::make_fn_glue(bcx, v, t, take_ty)
       }
@@ -623,14 +615,6 @@ fn make_free_glue(bcx: block, v: ValueRef, t: ty::t) {
       ty::ty_vec(_) | ty::ty_str {
         tvec::make_free_glue(bcx, PointerCast(bcx, v, type_of(ccx, t)), t)
       }
-      ty::ty_send_type {
-        // sendable type descriptors are basically unique pointers,
-        // they must be freed.
-        let ccx = bcx.ccx();
-        let v = PointerCast(bcx, v, T_ptr(ccx.tydesc_type));
-        Call(bcx, ccx.upcalls.free_shared_type_desc, [v]);
-        bcx
-      }
       ty::ty_fn(_) {
         closure::make_fn_glue(bcx, v, t, free_ty)
       }
@@ -649,7 +633,7 @@ fn make_drop_glue(bcx: block, v0: ValueRef, t: ty::t) {
       ty::ty_box(_) | ty::ty_opaque_box {
         decr_refcnt_maybe_free(bcx, Load(bcx, v0), t)
       }
-      ty::ty_uniq(_) | ty::ty_vec(_) | ty::ty_str | ty::ty_send_type {
+      ty::ty_uniq(_) | ty::ty_vec(_) | ty::ty_str {
         free_ty(bcx, Load(bcx, v0), t)
       }
       ty::ty_res(did, inner, tps) {
diff --git a/src/rustc/middle/trans/closure.rs b/src/rustc/middle/trans/closure.rs
index e2546717732..ad24195d901 100644
--- a/src/rustc/middle/trans/closure.rs
+++ b/src/rustc/middle/trans/closure.rs
@@ -114,16 +114,8 @@ fn ev_to_str(ccx: @crate_ctxt, ev: environment_value) -> str {
     }
 }
 
-fn mk_tydesc_ty(tcx: ty::ctxt, ck: ty::closure_kind) -> ty::t {
-    ret alt ck {
-      ty::ck_block | ty::ck_box { ty::mk_type(tcx) }
-      ty::ck_uniq { ty::mk_send_type(tcx) }
-    };
-}
-
 fn mk_tuplified_uniq_cbox_ty(tcx: ty::ctxt, cdata_ty: ty::t) -> ty::t {
-    let tydesc_ty = mk_tydesc_ty(tcx, ty::ck_uniq);
-    let cbox_ty = tuplify_cbox_ty(tcx, cdata_ty, tydesc_ty);
+    let cbox_ty = tuplify_cbox_ty(tcx, cdata_ty, ty::mk_type(tcx));
     ret ty::mk_imm_uniq(tcx, cbox_ty);
 }
 
@@ -166,14 +158,12 @@ fn allocate_cbox(bcx: block,
         Store(bcx, rc, ref_cnt);
     }
 
-    fn store_uniq_tydesc(bcx: block,
-                         cdata_ty: ty::t,
-                         box: ValueRef,
-                         &ti: option<@tydesc_info>) -> block {
-        let ccx = bcx.ccx();
+    fn store_tydesc(bcx: block,
+                    cdata_ty: ty::t,
+                    box: ValueRef,
+                    &ti: option<@tydesc_info>) -> block {
         let bound_tydesc = GEPi(bcx, box, [0, abi::box_field_tydesc]);
         let {bcx, val: td} = base::get_tydesc(bcx, cdata_ty, ti);
-        let td = Call(bcx, ccx.upcalls.create_shared_type_desc, [td]);
         Store(bcx, td, bound_tydesc);
         bcx
     }
@@ -190,7 +180,7 @@ fn allocate_cbox(bcx: block,
         let uniq_cbox_ty = mk_tuplified_uniq_cbox_ty(tcx, cdata_ty);
         let {bcx, val: box} = uniq::alloc_uniq(bcx, uniq_cbox_ty);
         nuke_ref_count(bcx, box);
-        let bcx = store_uniq_tydesc(bcx, cdata_ty, box, ti);
+        let bcx = store_tydesc(bcx, cdata_ty, box, ti);
         (bcx, box)
       }
       ty::ck_block {
@@ -222,19 +212,6 @@ fn store_environment(bcx: block,
                      bound_values: [environment_value],
                      ck: ty::closure_kind) -> closure_result {
 
-    fn maybe_clone_tydesc(bcx: block,
-                          ck: ty::closure_kind,
-                          td: ValueRef) -> ValueRef {
-        ret alt ck {
-          ty::ck_block | ty::ck_box {
-            td
-          }
-          ty::ck_uniq {
-            Call(bcx, bcx.ccx().upcalls.create_shared_type_desc, [td])
-          }
-        };
-    }
-
     let ccx = bcx.ccx(), tcx = ccx.tcx;
 
     // compute the shape of the closure
@@ -551,7 +528,7 @@ fn make_opaque_cbox_take_glue(
 
         // Take the (deeply cloned) type descriptor
         let tydesc_out = GEPi(bcx, cbox_out, [0, abi::box_field_tydesc]);
-        let bcx = take_ty(bcx, tydesc_out, mk_tydesc_ty(tcx, ty::ck_uniq));
+        let bcx = take_ty(bcx, tydesc_out, ty::mk_type(tcx));
 
         // Take the data in the tuple
         let ti = none;
@@ -612,7 +589,7 @@ fn make_opaque_cbox_free_glue(
             trans_free(bcx, cbox)
           }
           ty::ck_uniq {
-            let bcx = free_ty(bcx, tydesc, mk_tydesc_ty(tcx, ck));
+            let bcx = free_ty(bcx, tydesc, ty::mk_type(tcx));
             trans_shared_free(bcx, cbox)
           }
         }
diff --git a/src/rustc/middle/trans/shape.rs b/src/rustc/middle/trans/shape.rs
index f632cf625c6..88860e2aeab 100644
--- a/src/rustc/middle/trans/shape.rs
+++ b/src/rustc/middle/trans/shape.rs
@@ -298,7 +298,6 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint]) -> [u8] {
       ty::ty_float(ast::ty_f) { [s_float(ccx.tcx)] }
       ty::ty_uint(ast::ty_u) | ty::ty_ptr(_) { [s_uint(ccx.tcx)] }
       ty::ty_type { [s_tydesc(ccx.tcx)] }
-      ty::ty_send_type { [s_send_tydesc(ccx.tcx)] }
       ty::ty_int(ast::ty_i8) { [shape_i8] }
       ty::ty_uint(ast::ty_u16) { [shape_u16] }
       ty::ty_int(ast::ty_i16) { [shape_i16] }
diff --git a/src/rustc/middle/trans/type_of.rs b/src/rustc/middle/trans/type_of.rs
index 7c19134faad..9d49c595fe5 100644
--- a/src/rustc/middle/trans/type_of.rs
+++ b/src/rustc/middle/trans/type_of.rs
@@ -71,7 +71,7 @@ fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
         ret T_struct([T_i8(), type_of(cx, sub1)]);
       }
       ty::ty_param(_, _) { T_typaram(cx.tn) }
-      ty::ty_send_type | ty::ty_type { T_ptr(cx.tydesc_type) }
+      ty::ty_type { T_ptr(cx.tydesc_type) }
       ty::ty_tup(elts) {
         let tys = [];
         for elt in elts {
diff --git a/src/rustc/middle/ty.rs b/src/rustc/middle/ty.rs
index 32be4ee5518..e91ef230cd2 100644
--- a/src/rustc/middle/ty.rs
+++ b/src/rustc/middle/ty.rs
@@ -82,7 +82,6 @@ export ty_rptr, mk_rptr;
 export ty_rec, mk_rec;
 export ty_enum, mk_enum, type_is_enum;
 export ty_tup, mk_tup;
-export ty_send_type, mk_send_type;
 export ty_type, mk_type;
 export ty_uint, mk_uint, mk_mach_uint;
 export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box;
@@ -267,7 +266,6 @@ enum sty {
     ty_self([t]), // interface method self type
 
     ty_type, // type_desc*
-    ty_send_type, // type_desc* that has been cloned into exchange heap
     ty_opaque_box, // used by monomorphizer to represent any @ box
     ty_constr(t, [@type_constr]),
     ty_opaque_closure_ptr(closure_kind), // ptr to env for fn, fn@, fn~
@@ -392,7 +390,7 @@ fn mk_t_with_id(cx: ctxt, st: sty, o_def_id: option<ast::def_id>) -> t {
     }
     alt st {
       ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
-      ty_str | ty_type | ty_send_type | ty_opaque_closure_ptr(_) |
+      ty_str | ty_type | ty_opaque_closure_ptr(_) |
       ty_opaque_box {}
       ty_param(_, _) { has_params = true; }
       ty_var(_) | ty_self(_) { has_vars = true; }
@@ -518,8 +516,6 @@ fn mk_param(cx: ctxt, n: uint, k: def_id) -> t { mk_t(cx, ty_param(n, k)) }
 
 fn mk_type(cx: ctxt) -> t { mk_t(cx, ty_type) }
 
-fn mk_send_type(cx: ctxt) -> t { mk_t(cx, ty_send_type) }
-
 fn mk_opaque_closure_ptr(cx: ctxt, ck: closure_kind) -> t {
     mk_t(cx, ty_opaque_closure_ptr(ck))
 }
@@ -553,7 +549,7 @@ fn maybe_walk_ty(ty: t, f: fn(t) -> bool) {
     if !f(ty) { ret; }
     alt get(ty).struct {
       ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
-      ty_str | ty_send_type | ty_type | ty_opaque_box |
+      ty_str | ty_type | ty_opaque_box |
       ty_opaque_closure_ptr(_) | ty_var(_) | ty_param(_, _) {}
       ty_box(tm) | ty_vec(tm) | ty_ptr(tm) | ty_rptr(_, tm) {
         maybe_walk_ty(tm.ty, f);
@@ -599,7 +595,7 @@ fn fold_ty(cx: ctxt, fld: fold_mode, ty_0: t) -> t {
 
     alt tb.struct {
       ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
-      ty_str | ty_type | ty_send_type | ty_opaque_closure_ptr(_) |
+      ty_str | ty_type | ty_opaque_closure_ptr(_) |
       ty_opaque_box {}
       ty_box(tm) {
         ty = mk_box(cx, {ty: fold_ty(cx, fld, tm.ty), mutbl: tm.mutbl});
@@ -780,7 +776,7 @@ pure fn type_is_unique(ty: t) -> bool {
 pure fn type_is_scalar(ty: t) -> bool {
     alt get(ty).struct {
       ty_nil | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
-      ty_send_type | ty_type | ty_ptr(_) | ty_rptr(_, _) { true }
+      ty_type | ty_ptr(_) | ty_rptr(_, _) { true }
       _ { false }
     }
 }
@@ -884,7 +880,7 @@ fn type_kind(cx: ctxt, ty: t) -> kind {
     let result = alt get(ty).struct {
       // Scalar and unique types are sendable
       ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
-      ty_ptr(_) | ty_send_type | ty_str { kind_sendable }
+      ty_ptr(_) | ty_str { kind_sendable }
       ty_type { kind_copyable }
       ty_fn(f) { proto_kind(f.proto) }
       ty_opaque_closure_ptr(ck_block) { kind_noncopyable }
@@ -1034,7 +1030,7 @@ fn type_is_pod(cx: ctxt, ty: t) -> bool {
     alt get(ty).struct {
       // Scalar types
       ty_nil | ty_bot | ty_bool | ty_int(_) | ty_float(_) | ty_uint(_) |
-      ty_send_type | ty_type | ty_ptr(_) { result = true; }
+      ty_type | ty_ptr(_) { result = true; }
       // Boxed types
       ty_str | ty_box(_) | ty_uniq(_) | ty_vec(_) | ty_fn(_) |
       ty_iface(_, _) | ty_rptr(_,_) | ty_opaque_box { result = false; }
@@ -1223,7 +1219,6 @@ fn hash_type_structure(st: sty) -> uint {
         h
       }
       ty_uniq(mt) { hash_subty(37u, mt.ty) }
-      ty_send_type { 38u }
       ty_iface(did, tys) {
         let h = hash_def(40u, did);
         for typ: t in tys { h = hash_subty(h, typ); }
@@ -1984,7 +1979,7 @@ mod unify {
           (_, ty_bot) { nxt(expected) }
           (ty_bot, _) { nxt(actual) }
           (ty_nil, _) | (ty_bool, _) | (ty_int(_), _) | (ty_uint(_), _) |
-          (ty_float(_), _) | (ty_str, _) | (ty_send_type, _) {
+          (ty_float(_), _) | (ty_str, _) {
             struct_cmp(cx, expected, actual, nxt)
           }
           (ty_param(e_n, _), ty_param(a_n, _)) if e_n == a_n {