Skip to content

Commit c8867f8

Browse files
authored
Auto merge of #37672 - japaric:msp430, r=alexcrichton
enable the MSP430 LLVM backend to let people experiment with this target out of tree. The MSP430 architecture is used in 16-bit microcontrollers commonly used in Digital Signal Processing applications. --- How this was tested: Declaring a custom target with the following specification: ``` json { "arch": "msp430", "data-layout": "e-m:e-p:16:16-i32:16:32-a:16-n8:16", "executables": true, "linker": "msp430-gcc", "llvm-target": "msp430", "max-atomic-width": 0, "no-integrated-as": true, "os": "none", "panic-strategy": "abort", "relocation-model": "static", "target-endian": "little", "target-pointer-width": "16" } ``` And this minimal file: ``` rust pub fn start() -> ! { loop {} } trait Copy {} trait Sized {} ``` Produces the following object files: ``` $ rustc --target=msp430 --emit=obj foo.rs $ msp430-objdump -Cd foo.o foo.o: file format elf32-msp430 Disassembly of section .text.start: 00000000 <start>: 0: 21 83 decd r1 2: 00 3c jmp $+2 ;abs 0x4 4: 00 3c jmp $+2 ;abs 0x6 6: ff 3f jmp $+0 ;abs 0x6 $ rustc --target=msp430 --emit=obj foo.rs -O $ msp430-objdump -Cd foo.o foo.o: file format elf32-msp430 Disassembly of section .text.start: 00000000 <start>: 0: ff 3f jmp $+0 ;abs 0x0 ``` --- r? @alexcrichton ~~TODO get this working with Makefiles so nightly releases include this backend~~ ~~TODO measure the increase in binary size~~ +187KiB (+0.47%) ~~FIXME --emit=obj produces empty object files~~
2 parents 0ed9519 + 80ca1e1 commit c8867f8

File tree

12 files changed

+106
-6
lines changed

12 files changed

+106
-6
lines changed

configure

+1-1
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ do
17821782
CMAKE_ARGS="$CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=ON"
17831783
fi
17841784

1785-
CMAKE_ARGS="$CMAKE_ARGS -DLLVM_TARGETS_TO_BUILD='X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend'"
1785+
CMAKE_ARGS="$CMAKE_ARGS -DLLVM_TARGETS_TO_BUILD='X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430'"
17861786
CMAKE_ARGS="$CMAKE_ARGS -G '$CFG_CMAKE_GENERATOR'"
17871787
CMAKE_ARGS="$CMAKE_ARGS $CFG_LLVM_SRC_DIR"
17881788

mk/main.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ endif
285285
# LLVM macros
286286
######################################################################
287287

288-
LLVM_OPTIONAL_COMPONENTS=x86 arm aarch64 mips powerpc pnacl systemz jsbackend
288+
LLVM_OPTIONAL_COMPONENTS=x86 arm aarch64 mips powerpc pnacl systemz jsbackend msp430
289289
LLVM_REQUIRED_COMPONENTS=ipo bitreader bitwriter linker asmparser mcjit \
290290
interpreter instrumentation
291291

src/bootstrap/native.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ pub fn llvm(build: &Build, target: &str) {
7979
.out_dir(&dst)
8080
.profile(profile)
8181
.define("LLVM_ENABLE_ASSERTIONS", assertions)
82-
.define("LLVM_TARGETS_TO_BUILD", "X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend")
82+
.define("LLVM_TARGETS_TO_BUILD",
83+
"X86;ARM;AArch64;Mips;PowerPC;SystemZ;JSBackend;MSP430")
8384
.define("LLVM_INCLUDE_EXAMPLES", "OFF")
8485
.define("LLVM_INCLUDE_TESTS", "OFF")
8586
.define("LLVM_INCLUDE_DOCS", "OFF")

src/librustc_back/target/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ pub struct TargetOptions {
359359
// will 'just work'.
360360
pub obj_is_bitcode: bool,
361361

362+
// LLVM can't produce object files for this target. Instead, we'll make LLVM
363+
// emit assembly and then use `gcc` to turn that assembly into an object
364+
// file
365+
pub no_integrated_as: bool,
366+
362367
/// Don't use this field; instead use the `.max_atomic_width()` method.
363368
pub max_atomic_width: Option<u64>,
364369

@@ -416,6 +421,7 @@ impl Default for TargetOptions {
416421
allow_asm: true,
417422
has_elf_tls: false,
418423
obj_is_bitcode: false,
424+
no_integrated_as: false,
419425
max_atomic_width: None,
420426
panic_strategy: PanicStrategy::Unwind,
421427
abi_blacklist: vec![],
@@ -576,6 +582,7 @@ impl Target {
576582
key!(exe_allocation_crate);
577583
key!(has_elf_tls, bool);
578584
key!(obj_is_bitcode, bool);
585+
key!(no_integrated_as, bool);
579586
key!(max_atomic_width, Option<u64>);
580587
try!(key!(panic_strategy, PanicStrategy));
581588

@@ -735,6 +742,7 @@ impl ToJson for Target {
735742
target_option_val!(exe_allocation_crate);
736743
target_option_val!(has_elf_tls);
737744
target_option_val!(obj_is_bitcode);
745+
target_option_val!(no_integrated_as);
738746
target_option_val!(max_atomic_width);
739747
target_option_val!(panic_strategy);
740748

src/librustc_driver/driver.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1056,14 +1056,29 @@ pub fn phase_4_translate_to_llvm<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10561056
pub fn phase_5_run_llvm_passes(sess: &Session,
10571057
trans: &trans::CrateTranslation,
10581058
outputs: &OutputFilenames) -> CompileResult {
1059-
if sess.opts.cg.no_integrated_as {
1059+
if sess.opts.cg.no_integrated_as ||
1060+
(sess.target.target.options.no_integrated_as &&
1061+
(outputs.outputs.contains_key(&OutputType::Object) ||
1062+
outputs.outputs.contains_key(&OutputType::Exe)))
1063+
{
10601064
let output_types = OutputTypes::new(&[(OutputType::Assembly, None)]);
10611065
time(sess.time_passes(),
10621066
"LLVM passes",
10631067
|| write::run_passes(sess, trans, &output_types, outputs));
10641068

10651069
write::run_assembler(sess, outputs);
10661070

1071+
// HACK the linker expects the object file to be named foo.0.o but
1072+
// `run_assembler` produces an object named just foo.o. Rename it if we
1073+
// are going to build an executable
1074+
if sess.opts.output_types.contains_key(&OutputType::Exe) {
1075+
let f = outputs.path(OutputType::Object);
1076+
fs::copy(&f,
1077+
f.with_file_name(format!("{}.0.o",
1078+
f.file_stem().unwrap().to_string_lossy()))).unwrap();
1079+
fs::remove_file(f).unwrap();
1080+
}
1081+
10671082
// Remove assembly source, unless --save-temps was specified
10681083
if !sess.opts.cg.save_temps {
10691084
fs::remove_file(&outputs.temp_path(OutputType::Assembly, None)).unwrap();

src/librustc_llvm/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main() {
6666
let is_crossed = target != host;
6767

6868
let optional_components =
69-
["x86", "arm", "aarch64", "mips", "powerpc", "pnacl", "systemz", "jsbackend"];
69+
["x86", "arm", "aarch64", "mips", "powerpc", "pnacl", "systemz", "jsbackend", "msp430"];
7070

7171
// FIXME: surely we don't need all these components, right? Stuff like mcjit
7272
// or interpreter the compiler itself never uses.

src/librustc_llvm/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ pub fn initialize_available_targets() {
413413
LLVMInitializeJSBackendTargetInfo,
414414
LLVMInitializeJSBackendTarget,
415415
LLVMInitializeJSBackendTargetMC);
416+
init_target!(llvm_component = "msp430",
417+
LLVMInitializeMSP430TargetInfo,
418+
LLVMInitializeMSP430Target,
419+
LLVMInitializeMSP430TargetMC,
420+
LLVMInitializeMSP430AsmPrinter);
416421
}
417422

418423
pub fn last_error() -> Option<String> {

src/librustc_trans/abi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use cabi_s390x;
2424
use cabi_mips;
2525
use cabi_mips64;
2626
use cabi_asmjs;
27+
use cabi_msp430;
2728
use machine::{llalign_of_min, llsize_of, llsize_of_alloc};
2829
use type_::Type;
2930
use type_of;
@@ -520,6 +521,7 @@ impl FnType {
520521
"s390x" => cabi_s390x::compute_abi_info(ccx, self),
521522
"asmjs" => cabi_asmjs::compute_abi_info(ccx, self),
522523
"wasm32" => cabi_asmjs::compute_abi_info(ccx, self),
524+
"msp430" => cabi_msp430::compute_abi_info(ccx, self),
523525
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a))
524526
}
525527

src/librustc_trans/cabi_msp430.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Reference: MSP430 Embedded Application Binary Interface
12+
// http://www.ti.com/lit/an/slaa534/slaa534.pdf
13+
14+
#![allow(non_upper_case_globals)]
15+
16+
use llvm::Struct;
17+
18+
use abi::{self, ArgType, FnType};
19+
use context::CrateContext;
20+
use type_::Type;
21+
22+
fn ty_size(ty: Type) -> usize {
23+
abi::ty_size(ty, 2)
24+
}
25+
26+
// 3.5 Structures or Unions Passed and Returned by Reference
27+
//
28+
// "Structures (including classes) and unions larger than 32 bits are passed and
29+
// returned by reference. To pass a structure or union by reference, the caller
30+
// places its address in the appropriate location: either in a register or on
31+
// the stack, according to its position in the argument list. (..)"
32+
fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {
33+
if ret.ty.kind() == Struct && ty_size(ret.ty) > 32 {
34+
ret.make_indirect(ccx);
35+
} else {
36+
ret.extend_integer_width_to(16);
37+
}
38+
}
39+
40+
fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) {
41+
if arg.ty.kind() == Struct && ty_size(arg.ty) > 32 {
42+
arg.make_indirect(ccx);
43+
} else {
44+
arg.extend_integer_width_to(16);
45+
}
46+
}
47+
48+
pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) {
49+
if !fty.ret.is_ignore() {
50+
classify_ret_ty(ccx, &mut fty.ret);
51+
}
52+
53+
for arg in &mut fty.args {
54+
if arg.is_ignore() {
55+
continue;
56+
}
57+
classify_arg_ty(ccx, arg);
58+
}
59+
}

src/librustc_trans/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ mod cabi_arm;
101101
mod cabi_asmjs;
102102
mod cabi_mips;
103103
mod cabi_mips64;
104+
mod cabi_msp430;
104105
mod cabi_powerpc;
105106
mod cabi_powerpc64;
106107
mod cabi_s390x;

src/librustc_trans/mir/rvalue.rs

+2
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,13 @@ fn get_overflow_intrinsic(oop: OverflowOp, bcx: &BlockAndBuilder, ty: Ty) -> Val
720720

721721
let new_sty = match ty.sty {
722722
TyInt(Is) => match &tcx.sess.target.target.target_pointer_width[..] {
723+
"16" => TyInt(I16),
723724
"32" => TyInt(I32),
724725
"64" => TyInt(I64),
725726
_ => panic!("unsupported target word size")
726727
},
727728
TyUint(Us) => match &tcx.sess.target.target.target_pointer_width[..] {
729+
"16" => TyUint(U16),
728730
"32" => TyUint(U32),
729731
"64" => TyUint(U64),
730732
_ => panic!("unsupported target word size")

src/rustllvm/PassWrapper.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,20 @@ LLVMRustAddPass(LLVMPassManagerRef PM, LLVMPassRef rust_pass) {
137137
#define SUBTARGET_SYSTEMZ
138138
#endif
139139

140+
#ifdef LLVM_COMPONENT_MSP430
141+
#define SUBTARGET_MSP430 SUBTARGET(MSP430)
142+
#else
143+
#define SUBTARGET_MSP430
144+
#endif
145+
140146
#define GEN_SUBTARGETS \
141147
SUBTARGET_X86 \
142148
SUBTARGET_ARM \
143149
SUBTARGET_AARCH64 \
144150
SUBTARGET_MIPS \
145151
SUBTARGET_PPC \
146-
SUBTARGET_SYSTEMZ
152+
SUBTARGET_SYSTEMZ \
153+
SUBTARGET_MSP430
147154

148155
#define SUBTARGET(x) namespace llvm { \
149156
extern const SubtargetFeatureKV x##FeatureKV[]; \

0 commit comments

Comments
 (0)