start to isolate target strings so that they can be configured for

x64 or other targets
This commit is contained in:
Niko Matsakis 2011-10-12 12:29:08 -07:00 committed by Brian Anderson
parent eed782c778
commit 5b3bddcd87
8 changed files with 114 additions and 74 deletions

View File

@ -94,7 +94,8 @@ mod write {
if opts.stack_growth { llvm::LLVMRustEnableSegmentedStacks(); }
link_intrinsics(sess, llmod);
let pm = mk_pass_manager();
let td = mk_target_data(x86::get_data_layout());
let td = mk_target_data(
sess.get_targ_cfg().target_strs.data_layout);
llvm::LLVMAddTargetData(td.lltd, pm.llpm);
// TODO: run the linter here also, once there are llvm-c bindings for
// it.
@ -199,16 +200,17 @@ mod write {
// Save the assembly file if -S is used
if opts.output_type == output_type_assembly {
let _: () = str::as_buf(x86::get_target_triple(), {|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(pm.llpm,
llmod,
buf_t,
buf_o,
LLVMAssemblyFile,
CodeGenOptLevel)
})
});
let _: () = str::as_buf(
sess.get_targ_cfg().target_strs.target_triple,
{|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(
pm.llpm,
llmod,
buf_t,
buf_o,
LLVMAssemblyFile,
CodeGenOptLevel)})});
}
@ -217,32 +219,34 @@ mod write {
if opts.output_type == output_type_object ||
opts.output_type == output_type_exe {
let _: () =
str::as_buf(x86::get_target_triple(), {|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(pm.llpm,
llmod,
buf_t,
buf_o,
LLVMObjectFile,
CodeGenOptLevel)
})
});
str::as_buf(
sess.get_targ_cfg().target_strs.target_triple,
{|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(
pm.llpm,
llmod,
buf_t,
buf_o,
LLVMObjectFile,
CodeGenOptLevel)})});
}
} else {
// If we aren't saving temps then just output the file
// type corresponding to the '-c' or '-S' flag used
let _: () =
str::as_buf(x86::get_target_triple(), {|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(pm.llpm,
llmod,
buf_t,
buf_o,
FileType,
CodeGenOptLevel)
})
});
str::as_buf(
sess.get_targ_cfg().target_strs.target_triple,
{|buf_t|
str::as_buf(output, {|buf_o|
llvm::LLVMRustWriteOutputFile(
pm.llpm,
llmod,
buf_t,
buf_o,
FileType,
CodeGenOptLevel)})});
}
// Clean up and return

View File

@ -0,0 +1,8 @@
import std::str;
type t = {
module_asm: str,
meta_sect_name: str,
data_layout: str,
target_triple: str
};

View File

@ -1,34 +1,42 @@
import lib::llvm::llvm;
import lib::llvm::llvm::ModuleRef;
import std::str;
import std::os::target_os;
import driver::session;
fn get_module_asm() -> str { ret ""; }
fn get_target_strs(target_os: session::os) -> target_strs::t {
ret {
module_asm: "",
fn get_meta_sect_name() -> str {
if str::eq(target_os(), "macos") { ret "__DATA,__note.rustc"; }
if str::eq(target_os(), "win32") { ret ".note.rustc"; }
ret ".note.rustc";
}
meta_sect_name: alt target_os {
session::os_macos. { "__DATA,__note.rustc" }
session::os_win32. { ".note.rustc" }
session::os_linux. { ".note.rustc" }
},
fn get_data_layout() -> str {
if str::eq(target_os(), "macos") {
ret "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16" + "-i32:32:32-i64:32:64" +
data_layout: alt target_os {
session::os_macos. {
"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";
}
if str::eq(target_os(), "win32") {
ret "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-n8:16:32";
}
ret "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-n8:16:32";
"-v128:128:128-a0:0:64-f80:128:128" + "-n8:16:32"
}
session::os_win32. {
"e-p:32:32-f64:64:64-i64:64:64-f80:32:32-n8:16:32"
}
session::os_linux. {
"e-p:32:32-f64:32:64-i64:32:64-f80:32:32-n8:16:32"
}
},
target_triple: alt target_os {
session::os_macos. { "i686-apple-darwin" }
session::os_win32. { "i686-pc-mingw32" }
session::os_linux. { "i686-unknown-linux-gnu" }
}
};
}
fn get_target_triple() -> str {
if str::eq(target_os(), "macos") { ret "i686-apple-darwin"; }
if str::eq(target_os(), "win32") { ret "i686-pc-mingw32"; }
ret "i686-unknown-linux-gnu";
}
//
// Local Variables:
// mode: rust

View File

@ -16,6 +16,7 @@ import std::map::mk_hashmap;
import std::option::{some, none};
import std::getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
import back::link::output_type;
import back::x86;
tag pp_mode { ppm_normal; ppm_expanded; ppm_typed; ppm_identified; }
@ -302,12 +303,21 @@ fn get_arch(triple: str) -> session::arch {
}
fn build_target_config(sopts: @session::options) -> @session::config {
let os = get_os(sopts.target_triple);
let arch = get_arch(sopts.target_triple);
let (int_type, uint_type, float_type) = alt arch {
session::arch_x86. {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
session::arch_x64. {(ast::ty_i64, ast::ty_u64, ast::ty_f64)}
session::arch_arm. {(ast::ty_i32, ast::ty_u32, ast::ty_f64)}
};
let target_strs = alt arch {
session::arch_x86. {x86::get_target_strs(os)}
session::arch_x64. {x86::get_target_strs(os)}
session::arch_arm. {x86::get_target_strs(os)}
};
let target_cfg: @session::config =
@{os: get_os(sopts.target_triple),
arch: get_arch(sopts.target_triple),
int_type: ast::ty_i32,
uint_type: ast::ty_u32,
float_type: ast::ty_f64};
@{os: os, arch: arch, target_strs: target_strs, int_type: int_type,
uint_type: uint_type, float_type: float_type};
ret target_cfg;
}
@ -531,7 +541,10 @@ fn main(args: [str]) {
none::<pp_mode>. {/* continue */ }
}
let ls = opt_present(match, "ls");
if ls { metadata::creader::list_file_metadata(ifile, io::stdout()); ret; }
if ls {
metadata::creader::list_file_metadata(sess, ifile, io::stdout());
ret;
}
let stop_after_codegen =
sopts.output_type != link::output_type_exe ||

View File

@ -7,6 +7,7 @@ import std::{uint, map, option, str};
import std::option::{some, none};
import syntax::parse::parser::parse_sess;
import util::filesearch;
import back::target_strs;
tag os { os_win32; os_macos; os_linux; }
@ -15,6 +16,7 @@ tag arch { arch_x86; arch_x64; arch_arm; }
type config =
{os: os,
arch: arch,
target_strs: target_strs::t,
int_type: ty_mach,
uint_type: ty_mach,
float_type: ty_mach};

View File

@ -72,8 +72,8 @@ fn visit_item(e: env, i: @ast::item) {
}
// A diagnostic function for dumping crate metadata to an output stream
fn list_file_metadata(path: str, out: io::writer) {
alt get_metadata_section(path) {
fn list_file_metadata(sess: session::session, path: str, out: io::writer) {
alt get_metadata_section(sess, path) {
option::some(bytes) { decoder::list_crate_metadata(bytes, out); }
option::none. {
out.write_str("Could not find metadata in " + path + ".\n");
@ -126,13 +126,14 @@ fn find_library_crate(sess: session::session, ident: ast::ident,
}
let nn = default_native_lib_naming(sess, sess.get_opts().static);
let x =
find_library_crate_aux(nn, metas, sess.filesearch());
find_library_crate_aux(sess, nn, metas, sess.filesearch());
if x != none || sess.get_opts().static { ret x; }
let nn2 = default_native_lib_naming(sess, true);
ret find_library_crate_aux(nn2, metas, sess.filesearch());
ret find_library_crate_aux(sess, nn2, metas, sess.filesearch());
}
fn find_library_crate_aux(nn: {prefix: str, suffix: str},
fn find_library_crate_aux(sess: session::session,
nn: {prefix: str, suffix: str},
metas: [@ast::meta_item],
filesearch: filesearch::filesearch) ->
option::t<{ident: str, data: @[u8]}> {
@ -148,7 +149,7 @@ fn find_library_crate_aux(nn: {prefix: str, suffix: str},
option::none
} else {
log #fmt("%s is a candidate", path);
alt get_metadata_section(path) {
alt get_metadata_section(sess, path) {
option::some(cvec) {
if !metadata_matches(cvec, metas) {
log #fmt["skipping %s, metadata doesn't match", path];
@ -167,7 +168,8 @@ fn find_library_crate_aux(nn: {prefix: str, suffix: str},
});
}
fn get_metadata_section(filename: str) -> option::t<@[u8]> unsafe {
fn get_metadata_section(sess: session::session,
filename: str) -> option::t<@[u8]> {
let mb = str::as_buf(filename, {|buf|
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
});
@ -176,12 +178,14 @@ fn get_metadata_section(filename: str) -> option::t<@[u8]> unsafe {
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 name = unsafe { str::str_from_cstr(name_buf) };
if str::eq(name, sess.get_targ_cfg().target_strs.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 cvbuf: *u8 = std::unsafe::reinterpret_cast(cbuf);
ret option::some::<@[u8]>(@vec::unsafe::from_buf(cvbuf, csz));
}
}
llvm::LLVMMoveToNextSection(si.llsi);
}

View File

@ -5999,7 +5999,7 @@ fn write_metadata(cx: @crate_ctxt, crate: @ast::crate) {
});
llvm::LLVMSetInitializer(llglobal, llconst);
let _: () =
str::as_buf(x86::get_meta_sect_name(),
str::as_buf(cx.sess.get_targ_cfg().target_strs.meta_sect_name,
{|buf| llvm::LLVMSetSection(llglobal, buf) });
llvm::LLVMSetLinkage(llglobal,
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
@ -6031,12 +6031,12 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
(buf, llvm::LLVMGetGlobalContext())
});
let _: () =
str::as_buf(x86::get_data_layout(),
str::as_buf(sess.get_targ_cfg().target_strs.data_layout,
{|buf| llvm::LLVMSetDataLayout(llmod, buf) });
let _: () =
str::as_buf(x86::get_target_triple(),
str::as_buf(sess.get_targ_cfg().target_strs.target_triple,
{|buf| llvm::LLVMSetTarget(llmod, buf) });
let td = mk_target_data(x86::get_data_layout());
let td = mk_target_data(sess.get_targ_cfg().target_strs.data_layout);
let tn = mk_type_names();
let intrinsics = declare_intrinsics(llmod);
let task_type = T_task();

View File

@ -93,6 +93,7 @@ mod back {
mod upcall;
mod x86;
mod rpath;
mod target_strs;
}
mod metadata {