Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM/clang outputting to MIPS, but not working in SPIM

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
like image 573
Joe Avatar asked Nov 12 '22 01:11

Joe


1 Answers

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.

like image 123
markgz Avatar answered Dec 18 '22 06:12

markgz