2011-05-12 17:24:54 +02:00
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::llvm::ModuleRef;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::str;
|
|
|
|
import std::vec;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::os::target_os;
|
|
|
|
import util::common::istr;
|
2010-09-23 15:46:31 -07:00
|
|
|
|
|
|
|
const int wordsz = 4;
|
|
|
|
|
|
|
|
fn wstr(int i) -> str {
|
|
|
|
ret istr(i * wordsz);
|
|
|
|
}
|
|
|
|
|
2011-04-28 16:14:04 -04:00
|
|
|
fn start() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret [".cfi_startproc"];
|
2011-04-28 16:14:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn end() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret [".cfi_endproc"];
|
2011-04-28 16:14:04 -04:00
|
|
|
}
|
|
|
|
|
2010-09-23 15:46:31 -07:00
|
|
|
fn save_callee_saves() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["pushl %ebp",
|
2010-09-23 15:46:31 -07:00
|
|
|
"pushl %edi",
|
|
|
|
"pushl %esi",
|
2011-05-16 18:21:22 -07:00
|
|
|
"pushl %ebx"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-04-28 16:14:04 -04:00
|
|
|
fn save_callee_saves_with_cfi() -> vec[str] {
|
|
|
|
auto offset = 8;
|
|
|
|
auto t;
|
2011-05-16 18:21:22 -07:00
|
|
|
t = ["pushl %ebp"];
|
|
|
|
t += [".cfi_def_cfa_offset " + istr(offset)];
|
|
|
|
t += [".cfi_offset %ebp, -" + istr(offset)];
|
2011-04-28 16:14:04 -04:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
t += ["pushl %edi"];
|
2011-04-28 16:14:04 -04:00
|
|
|
offset += 4;
|
2011-05-16 18:21:22 -07:00
|
|
|
t += [".cfi_def_cfa_offset " + istr(offset)];
|
2011-04-28 16:14:04 -04:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
t += ["pushl %esi"];
|
2011-04-28 16:14:04 -04:00
|
|
|
offset += 4;
|
2011-05-16 18:21:22 -07:00
|
|
|
t += [".cfi_def_cfa_offset " + istr(offset)];
|
2011-04-28 16:14:04 -04:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
t += ["pushl %ebx"];
|
2011-04-28 16:14:04 -04:00
|
|
|
offset += 4;
|
2011-05-16 18:21:22 -07:00
|
|
|
t += [".cfi_def_cfa_offset " + istr(offset)];
|
2011-04-28 16:14:04 -04:00
|
|
|
ret t;
|
|
|
|
}
|
|
|
|
|
2010-09-23 15:46:31 -07:00
|
|
|
fn restore_callee_saves() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["popl %ebx",
|
2010-09-23 15:46:31 -07:00
|
|
|
"popl %esi",
|
|
|
|
"popl %edi",
|
2011-05-16 18:21:22 -07:00
|
|
|
"popl %ebp"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
fn load_esp_from_rust_sp_first_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl " + wstr(abi::task_field_rust_sp) + "(%ecx), %esp"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
fn load_esp_from_runtime_sp_first_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl " + wstr(abi::task_field_runtime_sp) + "(%ecx), %esp"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
fn store_esp_to_rust_sp_first_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl %esp, " + wstr(abi::task_field_rust_sp) + "(%ecx)"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
fn store_esp_to_runtime_sp_first_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl %esp, " + wstr(abi::task_field_runtime_sp) + "(%ecx)"];
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
fn load_esp_from_rust_sp_second_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl " + wstr(abi::task_field_rust_sp) + "(%edx), %esp"];
|
2011-03-09 15:05:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load_esp_from_runtime_sp_second_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl " + wstr(abi::task_field_runtime_sp) + "(%edx), %esp"];
|
2011-03-09 15:05:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn store_esp_to_rust_sp_second_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl %esp, " + wstr(abi::task_field_rust_sp) + "(%edx)"];
|
2011-03-09 15:05:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn store_esp_to_runtime_sp_second_arg() -> vec[str] {
|
2011-05-16 18:21:22 -07:00
|
|
|
ret ["movl %esp, " + wstr(abi::task_field_runtime_sp) + "(%edx)"];
|
2011-03-09 15:05:22 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn native_glue(int n_args, abi::native_glue_type ngt) -> vec[str] {
|
2011-04-29 16:40:30 -07:00
|
|
|
|
|
|
|
let bool pass_task;
|
|
|
|
alt (ngt) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (abi::ngt_rust) { pass_task = true; }
|
|
|
|
case (abi::ngt_pure_rust) { pass_task = true; }
|
|
|
|
case (abi::ngt_cdecl) { pass_task = false; }
|
2011-04-29 16:40:30 -07:00
|
|
|
}
|
2010-09-23 15:46:31 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 0, 4, 8, 12 are callee-saves
|
|
|
|
* 16 is retpc
|
2010-11-14 13:41:10 -08:00
|
|
|
* 20 .. (5+i) * 4 are args
|
|
|
|
*
|
|
|
|
* ecx is taskptr
|
|
|
|
* edx is callee
|
|
|
|
*
|
2010-09-23 15:46:31 -07:00
|
|
|
*/
|
|
|
|
|
2011-03-21 17:47:38 -04:00
|
|
|
fn copy_arg(bool pass_task, uint i) -> str {
|
|
|
|
if (i == 0u && pass_task) {
|
2011-03-09 14:16:11 -05:00
|
|
|
ret "movl %edx, (%esp)";
|
|
|
|
}
|
|
|
|
auto dst_off = wstr(0 + (i as int));
|
2011-03-21 17:47:38 -04:00
|
|
|
auto src_off;
|
|
|
|
if (pass_task) {
|
|
|
|
src_off = wstr(4 + (i as int));
|
|
|
|
} else {
|
|
|
|
src_off = wstr(5 + (i as int));
|
|
|
|
}
|
2011-05-16 18:21:22 -07:00
|
|
|
auto m = ["movl " + src_off + "(%ebp),%eax",
|
|
|
|
"movl %eax," + dst_off + "(%esp)"];
|
2011-05-17 20:41:41 +02:00
|
|
|
ret str::connect(m, "\n\t");
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-21 17:47:38 -04:00
|
|
|
auto carg = bind copy_arg(pass_task, _);
|
2010-09-23 15:46:31 -07:00
|
|
|
|
|
|
|
ret
|
2011-04-28 16:14:04 -04:00
|
|
|
start()
|
|
|
|
+ save_callee_saves_with_cfi()
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
+ ["movl %esp, %ebp # ebp = rust_sp"]
|
|
|
|
+ [".cfi_def_cfa_register %ebp"]
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
+ store_esp_to_rust_sp_second_arg()
|
|
|
|
+ load_esp_from_runtime_sp_second_arg()
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
+ ["subl $" + wstr(n_args) + ", %esp # esp -= args",
|
|
|
|
"andl $~0xf, %esp # align esp down"]
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-05-17 20:41:41 +02:00
|
|
|
+ vec::init_fn[str](carg, (n_args) as uint)
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
+ ["movl %edx, %edi # save task from edx to edi",
|
2011-03-09 15:05:22 -05:00
|
|
|
"call *%ecx # call *%ecx",
|
2011-05-16 18:21:22 -07:00
|
|
|
"movl %edi, %edx # restore edi-saved task to edx"]
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-03-09 15:05:22 -05:00
|
|
|
+ load_esp_from_rust_sp_second_arg()
|
2010-09-23 15:46:31 -07:00
|
|
|
+ restore_callee_saves()
|
2011-05-16 18:21:22 -07:00
|
|
|
+ ["ret"]
|
2011-04-28 16:14:04 -04:00
|
|
|
+ end();
|
2010-09-23 15:46:31 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn decl_glue(int align, str prefix, str name, vec[str] insns) -> str {
|
|
|
|
auto sym = prefix + name;
|
|
|
|
ret "\t.globl " + sym + "\n" +
|
|
|
|
"\t.balign " + istr(align) + "\n" +
|
|
|
|
sym + ":\n" +
|
2011-05-17 20:41:41 +02:00
|
|
|
"\t" + str::connect(insns, "\n\t");
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn decl_native_glue(int align, str prefix, abi::native_glue_type ngt, uint n)
|
2011-04-29 16:40:30 -07:00
|
|
|
-> str {
|
2010-09-23 17:16:34 -07:00
|
|
|
let int i = n as int;
|
|
|
|
ret decl_glue(align, prefix,
|
2011-05-12 17:24:54 +02:00
|
|
|
abi::native_glue_name(i, ngt),
|
2011-04-29 16:40:30 -07:00
|
|
|
native_glue(i, ngt));
|
2010-09-23 17:16:34 -07:00
|
|
|
}
|
|
|
|
|
2010-11-05 18:29:18 -07:00
|
|
|
fn get_symbol_prefix() -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "macos") ||
|
|
|
|
str::eq(target_os(), "win32")) {
|
2010-11-05 18:29:18 -07:00
|
|
|
ret "_";
|
|
|
|
} else {
|
|
|
|
ret "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-23 15:46:31 -07:00
|
|
|
fn get_module_asm() -> str {
|
|
|
|
auto align = 4;
|
2010-10-22 16:15:06 -07:00
|
|
|
|
2010-11-05 18:29:18 -07:00
|
|
|
auto prefix = get_symbol_prefix();
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-05-24 18:23:39 -04:00
|
|
|
let vec[str] glues =
|
|
|
|
[]
|
2011-05-17 20:41:41 +02:00
|
|
|
+ vec::init_fn[str](bind decl_native_glue(align, prefix,
|
2011-05-12 17:24:54 +02:00
|
|
|
abi::ngt_rust, _), (abi::n_native_glues + 1) as uint)
|
2011-05-17 20:41:41 +02:00
|
|
|
+ vec::init_fn[str](bind decl_native_glue(align, prefix,
|
2011-05-12 17:24:54 +02:00
|
|
|
abi::ngt_pure_rust, _), (abi::n_native_glues + 1) as uint)
|
2011-05-17 20:41:41 +02:00
|
|
|
+ vec::init_fn[str](bind decl_native_glue(align, prefix,
|
2011-05-12 17:24:54 +02:00
|
|
|
abi::ngt_cdecl, _), (abi::n_native_glues + 1) as uint);
|
2010-09-23 15:46:31 -07:00
|
|
|
|
2011-03-21 17:47:38 -04:00
|
|
|
|
2011-05-17 20:41:41 +02:00
|
|
|
ret str::connect(glues, "\n\n");
|
2010-09-23 15:46:31 -07:00
|
|
|
}
|
|
|
|
|
2011-03-11 15:35:20 -08:00
|
|
|
fn get_meta_sect_name() -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "macos")) {
|
2011-03-11 15:35:20 -08:00
|
|
|
ret "__DATA,__note.rustc";
|
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "win32")) {
|
2011-03-11 15:35:20 -08:00
|
|
|
ret ".note.rustc";
|
|
|
|
}
|
|
|
|
ret ".note.rustc";
|
|
|
|
}
|
|
|
|
|
2010-12-03 13:51:46 -08:00
|
|
|
fn get_data_layout() -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "macos")) {
|
2011-05-05 12:45:34 -07:00
|
|
|
ret "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";
|
2010-12-03 13:51:46 -08:00
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "win32")) {
|
2010-12-03 13:51:46 -08:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_target_triple() -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "macos")) {
|
2010-12-03 13:51:46 -08:00
|
|
|
ret "i686-apple-darwin";
|
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::eq(target_os(), "win32")) {
|
2010-12-03 13:51:46 -08:00
|
|
|
ret "i686-pc-mingw32";
|
|
|
|
}
|
2011-05-05 17:20:27 -07:00
|
|
|
ret "i686-unknown-linux-gnu";
|
2010-12-03 13:51:46 -08:00
|
|
|
}
|
|
|
|
|
2010-09-23 17:16:34 -07:00
|
|
|
|
2010-09-23 15:46:31 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 15:07:27 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-23 15:46:31 -07:00
|
|
|
// End:
|
|
|
|
//
|