From 66c52036dabdeba6f28dcc0401d5a64c232e35d7 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Jun 2011 10:12:56 -0700 Subject: [PATCH 1/2] rustc: Change interior vector syntax to `T[]` --- src/comp/front/parser.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 165ca574df5..c733e5b372d 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -411,6 +411,26 @@ fn parse_ty_constrs(@ast::ty t, &parser p) -> @ast::ty { ret t; } +fn parse_ty_postfix(@ast::ty orig_t, &parser p) -> @ast::ty { + auto lo = p.get_lo_pos(); + if (p.peek() == token::LBRACKET) { + p.bump(); + + auto mut; + if (eat_word(p, "mutable")) { + mut = ast::mut; + } else { + mut = ast::imm; + } + + expect(p, token::RBRACKET); + auto hi = p.get_hi_pos(); + auto t = ast::ty_ivec(rec(ty=orig_t, mut=mut)); + ret parse_ty_postfix(@spanned(lo, hi, t), p); + } + ret parse_ty_constrs(orig_t, p); +} + fn parse_ty_or_bang(&parser p) -> ty_or_bang { alt (p.peek()) { case (token::NOT) { p.bump(); ret a_bang[@ast::ty]; } @@ -522,11 +542,6 @@ fn parse_ty(&parser p) -> @ast::ty { t = ast::ty_chan(parse_ty(p)); hi = p.get_hi_pos(); expect(p, token::RBRACKET); - } else if (eat_word(p, "ivec")) { - expect(p, token::LBRACKET); - t = ast::ty_ivec(parse_mt(p)); - hi = p.get_hi_pos(); - expect(p, token::RBRACKET); } else if (eat_word(p, "mutable")) { p.get_session().span_warn(p.get_span(), "ignoring deprecated 'mutable'" @@ -539,7 +554,7 @@ fn parse_ty(&parser p) -> @ast::ty { t = ast::ty_path(path, p.get_ann()); hi = path.span.hi; } else { p.err("expecting type"); t = ast::ty_nil; fail; } - ret parse_ty_constrs(@spanned(lo, hi, t), p); + ret parse_ty_postfix(@spanned(lo, hi, t), p); } fn parse_arg(&parser p) -> ast::arg { From f9da8b2676c192e7a1ad68c719b84edf1b52ccb7 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Jun 2011 10:32:52 -0700 Subject: [PATCH 2/2] rt: Add an ivec length intrinsic and an ivec reserve function, both untested as of yet --- src/rt/intrinsics/intrinsics.cpp | 17 ++++++- src/rt/intrinsics/intrinsics.ll.in | 81 ++++++++++++++++++++++-------- src/rt/rust_builtin.cpp | 31 ++++++++++++ src/rt/rustrt.def.in | 1 + 4 files changed, 106 insertions(+), 24 deletions(-) diff --git a/src/rt/intrinsics/intrinsics.cpp b/src/rt/intrinsics/intrinsics.cpp index b327c0f105f..ffacc7224bf 100644 --- a/src/rt/intrinsics/intrinsics.cpp +++ b/src/rt/intrinsics/intrinsics.cpp @@ -1,5 +1,5 @@ -// Use `clang++ -emit-llvm -S -arch i386 -O3 -I../isaac -I../uthash -o -// intrinsics.ll intrinsics.cpp` +// Use `clang++ -emit-llvm -S -arch i386 -O3 -I../isaac -I../uthash +// -I../arch/i386 -o intrinsics.ll intrinsics.cpp` #include "../rust_internal.h" @@ -9,3 +9,16 @@ rust_intrinsic_vec_len(rust_task *task, type_desc *ty, rust_vec *v) return v->fill / ty->size; } +extern "C" size_t +rust_intrinsic_ivec_len(rust_task *task, type_desc *ty, rust_ivec *v) +{ + size_t fill; + if (v->fill) + fill = v->fill; + else if (v->payload.ptr) + fill = v->payload.ptr->fill; + else + fill = 0; + return fill / ty->size; +} + diff --git a/src/rt/intrinsics/intrinsics.ll.in b/src/rt/intrinsics/intrinsics.ll.in index ab78bab75cc..f163df46e6d 100644 --- a/src/rt/intrinsics/intrinsics.ll.in +++ b/src/rt/intrinsics/intrinsics.ll.in @@ -3,53 +3,63 @@ target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3 target triple = "@CFG_LLVM_TRIPLE@" %0 = type { i32, i8**, i32 } -%1 = type { %"struct.hash_map *>::map_entry"* } -%2 = type { %"struct.hash_map *>::map_entry"* } -%3 = type { %"struct.hash_map *>::map_entry"* } -%4 = type { i32, %struct.rust_dom**, i32 } +%1 = type { i32, %struct.rust_task**, i32 } +%2 = type { %"struct.hash_map *>::map_entry"* } +%3 = type { %"struct.hash_map *>::map_entry"* } +%4 = type { %"struct.hash_map *>::map_entry"* } %5 = type { i32 (...)**, %6 } -%6 = type { i32, %class.rust_message_queue**, i32 } -%7 = type opaque -%8 = type { %"struct.hash_map *>::map_entry"* } +%6 = type { i32, %struct.rust_dom**, i32 } +%7 = type { i32 (...)**, %8 } +%8 = type { i32, %class.rust_message_queue**, i32 } +%9 = type opaque +%10 = type opaque +%11 = type { %"struct.hash_map *>::map_entry"* } %class.array_list = type { i32, %struct.maybe_proxy**, i32 } +%class.context = type { %struct.registers_t, %class.context* } %class.hash_map = type { %"struct.hash_map *>::map_entry"* } -%class.indexed_list = type { i32 (...)**, %4 } +%class.indexed_list = type { i32 (...)**, %1 } %class.lock_and_signal = type { i32 (...)**, %struct._opaque_pthread_cond_t, %struct._opaque_pthread_mutex_t } +%class.lock_free_queue = type { i32 (...)**, %"struct.lock_free_queue::pointer_t", %"struct.lock_free_queue::pointer_t" } %class.memory_region = type { i32 (...)**, %class.rust_srv*, %class.memory_region*, i32, %0, i8, i8, %class.lock_and_signal } -%class.ptr_vec = type { %struct.rust_dom*, i32, i32, %class.rust_crate_cache** } -%class.rust_crate = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 } -%class.rust_crate_cache = type { [4 x i8], %"class.rust_crate_cache::rust_sym"**, %"class.rust_crate_cache::c_sym"**, %"class.rust_crate_cache::lib"**, %struct.type_desc*, %class.rust_crate*, %struct.rust_dom*, i32 } -%"class.rust_crate_cache::c_sym" = type { [4 x i8], i32, %"class.rust_crate_cache::lib"*, %struct.rust_dom* } -%"class.rust_crate_cache::lib" = type { [4 x i8], i32, %struct.rust_dom* } -%"class.rust_crate_cache::rust_sym" = type { [4 x i8], i32, %"class.rust_crate_cache::c_sym"*, %struct.rust_dom* } +%class.rust_crate_cache = type { %struct.type_desc*, %struct.rust_dom*, i32 } %class.rust_handle = type opaque -%class.rust_kernel = type { [12 x i8], %class.memory_region*, %class.rust_log, %class.rust_srv*, %1, %2, %3, i8, %class.lock_and_signal, %class.indexed_list, %5 } +%class.rust_kernel = type { %class.rust_thread, %class.memory_region*, %class.rust_log, %class.rust_srv*, %2, %3, %4, i8, %class.lock_and_signal, %5, %7 } %class.rust_log = type { i32 (...)**, %class.rust_srv*, %struct.rust_dom*, i8, i8 } -%class.rust_message_queue = type { [20 x i8], %class.memory_region, %class.rust_kernel*, %7*, i32 } +%class.rust_message_queue = type { %class.lock_free_queue, %class.memory_region, %class.rust_kernel*, %10*, i32 } %class.rust_srv = type { i32 (...)**, %class.memory_region, %class.memory_region } -%class.rust_task_list = type { [16 x i8], %struct.rust_dom*, i8* } +%class.rust_task_list = type { %class.indexed_list, %struct.rust_dom*, i8* } +%class.rust_thread = type { i32 (...)**, i8, %struct._opaque_pthread_t* } %class.timer = type { i32 (...)**, i64, i64 } %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.gc_alloc = type { %struct.gc_alloc*, %struct.gc_alloc*, i32, [0 x i8] } %"struct.hash_map *>::map_entry" = type opaque %"struct.hash_map *>::map_entry" = type opaque %"struct.hash_map *>::map_entry" = type opaque %"struct.hash_map *>::map_entry" = type opaque %"struct.hash_map *>::map_entry" = type opaque -%struct.maybe_proxy = type { [4 x i8], %struct.rust_task* } +%"struct.lock_free_queue::node_t" = type { %9*, %"struct.lock_free_queue::pointer_t" } +%"struct.lock_free_queue::pointer_t" = type { %"struct.lock_free_queue::node_t"*, i32 } +%struct.maybe_proxy = type { %struct.rc_base, %struct.rust_task* } %struct.randctx = type { i32, [256 x i32], [256 x i32], i32, i32, i32 } +%struct.rc_base = type { i32 } +%struct.registers_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, i16, i32, i32 } %struct.rust_alarm = type { %struct.rust_task*, i32 } %struct.rust_cond = type { i8 } -%struct.rust_dom = type { [4 x i8], i32, %class.rust_crate*, %class.rust_log, i32, %class.rust_srv*, %class.memory_region, %class.memory_region, i8*, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.ptr_vec, %struct.randctx, %struct.rust_task*, %struct.rust_task*, i32, %class.rust_kernel*, i32, %class.hash_map, %8, %class.rust_message_queue*, %struct._opaque_pthread_attr_t } -%struct.rust_task = type { [8 x i8], %struct.stk_seg*, i32, i32, %struct.gc_alloc*, %struct.rust_dom*, %class.rust_crate_cache*, i8*, %class.rust_task_list*, %struct.rust_cond*, i8*, %struct.rust_task*, i32, i32, i32, %class.timer, i32*, %class.array_list, %struct.rust_alarm, %class.rust_handle* } -%struct.rust_vec = type { [4 x i8], i32, i32, i32, [0 x i8] } +%struct.rust_dom = type { %struct.rc_base, i32, %class.rust_log, i32, %class.rust_srv*, %class.memory_region, %class.memory_region, i8*, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_crate_cache, %struct.randctx, %struct.rust_task*, %struct.rust_task*, i32, %class.rust_kernel*, i32, %class.hash_map, %11, %class.rust_message_queue*, %struct._opaque_pthread_attr_t } +%struct.rust_ivec = type { i32, i32, %union.rust_ivec_payload } +%struct.rust_ivec_heap = type { i32, [0 x i8] } +%struct.rust_task = type { %struct.maybe_proxy, %struct.stk_seg*, i32, i32, %struct.gc_alloc*, %struct.rust_dom*, %class.rust_crate_cache*, i8*, %class.rust_task_list*, %struct.rust_cond*, i8*, %struct.rust_task*, i32, i32, i32, %class.timer, i32*, %class.array_list, %struct.rust_alarm, %class.rust_handle*, %class.context } +%struct.rust_vec = type { %struct.rc_base, i32, i32, i32, [0 x i8] } %struct.stk_seg = type { i32, i32, [0 x i8] } -%struct.type_desc = type { %struct.type_desc**, i32, i32, i32, i32, i32, i32, i32, i32, i32, %struct.UT_hash_handle, i32, [0 x %struct.type_desc*] } +%struct.type_desc = type { %struct.type_desc**, i32, i32, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, i32, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*, i8*, i8)*, %struct.UT_hash_handle, i32, [0 x %struct.type_desc*] } +%union.rust_ivec_payload = type { %struct.rust_ivec_heap* } define linkonce_odr i32 @rust_intrinsic_vec_len(%struct.rust_task* nocapture %task, %struct.type_desc* nocapture %ty, %struct.rust_vec* nocapture %v) nounwind readonly ssp { entry: @@ -61,6 +71,33 @@ entry: ret i32 %div } +define linkonce_odr i32 @rust_intrinsic_ivec_len(%struct.rust_task* nocapture %task, %struct.type_desc* nocapture %ty, %struct.rust_ivec* nocapture %v) nounwind readonly ssp { +entry: + %fill1 = getelementptr inbounds %struct.rust_ivec* %v, i32 0, i32 0 + %tmp2 = load i32* %fill1, align 4, !tbaa !0 + %tobool = icmp eq i32 %tmp2, 0 + br i1 %tobool, label %if.else, label %if.end17 + +if.else: ; preds = %entry + %ptr = getelementptr inbounds %struct.rust_ivec* %v, i32 0, i32 2, i32 0 + %tmp7 = load %struct.rust_ivec_heap** %ptr, align 4, !tbaa !3 + %tobool8 = icmp eq %struct.rust_ivec_heap* %tmp7, null + br i1 %tobool8, label %if.end17, label %if.then9 + +if.then9: ; preds = %if.else + %fill14 = getelementptr inbounds %struct.rust_ivec_heap* %tmp7, i32 0, i32 0 + %tmp15 = load i32* %fill14, align 4, !tbaa !0 + br label %if.end17 + +if.end17: ; preds = %if.else, %entry, %if.then9 + %fill.0 = phi i32 [ %tmp15, %if.then9 ], [ %tmp2, %entry ], [ 0, %if.else ] + %size = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1 + %tmp20 = load i32* %size, align 4, !tbaa !0 + %div = udiv i32 %fill.0, %tmp20 + ret i32 %div +} + !0 = metadata !{metadata !"long", metadata !1} !1 = metadata !{metadata !"omnipotent char", metadata !2} !2 = metadata !{metadata !"Simple C/C++ TBAA", null} +!3 = metadata !{metadata !"any pointer", metadata !1} diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index 3500743a79e..2437a5c364d 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -576,6 +576,37 @@ get_time(rust_task *task, uint32_t *sec, uint32_t *usec) { } #endif +/** + * Preallocates the exact number of bytes in the given interior vector. + */ +extern "C" void +ivec_reserve(rust_task *task, type_desc *ty, rust_ivec *v, size_t n_elems) +{ + size_t new_alloc = n_elems * ty->size; + if (new_alloc <= v->alloc) + return; // Already big enough. + + rust_ivec_heap *heap_part; + if (v->fill) { + // On stack; spill to heap. + heap_part = (rust_ivec_heap *)task->malloc(new_alloc + + sizeof(size_t)); + heap_part->fill = v->fill; + memcpy(&heap_part->data, v->payload.data, v->fill); + + v->fill = 0; + v->payload.ptr = heap_part; + } else { + // On heap; resize. + heap_part = (rust_ivec_heap *)task->realloc(v->payload.ptr, + new_alloc); + v->payload.ptr = heap_part; + } + + v->alloc = new_alloc; +} + + // // Local Variables: // mode: C++ diff --git a/src/rt/rustrt.def.in b/src/rt/rustrt.def.in index 9be5141f04d..be4a5a8d96e 100644 --- a/src/rt/rustrt.def.in +++ b/src/rt/rustrt.def.in @@ -9,6 +9,7 @@ debug_trap debug_tydesc do_gc get_time +ivec_reserve last_os_error rand_free rand_new