Given the file
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
I can use the commands
clang -emit-llvm hello.c -c -o hello.bc
llc hello.bc -march=mipsel -relocation-model=static -o hello.s
to produce a nice bit of what looks like MIPS code, (placed below, to avoid breaking up the text) unfortunately, when I try and run it in my trusty SPIM simulator for MIPS I find that SPIM objects to almost every line of it. Not only the '.Section .mdebug.abi32' lines but also any line of the form '.cfi*' - and even more confusingly (because it looks like MIPS to me...) the line 'lui $2, %hi(__gnu_local_gp)" is objected to.
I am looking for some information on the different flavours of MIPS that SPIM and LLVM cope with, or someone to give an example of a MIPS simulator I can run that accepts the MIPS code that LLVM is producing.
.Section .mdebug.abi32
.previous
.file "hello.bc"
.text
.globl main
.align 2
.type main,@function
.set nomips16 # @main
.ent main
main:
.cfi_startproc
.frame $sp,32,$ra
.mask 0x80000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
# BB#0: # %entry
addiu $sp, $sp, -32
$tmp2:
.cfi_def_cfa_offset 32
sw $ra, 28($sp) # 4-byte Folded Spill
$tmp3:
.cfi_offset 31, -4
lui $2, %hi(__gnu_local_gp)
addiu $2, $2, %lo(__gnu_local_gp)
sw $2, 16($sp)
sw $zero, 24($sp)
lui $2, %hi($.str)
addiu $4, $2, %lo($.str)
jal printf
nop
addiu $2, $zero, 0
lw $ra, 28($sp) # 4-byte Folded Reload
addiu $sp, $sp, 32
jr $ra
nop
.set macro
.set reorder
.end main
$tmp4:
.size main, ($tmp4)-main
.cfi_endproc
.type $.str,@object # @.str
.section .rodata.str1.1,"aMS",@progbits,1
$.str:
.asciz "hello world\n"
.size $.str, 13
Spim is a simple teaching tool which does not support gnu assembler. You might try using OVPsim, which has complete models of various real MIPS processors. You can run Linux on OVPsim, and you should be able to run a MIPS Linux executable produced by clang on that simulated Linux.
The Mips assembly printer in LLVM emits assembly in GAS format (suitable for consumption by the GNU assembler and compatible tools). There's a very good chance SPIM cannot read that. However, if SPIM could read Mips binaries, you could try to emit an object file from LLVM and let SPIM handle that.
Binaries are more "universal" since they must be understood by the CPU itself. Alas, each assembler usually has its own specific syntax only it understands, and assemblers don't tend to be compatible with each other and disagree on basic things like operator ordering, punctuation semantics, directives, and so on.
User contributions licensed under CC BY-SA 3.0