}
fn llvm_err(sess: session::session, msg: str) {
- let buf = llvm::LLVMRustGetLastError();
- if buf == std::ptr::null() {
- sess.fatal(msg);
- } else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
+ unsafe {
+ let buf = llvm::LLVMRustGetLastError();
+ if buf == std::ptr::null() {
+ sess.fatal(msg);
+ } else { sess.fatal(msg + ": " + str::str_from_cstr(buf)); }
+ }
}
fn link_intrinsics(sess: session::session, llmod: ModuleRef) {
let out_ty: TypeRef = llvm::LLVMGetReturnType(ty);
let n_args: uint = llvm::LLVMCountParamTypes(ty);
let args: [TypeRef] = vec::init_elt::<TypeRef>(0 as TypeRef, n_args);
- llvm::LLVMGetParamTypes(ty, vec::to_ptr(args));
+ unsafe {
+ llvm::LLVMGetParamTypes(ty, vec::to_ptr(args));
+ }
s += tys_str(names, outer, args);
s += ") -> ";
s += type_to_str_inner(names, outer, out_ty);
let s: str = "{";
let n_elts: uint = llvm::LLVMCountStructElementTypes(ty);
let elts: [TypeRef] = vec::init_elt::<TypeRef>(0 as TypeRef, n_elts);
- llvm::LLVMGetStructElementTypes(ty, vec::to_ptr(elts));
+ unsafe {
+ llvm::LLVMGetStructElementTypes(ty, vec::to_ptr(elts));
+ }
s += tys_str(names, outer, elts);
s += "}";
ret s;
fn fn_ty_param_tys(fn_ty: TypeRef) -> [TypeRef] {
let args = vec::init_elt(0 as TypeRef, llvm::LLVMCountParamTypes(fn_ty));
- llvm::LLVMGetParamTypes(fn_ty, vec::to_ptr(args));
+ unsafe {
+ llvm::LLVMGetParamTypes(fn_ty, vec::to_ptr(args));
+ }
ret args;
}
}
fn get_metadata_section(filename: str) -> option::t<@[u8]> {
- let mb = str::as_buf(filename, {|buf|
- llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
- });
- if mb as int == 0 { ret option::none::<@[u8]>; }
- let of = mk_object_file(mb);
- let si = mk_section_iter(of.llof);
- while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
- let name_buf = llvm::LLVMGetSectionName(si.llsi);
- let name = str::str_from_cstr(name_buf);
- if str::eq(name, x86::get_meta_sect_name()) {
- let cbuf = llvm::LLVMGetSectionContents(si.llsi);
- let csz = llvm::LLVMGetSectionSize(si.llsi);
- let cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
- ret option::some::<@[u8]>(@vec::unsafe::from_buf(cvbuf, csz));
+ unsafe {
+ let mb = str::as_buf(filename, {|buf|
+ llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
+ });
+ if mb as int == 0 { ret option::none::<@[u8]>; }
+ let of = mk_object_file(mb);
+ let si = mk_section_iter(of.llof);
+ while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
+ let name_buf = llvm::LLVMGetSectionName(si.llsi);
+ let name = str::str_from_cstr(name_buf);
+ if str::eq(name, x86::get_meta_sect_name()) {
+ let cbuf = llvm::LLVMGetSectionContents(si.llsi);
+ let csz = llvm::LLVMGetSectionSize(si.llsi);
+ let cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
+ ret option::some::<@[u8]>(@vec::unsafe::from_buf(cvbuf, csz));
+ }
+ llvm::LLVMMoveToNextSection(si.llsi);
}
- llvm::LLVMMoveToNextSection(si.llsi);
+ ret option::none::<@[u8]>;
}
- ret option::none::<@[u8]>;
}
fn load_library_crate(sess: session::session, span: span, ident: ast::ident,
if cx.unreachable { ret; }
assert (!cx.terminated);
cx.terminated = true;
- llvm::LLVMBuildAggregateRet(B(cx), vec::to_ptr(RetVals),
- vec::len(RetVals));
+ unsafe {
+ llvm::LLVMBuildAggregateRet(B(cx), vec::to_ptr(RetVals),
+ vec::len(RetVals));
+ }
}
fn Br(cx: @block_ctxt, Dest: BasicBlockRef) {
if cx.unreachable { ret; }
assert (!cx.terminated);
cx.terminated = true;
- llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
- vec::len(Args), Then, Catch, noname());
+ unsafe {
+ llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
+ vec::len(Args), Then, Catch, noname());
+ }
}
fn FastInvoke(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef],
if cx.unreachable { ret; }
assert (!cx.terminated);
cx.terminated = true;
- let v = llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
- vec::len(Args), Then, Catch, noname());
- llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+ unsafe {
+ let v = llvm::LLVMBuildInvoke(B(cx), Fn, vec::to_ptr(Args),
+ vec::len(Args), Then, Catch, noname());
+ llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+ }
}
fn Unreachable(cx: @block_ctxt) {
fn GEP(cx: @block_ctxt, Pointer: ValueRef, Indices: [ValueRef]) -> ValueRef {
if cx.unreachable { ret llvm::LLVMGetUndef(T_ptr(T_nil())); }
- ret llvm::LLVMBuildGEP(B(cx), Pointer, vec::to_ptr(Indices),
- vec::len(Indices), noname());
+ unsafe {
+ ret llvm::LLVMBuildGEP(B(cx), Pointer, vec::to_ptr(Indices),
+ vec::len(Indices), noname());
+ }
}
fn InBoundsGEP(cx: @block_ctxt, Pointer: ValueRef, Indices: [ValueRef]) ->
ValueRef {
if cx.unreachable { ret llvm::LLVMGetUndef(T_ptr(T_nil())); }
- ret llvm::LLVMBuildInBoundsGEP(B(cx), Pointer, vec::to_ptr(Indices),
- vec::len(Indices), noname());
+ unsafe {
+ ret llvm::LLVMBuildInBoundsGEP(B(cx), Pointer, vec::to_ptr(Indices),
+ vec::len(Indices), noname());
+ }
}
fn StructGEP(cx: @block_ctxt, Pointer: ValueRef, Idx: uint) -> ValueRef {
if cx.unreachable { ret llvm::LLVMGetUndef(Ty); }
assert (vec::len::<ValueRef>(vals) == vec::len::<BasicBlockRef>(bbs));
let phi = EmptyPhi(cx, Ty);
- llvm::LLVMAddIncoming(phi, vec::to_ptr(vals), vec::to_ptr(bbs),
- vec::len(vals));
- ret phi;
+ unsafe {
+ llvm::LLVMAddIncoming(phi, vec::to_ptr(vals), vec::to_ptr(bbs),
+ vec::len(vals));
+ ret phi;
+ }
}
fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
}
fn Call(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
- if cx.unreachable { ret _UndefReturn(Fn); }
- ret llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
- vec::len(Args), noname());
+ unsafe {
+ if cx.unreachable { ret _UndefReturn(Fn); }
+ ret llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+ vec::len(Args), noname());
+ }
}
fn FastCall(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef]) -> ValueRef {
- if cx.unreachable { ret _UndefReturn(Fn); }
- let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
- vec::len(Args), noname());
- llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
- ret v;
+ unsafe {
+ if cx.unreachable { ret _UndefReturn(Fn); }
+ let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+ vec::len(Args), noname());
+ llvm::LLVMSetInstructionCallConv(v, lib::llvm::LLVMFastCallConv);
+ ret v;
+ }
}
fn CallWithConv(cx: @block_ctxt, Fn: ValueRef, Args: [ValueRef], Conv: uint)
-> ValueRef {
- if cx.unreachable { ret _UndefReturn(Fn); }
- let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
- vec::len(Args), noname());
- llvm::LLVMSetInstructionCallConv(v, Conv);
- ret v;
+ unsafe {
+ if cx.unreachable { ret _UndefReturn(Fn); }
+ let v = llvm::LLVMBuildCall(B(cx), Fn, vec::to_ptr(Args),
+ vec::len(Args), noname());
+ llvm::LLVMSetInstructionCallConv(v, Conv);
+ ret v;
+ }
}
fn Select(cx: @block_ctxt, If: ValueRef, Then: ValueRef, Else: ValueRef) ->
});
assert (T as int != 0);
let Args: [ValueRef] = [];
- llvm::LLVMBuildCall(b, T, vec::to_ptr(Args), vec::len(Args), noname());
+ unsafe {
+ llvm::LLVMBuildCall(b, T, vec::to_ptr(Args),
+ vec::len(Args), noname());
+ }
}
fn LandingPad(cx: @block_ctxt, Ty: TypeRef, PersFn: ValueRef,
}
fn T_fn(inputs: [TypeRef], output: TypeRef) -> TypeRef {
- ret llvm::LLVMFunctionType(output, to_ptr(inputs),
- std::vec::len::<TypeRef>(inputs), False);
+ unsafe {
+ ret llvm::LLVMFunctionType(output, to_ptr(inputs),
+ std::vec::len::<TypeRef>(inputs), False);
+ }
}
fn T_fn_pair(cx: crate_ctxt, tfn: TypeRef) -> TypeRef {
fn T_ptr(t: TypeRef) -> TypeRef { ret llvm::LLVMPointerType(t, 0u); }
fn T_struct(elts: [TypeRef]) -> TypeRef {
- ret llvm::LLVMStructType(to_ptr(elts), std::vec::len(elts), False);
+ unsafe {
+ ret llvm::LLVMStructType(to_ptr(elts), std::vec::len(elts), False);
+ }
}
fn T_named_struct(name: str) -> TypeRef {
}
fn set_struct_body(t: TypeRef, elts: [TypeRef]) {
- llvm::LLVMStructSetBody(t, to_ptr(elts), std::vec::len(elts), False);
+ unsafe {
+ llvm::LLVMStructSetBody(t, to_ptr(elts), std::vec::len(elts), False);
+ }
}
fn T_empty_struct() -> TypeRef { ret T_struct([]); }
fn T_tydesc_field(cx: crate_ctxt, field: int) -> TypeRef {
// Bit of a kludge: pick the fn typeref out of the tydesc..
- let tydesc_elts: [TypeRef] =
- std::vec::init_elt::<TypeRef>(T_nil(), abi::n_tydesc_fields as uint);
- llvm::LLVMGetStructElementTypes(cx.tydesc_type,
- to_ptr::<TypeRef>(tydesc_elts));
- let t = llvm::LLVMGetElementType(tydesc_elts[field]);
- ret t;
+ unsafe {
+ let tydesc_elts: [TypeRef] =
+ std::vec::init_elt::<TypeRef>(T_nil(),
+ abi::n_tydesc_fields as uint);
+ llvm::LLVMGetStructElementTypes(cx.tydesc_type,
+ to_ptr::<TypeRef>(tydesc_elts));
+ let t = llvm::LLVMGetElementType(tydesc_elts[field]);
+ ret t;
+ }
}
fn T_glue_fn(cx: crate_ctxt) -> TypeRef {
}
fn C_zero_byte_arr(size: uint) -> ValueRef {
- let i = 0u;
- let elts: [ValueRef] = [];
- while i < size { elts += [C_u8(0u)]; i += 1u; }
- ret llvm::LLVMConstArray(T_i8(), std::vec::to_ptr(elts),
- std::vec::len(elts));
+ unsafe {
+ let i = 0u;
+ let elts: [ValueRef] = [];
+ while i < size { elts += [C_u8(0u)]; i += 1u; }
+ ret llvm::LLVMConstArray(T_i8(), std::vec::to_ptr(elts),
+ std::vec::len(elts));
+ }
}
fn C_struct(elts: [ValueRef]) -> ValueRef {
- ret llvm::LLVMConstStruct(std::vec::to_ptr(elts), std::vec::len(elts),
- False);
+ unsafe {
+ ret llvm::LLVMConstStruct(std::vec::to_ptr(elts), std::vec::len(elts),
+ False);
+ }
}
fn C_named_struct(T: TypeRef, elts: [ValueRef]) -> ValueRef {
- ret llvm::LLVMConstNamedStruct(T, std::vec::to_ptr(elts),
- std::vec::len(elts));
+ unsafe {
+ ret llvm::LLVMConstNamedStruct(T, std::vec::to_ptr(elts),
+ std::vec::len(elts));
+ }
}
fn C_array(ty: TypeRef, elts: [ValueRef]) -> ValueRef {
- ret llvm::LLVMConstArray(ty, std::vec::to_ptr(elts), std::vec::len(elts));
+ unsafe {
+ ret llvm::LLVMConstArray(ty, std::vec::to_ptr(elts),
+ std::vec::len(elts));
+ }
}
fn C_bytes(bytes: [u8]) -> ValueRef {
- ret llvm::LLVMConstString(unsafe::reinterpret_cast(vec::to_ptr(bytes)),
- vec::len(bytes), False);
+ unsafe {
+ ret llvm::LLVMConstString(
+ unsafe::reinterpret_cast(vec::to_ptr(bytes)),
+ vec::len(bytes), False);
+ }
}
fn C_shape(ccx: @crate_ctxt, bytes: [u8]) -> ValueRef {
}
#[test]
-fn str_from_cstr() {
+unsafe fn str_from_cstr() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let b = vec::to_ptr(a);
let c = str::str_from_cstr(b);
}
#[test]
-fn as_buf2() {
+unsafe fn as_buf2() {
let s = "hello";
let sb = str::as_buf(s, {|b| b });
let s_cstr = str::str_from_cstr(sb);
fn add(&&x: uint, &&y: uint) -> uint { ret x + y; }
#[test]
-fn test_unsafe_ptrs() {
+unsafe fn test_unsafe_ptrs() {
// Test on-stack copy-from-buf.
let a = [1, 2, 3];
let ptr = vec::to_ptr(a);