[Source Code (.cpp)] -> [clang] --.ll --> [llc] --.s --> [msp430-gcc] --> elf
brew tap wbennett/homebrew-mspgcc brew install wbennett/mspgcc/msp430-binutils brew install wbennett/mspgcc/msp430-gcc brew install wbennett/mspgcc/msp430-libc brew install wbennett/mspgcc/msp430-mcu brew install wbennett/mspgcc/msp430-gdb brew install mspdebug
$>clang -ccc-host-triple msp430-elf clang-3.5: error: unsupported option '-ccc-host-triple' clang-3.5: error: no such file or directory: 'msp430-elf' clang-3.5: error: no input files
#include "msp430.h" #include "stdlib.h" int main(void) { //disable watchdog WDCTL = WDTPW + WDTHOLD; //do something silly auto i = 1; auto result = i+1; //avoid warnings if(result == 2) return 2; //loop forever while(true){} //never reach this return 0; }
#compile application to llvm assembler ~/local/bin/clang++ -I/usr/local/Cellar/msp430-libc/20120716/msp430/include/ -D__MSP430F5438__ -g -S -emit-llvm -std=c++11 -Wall -c src/simpleadd.cpp -o src/simpleadd.ll
; ModuleID = 'src/simpleadd.cpp' target datalayout = "em:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.9.0" @"\01__WDTCTL" = external global i32 ; Function Attrs: nounwind ssp uwtable define i32 @main() #0 { %1 = alloca i32, align 4 %i = alloca i32, align 4 %result = alloca i32, align 4 store i32 0, i32* %1 store volatile i32 23168, i32* @"\01__WDTCTL", align 4 store i32 1, i32* %i, align 4 %2 = load i32* %i, align 4 %3 = add nsw i32 %2, 1 store i32 %3, i32* %result, align 4 %4 = load i32* %result, align 4 %5 = icmp eq i32 %4, 2 br i1 %5, label %6, label %7 ; <label>:6 ; preds = %0 ret i32 2 ; <label>:7 ; preds = %0 br label %8 ; <label>:8 ; preds = %7, %8 br label %8 } attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } !llvm.ident = !{!0} !0 = metadata !{metadata !"clang version 3.5 "}
$>~/local/bin/llc --help ... -mcpu=<cpu-name> - Target a specific cpu type (-mcpu=help for details) #ok more specific plz $>~/local/bin/llc -mcpu=help ... amdfam10 - Select the amdfam10 processor. athlon - Select the athlon processor. athlon-4 - Select the athlon-4 processor. athlon-fx - Select the athlon-fx processor. athlon-mp - Select the athlon-mp processor. athlon-tbird - Select the athlon-tbird processor. athlon-xp - Select the athlon-xp processor. athlon64 - Select the athlon64 processor. athlon64-sse3 - Select the athlon64-sse3 processor. ...
$>~/local/bin/llc --help ... -march=<string> - Architecture to generate code for (see --version) #ok $>~/local/bin/llc --version LLVM (http://llvm.org/): LLVM version 3.5svn DEBUG build. Built Mar 1 2014 (22:49:35). Default target: x86_64-apple-darwin13.1.0 Host CPU: core-avx-i Registered Targets: aarch64 - AArch64 (ARM 64-bit little endian target) aarch64_be - AArch64 (ARM 64-bit big endian target) arm - ARM cpp - C++ backend hexagon - Hexagon mips - Mips mips64 - Mips64 [experimental] mips64el - Mips64el [experimental] mipsel - Mipsel msp430 - MSP430 [experimental] nvptx - NVIDIA PTX 32-bit nvptx64 - NVIDIA PTX 64-bit ppc32 - PowerPC 32 ppc64 - PowerPC 64 ppc64le - PowerPC 64 LE r600 - AMD GPUs HD2XXX-HD6XXX sparc - Sparc sparcv9 - Sparc V9 systemz - SystemZ thumb - Thumb x86 - 32-bit X86: Pentium-Pro and above x86-64 - 64-bit X86: EM64T and AMD64 xcore - XCore
~/local/bin/llc -march=msp430 src/simpleadd.ll -o src/simpleadd.s
.file "src/simpleadd.ll" .text .globl main .align 2 .type main,@function main: ; @main ; BB#0: push.w r4 mov.w r1, r4 sub.w #12, r1 mov.w #0, -2(r4) mov.w #0, -4(r4) mov.w #0, &__WDTCTL+2 mov.w #23168, &__WDTCTL mov.w #0, -6(r4) mov.w #1, -8(r4) mov.w #0, -10(r4) mov.w #2, -12(r4) mov.w #0, r12 cmp.w #0, r12 jne .LBB0_2 ; BB#1: mov.w #2, r14 mov.w #0, r15 add.w #12, r1 pop.w r4 ret .LBB0_2: ; =>This Inner Loop Header: Depth=1 jmp .LBB0_2 .Ltmp0: .size main, .Ltmp0-main .ident "clang version 3.5 "
$>msp430-gcc -Wall -D_GNU_ASSEMBLER_ -I/usr/local/Cellar/msp430-libc/20120716/msp430/include/ -mmcu=msp430f5438 -mcpu=430 -x assembler -Wa,-gstabs -c src/simpleadd.s -o src/simpleadd.o #let's hint our entry point (-e main), because it's weird to be placing main in the text section #let's also generate a map to see how everything is linked $>msp430-gcc -mmcu=msp430f5438 -mcpu=430 -Wl,-Map=a.out.map src/simpleadd.o -o a.out -e main
*(.init .init.*) 791 *(.init0) 792 .init0 0x0000000000005c00 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_re# 793 0x0000000000005c00 _reset_vector__ 794 *(.init1) 795 .init1 0x0000000000005c00 0xc /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__w# 796 0x0000000000005c00 __watchdog_support 797 *(.init2) 798 .init2 0x0000000000005c0c 0x4 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__i# 799 0x0000000000005c0c __init_stack 800 *(.init3) 801 .init3 0x0000000000005c10 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__l# 802 0x0000000000005c10 __low_level_init 803 *(.init4) 804 .init4 0x0000000000005c10 0x18 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_co# 805 0x0000000000005c10 __do_copy_data 806 .init4 0x0000000000005c28 0x16 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_cl# 807 0x0000000000005c28 __do_clear_bss 808 *(.init5) 809 *(.init6) 810 *(.init7) 811 *(.init8) 812 *(.init9) 813 *(.fini9) 814 .fini9 0x0000000000005c3e 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__s# 815 0x0000000000005c3e __stop_progExec__ 816 *(.fini8) 817 *(.fini7) 818 *(.fini6) 819 *(.fini5) 820 *(.fini4) 821 *(.fini3) 822 *(.fini2) 823 *(.fini1) 824 *(.fini0) 825 .fini0 0x0000000000005c3e 0x6 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_en# 826 0x0000000000005c3e _endless_loop__ .... 869 .text.crt0 0x0000000000005c48 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/crt0ivtbl64.o 870 .text 0x0000000000005c48 0x40 src/simpleadd.o 871 0x0000000000005c48 main
... .file "src/simpleadd.ll" .text .globl main .align 2 .type main,@function main: ; @main ...
__attribute__((section(".init9")),aligned(2)).
__attribute__((section(".init9"),aligned(2))) int main(void) { ...
$>~/local/bin/clang++ -I/usr/local/Cellar/msp430-libc/20120716/msp430/include/ -D__MSP430F5438__ -S -emit-llvm -std=c++11 -Wall -c src/simpleadd.cpp -o src/simpleadd.ll ... rc/simpleadd.cpp:4:24: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a comma __attribute__((section(".init9"),aligned(2)))
$>~/local/bin/clang++ --help ... --target=<value> Generate code for the given target ...
~/local/bin/clang++ --target=help clang-3.5: error: no input files #version maybe? ~/local/bin/clang++ --version clang version 3.5 Target: x86_64-apple-darwin13.1.0 Thread model: posix
~/local/bin/clang++ -I/usr/local/Cellar/msp430-libc/20120716/msp430/include/ \ -D__MSP430F5438__ -S -emit-llvm -std=c++11 --target=msp430 \ -Wall -c src/simpleadd.cpp -o src/simpleadd.ll
; ModuleID = 'src/simpleadd.cpp' target datalayout = "em:ep:16:16-i32:16:32-n8:16" target triple = "msp430" @"\01__WDTCTL" = external global i16 ; Function Attrs: nounwind define i16 @main() #0 section ".init9" align 2 { %1 = alloca i16, align 2 %i = alloca i16, align 2 %result = alloca i16, align 2 store i16 0, i16* %1 store volatile i16 23168, i16* @"\01__WDTCTL", align 2 store i16 1, i16* %i, align 2 %2 = load i16* %i, align 2 %3 = add nsw i16 %2, 1 store i16 %3, i16* %result, align 2 %4 = load i16* %result, align 2 %5 = icmp eq i16 %4, 2 br i1 %5, label %6, label %7 ; <label>:6 ; preds = %0 ret i16 2 ; <label>:7 ; preds = %0 br label %8 ; <label>:8 ; preds = %7, %8 br label %8 } attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } !llvm.ident = !{!0} !0 = metadata !{metadata !"clang version 3.5 "}
.file "src/simpleadd.ll" .section .init9,"ax",@progbits .globl main .align 1 .type main,@function main: ; @main ; BB#0: push.w r4 mov.w r1, r4 sub.w #6, r1 mov.w #0, -2(r4) mov.w #23168, &__WDTCTL mov.w #1, -4(r4) mov.w #2, -6(r4) mov.w #2, r12 cmp.w #2, r12 jne .LBB0_2 ; BB#1: mov.w #2, r15 add.w #6, r1 pop.w r4 ret .LBB0_2: ; =>This Inner Loop Header: Depth=1 jmp .LBB0_2 .Ltmp0: .size main, .Ltmp0-main .ident "clang version 3.5 "
792 .init0 0x0000000000005c00 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_re# 793 0x0000000000005c00 _reset_vector__ 794 *(.init1) 795 .init1 0x0000000000005c00 0xc /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__w# 796 0x0000000000005c00 __watchdog_support 797 *(.init2) 798 .init2 0x0000000000005c0c 0x4 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__i# 799 0x0000000000005c0c __init_stack 800 *(.init3) 801 .init3 0x0000000000005c10 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__l# 802 0x0000000000005c10 __low_level_init 803 *(.init4) 804 .init4 0x0000000000005c10 0x18 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_co# 805 0x0000000000005c10 __do_copy_data 806 .init4 0x0000000000005c28 0x16 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(_cl# 807 0x0000000000005c28 __do_clear_bss 808 *(.init5) 809 *(.init6) 810 *(.init7) 811 *(.init8) 812 *(.init9) 813 .init9 0x0000000000005c3e 0x2c src/simpleadd.o 814 0x0000000000005c3e main 815 *(.fini9) 816 .fini9 0x0000000000005c6a 0x0 /usr/local/Cellar/msp430-gcc/4.7.0/lib/gcc/msp430/4.7.0/mmpy-16/libcrt0.a(__s# 817 0x0000000000005c6a __stop_progExec__
Source: https://habr.com/ru/post/241812/
All Articles